hasRight("declarationtva", "declarationtva", "write")) { accessforbidden(); } // Load language files $langs->load("declarationtva@declarationtva"); // Ensure tables exist (create if missing) $tables_to_check = array( 'declarationtva_declarations', 'declarationtva_periods', 'declarationtva_ca3_lines' ); foreach ($tables_to_check as $table_name) { $full_table_name = MAIN_DB_PREFIX . $table_name; $check_table_sql = "SHOW TABLES LIKE '" . $full_table_name . "'"; $table_exists = $db->query($check_table_sql); if (!$table_exists || $db->num_rows($table_exists) == 0) { // Create the table based on schema if ($table_name == 'declarationtva_declarations') { $create_table_sql = "CREATE TABLE IF NOT EXISTS `" . $full_table_name . "` ( `rowid` int(11) NOT NULL AUTO_INCREMENT, `entity` int(11) NOT NULL DEFAULT 1, `period_id` int(11) NOT NULL DEFAULT 0, `declaration_number` varchar(32) NOT NULL, `declaration_name` varchar(255) DEFAULT NULL, `start_date` date DEFAULT NULL, `end_date` date DEFAULT NULL, `status` varchar(32) DEFAULT 'draft' COMMENT 'draft, validated, submitted', `total_vat_collected` decimal(15,2) DEFAULT 0.00, `total_vat_deductible` decimal(15,2) DEFAULT 0.00, `net_vat_due` decimal(15,2) DEFAULT 0.00, `vat_credit` decimal(15,2) DEFAULT 0.00, `submission_date` datetime DEFAULT NULL, `created_date` datetime DEFAULT NULL, PRIMARY KEY (`rowid`), UNIQUE KEY `uk_declaration_entity_number` (`entity`, `declaration_number`), KEY `idx_period_id` (`period_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"; } elseif ($table_name == 'declarationtva_periods') { $create_table_sql = "CREATE TABLE IF NOT EXISTS `" . $full_table_name . "` ( `rowid` int(11) NOT NULL AUTO_INCREMENT, `entity` int(11) NOT NULL DEFAULT 1, `period_name` varchar(32) NOT NULL COMMENT 'Q1-2024, Q2-2024, etc.', `start_date` date NOT NULL, `end_date` date NOT NULL, `status` varchar(32) DEFAULT 'draft' COMMENT 'draft, validated, submitted', `created_date` datetime DEFAULT NULL, PRIMARY KEY (`rowid`), UNIQUE KEY `uk_period_entity_name` (`entity`, `period_name`), KEY `idx_period_dates` (`start_date`, `end_date`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"; } elseif ($table_name == 'declarationtva_ca3_lines') { $create_table_sql = "CREATE TABLE IF NOT EXISTS `" . $full_table_name . "` ( `rowid` int(11) NOT NULL AUTO_INCREMENT, `declaration_id` int(11) NOT NULL, `ca3_line` varchar(8) NOT NULL, `line_label` varchar(255) DEFAULT NULL, `base_amount` decimal(15,2) DEFAULT 0.00, `vat_amount` decimal(15,2) DEFAULT 0.00, `total_amount` decimal(15,2) DEFAULT 0.00, `created_date` datetime DEFAULT NULL, PRIMARY KEY (`rowid`), KEY `idx_declaration_id` (`declaration_id`), KEY `idx_ca3_line` (`ca3_line`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"; } $db->query($create_table_sql); } } // Initialize objects $declarationtva = new DeclarationTVA($db, $conf->entity); $config = new DeclarationTVA_Config($db, $conf->entity); $period = new DeclarationTVA_Period($db, $conf->entity); // Handle form submission $action = GETPOST('action', 'alpha'); $declaration_name = GETPOST('declaration_name', 'alpha'); $start_date = GETPOST('start_date', 'alpha'); $end_date = GETPOST('end_date', 'alpha'); $error = ''; $success = ''; if ($action == 'create' && !empty($start_date) && !empty($end_date)) { // Create the declaration with dates $declaration_id = $declarationtva->createDeclarationWithDates($start_date, $end_date, $declaration_name); if ($declaration_id > 0) { $success = $langs->trans("DeclarationCreated"); // Redirect to view the created declaration header("Location: declarationtva_view.php?id=" . $declaration_id); exit; } else { $error = $langs->trans("ErrorCreatingDeclaration") . ": " . $declarationtva->error; } } elseif ($action == 'create' && (empty($start_date) || empty($end_date))) { $error = $langs->trans("ErrorMissingDates"); } // Page title $title = $langs->trans("CreateDeclaration"); llxHeader('', $title); // Print page header print load_fiche_titre($title, '', 'title_accountancy'); // Display messages if ($error) { setEventMessages($error, null, 'errors'); } if ($success) { setEventMessages($success, null, 'mesgs'); } // Print form print '
'; print '
' . $langs->trans("DeclarationDetails") . '
'; print '
'; print ''; print ''; print ''; // Date range (now mandatory and at the top) print ''; print ''; print ''; print ''; // Declaration name print ''; print ''; print ''; print ''; print '
' . $langs->trans("SelectPeriod") . ''; print ''; print ' - '; print ''; print '
' . $langs->trans("DateRangeHelp") . ''; print '
' . $langs->trans("DeclarationName") . ''; print ''; print '
' . $langs->trans("DeclarationNameHelp") . ''; print '
'; // Buttons print '
'; print ''; print '' . $langs->trans("Cancel") . ''; print '
'; print '
'; print '
'; // Print configuration section print '
'; print '
' . $langs->trans("DeclarationTVAConfiguration") . '
'; print '
'; print $langs->trans("ConfigurationInfo") . ' '; print '' . $langs->trans("ConfigurePCGAccounts") . ''; print '
'; print '
'; // Print footer llxFooter(); ?>