'use strict'; const { historicalAction } = require('./fixtures/legacy-cancellation.cjs'); const { test } = require('node:test'), assert = require('node:assert/strict'), { randomUUID } = require('node:crypto'); const { Ledger } = require('../core/ledger'), Calc = require('../public/calculations'), { receipt } = require('../domains/printing/printing'); const date = '2026-10-17', actor = { id: 'cost-owner', name: '测试麦麦', role: 'owner' }; function input() { return { date, customer: '合成客户', salesperson: '合成开单人', payment_account: '助理微信', lines: [ { name: '足金999', unit: 'g', quantity: '2.2', pocket: '', payer: 'owner', sale: '2300', costParts: { material: '1980', labor: '0', accessories: '5', other: '0' }, costLabor: { mode: 'gram', rate: '20' }, charges: [{ name: '证书', amount: '10' }], }, { name: '纯银925', unit: '件', quantity: '1', pocket: '', payer: 'assistant', sale: '200', costParts: { material: '80', labor: '10', accessories: '0', other: '0' }, }, ], fee_items: [ { key: 'shipping', note: '快递费', amount: '6', payer: 'assistant', status: 'estimated' }, { key: 'certificate', note: '证书', amount: '4', payer: 'owner', status: 'estimated' }, { key: 'box', note: '包装', amount: '3', payer: 'owner', status: 'estimated' }, ], }; } function fixture(t, p = input(), legacy = false) { const ledger = new Ledger(':memory:'); t.after(() => ledger.close()); const run = (p, fn) => ledger.transact(randomUUID(), actor, p, fn); const id = run(p, () => (legacy ? ledger.saveOrder(p, actor) : ledger.issueOrder(p, actor))).id; const action = (kind, p = {}) => run({ kind, p }, () => historicalAction(ledger, id, kind, { date, ...p }, actor)); if (legacy) action('confirm'); const detail = () => ledger.detail(id), costs = () => Calc.orderCosts(detail()); return { ledger, id, action, detail, costs }; } test('开单后按两名付款人汇总货品与实际费用,克工费只乘一次且不把客户收费当成本', t => { const { ledger, id, detail, costs } = fixture(t), before = detail(), c = costs(); assert.equal(before.total, 251000); assert.equal(c.byPayer.owner.goods, 202900); assert.equal(c.byPayer.assistant.goods, 9000); assert.equal(c.byPayer.owner.paidFees, 700); assert.equal(c.byPayer.assistant.paidFees, 600); assert.equal(c.byPayer.owner.total, 203600); assert.equal(c.byPayer.assistant.total, 9600); assert.equal(c.total.total, 213200); assert.equal(c.total.paidFees, 1300); assert.equal(c.total.pendingFees, 0); assert.equal(c.total.missing, 0); assert.equal(before.profit, before.total - c.total.total); assert.equal(c.rows.filter(r => r.name === '进货工费')[0].amount, 4400); assert.deepEqual(detail(), before, '查看成本不应修改任何账务'); const customer = ledger.publicOrder(id), html = receipt(customer); for (const key of ['payer', 'costParts', 'expenses', 'fee_items', 'orderCosts']) assert.ok(!JSON.stringify(customer).includes('"' + key + '"')); for (const text of ['成本明细与付款人', '1980.00', '2029.00', '2132.00']) assert.ok(!html.includes(text)); }); test('开单同时完成费用与交付后只计一次,付款人与利润对得上', t => { const { detail, costs, action } = fixture(t); let c = costs(); assert.equal(c.pendingCount, 0); assert.equal(c.total.pendingFees, 0); assert.equal(c.total.paidFees, 1300); assert.equal(c.total.total, 213200); assert.equal(c.rows.filter(r => r.name === '证书').length, 1); assert.equal(c.rows.filter(r => r.kind === 'paid').length, 3); assert.throws(() => action('fees', { revision: detail().revision, verified: true, fee_items: [] }), /没有待核实/); c = costs(); assert.equal(detail().profit, 37800); assert.equal(detail().total - c.total.total, detail().profit); assert.equal(c.byPayer.owner.total, detail().ownerCost); }); test('更正费用金额或付款人后按最新有效支出汇总,连续更正和撤销不重复累计', t => { const { detail, costs, action } = fixture(t); let target = detail().expenses.find(f => f.note === '快递费').id; action('correct', { type: 'expense', target, amount: '8', payer: 'owner', reason: '快递实际由麦麦支付8元', verified: true, }); let c = costs(); assert.equal(c.byPayer.owner.paidFees, 1500); assert.equal(c.byPayer.assistant.paidFees, 0); assert.equal(c.total.total, 213400); assert.equal(c.rows.filter(r => r.kind === 'paid').length, 3); assert.equal(detail().total - c.total.total, detail().profit); target = detail().expenses.find(f => f.amount === 800).id; action('correct', { type: 'expense', target, amount: '5', payer: 'assistant', reason: '最终核实快递助理支付5元', verified: true, }); c = costs(); assert.equal(c.byPayer.owner.paidFees, 700); assert.equal(c.byPayer.assistant.paidFees, 500); assert.equal(c.total.total, 213100); target = detail().expenses.find(f => f.amount === 500).id; action('correct', { type: 'expense', target, amount: '0', payer: 'assistant', reason: '撤销误记费用', verified: true, }); c = costs(); assert.equal(c.total.paidFees, 700); assert.equal(c.rows.filter(r => r.kind === 'paid').length, 2); assert.equal(detail().total - c.total.total, detail().profit); }); test('旧单未知成本仍显示待补,新单0元费用直接核实且不会伪造付款', t => { const p = input(); p.lines[0].costParts.material = ''; p.fee_items[0].amount = ''; p.fee_items[2].amount = '0'; const { detail, costs } = fixture(t, p, true), c = costs(); assert.equal(c.byPayer.owner.goodsMissing, 1); assert.equal(c.byPayer.assistant.pendingMissing, 1); assert.equal(c.total.missing, 2); assert.equal(c.total.total, 14300); assert.equal(detail().estimatedProfit, null); assert.equal(c.rows.find(r => r.name === '快递费').amount, null); assert.equal(c.rows.find(r => r.name === '包装').amount, 0); const zero = input(); zero.fee_items[2].amount = '0'; const f = fixture(t, zero); assert.equal(f.costs().rows.find(r => r.name === '包装').kind, 'zero'); assert.equal(f.detail().expenses.length, 2); assert.equal(f.costs().total.paidFees, 1000); }); test('退货与取消不再计算原货品垫资,实际费用保留,未核实预估不冒充支出', t => { const f = fixture(t); f.action('return', { verified: true, reason: '测试退货' }); let c = f.costs(); assert.equal(c.goodsExcluded, true); assert.equal(c.total.goods, 0); assert.equal(c.total.total, 1300); assert.equal(f.detail().profit, -c.total.total); assert.ok(c.rows.filter(r => r.kind === 'goods').every(r => r.excluded)); const g = fixture(t, input(), true); g.action('expense', { amount: '2', payer: 'owner', note: '已支付的打样费用' }); g.action('cancel', { reason: '取消订单' }); c = g.costs(); assert.equal(c.pendingCount, 0); assert.equal(c.total.total, 200); assert.equal(c.byPayer.assistant.total, 0); assert.equal(g.detail().profit, -200); }); test('旧单只显示已存总成本,分币金额精确累加并保留附加项目来源', t => { const p = input(), chargeId = randomUUID(); p.lines = [ { name: '旧款', unit: '件', quantity: '1', pocket: '', payer: 'owner', cost: '12.34', sale: '50', charges: [{ id: chargeId, name: '特殊包装', amount: '10' }], }, ]; p.fee_items = [ { key: 'charge:' + chargeId, note: '特殊包装', amount: '0.01', payer: 'assistant', status: 'estimated' }, ]; const { costs, detail } = fixture(t, p), c = costs(); assert.equal(c.total.total, 1235); assert.equal(c.rows.length, 2); assert.equal(c.rows[0].name, '货品成本(原单未分项)'); assert.equal(c.rows[1].source, date); assert.equal(detail().fee_items[0].key, 'charge:' + chargeId); assert.equal(detail().lines[0].charges[0].id, chargeId); });