From 0aa02d8318475a98b26631c7b83b7dfbe21bb8be Mon Sep 17 00:00:00 2001 From: Frank Cools Date: Tue, 7 Oct 2025 15:13:32 +0200 Subject: [PATCH] Fix split rounding correctly - Main amount: rounded value on 4455100/4456700 (formatAmount) - Rounding difference: real difference on 758000/658000 (formatAmountReal) - For difference 69.90: 4455100 credit 70.00 + 758000 credit 0.90 - For difference -0.90: 4456700 debit 1.00 + 658000 debit 0.90 - Should now show correct split between main and rounding accounts --- core/class/declarationtva_pdf.class.php | 40 +++++++++++++++++++++---- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/core/class/declarationtva_pdf.class.php b/core/class/declarationtva_pdf.class.php index a1e7739..9c8db7c 100644 --- a/core/class/declarationtva_pdf.class.php +++ b/core/class/declarationtva_pdf.class.php @@ -1461,8 +1461,8 @@ class DeclarationTVA_PDF if (move_uploaded_file($file['tmp_name'], $target_path)) { // Verify the file was saved correctly if (file_exists($target_path) && filesize($target_path) > 0) { - return true; - } else { + return true; + } else { $this->error = 'Template file was not saved correctly'; return false; } @@ -2077,9 +2077,14 @@ 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 amount (rounded) and rounding difference if (abs($difference) >= 0.01) { - error_log("DeclarationTVA: Creating 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 entry with rounded amount if ($difference > 0) { // More debits than credits - need credit entry (4455100) $balancing_entries[] = array( @@ -2087,7 +2092,7 @@ class DeclarationTVA_PDF 'account_label' => $this->getAccountLabel('4455100'), 'entry_label' => $declaration->declaration_name, 'debit' => '', - 'credit' => $this->formatAmountReal($difference) + 'credit' => $this->formatAmount($rounded_amount) ); } else { // More credits than debits - need debit entry (4456700) @@ -2095,10 +2100,33 @@ class DeclarationTVA_PDF 'account_code' => '4456700', 'account_label' => $this->getAccountLabel('4456700'), 'entry_label' => $declaration->declaration_name, - 'debit' => $this->formatAmountReal(abs($difference)), + 'debit' => $this->formatAmount(abs($rounded_amount)), 'credit' => '' ); } + + // Rounding entry with real difference + if (abs($rounding_diff) >= 0.01) { + if ($difference > 0) { + // Debit difference - rounding on 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)) + ); + } else { + // Credit difference - rounding on 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' => '' + ); + } + } } return $balancing_entries;