From cf3eb9d35928f8b1cf143ab34990a4b388c14055 Mon Sep 17 00:00:00 2001 From: Frank Cools Date: Tue, 7 Oct 2025 15:42:32 +0200 Subject: [PATCH] Fix journal entries to use TD line calculation - Now uses Line 16 - Line 23 calculation (same as view page) - Added getLineAmount method to get line amounts from database - Uses TD amount as main balancing entry instead of journal difference - Should handle both positive and negative TD amounts correctly - Main amount on 4455100, rounding difference on 758000 --- core/class/declarationtva_pdf.class.php | 43 +++++++++++++++++++++---- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/core/class/declarationtva_pdf.class.php b/core/class/declarationtva_pdf.class.php index 103d669..b6f9492 100644 --- a/core/class/declarationtva_pdf.class.php +++ b/core/class/declarationtva_pdf.class.php @@ -2087,20 +2087,26 @@ class DeclarationTVA_PDF error_log("DeclarationTVA: Total difference: " . $difference); error_log("DeclarationTVA: Difference > 0: " . ($difference > 0 ? 'true' : 'false')); - // Split into main amount (rounded) and rounding difference - if (abs($difference) >= 0.01) { - $rounded_amount = round($difference); - $rounding_diff = $difference - $rounded_amount; + // Use TD line calculation (Line 16 - Line 23) as main balancing amount + $line_16_amount = $this->getLineAmount($declaration, '16'); + $line_23_amount = $this->getLineAmount($declaration, '23'); + $td_amount = $line_16_amount - $line_23_amount; + + error_log("DeclarationTVA: Line 16: " . $line_16_amount . ", Line 23: " . $line_23_amount . ", TD: " . $td_amount); + + if (abs($td_amount) >= 0.01) { + $rounded_td = round($td_amount); + $rounding_diff = $td_amount - $rounded_td; - error_log("DeclarationTVA: Difference: " . $difference . ", Rounded: " . $rounded_amount . ", Rounding diff: " . $rounding_diff); + error_log("DeclarationTVA: TD amount: " . $td_amount . ", Rounded: " . $rounded_td . ", Rounding diff: " . $rounding_diff); - // Main amount (rounded) on 4455100 + // Main TD amount (rounded) on 4455100 $balancing_entries[] = array( 'account_code' => '4455100', 'account_label' => $this->getAccountLabel('4455100'), 'entry_label' => $declaration->declaration_name, 'debit' => '', - 'credit' => $this->formatAmount(abs($rounded_amount)) + 'credit' => $this->formatAmount(abs($rounded_td)) ); // Rounding difference on 758000 @@ -2118,6 +2124,29 @@ class DeclarationTVA_PDF return $balancing_entries; } + /** + * Get line amount for a specific CA-3 line + * + * @param DeclarationTVA $declaration Declaration object + * @param string $line_code CA-3 line code + * @return float Line amount + */ + private function getLineAmount($declaration, $line_code) + { + $sql = "SELECT vat_amount FROM " . MAIN_DB_PREFIX . "declarationtva_ca3_lines + WHERE declaration_id = " . $declaration->rowid . " + AND ca3_line = '" . $this->db->escape($line_code) . "' + 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; + } + /** * Get VAT amount from line TD *