diff --git a/migrations/20261021_main_supplier_bindings_branch_name.sql b/migrations/20261021_main_supplier_bindings_branch_name.sql new file mode 100644 index 0000000..4ba3595 --- /dev/null +++ b/migrations/20261021_main_supplier_bindings_branch_name.sql @@ -0,0 +1,9 @@ +-- 绑定表补「主系统分公司名称」(2026-10-18 用户要求:建客户用真名,而不是「主系统分公司 BR002」) +-- +-- 来源:主系统绑定推送/回读里的 `main_branch_name`(缺省 null ⇒ 建客户时退回兜底名)。 +-- 可重放:ADD COLUMN IF NOT EXISTS。 +ALTER TABLE public.main_supplier_bindings + ADD COLUMN IF NOT EXISTS main_branch_name VARCHAR(100); + +COMMENT ON COLUMN public.main_supplier_bindings.main_branch_name IS + '主系统分公司名称(如「自贡」);建 MAIN_<分公司码> 客户时用作客户名'; diff --git a/server/src/services/branchAccessCode.ts b/server/src/services/branchAccessCode.ts index 36d213e..c4ead0f 100644 --- a/server/src/services/branchAccessCode.ts +++ b/server/src/services/branchAccessCode.ts @@ -288,6 +288,8 @@ export type UpsertBindingInput = { accessCode: string; mainBranchCode: string; mainSupplierCode: string; + /** 主系统分公司名称(如「自贡」):绑定即建客户时用作客户名 */ + mainBranchName?: string | null; source?: 'main_push' | 'main_pull' | 'manual' | 'backfill'; note?: string | null; }; @@ -313,6 +315,7 @@ export async function upsertBinding(input: UpsertBindingInput): Promise { supplier_code: 'sup1', action: 'bind', }) - ).toEqual({ accessCode: 'sc7k3q9m2xtp', mainBranchCode: 'br002', mainSupplierCode: 'sup1', action: 'bind' }); + ).toEqual({ accessCode: 'sc7k3q9m2xtp', mainBranchCode: 'br002', mainSupplierCode: 'sup1', mainBranchName: null, action: 'bind' }); expect( parseBindingChange({ access_code: 'SC1', main_branch_code: 'BR2', main_supplier_code: 'SUP3' }) - ).toEqual({ accessCode: 'SC1', mainBranchCode: 'BR2', mainSupplierCode: 'SUP3', action: 'bind' }); + ).toEqual({ accessCode: 'SC1', mainBranchCode: 'BR2', mainSupplierCode: 'SUP3', mainBranchName: null, action: 'bind' }); }); it('缺编码或缺供应商标记为不可解析(整条丢掉,不猜)', () => { diff --git a/server/tests/mainSystemBranchCustomer.test.ts b/server/tests/mainSystemBranchCustomer.test.ts index a6abdcb..94fc60c 100644 --- a/server/tests/mainSystemBranchCustomer.test.ts +++ b/server/tests/mainSystemBranchCustomer.test.ts @@ -82,4 +82,21 @@ describe('存量归属改挂(决策 ③)', () => { expect(fn).toContain('SELECT count(*)::text AS count FROM orders WHERE customer_code = $1'); expect(script).toContain('reattributeMainSystemOrdersToBranchCustomers'); }); + + it('已有客户时不再新建:订单导入按 customer_code 命中即更新,INSERT 走 ON CONFLICT DO NOTHING', () => { + const start = importSrc.indexOf('async function ensureImportCustomer'); + const fn = importSrc.slice(start, start + 3200); + expect(fn).toContain('SELECT status, source, company_name FROM customers WHERE customer_code = $1'); + expect(fn).toContain('INSERT INTO customers (customer_code, company_name, branch_code, status, source)'); + expect(fn).toContain('ON CONFLICT (customer_code) DO NOTHING'); + // 先查后插:已有客户只更新、不新建 + expect(fn.indexOf('SELECT status, source, company_name')).toBeLessThan(fn.indexOf('INSERT INTO customers')); + }); + + it('绑定即建客户:重新绑定时把停用(40)恢复为启用(10)', () => { + const start = importSrc.indexOf('export async function ensureMainBranchCustomerForBinding'); + const fn = importSrc.slice(start, start + 1200); + expect(fn).toContain('UPDATE customers SET status = 10 WHERE customer_code = $1 AND status = 40'); + expect(fn).toContain('ensureImportCustomer(params.branchCode'); + }); });