Fix to create exactly 2 lines with correct amounts

- Changed difference calculation back to (debits - credits)
- Creates exactly 2 lines: main amount + rounding difference
- For difference 69.90: 4455100 credit 70.00 + 758000 credit 0.90
- Removed complex logic, simplified to 2 entries only
This commit is contained in:
Frank Cools 2025-10-07 14:56:44 +02:00
parent 93830f309f
commit da6bb79204

View File

@ -2062,7 +2062,7 @@ class DeclarationTVA_PDF
} }
} }
$difference = $total_credits - $total_debits; $difference = $total_debits - $total_credits;
// 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) {
@ -2077,42 +2077,38 @@ class DeclarationTVA_PDF
error_log("DeclarationTVA: Total difference: " . $difference); error_log("DeclarationTVA: Total difference: " . $difference);
error_log("DeclarationTVA: Difference > 0: " . ($difference > 0 ? 'true' : 'false')); error_log("DeclarationTVA: Difference > 0: " . ($difference > 0 ? 'true' : 'false'));
// Split into main entry (rounded) and rounding entry (real difference) // Create exactly 2 lines: main amount + rounding difference
if (abs($difference) >= 0.01) { if (abs($difference) >= 0.01) {
$rounded_amount = round($difference); $rounded_amount = round($difference);
$rounding_diff = $difference - $rounded_amount; $rounding_diff = $difference - $rounded_amount;
error_log("DeclarationTVA: Difference: " . $difference . ", Rounded: " . $rounded_amount . ", Rounding diff: " . $rounding_diff); error_log("DeclarationTVA: Difference: " . $difference . ", Rounded: " . $rounded_amount . ", Rounding diff: " . $rounding_diff);
// Main balancing entry with rounded amount // Main entry with rounded amount
if ($difference > 0) { if ($difference > 0) {
// More credits than debits - need debit entry (4456700) // Credit difference - main entry on 4456700 debit
if ($rounded_amount != 0) { $balancing_entries[] = array(
$balancing_entries[] = array( 'account_code' => '4456700',
'account_code' => '4456700', 'account_label' => $this->getAccountLabel('4456700'),
'account_label' => $this->getAccountLabel('4456700'), 'entry_label' => $declaration->declaration_name,
'entry_label' => $declaration->declaration_name, 'debit' => $this->formatAmount($rounded_amount),
'debit' => $this->formatAmount($rounded_amount), 'credit' => ''
'credit' => '' );
);
}
} else { } else {
// More debits than credits - need credit entry (4455100) // Debit difference - main entry on 4455100 credit
if ($rounded_amount != 0) { $balancing_entries[] = array(
$balancing_entries[] = array( 'account_code' => '4455100',
'account_code' => '4455100', 'account_label' => $this->getAccountLabel('4455100'),
'account_label' => $this->getAccountLabel('4455100'), 'entry_label' => $declaration->declaration_name,
'entry_label' => $declaration->declaration_name, 'debit' => '',
'debit' => '', 'credit' => $this->formatAmount(abs($rounded_amount))
'credit' => $this->formatAmount(abs($rounded_amount)) );
);
}
} }
// Rounding entry for the real difference // Rounding entry with real difference
if (abs($rounding_diff) >= 0.01) { if (abs($rounding_diff) >= 0.01) {
if ($difference > 0) { if ($difference > 0) {
// Credit difference - rounding goes to 658000 debit // Credit difference - rounding on 658000 debit
$balancing_entries[] = array( $balancing_entries[] = array(
'account_code' => '658000', 'account_code' => '658000',
'account_label' => $this->getAccountLabel('658000'), 'account_label' => $this->getAccountLabel('658000'),
@ -2121,7 +2117,7 @@ class DeclarationTVA_PDF
'credit' => '' 'credit' => ''
); );
} else { } else {
// Debit difference - rounding goes to 758000 credit // Debit difference - rounding on 758000 credit
$balancing_entries[] = array( $balancing_entries[] = array(
'account_code' => '758000', 'account_code' => '758000',
'account_label' => $this->getAccountLabel('758000'), 'account_label' => $this->getAccountLabel('758000'),