Add database debugging and fix schema for multiple accounts

Database Schema Fix:
- Fixed unique constraint to allow multiple accounts per CA-3 line
- Changed from (entity, ca3_line) to (entity, ca3_line, account_code)
- Added migration script to update existing tables
- Updated table comments to reflect new CA-3 line structure

Debug Features Added:
- Database operation logging in updateAccountMapping method
- Table existence and structure checking in setup page
- Detailed error logging for SQL operations
- Form data processing visibility

The issue was likely the unique constraint preventing multiple accounts per line!
This commit is contained in:
Frank Cools 2025-10-02 17:30:55 +02:00
parent c08e7df14c
commit c74e09993f
4 changed files with 57 additions and 6 deletions

View File

@ -70,6 +70,27 @@ if ($action == 'update_mappings') {
$mappings_by_line = $config->getAccountMappingsByLine();
$accounts = $config->getAccountingAccounts();
$ca3_definitions = $config->getCA3LineDefinitions();
// Debug: Check if table exists and show structure
$table_name = MAIN_DB_PREFIX . "declarationtva_account_mappings";
$check_table_sql = "SHOW TABLES LIKE '" . $table_name . "'";
$table_exists = $db->query($check_table_sql);
if ($table_exists && $db->num_rows($table_exists) > 0) {
setEventMessages("Debug: Table $table_name exists", null, 'mesgs');
// Show table structure
$structure_sql = "DESCRIBE " . $table_name;
$structure_result = $db->query($structure_sql);
if ($structure_result) {
$structure_info = array();
while ($row = $db->fetch_array($structure_result)) {
$structure_info[] = $row['Field'] . ' (' . $row['Type'] . ')';
}
setEventMessages("Debug: Table structure: " . implode(', ', $structure_info), null, 'mesgs');
}
} else {
setEventMessages("Debug: Table $table_name does NOT exist - need to run database setup", null, 'errors');
}
$section_headers = $config->getCA3SectionHeaders();
// Page title

View File

@ -102,11 +102,15 @@ class DeclarationTVA_Config
*/
public function updateAccountMapping($ca3_line, $account_codes)
{
// Debug: Log what we're trying to save
error_log("DeclarationTVA: updateAccountMapping called for line $ca3_line with codes: " . print_r($account_codes, true));
// 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);
$result1 = $this->db->query($sql);
error_log("DeclarationTVA: Deactivate query result: " . ($result1 ? 'SUCCESS' : 'FAILED - ' . $this->db->lasterror()));
// Then insert/activate new mappings
if (!empty($account_codes)) {
@ -118,6 +122,7 @@ class DeclarationTVA_Config
AND ca3_line = '" . $this->db->escape($ca3_line) . "'
AND account_code = '" . $this->db->escape($account_code) . "'";
$check_result = $this->db->query($check_sql);
error_log("DeclarationTVA: Check query result: " . ($check_result ? 'SUCCESS' : 'FAILED - ' . $this->db->lasterror()));
if ($check_result && $this->db->num_rows($check_result) > 0) {
// Update existing mapping
@ -126,14 +131,17 @@ class DeclarationTVA_Config
WHERE entity = " . $this->entity . "
AND ca3_line = '" . $this->db->escape($ca3_line) . "'
AND account_code = '" . $this->db->escape($account_code) . "'";
$result2 = $this->db->query($sql);
error_log("DeclarationTVA: Update query result: " . ($result2 ? 'SUCCESS' : 'FAILED - ' . $this->db->lasterror()));
} 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())";
$result3 = $this->db->query($sql);
error_log("DeclarationTVA: Insert query result: " . ($result3 ? 'SUCCESS' : 'FAILED - ' . $this->db->lasterror()));
}
$this->db->query($sql);
}
}
}

View File

@ -0,0 +1,21 @@
--
-- Migration script to fix account mappings table
-- Fixes the unique constraint to allow multiple accounts per CA-3 line
--
-- Drop the old unique constraint that prevents multiple accounts per line
ALTER TABLE `llx_declarationtva_account_mappings`
DROP INDEX `uk_mapping_entity_line`;
-- Add new unique constraint that allows multiple accounts per line
-- but prevents duplicate account mappings
ALTER TABLE `llx_declarationtva_account_mappings`
ADD UNIQUE KEY `uk_mapping_entity_line_account` (`entity`, `ca3_line`, `account_code`);
-- Add index on account_code for better performance
ALTER TABLE `llx_declarationtva_account_mappings`
ADD INDEX `idx_account_code` (`account_code`);
-- Update the comment to reflect the new structure
ALTER TABLE `llx_declarationtva_account_mappings`
MODIFY COLUMN `ca3_line` varchar(8) NOT NULL COMMENT 'A1, A2, A3, A4, A5, 08, 09, 9B, 17, 20, 21, 22, 25, 26, 28, 29';

View File

@ -19,19 +19,20 @@ CREATE TABLE IF NOT EXISTS `llx_declarationtva_config` (
UNIQUE KEY `uk_config_entity_key` (`entity`, `config_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Simplified PCG account mappings (one account per CA-3 line for MVP)
-- PCG account mappings (multiple accounts per CA-3 line supported)
CREATE TABLE IF NOT EXISTS `llx_declarationtva_account_mappings` (
`rowid` int(11) NOT NULL AUTO_INCREMENT,
`entity` int(11) NOT NULL DEFAULT 1,
`ca3_line` varchar(8) NOT NULL COMMENT 'A1, A2, B1, B2, B3, B4, 17, 20, 21, 22, 28, 29',
`ca3_line` varchar(8) NOT NULL COMMENT 'A1, A2, A3, A4, A5, 08, 09, 9B, 17, 20, 21, 22, 25, 26, 28, 29',
`account_code` varchar(32) NOT NULL COMMENT 'PCG account code',
`account_label` varchar(255) DEFAULT NULL,
`vat_rate` decimal(5,2) DEFAULT NULL,
`is_active` tinyint(1) DEFAULT 1,
`created_date` datetime DEFAULT NULL,
PRIMARY KEY (`rowid`),
UNIQUE KEY `uk_mapping_entity_line` (`entity`, `ca3_line`),
KEY `idx_ca3_line` (`ca3_line`)
UNIQUE KEY `uk_mapping_entity_line_account` (`entity`, `ca3_line`, `account_code`),
KEY `idx_ca3_line` (`ca3_line`),
KEY `idx_account_code` (`account_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Declaration periods (quarterly only for MVP)