diff --git a/ChangeLog.md b/ChangeLog.md index 966982e..0943175 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,5 +1,14 @@ # CHANGELOG MODULE DECLARATIONTVA FOR [DOLIBARR ERP CRM](https://www.dolibarr.org) +## 2.5.30 + +### Major Refactoring +- **Unified PDF Generation**: Created single method `generateCompleteCA3PDF()` for both export and validation +- **Status-Based Generation**: Export uses fillable template when available, validation uses improved layout +- **Consistent Layout**: Both export and validation now use the same improved detailed pages +- **Cleaner Architecture**: Single method handles all PDF generation with status parameter +- **Better Maintainability**: All PDF improvements are now centralized in one method + ## 2.5.29 ### Bug Fixes diff --git a/core/class/declarationtva_pdf.class.php b/core/class/declarationtva_pdf.class.php index ce2656b..1df28e2 100644 --- a/core/class/declarationtva_pdf.class.php +++ b/core/class/declarationtva_pdf.class.php @@ -157,13 +157,15 @@ class DeclarationTVA_PDF } /** - * Generate CA-3 declaration PDF + * Generate complete CA-3 declaration PDF (CA-3 form + detailed pages) * * @param int $declaration_id Declaration ID + * @param string $output_path Output file path + * @param string $status Status for PDF generation (export/validation) * @param string $outputlangs Output language * @return string|false PDF file path or false on error */ - public function generateCA3PDF($declaration_id, $outputlangs = '') + public function generateCompleteCA3PDF($declaration_id, $output_path, $status = 'export', $outputlangs = '') { global $conf, $langs, $user; @@ -182,11 +184,121 @@ class DeclarationTVA_PDF return false; } - // Get company information from Dolibarr's company configuration (same as used in CA-3 form) + // Get company information from Dolibarr's company configuration global $mysoc; + // For export, check if we have a custom fillable template + if ($status === 'export') { + $template_file = $this->getTemplatePath(); + if ($template_file && file_exists($template_file) && $this->isFillablePDF($template_file)) { + // Use fillable PDF template for export + return $this->fillFillablePDF($template_file, $output_path, $declaration, $ca3_data, $mysoc); + } + } + + // For validation or when no fillable template exists, use improved layout + return $this->generateImprovedPDF($output_path, $declaration, $ca3_data, $mysoc); + } + + /** + * Generate improved PDF with complete layout (CA-3 form + detailed pages) + * + * @param string $output_path Output file path + * @param DeclarationTVA $declaration Declaration object + * @param array $ca3_data CA-3 line data + * @param Societe $mysoc Company object + * @return bool Success + */ + private function generateImprovedPDF($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 journal entry table + $this->addJournalEntryTable($pdf, $declaration, $ca3_data); + + // Add bank journal entry table + $this->addBankJournalEntryTable($pdf, $declaration, $ca3_data); + + // 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 = 'Improved PDF generation failed: ' . $e->getMessage(); + return false; + } + } + + /** + * Generate CA-3 declaration PDF (legacy method for backward compatibility) + * + * @param int $declaration_id Declaration ID + * @param string $outputlangs Output language + * @return string|false PDF file path or false on error + */ + public function generateCA3PDF($declaration_id, $outputlangs = '') + { // Generate PDF filename - $filename = 'CA3_' . $declaration->declaration_number . '_' . date('Y-m-d') . '.pdf'; + $filename = 'CA3_' . $declaration_id . '_' . date('Y-m-d') . '.pdf'; $filepath = DOL_DATA_ROOT . '/declarationtva/' . $filename; // Ensure directory exists @@ -194,21 +306,8 @@ class DeclarationTVA_PDF dol_mkdir(DOL_DATA_ROOT . '/declarationtva/'); } - // Check if we have a custom template - $template_file = $this->getTemplatePath(); - if (!$template_file) { - $this->error = 'CA-3 template not found'; - return false; - } - - // Generate PDF using the improved method with all enhancements - $result = $this->fillPDFTemplate($template_file, $filepath, $declaration, $ca3_data, $mysoc); - - if ($result) { - return $filepath; - } else { - return false; - } + // Use the new unified method + return $this->generateCompleteCA3PDF($declaration_id, $filepath, 'export', $outputlangs); } /** @@ -220,29 +319,8 @@ class DeclarationTVA_PDF */ public function generateDetailedCA3PDF($declaration_id, $outputlangs = '') { - global $conf, $langs, $user; - - // Load declaration data - $declaration = new DeclarationTVA($this->db); - $result = $declaration->fetch($declaration_id); - if ($result <= 0) { - $this->error = 'Declaration not found'; - return false; - } - - - // Get CA-3 line data - $ca3_data = $declaration->getCA3Lines($declaration_id); - if (empty($ca3_data)) { - $this->error = 'No CA-3 data found'; - return false; - } - - // Get company information from Dolibarr's company configuration (same as used in CA-3 form) - global $mysoc; - // Generate PDF filename - $filename = 'CA3_' . $declaration->declaration_number . '_' . date('Y-m-d') . '.pdf'; + $filename = 'CA3_' . $declaration_id . '_' . date('Y-m-d') . '.pdf'; // Create VAT declarations documents directory structure $vat_declarations_dir = DOL_DATA_ROOT . '/declarationtva/validated/'; @@ -254,15 +332,8 @@ class DeclarationTVA_PDF $filepath = $vat_declarations_dir . $filename; - // For validation, always use the improved PDF generation with all enhancements - // This ensures validation PDF has the same layout as export - $result = $this->generateBasicPDF($filepath, $declaration, $ca3_data, $mysoc); - - if ($result) { - return $filepath; - } else { - return false; - } + // Use the new unified method for validation + return $this->generateCompleteCA3PDF($declaration_id, $filepath, 'validation', $outputlangs); } /** @@ -881,7 +952,7 @@ class DeclarationTVA_PDF $pdf->Cell(0, 8, 'CRÉDIT DE TVA: ' . price($declaration->vat_credit, 0, '', 1, 0), 0, 1); } - // Add detailed breakdown pages + // Add detailed breakdown pages using improved layout $this->addDetailPages($pdf, $declaration, $ca3_data); // Set total pages for footer after all pages are generated diff --git a/core/modules/modDeclarationTVA.class.php b/core/modules/modDeclarationTVA.class.php index 099951e..fa12a15 100644 --- a/core/modules/modDeclarationTVA.class.php +++ b/core/modules/modDeclarationTVA.class.php @@ -76,7 +76,7 @@ class modDeclarationTVA extends DolibarrModules $this->editor_squarred_logo = ''; // Must be image filename into the module/img directory followed with @modulename. Example: 'myimage.png@declarationtva' // Possible values for version are: 'development', 'experimental', 'dolibarr', 'dolibarr_deprecated', 'experimental_deprecated' or a version string like 'x.y.z' - $this->version = '2.5.29'; + $this->version = '2.5.30'; // Url to the file with your last numberversion of this module //$this->url_last_version = 'http://www.example.com/versionmodule.txt';