Files
SupplierSystem/migrations/20261018_quote_sheet_change_requests.sql
Ubuntu 3e4bc35b26 feat(报价单): 双向价格传递/改价审核/SKU 映射/成本同步推送(供应商侧)
- 报价单生效自动推销售价 → 配送成本价(台账+重试+rearm,映射优先)
- 编辑生效单改价走内部审核(quote_sheet_change_requests)
- 双向价格传递:A 提交配送商审核 / B 拉回并反查映射后写价、D3 回执
- SKU 映射:D4 字典推送、D5 完整拉取(截断不删)、只读本地副本
- 迁移登记 tenant_business_tables(三张租户表数组互相补齐,避免顺序敏感)
2026-10-18 20:10:58 +08:00

242 lines
8.2 KiB
PL/PgSQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 编辑生效报价单改价 → 待审变更单(审核通过才应用 + 才同步成本价)(2026-10-18)
--
-- 背景(用户原话「编辑生效单改价需要有同步审核,审核通过就同步」+ 冻结决策 D1–D5):
-- · 生效中的报价单(`status=1 AND approval_status=30`)改价,过去是**直接落库**(DELETE+INSERT 明细 + 刷版本);
-- · 现在价格相关改动先落本表(`pending`),审核通过才在**单事务**内应用并触发成本价同步;
-- · 一张单只保留一条 `pending`(D4:新提交把旧的置 `superseded`);驳回/通过都留痕(reviewer / 时间 / 理由)。
--
-- 新增业务表四步(见 docs/design/20260915-tenant-table-list-guard.md):
-- ① 建表(tenant_template + 各租户 schema,本文件 DO 块)
-- ② 同步名单 public.tenant_business_tables()(本文件尾部)
-- ③ ensure_tenant_schemas_match_template() 给已有分公司补建
-- ④ assert_tenant_template_matches_business_tables() 断言(漏登记即中断迁移)
-- 设计:docs/design/20261018-报价单改价审核与成本同步.md
DO $$
DECLARE
sch text;
BEGIN
FOR sch IN
SELECT nspname
FROM pg_namespace
WHERE nspname IN ('public', 'legacy_public', 'tenant_template')
OR nspname LIKE 'tenant_%'
LOOP
-- 只在「真实租户业务 schema」建表(锚点 = customer_quote_sheets,与报价单同域)
IF to_regclass(format('%I.customer_quote_sheets', sch)) IS NULL THEN
CONTINUE;
END IF;
EXECUTE format(
'CREATE TABLE IF NOT EXISTS %I.quote_sheet_change_requests (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
branch_code VARCHAR(50) NOT NULL,
quote_sheet_code VARCHAR(64) NOT NULL,
payload JSONB NOT NULL DEFAULT ''{}''::jsonb,
base_version_no INTEGER,
base_items_fingerprint VARCHAR(64),
status VARCHAR(16) NOT NULL DEFAULT ''pending'',
requested_by UUID,
requested_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
reviewed_by UUID,
reviewed_at TIMESTAMPTZ,
reject_reason TEXT,
applied_at TIMESTAMPTZ,
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT quote_sheet_change_requests_status_chk
CHECK (status IN (''pending'', ''approved'', ''rejected'', ''superseded''))
)',
sch
);
EXECUTE format(
'COMMENT ON TABLE %I.quote_sheet_change_requests IS %L',
sch,
'编辑生效报价单的待审变更单:价格相关改动先入此表,审核通过才应用并同步成本价'
);
EXECUTE format(
'COMMENT ON COLUMN %I.quote_sheet_change_requests.payload IS %L',
sch,
'整单一次审核(D2):保存时提交的完整编辑入参(items/customer_codes/title/quote_cycle/effective_at/auto_sale_price_by_cost)'
);
EXECUTE format(
'COMMENT ON COLUMN %I.quote_sheet_change_requests.base_version_no IS %L',
sch,
'提交时的报价单 version_no(留档:审批时可判断期间是否被其它途径改过)'
);
EXECUTE format(
'COMMENT ON COLUMN %I.quote_sheet_change_requests.base_items_fingerprint IS %L',
sch,
'提交时报价单明细指纹(复用 quoteCostSyncItemsFingerprint 算法:sku_code/unit/sale_price 排序后 sha256);审批时重算不一致 ⇒ 409 base_changed 并整单拒绝'
);
EXECUTE format(
'COMMENT ON COLUMN %I.quote_sheet_change_requests.status IS %L',
sch,
'pending=待审;approved=已通过并应用;rejected=已驳回(报价单不变);superseded=被新一次提交覆盖(D4)'
);
-- 一张单只保留一条待审(D4):新提交把旧的置 superseded
EXECUTE format(
'CREATE UNIQUE INDEX IF NOT EXISTS uq_quote_sheet_change_requests_pending
ON %I.quote_sheet_change_requests (branch_code, quote_sheet_code)
WHERE status = ''pending''',
sch
);
-- 待审列表:按分公司 + 状态取最近
EXECUTE format(
'CREATE INDEX IF NOT EXISTS idx_quote_sheet_change_requests_status
ON %I.quote_sheet_change_requests (branch_code, status, requested_at DESC)',
sch
);
-- 单张单的历史(通过/驳回/覆盖留痕)
EXECUTE format(
'CREATE INDEX IF NOT EXISTS idx_quote_sheet_change_requests_sheet
ON %I.quote_sheet_change_requests (quote_sheet_code, requested_at DESC)',
sch
);
IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'canteen_app_rls') THEN
BEGIN
EXECUTE format(
'GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE %I.quote_sheet_change_requests TO canteen_app_rls',
sch
);
EXCEPTION WHEN OTHERS THEN
RAISE NOTICE 'skip grant on %.quote_sheet_change_requests: %', sch, SQLERRM;
END;
END IF;
END LOOP;
END $$;
-- ② 名单同步(116 → 117)
CREATE OR REPLACE FUNCTION public.tenant_business_tables()
RETURNS TEXT[]
LANGUAGE sql
IMMUTABLE
AS $$
SELECT ARRAY[
'main_system_sku_mappings',
'after_sale_returns',
'after_sales',
'ai_prompt_overrides',
'ai_prompt_revisions',
'ai_prompt_templates',
'alias_recognition_logs',
'allocation_tasks',
'assistant_conversations',
'assistant_messages',
'assistant_turns',
'bid_awards',
'bid_invitations',
'bid_prices',
'bid_quotes',
'bid_round_items',
'bid_rounds',
'categories',
'cost_adjustments',
'cost_details',
'customer_alias_stats',
'customer_delivery_assignments',
'customer_portal_users',
'customer_processing_aliases',
'customer_quote_sheet_customers',
'customer_quote_sheet_items',
'customer_quote_sheets',
'customer_quote_versions',
'customer_recipients',
'customer_sku_aliases',
'customer_sku_remarks',
'customer_skus',
'customer_statements',
'customers',
'delivery_location_history',
'delivery_person_locations',
'delivery_person_users',
'delivery_persons',
'delivery_schedules',
'delivery_signatures',
'delivery_tasks',
'exception_tickets',
'fixed_costs',
'free_cost_invoice_links',
'free_cost_invoices',
'free_cost_payment_vouchers',
'import_mapping_memories',
'inspection_loss_confirmations',
'inspection_photo_archive_entries',
'inspection_photo_archives',
'inspection_returns',
'inspection_review_logs',
'inspections',
'inventory',
'inventory_batch_report_matches',
'inventory_batch_reports',
'inventory_logs',
'inventory_reservations',
'invoice_statement_links',
'invoices',
'market_compare_exclusions',
'market_external_catalog',
'market_price_history',
'market_price_indices',
'market_prices',
'market_sku_external_bindings',
'notifications',
'operation_logs',
'order_allocation_batches',
'order_item_fulfillments',
'order_item_returns',
'order_items',
'orders',
'parse_session_items',
'parse_sessions',
'picking_assign_rules',
'picking_containers',
'picking_items',
'picking_scan_logs',
'picking_tasks',
'price_invert_events',
'print_jobs',
'processing_demand_items',
'processing_demand_picks',
'processing_logs',
'processing_tasks',
'product_catalog',
'product_substitutions',
'purchase_orders',
'purchaser_users',
'quality_report_prep_links',
'quality_reports',
'quote_price_proposals',
'quote_sheet_change_requests',
'seasonal_recommendations',
'self_purchase_stock_inbounds',
'sign_records',
'sku_direct_delivery_customers',
'sku_suppliers',
'skus',
'smart_classification_rules',
'statement_disputes',
'statement_lines',
'statement_payments',
'supplier_categories',
'supplier_prep_amend_requests',
'supplier_preparations',
'supplier_price_agreements',
'supplier_rating_snapshots',
'supplier_statements',
'supplier_users',
'suppliers',
'system_configs',
'unit_conversions',
'user_preferences',
'voice_recognition_logs',
'warehouse_locations',
'warehouses',
'weigh_devices'
]
$$;
-- ③④ 已有分公司补建 + 漏登记断言(漏了会中断迁移)
SELECT public.ensure_tenant_schemas_match_template();
SELECT public.assert_tenant_template_matches_business_tables();