From ffe8a960e20a345773ec47306e9c622db943218f Mon Sep 17 00:00:00 2001 From: Frank Cools Date: Tue, 7 Oct 2025 12:44:19 +0200 Subject: [PATCH] Fix VAT credit scenario balancing logic - Main balancing entry now uses rounded amount (formatAmount) for proper accounting - Rounding difference goes to 658000/758000 with real value (formatAmountReal) - Fixed fallback logic to handle both VAT debit and credit scenarios correctly - VAT credit: rounded amount on 4456700, real difference on 658000 - VAT debit: rounded amount on 4455100, real difference on 758000 --- core/class/declarationtva_pdf.class.php | 37 +++++++++++++++++++++---- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/core/class/declarationtva_pdf.class.php b/core/class/declarationtva_pdf.class.php index ba51c56..11c6d3c 100644 --- a/core/class/declarationtva_pdf.class.php +++ b/core/class/declarationtva_pdf.class.php @@ -2124,15 +2124,18 @@ class DeclarationTVA_PDF } } } else { - // Fallback to original logic if no TD line found - use real value, not rounded - error_log("DeclarationTVA: No TD line found, using fallback logic with real value"); - if (abs($difference) >= 0.01) { - if ($difference < 0) { + // Fallback to original logic if no TD line found + error_log("DeclarationTVA: No TD line found, using fallback logic"); + + // Use rounded amount for main balancing entry + $rounded_difference = round($difference); + if ($rounded_difference != 0) { + if ($rounded_difference < 0) { $balancing_entries[] = array( 'account_code' => '4456700', 'account_label' => $this->getAccountLabel('4456700'), 'entry_label' => $declaration->declaration_name, - 'debit' => $this->formatAmountReal(abs($difference)), + 'debit' => $this->formatAmount(abs($rounded_difference)), 'credit' => '' ); } else { @@ -2141,9 +2144,31 @@ class DeclarationTVA_PDF 'account_label' => $this->getAccountLabel('4455100'), 'entry_label' => $declaration->declaration_name, 'debit' => '', - 'credit' => $this->formatAmountReal($difference) + 'credit' => $this->formatAmount($rounded_difference) ); } + + // Add 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) + ); + } + } } }