Add multi-line sale builder with pending-draft safeguard

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
David Beccue
2026-05-16 12:57:49 +05:00
parent 9d756e2940
commit 00ee9fb1fe
10 changed files with 809 additions and 0 deletions

View File

@ -54,3 +54,28 @@ CREATE INDEX IF NOT EXISTS idx_parts_barcode ON parts(barcode);
CREATE INDEX IF NOT EXISTS idx_parts_category ON parts(category_id);
CREATE INDEX IF NOT EXISTS idx_movements_part ON stock_movements(part_id);
CREATE INDEX IF NOT EXISTS idx_movements_created ON stock_movements(created_at);
CREATE TABLE IF NOT EXISTS invoices (
id INTEGER PRIMARY KEY AUTOINCREMENT,
status TEXT NOT NULL CHECK(status IN ('pending','saved')) DEFAULT 'pending',
total_dirams INTEGER NOT NULL DEFAULT 0,
notes TEXT,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
saved_at TEXT
);
CREATE TABLE IF NOT EXISTS invoice_lines (
id INTEGER PRIMARY KEY AUTOINCREMENT,
invoice_id INTEGER NOT NULL REFERENCES invoices(id) ON DELETE CASCADE,
part_id INTEGER REFERENCES parts(id) ON DELETE SET NULL,
label TEXT,
quantity INTEGER NOT NULL CHECK(quantity > 0),
unit_price_dirams INTEGER NOT NULL DEFAULT 0,
affects_inventory INTEGER NOT NULL DEFAULT 1,
sort_order INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_invoice_lines_invoice ON invoice_lines(invoice_id);
CREATE UNIQUE INDEX IF NOT EXISTS idx_invoices_one_pending
ON invoices(status) WHERE status = 'pending';