diff --git a/core/class/declarationtva_pdf.class.php b/core/class/declarationtva_pdf.class.php index 9f1f963..4108660 100644 --- a/core/class/declarationtva_pdf.class.php +++ b/core/class/declarationtva_pdf.class.php @@ -1863,14 +1863,10 @@ class DeclarationTVA_PDF } // Add balancing entry to ensure debits equal credits - $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; + $balancing_entries = $this->getBalancingEntries($declaration, $entries); + foreach ($balancing_entries as $entry) { + if ($entry) { + $entries[] = $entry; } } @@ -2045,13 +2041,13 @@ class DeclarationTVA_PDF } /** - * Get balancing entry to ensure debits equal credits + * Get balancing entries (main balancing + rounding) * * @param DeclarationTVA $declaration Declaration object * @param array $entries Current journal entries - * @return array|null Journal entry + * @return array Array of balancing entries */ - private function getBalancingEntry($declaration, $entries) + private function getBalancingEntries($declaration, $entries) { $total_debits = 0; $total_credits = 0; @@ -2070,30 +2066,58 @@ class DeclarationTVA_PDF // If difference is very small (less than 0.01), no balancing entry needed if (abs($difference) < 0.01) { - return null; + return array(); } - if ($difference < 0) { - // More credits than debits, add debit to balance - $entry = array( - 'account_code' => '4456700', // Debit entry uses 4456700 - 'account_label' => $this->getAccountLabel('4456700'), - 'entry_label' => $declaration->declaration_name, - 'debit' => $this->formatAmount(round(abs($difference))), - 'credit' => '' - ); - } else { - // More debits than credits, add credit to balance - $entry = array( - 'account_code' => '4455100', // Credit entry uses 4455100 - 'account_label' => $this->getAccountLabel('4455100'), - 'entry_label' => $declaration->declaration_name, - 'debit' => '', - 'credit' => $this->formatAmount(round($difference)) - ); + $balancing_entries = array(); + + // Main balancing entry (rounded) + $rounded_difference = round($difference); + if ($rounded_difference != 0) { + if ($rounded_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)), + 'credit' => '' + ); + } else { + // Need credit entry + $balancing_entries[] = array( + 'account_code' => '4455100', + 'account_label' => $this->getAccountLabel('4455100'), + 'entry_label' => $declaration->declaration_name, + 'debit' => '', + 'credit' => $this->formatAmount($rounded_difference) + ); + } } - return $entry; + // 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) + ); + } + } + + return $balancing_entries; } /** @@ -2155,59 +2179,4 @@ class DeclarationTVA_PDF 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; - } }