From 4f82e24e29a24aa288a6d8830ecf80c7bcb73e93 Mon Sep 17 00:00:00 2001 From: Frank Cools Date: Tue, 7 Oct 2025 11:49:40 +0200 Subject: [PATCH] Improve balancing entry with real account descriptions and separate rounding - Added getAccountLabel method to fetch real account descriptions from chart of accounts - Balancing entry now uses real account description for 4456700 - Balancing amounts are now rounded (no decimals) - Added getBalancingRoundingEntry method for separate rounding line - Rounding difference goes to 658000 (debit) or 758000 (credit) with real descriptions - Improved double-entry bookkeeping with proper account labels and rounding --- core/class/declarationtva_pdf.class.php | 104 +++++++++++++++++++++++- 1 file changed, 101 insertions(+), 3 deletions(-) diff --git a/core/class/declarationtva_pdf.class.php b/core/class/declarationtva_pdf.class.php index 9c9ab1c..64f2e30 100644 --- a/core/class/declarationtva_pdf.class.php +++ b/core/class/declarationtva_pdf.class.php @@ -1866,6 +1866,12 @@ class DeclarationTVA_PDF $balancing_entry = $this->getBalancingEntry($declaration, $entries); if ($balancing_entry) { $entries[] = $balancing_entry; + + // Add rounding entry for the balancing difference + $balancing_rounding_entry = $this->getBalancingRoundingEntry($declaration, $entries); + if ($balancing_rounding_entry) { + $entries[] = $balancing_rounding_entry; + } } return $entries; @@ -2067,9 +2073,12 @@ class DeclarationTVA_PDF return null; } + // Get real account description for 4456700 + $account_label = $this->getAccountLabel('4456700'); + $entry = array( 'account_code' => '4456700', - 'account_label' => 'TVA à payer', + 'account_label' => $account_label, 'entry_label' => $declaration->declaration_name, 'debit' => '', 'credit' => '' @@ -2077,10 +2086,10 @@ class DeclarationTVA_PDF if ($difference < 0) { // More credits than debits, add debit to balance - $entry['debit'] = $this->formatAmountReal(abs($difference)); + $entry['debit'] = $this->formatAmount(round(abs($difference))); } else { // More debits than credits, add credit to balance - $entry['credit'] = $this->formatAmountReal($difference); + $entry['credit'] = $this->formatAmount(round($difference)); } return $entry; @@ -2099,4 +2108,93 @@ class DeclarationTVA_PDF $amount_string = str_replace(',', '.', $amount_string); return (float)$amount_string; } + + /** + * Get account label from chart of accounts + * + * @param string $account_code Account code + * @return string Account label + */ + private function getAccountLabel($account_code) + { + $sql = "SELECT label FROM " . MAIN_DB_PREFIX . "accounting_account + WHERE account_number = '" . $this->db->escape($account_code) . "' + AND entity = " . $this->entity . " + AND active = 1 + LIMIT 1"; + + $result = $this->db->query($sql); + if ($result && $this->db->num_rows($result) > 0) { + $obj = $this->db->fetch_object($result); + return $obj->label; + } + + // Fallback to default labels + switch ($account_code) { + case '4456700': + return 'TVA à payer'; + case '658000': + return 'Charges exceptionnelles'; + case '758000': + return 'Produits exceptionnels'; + default: + return 'Compte ' . $account_code; + } + } + + /** + * Get balancing rounding entry for the difference between real and rounded amounts + * + * @param DeclarationTVA $declaration Declaration object + * @param array $entries Current journal entries + * @return array|null Journal entry + */ + private function getBalancingRoundingEntry($declaration, $entries) + { + $total_debits = 0; + $total_credits = 0; + $total_debits_rounded = 0; + $total_credits_rounded = 0; + + // Calculate totals from existing entries (real values) + foreach ($entries as $entry) { + if (!empty($entry['debit'])) { + $total_debits += $this->parseAmount($entry['debit']); + $total_debits_rounded += round($this->parseAmount($entry['debit'])); + } + if (!empty($entry['credit'])) { + $total_credits += $this->parseAmount($entry['credit']); + $total_credits_rounded += round($this->parseAmount($entry['credit'])); + } + } + + $real_difference = $total_debits - $total_credits; + $rounded_difference = $total_debits_rounded - $total_credits_rounded; + $rounding_diff = $real_difference - $rounded_difference; + + // If rounding difference is very small, no entry needed + if (abs($rounding_diff) < 0.01) { + return null; + } + + $entry = array( + 'account_code' => '', + 'account_label' => '', + 'entry_label' => $declaration->declaration_name, + 'debit' => '', + 'credit' => '' + ); + + if ($rounding_diff < 0) { + $entry['account_code'] = '658000'; + $entry['account_label'] = $this->getAccountLabel('658000'); + $entry['debit'] = $this->formatAmountReal(abs($rounding_diff)); + } else { + $entry['account_code'] = '758000'; + $entry['account_label'] = $this->getAccountLabel('758000'); + $entry['credit'] = $this->formatAmountReal($rounding_diff); + } + + return $entry; + } }