import { hashPin } from '@sims/auth' import { uuidv7 } from '@sims/domain' import type { DB } from './db' /** Idempotent demo seed — real installs get the onboarding wizard instead. */ export function seedIfEmpty(db: DB): void { const has = db.prepare('SELECT COUNT(*) AS n FROM tenant').get() as { n: number } if (has.n > 0) return const now = new Date().toISOString() db.prepare( `INSERT INTO tenant (id, code, name, gstin, gst_scheme, fy_start_month, created_at) VALUES ('t1', 'MEGA', 'MegaMart Retail', '32ABCDE1234F1Z5', 'regular', 4, ?)`, ).run(now) db.prepare( `INSERT INTO store (id, tenant_id, code, name, state_code) VALUES ('s1', 't1', 'ST1', 'MegaMart T.Nagar', '32')`, ).run() db.prepare(`INSERT INTO counter (id, tenant_id, store_id, code, name) VALUES ('c1','t1','s1','C1','Counter 1')`).run() db.prepare(`INSERT INTO counter (id, tenant_id, store_id, code, name) VALUES ('c2','t1','s1','C2','Counter 2')`).run() const user = db.prepare( `INSERT INTO app_user (id, tenant_id, username, display_name, roles, store_ids, language, pin_salt, pin_hash, pw_salt, pw_hash) VALUES (?, 't1', ?, ?, ?, '"all"', ?, ?, ?, ?, ?)`, ) const mk = (id: string, uname: string, name: string, roles: string[], lang: string, pin?: string, pw?: string) => { const p = pin !== undefined ? hashPin(pin) : undefined const w = pw !== undefined ? hashPin(pw) : undefined user.run(id, uname, name, JSON.stringify(roles), lang, p?.salt ?? null, p?.hash ?? null, w?.salt ?? null, w?.hash ?? null) } mk('u1', 'ramesh', 'Ramesh', ['cashier'], 'ml', '4728') mk('u2', 'suresh', 'Suresh', ['supervisor'], 'ml', '8265') mk('u3', 'thomas', 'Thomas', ['owner'], 'en', '9174', 'sims') mk('u4', 'divya', 'Divya', ['cashier'], 'ta', '3591') const tax = db.prepare( `INSERT INTO tax_class (class_code, rate_pct_bp, cess_pct_bp, effective_from) VALUES (?, ?, ?, '2017-07-01')`, ) for (const [c, r, x] of [['ZERO', 0, 0], ['GST5', 500, 0], ['GST12', 1200, 0], ['GST18', 1800, 0], ['DEMERIT', 2800, 1200]] as const) { tax.run(c, r, x) } const item = db.prepare( `INSERT INTO item (id, tenant_id, code, name, hsn, tax_class_code, unit_code, unit_decimals, mrp_paise, sale_price_paise, price_includes_tax, barcodes) VALUES (?, 't1', ?, ?, ?, ?, ?, ?, ?, ?, 1, ?)`, ) const rows: [string, string, string, string, string, number, number | null, number, string[]][] = [ ['101', 'Aashirvaad Atta 5kg', '1101', 'ZERO', 'PCS', 0, 28_500, 27_000, ['8901063014357']], ['102', 'Tata Salt 1kg', '2501', 'ZERO', 'PCS', 0, 3_000, 2_800, ['8904063202016']], ['103', 'Surf Excel 1kg', '3402', 'GST18', 'PCS', 0, 15_000, 14_500, ['8901030704833']], ['104', 'Parle-G 800g', '1905', 'GST5', 'PCS', 0, 9_500, 9_000, ['8901063092730']], ['105', 'Maggi Noodles 12-pack', '1902', 'GST12', 'PCS', 0, 14_400, 14_000, ['8901058851298']], ['106', 'Coca-Cola 1.25L', '2202', 'DEMERIT', 'PCS', 0, 6_500, 6_500, ['8901764012341']], ['107', 'Amul Butter 500g', '0405', 'GST12', 'PCS', 0, 28_000, 27_500, ['8901262010337']], ['108', 'Carry Bag', '3923', 'GST18', 'PCS', 0, null, 500, []], ['42', 'Tomato (loose)', '0702', 'ZERO', 'KG', 3, null, 3_200, []], ['43', 'Onion (loose)', '0703', 'ZERO', 'KG', 3, null, 4_500, []], ['44', 'Potato (loose)', '0701', 'ZERO', 'KG', 3, null, 2_600, []], ] for (const [code, name, hsn, cls, unit, dec, mrp, price, barcodes] of rows) { item.run(uuidv7(), code, name, hsn, cls, unit, dec, mrp, price, JSON.stringify(barcodes)) // opening stock so the Stock page has something honest to derive db.prepare( `INSERT INTO stock_movement (id, tenant_id, store_id, item_id, qty_delta, reason, business_date, created_at_wall) SELECT ?, 't1', 's1', id, ?, 'OPENING', ?, ? FROM item WHERE tenant_id='t1' AND code=?`, ).run(uuidv7(), unit === 'KG' ? 50 : 120, now.slice(0, 10), now, code) } const party = db.prepare( `INSERT INTO party (id, tenant_id, code, name, kind, phone, gstin, state_code, credit_limit_paise) VALUES (?, 't1', ?, ?, ?, ?, ?, ?, ?)`, ) party.run(uuidv7(), 'CU001', 'Lakshmi', 'customer', '9840012345', null, '32', 500_000) party.run(uuidv7(), 'CU002', 'Joseph', 'customer', '9847055210', null, '32', null) party.run(uuidv7(), 'SU001', 'HUL Distributor', 'supplier', '9847100001', '32AABCH1234K1Z6', '32', null) party.run(uuidv7(), 'SU002', 'Amul Agency', 'supplier', '9847100002', '32AAACA5678L1Z2', '32', null) const setting = db.prepare( `INSERT INTO setting (scope_type, scope_id, key, value) VALUES (?, ?, ?, ?)`, ) setting.run('system', '', 'gst.roundToRupee', 'true') setting.run('system', '', 'discount.capBp', '1000') setting.run('tenant', 't1', 'discount.capBp', '1500') db.prepare(`INSERT INTO message_catalog (code, channel, lang, template) VALUES ('BILL_ESHARE','whatsapp','en','Hi {name}, your bill of {amount} from {store}. Thank you!')`).run() }