diff --git a/core/class/declarationtva_pdf.class.php b/core/class/declarationtva_pdf.class.php index bc95f50..816023c 100644 --- a/core/class/declarationtva_pdf.class.php +++ b/core/class/declarationtva_pdf.class.php @@ -395,8 +395,9 @@ class DeclarationTVA_PDF if (file_exists($template_path) && $this->isFillablePDF($template_path)) { return $this->fillFillablePDF($template_path, $output_path, $declaration, $ca3_data, $mysoc); } else { - // Fall back to basic PDF generation - return $this->generateBasicPDF($output_path, $declaration, $ca3_data, $mysoc); + // No fillable template available - show error + $this->error = 'No fillable PDF template available. Please upload a CA-3 template.'; + return false; } } catch (Exception $e) { $this->error = 'PDF generation failed: ' . $e->getMessage(); @@ -916,78 +917,6 @@ class DeclarationTVA_PDF * @param Societe $company Company object * @return bool Success */ - private function generateBasicPDF($output_path, $declaration, $ca3_data, $mysoc) - { - try { - // Create a new PDF document - $pdf = new DeclarationTVA_CustomPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); - - // Set company name and declaration period for footer - $pdf->setCompanyName($mysoc->name); - $pdf->setDeclarationPeriod(dol_print_date($declaration->start_date, 'day') . ' - ' . dol_print_date($declaration->end_date, 'day')); - - // Set document information - $pdf->SetCreator('DeclarationTVA Module'); - $pdf->SetAuthor($mysoc->name); - $pdf->SetTitle('CA-3 Declaration ' . $declaration->declaration_number); - $pdf->SetSubject('French VAT Declaration'); - - // Set margins - $pdf->SetMargins(15, 15, 15); - $pdf->SetHeaderMargin(5); - $pdf->SetFooterMargin(10); - - // Set thin borders for all elements - $pdf->SetLineWidth(0.1); - - // Add a page - $pdf->AddPage(); - - // Add title - $pdf->SetFont('helvetica', 'B', 16); - $pdf->Cell(0, 10, 'Déclaration TVA CA-3', 0, 1, 'C'); - $pdf->Ln(10); - - // Add declaration information - $pdf->SetFont('helvetica', '', 12); - $pdf->Cell(0, 8, 'Numéro de déclaration: ' . $declaration->declaration_number, 0, 1); - $pdf->Cell(0, 8, 'Période: ' . dol_print_date($declaration->start_date, 'day') . ' - ' . dol_print_date($declaration->end_date, 'day'), 0, 1); - $pdf->Cell(0, 8, 'Statut: ' . $this->translateStatus($declaration->status), 0, 1); - $pdf->Ln(10); - - // Add CA-3 sections - $this->addCA3Section($pdf, 'A. Opérations imposables', $ca3_data, array('A1', 'A2', 'A3', 'A4', 'A5')); - $this->addCA3Section($pdf, 'B. TVA due', $ca3_data, array('08', '09', '9B', '17')); - $this->addCA3Section($pdf, 'C. TVA déductible', $ca3_data, array('20', '21', '22')); - $this->addCA3Section($pdf, 'D. Résultat', $ca3_data, array('25', '26', '27', 'TD', '28', '32')); - - // Add totals - $pdf->Ln(10); - $pdf->SetFont('helvetica', 'B', 12); - $pdf->Cell(0, 8, 'TOTAL TVA COLLECTÉE: ' . price($declaration->total_vat_collected, 0, '', 1, 0), 0, 1); - $pdf->Cell(0, 8, 'TOTAL TVA DÉDUCTIBLE: ' . price($declaration->total_vat_deductible, 0, '', 1, 0), 0, 1); - $pdf->Cell(0, 8, 'TVA NETTE DUE: ' . price($declaration->net_vat_due, 0, '', 1, 0), 0, 1); - - if ($declaration->vat_credit > 0) { - $pdf->Cell(0, 8, 'CRÉDIT DE TVA: ' . price($declaration->vat_credit, 0, '', 1, 0), 0, 1); - } - - // Add detailed breakdown pages using improved layout - $this->addDetailPages($pdf, $declaration, $ca3_data); - - // Set total pages for footer after all pages are generated - $pdf->setTotalPages($pdf->getNumPages()); - - // Output PDF - $pdf->Output($output_path, 'F'); - - return true; - - } catch (Exception $e) { - $this->error = 'Basic PDF generation failed: ' . $e->getMessage(); - return false; - } - } /** * Add CA-3 section to PDF @@ -1027,71 +956,6 @@ class DeclarationTVA_PDF * @param DeclarationTVA $declaration Declaration object * @param array $ca3_data CA-3 line data */ - private function addDetailPages($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->addLineDetailPage($pdf, $declaration, $line_code); - } - } /** * Add a detail section for a specific CA-3 line @@ -1100,133 +964,6 @@ class DeclarationTVA_PDF * @param DeclarationTVA $declaration Declaration object * @param string $line_code CA-3 line code */ - private function addLineDetailPage($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