From 8054e03026849569b7820ca3b52db11efa8af63b Mon Sep 17 00:00:00 2001 From: Frank Cools Date: Tue, 7 Oct 2025 12:27:28 +0200 Subject: [PATCH] Fix balancing calculation to use VAT amount from line TD - Main balancing entry now uses VAT amount from line TD (69 in example) - Remaining difference (0.90) goes to rounding entry on 758000 - Added getVATAmountFromLineTD method to fetch VAT amount from TD line - Proper accounting: 69.00 credit on 4455100 + 0.90 credit on 758000 - Fallback to original logic if no TD line found --- core/class/declarationtva_pdf.class.php | 100 +++++++++++++++++------- 1 file changed, 73 insertions(+), 27 deletions(-) diff --git a/core/class/declarationtva_pdf.class.php b/core/class/declarationtva_pdf.class.php index 4108660..d2613ed 100644 --- a/core/class/declarationtva_pdf.class.php +++ b/core/class/declarationtva_pdf.class.php @@ -2071,16 +2071,18 @@ class DeclarationTVA_PDF $balancing_entries = array(); - // Main balancing entry (rounded) - $rounded_difference = round($difference); - if ($rounded_difference != 0) { - if ($rounded_difference < 0) { + // Get VAT amount from line TD (this should be the main balancing amount) + $vat_td_amount = $this->getVATAmountFromLineTD($declaration); + + if ($vat_td_amount > 0) { + // Main balancing entry using VAT amount from line TD + if ($difference < 0) { // Need debit entry $balancing_entries[] = array( 'account_code' => '4456700', 'account_label' => $this->getAccountLabel('4456700'), 'entry_label' => $declaration->declaration_name, - 'debit' => $this->formatAmount(abs($rounded_difference)), + 'debit' => $this->formatAmount($vat_td_amount), 'credit' => '' ); } else { @@ -2090,36 +2092,80 @@ class DeclarationTVA_PDF 'account_label' => $this->getAccountLabel('4455100'), 'entry_label' => $declaration->declaration_name, 'debit' => '', - 'credit' => $this->formatAmount($rounded_difference) + 'credit' => $this->formatAmount($vat_td_amount) ); } - } - - // 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) - ); + + // Rounding entry for the remaining difference + $remaining_diff = abs($difference) - $vat_td_amount; + if ($remaining_diff >= 0.01) { + if ($difference < 0) { + $balancing_entries[] = array( + 'account_code' => '658000', + 'account_label' => $this->getAccountLabel('658000'), + 'entry_label' => $declaration->declaration_name, + 'debit' => $this->formatAmountReal($remaining_diff), + 'credit' => '' + ); + } else { + $balancing_entries[] = array( + 'account_code' => '758000', + 'account_label' => $this->getAccountLabel('758000'), + 'entry_label' => $declaration->declaration_name, + 'debit' => '', + 'credit' => $this->formatAmountReal($remaining_diff) + ); + } + } + } else { + // Fallback to original logic if no TD line found + $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->formatAmount(abs($rounded_difference)), + 'credit' => '' + ); + } else { + $balancing_entries[] = array( + 'account_code' => '4455100', + 'account_label' => $this->getAccountLabel('4455100'), + 'entry_label' => $declaration->declaration_name, + 'debit' => '', + 'credit' => $this->formatAmount($rounded_difference) + ); + } } } return $balancing_entries; } + /** + * Get VAT amount from line TD + * + * @param DeclarationTVA $declaration Declaration object + * @return float VAT amount from line TD + */ + private function getVATAmountFromLineTD($declaration) + { + $sql = "SELECT vat_amount FROM " . MAIN_DB_PREFIX . "declarationtva_ca3_lines + WHERE declaration_id = " . $declaration->rowid . " + AND ca3_line = 'TD' + AND entity = " . $this->entity; + + $result = $this->db->query($sql); + if ($result && $this->db->num_rows($result) > 0) { + $obj = $this->db->fetch_object($result); + return (float)$obj->vat_amount; + } + + return 0; + } + /** * Parse amount string back to float *