36656-vm/db/setup.php
Flatlogic Bot 140149344e 1.0
2025-12-04 16:34:54 +00:00

109 lines
4.2 KiB
PHP

<?php
function setup_database() {
try {
$pdo = db();
// Check if tables already exist
$stmt = $pdo->query("SELECT 1 FROM equivalence_catalog LIMIT 1");
if ($stmt !== false && $stmt->fetch()) {
// Tables seem to exist, do nothing
return;
}
} catch (PDOException $e) {
// Table doesn't exist, proceed with creation. This is expected on first run.
}
try {
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// 1. Shipping Lines Table
$pdo->exec("CREATE TABLE IF NOT EXISTS shipping_lines (
ShippingLineCode VARCHAR(10) PRIMARY KEY,
ShippingLineName VARCHAR(255) NOT NULL,
IsActive BOOLEAN NOT NULL DEFAULT TRUE
)");
// 2. Customs Container Types Table
$pdo->exec("CREATE TABLE IF NOT EXISTS customs_container_types (
CustomsKey VARCHAR(10) PRIMARY KEY,
CustomsDescription VARCHAR(255) NOT NULL,
IsActive BOOLEAN NOT NULL DEFAULT TRUE
)");
// 3. N4 Container Types Table
$pdo->exec("CREATE TABLE IF NOT EXISTS n4_container_types (
N4Key VARCHAR(10) PRIMARY KEY,
N4Description VARCHAR(255) NOT NULL,
IsActive BOOLEAN NOT NULL DEFAULT TRUE,
CustomsKeyFK VARCHAR(10),
FOREIGN KEY (CustomsKeyFK) REFERENCES customs_container_types(CustomsKey)
)");
// 4. Equivalence Catalog Table
$pdo->exec("CREATE TABLE IF NOT EXISTS equivalence_catalog (
EquivalenceID INT AUTO_INCREMENT PRIMARY KEY,
ShippingLineCode VARCHAR(10),
CustomsKeyFK VARCHAR(10),
N4KeyFK VARCHAR(10),
EffectiveDate DATE NOT NULL,
IsActive BOOLEAN NOT NULL DEFAULT TRUE,
FOREIGN KEY (ShippingLineCode) REFERENCES shipping_lines(ShippingLineCode),
FOREIGN KEY (CustomsKeyFK) REFERENCES customs_container_types(CustomsKey),
FOREIGN KEY (N4KeyFK) REFERENCES n4_container_types(N4Key),
UNIQUE (ShippingLineCode, CustomsKeyFK, N4KeyFK)
)");
// 5. Audit Log Table
$pdo->exec("CREATE TABLE IF NOT EXISTS audit_log (
LogID INT AUTO_INCREMENT PRIMARY KEY,
EquivalenceIDFK INT,
Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
`User` VARCHAR(255),
Action VARCHAR(50),
OldValue TEXT,
NewValue TEXT,
FOREIGN KEY (EquivalenceIDFK) REFERENCES equivalence_catalog(EquivalenceID) ON DELETE SET NULL
)");
// --- MOCK DATA INSERTION ---
// Shipping Lines
$pdo->exec("INSERT INTO shipping_lines (ShippingLineCode, ShippingLineName) VALUES
('MSCU', 'Mediterranean Shipping Company'),
('ONEY', 'Ocean Network Express'),
('MAEU', 'Maersk Line')");
// Customs Types
$pdo->exec("INSERT INTO customs_container_types (CustomsKey, CustomsDescription) VALUES
('20GP', '20ft General Purpose'),
('40HC', '40ft High Cube'),
('20RF', '20ft Reefer')");
// N4 Types
$pdo->exec("INSERT INTO n4_container_types (N4Key, N4Description, CustomsKeyFK) VALUES
('GP20', 'General Purpose 20 FT', '20GP'),
('DV20', 'Dry Van 20 FT', '20GP'),
('HC40', 'High Cube 40 FT', '40HC'),
('HQ40', 'High Quality 40 FT', '40HC'),
('RF20', 'Refrigerated 20 FT', '20RF')");
// Equivalence Mappings
$pdo->exec("INSERT INTO equivalence_catalog (ShippingLineCode, CustomsKeyFK, N4KeyFK, EffectiveDate, IsActive) VALUES
('MSCU', '20GP', 'GP20', '2023-01-01', TRUE),
('MSCU', '40HC', 'HC40', '2023-01-01', TRUE),
('ONEY', '20GP', 'DV20', '2023-05-15', TRUE),
('ONEY', '40HC', 'HQ40', '2023-05-15', FALSE),
('MAEU', '20RF', 'RF20', '2024-02-20', TRUE)");
} catch (PDOException $e) {
// In a real app, you'd log this error.
// For this setup, we can die to see the error clearly.
die("DB Setup Failed: " . $e->getMessage());
}
}
// Run the setup
setup_database();