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
This commit is contained in:
Frank Cools 2025-10-07 12:27:28 +02:00
parent 295f264090
commit 8054e03026

View File

@ -2071,16 +2071,18 @@ class DeclarationTVA_PDF
$balancing_entries = array(); $balancing_entries = array();
// Main balancing entry (rounded) // Get VAT amount from line TD (this should be the main balancing amount)
$rounded_difference = round($difference); $vat_td_amount = $this->getVATAmountFromLineTD($declaration);
if ($rounded_difference != 0) {
if ($rounded_difference < 0) { if ($vat_td_amount > 0) {
// Main balancing entry using VAT amount from line TD
if ($difference < 0) {
// Need debit entry // Need debit entry
$balancing_entries[] = array( $balancing_entries[] = array(
'account_code' => '4456700', 'account_code' => '4456700',
'account_label' => $this->getAccountLabel('4456700'), 'account_label' => $this->getAccountLabel('4456700'),
'entry_label' => $declaration->declaration_name, 'entry_label' => $declaration->declaration_name,
'debit' => $this->formatAmount(abs($rounded_difference)), 'debit' => $this->formatAmount($vat_td_amount),
'credit' => '' 'credit' => ''
); );
} else { } else {
@ -2090,36 +2092,80 @@ class DeclarationTVA_PDF
'account_label' => $this->getAccountLabel('4455100'), 'account_label' => $this->getAccountLabel('4455100'),
'entry_label' => $declaration->declaration_name, 'entry_label' => $declaration->declaration_name,
'debit' => '', 'debit' => '',
'credit' => $this->formatAmount($rounded_difference) 'credit' => $this->formatAmount($vat_td_amount)
); );
} }
}
// Rounding entry for the remaining difference
// Rounding entry for the difference between real and rounded $remaining_diff = abs($difference) - $vat_td_amount;
$rounding_diff = $difference - $rounded_difference; if ($remaining_diff >= 0.01) {
if (abs($rounding_diff) >= 0.01) { if ($difference < 0) {
if ($rounding_diff < 0) { $balancing_entries[] = array(
$balancing_entries[] = array( 'account_code' => '658000',
'account_code' => '658000', 'account_label' => $this->getAccountLabel('658000'),
'account_label' => $this->getAccountLabel('658000'), 'entry_label' => $declaration->declaration_name,
'entry_label' => $declaration->declaration_name, 'debit' => $this->formatAmountReal($remaining_diff),
'debit' => $this->formatAmountReal(abs($rounding_diff)), 'credit' => ''
'credit' => '' );
); } else {
} else { $balancing_entries[] = array(
$balancing_entries[] = array( 'account_code' => '758000',
'account_code' => '758000', 'account_label' => $this->getAccountLabel('758000'),
'account_label' => $this->getAccountLabel('758000'), 'entry_label' => $declaration->declaration_name,
'entry_label' => $declaration->declaration_name, 'debit' => '',
'debit' => '', 'credit' => $this->formatAmountReal($remaining_diff)
'credit' => $this->formatAmountReal($rounding_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; 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 * Parse amount string back to float
* *