From a60f0a9dda0b20045059f8e8fcf399df5915b7f5 Mon Sep 17 00:00:00 2001 From: Alexandre SPANGARO Date: Mon, 8 Dec 2025 09:56:42 +0100 Subject: [PATCH 1/3] FIX Accountancy - Problem calculating exercises with different tax periods on fixed assets --- htdocs/asset/class/asset.class.php | 27 +++++++++++++++++++----- htdocs/core/lib/accounting.lib.php | 34 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/htdocs/asset/class/asset.class.php b/htdocs/asset/class/asset.class.php index 6cb18a28394..5f9f9a3dec4 100644 --- a/htdocs/asset/class/asset.class.php +++ b/htdocs/asset/class/asset.class.php @@ -1184,13 +1184,30 @@ class Asset extends CommonObject // Next fiscal period (+1 day/month/year) $fiscal_period_start = dol_time_plus_duree($fiscal_period_end, 1, 'd'); - $dates_fiscal_period = getCurrentPeriodOfFiscalYear($this->db, $conf, $fiscal_period_start, 'gmt'); - if ($fields['duration_type'] == 2) { // Daily - $fiscal_period_end = $fiscal_period_start; + + if ($fields['duration_type'] == 0) { // Annually - use fiscal year end + // Search for the NEXT fiscal year + $next_fy = getNextFiscalYear($this->db, $conf, $fiscal_period_end, 'gmt'); + + if ($next_fy !== null) { + // Exercise found in database + $fiscal_period_start = $next_fy['date_start']; + $fiscal_period_end = $next_fy['date_end']; + } else { + // If there is no subsequent fiscal year in the database, a one-year fiscal year is automatically generated. + // based on the structure of the current fiscal year + $current_fy_start_parts = dol_getdate($fiscal_period_start); + + // Calculate the end: 1 year minus 1 day from the start + $fiscal_period_end = dol_time_plus_duree(dol_time_plus_duree($fiscal_period_start, 1, 'y'), -1, 'd'); + + // If the amortization end date is exceeded, there is no need to continue after that. + // The loop will end naturally with the while condition. + } } elseif ($fields['duration_type'] == 1) { // Monthly $fiscal_period_end = dol_time_plus_duree(dol_time_plus_duree($fiscal_period_start, 1, 'm'), -1, 'd'); - } else { // Annually - $fiscal_period_end = $dates_fiscal_period['date_end']; + } else { // Daily + $fiscal_period_end = $fiscal_period_start; } $last_period_date = $disposal_date !== "" && $disposal_date < $depreciation_date_end ? $disposal_date : $depreciation_date_end; } while ($fiscal_period_start < $last_period_date); diff --git a/htdocs/core/lib/accounting.lib.php b/htdocs/core/lib/accounting.lib.php index c73f4b8b844..ab4489e66cf 100644 --- a/htdocs/core/lib/accounting.lib.php +++ b/htdocs/core/lib/accounting.lib.php @@ -410,3 +410,37 @@ function getCurrentPeriodOfFiscalYear($db, $conf, $from_time = null, $gm = 'tzse 'date_end' => $date_end, ); } + +/** + * Get next fiscal year period after a given date + * + * @param DoliDB $db Database handler + * @param int $after_date Get fiscal period that starts after this date + * @param 'tzserver'|'gmt' $gm 'gmt' => we return GMT timestamp, 'tzserver' => PHP server timezone + * @return array{date_start:int,date_end:int}|null Period of next fiscal year or null if not found + */ +function getNextFiscalYear($db, $after_date, $gm = 'tzserver') +{ + // Find the fiscal year that starts strictly after the given date + $sql = "SELECT date_start, date_end FROM ".$db->prefix()."accounting_fiscalyear"; + $sql .= " WHERE date_start > '".$db->idate($after_date, $gm)."'"; + $sql .= " AND entity IN (".getEntity('accounting_fiscalyear').")"; + $sql .= $db->order('date_start', 'ASC'); // Get the first one (earliest) + $sql .= $db->plimit(1); + + $res = $db->query($sql); + + if ($res && $db->num_rows($res) > 0) { + $obj = $db->fetch_object($res); + + $date_start = $db->jdate($obj->date_start, $gm); + $date_end = $db->jdate($obj->date_end, $gm); // Sans dol_get_last_hour pour éviter le bug + + return array( + 'date_start' => $date_start, + 'date_end' => $date_end, + ); + } + + return null; // No next fiscal year found +} From 2abd7a561ffe69ad1a0c84b3331879ef281ad4e9 Mon Sep 17 00:00:00 2001 From: Alexandre SPANGARO Date: Mon, 8 Dec 2025 10:10:39 +0100 Subject: [PATCH 2/3] CI --- htdocs/asset/class/asset.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/asset/class/asset.class.php b/htdocs/asset/class/asset.class.php index 5f9f9a3dec4..20428ce9661 100644 --- a/htdocs/asset/class/asset.class.php +++ b/htdocs/asset/class/asset.class.php @@ -1185,9 +1185,9 @@ class Asset extends CommonObject // Next fiscal period (+1 day/month/year) $fiscal_period_start = dol_time_plus_duree($fiscal_period_end, 1, 'd'); - if ($fields['duration_type'] == 0) { // Annually - use fiscal year end + if ($fields['duration_type'] == 0) { // Annually - use fiscal year-end // Search for the NEXT fiscal year - $next_fy = getNextFiscalYear($this->db, $conf, $fiscal_period_end, 'gmt'); + $next_fy = getNextFiscalYear($this->db, $fiscal_period_end, 'gmt'); if ($next_fy !== null) { // Exercise found in database From 75b357ff70756e7d74f0b698b52651e03635800e Mon Sep 17 00:00:00 2001 From: Alexandre SPANGARO Date: Mon, 8 Dec 2025 10:12:02 +0100 Subject: [PATCH 3/3] Translate --- htdocs/core/lib/accounting.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/lib/accounting.lib.php b/htdocs/core/lib/accounting.lib.php index ab4489e66cf..ecf15e33f82 100644 --- a/htdocs/core/lib/accounting.lib.php +++ b/htdocs/core/lib/accounting.lib.php @@ -434,7 +434,7 @@ function getNextFiscalYear($db, $after_date, $gm = 'tzserver') $obj = $db->fetch_object($res); $date_start = $db->jdate($obj->date_start, $gm); - $date_end = $db->jdate($obj->date_end, $gm); // Sans dol_get_last_hour pour éviter le bug + $date_end = $db->jdate($obj->date_end, $gm); // Without dol_get_last_hour to avoid the bug return array( 'date_start' => $date_start,