diff --git a/admin/setup_mvp.php b/admin/setup_mvp.php
index 8af718c..f470554 100644
--- a/admin/setup_mvp.php
+++ b/admin/setup_mvp.php
@@ -1,7 +1,7 @@
getCA3LineDefinitions();
foreach ($ca3_definitions as $line => $definition) {
- $account_code = GETPOST('account_code_' . $line, 'alpha');
+ $account_codes = GETPOST('account_codes_' . $line, 'array');
- if (!empty($account_code)) {
- $config->updateAccountMapping($line, $account_code, '', '');
+ if (!empty($account_codes)) {
+ $config->updateAccountMapping($line, $account_codes);
}
}
@@ -48,13 +48,7 @@ if ($action == 'update_mappings') {
}
// Get current mappings
-$mappings = $config->getAllAccountMappings();
-$account_mappings = array();
-foreach ($mappings as $mapping) {
- $account_mappings[$mapping['ca3_line']] = $mapping;
-}
-
-// Get available accounting accounts
+$mappings_by_line = $config->getAccountMappingsByLine();
$accounts = $config->getAccountingAccounts();
$ca3_definitions = $config->getCA3LineDefinitions();
$section_headers = $config->getCA3SectionHeaders();
@@ -106,11 +100,11 @@ foreach ($lines_by_section as $section_code => $lines) {
print '
';
print '
' . $langs->trans("CurrentConfiguration") . '
';
-if (empty($mappings)) {
+if (empty($mappings_by_line)) {
print '
' . $langs->trans("NoConfigurationFound") . '
';
} else {
// Group by section for display
$mappings_by_section = array();
- foreach ($mappings as $mapping) {
- $line = $mapping['ca3_line'];
+ foreach ($mappings_by_line as $line => $account_codes) {
if (isset($ca3_definitions[$line])) {
$section = $ca3_definitions[$line]['section'];
if (!isset($mappings_by_section[$section])) {
$mappings_by_section[$section] = array();
}
- $mappings_by_section[$section][] = $mapping;
+ $mappings_by_section[$section][$line] = $account_codes;
}
}
@@ -167,15 +170,15 @@ if (empty($mappings)) {
print '
';
print '';
print '| ' . $langs->trans("CA3Line") . ' | ';
- print '' . $langs->trans("AccountCode") . ' | ';
- print '' . $langs->trans("Status") . ' | ';
+ print '' . $langs->trans("SelectedAccounts") . ' | ';
+ print '' . $langs->trans("AccountCount") . ' | ';
print '
';
- foreach ($section_mappings as $mapping) {
+ foreach ($section_mappings as $line => $account_codes) {
print '';
- print '| ' . $mapping['ca3_line'] . ' | ';
- print '' . $mapping['account_code'] . ' | ';
- print '' . ($mapping['is_active'] ? $langs->trans("Active") : $langs->trans("Inactive")) . ' | ';
+ print '' . $line . ' | ';
+ print '' . implode(', ', $account_codes) . ' | ';
+ print '' . count($account_codes) . ' | ';
print '
';
}
print '
';
diff --git a/core/class/declarationtva_config.class.php b/core/class/declarationtva_config.class.php
index ea6edb4..6049963 100644
--- a/core/class/declarationtva_config.class.php
+++ b/core/class/declarationtva_config.class.php
@@ -94,28 +94,36 @@ class DeclarationTVA_Config
}
/**
- * Update account mapping
+ * Update account mapping for multiple accounts
*
* @param string $ca3_line CA-3 line code
- * @param string $account_code Account code
- * @param string $account_label Account label
- * @param float $vat_rate VAT rate
+ * @param array $account_codes Array of account codes
* @return bool Success
*/
- public function updateAccountMapping($ca3_line, $account_code, $account_label, $vat_rate)
+ public function updateAccountMapping($ca3_line, $account_codes)
{
- $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) . "', '" . $this->db->escape($account_label) . "',
- " . $vat_rate . ", 1, NOW())
- ON DUPLICATE KEY UPDATE
- account_code = '" . $this->db->escape($account_code) . "',
- account_label = '" . $this->db->escape($account_label) . "',
- vat_rate = " . $vat_rate;
-
- $result = $this->db->query($sql);
- return $result !== false;
+ // First, deactivate all existing mappings for this CA-3 line
+ $sql = "UPDATE " . MAIN_DB_PREFIX . "declarationtva_account_mappings
+ SET is_active = 0
+ WHERE entity = " . $this->entity . " AND ca3_line = '" . $this->db->escape($ca3_line) . "'";
+ $this->db->query($sql);
+
+ // Then insert/activate new mappings
+ 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) . "'";
+ $this->db->query($sql);
+ }
+ }
+ }
+
+ return true;
}
/**
@@ -148,6 +156,32 @@ class DeclarationTVA_Config
return $mappings;
}
+ /**
+ * Get account mappings grouped by CA-3 line
+ *
+ * @return array Account mappings grouped by CA-3 line
+ */
+ public function getAccountMappingsByLine()
+ {
+ $sql = "SELECT ca3_line, account_code FROM " . MAIN_DB_PREFIX . "declarationtva_account_mappings
+ WHERE entity = " . $this->entity . " AND is_active = 1
+ ORDER BY ca3_line, account_code";
+
+ $result = $this->db->query($sql);
+ $mappings = array();
+
+ if ($result) {
+ while ($obj = $this->db->fetch_object($result)) {
+ if (!isset($mappings[$obj->ca3_line])) {
+ $mappings[$obj->ca3_line] = array();
+ }
+ $mappings[$obj->ca3_line][] = $obj->account_code;
+ }
+ }
+
+ return $mappings;
+ }
+
/**
* Get available accounting accounts from Dolibarr
*
diff --git a/langs/en_US/declarationtva.lang b/langs/en_US/declarationtva.lang
index 2b7d25e..cec0999 100644
--- a/langs/en_US/declarationtva.lang
+++ b/langs/en_US/declarationtva.lang
@@ -365,3 +365,7 @@ CA3Line29 = VAT credit to carry forward or refund
# Form Labels
Description = Description
PCGAccounts = PCG Accounts
+AccountSelection = Account Selection
+SelectedAccounts = Selected Accounts
+AccountCount = Account Count
+MultiSelectHelp = Hold Ctrl (or Cmd on Mac) to select multiple accounts
diff --git a/langs/fr_FR/declarationtva.lang b/langs/fr_FR/declarationtva.lang
index a60dcda..6af63b9 100644
--- a/langs/fr_FR/declarationtva.lang
+++ b/langs/fr_FR/declarationtva.lang
@@ -354,3 +354,7 @@ CA3Line29 = Crédit de TVA à reporter ou remboursement
# Labels de formulaire
Description = Description
PCGAccounts = Comptes PCG
+AccountSelection = Sélection de comptes
+SelectedAccounts = Comptes sélectionnés
+AccountCount = Nombre de comptes
+MultiSelectHelp = Maintenez Ctrl (ou Cmd sur Mac) pour sélectionner plusieurs comptes