diff --git a/api/database.php b/api/database.php index 9bb0819..662d136 100644 --- a/api/database.php +++ b/api/database.php @@ -53,7 +53,7 @@ function initializeDB(PDO $db): void { CREATE TABLE IF NOT EXISTS transactions ( id INTEGER PRIMARY KEY AUTOINCREMENT, product_id INTEGER NOT NULL, - type TEXT NOT NULL CHECK(type IN ('in', 'out')), + type TEXT NOT NULL CHECK(type IN ('in', 'out', 'waste')), quantity REAL NOT NULL, location TEXT NOT NULL DEFAULT 'dispensa', notes TEXT DEFAULT '', @@ -77,6 +77,28 @@ function migrateDB(PDO $db): void { $db->exec("ALTER TABLE products ADD COLUMN package_unit TEXT DEFAULT ''"); } + // Migrate transactions CHECK constraint to allow 'waste' type + $sql = $db->query("SELECT sql FROM sqlite_master WHERE type='table' AND name='transactions'")->fetchColumn(); + if ($sql && strpos($sql, "'waste'") === false) { + $db->exec("ALTER TABLE transactions RENAME TO transactions_old"); + $db->exec(" + CREATE TABLE transactions ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + product_id INTEGER NOT NULL, + type TEXT NOT NULL CHECK(type IN ('in', 'out', 'waste')), + quantity REAL NOT NULL, + location TEXT NOT NULL DEFAULT 'dispensa', + notes TEXT DEFAULT '', + created_at DATETIME DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE + ) + "); + $db->exec("INSERT INTO transactions SELECT * FROM transactions_old"); + $db->exec("DROP TABLE transactions_old"); + $db->exec("CREATE INDEX IF NOT EXISTS idx_transactions_product ON transactions(product_id)"); + $db->exec("CREATE INDEX IF NOT EXISTS idx_transactions_date ON transactions(created_at)"); + } + // --- New shared tables --- // app_settings: key-value store shared across all devices $tables = $db->query("SELECT name FROM sqlite_master WHERE type='table' AND name='app_settings'")->fetchAll(); diff --git a/data/dispensa.db b/data/dispensa.db index c21fdc0..e5d06e0 100644 Binary files a/data/dispensa.db and b/data/dispensa.db differ