diff --git a/core/class/declarationtva.class.php b/core/class/declarationtva.class.php index e33f887..f32613e 100644 --- a/core/class/declarationtva.class.php +++ b/core/class/declarationtva.class.php @@ -203,43 +203,46 @@ 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) { - $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']; + // 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']; + $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 + // 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; - } + 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; } }