From 93830f309fc4032ed6c36657aa507f5550d4d642 Mon Sep 17 00:00:00 2001 From: Frank Cools Date: Tue, 7 Oct 2025 14:53:46 +0200 Subject: [PATCH] Fix split entries for proper balancing - Main entry: rounded amount on 4455100/4456700 (formatAmount) - Rounding entry: real difference on 758000/658000 (formatAmountReal) - For difference -0.90: 4455100 credit 1.00 + 758000 credit 0.90 - Should now show correct split between main and rounding accounts --- core/class/declarationtva_pdf.class.php | 64 ++++++++++++++++++------- 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/core/class/declarationtva_pdf.class.php b/core/class/declarationtva_pdf.class.php index b8119fa..2db2307 100644 --- a/core/class/declarationtva_pdf.class.php +++ b/core/class/declarationtva_pdf.class.php @@ -2077,27 +2077,59 @@ class DeclarationTVA_PDF error_log("DeclarationTVA: Total difference: " . $difference); error_log("DeclarationTVA: Difference > 0: " . ($difference > 0 ? 'true' : 'false')); - // Use real difference, no rounding + // Split into main entry (rounded) and rounding entry (real difference) if (abs($difference) >= 0.01) { - error_log("DeclarationTVA: Creating balancing entry for real difference: " . $difference); + $rounded_amount = round($difference); + $rounding_diff = $difference - $rounded_amount; + + error_log("DeclarationTVA: Difference: " . $difference . ", Rounded: " . $rounded_amount . ", Rounding diff: " . $rounding_diff); + + // Main balancing entry with rounded amount if ($difference > 0) { // More credits than debits - need debit entry (4456700) - $balancing_entries[] = array( - 'account_code' => '4456700', - 'account_label' => $this->getAccountLabel('4456700'), - 'entry_label' => $declaration->declaration_name, - 'debit' => $this->formatAmountReal($difference), - 'credit' => '' - ); + if ($rounded_amount != 0) { + $balancing_entries[] = array( + 'account_code' => '4456700', + 'account_label' => $this->getAccountLabel('4456700'), + 'entry_label' => $declaration->declaration_name, + 'debit' => $this->formatAmount($rounded_amount), + 'credit' => '' + ); + } } else { // More debits than credits - need credit entry (4455100) - $balancing_entries[] = array( - 'account_code' => '4455100', - 'account_label' => $this->getAccountLabel('4455100'), - 'entry_label' => $declaration->declaration_name, - 'debit' => '', - 'credit' => $this->formatAmountReal(abs($difference)) - ); + if ($rounded_amount != 0) { + $balancing_entries[] = array( + 'account_code' => '4455100', + 'account_label' => $this->getAccountLabel('4455100'), + 'entry_label' => $declaration->declaration_name, + 'debit' => '', + 'credit' => $this->formatAmount(abs($rounded_amount)) + ); + } + } + + // Rounding entry for the real difference + if (abs($rounding_diff) >= 0.01) { + if ($difference > 0) { + // Credit difference - rounding goes to 658000 debit + $balancing_entries[] = array( + 'account_code' => '658000', + 'account_label' => $this->getAccountLabel('658000'), + 'entry_label' => $declaration->declaration_name, + 'debit' => $this->formatAmountReal(abs($rounding_diff)), + 'credit' => '' + ); + } else { + // Debit difference - rounding goes to 758000 credit + $balancing_entries[] = array( + 'account_code' => '758000', + 'account_label' => $this->getAccountLabel('758000'), + 'entry_label' => $declaration->declaration_name, + 'debit' => '', + 'credit' => $this->formatAmountReal(abs($rounding_diff)) + ); + } } }