diff --git a/core/class/declarationtva_pdf.class.php b/core/class/declarationtva_pdf.class.php index 11c6d3c..718ae66 100644 --- a/core/class/declarationtva_pdf.class.php +++ b/core/class/declarationtva_pdf.class.php @@ -2071,104 +2071,58 @@ class DeclarationTVA_PDF $balancing_entries = array(); - // Get VAT amount from line TD (this should be the main balancing amount) - $vat_td_amount = $this->getVATAmountFromLineTD($declaration); - // Debug logging error_log("DeclarationTVA: Total difference: " . $difference); - error_log("DeclarationTVA: VAT TD amount: " . $vat_td_amount); - if ($vat_td_amount > 0) { - // Main balancing entry using VAT amount from line TD + // Calculate rounded difference for main entry + $rounded_difference = round($difference); + + if ($rounded_difference != 0) { + // Main balancing entry with rounded amount if ($difference < 0) { - // Need debit entry + // More credits than debits - need debit entry (4456700) $balancing_entries[] = array( 'account_code' => '4456700', 'account_label' => $this->getAccountLabel('4456700'), 'entry_label' => $declaration->declaration_name, - 'debit' => $this->formatAmount($vat_td_amount), + 'debit' => $this->formatAmount(abs($rounded_difference)), 'credit' => '' ); } else { - // Need credit entry + // More debits than credits - need credit entry (4455100) $balancing_entries[] = array( 'account_code' => '4455100', 'account_label' => $this->getAccountLabel('4455100'), 'entry_label' => $declaration->declaration_name, 'debit' => '', - 'credit' => $this->formatAmount($vat_td_amount) + 'credit' => $this->formatAmount($rounded_difference) ); } - // Rounding entry for the remaining difference (use real value, not rounded) - $remaining_diff = abs($difference) - $vat_td_amount; - error_log("DeclarationTVA: Remaining difference: " . $remaining_diff); + // Rounding entry for the difference between real and rounded + $rounding_diff = $difference - $rounded_difference; + error_log("DeclarationTVA: Rounding difference: " . $rounding_diff); - if ($remaining_diff >= 0.01) { + if (abs($rounding_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 { + // Credit difference - rounding goes to 758000 debit $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 - error_log("DeclarationTVA: No TD line found, using fallback logic"); - - // Use rounded amount for main balancing entry - $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)), + 'debit' => $this->formatAmountReal(abs($rounding_diff)), 'credit' => '' ); } else { + // Debit difference - rounding goes to 658000 debit $balancing_entries[] = array( - 'account_code' => '4455100', - 'account_label' => $this->getAccountLabel('4455100'), + 'account_code' => '658000', + 'account_label' => $this->getAccountLabel('658000'), 'entry_label' => $declaration->declaration_name, - 'debit' => '', - 'credit' => $this->formatAmount($rounded_difference) + 'debit' => $this->formatAmountReal(abs($rounding_diff)), + 'credit' => '' ); } - - // Add 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) - ); - } - } } }