38471-vm/db_migrations_outlet.php
2026-02-25 09:58:14 +00:00

48 lines
1.4 KiB
PHP

<?php
require 'db/config.php';
try {
$pdo = db();
// Create outlets table
$pdo->exec("CREATE TABLE IF NOT EXISTS `outlets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`address` text,
`phone` varchar(50),
`status` enum('active','inactive') DEFAULT 'active',
`created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");
// Insert a default Main Branch if table is empty
$stmt = $pdo->query("SELECT COUNT(*) FROM outlets");
if ($stmt->fetchColumn() == 0) {
$pdo->exec("INSERT INTO outlets (name, address, status) VALUES ('Main Branch', 'Headquarters', 'active')");
}
// Add outlet_id to tables
$tablesToUpdate = [
'users',
'invoices',
'purchases',
'lpos',
'quotations',
'expenses',
'payments'
];
foreach ($tablesToUpdate as $table) {
$stmt = $pdo->query("SHOW COLUMNS FROM `$table` LIKE 'outlet_id'");
if ($stmt->rowCount() == 0) {
$pdo->exec("ALTER TABLE `$table` ADD COLUMN `outlet_id` int(11) NULL DEFAULT 1 AFTER `id`");
echo "Added outlet_id to $table\n";
} else {
echo "outlet_id already exists in $table\n";
}
}
echo "Migration completed successfully.";
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}