From b9725755a26450fac95b35c8de57a6bbfafe1674 Mon Sep 17 00:00:00 2001 From: Frank Cools Date: Thu, 2 Oct 2025 20:51:34 +0200 Subject: [PATCH] Fix object initialization order in declaration view Fixed: - Moved object initialization before action processing - Prevents 'Call to a member function on null' error - recalculateCA3Amounts() now works properly - Objects are available when action handlers run --- declarationtva_view.php | 163 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) diff --git a/declarationtva_view.php b/declarationtva_view.php index e69de29..95d848e 100644 --- a/declarationtva_view.php +++ b/declarationtva_view.php @@ -0,0 +1,163 @@ +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 ''; +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 ''; + +print ''; +print ''; +print ''; +print ''; +print ''; + +// Get actual CA-3 lines from database +$ca3_lines = $declarationtva->getCA3Lines($id); + +if (empty($ca3_lines)) { + print ''; + print ''; + print ''; +} else { + foreach ($ca3_lines as $line) { + print ''; + print ''; + print ''; + print ''; + print ''; + } +} + +print '
' . $langs->trans("CA3Line") . '' . $langs->trans("Description") . '' . $langs->trans("Amount") . '
' . $langs->trans("NoCA3Data") . '
' . $line['ca3_line'] . '' . $line['line_label'] . '' . price($line['vat_amount']) . '
'; +print '
'; + +// Print actions +print '
'; +print '
' . $langs->trans("Actions") . '
'; + +print '
'; + +// Recalculate button (always available) +print '' . $langs->trans("Recalculate") . ' '; + +if ($declarationtva->status == 'draft') { + print '' . $langs->trans("Validate") . ' '; +} elseif ($declarationtva->status == 'validated') { + print '' . $langs->trans("Submit") . ' '; +} + +print '' . $langs->trans("BackToList") . ''; + +print '
'; +print '
'; + +// Print footer +llxFooter(); +?>