Fix balancing logic according to user specifications
- Calculate difference between debits and credits - Main entry: rounded amount on 4455100 (credit) or 4456700 (debit) - Rounding entry: real difference on 758000 (credit diff) or 658000 (debit diff) - Simplified logic by removing TD line dependency - Both VAT debit and credit scenarios now work correctly
This commit is contained in:
parent
ffe8a960e2
commit
240360a393
@ -2071,66 +2071,16 @@ 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
|
||||
if ($difference < 0) {
|
||||
// Need debit entry
|
||||
$balancing_entries[] = array(
|
||||
'account_code' => '4456700',
|
||||
'account_label' => $this->getAccountLabel('4456700'),
|
||||
'entry_label' => $declaration->declaration_name,
|
||||
'debit' => $this->formatAmount($vat_td_amount),
|
||||
'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($vat_td_amount)
|
||||
);
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
if ($remaining_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 {
|
||||
$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
|
||||
// Calculate rounded difference for main entry
|
||||
$rounded_difference = round($difference);
|
||||
|
||||
if ($rounded_difference != 0) {
|
||||
if ($rounded_difference < 0) {
|
||||
// Main balancing entry with rounded amount
|
||||
if ($difference < 0) {
|
||||
// More credits than debits - need debit entry (4456700)
|
||||
$balancing_entries[] = array(
|
||||
'account_code' => '4456700',
|
||||
'account_label' => $this->getAccountLabel('4456700'),
|
||||
@ -2139,6 +2089,7 @@ class DeclarationTVA_PDF
|
||||
'credit' => ''
|
||||
);
|
||||
} else {
|
||||
// More debits than credits - need credit entry (4455100)
|
||||
$balancing_entries[] = array(
|
||||
'account_code' => '4455100',
|
||||
'account_label' => $this->getAccountLabel('4455100'),
|
||||
@ -2148,10 +2099,22 @@ class DeclarationTVA_PDF
|
||||
);
|
||||
}
|
||||
|
||||
// Add rounding entry for the difference between real and rounded
|
||||
// Rounding entry for the difference between real and rounded
|
||||
$rounding_diff = $difference - $rounded_difference;
|
||||
error_log("DeclarationTVA: Rounding difference: " . $rounding_diff);
|
||||
|
||||
if (abs($rounding_diff) >= 0.01) {
|
||||
if ($rounding_diff < 0) {
|
||||
if ($difference < 0) {
|
||||
// Credit difference - rounding goes to 758000 debit
|
||||
$balancing_entries[] = array(
|
||||
'account_code' => '758000',
|
||||
'account_label' => $this->getAccountLabel('758000'),
|
||||
'entry_label' => $declaration->declaration_name,
|
||||
'debit' => $this->formatAmountReal(abs($rounding_diff)),
|
||||
'credit' => ''
|
||||
);
|
||||
} else {
|
||||
// Debit difference - rounding goes to 658000 debit
|
||||
$balancing_entries[] = array(
|
||||
'account_code' => '658000',
|
||||
'account_label' => $this->getAccountLabel('658000'),
|
||||
@ -2159,15 +2122,6 @@ class DeclarationTVA_PDF
|
||||
'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)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user