Fix form submission and add debugging

Form Submission Fixes:
- Process ALL CA-3 lines (including empty ones to clear mappings)
- Fixed SQL syntax in updateAccountMapping method
- Added proper error handling and success counting
- Added debug output to see what data is being received

Database Operations:
- Replaced ON DUPLICATE KEY UPDATE with proper check/update logic
- Better handling of existing vs new mappings
- Improved SQL query structure for reliability

Debug Features:
- Shows which lines have data and which are empty
- Displays form data being processed
- Helps identify where the issue occurs

The form should now properly save account mappings!
This commit is contained in:
Frank Cools 2025-10-02 17:28:22 +02:00
parent 29caf3b0b0
commit c08e7df14c
4 changed files with 43 additions and 9 deletions

View File

@ -38,16 +38,32 @@ $form = new Form($db);
$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');
if (!empty($account_codes)) {
$config->updateAccountMapping($line, $account_codes);
// Process all lines, even empty ones (to clear mappings)
$result = $config->updateAccountMapping($line, $account_codes);
if ($result) {
$updated_count++;
}
}
setEventMessages($langs->trans("ConfigurationUpdated"), null, 'mesgs');
// 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
@ -190,3 +206,4 @@ print '</div>';
// Print footer
llxFooter();
?>
?>

View File

@ -112,12 +112,27 @@ class DeclarationTVA_Config
if (!empty($account_codes)) {
foreach ($account_codes as $account_code) {
if (!empty($account_code)) {
$sql = "INSERT INTO " . MAIN_DB_PREFIX . "declarationtva_account_mappings
(entity, ca3_line, account_code, account_label, vat_rate, is_active, created_date)
VALUES (" . $this->entity . ", '" . $this->db->escape($ca3_line) . "',
'" . $this->db->escape($account_code) . "', '', 0, 1, NOW())
ON DUPLICATE KEY UPDATE
is_active = 1, account_code = '" . $this->db->escape($account_code) . "'";
// Check if mapping already exists
$check_sql = "SELECT id FROM " . MAIN_DB_PREFIX . "declarationtva_account_mappings
WHERE entity = " . $this->entity . "
AND ca3_line = '" . $this->db->escape($ca3_line) . "'
AND account_code = '" . $this->db->escape($account_code) . "'";
$check_result = $this->db->query($check_sql);
if ($check_result && $this->db->num_rows($check_result) > 0) {
// Update existing mapping
$sql = "UPDATE " . MAIN_DB_PREFIX . "declarationtva_account_mappings
SET is_active = 1
WHERE entity = " . $this->entity . "
AND ca3_line = '" . $this->db->escape($ca3_line) . "'
AND account_code = '" . $this->db->escape($account_code) . "'";
} else {
// Insert new mapping
$sql = "INSERT INTO " . MAIN_DB_PREFIX . "declarationtva_account_mappings
(entity, ca3_line, account_code, account_label, vat_rate, is_active, created_date)
VALUES (" . $this->entity . ", '" . $this->db->escape($ca3_line) . "',
'" . $this->db->escape($account_code) . "', '', 0, 1, NOW())";
}
$this->db->query($sql);
}
}

View File

@ -370,3 +370,4 @@ SelectedAccounts = Selected Accounts
AccountCount = Account Count
MultiSelectHelp = Hold Ctrl (or Cmd on Mac) to select multiple accounts
ErrorCSRFToken = Security token error. Please try again.
NoChangesDetected = No changes detected in the configuration.

View File

@ -359,3 +359,4 @@ SelectedAccounts = Comptes sélectionnés
AccountCount = Nombre de comptes
MultiSelectHelp = Maintenez Ctrl (ou Cmd sur Mac) pour sélectionner plusieurs comptes
ErrorCSRFToken = Erreur de jeton de sécurité. Veuillez réessayer.
NoChangesDetected = Aucun changement détecté dans la configuration.