hasRight("declarationtva", "declarationtva", "admin")) { accessforbidden(); } // Load language files $langs->load("declarationtva@declarationtva"); // Initialize objects $config = new DeclarationTVA_Config($db, $conf->entity); $form = new Form($db); // Handle form submission $action = GETPOST('action', 'alpha'); if ($action == 'update_mappings') { $ca3_definitions = $config->getCA3LineDefinitions(); $updated_count = 0; $debug_info = array(); foreach ($ca3_definitions as $line => $definition) { $account_codes = GETPOST('account_codes_' . $line, 'array'); $debug_info[] = "Line $line: " . (is_array($account_codes) ? implode(',', $account_codes) : 'empty'); // Process all lines, even empty ones (to clear mappings) $result = $config->updateAccountMapping($line, $account_codes); if ($result) { $updated_count++; } } // Debug output if (empty($debug_info)) { setEventMessages("Debug: No form data received", null, 'warnings'); } else { setEventMessages("Debug: " . implode(' | ', $debug_info), null, 'warnings'); } if ($updated_count > 0) { setEventMessages($langs->trans("ConfigurationUpdated"), null, 'mesgs'); } else { setEventMessages($langs->trans("NoChangesDetected"), null, 'warnings'); } } // Get current mappings $mappings_by_line = $config->getAccountMappingsByLine(); $accounts = $config->getAccountingAccounts(); $ca3_definitions = $config->getCA3LineDefinitions(); // Debug: Check if table exists and create if missing $table_name = MAIN_DB_PREFIX . "declarationtva_account_mappings"; $check_table_sql = "SHOW TABLES LIKE '" . $table_name . "'"; $table_exists = $db->query($check_table_sql); if ($table_exists && $db->num_rows($table_exists) > 0) { // Table exists - check if migration is needed $constraint_check = "SHOW INDEX FROM " . $table_name . " WHERE Key_name = 'uk_mapping_entity_line_account'"; $constraint_result = $db->query($constraint_check); if ($constraint_result && $db->num_rows($constraint_result) > 0) { setEventMessages("Debug: Table structure is correct (supports multiple accounts per line)", null, 'mesgs'); } else { setEventMessages("Debug: Table exists but needs migration - please deactivate and reactivate the module", null, 'warnings'); } } else { setEventMessages("Debug: Table $table_name does NOT exist - creating it now", null, 'warnings'); // Create the table manually $create_table_sql = "CREATE TABLE IF NOT EXISTS `" . $table_name . "` ( `rowid` int(11) NOT NULL AUTO_INCREMENT, `entity` int(11) NOT NULL DEFAULT 1, `ca3_line` varchar(8) NOT NULL COMMENT 'A1, A2, A3, A4, A5, 08, 09, 9B, 17, 20, 21, 22, 25, 26, 28, 29', `account_code` varchar(32) NOT NULL COMMENT 'PCG account code', `account_label` varchar(255) DEFAULT NULL, `vat_rate` decimal(5,2) DEFAULT NULL, `is_active` tinyint(1) DEFAULT 1, `created_date` datetime DEFAULT NULL, PRIMARY KEY (`rowid`), UNIQUE KEY `uk_mapping_entity_line_account` (`entity`, `ca3_line`, `account_code`), KEY `idx_ca3_line` (`ca3_line`), KEY `idx_account_code` (`account_code`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"; $create_result = $db->query($create_table_sql); if ($create_result) { setEventMessages("Debug: Table created successfully!", null, 'mesgs'); } else { setEventMessages("Debug: Failed to create table: " . $db->lasterror(), null, 'errors'); } } $section_headers = $config->getCA3SectionHeaders(); // Page title $title = $langs->trans("DeclarationTVASetup"); llxHeader('', $title); // Print page header print load_fiche_titre($title, '', 'title_accountancy'); // Print notice information print '
| ' . $langs->trans("CA3Line") . ' | '; print '' . $langs->trans("SelectedAccounts") . ' | '; print '' . $langs->trans("AccountCount") . ' | '; print '
|---|---|---|
| ' . $line . ' | '; print '' . implode(', ', $account_codes) . ' | '; print '' . count($account_codes) . ' | '; print '