From 49174f610f9a9c62a9fc0e23733f6adeec7029cb Mon Sep 17 00:00:00 2001 From: Frank Cools Date: Thu, 2 Oct 2025 17:13:44 +0200 Subject: [PATCH] Implement multi-select PCG account mapping using Dolibarr native style Multi-Select Account Mapping: - Replaced single account dropdown with multi-select field - Users can now select multiple PCG accounts for each CA-3 line - Uses Dolibarr native multi-select style with proper styling - Supports Ctrl/Cmd key for multiple selection Enhanced Configuration Interface: - Multi-select dropdown for each CA-3 line - Account selection from Dolibarr's chart of accounts - Real-time account validation against existing accounts - Better visual layout with proper sizing Technical Updates: - Updated config class to handle multiple account selections - Added method to get account mappings grouped by CA-3 line - Enhanced form submission to process array of account codes - Improved current configuration display Database Handling: - Deactivate existing mappings before adding new ones - Support for multiple accounts per CA-3 line - Proper account code validation - Clean data structure for multi-select support User Experience: - Native Dolibarr multi-select styling - Helper text for multi-selection instructions - Better account display with codes and labels - Improved current configuration summary Language Support: - Added English translations for multi-select interface - Added French translations for multi-select interface - Complete bilingual support for new features The configuration interface now supports multiple PCG account selection using Dolibarr's native multi-select style! --- admin/setup_mvp.php | 57 +++++++++--------- core/class/declarationtva_config.class.php | 68 ++++++++++++++++------ langs/en_US/declarationtva.lang | 4 ++ langs/fr_FR/declarationtva.lang | 4 ++ 4 files changed, 89 insertions(+), 44 deletions(-) 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 '' . $langs->trans("LineLabel") . ''; print '' . $langs->trans("Description") . ''; print '' . $langs->trans("PCGAccounts") . ''; - print '' . $langs->trans("AccountCode") . ''; + print '' . $langs->trans("AccountSelection") . ''; print ''; foreach ($lines as $line => $definition) { - $mapping = isset($account_mappings[$line]) ? $account_mappings[$line] : array(); + $selected_accounts = isset($mappings_by_line[$line]) ? $mappings_by_line[$line] : array(); print ''; print '' . $line . ''; @@ -118,13 +112,23 @@ foreach ($lines_by_section as $section_code => $lines) { print '' . $definition['description'] . ''; print '' . $definition['pcg_accounts'] . ''; print ''; - print ''; + foreach ($account_options as $account_code => $account_label) { + $selected = in_array($account_code, $selected_accounts) ? 'selected' : ''; + print ''; } print ''; + + // Add helper text + print '
' . $langs->trans("MultiSelectHelp") . ''; print ''; print ''; } @@ -143,19 +147,18 @@ print ''; 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 ''; - print ''; - print ''; + print ''; + print ''; print ''; - foreach ($section_mappings as $mapping) { + foreach ($section_mappings as $line => $account_codes) { print ''; - print ''; - print ''; - print ''; + print ''; + print ''; + print ''; print ''; } print '
' . $langs->trans("CA3Line") . '' . $langs->trans("AccountCode") . '' . $langs->trans("Status") . '' . $langs->trans("SelectedAccounts") . '' . $langs->trans("AccountCount") . '
' . $mapping['ca3_line'] . '' . $mapping['account_code'] . '' . ($mapping['is_active'] ? $langs->trans("Active") : $langs->trans("Inactive")) . '' . $line . '' . implode(', ', $account_codes) . '' . count($account_codes) . '
'; 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