Fix balancing structure to create correct journal entries

- Combined balancing and rounding logic into single method
- Main balancing entry uses correct account: 4456700 for debit, 4455100 for credit
- Separate rounding entry uses 658000/758000 for rounding differences
- Eliminates duplicate entries and ensures proper accounting structure
- Example: 69.00 credit on 4455100 + 0.90 on 758000 instead of two separate lines
This commit is contained in:
Frank Cools 2025-10-07 12:14:46 +02:00
parent 4fd0e13198
commit 295f264090

View File

@ -1863,14 +1863,10 @@ class DeclarationTVA_PDF
} }
// Add balancing entry to ensure debits equal credits // Add balancing entry to ensure debits equal credits
$balancing_entry = $this->getBalancingEntry($declaration, $entries); $balancing_entries = $this->getBalancingEntries($declaration, $entries);
if ($balancing_entry) { foreach ($balancing_entries as $entry) {
$entries[] = $balancing_entry; if ($entry) {
$entries[] = $entry;
// Add rounding entry for the balancing difference
$balancing_rounding_entry = $this->getBalancingRoundingEntry($declaration, $entries);
if ($balancing_rounding_entry) {
$entries[] = $balancing_rounding_entry;
} }
} }
@ -2045,13 +2041,13 @@ class DeclarationTVA_PDF
} }
/** /**
* Get balancing entry to ensure debits equal credits * Get balancing entries (main balancing + rounding)
* *
* @param DeclarationTVA $declaration Declaration object * @param DeclarationTVA $declaration Declaration object
* @param array $entries Current journal entries * @param array $entries Current journal entries
* @return array|null Journal entry * @return array Array of balancing entries
*/ */
private function getBalancingEntry($declaration, $entries) private function getBalancingEntries($declaration, $entries)
{ {
$total_debits = 0; $total_debits = 0;
$total_credits = 0; $total_credits = 0;
@ -2070,30 +2066,58 @@ class DeclarationTVA_PDF
// If difference is very small (less than 0.01), no balancing entry needed // If difference is very small (less than 0.01), no balancing entry needed
if (abs($difference) < 0.01) { if (abs($difference) < 0.01) {
return null; return array();
} }
if ($difference < 0) { $balancing_entries = array();
// More credits than debits, add debit to balance
$entry = array( // Main balancing entry (rounded)
'account_code' => '4456700', // Debit entry uses 4456700 $rounded_difference = round($difference);
'account_label' => $this->getAccountLabel('4456700'), if ($rounded_difference != 0) {
'entry_label' => $declaration->declaration_name, if ($rounded_difference < 0) {
'debit' => $this->formatAmount(round(abs($difference))), // Need debit entry
'credit' => '' $balancing_entries[] = array(
); 'account_code' => '4456700',
} else { 'account_label' => $this->getAccountLabel('4456700'),
// More debits than credits, add credit to balance 'entry_label' => $declaration->declaration_name,
$entry = array( 'debit' => $this->formatAmount(abs($rounded_difference)),
'account_code' => '4455100', // Credit entry uses 4455100 'credit' => ''
'account_label' => $this->getAccountLabel('4455100'), );
'entry_label' => $declaration->declaration_name, } else {
'debit' => '', // Need credit entry
'credit' => $this->formatAmount(round($difference)) $balancing_entries[] = array(
); 'account_code' => '4455100',
'account_label' => $this->getAccountLabel('4455100'),
'entry_label' => $declaration->declaration_name,
'debit' => '',
'credit' => $this->formatAmount($rounded_difference)
);
}
} }
return $entry; // Rounding entry for the difference between real and rounded
$rounding_diff = $difference - $rounded_difference;
if (abs($rounding_diff) >= 0.01) {
if ($rounding_diff < 0) {
$balancing_entries[] = array(
'account_code' => '658000',
'account_label' => $this->getAccountLabel('658000'),
'entry_label' => $declaration->declaration_name,
'debit' => $this->formatAmountReal(abs($rounding_diff)),
'credit' => ''
);
} else {
$balancing_entries[] = array(
'account_code' => '758000',
'account_label' => $this->getAccountLabel('758000'),
'entry_label' => $declaration->declaration_name,
'debit' => '',
'credit' => $this->formatAmountReal($rounding_diff)
);
}
}
return $balancing_entries;
} }
/** /**
@ -2155,59 +2179,4 @@ class DeclarationTVA_PDF
return 'Compte ' . $account_code; return 'Compte ' . $account_code;
} }
/**
* Get balancing rounding entry for the difference between real and rounded amounts
*
* @param DeclarationTVA $declaration Declaration object
* @param array $entries Current journal entries
* @return array|null Journal entry
*/
private function getBalancingRoundingEntry($declaration, $entries)
{
$total_debits = 0;
$total_credits = 0;
$total_debits_rounded = 0;
$total_credits_rounded = 0;
// Calculate totals from existing entries (real values)
foreach ($entries as $entry) {
if (!empty($entry['debit'])) {
$total_debits += $this->parseAmount($entry['debit']);
$total_debits_rounded += round($this->parseAmount($entry['debit']));
}
if (!empty($entry['credit'])) {
$total_credits += $this->parseAmount($entry['credit']);
$total_credits_rounded += round($this->parseAmount($entry['credit']));
}
}
$real_difference = $total_debits - $total_credits;
$rounded_difference = $total_debits_rounded - $total_credits_rounded;
$rounding_diff = $real_difference - $rounded_difference;
// If rounding difference is very small, no entry needed
if (abs($rounding_diff) < 0.01) {
return null;
}
$entry = array(
'account_code' => '',
'account_label' => '',
'entry_label' => $declaration->declaration_name,
'debit' => '',
'credit' => ''
);
if ($rounding_diff < 0) {
$entry['account_code'] = '658000';
$entry['account_label'] = $this->getAccountLabel('658000');
$entry['debit'] = $this->formatAmountReal(abs($rounding_diff));
} else {
$entry['account_code'] = '758000';
$entry['account_label'] = $this->getAccountLabel('758000');
$entry['credit'] = $this->formatAmountReal($rounding_diff);
}
return $entry;
}
} }