Fix CA-3 calculation to process all lines, not just mapped ones

Fixed:
- Process ALL CA-3 lines (A1-A5, 08, 09, 9B, 17, 20-22, 25-26, 28-29)
- Create records for all lines, even if no accounts mapped
- Show 'No accounts mapped' for lines without configuration
- Now all CA-3 lines will appear in the table with proper amounts
- Lines with mapped accounts will show calculated amounts
- Lines without mappings will show 0.00 but still be visible
This commit is contained in:
Frank Cools 2025-10-02 21:28:04 +02:00
parent 6723375a16
commit ffece31f7a

View File

@ -203,18 +203,22 @@ class DeclarationTVA
$grouped_mappings[$ca3_line][] = $mapping;
}
// Define all CA-3 lines that should be processed
$all_ca3_lines = array('A1', 'A2', 'A3', 'A4', 'A5', '08', '09', '9B', '17', '20', '21', '22', '25', '26', '28', '29');
$total_vat_collected = 0;
$total_vat_deductible = 0;
// Process each CA-3 line (sum all accounts for the same line)
foreach ($grouped_mappings as $ca3_line => $line_mappings) {
// Process ALL CA-3 lines (even those without mappings)
foreach ($all_ca3_lines as $ca3_line) {
$line_total_base = 0;
$line_total_vat = 0;
$line_total_amount = 0;
$account_labels = array();
// Sum all accounts for this CA-3 line
foreach ($line_mappings as $mapping) {
// Sum all accounts for this CA-3 line (if any mappings exist)
if (isset($grouped_mappings[$ca3_line])) {
foreach ($grouped_mappings[$ca3_line] as $mapping) {
$amounts = $this->getAccountAmounts($mapping['account_code'], $period['start_date'], $period['end_date']);
$line_total_base += $amounts['base_amount'];
@ -222,26 +226,25 @@ class DeclarationTVA
$line_total_amount += $amounts['total_amount'];
$account_labels[] = $mapping['account_label'];
}
}
// Create CA-3 line record with summed amounts
// Create CA-3 line record with summed amounts (even if zero)
$combined_amounts = array(
'base_amount' => $line_total_base,
'vat_amount' => $line_total_vat,
'total_amount' => $line_total_amount
);
$combined_label = implode(', ', $account_labels);
$combined_label = !empty($account_labels) ? implode(', ', $account_labels) : 'No accounts mapped';
$this->createCA3Line($declaration_id, $ca3_line, $combined_label, $combined_amounts);
// Update totals
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;
}
}
}
// Calculate net amounts
$net_vat_due = $total_vat_collected - $total_vat_deductible;