hasRight("declarationtva", "declarationtva", "read")) { accessforbidden(); } // Load language files $langs->load("declarationtva@declarationtva"); // Get declaration ID $id = GETPOST('id', 'int'); if (empty($id)) { accessforbidden(); } // Initialize objects $declarationtva = new DeclarationTVA($db, $conf->entity); $config = new DeclarationTVA_Config($db, $conf->entity); $period = new DeclarationTVA_Period($db, $conf->entity); // Handle actions $action = GETPOST('action', 'alpha'); $token = GETPOST('token', 'alpha'); if ($action == 'recalculate' && $token) { if ($declarationtva->recalculateCA3Amounts($id)) { setEventMessages($langs->trans("DeclarationRecalculated"), null, 'mesgs'); } else { setEventMessages($langs->trans("ErrorRecalculatingDeclaration"), null, 'errors'); } } if ($action == 'export_pdf') { // Load PDF generator require_once DOL_DOCUMENT_ROOT . '/custom/declarationtva/core/class/declarationtva_pdf.class.php'; $pdf_generator = new DeclarationTVA_PDF($db); // Generate PDF $pdf_path = $pdf_generator->generateCA3PDF($id); if ($pdf_path && file_exists($pdf_path)) { // Set headers for PDF download header('Content-Type: application/pdf'); header('Content-Disposition: attachment; filename="CA3_' . $declarationtva->declaration_number . '.pdf"'); header('Content-Length: ' . filesize($pdf_path)); // Output PDF readfile($pdf_path); exit; } else { setEventMessages($pdf_generator->error ?: $langs->trans("ErrorGeneratingPDF"), null, 'errors'); } } if ($action == 'validate' && $token) { // Validate the declaration if ($declarationtva->validateDeclaration($id)) { // Generate and save detailed PDF to Dolibarr documents require_once DOL_DOCUMENT_ROOT . '/custom/declarationtva/core/class/declarationtva_pdf.class.php'; $pdf_generator = new DeclarationTVA_PDF($db); $pdf_path = $pdf_generator->generateDetailedCA3PDF($id); if ($pdf_path && file_exists($pdf_path)) { // Save PDF to Dolibarr documents $save_result = $declarationtva->saveValidatedPDF($id, $pdf_path); if (!$save_result) { setEventMessages("Warning: Declaration validated but PDF save failed: " . $declarationtva->error, null, 'warnings'); } else { setEventMessages($langs->trans("DeclarationValidated"), null, 'mesgs'); } } else { setEventMessages("Warning: Declaration validated but PDF generation failed: " . $pdf_generator->error, null, 'warnings'); } } else { setEventMessages($langs->trans("ErrorValidatingDeclaration") . ": " . $declarationtva->error, null, 'errors'); } } if ($action == 'unvalidate' && $token) { // Unvalidate the declaration (for testing purposes) if ($declarationtva->unvalidateDeclaration($id)) { setEventMessages($langs->trans("DeclarationUnvalidated"), null, 'mesgs'); } else { setEventMessages($langs->trans("ErrorUnvalidatingDeclaration") . ": " . $declarationtva->error, null, 'errors'); } } if ($action == 'submit' && $token) { // Submit the declaration (create accounting entries and update status) if ($declarationtva->submitDeclaration($id)) { setEventMessages($langs->trans("DeclarationSubmitted"), null, 'mesgs'); } else { setEventMessages($langs->trans("ErrorSubmittingDeclaration") . ": " . $declarationtva->error, null, 'errors'); } } // Fetch declaration if ($declarationtva->fetch($id) < 0) { setEventMessages($langs->trans("DeclarationNotFound"), null, 'errors'); header("Location: declarationtvaindex.php"); exit; } // Use declaration's own dates $start_date = $declarationtva->start_date; $end_date = $declarationtva->end_date; // Page title $title = $langs->trans("ViewDeclaration") . ' - ' . $declarationtva->declaration_number; llxHeader('', $title); // Print page header print load_fiche_titre($title, '', 'title_accountancy'); // Print declaration details print '
'; print '
' . $langs->trans("DeclarationDetails") . '
'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print '
' . $langs->trans("DeclarationNumber") . '' . $declarationtva->declaration_number . '
' . $langs->trans("DeclarationName") . '' . $declarationtva->declaration_name . '
' . $langs->trans("Period") . '' . dol_print_date($start_date, 'day') . ' - ' . dol_print_date($end_date, 'day') . '
' . $langs->trans("Status") . '' . $langs->trans("Status" . ucfirst($declarationtva->status)) . '
' . $langs->trans("CreatedDate") . '' . dol_print_date($declarationtva->created_date, 'dayhour') . '
'; print '
'; // Print CA-3 amounts (placeholder for now) print '
'; print '
' . $langs->trans("CA3Amounts") . '
'; print ''; // Get actual CA-3 lines from database $ca3_lines = $declarationtva->getCA3Lines($id); // Get CA-3 line definitions for proper descriptions $ca3_definitions = $config->getCA3LineDefinitions(); // Helper function to format amounts with original values in brackets function formatAmountWithOriginal($amount, $line_label, $amount_type = 'vat') { // If amount is zero, show empty field if ($amount == 0) { return ''; } // Parse original amounts from line_label if they exist if (strpos($line_label, '|ORIGINAL_') !== false) { $parts = explode('|', $line_label); $original_info = $parts[1] ?? ''; $pattern = $amount_type == 'base' ? '/ORIGINAL_BASE:([0-9.]+)/' : '/ORIGINAL_VAT:([0-9.]+)/'; if (preg_match($pattern, $original_info, $matches)) { $original_amount = floatval($matches[1]); if ($original_amount != $amount) { return number_format($amount, 0) . ' (' . number_format($original_amount, 2) . ')'; } } } return number_format($amount, 0); } // Helper function to format simple amounts (no original values) function formatAmount($amount) { // If amount is zero, show empty field if ($amount == 0) { return ''; } return number_format($amount, 0); } // Create a lookup array for quick access $ca3_data = array(); foreach ($ca3_lines as $line) { $ca3_data[$line['ca3_line']] = $line; } // Section A: Opérations imposables print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; $section_a_lines = array('A1', 'A2', 'A3', 'A4', 'A5', 'E1', 'E2', 'E3', 'E4', 'E5', 'E6', 'F1', 'F2', 'F6', 'F7', 'F8'); foreach ($section_a_lines as $line) { $data = isset($ca3_data[$line]) ? $ca3_data[$line] : array('line_label' => '', 'vat_amount' => 0); $description = isset($ca3_definitions[$line]) ? $ca3_definitions[$line]['label'] : $data['line_label']; print ''; print ''; print ''; print ''; print ''; // Add dropdown row for account details print ''; print ''; print ''; } // Section B: TVA due print ''; print ''; print ''; // Column headers for lines 08, 09, 9B with base and VAT columns print ''; print ''; print ''; print ''; print ''; print ''; $base_vat_lines = array('08', '09', '9B'); foreach ($base_vat_lines as $line) { $data = isset($ca3_data[$line]) ? $ca3_data[$line] : array('line_label' => '', 'base_amount' => 0, 'vat_amount' => 0); $description = isset($ca3_definitions[$line]) ? $ca3_definitions[$line]['label'] : $data['line_label']; print ''; print ''; print ''; print ''; print ''; print ''; // Add dropdown row for account details print ''; print ''; print ''; } // Line 16: Subtotal (calculated automatically) $data = isset($ca3_data['16']) ? $ca3_data['16'] : array('line_label' => '', 'vat_amount' => 0); print ''; print ''; print ''; print ''; print ''; // Line 17 $data = isset($ca3_data['17']) ? $ca3_data['17'] : array('line_label' => '', 'vat_amount' => 0); $description = isset($ca3_definitions['17']) ? $ca3_definitions['17']['label'] : $data['line_label']; print ''; print ''; print ''; print ''; print ''; // Add dropdown row for account details print ''; print ''; print ''; // Line 18: Monaco operations $data = isset($ca3_data['18']) ? $ca3_data['18'] : array('line_label' => '', 'vat_amount' => 0); $description = isset($ca3_definitions['18']) ? $ca3_definitions['18']['label'] : $data['line_label']; print ''; print ''; print ''; print ''; print ''; // Add dropdown row for account details print ''; print ''; print ''; // Section B sub-section: TVA DÉDUCTIBLE print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; $section_c_lines = array('19', '20', '21', '22'); foreach ($section_c_lines as $line) { $data = isset($ca3_data[$line]) ? $ca3_data[$line] : array('line_label' => '', 'vat_amount' => 0); $description = isset($ca3_definitions[$line]) ? $ca3_definitions[$line]['label'] : $data['line_label']; print ''; print ''; print ''; print ''; print ''; // Add dropdown row for account details print ''; print ''; print ''; } // Line 23: Subtotal (calculated automatically) $data = isset($ca3_data['23']) ? $ca3_data['23'] : array('line_label' => '', 'vat_amount' => 0); print ''; print ''; print ''; print ''; print ''; // Section D: Résultat print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; $section_d_lines = array('25', '26', '27', 'TD', '28', '32'); foreach ($section_d_lines as $line) { $data = isset($ca3_data[$line]) ? $ca3_data[$line] : array('line_label' => '', 'vat_amount' => 0); // Prioritize database line_label over definitions (for calculated lines) $description = !empty($data['line_label']) ? $data['line_label'] : (isset($ca3_definitions[$line]) ? $ca3_definitions[$line]['label'] : ''); // Special formatting for line 32 (bold) $is_line_32 = ($line == '32'); $bold_style = $is_line_32 ? 'font-weight: bold;' : ''; print ''; print ''; print ''; print ''; print ''; } // Show message if no data if (empty($ca3_lines)) { print ''; print ''; print ''; } print '
A. ' . $langs->trans("CA3SectionA") . '
' . $langs->trans("CA3Line") . '' . $langs->trans("Description") . '' . $langs->trans("Amount") . '
' . $line . '' . $description . '' . formatAmountWithOriginal($data['vat_amount'], $data['line_label']) . '
B. ' . $langs->trans("CA3SectionB") . '
' . $langs->trans("CA3Line") . '' . $langs->trans("Description") . '' . $langs->trans("BaseAmount") . '' . $langs->trans("VATAmount") . '
' . $line . '' . $description . '' . formatAmountWithOriginal($data['base_amount'], $data['line_label'], 'base') . '' . formatAmountWithOriginal($data['vat_amount'], $data['line_label']) . '
16Total de la TVA brute due (lignes 08 à 5B)' . formatAmount($data['vat_amount']) . '
17' . $description . '' . formatAmountWithOriginal($data['vat_amount'], $data['line_label']) . '
18' . $description . '' . formatAmountWithOriginal($data['vat_amount'], $data['line_label']) . '
TVA DÉDUCTIBLE
' . $langs->trans("CA3Line") . '' . $langs->trans("Description") . '' . $langs->trans("Amount") . '
' . $line . '' . $description . '' . formatAmountWithOriginal($data['vat_amount'], $data['line_label']) . '
23Total TVA déductible (ligne 19 à 2C)' . formatAmount($data['vat_amount']) . '
D. ' . $langs->trans("CA3SectionD") . '
' . $langs->trans("CA3Line") . '' . $langs->trans("Description") . '' . $langs->trans("Amount") . '
' . $line . '' . $description . '' . formatAmount($data['vat_amount']) . '
' . $langs->trans("NoCA3Data") . '
'; print '
'; // Print actions print '
'; print '
' . $langs->trans("Actions") . '
'; print '
'; // Recalculate button (only for draft status) if ($declarationtva->status == 'draft') { print '' . $langs->trans("Recalculate") . ' '; } // PDF Export button (only for draft status) if ($declarationtva->status == 'draft') { print '' . $langs->trans("ExportPDF") . ' '; } if ($declarationtva->status == 'draft') { // Validation button with confirmation dialog print '' . $langs->trans("Validate") . ' '; } elseif ($declarationtva->status == 'validated') { print '' . $langs->trans("Submit") . ' '; // Add unvalidate button for testing print '' . $langs->trans("Unvalidate") . ' '; } print '' . $langs->trans("BackToList") . ''; print '
'; print '
'; // Add CSS for dropdown styling print ''; // Add JavaScript for dropdown functionality print ''; // Print footer llxFooter(); ?>