Files
weiguang-jewelry-ledger/新账系统/test/fee-estimates.test.js
T

271 lines
11 KiB
JavaScript
Raw Normal View History

'use strict';
const { historicalAction } = require('./fixtures/legacy-cancellation.cjs');
const { test } = require('node:test'),
assert = require('node:assert/strict'),
fs = require('node:fs'),
os = require('node:os'),
path = require('node:path'),
{ randomUUID } = require('node:crypto');
const Estimates = require('../public/fee-estimates'),
Calc = require('../public/calculations'),
{ Ledger } = require('../ledger');
const actor = { id: 'fee-test', name: '测试麦麦', role: 'owner' },
date = '2026-10-17';
const charge = (name, amount = '10') => ({ id: randomUUID(), name, amount });
const product = charges => ({
name: '足金999',
unit: 'g',
quantity: '2',
pocket: '',
payer: 'owner',
cost: '1000',
pricingMode: 'manual',
baseSale: '1200',
charges,
});
function fixture(t) {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'maimai-fees-'));
const l = new Ledger(path.join(dir, 'ledger.sqlite'));
t.after(() => {
l.close();
fs.rmSync(dir, { recursive: true, force: true });
});
const run = (data, fn, key = randomUUID()) => l.transact(key, actor, data, fn);
const save = input =>
run(input, () =>
l.saveOrder({ date, customer: '合成客户', salesperson: '合成开单人', discount: '0', ...input }, actor),
).id;
const act = (id, action, input = {}) =>
run({ id, action, input }, () => historicalAction(l, id, action, { date, ...input }, actor));
return { l, run, save, act };
}
test('选入附加项目自动带入成本:证书4、包装3、快递整单6;未知不猜数', () => {
const charges = [charge('附加费'), charge('编绳'), charge('证书'), charge('包装'), charge('物料', '')];
const rows = Estimates.sync([], [product(charges)], { addShipping: true });
assert.deepEqual(
rows.map(r => [r.note, r.amount]),
[
['快递费', '6'],
['附加费', ''],
['编绳', ''],
['证书', '4'],
['包装', '3'],
['物料', ''],
],
);
assert.ok(rows.every(r => r.status === 'estimated'));
assert.deepEqual(
rows.map(r => r.payer),
['assistant', 'assistant', 'assistant', 'assistant', 'owner', 'assistant'],
);
assert.equal(Estimates.sync([], [product([charge('包装盒')])])[0].payer, 'owner');
const priced = Calc.sale(product(charges));
assert.equal(priced.sale, 124000);
assert.equal(priced.charges.length, 5);
assert.equal(priced.charges[4].amount, null);
});
test('修改默认金额、清空或填0后都不覆盖;切换未修改的项目才更新默认', () => {
const c = charge('证书');
let rows = Estimates.sync([], [product([c])]);
rows[0] = { ...rows[0], amount: '4.5', edited: true, payer: 'owner' };
for (const payer of ['owner', 'assistant'])
for (const amount of ['4.5', '', '0']) {
rows[0] = { ...rows[0], amount, payer };
const next = Estimates.sync(rows, [product([{ ...c, name: '包装' }])]);
assert.equal(next[0].amount, amount);
assert.equal(next[0].payer, payer);
}
for (const name of ['编绳', '证书']) {
const untouched = Estimates.sync([], [product([{ ...c, name }])]),
packaging = Estimates.sync(untouched, [product([{ ...c, name: '包装' }])]);
assert.equal(packaging[0].amount, '3');
assert.equal(packaging[0].payer, 'owner');
assert.equal(Estimates.sync(packaging, [product([{ ...c, name: '证书' }])])[0].payer, 'assistant');
}
});
test('删除或调整商品顺序不会串成本,重复项目逐项对应,快递不重复', () => {
const a = charge('证书'),
b = charge('证书'),
shipping = charge('快递');
let rows = Estimates.sync([], [product([a]), product([b, shipping])], { addShipping: true });
assert.equal(rows.length, 3);
rows[1] = { ...rows[1], amount: '5', edited: true };
rows[2] = { ...rows[2], amount: '7', edited: true };
rows = Estimates.sync(rows, [product([b, shipping]), product([a])]);
assert.deepEqual(
rows.map(r => r.amount),
['6', '7', '5'],
);
rows = Estimates.sync(rows, [product([b])]);
assert.deepEqual(
rows.map(r => r.amount),
['6', '7'],
);
assert.equal(Estimates.sync([], [product([])]).length, 0);
});
test('旧版已填成本按名称接入,不重复生成也不覆盖原金额', () => {
const rows = Estimates.sync(
[{ key: 'certificate', note: '证书', amount: '5.5', payer: 'owner' }],
[product([charge('证书')])],
);
assert.equal(rows.length, 1);
assert.equal(rows[0].amount, '5.5');
assert.equal(rows[0].payer, 'owner');
const c = charge('包装盒'),
old = { key: 'charge:' + c.id, note: '包装', sourceName: '包装', amount: '3', payer: 'assistant', edited: false };
for (const previous of [old, { ...old, key: 'box', sourceName: undefined }]) {
const next = Estimates.sync([previous], [product([c])]);
assert.equal(next.length, 1);
assert.equal(next[0].payer, 'assistant');
}
});
test('店铺费用配置只生成新默认值,保留旧订单、模板及手填成本', () => {
const certificate = charge('证书'),
packaging = charge('包装'),
shipping = charge('快递');
const original = Estimates.sync([], [product([certificate, packaging, shipping])], { addShipping: true });
const shopDefaults = { certificate: '8.50', packaging: '7', shipping: '12', packagingPayer: 'assistant2' };
const fresh = Estimates.sync([], [product([certificate, packaging, shipping])], {
addShipping: true,
defaultPayer: 'assistant',
shopDefaults,
});
assert.deepEqual(
fresh.map(row => [row.amount, row.payer]),
[
['12', 'assistant'],
['8.50', 'assistant'],
['7', 'assistant2'],
],
);
assert.deepEqual(
Estimates.sync(original, [product([certificate, packaging, shipping])], { addShipping: true, shopDefaults }),
original,
);
const custom = original.map(row => ({ ...row, amount: '0', payer: 'assistant2', edited: true }));
assert.deepEqual(
Estimates.sync(custom, [product([certificate, packaging, shipping])], { addShipping: true, shopDefaults }),
custom,
);
assert.deepEqual(
Estimates.sync([], [product([certificate, packaging])], {
shopDefaults: { certificate: '0', packaging: '0', shipping: '0', packagingPayer: 'assistant' },
}).map(row => [row.amount, row.payer]),
[
['0', 'assistant'],
['0', 'assistant'],
],
);
});
test('预估保存与确认订单不造支出;交付前核实真实费用,防重并准确算利润', t => {
const { l, save, act, run } = fixture(t),
charges = [charge('证书'), charge('包装')],
lines = [product(charges)],
fees = Estimates.sync([], lines, { addShipping: true });
const id = save({ lines, fee_items: fees });
assert.equal(l.detail(id).draftFees, 1300);
assert.equal(l.detail(id).estimatedProfit, 20700);
save({ id, revision: l.detail(id).revision, lines, fee_items: fees });
assert.equal(l.detail(id).fee_items.length, 3);
act(id, 'confirm');
assert.equal(l.detail(id).expenses.length, 0);
assert.equal(l.detail(id).pendingFees.length, 3);
assert.equal(l.report(date).expense, 0);
assert.ok(l.report(date).warnings.some(w => w.message === '附加成本待核实'));
act(id, 'receipt', { amount: '1220', account: '助理微信', verified: true });
assert.throws(() => act(id, 'deliver', { verified: true }), /附加成本/);
const payload = {
date,
revision: l.detail(id).revision,
verified: true,
fee_items: fees.map(f => ({ ...f, amount: f.note === '证书' ? '4.5' : f.note === '包装' ? '0' : '6' })),
},
key = randomUUID();
run(payload, () => historicalAction(l, id, 'fees', payload, actor), key);
run(payload, () => historicalAction(l, id, 'fees', payload, actor), key);
assert.equal(l.detail(id).expenses.length, 2);
assert.equal(l.detail(id).directCost, 1050);
assert.equal(l.detail(id).pendingFees.length, 0);
assert.equal(l.detail(id).draftFees, 0);
assert.throws(() => act(id, 'fees', payload), /没有待核实/);
act(id, 'deliver', { verified: true });
assert.equal(l.detail(id).profit, 20950);
assert.equal(l.report(date).expense, 1050);
const publicJson = JSON.stringify(l.publicOrder(id));
for (const privateKey of ['pendingFees', 'fee_items', 'sourceName', 'estimated', 'payer', '4.5'])
assert.ok(!publicJson.includes(privateKey));
});
test('未知预估成本留空,不能当作0确认;补齐和旧版本检查可阻止漏记', t => {
const { l, save, act } = fixture(t),
lines = [product([charge('编绳')])],
fees = Estimates.sync([], lines);
const id = save({ lines, fee_items: fees });
assert.equal(l.detail(id).estimatedProfit, null);
assert.equal(l.detail(id).fee_items[0].amount, null);
assert.throws(() => act(id, 'confirm', { feesVerified: true }), /补齐/);
act(id, 'confirm');
const revision = l.detail(id).revision;
assert.throws(() => act(id, 'fees', { revision, verified: true, fee_items: fees }), /补齐/);
assert.throws(
() => act(id, 'fees', { revision: revision - 1, verified: true, fee_items: [{ ...fees[0], amount: '0' }] }),
/更新/,
);
assert.throws(() => act(id, 'fees', { revision, verified: true, fee_items: [] }), /逐项/);
act(id, 'fees', { revision, verified: true, fee_items: [{ ...fees[0], amount: '0' }] });
assert.equal(l.detail(id).expenses.length, 0);
assert.equal(l.detail(id).pendingFees.length, 0);
});
test('确认时勾选已付款只记一次;未付预估取消时不扣利润', t => {
const { l, save, act } = fixture(t),
lines = [product([charge('证书')])],
fee_items = Estimates.sync([], lines, { addShipping: true });
const id = save({ lines, fee_items });
act(id, 'confirm', { feesVerified: true });
assert.equal(l.detail(id).directCost, 1000);
assert.equal(l.detail(id).pendingFees.length, 0);
assert.throws(() => act(id, 'confirm', { feesVerified: true }), /已经确认/);
const cancelled = save({ lines, fee_items });
act(cancelled, 'confirm');
act(cancelled, 'cancel', { reason: '取消预订' });
assert.equal(l.detail(cancelled).profit, 0);
assert.equal(l.detail(cancelled).expenses.length, 0);
assert.equal(l.detail(cancelled).pendingFees.length, 0);
});
test('店铺费用配置只生成新默认值,保留旧订单、模板及手填成本', () => {
const certificate = charge('证书'),
packaging = charge('包装'),
shipping = charge('快递');
const original = Estimates.sync([], [product([certificate, packaging, shipping])], { addShipping: true });
const shopDefaults = { certificate: '8.50', packaging: '7', shipping: '12', packagingPayer: 'assistant2' };
const fresh = Estimates.sync([], [product([certificate, packaging, shipping])], {
addShipping: true,
defaultPayer: 'assistant',
shopDefaults,
});
assert.deepEqual(
fresh.map(row => [row.amount, row.payer]),
[
['12', 'assistant'],
['8.50', 'assistant'],
['7', 'assistant2'],
],
);
assert.deepEqual(
Estimates.sync(original, [product([certificate, packaging, shipping])], { addShipping: true, shopDefaults }),
original,
);
const custom = original.map(row => ({ ...row, amount: '0', payer: 'assistant2', edited: true }));
assert.deepEqual(
Estimates.sync(custom, [product([certificate, packaging, shipping])], { addShipping: true, shopDefaults }),
custom,
);
assert.deepEqual(
Estimates.sync([], [product([certificate, packaging])], {
shopDefaults: { certificate: '0', packaging: '0', shipping: '0', packagingPayer: 'assistant' },
}).map(row => [row.amount, row.payer]),
[
['0', 'assistant'],
['0', 'assistant'],
],
);
});