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
This commit is contained in:
Frank Cools 2025-10-06 17:07:49 +02:00
parent 105036adc2
commit 8d42e47b52
3 changed files with 135 additions and 14 deletions

View File

@ -999,13 +999,26 @@ class DeclarationTVA
*/
public function validateDeclaration($declaration_id)
{
// First, try to update the declaration status
// 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) {

66
run_migration.php Normal file
View File

@ -0,0 +1,66 @@
<?php
/**
* Run database migration for DeclarationTVA module
* This script creates the necessary tables for the validation system
*/
// Load Dolibarr environment
if (file_exists('../main.inc.php')) {
$res = @include '../main.inc.php';
} elseif (file_exists('../../main.inc.php')) {
$res = @include '../../main.inc.php';
} else {
die("Include of main fails");
}
if (!$res) {
die("Include of main fails");
}
// Load language files
$langs->load("declarationtva@declarationtva");
echo "<h2>DeclarationTVA Database Migration</h2>";
// 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 "<p>Executing: " . htmlspecialchars(substr($sql, 0, 100)) . "...</p>";
$result = $db->query($sql);
if ($result) {
echo "<p style='color: green;'>✓ Success</p>";
$success_count++;
} else {
echo "<p style='color: red;'>✗ Error: " . htmlspecialchars($db->lasterror()) . "</p>";
$error_count++;
}
}
echo "<h3>Migration Summary</h3>";
echo "<p>Successful statements: " . $success_count . "</p>";
echo "<p>Failed statements: " . $error_count . "</p>";
if ($error_count == 0) {
echo "<p style='color: green; font-weight: bold;'>Migration completed successfully!</p>";
} else {
echo "<p style='color: red; font-weight: bold;'>Migration completed with errors. Please check the error messages above.</p>";
}
echo "<p><a href='declarationtvaindex.php'>← Back to Declaration List</a></p>";
?>

View File

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