diff --git a/admin/setup_mvp.php b/admin/setup_mvp.php index 4bbc699..cb6c626 100644 --- a/admin/setup_mvp.php +++ b/admin/setup_mvp.php @@ -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 @@ -189,4 +205,5 @@ print ''; // Print footer llxFooter(); +?> ?> \ No newline at end of file diff --git a/core/class/declarationtva_config.class.php b/core/class/declarationtva_config.class.php index 6049963..e381cca 100644 --- a/core/class/declarationtva_config.class.php +++ b/core/class/declarationtva_config.class.php @@ -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); } } diff --git a/langs/en_US/declarationtva.lang b/langs/en_US/declarationtva.lang index 828dc0e..40f967e 100644 --- a/langs/en_US/declarationtva.lang +++ b/langs/en_US/declarationtva.lang @@ -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. diff --git a/langs/fr_FR/declarationtva.lang b/langs/fr_FR/declarationtva.lang index 356edbf..3318a9b 100644 --- a/langs/fr_FR/declarationtva.lang +++ b/langs/fr_FR/declarationtva.lang @@ -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.