From bbecae15f050f73e93481e8e24fa45a9a5ca68d6 Mon Sep 17 00:00:00 2001 From: Frank Cools Date: Thu, 2 Oct 2025 21:22:46 +0200 Subject: [PATCH] Fix CA-3 calculation to sum multiple accounts per line Fixed: - Group account mappings by CA-3 line before processing - Sum all accounts for the same CA-3 line (e.g. A1 = 7061330 + 7061320) - Create single CA-3 line record with combined amounts - Combined account labels for better visibility - Now correctly calculates: A1 = 904.12 + 1053.45 = 1957.57 --- core/class/declarationtva.class.php | 52 +++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/core/class/declarationtva.class.php b/core/class/declarationtva.class.php index 3cd0269..41d96dc 100644 --- a/core/class/declarationtva.class.php +++ b/core/class/declarationtva.class.php @@ -190,24 +190,56 @@ class DeclarationTVA */ public function calculateCA3Amounts($declaration_id, $period) { - // Get account mappings + // Get account mappings grouped by CA-3 line $mappings = $this->getAccountMappings(); + + // Group mappings by CA-3 line + $grouped_mappings = array(); + foreach ($mappings as $mapping) { + $ca3_line = $mapping['ca3_line']; + if (!isset($grouped_mappings[$ca3_line])) { + $grouped_mappings[$ca3_line] = array(); + } + $grouped_mappings[$ca3_line][] = $mapping; + } $total_vat_collected = 0; $total_vat_deductible = 0; - // Process each CA-3 line - foreach ($mappings as $mapping) { - $amounts = $this->getAccountAmounts($mapping['account_code'], $period['start_date'], $period['end_date']); + // Process each CA-3 line (sum all accounts for the same line) + foreach ($grouped_mappings as $ca3_line => $line_mappings) { + $line_total_base = 0; + $line_total_vat = 0; + $line_total_amount = 0; + $account_labels = array(); - // Create CA-3 line record - $this->createCA3Line($declaration_id, $mapping['ca3_line'], $mapping['account_label'], $amounts); + // Sum all accounts for this CA-3 line + foreach ($line_mappings as $mapping) { + $amounts = $this->getAccountAmounts($mapping['account_code'], $period['start_date'], $period['end_date']); + + $line_total_base += $amounts['base_amount']; + $line_total_vat += $amounts['vat_amount']; + $line_total_amount += $amounts['total_amount']; + $account_labels[] = $mapping['account_label']; + } + + // Create CA-3 line record with summed amounts + $combined_amounts = array( + 'base_amount' => $line_total_base, + 'vat_amount' => $line_total_vat, + 'total_amount' => $line_total_amount + ); + + $combined_label = implode(', ', $account_labels); + $this->createCA3Line($declaration_id, $ca3_line, $combined_label, $combined_amounts); // Update totals - if (in_array($mapping['ca3_line'], ['A1', 'A2', 'B1', 'B2', 'B3', 'B4', '17'])) { - $total_vat_collected += $amounts['vat_amount']; - } elseif (in_array($mapping['ca3_line'], ['20', '21'])) { - $total_vat_deductible += $amounts['vat_amount']; + if (in_array($ca3_line, ['A1', 'A2', 'A3', 'A4', 'A5', '08', '09', '9B', '17', '20', '21', '22', '25', '26', '28', '29'])) { + if (in_array($ca3_line, ['A1', 'A2', 'A3', 'A4', 'A5', '08', '09', '9B', '17'])) { + $total_vat_collected += $line_total_vat; + } elseif (in_array($ca3_line, ['20', '21', '22'])) { + $total_vat_deductible += $line_total_vat; + } } }