Merge pull request #36572 from aspangaro/22_asset5
FIX Accountancy - Problem calculating fiscal year with different periods on fixed assets
This commit is contained in:
commit
45c4f4fa3c
2 changed files with 56 additions and 5 deletions
|
|
@ -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, $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);
|
||||
|
|
|
|||
|
|
@ -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); // Without dol_get_last_hour to avoid the bug
|
||||
|
||||
return array(
|
||||
'date_start' => $date_start,
|
||||
'date_end' => $date_end,
|
||||
);
|
||||
}
|
||||
|
||||
return null; // No next fiscal year found
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue