- 报价单生效自动推销售价 → 配送成本价(台账+重试+rearm,映射优先) - 编辑生效单改价走内部审核(quote_sheet_change_requests) - 双向价格传递:A 提交配送商审核 / B 拉回并反查映射后写价、D3 回执 - SKU 映射:D4 字典推送、D5 完整拉取(截断不删)、只读本地副本 - 迁移登记 tenant_business_tables(三张租户表数组互相补齐,避免顺序敏感)
267 lines
9.3 KiB
PL/PgSQL
267 lines
9.3 KiB
PL/PgSQL
-- 报价单双向价格传递:本地提议台账(2026-10-18 增量)
|
||
--
|
||
-- 背景(用户原话 + 冻结契约):报价单的价可以在两个方向发起协商 ——
|
||
-- · A 我方(供应商)→ 配送商:把整张单的销售价交对方审(出站 D1,对方写自己的 cost_price);
|
||
-- · B 配送商 → 我方:对方把成本价提议推过来(我方经 D2 拉回),我方审(通过就写自己的 sale_price,
|
||
-- 再由**既有成本价同步通道** rearm 推回对方 cost_price)。
|
||
--
|
||
-- 这张表只记**我方视角**的提议台账(不含价本身——价仍在 `customer_quote_sheet_items`):
|
||
-- · partial unique `(branch_code, quote_sheet_code, direction) WHERE status='pending'`
|
||
-- ⇒ 一张单一个方向只允许一条在途(并发/重复提交由索引兜底);
|
||
-- · `items` = 提交时的整单快照(sku_code/unit/price),`payload_hash` 供幂等与排障;
|
||
-- · `client_request_id` 用于 D1 幂等(同号同 hash 对方回 replay);`remote_proposal_id` 是对方的提议 id,
|
||
-- 方向 A 的结果同步、方向 B 的 D3 回执都靠它对应。
|
||
--
|
||
-- 新增业务表四步(见 docs/design/20260915-tenant-table-list-guard.md):
|
||
-- ① 建表(tenant_template + 各租户 schema,本文件 DO 块)
|
||
-- ② 同步名单 public.tenant_business_tables()(117 → 118)
|
||
-- ③ 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_price_proposals (
|
||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||
branch_code VARCHAR(50) NOT NULL,
|
||
quote_sheet_code VARCHAR(64) NOT NULL,
|
||
main_branch_code VARCHAR(64) NOT NULL,
|
||
direction VARCHAR(16) NOT NULL,
|
||
items JSONB NOT NULL DEFAULT ''[]''::jsonb,
|
||
payload_hash VARCHAR(64),
|
||
status VARCHAR(16) NOT NULL DEFAULT ''pending'',
|
||
reject_reason TEXT,
|
||
client_request_id UUID,
|
||
remote_proposal_id TEXT,
|
||
requested_by UUID,
|
||
requested_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||
reviewed_by UUID,
|
||
reviewed_at TIMESTAMPTZ,
|
||
applied_at TIMESTAMPTZ,
|
||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||
CONSTRAINT quote_price_proposals_direction_chk
|
||
CHECK (direction IN (''to_delivery'', ''from_delivery'')),
|
||
CONSTRAINT quote_price_proposals_status_chk
|
||
CHECK (status IN (''pending'', ''approved'', ''rejected'', ''cancelled'', ''superseded''))
|
||
)',
|
||
sch
|
||
);
|
||
EXECUTE format(
|
||
'COMMENT ON TABLE %I.quote_price_proposals IS %L',
|
||
sch,
|
||
'报价单双向价格传递台账(A 我方→配送商 / B 配送商→我方);只记提议与状态,价仍在 customer_quote_sheet_items'
|
||
);
|
||
EXECUTE format(
|
||
'COMMENT ON COLUMN %I.quote_price_proposals.direction IS %L',
|
||
sch,
|
||
'to_delivery=A 我方提交给配送商审;from_delivery=B 配送商提议、由我方审'
|
||
);
|
||
EXECUTE format(
|
||
'COMMENT ON COLUMN %I.quote_price_proposals.items IS %L',
|
||
sch,
|
||
'整单快照 [{sku_code, unit, price}](A=我方 sale_price;B=对方 cost_price=同一个数)'
|
||
);
|
||
EXECUTE format(
|
||
'COMMENT ON COLUMN %I.quote_price_proposals.client_request_id IS %L',
|
||
sch,
|
||
'D1 幂等请求号(同号同 hash 对方回 replay);方向 B 的行为空(由对方发起)'
|
||
);
|
||
EXECUTE format(
|
||
'COMMENT ON COLUMN %I.quote_price_proposals.remote_proposal_id IS %L',
|
||
sch,
|
||
'对方(配送系统)的 proposal id:A 的结果同步与 B 的 D3 回执都靠它对应'
|
||
);
|
||
|
||
-- T6:status 枚举含 `superseded`(同方向重复提交时旧 pending 置它)。
|
||
-- ⚠️ 表已存在的库也必须能拿到这个枚举 ⇒ 先 DROP CONSTRAINT IF EXISTS 再重建(迁移可重放)。
|
||
EXECUTE format(
|
||
'ALTER TABLE %I.quote_price_proposals DROP CONSTRAINT IF EXISTS quote_price_proposals_status_chk',
|
||
sch
|
||
);
|
||
EXECUTE format(
|
||
'ALTER TABLE %I.quote_price_proposals
|
||
ADD CONSTRAINT quote_price_proposals_status_chk
|
||
CHECK (status IN (''pending'', ''approved'', ''rejected'', ''cancelled'', ''superseded''))',
|
||
sch
|
||
);
|
||
|
||
-- 一张单一个方向只允许一条在途
|
||
EXECUTE format(
|
||
'CREATE UNIQUE INDEX IF NOT EXISTS uq_quote_price_proposals_pending
|
||
ON %I.quote_price_proposals (branch_code, quote_sheet_code, direction)
|
||
WHERE status = ''pending''',
|
||
sch
|
||
);
|
||
-- 列表:按分公司 + 方向 + 状态取最近
|
||
EXECUTE format(
|
||
'CREATE INDEX IF NOT EXISTS idx_quote_price_proposals_list
|
||
ON %I.quote_price_proposals (branch_code, direction, status, requested_at DESC)',
|
||
sch
|
||
);
|
||
-- 对账:按对方 proposal id 定位本地行
|
||
EXECUTE format(
|
||
'CREATE INDEX IF NOT EXISTS idx_quote_price_proposals_remote
|
||
ON %I.quote_price_proposals (remote_proposal_id)',
|
||
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_price_proposals TO canteen_app_rls',
|
||
sch
|
||
);
|
||
EXCEPTION WHEN OTHERS THEN
|
||
RAISE NOTICE 'skip grant on %.quote_price_proposals: %', sch, SQLERRM;
|
||
END;
|
||
END IF;
|
||
END LOOP;
|
||
END $$;
|
||
|
||
-- ② 名单同步(117 → 118)
|
||
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();
|