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!
This commit is contained in:
Frank Cools 2025-10-02 17:13:44 +02:00
parent 734a74c5f3
commit 49174f610f
4 changed files with 89 additions and 44 deletions

View File

@ -1,7 +1,7 @@
<?php
/**
* MVP Setup page for DeclarationTVA module
* Updated for Notice 4722 - Latest Official CA-3 Structure (3310-CA3-SD)
* Multi-select PCG account mapping using Dolibarr native style
*/
// Load Dolibarr environment
@ -37,10 +37,10 @@ if ($action == 'update_mappings') {
$ca3_definitions = $config->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 '<th>' . $langs->trans("LineLabel") . '</th>';
print '<th>' . $langs->trans("Description") . '</th>';
print '<th>' . $langs->trans("PCGAccounts") . '</th>';
print '<th>' . $langs->trans("AccountCode") . '</th>';
print '<th>' . $langs->trans("AccountSelection") . '</th>';
print '</tr>';
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 '<tr>';
print '<td><strong>' . $line . '</strong></td>';
@ -118,13 +112,23 @@ foreach ($lines_by_section as $section_code => $lines) {
print '<td><small>' . $definition['description'] . '</small></td>';
print '<td><small>' . $definition['pcg_accounts'] . '</small></td>';
print '<td>';
print '<select name="account_code_' . $line . '" class="flat">';
print '<option value="">' . $langs->trans("SelectAccount") . '</option>';
// Create multi-select field using Dolibarr style
$account_options = array();
foreach ($accounts as $account) {
$selected = (isset($mapping['account_code']) && $mapping['account_code'] == $account['account_number']) ? 'selected' : '';
print '<option value="' . $account['account_number'] . '" ' . $selected . '>' . $account['account_number'] . ' - ' . $account['label'] . '</option>';
$account_options[$account['account_number']] = $account['account_number'] . ' - ' . $account['label'];
}
// Use Dolibarr's native multi-select style
print '<select name="account_codes_' . $line . '[]" class="flat" multiple size="3" style="min-width: 300px;">';
foreach ($account_options as $account_code => $account_label) {
$selected = in_array($account_code, $selected_accounts) ? 'selected' : '';
print '<option value="' . $account_code . '" ' . $selected . '>' . $account_label . '</option>';
}
print '</select>';
// Add helper text
print '<br><small>' . $langs->trans("MultiSelectHelp") . '</small>';
print '</td>';
print '</tr>';
}
@ -143,19 +147,18 @@ print '</form>';
print '<div class="fiche">';
print '<div class="titre">' . $langs->trans("CurrentConfiguration") . '</div>';
if (empty($mappings)) {
if (empty($mappings_by_line)) {
print '<div class="info">' . $langs->trans("NoConfigurationFound") . '</div>';
} 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 '<table class="noborder centpercent">';
print '<tr class="liste_titre">';
print '<th>' . $langs->trans("CA3Line") . '</th>';
print '<th>' . $langs->trans("AccountCode") . '</th>';
print '<th>' . $langs->trans("Status") . '</th>';
print '<th>' . $langs->trans("SelectedAccounts") . '</th>';
print '<th>' . $langs->trans("AccountCount") . '</th>';
print '</tr>';
foreach ($section_mappings as $mapping) {
foreach ($section_mappings as $line => $account_codes) {
print '<tr>';
print '<td><strong>' . $mapping['ca3_line'] . '</strong></td>';
print '<td>' . $mapping['account_code'] . '</td>';
print '<td>' . ($mapping['is_active'] ? $langs->trans("Active") : $langs->trans("Inactive")) . '</td>';
print '<td><strong>' . $line . '</strong></td>';
print '<td>' . implode(', ', $account_codes) . '</td>';
print '<td>' . count($account_codes) . '</td>';
print '</tr>';
}
print '</table>';

View File

@ -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;
// 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);
$result = $this->db->query($sql);
return $result !== false;
// 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
*

View File

@ -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

View File

@ -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