Fix: allow 'waste' transaction type for thrown-out items (CHECK constraint)
This commit is contained in:
+23
-1
@@ -53,7 +53,7 @@ function initializeDB(PDO $db): void {
|
|||||||
CREATE TABLE IF NOT EXISTS transactions (
|
CREATE TABLE IF NOT EXISTS transactions (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
product_id INTEGER NOT NULL,
|
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,
|
quantity REAL NOT NULL,
|
||||||
location TEXT NOT NULL DEFAULT 'dispensa',
|
location TEXT NOT NULL DEFAULT 'dispensa',
|
||||||
notes TEXT DEFAULT '',
|
notes TEXT DEFAULT '',
|
||||||
@@ -77,6 +77,28 @@ function migrateDB(PDO $db): void {
|
|||||||
$db->exec("ALTER TABLE products ADD COLUMN package_unit TEXT DEFAULT ''");
|
$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 ---
|
// --- New shared tables ---
|
||||||
// app_settings: key-value store shared across all devices
|
// 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();
|
$tables = $db->query("SELECT name FROM sqlite_master WHERE type='table' AND name='app_settings'")->fetchAll();
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user