diff --git a/core/class/declarationtva_pdf.class.php b/core/class/declarationtva_pdf.class.php index 816023c..1e7b1e1 100644 --- a/core/class/declarationtva_pdf.class.php +++ b/core/class/declarationtva_pdf.class.php @@ -965,6 +965,213 @@ class DeclarationTVA_PDF * @param string $line_code CA-3 line code */ + /** + * Add detailed pages content to PDF + * + * @param TCPDF $pdf PDF object + * @param DeclarationTVA $declaration Declaration object + * @param array $ca3_data CA-3 line data + */ + private function addDetailedPagesContent($pdf, $declaration, $ca3_data) + { + // Define calculated lines that should be excluded from detailed breakdown + $calculated_lines = array('25', '26', '27', 'TD', '28', '32', '16', '23'); + + // Define the order of lines as they appear in the view page + $ordered_lines = array( + // Section A: Opérations imposables + 'A1', 'A2', 'A3', 'A4', 'A5', 'E1', 'E2', 'E3', 'E4', 'E5', 'E6', 'F1', 'F2', 'F6', 'F7', 'F8', + // Section B: TVA due + '08', '09', '9B', '17', + // Section C: TVA déductible + '19', '20', '21', '22' + ); + + // Create a lookup array for quick access to line data + $ca3_lookup = array(); + foreach ($ca3_data as $line_data) { + $ca3_lookup[$line_data['ca3_line']] = $line_data; + } + + // Get lines that have actual values and are not calculated lines, in the correct order + $lines_with_data = array(); + foreach ($ordered_lines as $line_code) { + // Skip if line doesn't exist in data + if (!isset($ca3_lookup[$line_code])) { + continue; + } + + // Skip calculated lines - they don't have account mappings + if (in_array($line_code, $calculated_lines)) { + continue; + } + + $line_data = $ca3_lookup[$line_code]; + $has_vat_amount = isset($line_data['vat_amount']) && $line_data['vat_amount'] > 0; + $has_base_amount = isset($line_data['base_amount']) && $line_data['base_amount'] > 0; + $has_total_amount = isset($line_data['total_amount']) && $line_data['total_amount'] > 0; + + // Only include lines that have meaningful values and are not calculated + if ($has_vat_amount || $has_base_amount || $has_total_amount) { + $lines_with_data[] = $line_code; + } + } + + // If no lines have data, add a message page + if (empty($lines_with_data)) { + $pdf->AddPage(); + $pdf->SetFont('helvetica', 'B', 16); + $pdf->Cell(0, 10, 'Aucune donnée détaillée disponible', 0, 1, 'C'); + $pdf->Ln(10); + $pdf->SetFont('helvetica', '', 12); + $pdf->Cell(0, 8, 'Aucune ligne de la déclaration ne contient de données comptables.', 0, 1); + $pdf->Cell(0, 8, 'Veuillez configurer les mappings de comptes dans la section Administration.', 0, 1); + return; + } + + // Start line details on page 2 + $pdf->AddPage(); + + // Add a detail section for each line with data in the correct order + foreach ($lines_with_data as $line_code) { + $this->addLineDetailPageContent($pdf, $declaration, $line_code); + } + } + + /** + * Add a detail section for a specific CA-3 line + * + * @param TCPDF $pdf PDF object + * @param DeclarationTVA $declaration Declaration object + * @param string $line_code CA-3 line code + */ + private function addLineDetailPageContent($pdf, $declaration, $line_code) + { + // Check if we need a new page (if current position is too low) + $current_y = $pdf->GetY(); + $page_height = $pdf->getPageHeight() - $pdf->getMargins()['bottom']; + + // If we're too close to the bottom, start a new page + if ($current_y > $page_height - 100) { + $pdf->AddPage(); + } else { + // Add some space between sections + $pdf->Ln(10); + } + + // Set section title with light gray background + $pdf->SetFont('helvetica', 'B', 16); + $pdf->SetTextColor(0, 0, 0); + $pdf->SetFillColor(240, 240, 240); // Light gray background + $pdf->Cell(0, 10, 'Détail de la ligne ' . $line_code, 1, 1, 'C', true); + + // Add separator line (thin border) + $pdf->SetDrawColor(0, 0, 0); + $pdf->SetLineWidth(0.1); // Thin border + $pdf->Line($pdf->getMargins()['left'], $pdf->GetY(), $pdf->getPageWidth() - $pdf->getMargins()['right'], $pdf->GetY()); + $pdf->Ln(5); + + // Get line details + $line_details = $declaration->getCA3LineDetails($declaration->rowid, $line_code); + + if (empty($line_details) || empty($line_details['account_details'])) { + $pdf->SetFont('helvetica', '', 12); + $pdf->Cell(0, 8, 'Aucun détail comptable disponible pour cette ligne.', 0, 1); + $pdf->Cell(0, 8, 'Cette ligne peut être calculée automatiquement ou ne pas avoir de mapping de comptes.', 0, 1); + + // Still show the calculated value if available + if (!empty($line_details['calculated_line'])) { + $calc = $line_details['calculated_line']; + $pdf->Ln(5); + $pdf->SetFont('helvetica', 'B', 12); + $pdf->Cell(0, 8, 'Valeur calculée:', 0, 1); + $pdf->SetFont('helvetica', '', 10); + if ($calc->base_amount > 0) { + $pdf->Cell(0, 6, 'Montant de base: ' . price($calc->base_amount, 0, '', 1, 0), 0, 1); + } + if ($calc->vat_amount > 0) { + $pdf->Cell(0, 6, 'Montant de TVA: ' . price($calc->vat_amount, 0, '', 1, 0), 0, 1); + } + if ($calc->total_amount > 0) { + $pdf->Cell(0, 6, 'Total: ' . price($calc->total_amount, 0, '', 1, 0), 0, 1); + } + } + return; + } + + // Add line summary information (without title) - using table font size + $pdf->SetFont('helvetica', '', 7); + $pdf->Cell(0, 6, 'Période: ' . dol_print_date($line_details['start_date'], 'day') . ' - ' . dol_print_date($line_details['end_date'], 'day'), 0, 1); + + // Special handling for lines with both base and VAT accounts (08, 09, 9B) + if (in_array($line_code, array('08', '09', '9B'))) { + // Count base and VAT accounts separately (only non-zero amounts) + $base_count = 0; + $vat_count = 0; + foreach ($line_details['account_details'] as $account) { + // Only count accounts with non-zero amounts + $has_amount = false; + if (isset($account['base_amount']) && $account['base_amount'] > 0) { + $has_amount = true; + } + if (isset($account['vat_amount']) && $account['vat_amount'] > 0) { + $has_amount = true; + } + if (isset($account['total_amount']) && $account['total_amount'] > 0) { + $has_amount = true; + } + + if (!$has_amount) { + continue; + } + + if (strpos($account['mapping_type'], '_BASE') !== false) { + $base_count++; + } elseif (strpos($account['mapping_type'], '_VAT') !== false) { + $vat_count++; + } + } + $pdf->Cell(0, 6, 'Comptes de base: ' . $base_count . ' | Comptes de TVA: ' . $vat_count, 0, 1); + } else { + // Count non-zero accounts for other lines + $non_zero_count = 0; + foreach ($line_details['account_details'] as $account) { + $has_amount = false; + if (isset($account['base_amount']) && $account['base_amount'] > 0) { + $has_amount = true; + } + if (isset($account['vat_amount']) && $account['vat_amount'] > 0) { + $has_amount = true; + } + if (isset($account['total_amount']) && $account['total_amount'] > 0) { + $has_amount = true; + } + + if ($has_amount) { + $non_zero_count++; + } + } + $pdf->Cell(0, 6, 'Nombre de comptes: ' . $non_zero_count, 0, 1); + } + + if (!empty($line_details['calculated_line'])) { + $calc = $line_details['calculated_line']; + + // Special handling for lines with both base and VAT amounts (08, 09, 9B) + if (in_array($line_code, array('08', '09', '9B'))) { + $pdf->Cell(0, 6, 'Montant de base: ' . price($calc->base_amount, 0, '', 1, 0) . ' | Montant de TVA: ' . price($calc->vat_amount, 0, '', 1, 0), 0, 1); + } else { + // Standard VAT amount for other lines + $pdf->Cell(0, 6, 'Montant calculé: ' . price($calc->vat_amount, 0, '', 1, 0), 0, 1); + } + } + + $pdf->Ln(10); + + // Add account details table + $this->addAccountDetailsTable($pdf, $line_details['account_details'], $line_code); + } + /** * Add account details table to PDF * @@ -1364,7 +1571,7 @@ class DeclarationTVA_PDF $this->addBankJournalEntryTable($pdf, $declaration, $ca3_data); // Add detailed breakdown pages starting on page 2 - $this->addDetailPages($pdf, $declaration, $ca3_data); + $this->addDetailedPagesContent($pdf, $declaration, $ca3_data); // Set total pages for footer after all pages are generated $pdf->setTotalPages($pdf->getNumPages());