Initial commit: Dispensa - home pantry management app
Features: - Barcode scanning (QuaggaJS) + Open Food Facts API lookup - Inventory management with locations (Frigo, Freezer, Dispensa) - Product database with Italian product suggestions - Expiry date estimation by category - AI expiry date reading via Gemini Vision API - Flexible unit of measure (pz, conf, g, kg, ml, L) - Nutriscore/NOVA/Ecoscore/allergens display - Mobile-first PWA with offline support - SQLite backend with PHP REST API
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
/**
|
||||
* Database initialization and connection for Dispensa Manager
|
||||
*/
|
||||
|
||||
define('DB_PATH', __DIR__ . '/../data/dispensa.db');
|
||||
|
||||
function getDB(): PDO {
|
||||
$isNew = !file_exists(DB_PATH);
|
||||
$db = new PDO('sqlite:' . DB_PATH);
|
||||
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
|
||||
$db->exec("PRAGMA journal_mode=WAL");
|
||||
$db->exec("PRAGMA foreign_keys=ON");
|
||||
|
||||
if ($isNew) {
|
||||
initializeDB($db);
|
||||
}
|
||||
return $db;
|
||||
}
|
||||
|
||||
function initializeDB(PDO $db): void {
|
||||
$db->exec("
|
||||
CREATE TABLE IF NOT EXISTS products (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
barcode TEXT UNIQUE,
|
||||
name TEXT NOT NULL,
|
||||
brand TEXT DEFAULT '',
|
||||
category TEXT DEFAULT '',
|
||||
image_url TEXT DEFAULT '',
|
||||
unit TEXT DEFAULT 'pz',
|
||||
default_quantity REAL DEFAULT 1,
|
||||
notes TEXT DEFAULT '',
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS inventory (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
product_id INTEGER NOT NULL,
|
||||
location TEXT NOT NULL DEFAULT 'dispensa',
|
||||
quantity REAL NOT NULL DEFAULT 1,
|
||||
expiry_date DATE,
|
||||
added_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
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')),
|
||||
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
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_products_barcode ON products(barcode);
|
||||
CREATE INDEX IF NOT EXISTS idx_inventory_product ON inventory(product_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_inventory_location ON inventory(location);
|
||||
CREATE INDEX IF NOT EXISTS idx_transactions_product ON transactions(product_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_transactions_date ON transactions(created_at);
|
||||
");
|
||||
}
|
||||
Reference in New Issue
Block a user