hasRight("declarationtva", "declarationtva", "read")) { accessforbidden(); } // Load language files $langs->load("declarationtva@declarationtva"); // 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'); $declaration_id = GETPOST('declaration_id', 'int'); $period_id = GETPOST('period_id', 'int'); // Process actions $token = GETPOST('token', 'alpha'); if ($action == 'create_declaration' && $period_id > 0) { $declaration_id = $declarationtva->createDeclaration($period_id); if ($declaration_id > 0) { setEventMessages($langs->trans("DeclarationCreated"), null, 'mesgs'); } else { setEventMessages($langs->trans("ErrorCreatingDeclaration") . ": " . $declarationtva->error, null, 'errors'); } } elseif ($action == 'validate_declaration' && $declaration_id > 0 && $token) { if ($declarationtva->validateDeclaration($declaration_id)) { setEventMessages($langs->trans("DeclarationValidated"), null, 'mesgs'); } else { setEventMessages($langs->trans("ErrorValidatingDeclaration"), null, 'errors'); } } elseif ($action == 'submit_declaration' && $declaration_id > 0 && $token) { if ($declarationtva->submitDeclaration($declaration_id)) { setEventMessages($langs->trans("DeclarationSubmitted"), null, 'mesgs'); } else { setEventMessages($langs->trans("ErrorSubmittingDeclaration"), null, 'errors'); } } elseif ($action == 'delete_declaration' && $declaration_id > 0 && $token) { if ($declarationtva->deleteDeclaration($declaration_id)) { setEventMessages($langs->trans("DeclarationDeleted"), null, 'mesgs'); } else { setEventMessages($langs->trans("ErrorDeletingDeclaration"), null, 'errors'); } } // Get data for display $declarations = array(); // Get all declarations $sql = "SELECT d.*, p.period_name, p.start_date as period_start_date, p.end_date as period_end_date FROM " . MAIN_DB_PREFIX . "declarationtva_declarations d LEFT JOIN " . MAIN_DB_PREFIX . "declarationtva_periods p ON d.period_id = p.rowid WHERE d.entity = " . $conf->entity . " ORDER BY d.created_date DESC"; $result = $db->query($sql); if ($result) { while ($obj = $db->fetch_object($result)) { // Use declaration's own dates $start_date = $obj->start_date ? $obj->start_date : $obj->period_start_date; $end_date = $obj->end_date ? $obj->end_date : $obj->period_end_date; $declarations[] = array( 'rowid' => $obj->rowid, 'declaration_number' => $obj->declaration_number, 'status' => $obj->status, 'total_vat_collected' => $obj->total_vat_collected, 'total_vat_deductible' => $obj->total_vat_deductible, 'net_vat_due' => $obj->net_vat_due, 'vat_credit' => $obj->vat_credit, 'created_date' => $obj->created_date, 'start_date' => $start_date, 'end_date' => $end_date ); } } // Page title $title = $langs->trans("DeclarationTVAMainInterface"); llxHeader('', $title); // Print page header with create button print load_fiche_titre($title, '' . $langs->trans("CreateDeclaration") . ''); // Print declarations section print '
| ' . $langs->trans("DeclarationNumber") . ' | '; print '' . $langs->trans("Period") . ' | '; print '' . $langs->trans("Status") . ' | '; print '' . $langs->trans("NetVATDue") . ' | '; print '' . $langs->trans("Document") . ' | '; print '' . $langs->trans("Actions") . ' | '; print '||
|---|---|---|---|---|---|---|---|
| ' . $d['declaration_number'] . ' | '; print '' . dol_print_date($d['start_date'], 'day') . ' - ' . dol_print_date($d['end_date'], 'day') . ' | '; // Status with icon $status_icon = ''; if ($d['status'] == 'validated') { $status_icon = ' '; } elseif ($d['status'] == 'draft') { $status_icon = ' '; } print '' . $langs->trans("Status" . ucfirst($d['status'])) . $status_icon . ' | '; // Show VAT amount with proper label - use CA-3 line values instead of main table $declarationtva = new DeclarationTVA($db, $conf->entity); $ca3_lines = $declarationtva->getCA3Lines($d['rowid']); // Get line 32 (net VAT due) and lines 26/27 (VAT credit) $line_32_amount = 0; $line_26_amount = 0; $line_27_amount = 0; foreach ($ca3_lines as $line) { if ($line['ca3_line'] == '32') { $line_32_amount = $line['vat_amount']; } elseif ($line['ca3_line'] == '26') { $line_26_amount = $line['vat_amount']; } elseif ($line['ca3_line'] == '27') { $line_27_amount = $line['vat_amount']; } } // Determine which value to show based on CA-3 lines if ($line_32_amount > 0) { // VAT due case - use line 32 print '' . price($line_32_amount) . ' ' . $langs->trans("NetVATDue") . ' | ';
} elseif ($line_26_amount > 0 || $line_27_amount > 0) {
// VAT refund case - use line 26 or 27 (whichever is not 0)
$refund_amount = $line_26_amount > 0 ? $line_26_amount : $line_27_amount;
print '' . price($refund_amount) . ' ' . $langs->trans("VATToReceive") . ' | ';
} else {
// Balanced case
print '' . price(0) . ' ' . $langs->trans("VATBalanced") . ' | ';
}
// Document column
print ''; if ($d['status'] == 'validated') { // Check if document exists $has_document = $declarationtva->hasValidatedDocument($d['rowid']); if ($has_document) { $pdf_path = $declarationtva->getValidatedPDFPath($d['rowid']); if ($pdf_path) { // Create a secure download link through the download handler $download_url = $_SERVER['PHP_SELF'] . '?action=download_pdf&declaration_id=' . $d['rowid'] . '&token=' . newToken(); print ''; print ' PDF'; print ''; } else { print ''; } } else { print ''; } } else { print ''; } print ' | '; print ''; if ($d['status'] == 'draft') { print '' . $langs->trans("Delete") . ' '; } print '' . $langs->trans("View") . ''; print ' | '; print '