-- 报价单重构:一张报价单含多商品,可绑定多客户;到期前提醒、未更新则顺延 CREATE TABLE IF NOT EXISTS customer_quote_sheets ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), quote_sheet_code VARCHAR(50) UNIQUE NOT NULL, title VARCHAR(200), version_no INTEGER NOT NULL DEFAULT 1, quote_cycle VARCHAR(20), effective_at TIMESTAMPTZ, expires_at TIMESTAMPTZ, status INTEGER NOT NULL DEFAULT 0, approval_status INTEGER NOT NULL DEFAULT 10, approval_remark TEXT, approved_by UUID, approved_at TIMESTAMPTZ, reminder_sent_at TIMESTAMPTZ, rollover_count INTEGER NOT NULL DEFAULT 0, parent_sheet_code VARCHAR(50), branch_code VARCHAR(50) REFERENCES branches(branch_code), created_by UUID, created_at TIMESTAMPTZ DEFAULT NOW(), updated_at TIMESTAMPTZ DEFAULT NOW() ); CREATE TABLE IF NOT EXISTS customer_quote_sheet_items ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), quote_sheet_code VARCHAR(50) NOT NULL REFERENCES customer_quote_sheets(quote_sheet_code) ON DELETE CASCADE, sku_code VARCHAR(50) NOT NULL REFERENCES skus(sku_code), sku_name VARCHAR(200), unit VARCHAR(20), price DECIMAL(10,2) NOT NULL, sort_order INTEGER NOT NULL DEFAULT 0, created_at TIMESTAMPTZ DEFAULT NOW() ); CREATE TABLE IF NOT EXISTS customer_quote_sheet_customers ( quote_sheet_code VARCHAR(50) NOT NULL REFERENCES customer_quote_sheets(quote_sheet_code) ON DELETE CASCADE, customer_code VARCHAR(50) NOT NULL REFERENCES customers(customer_code), created_at TIMESTAMPTZ DEFAULT NOW(), PRIMARY KEY (quote_sheet_code, customer_code) ); ALTER TABLE customer_quote_versions ADD COLUMN IF NOT EXISTS quote_sheet_code VARCHAR(50); CREATE INDEX IF NOT EXISTS idx_quote_sheets_active ON customer_quote_sheets(status, approval_status, expires_at DESC); CREATE INDEX IF NOT EXISTS idx_quote_sheets_branch ON customer_quote_sheets(branch_code, version_no DESC); CREATE INDEX IF NOT EXISTS idx_quote_sheet_items_sheet ON customer_quote_sheet_items(quote_sheet_code, sort_order); CREATE INDEX IF NOT EXISTS idx_quote_sheet_customers_code ON customer_quote_sheet_customers(customer_code); CREATE INDEX IF NOT EXISTS idx_quote_versions_sheet ON customer_quote_versions(quote_sheet_code); ALTER TABLE customer_quote_sheets ENABLE ROW LEVEL SECURITY; ALTER TABLE customer_quote_sheet_items ENABLE ROW LEVEL SECURITY; ALTER TABLE customer_quote_sheet_customers ENABLE ROW LEVEL SECURITY; DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_policies WHERE tablename = 'customer_quote_sheets' AND policyname = 'anon_all_quote_sheets') THEN CREATE POLICY anon_all_quote_sheets ON customer_quote_sheets FOR ALL USING (true) WITH CHECK (true); END IF; IF NOT EXISTS (SELECT 1 FROM pg_policies WHERE tablename = 'customer_quote_sheet_items' AND policyname = 'anon_all_quote_sheet_items') THEN CREATE POLICY anon_all_quote_sheet_items ON customer_quote_sheet_items FOR ALL USING (true) WITH CHECK (true); END IF; IF NOT EXISTS (SELECT 1 FROM pg_policies WHERE tablename = 'customer_quote_sheet_customers' AND policyname = 'anon_all_quote_sheet_customers') THEN CREATE POLICY anon_all_quote_sheet_customers ON customer_quote_sheet_customers FOR ALL USING (true) WITH CHECK (true); END IF; END $$;