File size: 3,814 Bytes
7c353a0
d77ccd1
7c353a0
d77ccd1
 
 
 
 
 
 
 
 
7c353a0
 
d77ccd1
 
7c353a0
d77ccd1
7c353a0
 
 
 
 
 
d77ccd1
 
 
 
 
 
 
 
 
 
 
7c353a0
d77ccd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c353a0
 
d77ccd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
-- =====================================================================
-- Lobels Stores Intelligence Assistant -- Database Schema
-- Stack: Supabase (PostgreSQL)
-- Run in Supabase -> SQL Editor. Safe to re-run.
--
-- Multi-client design: lobels_stores carries a client_id column, and the
-- app filters every query on it (CLIENT_ID = "lobels" in app.py).
-- lobels_materials is a global reference table with no client_id.
--
-- Access: the app and the sheet sync both use the service key, which
-- bypasses RLS. RLS is enabled with no policies, so the anon and
-- authenticated roles cannot read or write these tables.
-- =====================================================================

-- ── Daily transactions (one row per material per day with issues) ───
CREATE TABLE IF NOT EXISTS lobels_stores (
  id                    BIGSERIAL PRIMARY KEY,
  client_id             TEXT NOT NULL DEFAULT 'lobels',
  txn_date              DATE NOT NULL,
  stock_code            TEXT NOT NULL,
  description           TEXT NOT NULL,
  category              TEXT,
  month                 TEXT,
  year                  INTEGER,
  daily_issues          NUMERIC(14,3) DEFAULT 0,
  opening_stock         NUMERIC(14,3) DEFAULT 0,
  receipts              NUMERIC(14,3) DEFAULT 0,
  transfer_from_sweets  NUMERIC(14,3) DEFAULT 0,
  transfer_to_sweets    NUMERIC(14,3) DEFAULT 0,
  syrup                 NUMERIC(14,3) DEFAULT 0,
  canteen_others        NUMERIC(14,3) DEFAULT 0,
  total_issues          NUMERIC(14,3) DEFAULT 0,
  theoretical_closing   NUMERIC(14,3) DEFAULT 0,
  physical_closing      NUMERIC(14,3) DEFAULT 0,
  variance              NUMERIC(14,3) DEFAULT 0,
  variance_flag         TEXT,
  unit_cost_usd         NUMERIC(14,4),
  value_usd             NUMERIC(14,2)
);

-- ── Materials master (reference data; app reads unit_cost_usd, supplier,
--    reorder_point, lead_time_days) ───────────────────────────────────
--    stock_code is NOT unique: the source Materials Master reuses 1602 and
--    1604 for different materials, and the app joins on description.
CREATE TABLE IF NOT EXISTS lobels_materials (
  id              BIGSERIAL PRIMARY KEY,
  stock_code      TEXT NOT NULL,
  description     TEXT NOT NULL UNIQUE,
  category        TEXT,
  unit            TEXT DEFAULT 'kg',
  reorder_point   NUMERIC(14,3),
  lead_time_days  INTEGER,
  unit_cost_usd   NUMERIC(14,4),
  supplier        TEXT,
  notes           TEXT
);

-- ── Indexes for chatbot queries ─────────────────────────────────────
CREATE INDEX IF NOT EXISTS idx_lobels_client   ON lobels_stores (client_id);
CREATE INDEX IF NOT EXISTS idx_lobels_date     ON lobels_stores (txn_date DESC);
CREATE INDEX IF NOT EXISTS idx_lobels_code     ON lobels_stores (stock_code);
CREATE INDEX IF NOT EXISTS idx_lobels_month    ON lobels_stores (client_id, month);
CREATE INDEX IF NOT EXISTS idx_lobels_category ON lobels_stores (category);
CREATE INDEX IF NOT EXISTS idx_lobels_materials_desc ON lobels_materials (description);

-- ── Monthly totals per material (not used by app.py; handy for ad hoc SQL)
CREATE OR REPLACE VIEW lobels_monthly_summary
WITH (security_invoker = true) AS
SELECT
  client_id,
  stock_code,
  description,
  category,
  month,
  year,
  SUM(daily_issues) AS month_issues,
  AVG(variance)     AS avg_variance
FROM lobels_stores
GROUP BY client_id, stock_code, description, category, month, year;

-- ── Lock down: service key only ─────────────────────────────────────
ALTER TABLE lobels_stores    ENABLE ROW LEVEL SECURITY;
ALTER TABLE lobels_materials ENABLE ROW LEVEL SECURITY;