2026-10-17 19:50:24 +08:00
|
|
|
'use strict';
|
2026-10-23 00:38:04 +08:00
|
|
|
const { historicalAction } = require('./fixtures/legacy-cancellation.cjs');
|
2026-10-17 19:50:24 +08:00
|
|
|
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'),
|
2026-12-04 13:01:19 -08:00
|
|
|
{ Ledger } = require('../core/ledger');
|
2026-10-17 19:50:24 +08:00
|
|
|
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,
|
|
|
|
|
});
|
2026-10-23 09:49:36 +08:00
|
|
|
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 });
|
2026-12-04 08:58:53 -08:00
|
|
|
});
|
2026-10-23 09:49:36 +08:00
|
|
|
const run = (data, fn, key = randomUUID()) => l.transact(key, actor, data, fn);
|
|
|
|
|
const save = input =>
|
|
|
|
|
run(input, () =>
|
2026-10-17 19:50:24 +08:00
|
|
|
l.saveOrder({ date, customer: '合成客户', salesperson: '合成开单人', discount: '0', ...input }, actor),
|
2026-12-04 08:58:53 -08:00
|
|
|
).id;
|
2026-10-23 09:49:36 +08:00
|
|
|
const act = (id, action, input = {}) =>
|
|
|
|
|
run({ id, action, input }, () => historicalAction(l, id, action, { date, ...input }, actor));
|
|
|
|
|
return { l, run, save, act };
|
2026-12-04 08:58:53 -08:00
|
|
|
}
|
2026-10-17 19:50:24 +08:00
|
|
|
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]),
|
2026-12-04 08:58:53 -08:00
|
|
|
[
|
2026-10-17 19:50:24 +08:00
|
|
|
['快递费', '6'],
|
|
|
|
|
['附加费', ''],
|
|
|
|
|
['编绳', ''],
|
|
|
|
|
['证书', '4'],
|
|
|
|
|
['包装', '3'],
|
|
|
|
|
['物料', ''],
|
2026-12-04 08:58:53 -08:00
|
|
|
],
|
2026-10-17 19:50:24 +08:00
|
|
|
);
|
|
|
|
|
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);
|
2026-12-04 08:58:53 -08:00
|
|
|
}
|
2026-10-17 19:50:24 +08:00
|
|
|
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');
|
2026-12-04 08:58:53 -08:00
|
|
|
}
|
2026-10-17 19:50:24 +08:00
|
|
|
});
|
|
|
|
|
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'],
|
2026-12-04 08:58:53 -08:00
|
|
|
);
|
2026-10-17 19:50:24 +08:00
|
|
|
assert.equal(Estimates.sync([], [product([])]).length, 0);
|
|
|
|
|
});
|
2026-11-14 01:16:35 +08:00
|
|
|
test('旧版已填成本按名称接入,不重复生成也不覆盖原金额', () => {
|
2026-10-17 19:50:24 +08:00
|
|
|
const rows = Estimates.sync(
|
2026-11-14 01:16:35 +08:00
|
|
|
[{ key: 'certificate', note: '证书', amount: '5.5', payer: 'owner' }],
|
|
|
|
|
[product([charge('证书')])],
|
2026-12-04 08:58:53 -08:00
|
|
|
);
|
2026-11-14 01:16:35 +08:00
|
|
|
assert.equal(rows.length, 1);
|
|
|
|
|
assert.equal(rows[0].amount, '5.5');
|
2026-10-17 19:50:24 +08:00
|
|
|
assert.equal(rows[0].payer, 'owner');
|
2026-11-14 01:16:35 +08:00
|
|
|
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');
|
2026-12-04 08:58:53 -08:00
|
|
|
}
|
2026-11-14 01:16:35 +08:00
|
|
|
});
|
2026-10-17 19:50:24 +08:00
|
|
|
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])], {
|
2026-11-16 23:03:36 +08:00
|
|
|
addShipping: true,
|
2026-10-17 19:50:24 +08:00
|
|
|
defaultPayer: 'assistant',
|
|
|
|
|
shopDefaults,
|
|
|
|
|
});
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
fresh.map(row => [row.amount, row.payer]),
|
2026-12-04 08:58:53 -08:00
|
|
|
[
|
2026-10-17 19:50:24 +08:00
|
|
|
['12', 'assistant'],
|
|
|
|
|
['8.50', 'assistant'],
|
2026-11-16 23:03:36 +08:00
|
|
|
['7', 'assistant2'],
|
2026-12-04 08:58:53 -08:00
|
|
|
],
|
2026-10-17 19:50:24 +08:00
|
|
|
);
|
|
|
|
|
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 }),
|
2026-12-04 08:58:53 -08:00
|
|
|
custom,
|
2026-10-17 19:50:24 +08:00
|
|
|
);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
Estimates.sync([], [product([certificate, packaging])], {
|
|
|
|
|
shopDefaults: { certificate: '0', packaging: '0', shipping: '0', packagingPayer: 'assistant' },
|
|
|
|
|
}).map(row => [row.amount, row.payer]),
|
2026-12-04 08:58:53 -08:00
|
|
|
[
|
2026-10-17 19:50:24 +08:00
|
|
|
['0', 'assistant'],
|
|
|
|
|
['0', 'assistant'],
|
2026-12-04 08:58:53 -08:00
|
|
|
],
|
2026-10-17 19:50:24 +08:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
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 }), /附加成本/);
|
2026-10-23 00:38:04 +08:00
|
|
|
const payload = {
|
2026-12-04 08:58:53 -08:00
|
|
|
date,
|
2026-10-17 19:50:24 +08:00
|
|
|
revision: l.detail(id).revision,
|
|
|
|
|
verified: true,
|
|
|
|
|
fee_items: fees.map(f => ({ ...f, amount: f.note === '证书' ? '4.5' : f.note === '包装' ? '0' : '6' })),
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
2026-10-23 09:49:36 +08:00
|
|
|
key = randomUUID();
|
2026-10-17 19:50:24 +08:00
|
|
|
run(payload, () => historicalAction(l, id, 'fees', payload, actor), key);
|
2026-10-23 00:38:04 +08:00
|
|
|
run(payload, () => historicalAction(l, id, 'fees', payload, actor), key);
|
|
|
|
|
assert.equal(l.detail(id).expenses.length, 2);
|
|
|
|
|
assert.equal(l.detail(id).directCost, 1050);
|
2026-10-17 19:50:24 +08:00
|
|
|
assert.equal(l.detail(id).pendingFees.length, 0);
|
2026-10-23 00:38:04 +08:00
|
|
|
assert.equal(l.detail(id).draftFees, 0);
|
|
|
|
|
assert.throws(() => act(id, 'fees', payload), /没有待核实/);
|
2026-10-17 19:50:24 +08:00
|
|
|
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' }] }),
|
2026-12-04 08:58:53 -08:00
|
|
|
/更新/,
|
2026-10-17 19:50:24 +08:00
|
|
|
);
|
|
|
|
|
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 => {
|
2026-10-23 09:49:36 +08:00
|
|
|
const { l, save, act } = fixture(t),
|
2026-10-17 19:50:24 +08:00
|
|
|
lines = [product([charge('证书')])],
|
2026-11-14 01:16:35 +08:00
|
|
|
fee_items = Estimates.sync([], lines, { addShipping: true });
|
2026-10-23 09:49:36 +08:00
|
|
|
const id = save({ lines, fee_items });
|
2026-10-17 19:50:24 +08:00
|
|
|
act(id, 'confirm', { feesVerified: true });
|
2026-10-23 00:38:04 +08:00
|
|
|
assert.equal(l.detail(id).directCost, 1000);
|
|
|
|
|
assert.equal(l.detail(id).pendingFees.length, 0);
|
2026-10-17 19:50:24 +08:00
|
|
|
assert.throws(() => act(id, 'confirm', { feesVerified: true }), /已经确认/);
|
2026-10-23 09:49:36 +08:00
|
|
|
const cancelled = save({ lines, fee_items });
|
|
|
|
|
act(cancelled, 'confirm');
|
2026-10-17 19:50:24 +08:00
|
|
|
act(cancelled, 'cancel', { reason: '取消预订' });
|
|
|
|
|
assert.equal(l.detail(cancelled).profit, 0);
|
2026-10-23 00:38:04 +08:00
|
|
|
assert.equal(l.detail(cancelled).expenses.length, 0);
|
|
|
|
|
assert.equal(l.detail(cancelled).pendingFees.length, 0);
|
2026-12-04 08:58:53 -08:00
|
|
|
});
|
2026-11-16 23:03:36 +08:00
|
|
|
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]),
|
2026-12-04 08:58:53 -08:00
|
|
|
[
|
2026-11-16 23:03:36 +08:00
|
|
|
['12', 'assistant'],
|
|
|
|
|
['8.50', 'assistant'],
|
|
|
|
|
['7', 'assistant2'],
|
2026-12-04 08:58:53 -08:00
|
|
|
],
|
2026-11-16 23:03:36 +08:00
|
|
|
);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
Estimates.sync(original, [product([certificate, packaging, shipping])], { addShipping: true, shopDefaults }),
|
|
|
|
|
original,
|
2026-12-04 08:58:53 -08:00
|
|
|
);
|
2026-11-16 23:03:36 +08:00
|
|
|
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]),
|
2026-12-04 08:58:53 -08:00
|
|
|
[
|
2026-11-16 23:03:36 +08:00
|
|
|
['0', 'assistant'],
|
|
|
|
|
['0', 'assistant'],
|
2026-12-04 08:58:53 -08:00
|
|
|
],
|
2026-11-16 23:03:36 +08:00
|
|
|
);
|
|
|
|
|
});
|