From 8d42e47b52ea47db43c3897fe21642ed24570903 Mon Sep 17 00:00:00 2001 From: Frank Cools Date: Mon, 6 Oct 2025 17:07:49 +0200 Subject: [PATCH] Fix validation error for missing database columns - Enhanced validateDeclaration() to check for column existence - Falls back to basic validation if enhanced columns don't exist - Updated migration script with proper column existence checks - Uses dynamic SQL to avoid errors on existing columns - System now works with or without the additional columns - Graceful degradation for different database states --- core/class/declarationtva.class.php | 27 ++++++++--- run_migration.php | 66 +++++++++++++++++++++++++++ sql/migration_add_documents_table.sql | 56 ++++++++++++++++++++--- 3 files changed, 135 insertions(+), 14 deletions(-) create mode 100644 run_migration.php diff --git a/core/class/declarationtva.class.php b/core/class/declarationtva.class.php index 5fdfae2..08cff94 100644 --- a/core/class/declarationtva.class.php +++ b/core/class/declarationtva.class.php @@ -999,13 +999,26 @@ class DeclarationTVA */ public function validateDeclaration($declaration_id) { - // First, try to update the declaration status - $sql = "UPDATE " . MAIN_DB_PREFIX . "declarationtva_declarations - SET status = 'validated', - validated_date = NOW(), - validated_by = " . $this->db->escape($GLOBALS['user']->id) . " - WHERE rowid = " . (int)$declaration_id . " - AND entity = " . $this->entity; + // First, check if the validated_date and validated_by columns exist + $sql_check_columns = "SHOW COLUMNS FROM " . MAIN_DB_PREFIX . "declarationtva_declarations LIKE 'validated_date'"; + $result_check = $this->db->query($sql_check_columns); + $has_validated_columns = ($result_check && $this->db->num_rows($result_check) > 0); + + if ($has_validated_columns) { + // Use the enhanced version with validated_date and validated_by + $sql = "UPDATE " . MAIN_DB_PREFIX . "declarationtva_declarations + SET status = 'validated', + validated_date = NOW(), + validated_by = " . $this->db->escape($GLOBALS['user']->id) . " + WHERE rowid = " . (int)$declaration_id . " + AND entity = " . $this->entity; + } else { + // Use the basic version without the additional columns + $sql = "UPDATE " . MAIN_DB_PREFIX . "declarationtva_declarations + SET status = 'validated' + WHERE rowid = " . (int)$declaration_id . " + AND entity = " . $this->entity; + } $result = $this->db->query($sql); if (!$result) { diff --git a/run_migration.php b/run_migration.php new file mode 100644 index 0000000..40c7c3b --- /dev/null +++ b/run_migration.php @@ -0,0 +1,66 @@ +load("declarationtva@declarationtva"); + +echo "

DeclarationTVA Database Migration

"; + +// Read the migration SQL file +$migration_file = __DIR__ . '/sql/migration_add_documents_table.sql'; +if (!file_exists($migration_file)) { + die("Migration file not found: " . $migration_file); +} + +$sql_content = file_get_contents($migration_file); +$sql_statements = explode(';', $sql_content); + +$success_count = 0; +$error_count = 0; + +foreach ($sql_statements as $sql) { + $sql = trim($sql); + if (empty($sql) || strpos($sql, '--') === 0) { + continue; + } + + echo "

Executing: " . htmlspecialchars(substr($sql, 0, 100)) . "...

"; + + $result = $db->query($sql); + if ($result) { + echo "

✓ Success

"; + $success_count++; + } else { + echo "

✗ Error: " . htmlspecialchars($db->lasterror()) . "

"; + $error_count++; + } +} + +echo "

Migration Summary

"; +echo "

Successful statements: " . $success_count . "

"; +echo "

Failed statements: " . $error_count . "

"; + +if ($error_count == 0) { + echo "

Migration completed successfully!

"; +} else { + echo "

Migration completed with errors. Please check the error messages above.

"; +} + +echo "

← Back to Declaration List

"; +?> diff --git a/sql/migration_add_documents_table.sql b/sql/migration_add_documents_table.sql index 504ecb1..f27c701 100644 --- a/sql/migration_add_documents_table.sql +++ b/sql/migration_add_documents_table.sql @@ -16,11 +16,53 @@ CREATE TABLE IF NOT EXISTS `llx_declarationtva_documents` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- Add validated_date and validated_by columns to declarations table if they don't exist -ALTER TABLE `llx_declarationtva_declarations` -ADD COLUMN IF NOT EXISTS `validated_date` datetime DEFAULT NULL, -ADD COLUMN IF NOT EXISTS `validated_by` int(11) DEFAULT NULL; +-- Check if validated_date column exists first +SET @sql = (SELECT IF( + (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'llx_declarationtva_declarations' + AND COLUMN_NAME = 'validated_date') = 0, + 'ALTER TABLE `llx_declarationtva_declarations` ADD COLUMN `validated_date` datetime DEFAULT NULL', + 'SELECT "validated_date column already exists" as message' +)); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; --- Add indexes for performance -ALTER TABLE `llx_declarationtva_declarations` -ADD INDEX IF NOT EXISTS `idx_validated_date` (`validated_date`), -ADD INDEX IF NOT EXISTS `idx_validated_by` (`validated_by`); +-- Check if validated_by column exists first +SET @sql = (SELECT IF( + (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'llx_declarationtva_declarations' + AND COLUMN_NAME = 'validated_by') = 0, + 'ALTER TABLE `llx_declarationtva_declarations` ADD COLUMN `validated_by` int(11) DEFAULT NULL', + 'SELECT "validated_by column already exists" as message' +)); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +-- Add indexes for performance (only if they don't exist) +SET @sql = (SELECT IF( + (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'llx_declarationtva_declarations' + AND INDEX_NAME = 'idx_validated_date') = 0, + 'ALTER TABLE `llx_declarationtva_declarations` ADD INDEX `idx_validated_date` (`validated_date`)', + 'SELECT "idx_validated_date index already exists" as message' +)); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @sql = (SELECT IF( + (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'llx_declarationtva_declarations' + AND INDEX_NAME = 'idx_validated_by') = 0, + 'ALTER TABLE `llx_declarationtva_declarations` ADD INDEX `idx_validated_by` (`validated_by`)', + 'SELECT "idx_validated_by index already exists" as message' +)); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt;