2026-10-17 19:50:24 +08:00
|
|
|
'use strict';
|
2026-12-04 08:58:53 -08:00
|
|
|
const { historicalAction } = require('./fixtures/legacy-cancellation.cjs');
|
|
|
|
|
const { test } = require('node:test'),
|
|
|
|
|
assert = require('node:assert/strict');
|
|
|
|
|
const { randomUUID } = require('node:crypto'),
|
|
|
|
|
fs = require('node:fs'),
|
|
|
|
|
os = require('node:os'),
|
|
|
|
|
path = require('node:path');
|
2026-12-04 13:01:19 -08:00
|
|
|
const { Ledger, today } = require('../core/ledger'),
|
2026-12-04 08:58:53 -08:00
|
|
|
{ createApp } = require('../server');
|
|
|
|
|
const actor = { id: 'book-owner', name: '测试麦麦', role: 'owner' };
|
|
|
|
|
const D = '2026-09-30',
|
|
|
|
|
D2 = '2026-10-01';
|
|
|
|
|
const line = (extra = {}) => ({
|
|
|
|
|
name: '合成商品',
|
|
|
|
|
unit: 'g',
|
|
|
|
|
quantity: '1.200',
|
|
|
|
|
pocket: '',
|
|
|
|
|
payer: 'owner',
|
|
|
|
|
sale: '1200',
|
|
|
|
|
costParts: { material: '980', labor: '20', accessories: '0', other: '0' },
|
|
|
|
|
...extra,
|
|
|
|
|
});
|
|
|
|
|
function fixture(t) {
|
|
|
|
|
const ledger = new Ledger(':memory:');
|
|
|
|
|
t.after(() => ledger.close());
|
|
|
|
|
const run = (payload, fn, key = randomUUID()) => ledger.transact(key, actor, payload, fn);
|
|
|
|
|
const save = (extra = {}, issue = false) => {
|
|
|
|
|
const input = {
|
|
|
|
|
date: D,
|
|
|
|
|
customer: '合成客户',
|
|
|
|
|
salesperson: '合成开单人',
|
|
|
|
|
note: '仅用于测试',
|
|
|
|
|
payment_account: '助理微信',
|
|
|
|
|
lines: [line()],
|
|
|
|
|
fee_items: [],
|
|
|
|
|
...extra,
|
|
|
|
|
};
|
|
|
|
|
return run(input, () => (issue ? ledger.issueOrder(input, actor) : ledger.saveOrder(input, actor))).id;
|
2026-10-17 19:50:24 +08:00
|
|
|
};
|
2026-12-04 08:58:53 -08:00
|
|
|
const action = (id, kind, extra = {}, key = randomUUID()) => {
|
|
|
|
|
const input = { date: D, ...extra };
|
|
|
|
|
return run({ id, kind, input }, () => historicalAction(ledger, id, kind, input, actor), key);
|
2026-10-17 19:50:24 +08:00
|
|
|
};
|
2026-12-04 08:58:53 -08:00
|
|
|
const book = (query = {}) => ledger.orderBook({ date: D, ...query });
|
|
|
|
|
return { ledger, save, action, book, run };
|
|
|
|
|
}
|
|
|
|
|
function channel(receipts, key) {
|
|
|
|
|
return receipts.channels.find(row => row.channel === key);
|
2026-10-17 19:50:24 +08:00
|
|
|
}
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('克重搜索精确匹配任一克商品行或整单展示合计,银饰按克、件数和金额不充当克重', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
g = (quantity, extra = {}) =>
|
|
|
|
|
line({
|
|
|
|
|
quantity,
|
|
|
|
|
sale: '100',
|
|
|
|
|
costParts: { material: '80', labor: '0', accessories: '0', other: '0' },
|
|
|
|
|
...extra,
|
|
|
|
|
}),
|
|
|
|
|
save = (lines, extra = {}) => f.save({ lines, ...extra }, true);
|
|
|
|
|
const one = save([g('1.38')]),
|
|
|
|
|
part = save([g('1.38'), g('0.5')]),
|
|
|
|
|
total = save([g('0.6'), g('0.78')]),
|
|
|
|
|
silver = save([g('1.38', { name: '纯银925' })]);
|
|
|
|
|
const piece = save([g('2', { unit: '件' })]),
|
|
|
|
|
near = save([g('1.381')]),
|
|
|
|
|
larger = save([g('13.8')]),
|
|
|
|
|
text = save([g('0.4')], { customer: '文字1.38克客户' }),
|
|
|
|
|
code = save([g('0.7', { code: 'SKU1.38' })]),
|
|
|
|
|
cash = save([
|
|
|
|
|
g('0.8', { sale: '1.38', costParts: { material: '0.30', labor: '0', accessories: '0', other: '0' } }),
|
|
|
|
|
]),
|
|
|
|
|
tiny = save([g('0.001')]);
|
|
|
|
|
const changes = f.ledger.one('SELECT total_changes() AS n').n,
|
|
|
|
|
report = f.ledger.reportByOrderDate(D),
|
|
|
|
|
expected = new Set([one, part, total, silver]);
|
|
|
|
|
for (const q of ['1.38克', '1.38g', '1.380G', ' 1.3800 克 ']) {
|
|
|
|
|
const result = f.book({ q });
|
|
|
|
|
assert.deepEqual(new Set(result.rows.map(row => row.id)), expected, q);
|
|
|
|
|
assert.equal(result.summaryWithoutCancelled.orderCount, 4);
|
|
|
|
|
assert.equal(result.summaryWithoutCancelled.sale, 60000);
|
|
|
|
|
assert.equal(result.summaryWithoutCancelled.totalCost, 48000);
|
|
|
|
|
assert.equal(result.summaryWithoutCancelled.profit, 12000);
|
|
|
|
|
assert.equal(result.receiptsWithoutCancelled.receipt, 60000);
|
|
|
|
|
assert.equal(result.summaryWithoutCancelled.mg, 4640);
|
|
|
|
|
assert.equal(result.rows.find(row => row.id === silver).mg, 1380);
|
|
|
|
|
}
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
new Set(f.book({ q: '1.38' }).rows.map(row => row.id)),
|
|
|
|
|
new Set([...expected, text, code]),
|
|
|
|
|
'纯数字保留客户及编号文字搜索OR',
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
f.book({ q: '1.381g' }).rows.map(row => row.id),
|
|
|
|
|
[near],
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
f.book({ q: '13.8克' }).rows.map(row => row.id),
|
|
|
|
|
[larger],
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
f.book({ q: '0.001g' }).rows.map(row => row.id),
|
|
|
|
|
[tiny],
|
|
|
|
|
);
|
|
|
|
|
for (const q of ['2克', '0g', '1.3801g', '9007199254740992g']) assert.equal(f.book({ q }).rows.length, 0, q);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
f.book({ q: '文字1.38克客户' }).rows.map(row => row.id),
|
|
|
|
|
[text],
|
|
|
|
|
);
|
|
|
|
|
assert.ok(!f.book({ q: '1.38克' }).rows.some(row => [piece, cash].includes(row.id)));
|
|
|
|
|
assert.deepEqual(f.ledger.reportByOrderDate(D), report);
|
|
|
|
|
assert.equal(f.ledger.one('SELECT total_changes() AS n').n, changes, '重量搜索只读');
|
2026-11-01 23:40:44 +08:00
|
|
|
});
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('订单簿与已记账成本、付款人和利润一致,克与件分开,附加成本组成可核对', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
const id = f.save(
|
|
|
|
|
{
|
|
|
|
|
lines: [
|
|
|
|
|
line({
|
|
|
|
|
quantity: '2.200',
|
|
|
|
|
sale: '2300',
|
|
|
|
|
costParts: { material: '1980', labor: '0', accessories: '5', other: '0' },
|
|
|
|
|
costLabor: { mode: 'gram', rate: '20' },
|
|
|
|
|
}),
|
|
|
|
|
line({
|
|
|
|
|
name: '纯银925',
|
|
|
|
|
unit: '件',
|
|
|
|
|
quantity: '2',
|
|
|
|
|
payer: 'assistant',
|
|
|
|
|
sale: '200',
|
|
|
|
|
costParts: { material: '80', labor: '10', accessories: '0', other: '0' },
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
fee_items: [
|
|
|
|
|
{ key: 'shipping', note: '快递费', amount: '6', payer: 'assistant' },
|
|
|
|
|
{ key: 'certificate', note: '证书', amount: '4', payer: 'owner' },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
true,
|
|
|
|
|
);
|
|
|
|
|
const b = f.book(),
|
|
|
|
|
r = b.rows[0];
|
|
|
|
|
assert.equal(r.id, id);
|
|
|
|
|
assert.deepEqual(r.types, ['配货', '配货 · 银饰']);
|
|
|
|
|
assert.equal(r.mg, 2200);
|
|
|
|
|
assert.equal(r.summaryMg, 2200);
|
|
|
|
|
assert.equal(r.pieces, 2);
|
|
|
|
|
assert.equal(r.sale, 250000);
|
|
|
|
|
assert.equal(r.material, 206000);
|
|
|
|
|
assert.equal(r.laborAndFees, 6900);
|
|
|
|
|
assert.equal(r.unallocated, 0);
|
|
|
|
|
assert.equal(r.totalCost, 212900);
|
|
|
|
|
assert.equal(r.ownerCost, 203300);
|
|
|
|
|
assert.equal(r.assistantCost, 9600);
|
|
|
|
|
assert.equal(r.profit, 37100);
|
|
|
|
|
assert.equal(r.profit, f.ledger.detail(id).profit);
|
|
|
|
|
assert.equal(r.laborAndFeeItems.find(row => row.component === 'labor').amount, 4400);
|
|
|
|
|
assert.equal(
|
|
|
|
|
r.laborAndFeeItems.reduce((s, row) => s + row.amount, 0),
|
|
|
|
|
r.laborAndFees,
|
|
|
|
|
);
|
|
|
|
|
assert.equal(r.material + r.laborAndFees + r.unallocated, r.totalCost);
|
|
|
|
|
assert.equal(b.summary.sale - b.summary.totalCost, b.summary.profit);
|
|
|
|
|
assert.equal(b.summary.completedCount, 1);
|
|
|
|
|
assert.equal(b.summary.partial, false);
|
|
|
|
|
assert.equal(b.summary.mg, 2200);
|
|
|
|
|
assert.equal(b.summary.pieces, 2);
|
2026-10-17 19:50:24 +08:00
|
|
|
});
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('旧单只保存总成本时保留未分项,不伪拆料费和工费,分币仍精确', t => {
|
|
|
|
|
const { save, book } = fixture(t);
|
|
|
|
|
save(
|
|
|
|
|
{
|
|
|
|
|
lines: [line({ unit: '件', quantity: '1', costParts: undefined, cost: '12.34', sale: '50' })],
|
|
|
|
|
fee_items: [{ key: 'box', note: '包装', amount: '0.01', payer: 'assistant' }],
|
|
|
|
|
},
|
|
|
|
|
true,
|
|
|
|
|
);
|
|
|
|
|
const b = book(),
|
|
|
|
|
r = b.rows[0];
|
|
|
|
|
assert.equal(r.material, null);
|
|
|
|
|
assert.equal(r.laborAndFees, null);
|
|
|
|
|
assert.equal(r.known.material, 0);
|
|
|
|
|
assert.equal(r.known.laborAndFees, 1);
|
|
|
|
|
assert.equal(r.unallocated, 1234);
|
|
|
|
|
assert.equal(r.totalCost, 1235);
|
|
|
|
|
assert.equal(r.profit, 3765);
|
|
|
|
|
assert.equal(r.costItems.find(row => row.component === 'unallocated').amount, 1234);
|
|
|
|
|
assert.equal(r.laborAndFeeItems.length, 1);
|
|
|
|
|
assert.equal(b.summary.unallocated, 1234);
|
|
|
|
|
assert.equal(b.summary.laborAndFees, 1);
|
|
|
|
|
assert.equal(b.summary.legacyUnallocatedCount, 1);
|
|
|
|
|
assert.equal(b.summary.unknownCostCount, 0);
|
|
|
|
|
assert.equal(b.summary.unknownCounts.material, 1);
|
|
|
|
|
assert.equal(b.summary.incompleteCount, 0);
|
|
|
|
|
assert.equal(b.summary.partial, true);
|
2026-10-17 19:50:24 +08:00
|
|
|
});
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('已交付料池锁价缺料时订单簿与日报保留待补而非零成本,补料后恢复', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
l = f.ledger,
|
|
|
|
|
date = today(),
|
|
|
|
|
purchase = input => f.run(input, () => l.createMaterialPurchase(input, actor));
|
|
|
|
|
f.run({}, () => l.stock({ date, kind: 'opening', pocket: '现货', grams: '10', note: '合成库存' }, actor));
|
|
|
|
|
const bought = purchase({ date, grams: '1', total: '900', payer: 'owner', note: '合成购料' }),
|
|
|
|
|
preview = l.rollingMaterialPreview({}, actor);
|
|
|
|
|
f.run(preview, () => l.enableRollingMaterial({ date, revision: preview.revision, verified: true }, actor));
|
|
|
|
|
const input = {
|
|
|
|
|
date,
|
|
|
|
|
customer: '合成料池锁价客户',
|
|
|
|
|
salesperson: '合成开单人',
|
|
|
|
|
line: { name: '足金锁价挂件', unit: 'g', quantity: '1.000', goldPrice: '1000', laborRate: '0', laborMode: 'gram' },
|
|
|
|
|
material: { source: 'pool', poolId: date, revision: l.materialPoolDetail(date).revision },
|
|
|
|
|
payment: { amount: '1000', account: '助理微信', verified: true },
|
|
|
|
|
};
|
|
|
|
|
const lock = f.run(input, () => l.createPriceLock(input, actor)),
|
|
|
|
|
delivery = {
|
|
|
|
|
date,
|
|
|
|
|
revision: l.priceLockDetail(lock.id).revision,
|
|
|
|
|
verified: true,
|
|
|
|
|
line: {
|
|
|
|
|
quantity: '1.000',
|
|
|
|
|
laborRate: '0',
|
|
|
|
|
laborMode: 'gram',
|
|
|
|
|
payer: 'owner',
|
|
|
|
|
costParts: { labor: '30', accessories: '0', other: '0' },
|
|
|
|
|
costPayers: { material: 'owner', labor: 'owner' },
|
|
|
|
|
},
|
|
|
|
|
inventory: { pocket: '现货', original: 'stocked' },
|
|
|
|
|
fee_items: [{ key: 'shipping', note: '合成快递费', amount: '6', payer: 'owner' }],
|
|
|
|
|
},
|
|
|
|
|
id = f.run(delivery, () => l.deliverPriceLock(lock.id, delivery, actor)).id;
|
|
|
|
|
f.run({}, () => l.deleteMaterialPurchase(bought.id, { revision: 1 }, actor));
|
|
|
|
|
let row = f.book({ date }).rows.find(row => row.id === id),
|
|
|
|
|
report = l.reportByOrderDate(date);
|
|
|
|
|
assert.deepEqual(row.materialPoolIds, [date]);
|
|
|
|
|
assert.equal(row.materialCostPending, true);
|
|
|
|
|
for (const key of ['material', 'totalCost', 'profit']) assert.equal(row[key], null, key);
|
|
|
|
|
assert.equal(row.known.totalCost, 3600);
|
|
|
|
|
assert.equal(row.missingCosts, 1);
|
|
|
|
|
assert.match(row.costHint, /料费待补/);
|
|
|
|
|
assert.equal(row.incomplete, true);
|
|
|
|
|
assert.equal(report.pendingMaterialCount, 1);
|
|
|
|
|
assert.equal(report.materialCostPending, true);
|
|
|
|
|
assert.equal(report.summary.unknownCostCount, 1);
|
|
|
|
|
assert.equal(report.summary.unknownProfitCount, 1);
|
|
|
|
|
assert.equal(report.orderRows.find(row => row.id === id).materialCostPending, true);
|
|
|
|
|
purchase({ date, grams: '1', total: '950', payer: 'assistant', note: '合成补料' });
|
|
|
|
|
row = f.book({ date }).rows.find(row => row.id === id);
|
|
|
|
|
report = l.reportByOrderDate(date);
|
|
|
|
|
assert.deepEqual(row.materialPoolIds, [date]);
|
|
|
|
|
assert.equal(row.materialCostPending, false);
|
|
|
|
|
assert.equal(row.material, 95000);
|
|
|
|
|
assert.equal(row.totalCost, 98600);
|
|
|
|
|
assert.equal(row.profit, 1400);
|
|
|
|
|
assert.equal(row.incomplete, false);
|
|
|
|
|
assert.equal(report.pendingMaterialCount, 0);
|
2026-11-05 00:04:01 +08:00
|
|
|
});
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('缺项旧单只累计已知实际成本,草稿与预估费用不冒充实际销售、成本或利润', t => {
|
|
|
|
|
const { save, action, book } = fixture(t);
|
|
|
|
|
const old = save({
|
|
|
|
|
lines: [line({ costParts: { material: '', labor: '0', accessories: '5', other: '0' } })],
|
|
|
|
|
fee_items: [
|
|
|
|
|
{ key: 'shipping', note: '待核快递', amount: '6', payer: 'assistant', status: 'estimated' },
|
|
|
|
|
{ key: 'box', note: '待补包装', amount: '', payer: 'owner', status: 'estimated' },
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
action(old, 'confirm');
|
|
|
|
|
action(old, 'expense', { amount: '24', payer: 'assistant', note: '实际付出的证书费' });
|
|
|
|
|
const draft = save({ customer: '草稿客户', lines: [line({ sale: '9999' })] });
|
|
|
|
|
const b = book(),
|
|
|
|
|
r = b.rows.find(row => row.id === old),
|
|
|
|
|
d = b.rows.find(row => row.id === draft);
|
|
|
|
|
assert.equal(r.material, null);
|
|
|
|
|
assert.equal(r.laborAndFees, null);
|
|
|
|
|
assert.equal(r.totalCost, null);
|
|
|
|
|
assert.equal(r.ownerCost, null);
|
|
|
|
|
assert.equal(r.assistantCost, null);
|
|
|
|
|
assert.equal(r.profit, null);
|
|
|
|
|
assert.equal(r.known.totalCost, 2900);
|
|
|
|
|
assert.equal(r.known.ownerCost, 500);
|
|
|
|
|
assert.equal(r.known.assistantCost, 2400);
|
|
|
|
|
assert.equal(r.missingCosts, 1);
|
|
|
|
|
assert.equal(r.pendingCosts, 2);
|
|
|
|
|
assert.equal(r.unknownCosts, 3);
|
|
|
|
|
assert.equal(r.pendingCostItems.length, 2);
|
|
|
|
|
assert.equal(r.costItems.filter(row => row.kind === 'paid').length, 1);
|
|
|
|
|
assert.ok(r.costItems.every(row => row.kind !== 'pending'));
|
|
|
|
|
for (const key of [
|
|
|
|
|
'sale',
|
|
|
|
|
'material',
|
|
|
|
|
'laborAndFees',
|
|
|
|
|
'unallocated',
|
|
|
|
|
'totalCost',
|
|
|
|
|
'ownerCost',
|
|
|
|
|
'assistantCost',
|
|
|
|
|
'profit',
|
|
|
|
|
])
|
|
|
|
|
assert.equal(d[key], null, key);
|
|
|
|
|
assert.equal(d.originalSale, 999900);
|
|
|
|
|
assert.equal(d.costItems.length, 0);
|
|
|
|
|
assert.equal(b.summary.orderCount, 2);
|
|
|
|
|
assert.equal(b.summary.draftCount, 1);
|
|
|
|
|
assert.equal(b.summary.incompleteCount, 1);
|
|
|
|
|
assert.equal(b.summary.sale, 120000);
|
|
|
|
|
assert.equal(b.summary.totalCost, 2900);
|
|
|
|
|
assert.equal(b.summary.ownerCost, 500);
|
|
|
|
|
assert.equal(b.summary.assistantCost, 2400);
|
|
|
|
|
assert.equal(b.summary.profit, 0);
|
|
|
|
|
assert.equal(b.summary.unknownProfitCount, 1);
|
|
|
|
|
assert.equal(b.summary.unknownCostCount, 1);
|
|
|
|
|
assert.equal(b.summary.partial, true);
|
2026-10-17 19:50:24 +08:00
|
|
|
});
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('待交付旧单即使录全成本也不产生已完成利润;已退和取消货品净额为0、实际费用保留', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
const confirmed = f.save({ customer: '成本已齐旧单' });
|
|
|
|
|
f.action(confirmed, 'confirm');
|
|
|
|
|
let r = f.book().rows[0];
|
|
|
|
|
assert.equal(r.totalCost, 100000);
|
|
|
|
|
assert.equal(r.profit, null);
|
|
|
|
|
assert.equal(r.incomplete, true);
|
|
|
|
|
const returned = f.save(
|
|
|
|
|
{ customer: '退货客户', fee_items: [{ key: 'shipping', note: '已付运费', amount: '8', payer: 'assistant' }] },
|
|
|
|
|
true,
|
|
|
|
|
);
|
|
|
|
|
f.action(returned, 'return', { date: D2, reason: '合成退货', verified: true });
|
|
|
|
|
f.action(returned, 'refund', { date: D2, amount: '1200', account: '助理微信', verified: true });
|
|
|
|
|
const cancelled = f.save({ customer: '取消客户' });
|
|
|
|
|
f.action(cancelled, 'confirm');
|
|
|
|
|
f.action(cancelled, 'expense', { amount: '2', payer: 'owner', note: '已付打样' });
|
|
|
|
|
f.action(cancelled, 'receipt', { amount: '100', account: '麦麦现金', verified: true });
|
|
|
|
|
f.action(cancelled, 'cancel', { date: D2, reason: '合成取消' });
|
|
|
|
|
f.action(cancelled, 'refund', { date: D2, amount: '100', account: '麦麦现金', verified: true });
|
|
|
|
|
const b = f.book();
|
|
|
|
|
r = b.rows.find(row => row.id === returned);
|
|
|
|
|
assert.equal(r.sale, 0);
|
|
|
|
|
assert.equal(r.originalSale, 120000);
|
|
|
|
|
assert.equal(r.material, 0);
|
|
|
|
|
assert.equal(r.laborAndFees, 800);
|
|
|
|
|
assert.equal(r.totalCost, 800);
|
|
|
|
|
assert.equal(r.profit, -800);
|
|
|
|
|
assert.equal(r.ownerCost, 0);
|
|
|
|
|
assert.equal(r.assistantCost, 800);
|
|
|
|
|
assert.ok(r.costItems.every(item => item.component === 'fee'));
|
|
|
|
|
r = b.rows.find(row => row.id === cancelled);
|
|
|
|
|
assert.equal(r.sale, 0);
|
|
|
|
|
assert.equal(r.totalCost, 200);
|
|
|
|
|
assert.equal(r.profit, -200);
|
|
|
|
|
assert.equal(r.ownerCost, 200);
|
|
|
|
|
assert.equal(b.receiptsByPaymentDate.receipt, 130000);
|
|
|
|
|
assert.equal(b.receiptsByPaymentDate.refund, 0);
|
|
|
|
|
assert.equal(b.receiptsByOrderDate.receipt, 130000);
|
|
|
|
|
assert.equal(b.receiptsByOrderDate.refund, 130000);
|
|
|
|
|
assert.equal(b.receiptsByOrderDate.net, 0);
|
|
|
|
|
assert.equal(f.book({ date: D2 }).rows.length, 0);
|
|
|
|
|
assert.equal(f.book({ date: D2 }).receiptsByPaymentDate.refund, 130000);
|
2026-10-17 19:50:24 +08:00
|
|
|
});
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('取消单保留原明细,订单页数量克重件数收退款成本利润汇总全排除,日报仍保留真实账务', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
id = f.save({
|
|
|
|
|
customer: '取消客户',
|
|
|
|
|
lines: [
|
|
|
|
|
line({
|
|
|
|
|
quantity: '2.345',
|
|
|
|
|
sale: '900',
|
|
|
|
|
costParts: { material: '700', labor: '0', accessories: '0', other: '0' },
|
|
|
|
|
}),
|
|
|
|
|
line({
|
|
|
|
|
unit: '件',
|
|
|
|
|
quantity: '3',
|
|
|
|
|
sale: '200',
|
|
|
|
|
payer: 'assistant',
|
|
|
|
|
costParts: { material: '80', labor: '20', accessories: '0', other: '0' },
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
f.action(id, 'confirm');
|
|
|
|
|
f.action(id, 'receipt', { amount: '300', account: '助理微信', verified: true });
|
|
|
|
|
f.action(id, 'expense', { amount: '8', payer: 'assistant', note: '取消前实际快递' });
|
|
|
|
|
f.action(id, 'cancel', { date: D2, reason: '合成取消' });
|
|
|
|
|
f.action(id, 'refund', { date: D2, amount: '100', account: '助理微信', verified: true });
|
|
|
|
|
f.run({ people: true }, () => f.ledger.savePeopleCount({ date: D, count: 9, revision: 0 }, actor));
|
|
|
|
|
const detail = f.ledger.detail(id),
|
|
|
|
|
daily = f.ledger.reportByOrderDate(D),
|
|
|
|
|
legacy = f.ledger.report(D2);
|
|
|
|
|
const snapshot = () =>
|
|
|
|
|
JSON.stringify(
|
|
|
|
|
f.ledger
|
|
|
|
|
.rows("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
|
|
|
|
|
.map(({ name }) => [name, f.ledger.rows('SELECT * FROM "' + name + '"')]),
|
|
|
|
|
);
|
|
|
|
|
const before = snapshot(),
|
|
|
|
|
changes = f.ledger.one('SELECT total_changes() AS count').count;
|
|
|
|
|
for (const query of [
|
|
|
|
|
{},
|
|
|
|
|
{ status: 'cancelled' },
|
|
|
|
|
{ q: '取消客户' },
|
|
|
|
|
{ from: D, date: D2 },
|
|
|
|
|
{ from: '2026-09-01', date: D },
|
|
|
|
|
]) {
|
|
|
|
|
const b = f.book(query),
|
|
|
|
|
s = b.summaryWithoutCancelled,
|
|
|
|
|
r = b.receiptsWithoutCancelled;
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
b.rows.map(row => row.id),
|
|
|
|
|
[id],
|
|
|
|
|
);
|
|
|
|
|
assert.equal(b.rows[0].status, 'cancelled');
|
|
|
|
|
assert.equal(b.rows[0].mg, 2345);
|
|
|
|
|
assert.equal(b.rows[0].pieces, 3);
|
|
|
|
|
assert.equal(b.rows[0].totalCost, 800);
|
|
|
|
|
assert.equal(b.rows[0].profit, -800);
|
|
|
|
|
for (const key of [
|
|
|
|
|
'orderCount',
|
|
|
|
|
'recordCount',
|
|
|
|
|
'cancelledCount',
|
|
|
|
|
'mg',
|
|
|
|
|
'pieces',
|
|
|
|
|
'sale',
|
|
|
|
|
'material',
|
|
|
|
|
'laborAndFees',
|
|
|
|
|
'unallocated',
|
|
|
|
|
'totalCost',
|
|
|
|
|
'profit',
|
|
|
|
|
'ownerCost',
|
|
|
|
|
'assistantCost',
|
|
|
|
|
])
|
|
|
|
|
assert.equal(s[key], 0, key);
|
|
|
|
|
assert.equal(s.partial, false);
|
|
|
|
|
assert.equal(r.basis, 'orderDate');
|
|
|
|
|
assert.equal(r.filterScope, 'matchingOrdersWithoutCancelled');
|
|
|
|
|
assert.equal(r.receipt, 0);
|
|
|
|
|
assert.equal(r.refund, 0);
|
|
|
|
|
assert.equal(r.net, 0);
|
|
|
|
|
assert.equal(r.paymentCount, 0);
|
|
|
|
|
assert.ok(r.accounts.every(account => account.receipt === 0 && account.refund === 0 && account.net === 0));
|
|
|
|
|
assert.equal(b.summary.orderCount, 1);
|
|
|
|
|
assert.equal(b.summary.mg, 2345);
|
|
|
|
|
assert.equal(b.summary.pieces, 3);
|
|
|
|
|
assert.equal(b.summary.totalCost, 800);
|
|
|
|
|
assert.equal(b.summary.profit, -800);
|
|
|
|
|
assert.equal(b.receiptsByOrderDate.receipt, 30000);
|
|
|
|
|
assert.equal(b.receiptsByOrderDate.refund, 10000);
|
|
|
|
|
assert.equal(b.receiptsByOrderDate.net, 20000);
|
|
|
|
|
assert.equal(b.peopleCount.total, b.date === D ? 9 : null, '旧总数字段也按所选结束日读取,不跨日期相加');
|
2026-10-17 19:50:24 +08:00
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
assert.equal(daily.expense, 800);
|
|
|
|
|
assert.equal(daily.profit, -800);
|
|
|
|
|
assert.equal(daily.accounts.find(account => account.account === '助理微信').receipt, 30000);
|
|
|
|
|
assert.equal(daily.accounts.find(account => account.account === '助理微信').refund, 10000);
|
|
|
|
|
assert.equal(legacy.expense, 800);
|
|
|
|
|
assert.equal(legacy.profit, -800);
|
|
|
|
|
assert.deepEqual(f.ledger.reportByOrderDate(D), daily);
|
|
|
|
|
assert.deepEqual(f.ledger.report(D2), legacy);
|
|
|
|
|
assert.deepEqual(f.ledger.detail(id), detail);
|
|
|
|
|
assert.equal(f.ledger.one('SELECT total_changes() AS count').count, changes);
|
|
|
|
|
assert.equal(snapshot(), before);
|
2026-10-17 19:50:24 +08:00
|
|
|
});
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('排除取消的汇总仍保留有效销售、独立售后及原退货口径,日月搜索状态共同生效', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
valid = f.save({ customer: '共同有效客户' }, true);
|
|
|
|
|
const returned = f.save(
|
|
|
|
|
{
|
|
|
|
|
customer: '共同退货客户',
|
|
|
|
|
payment_account: '助理支付宝',
|
|
|
|
|
fee_items: [{ key: 'shipping', note: '已付运费', amount: '8', payer: 'assistant' }],
|
|
|
|
|
},
|
|
|
|
|
true,
|
|
|
|
|
);
|
|
|
|
|
f.action(returned, 'return', { date: D2, reason: '合成退货', verified: true });
|
|
|
|
|
f.action(returned, 'refund', { date: D2, amount: '1200', account: '助理支付宝', verified: true });
|
|
|
|
|
const cancelled = f.save({
|
|
|
|
|
customer: '共同取消客户',
|
|
|
|
|
lines: [line({ quantity: '2.500' }), line({ unit: '件', quantity: '2', sale: '20' })],
|
|
|
|
|
});
|
|
|
|
|
f.action(cancelled, 'confirm');
|
|
|
|
|
f.action(cancelled, 'expense', { amount: '2', payer: 'owner', note: '取消前实付' });
|
|
|
|
|
f.action(cancelled, 'receipt', { amount: '100', account: '麦麦微信', verified: true });
|
|
|
|
|
f.action(cancelled, 'cancel', { date: D2, reason: '合成取消' });
|
|
|
|
|
f.action(cancelled, 'refund', { date: D2, amount: '30', account: '麦麦微信', verified: true });
|
|
|
|
|
const after = f.run({ after: true }, () =>
|
|
|
|
|
f.ledger.saveAfterSale(
|
|
|
|
|
{ date: D, project: '共同售后', customer: '售后客户', amount: '20', payer: 'assistant' },
|
|
|
|
|
actor,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
f.run({ afterNext: true }, () =>
|
|
|
|
|
f.ledger.saveAfterSale(
|
|
|
|
|
{ date: D2, project: '共同隔日售后', customer: '隔日客户', amount: '1', payer: 'owner' },
|
|
|
|
|
actor,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
for (const query of [{}, { q: '共同' }, { from: '2026-09-01', date: D }]) {
|
|
|
|
|
const b = f.book(query),
|
|
|
|
|
s = b.summaryWithoutCancelled,
|
|
|
|
|
r = b.receiptsWithoutCancelled;
|
|
|
|
|
assert.equal(b.rows.length, 4);
|
|
|
|
|
assert.ok(b.rows.some(row => row.id === cancelled));
|
|
|
|
|
assert.ok(b.rows.some(row => row.after_sale_id === after.after_sale_id));
|
|
|
|
|
assert.equal(s.orderCount, 2);
|
|
|
|
|
assert.equal(s.completedCount, 1);
|
|
|
|
|
assert.equal(s.returnedCount, 1);
|
|
|
|
|
assert.equal(s.cancelledCount, 0);
|
|
|
|
|
assert.equal(s.afterSaleCount, 1);
|
|
|
|
|
assert.equal(s.recordCount, 3);
|
|
|
|
|
assert.equal(s.mg, 2400);
|
|
|
|
|
assert.equal(s.pieces, 0);
|
|
|
|
|
assert.equal(s.sale, 120000);
|
|
|
|
|
assert.equal(s.totalCost, 102800);
|
|
|
|
|
assert.equal(s.profit, 17200);
|
|
|
|
|
assert.equal(s.ownerCost, 100000);
|
|
|
|
|
assert.equal(s.assistantCost, 2800);
|
|
|
|
|
assert.equal(r.receipt, 240000);
|
|
|
|
|
assert.equal(r.refund, 120000);
|
|
|
|
|
assert.equal(r.net, 120000);
|
|
|
|
|
assert.equal(b.receiptsByOrderDate.receipt, 250000);
|
|
|
|
|
assert.equal(b.receiptsByOrderDate.refund, 123000);
|
|
|
|
|
assert.equal(b.summary.orderCount, 3);
|
|
|
|
|
assert.equal(b.summary.profit, 17000);
|
|
|
|
|
assert.equal(f.ledger.reportByOrderDate(D).profit, 17000);
|
2026-10-17 19:50:24 +08:00
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
const onlyCancelled = f.book({ q: '共同取消客户', status: 'cancelled' });
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
onlyCancelled.rows.map(row => row.id),
|
|
|
|
|
[cancelled],
|
|
|
|
|
);
|
|
|
|
|
assert.equal(onlyCancelled.summaryWithoutCancelled.orderCount, 0);
|
|
|
|
|
assert.equal(onlyCancelled.receiptsWithoutCancelled.receipt, 0);
|
|
|
|
|
for (const [status, id] of [
|
|
|
|
|
['delivered', valid],
|
|
|
|
|
['returned', returned],
|
|
|
|
|
]) {
|
|
|
|
|
const b = f.book({ q: '共同', status });
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
b.rows.map(row => row.id),
|
|
|
|
|
[id],
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(b.summaryWithoutCancelled, b.summary);
|
|
|
|
|
assert.deepEqual(b.receiptsWithoutCancelled.accounts, b.receiptsByOrderDate.accounts);
|
2026-10-17 19:50:24 +08:00
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
const onlyAfter = f.book({ status: 'after_sale' });
|
|
|
|
|
assert.equal(onlyAfter.summaryWithoutCancelled.afterSaleCount, 1);
|
|
|
|
|
assert.equal(onlyAfter.summaryWithoutCancelled.profit, -2000);
|
|
|
|
|
const next = f.book({ date: D2 });
|
|
|
|
|
assert.equal(next.summaryWithoutCancelled.afterSaleCount, 1);
|
|
|
|
|
assert.equal(next.summaryWithoutCancelled.profit, -100);
|
|
|
|
|
assert.equal(next.receiptsWithoutCancelled.receipt, 0);
|
|
|
|
|
const month = f.book({ from: D, date: D2, q: '共同' });
|
|
|
|
|
assert.equal(month.summaryWithoutCancelled.afterSaleCount, 2);
|
|
|
|
|
assert.equal(month.summaryWithoutCancelled.profit, 17100);
|
2026-10-17 19:50:24 +08:00
|
|
|
});
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('费用连续更正、换付款人与撤销只计最新有效费用,不重复原行和冲回行', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
id = f.save({ fee_items: [{ key: 'shipping', note: '邮费', amount: '6', payer: 'assistant' }] }, true);
|
|
|
|
|
let target = f.ledger.detail(id).expenses[0].id;
|
|
|
|
|
for (const [amount, payer, expected] of [
|
|
|
|
|
['8', 'owner', 100800],
|
|
|
|
|
['5', 'assistant', 100500],
|
|
|
|
|
['0', 'assistant', 100000],
|
|
|
|
|
]) {
|
|
|
|
|
f.action(id, 'correct', {
|
|
|
|
|
type: 'expense',
|
|
|
|
|
target,
|
|
|
|
|
amount,
|
|
|
|
|
payer,
|
|
|
|
|
reason: '合成费用更正',
|
|
|
|
|
verified: true,
|
|
|
|
|
date: D2,
|
|
|
|
|
});
|
|
|
|
|
const r = f.book().rows[0];
|
|
|
|
|
assert.equal(r.totalCost, expected);
|
|
|
|
|
assert.equal(r.profit, 120000 - expected);
|
|
|
|
|
assert.equal(r.profit, f.ledger.detail(id).profit);
|
|
|
|
|
assert.equal(r.costItems.filter(item => item.component === 'fee').length, amount === '0' ? 0 : 1);
|
|
|
|
|
assert.equal(r.ownerCost + r.assistantCost, r.totalCost);
|
|
|
|
|
if (amount !== '0') target = f.ledger.detail(id).expenses.find(item => item.amount === Number(amount) * 100).id;
|
2026-10-17 19:50:24 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('跨年开单与跨天尾款分开,重试不重复收款,收款更正按实际日期带符号汇总、退款另列', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
start = '2025-12-31',
|
|
|
|
|
end = '2026-01-01',
|
|
|
|
|
correction = '2026-01-02',
|
|
|
|
|
refund = '2026-01-03';
|
|
|
|
|
const id = f.save({
|
|
|
|
|
date: start,
|
|
|
|
|
lines: [line({ sale: '100', costParts: { material: '8', labor: '2', accessories: '0', other: '0' } })],
|
|
|
|
|
});
|
|
|
|
|
f.action(id, 'confirm', { date: start });
|
|
|
|
|
const key = randomUUID(),
|
|
|
|
|
deposit = { date: start, amount: '40', account: '助理微信', verified: true };
|
|
|
|
|
f.action(id, 'receipt', deposit, key);
|
|
|
|
|
f.action(id, 'receipt', deposit, key);
|
|
|
|
|
f.action(id, 'receipt', { date: end, amount: '60', account: '麦麦支付宝', verified: true });
|
|
|
|
|
f.action(id, 'deliver', { date: end, verified: true });
|
|
|
|
|
const cash = f.save(
|
|
|
|
|
{
|
|
|
|
|
date: end,
|
|
|
|
|
customer: '现金客户',
|
|
|
|
|
payment_account: '助理现金',
|
|
|
|
|
lines: [line({ sale: '30', costParts: { material: '10', labor: '0', accessories: '0', other: '0' } })],
|
|
|
|
|
},
|
|
|
|
|
true,
|
|
|
|
|
);
|
|
|
|
|
let b = f.book({ date: end });
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
b.rows.map(row => row.id),
|
|
|
|
|
[cash],
|
|
|
|
|
);
|
|
|
|
|
assert.equal(b.receiptsByPaymentDate.receipt, 9000);
|
|
|
|
|
assert.equal(channel(b.receiptsByPaymentDate, 'alipay').receipt, 6000);
|
|
|
|
|
assert.equal(channel(b.receiptsByPaymentDate, 'cash').receipt, 3000);
|
|
|
|
|
b = f.book({ date: start });
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
b.rows.map(row => row.id),
|
|
|
|
|
[id],
|
|
|
|
|
);
|
|
|
|
|
assert.equal(b.receiptsByPaymentDate.receipt, 4000);
|
|
|
|
|
assert.equal(b.receiptsByOrderDate.receipt, 10000);
|
|
|
|
|
f.action(id, 'correct', {
|
|
|
|
|
date: correction,
|
|
|
|
|
type: 'payment',
|
|
|
|
|
target: f.ledger.detail(id).payments[0].id,
|
|
|
|
|
amount: '40',
|
|
|
|
|
account: '助理支付宝',
|
|
|
|
|
reason: '实际为支付宝',
|
|
|
|
|
verified: true,
|
|
|
|
|
});
|
|
|
|
|
b = f.book({ date: correction });
|
|
|
|
|
assert.equal(b.rows.length, 0);
|
|
|
|
|
assert.equal(b.receiptsByPaymentDate.receipt, 0);
|
|
|
|
|
assert.equal(b.receiptsByPaymentDate.refund, 0);
|
|
|
|
|
assert.equal(channel(b.receiptsByPaymentDate, 'wechat').receipt, -4000);
|
|
|
|
|
assert.equal(channel(b.receiptsByPaymentDate, 'alipay').receipt, 4000);
|
|
|
|
|
b = f.book({ date: start });
|
|
|
|
|
assert.equal(channel(b.receiptsByPaymentDate, 'wechat').receipt, 4000);
|
|
|
|
|
assert.equal(channel(b.receiptsByOrderDate, 'wechat').receipt, 0);
|
|
|
|
|
assert.equal(channel(b.receiptsByOrderDate, 'alipay').receipt, 10000);
|
|
|
|
|
f.action(id, 'return', { date: refund, reason: '合成跨月退货', verified: true });
|
|
|
|
|
f.action(id, 'refund', { date: refund, amount: '60', account: '麦麦支付宝', verified: true });
|
|
|
|
|
f.action(id, 'refund', { date: refund, amount: '40', account: '助理支付宝', verified: true });
|
|
|
|
|
const originalRefund = f.ledger
|
|
|
|
|
.detail(id)
|
|
|
|
|
.payments.find(payment => payment.kind === 'refund' && payment.account === '助理支付宝');
|
|
|
|
|
f.action(id, 'correct', {
|
|
|
|
|
date: refund,
|
|
|
|
|
type: 'payment',
|
|
|
|
|
target: originalRefund.id,
|
|
|
|
|
amount: '40',
|
|
|
|
|
account: '助理支付宝',
|
|
|
|
|
reason: '退款凭据确认',
|
|
|
|
|
verified: true,
|
|
|
|
|
});
|
|
|
|
|
b = f.book({ date: refund });
|
|
|
|
|
assert.equal(b.receiptsByPaymentDate.receipt, 0);
|
|
|
|
|
assert.equal(b.receiptsByPaymentDate.refund, 10000);
|
|
|
|
|
assert.equal(b.receiptsByPaymentDate.net, -10000);
|
|
|
|
|
const month = f.book({ from: '2026-01-01', date: '2026-01-31' });
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
month.rows.map(row => row.id),
|
|
|
|
|
[cash],
|
|
|
|
|
);
|
|
|
|
|
assert.equal(month.receiptsByPaymentDate.receipt, 9000);
|
|
|
|
|
assert.equal(month.receiptsByPaymentDate.refund, 10000);
|
|
|
|
|
assert.equal(month.receiptsByPaymentDate.net, -1000);
|
2026-10-17 19:50:24 +08:00
|
|
|
});
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('日期、搜索与状态共同约束列表和汇总,空结果不误清除该日实际收款', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
const id = f.save(
|
|
|
|
|
{ customer: '林客户', note: '定制字样', lines: [line({ name: '足金指定款', code: 'ABc-01' })] },
|
|
|
|
|
true,
|
|
|
|
|
);
|
|
|
|
|
f.save({ customer: '其他客户' });
|
|
|
|
|
f.save(
|
|
|
|
|
{
|
|
|
|
|
customer: '别的已完成客户',
|
|
|
|
|
payment_account: '助理现金',
|
|
|
|
|
lines: [line({ sale: '50', costParts: { material: '20', labor: '0', accessories: '0', other: '0' } })],
|
|
|
|
|
},
|
|
|
|
|
true,
|
|
|
|
|
);
|
|
|
|
|
f.action(id, 'settlement', {
|
|
|
|
|
amount: '1000',
|
|
|
|
|
bucket: 'cost',
|
|
|
|
|
direction: 'toOwner',
|
|
|
|
|
note: '合成货款交款',
|
|
|
|
|
verified: true,
|
|
|
|
|
});
|
|
|
|
|
f.action(id, 'settlement', {
|
|
|
|
|
amount: '200',
|
|
|
|
|
bucket: 'profit',
|
|
|
|
|
direction: 'toOwner',
|
|
|
|
|
note: '合成利润交款',
|
|
|
|
|
verified: true,
|
|
|
|
|
});
|
|
|
|
|
for (const q of ['林客户', '定制字样', '足金指定款', 'abc-01', f.ledger.detail(id).paper_number]) {
|
|
|
|
|
const b = f.book({ q, status: 'delivered' });
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
b.rows.map(row => row.id),
|
|
|
|
|
[id],
|
|
|
|
|
);
|
|
|
|
|
assert.equal(b.summary.orderCount, 1);
|
|
|
|
|
assert.equal(b.summary.sale, 120000);
|
|
|
|
|
assert.equal(b.summary.totalCost, 100000);
|
|
|
|
|
assert.equal(b.receiptsByOrderDate.receipt, 120000);
|
2026-10-17 19:50:24 +08:00
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
const empty = f.book({ q: '没有这个客户', status: 'delivered' });
|
|
|
|
|
assert.deepEqual(empty.rows, []);
|
|
|
|
|
assert.equal(empty.summary.orderCount, 0);
|
|
|
|
|
assert.equal(empty.summary.sale, 0);
|
|
|
|
|
assert.equal(empty.summary.totalCost, 0);
|
|
|
|
|
assert.equal(empty.summary.partial, false);
|
|
|
|
|
assert.equal(empty.receiptsByPaymentDate.receipt, 125000);
|
|
|
|
|
assert.equal(empty.receiptsByOrderDate.receipt, 0);
|
|
|
|
|
assert.equal(f.book().receiptsByOrderDate.receipt, 125000, '双方交款不属于客户收款');
|
|
|
|
|
assert.equal(f.book({ status: 'draft' }).summary.draftCount, 1);
|
|
|
|
|
assert.equal(f.book({ status: 'draft' }).summary.sale, 0);
|
|
|
|
|
const defaults = f.ledger.orderBook();
|
|
|
|
|
assert.equal(defaults.date, today());
|
|
|
|
|
assert.equal(defaults.from, today());
|
|
|
|
|
for (const query of [
|
|
|
|
|
{ date: '2026-02-30' },
|
|
|
|
|
{ date: '2026-2-03' },
|
|
|
|
|
{ date: '2025-02-29' },
|
|
|
|
|
{ date: '1999-12-31' },
|
|
|
|
|
{ date: '2099-01-01' },
|
|
|
|
|
{ date: '' },
|
|
|
|
|
{ from: D2, date: D },
|
|
|
|
|
{ status: 'made-up' },
|
|
|
|
|
{ q: '字'.repeat(101) },
|
|
|
|
|
])
|
|
|
|
|
assert.throws(() => f.ledger.orderBook(query));
|
|
|
|
|
assert.doesNotThrow(() => f.book({ date: '2024-02-29' }));
|
|
|
|
|
assert.doesNotThrow(() => f.book({ from: '2025-12-31', date: '2026-01-01' }));
|
2026-10-17 19:50:24 +08:00
|
|
|
});
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('货盘类型按实际货盘去重,旧分精度混合商品的毫克和件数分别加总', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
for (const pocket of ['现货', '金料'])
|
|
|
|
|
f.run({ pocket }, () => f.ledger.stock({ kind: 'opening', date: D, pocket, grams: '10', note: '合成库存' }, actor));
|
|
|
|
|
f.save(
|
|
|
|
|
{
|
|
|
|
|
rounding_mode: 'cent',
|
|
|
|
|
lines: [
|
|
|
|
|
line({
|
|
|
|
|
quantity: '0.001',
|
|
|
|
|
pocket: '现货',
|
|
|
|
|
sale: '0.01',
|
|
|
|
|
costParts: { material: '0', labor: '0', accessories: '0', other: '0' },
|
|
|
|
|
}),
|
|
|
|
|
line({ quantity: '1.001', pocket: '现货' }),
|
|
|
|
|
line({ quantity: '2', pocket: '金料' }),
|
|
|
|
|
line({ unit: '件', quantity: '3', sale: '80' }),
|
|
|
|
|
line({ unit: '件', quantity: '2', sale: '20' }),
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
true,
|
|
|
|
|
);
|
|
|
|
|
const b = f.book(),
|
|
|
|
|
r = b.rows[0];
|
|
|
|
|
assert.deepEqual(r.types, ['货盘现货', '货盘·金料', '配货']);
|
|
|
|
|
assert.equal(r.mg, 3002);
|
|
|
|
|
assert.equal(r.pieces, 5);
|
|
|
|
|
assert.equal(b.summary.mg, 3002);
|
|
|
|
|
assert.equal(b.summary.pieces, 5);
|
|
|
|
|
assert.equal(r.sale, 250001);
|
2026-10-17 19:50:24 +08:00
|
|
|
});
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('反复读取订单簿不改账本、操作号、审计、同步或纸单序号,客户投影不含内部聚合', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
id = f.save({}, true);
|
|
|
|
|
const snapshot = () =>
|
|
|
|
|
JSON.stringify(
|
|
|
|
|
f.ledger
|
|
|
|
|
.rows("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
|
|
|
|
|
.map(({ name }) => [name, f.ledger.rows('SELECT * FROM "' + name + '"')]),
|
|
|
|
|
);
|
|
|
|
|
const before = snapshot(),
|
|
|
|
|
changes = f.ledger.one('SELECT total_changes() AS count').count;
|
|
|
|
|
const first = f.book();
|
|
|
|
|
assert.deepEqual(f.book(), first);
|
|
|
|
|
f.book({ q: '合成' });
|
|
|
|
|
f.book({ from: '2026-09-01', date: D });
|
|
|
|
|
assert.equal(f.ledger.one('SELECT total_changes() AS count').count, changes);
|
|
|
|
|
assert.equal(snapshot(), before);
|
|
|
|
|
const customer = JSON.stringify(f.ledger.publicOrder(id));
|
|
|
|
|
for (const key of [
|
|
|
|
|
'costItems',
|
|
|
|
|
'laborAndFeeItems',
|
|
|
|
|
'ownerCost',
|
|
|
|
|
'assistantCost',
|
|
|
|
|
'profit',
|
|
|
|
|
'payments',
|
|
|
|
|
'expenses',
|
|
|
|
|
'corrections',
|
|
|
|
|
'known',
|
|
|
|
|
])
|
|
|
|
|
assert.ok(!customer.includes('"' + key + '"'));
|
2026-10-17 19:50:24 +08:00
|
|
|
});
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('订单簿 HTTP 只允许已登录读取,助理可用,未来日期拒绝,不改变流水且打印不泄漏', async t => {
|
|
|
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'maimai-order-book-test-'));
|
|
|
|
|
const app = createApp({ mode: 'demo', port: 0, dataDir: dir, ledger: new Ledger(':memory:') });
|
|
|
|
|
await new Promise(resolve => app.server.listen(0, '127.0.0.1', resolve));
|
|
|
|
|
t.after(async () => {
|
|
|
|
|
await new Promise(resolve => {
|
|
|
|
|
app.server.closeAllConnections();
|
|
|
|
|
app.server.close(resolve);
|
|
|
|
|
});
|
|
|
|
|
app.ledger.close();
|
|
|
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
|
|
|
});
|
|
|
|
|
const base = 'http://127.0.0.1:' + app.server.address().port;
|
|
|
|
|
let cookie = '';
|
|
|
|
|
const call = (route, data) =>
|
|
|
|
|
fetch(base + route, {
|
|
|
|
|
method: data ? 'POST' : 'GET',
|
|
|
|
|
headers: {
|
|
|
|
|
...(cookie ? { Cookie: cookie } : {}),
|
|
|
|
|
...(data
|
|
|
|
|
? {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
Origin: base,
|
|
|
|
|
'X-Requested-With': 'maimai-ledger',
|
|
|
|
|
'Idempotency-Key': randomUUID(),
|
|
|
|
|
}
|
|
|
|
|
: {}),
|
|
|
|
|
},
|
|
|
|
|
...(data ? { body: JSON.stringify(data) } : {}),
|
|
|
|
|
});
|
|
|
|
|
assert.equal((await call('/api/order-book')).status, 401);
|
|
|
|
|
assert.equal((await call('/api/order-book?date=2099-01-01')).status, 401);
|
|
|
|
|
const login = await call('/api/demo-login', { role: 'assistant' });
|
|
|
|
|
cookie = login.headers.get('set-cookie').split(';')[0];
|
|
|
|
|
const changes = app.ledger.one('SELECT total_changes() AS count').count;
|
|
|
|
|
const response = await call('/api/order-book'),
|
|
|
|
|
b = await response.json();
|
|
|
|
|
assert.equal(response.status, 200);
|
|
|
|
|
assert.equal(b.summary.completedCount, 1);
|
|
|
|
|
assert.equal(b.summary.totalCost, 100000);
|
|
|
|
|
assert.equal(b.summary.profit, 20000);
|
|
|
|
|
assert.deepEqual(b.summaryWithoutCancelled, b.summary);
|
|
|
|
|
assert.equal(b.receiptsWithoutCancelled.filterScope, 'matchingOrdersWithoutCancelled');
|
|
|
|
|
assert.equal(b.receiptsWithoutCancelled.receipt, b.receiptsByOrderDate.receipt);
|
|
|
|
|
assert.equal((await call('/api/order-book?date=2099-01-01')).status, 400);
|
|
|
|
|
assert.equal((await call('/api/order-book?date=2026-02-30')).status, 400);
|
|
|
|
|
assert.equal((await call('/api/order-book?status=all&q=%E6%BC%94%E7%A4%BA')).status, 200);
|
|
|
|
|
assert.equal((await call('/api/order-book', {})).status, 404);
|
|
|
|
|
assert.equal(app.ledger.one('SELECT total_changes() AS count').count, changes);
|
|
|
|
|
const customer = await (await call('/api/orders/' + b.rows[0].id + '/public')).json();
|
|
|
|
|
assert.ok(!('profit' in customer));
|
|
|
|
|
assert.ok(!('costItems' in customer));
|
|
|
|
|
const printed = await (await call('/print/' + b.rows[0].id)).text();
|
|
|
|
|
assert.ok(!printed.includes('1000.00'));
|
|
|
|
|
assert.ok(!printed.includes('laborAndFeeItems'));
|
2026-10-17 19:50:24 +08:00
|
|
|
});
|