- New stock_adjustments table logs every stock change (restock, order create/update/delete) with reason and reference - Orders now track payment_method and amount_paid with validation - New /api/reports endpoint with 5 aggregation queries and date filtering - Reports page with date range presets and sales, customer, revenue, status, and inventory sections - Payment fields added to OrderNew and OrderDetail pages with balance due - Girl Scouts trefoil logo added to header - Vite dev server exposed on network for mobile access Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
76 lines
2.2 KiB
JavaScript
76 lines
2.2 KiB
JavaScript
const Database = require('better-sqlite3');
|
|
const path = require('path');
|
|
|
|
const dbPath = process.env.DATABASE_PATH || path.join(__dirname, '..', 'data', 'cookies.db');
|
|
|
|
let db;
|
|
|
|
function getDb() {
|
|
if (!db) {
|
|
const dir = path.dirname(dbPath);
|
|
const fs = require('fs');
|
|
if (!fs.existsSync(dir)) {
|
|
fs.mkdirSync(dir, { recursive: true });
|
|
}
|
|
db = new Database(dbPath);
|
|
db.pragma('foreign_keys = ON');
|
|
initSchema(db);
|
|
}
|
|
return db;
|
|
}
|
|
|
|
function initSchema(database) {
|
|
database.exec(`
|
|
CREATE TABLE IF NOT EXISTS products (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL,
|
|
price REAL NOT NULL DEFAULT 0,
|
|
quantity_on_hand INTEGER NOT NULL DEFAULT 0,
|
|
low_stock_threshold INTEGER DEFAULT 0,
|
|
created_at TEXT DEFAULT (datetime('now'))
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS customers (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL,
|
|
phone TEXT,
|
|
email TEXT,
|
|
address TEXT,
|
|
notes TEXT,
|
|
created_at TEXT DEFAULT (datetime('now'))
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS orders (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
customer_id INTEGER REFERENCES customers(id),
|
|
status TEXT NOT NULL DEFAULT 'pending',
|
|
notes TEXT,
|
|
created_at TEXT DEFAULT (datetime('now')),
|
|
updated_at TEXT DEFAULT (datetime('now'))
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS order_items (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
order_id INTEGER NOT NULL REFERENCES orders(id) ON DELETE CASCADE,
|
|
product_id INTEGER NOT NULL REFERENCES products(id),
|
|
quantity INTEGER NOT NULL,
|
|
price_at_sale REAL NOT NULL,
|
|
UNIQUE(order_id, product_id)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS stock_adjustments (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
product_id INTEGER NOT NULL REFERENCES products(id),
|
|
adjustment INTEGER NOT NULL,
|
|
reason TEXT NOT NULL,
|
|
reference_id INTEGER,
|
|
created_at TEXT DEFAULT (datetime('now'))
|
|
);
|
|
`);
|
|
|
|
try { database.exec("ALTER TABLE orders ADD COLUMN payment_method TEXT"); } catch (e) {}
|
|
try { database.exec("ALTER TABLE orders ADD COLUMN amount_paid REAL NOT NULL DEFAULT 0"); } catch (e) {}
|
|
}
|
|
|
|
module.exports = { getDb, initSchema };
|