diff --git a/core/modules/modDeclarationTVA.class.php b/core/modules/modDeclarationTVA.class.php index 1300c95..1361d5a 100644 --- a/core/modules/modDeclarationTVA.class.php +++ b/core/modules/modDeclarationTVA.class.php @@ -472,10 +472,31 @@ class modDeclarationTVA extends DolibarrModules global $conf, $langs; // Create tables of module at module activation - //$result = $this->_load_tables('/install/mysql/', 'declarationtva'); - $result = $this->_load_tables('/custom/declarationtva/sql/'); + // Try different paths to find the SQL files + $sql_paths = array( + '/custom/declarationtva/sql/', + '/declarationtva/sql/', + '/install/mysql/' + ); + + $result = -1; + foreach ($sql_paths as $path) { + dol_syslog("DeclarationTVA: Trying SQL path: " . $path, LOG_INFO); + $result = $this->_load_tables($path); + dol_syslog("DeclarationTVA: SQL loading result for $path: " . $result, LOG_INFO); + if ($result >= 0) { + dol_syslog("DeclarationTVA: SQL loading successful with path: " . $path, LOG_INFO); + break; + } + } + if ($result < 0) { - return -1; // Do not activate module if error 'not allowed' returned when loading module SQL queries (the _load_table run sql with run_sql with the error allowed parameter set to 'default') + dol_syslog("DeclarationTVA: All SQL loading attempts failed, trying manual table creation", LOG_WARNING); + $manual_result = $this->_create_tables_manually(); + if ($manual_result < 0) { + dol_syslog("DeclarationTVA: Manual table creation also failed", LOG_ERR); + return -1; + } } // Run migration to fix account mappings table structure @@ -604,4 +625,58 @@ class modDeclarationTVA extends DolibarrModules dol_syslog("DeclarationTVA: Migration completed successfully", LOG_INFO); return 1; } + + /** + * Manually create tables if SQL file loading fails + * + * @return int 1 if OK, -1 if KO + */ + private function _create_tables_manually() + { + global $conf; + + dol_syslog("DeclarationTVA: Creating tables manually", LOG_INFO); + + // Create account mappings table + $sql = "CREATE TABLE IF NOT EXISTS `" . MAIN_DB_PREFIX . "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, 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_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"; + + $result = $this->db->query($sql); + if (!$result) { + dol_syslog("DeclarationTVA: Failed to create account_mappings table: " . $this->db->lasterror(), LOG_ERR); + return -1; + } + + // Create config table + $sql = "CREATE TABLE IF NOT EXISTS `" . MAIN_DB_PREFIX . "declarationtva_config` ( + `rowid` int(11) NOT NULL AUTO_INCREMENT, + `entity` int(11) NOT NULL DEFAULT 1, + `config_key` varchar(64) NOT NULL, + `config_value` text, + `created_date` datetime DEFAULT NULL, + PRIMARY KEY (`rowid`), + UNIQUE KEY `uk_config_entity_key` (`entity`, `config_key`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"; + + $result = $this->db->query($sql); + if (!$result) { + dol_syslog("DeclarationTVA: Failed to create config table: " . $this->db->lasterror(), LOG_ERR); + return -1; + } + + dol_syslog("DeclarationTVA: Manual table creation completed successfully", LOG_INFO); + return 1; + } }