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');
}
}
// 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 '| ' . $langs->trans("DeclarationNumber") . ' | ';
print '' . $declarationtva->declaration_number . ' | ';
print '
';
print '';
print '| ' . $langs->trans("DeclarationName") . ' | ';
print '' . $declarationtva->declaration_name . ' | ';
print '
';
print '';
print '| ' . $langs->trans("Period") . ' | ';
print '' . dol_print_date($start_date, 'day') . ' - ' . dol_print_date($end_date, 'day') . ' | ';
print '
';
print '';
print '| ' . $langs->trans("Status") . ' | ';
print '' . $langs->trans("Status" . ucfirst($declarationtva->status)) . ' | ';
print '
';
print '';
print '| ' . $langs->trans("CreatedDate") . ' | ';
print '' . dol_print_date($declarationtva->created_date, 'dayhour') . ' | ';
print '
';
print '
';
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 '| A. ' . $langs->trans("CA3SectionA") . ' | ';
print '
';
print '';
print '| ' . $langs->trans("CA3Line") . ' | ';
print '' . $langs->trans("Description") . ' | ';
print '' . $langs->trans("Amount") . ' | ';
print '
';
$section_a_lines = array('A1', 'A2', 'A3', 'A4', 'A5');
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 '| ' . $line . ' | ';
print '' . $description . ' | ';
print '' . formatAmountWithOriginal($data['vat_amount'], $data['line_label']) . ' | ';
print '
';
}
// Section B: TVA due
print '';
print '| B. ' . $langs->trans("CA3SectionB") . ' | ';
print '
';
// Special header for lines 08, 09, 9B with base and VAT columns
print '';
print '| ' . $langs->trans("BaseHTAndVATByRate") . ' | ';
print '
';
print '';
print '| ' . $langs->trans("CA3Line") . ' | ';
print '' . $langs->trans("Description") . ' | ';
print '' . $langs->trans("BaseAmount") . ' | ';
print '' . $langs->trans("VATAmount") . ' | ';
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 '| ' . $line . ' | ';
print '' . $description . ' | ';
print '' . formatAmountWithOriginal($data['base_amount'], $data['line_label'], 'base') . ' | ';
print '' . formatAmountWithOriginal($data['vat_amount'], $data['line_label']) . ' | ';
print '
';
}
// Line 16: Subtotal (calculated automatically)
$data = isset($ca3_data['16']) ? $ca3_data['16'] : array('line_label' => '', 'vat_amount' => 0);
print '';
print '| 16 | ';
print 'Sous-total TVA due (08 + 09 + 9B) | ';
print '' . formatAmount($data['vat_amount']) . ' | ';
print '
';
// Reset to normal layout for line 17
print '';
print '| ' . $langs->trans("CA3Line") . ' | ';
print '' . $langs->trans("Description") . ' | ';
print '' . $langs->trans("Amount") . ' | ';
print '
';
$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 '| 17 | ';
print '' . $description . ' | ';
print '' . formatAmountWithOriginal($data['vat_amount'], $data['line_label']) . ' | ';
print '
';
// Section C: TVA déductible
print '';
print '| C. ' . $langs->trans("CA3SectionC") . ' | ';
print '
';
print '';
print '| ' . $langs->trans("CA3Line") . ' | ';
print '' . $langs->trans("Description") . ' | ';
print '' . $langs->trans("Amount") . ' | ';
print '
';
$section_c_lines = array('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 '| ' . $line . ' | ';
print '' . $description . ' | ';
print '' . formatAmountWithOriginal($data['vat_amount'], $data['line_label']) . ' | ';
print '
';
}
// Line 23: Subtotal (calculated automatically)
$data = isset($ca3_data['23']) ? $ca3_data['23'] : array('line_label' => '', 'vat_amount' => 0);
print '';
print '| 23 | ';
print 'Sous-total TVA déductible (20 + 21 + 22) | ';
print '' . formatAmount($data['vat_amount']) . ' | ';
print '
';
// Section D: Résultat
print '';
print '| D. ' . $langs->trans("CA3SectionD") . ' | ';
print '
';
print '';
print '| ' . $langs->trans("CA3Line") . ' | ';
print '' . $langs->trans("Description") . ' | ';
print '' . $langs->trans("Amount") . ' | ';
print '
';
$section_d_lines = array('25', '26', 'TD', '28', '29');
foreach ($section_d_lines as $line) {
$data = isset($ca3_data[$line]) ? $ca3_data[$line] : array('line_label' => '', 'vat_amount' => 0);
// Special handling for line TD
if ($line == 'TD') {
$description = 'TVA due (montant à payer)';
} else {
$description = isset($ca3_definitions[$line]) ? $ca3_definitions[$line]['label'] : $data['line_label'];
}
print '';
print '| ' . $line . ' | ';
print '' . $description . ' | ';
print '' . formatAmount($data['vat_amount']) . ' | ';
print '
';
}
// Show message if no data
if (empty($ca3_lines)) {
print '';
print '| ' . $langs->trans("NoCA3Data") . ' | ';
print '
';
}
print '
';
print '
';
// Print actions
print '';
print '
' . $langs->trans("Actions") . '
';
print '
';
print '
';
// Print footer
llxFooter();
?>