- 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
67 lines
1.8 KiB
PHP
67 lines
1.8 KiB
PHP
<?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>";
|
|
?>
|