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
$debug_messages = array();
$debug_messages[] = "Starting validation process...";
if ($declarationtva->validateDeclaration($id)) {
$debug_messages[] = "✓ Declaration validated successfully";
// Generate and save detailed PDF to Dolibarr documents
$debug_messages[] = "Loading PDF generator...";
require_once DOL_DOCUMENT_ROOT . '/custom/declarationtva/core/class/declarationtva_pdf.class.php';
$pdf_generator = new DeclarationTVA_PDF($db);
$debug_messages[] = "Generating detailed PDF...";
$pdf_path = $pdf_generator->generateDetailedCA3PDF($id);
if ($pdf_path && file_exists($pdf_path)) {
$debug_messages[] = "✓ PDF generated successfully: " . $pdf_path;
// Save PDF to Dolibarr documents
$debug_messages[] = "Saving PDF to documents...";
$save_result = $declarationtva->saveValidatedPDF($id, $pdf_path);
if (!$save_result) {
$debug_messages[] = "✗ PDF save failed: " . $declarationtva->error;
setEventMessages("Warning: Declaration validated but PDF save failed: " . $declarationtva->error, null, 'warnings');
} else {
$debug_messages[] = "✓ PDF generated and saved successfully: " . $pdf_path;
setEventMessages("PDF generated and saved successfully: " . $pdf_path, null, 'mesgs');
}
} else {
$debug_messages[] = "✗ PDF generation failed: " . $pdf_generator->error;
$debug_messages[] = "PDF path returned: " . ($pdf_path ? $pdf_path : 'NULL');
$debug_messages[] = "File exists: " . ($pdf_path && file_exists($pdf_path) ? 'YES' : 'NO');
setEventMessages("Warning: Declaration validated but PDF generation failed: " . $pdf_generator->error, null, 'warnings');
}
// Show all debug messages in a single message
$full_debug = implode('
', $debug_messages);
setEventMessages("Validation Debug Info:
" . $full_debug, null, 'mesgs');
} else {
$debug_messages[] = "✗ Declaration validation failed: " . $declarationtva->error;
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');
}
}
// 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 '
| ' . $langs->trans("DeclarationNumber") . ' | '; print '' . $declarationtva->declaration_number . ' | '; print '
| ' . $langs->trans("DeclarationName") . ' | '; print '' . $declarationtva->declaration_name . ' | '; print '
| ' . $langs->trans("Period") . ' | '; print '' . dol_print_date($start_date, 'day') . ' - ' . dol_print_date($end_date, 'day') . ' | '; print '
| ' . $langs->trans("Status") . ' | '; print '' . $langs->trans("Status" . ucfirst($declarationtva->status)) . ' | '; print '
| ' . $langs->trans("CreatedDate") . ' | '; print '' . dol_print_date($declarationtva->created_date, 'dayhour') . ' | '; print '
| A. ' . $langs->trans("CA3SectionA") . ' | '; print '|||
| ' . $langs->trans("CA3Line") . ' | '; print '' . $langs->trans("Description") . ' | '; print '' . $langs->trans("Amount") . ' | '; print '|
|---|---|---|---|
| ' . $line . ' | '; print '' . $description . ' | '; print '' . formatAmountWithOriginal($data['vat_amount'], $data['line_label']) . ' | '; print '|
| B. ' . $langs->trans("CA3SectionB") . ' | '; print '|||
| ' . $langs->trans("CA3Line") . ' | '; print '' . $langs->trans("Description") . ' | '; print '' . $langs->trans("BaseAmount") . ' | '; print '' . $langs->trans("VATAmount") . ' | '; print '
| ' . $line . ' | '; print '' . $description . ' | '; print '' . formatAmountWithOriginal($data['base_amount'], $data['line_label'], 'base') . ' | '; print '' . formatAmountWithOriginal($data['vat_amount'], $data['line_label']) . ' | '; print '
| 16 | '; print 'Total de la TVA brute due (lignes 08 à 5B) | '; print '' . formatAmount($data['vat_amount']) . ' | '; print '|
| 17 | '; print '' . $description . ' | '; print '' . formatAmountWithOriginal($data['vat_amount'], $data['line_label']) . ' | '; print '|
| 18 | '; print '' . $description . ' | '; print '' . formatAmountWithOriginal($data['vat_amount'], $data['line_label']) . ' | '; print '|
| TVA DÉDUCTIBLE | '; print '|||
| ' . $langs->trans("CA3Line") . ' | '; print '' . $langs->trans("Description") . ' | '; print '' . $langs->trans("Amount") . ' | '; print '|
| ' . $line . ' | '; print '' . $description . ' | '; print '' . formatAmountWithOriginal($data['vat_amount'], $data['line_label']) . ' | '; print '|
| 23 | '; print 'Total TVA déductible (ligne 19 à 2C) | '; print '' . formatAmount($data['vat_amount']) . ' | '; print '|
| D. ' . $langs->trans("CA3SectionD") . ' | '; print '|||
| ' . $langs->trans("CA3Line") . ' | '; print '' . $langs->trans("Description") . ' | '; print '' . $langs->trans("Amount") . ' | '; print '|
| ' . $line . ' | '; print '' . $description . ' | '; print '' . formatAmount($data['vat_amount']) . ' | '; print '|
| ' . $langs->trans("NoCA3Data") . ' | '; print '|||