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'),
|
|
|
|
|
|
fs = require('node:fs'),
|
|
|
|
|
|
os = require('node:os'),
|
|
|
|
|
|
path = require('node:path');
|
|
|
|
|
|
const { randomUUID } = require('node:crypto');
|
|
|
|
|
|
const { Ledger } = require('../ledger'),
|
|
|
|
|
|
Calc = require('../public/calculations'),
|
|
|
|
|
|
{ receipt } = require('../printing'),
|
|
|
|
|
|
{ createApp } = require('../server');
|
|
|
|
|
|
const actor = { id: 'entry-owner', name: '测试麦麦', role: 'owner' },
|
|
|
|
|
|
D = '2026-10-16',
|
|
|
|
|
|
D2 = '2026-10-17';
|
|
|
|
|
|
const line = (extra = {}) => ({
|
|
|
|
|
|
name: '足金999',
|
|
|
|
|
|
unit: 'g',
|
|
|
|
|
|
quantity: '2.2',
|
|
|
|
|
|
pocket: '',
|
|
|
|
|
|
payer: 'owner',
|
|
|
|
|
|
pricingMode: 'auto',
|
|
|
|
|
|
goldPrice: '940',
|
|
|
|
|
|
laborRate: '45',
|
|
|
|
|
|
laborMode: 'gram',
|
|
|
|
|
|
pieces: '1',
|
|
|
|
|
|
costParts: { material: '1980', labor: '44', accessories: '0', other: '0' },
|
|
|
|
|
|
...extra,
|
|
|
|
|
|
});
|
|
|
|
|
|
function fixture(t) {
|
|
|
|
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'maimai-entry-'));
|
|
|
|
|
|
const file = path.join(dir, 'ledger.sqlite');
|
|
|
|
|
|
const l = new Ledger(file);
|
|
|
|
|
|
t.after(() => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
l.close();
|
|
|
|
|
|
} catch {}
|
|
|
|
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
|
|
|
|
});
|
|
|
|
|
|
const run = (p, fn, key = randomUUID()) => l.transact(key, actor, p, fn);
|
|
|
|
|
|
const save = (extra = {}) => {
|
|
|
|
|
|
const p = {
|
|
|
|
|
|
date: D,
|
|
|
|
|
|
customer: '合成客户',
|
|
|
|
|
|
internal_note: '测试内部备注',
|
|
|
|
|
|
salesperson: '合成开单人',
|
|
|
|
|
|
lines: [line()],
|
|
|
|
|
|
discount: '0',
|
|
|
|
|
|
...extra,
|
|
|
|
|
|
};
|
|
|
|
|
|
return run(p, () => l.saveOrder(p, actor)).id;
|
|
|
|
|
|
};
|
|
|
|
|
|
const act = (id, a, p = {}) => run({ id, a, p }, () => historicalAction(l, id, a, { date: D, ...p }, actor));
|
|
|
|
|
|
return { dir, file, l, run, save, act };
|
|
|
|
|
|
}
|
|
|
|
|
|
test('克工费与件工费:用户给定案例、整数分舍入、件数及非法输入', () => {
|
|
|
|
|
|
assert.equal(Calc.calculate(line()).sale, 216700);
|
|
|
|
|
|
assert.equal(Calc.calculate(line({ laborMode: 'piece' })).sale, 211300);
|
|
|
|
|
|
assert.equal(Calc.calculate(line({ laborMode: 'piece', pieces: '2' })).sale, 215800);
|
|
|
|
|
|
assert.equal(Calc.calculate(line({ quantity: '0.001', goldPrice: '1', laborRate: '4' })).sale, 1);
|
|
|
|
|
|
assert.equal(Calc.calculate(line({ quantity: '0.001', goldPrice: '1', laborRate: '3' })).sale, 0);
|
|
|
|
|
|
for (const change of [
|
|
|
|
|
|
{ goldPrice: '-1' },
|
|
|
|
|
|
{ quantity: '2.2001' },
|
|
|
|
|
|
{ laborRate: '' },
|
|
|
|
|
|
{ laborMode: 'other' },
|
|
|
|
|
|
{ laborMode: 'piece', pieces: '0' },
|
|
|
|
|
|
{ laborMode: 'piece', pieces: '1.2' },
|
|
|
|
|
|
{ unit: '件' },
|
|
|
|
|
|
])
|
|
|
|
|
|
assert.throws(() => Calc.calculate(line(change)));
|
|
|
|
|
|
});
|
|
|
|
|
|
test('附加项目选填、逐项按分相加,支持两种工费与直接报价', () => {
|
|
|
|
|
|
const charges = [
|
|
|
|
|
|
{ name: '编绳', amount: '10' },
|
|
|
|
|
|
{ name: '证书', amount: '5.01' },
|
|
|
|
|
|
{ name: '包装', amount: '' },
|
|
|
|
|
|
{ name: '附加费', amount: '0' },
|
|
|
|
|
|
];
|
|
|
|
|
|
assert.equal(Calc.sale(line({ charges })).sale, 218201);
|
|
|
|
|
|
assert.equal(Calc.sale(line({ laborMode: 'piece', charges })).sale, 212801);
|
|
|
|
|
|
const manual = Calc.sale({ pricingMode: 'manual', baseSale: '100', sale: '999', charges });
|
|
|
|
|
|
assert.equal(manual.baseSale, 10000);
|
|
|
|
|
|
assert.equal(manual.sale, 11501);
|
|
|
|
|
|
assert.equal(manual.charges.length, 3);
|
|
|
|
|
|
assert.equal(Calc.sale(line({ charges: [] })).sale, 216700);
|
|
|
|
|
|
for (const raw of [
|
|
|
|
|
|
[{ name: '编绳', amount: '-1' }],
|
|
|
|
|
|
[{ name: '', amount: '1' }],
|
|
|
|
|
|
[{ name: '编绳', amount: '0.001' }],
|
|
|
|
|
|
{},
|
|
|
|
|
|
])
|
|
|
|
|
|
assert.throws(() => Calc.sale(line({ charges: raw })));
|
|
|
|
|
|
});
|
|
|
|
|
|
test('附加收费进入销售和收款,内部成本独立;编辑不重复加,退款按含附加总额', t => {
|
|
|
|
|
|
const { l, save, act } = fixture(t),
|
|
|
|
|
|
charges = [
|
|
|
|
|
|
{ name: '编绳', amount: '10' },
|
|
|
|
|
|
{ name: '证书', amount: '5' },
|
|
|
|
|
|
];
|
|
|
|
|
|
const input = line({ charges, sale: '0.01' }),
|
|
|
|
|
|
id = save({ lines: [input] });
|
|
|
|
|
|
assert.equal(l.detail(id).total, 218200);
|
|
|
|
|
|
assert.equal(l.detail(id).goodsCost, 202400);
|
|
|
|
|
|
assert.equal(l.detail(id).expenses.length, 0);
|
|
|
|
|
|
save({ id, revision: l.detail(id).revision, lines: [input] });
|
|
|
|
|
|
assert.equal(l.detail(id).total, 218200);
|
|
|
|
|
|
act(id, 'confirm');
|
|
|
|
|
|
act(id, 'expense', { amount: '3', payer: 'assistant', note: '实际编绳成本' });
|
|
|
|
|
|
act(id, 'receipt', { amount: '2182', account: '助理微信', verified: true });
|
|
|
|
|
|
act(id, 'deliver', { verified: true });
|
|
|
|
|
|
assert.equal(l.detail(id).profit, 15500);
|
|
|
|
|
|
act(id, 'return', { date: D2, reason: '含附加项目整单退回', verified: true });
|
|
|
|
|
|
assert.equal(l.detail(id).balance, -218200);
|
|
|
|
|
|
assert.equal(l.detail(id).profit, -300);
|
|
|
|
|
|
act(id, 'refund', { date: D2, amount: '2182', account: '助理微信', verified: true });
|
|
|
|
|
|
assert.equal(l.detail(id).balance, 0);
|
|
|
|
|
|
});
|
|
|
|
|
|
test('附加收费可删除、修改及清空;手填金额重复保存不翻倍', t => {
|
|
|
|
|
|
const { l, save } = fixture(t);
|
|
|
|
|
|
const base = {
|
|
|
|
|
|
name: '其他商品',
|
|
|
|
|
|
unit: '件',
|
|
|
|
|
|
quantity: '1',
|
|
|
|
|
|
pocket: '',
|
|
|
|
|
|
payer: 'owner',
|
|
|
|
|
|
cost: '50',
|
|
|
|
|
|
pricingMode: 'manual',
|
|
|
|
|
|
baseSale: '100',
|
|
|
|
|
|
charges: [
|
|
|
|
|
|
{ name: '包装', amount: '5' },
|
|
|
|
|
|
{ name: '附加费', amount: '10' },
|
|
|
|
|
|
],
|
|
|
|
|
|
};
|
|
|
|
|
|
const id = save({ lines: [base] });
|
|
|
|
|
|
assert.equal(l.detail(id).total, 11500);
|
|
|
|
|
|
save({ id, revision: l.detail(id).revision, lines: [{ ...base, charges: [{ name: '包装', amount: '3' }] }] });
|
|
|
|
|
|
assert.equal(l.detail(id).total, 10300);
|
|
|
|
|
|
save({ id, revision: l.detail(id).revision, lines: [{ ...base, charges: [] }] });
|
|
|
|
|
|
assert.equal(l.detail(id).total, 10000);
|
|
|
|
|
|
});
|
|
|
|
|
|
test('旧分精度订单的多个附加项目完整保留在一页,分项金额与整单一致且名称转义', t => {
|
|
|
|
|
|
const { l, save } = fixture(t);
|
|
|
|
|
|
const charges = Array.from({ length: 21 }, (_, i) => ({ name: '项目' + i, amount: '1.01' }));
|
|
|
|
|
|
charges[0].name = '<img src=x onerror=evil()>';
|
|
|
|
|
|
const id = save({ rounding_mode: 'cent', lines: [line({ charges })] });
|
|
|
|
|
|
const o = l.publicOrder(id),
|
|
|
|
|
|
html = receipt(o);
|
|
|
|
|
|
assert.equal(o.total, 218821);
|
|
|
|
|
|
assert.equal(o.lines[0].charges.length, 21);
|
|
|
|
|
|
assert.ok(!JSON.stringify(o).includes('costParts'));
|
|
|
|
|
|
assert.ok(!html.includes('<img src=x'));
|
|
|
|
|
|
assert.ok(html.includes('<img'));
|
|
|
|
|
|
assert.ok(html.includes('附加:项目20'));
|
|
|
|
|
|
assert.equal((html.match(/<section class="sheet /g) || []).length, 1);
|
|
|
|
|
|
const rowAmounts = [...html.matchAll(/<td>(\d+\.\d{2})<\/td><\/tr>/g)].map(m => Math.round(Number(m[1]) * 100));
|
|
|
|
|
|
assert.equal(rowAmounts.length, 22);
|
|
|
|
|
|
assert.equal(
|
|
|
|
|
|
rowAmounts.reduce((a, b) => a + b, 0),
|
|
|
|
|
|
o.total,
|
|
|
|
|
|
);
|
|
|
|
|
|
assert.equal((html.match(/合计金额/g) || []).length, 1);
|
|
|
|
|
|
assert.ok(html.includes('贰仟壹佰捌拾捌元贰角壹分'));
|
|
|
|
|
|
});
|
|
|
|
|
|
test('新纸单号不带横线,旧有横线号仍计入累计且草稿改号不新增序号', t => {
|
|
|
|
|
|
const { l, save } = fixture(t);
|
|
|
|
|
|
const id = save();
|
|
|
|
|
|
l.run('UPDATE orders SET paper_number=? WHERE id=?', '20261016-008', id);
|
|
|
|
|
|
assert.equal(l.paperNumber(D2), '20261017009');
|
|
|
|
|
|
assert.equal(l.detail(id).paper_number, '20261016-008');
|
|
|
|
|
|
save({ id, revision: l.detail(id).revision, date: D2 });
|
|
|
|
|
|
assert.equal(l.detail(id).paper_number, '20261017008');
|
|
|
|
|
|
assert.equal(l.paperNumber(D), '20261016009');
|
|
|
|
|
|
});
|
|
|
|
|
|
test('服务器重新计算销售额和成本,忽略伪造总额,成本空项阻止交付', t => {
|
|
|
|
|
|
const { l, save, act } = fixture(t);
|
|
|
|
|
|
const id = save({
|
|
|
|
|
|
lines: [line({ sale: '1', cost: '1', costParts: { material: '1980', labor: '', accessories: '0', other: '0' } })],
|
|
|
|
|
|
});
|
|
|
|
|
|
assert.equal(l.detail(id).total, 216700);
|
|
|
|
|
|
assert.equal(l.detail(id).goodsCost, null);
|
|
|
|
|
|
act(id, 'confirm');
|
|
|
|
|
|
act(id, 'receipt', { amount: '2167', account: '助理支付宝', verified: true });
|
|
|
|
|
|
assert.throws(() => act(id, 'deliver', { verified: true }), /补齐/);
|
|
|
|
|
|
assert.throws(() => act(id, 'cost', { costs: ['2024'] }), /逐项/);
|
|
|
|
|
|
act(id, 'cost', { costs: [{ material: '1980', labor: '44', accessories: '0', other: '0' }] });
|
|
|
|
|
|
act(id, 'deliver', { verified: true });
|
|
|
|
|
|
assert.equal(l.detail(id).profit, 14300);
|
|
|
|
|
|
});
|
|
|
|
|
|
test('纸单跨日持续累计、预览不占号、取消不复用、改日期保留原序号', t => {
|
|
|
|
|
|
const { l, save, act } = fixture(t);
|
|
|
|
|
|
assert.equal(l.paperNumber(D), '20261016001');
|
|
|
|
|
|
assert.equal(l.paperNumber(D), '20261016001');
|
|
|
|
|
|
const a = save();
|
|
|
|
|
|
assert.equal(l.detail(a).paper_number, '20261016001');
|
|
|
|
|
|
save({ id: a, revision: l.detail(a).revision });
|
|
|
|
|
|
assert.equal(l.detail(a).paper_number, '20261016001');
|
|
|
|
|
|
act(a, 'cancel', { reason: '合成测试' });
|
|
|
|
|
|
const b = save();
|
|
|
|
|
|
assert.equal(l.detail(b).paper_number, '20261016002');
|
|
|
|
|
|
const c = save({ date: D2 });
|
|
|
|
|
|
assert.equal(l.detail(c).paper_number, '20261017003');
|
|
|
|
|
|
save({ id: b, revision: l.detail(b).revision, date: D2 });
|
|
|
|
|
|
assert.equal(l.detail(b).paper_number, '20261017002');
|
|
|
|
|
|
assert.equal(l.paperNumber(D), '20261016004');
|
|
|
|
|
|
assert.throws(() => save({ lines: [line({ goldPrice: 'bad' })] }));
|
|
|
|
|
|
assert.equal(l.paperNumber(D), '20261016004');
|
|
|
|
|
|
});
|
|
|
|
|
|
test('重复保存只生成一张单一个号码;重开数据库接续流水号', t => {
|
|
|
|
|
|
const { l, run, file } = fixture(t),
|
|
|
|
|
|
p = {
|
|
|
|
|
|
date: D,
|
|
|
|
|
|
customer: '合成客户',
|
|
|
|
|
|
internal_note: '测试内部备注',
|
|
|
|
|
|
salesperson: '合成开单人',
|
|
|
|
|
|
lines: [line()],
|
|
|
|
|
|
payment_account: '助理支付宝',
|
|
|
|
|
|
},
|
|
|
|
|
|
key = randomUUID();
|
|
|
|
|
|
const a = run(p, () => l.saveOrder(p, actor), key);
|
|
|
|
|
|
const b = run(p, () => l.saveOrder(p, actor), key);
|
|
|
|
|
|
assert.equal(a.id, b.id);
|
|
|
|
|
|
assert.equal(l.rows('SELECT id FROM orders').length, 1);
|
|
|
|
|
|
l.close();
|
|
|
|
|
|
const reopened = new Ledger(file);
|
|
|
|
|
|
assert.equal(reopened.paperNumber(D2), '20261017002');
|
|
|
|
|
|
assert.equal(reopened.detail(a.id).payment_account, '助理支付宝');
|
|
|
|
|
|
reopened.close();
|
|
|
|
|
|
});
|
|
|
|
|
|
test('接续旧版最大序号,跨月跨年不归零,超过999继续累计,历史单号不改写', t => {
|
|
|
|
|
|
const { l, save, act } = fixture(t);
|
|
|
|
|
|
const old = save({ date: '2025-12-31' });
|
|
|
|
|
|
const original = l.detail(old).paper_number;
|
|
|
|
|
|
l.setMeta('paper-seq:2025-12-31', 999);
|
|
|
|
|
|
assert.equal(l.paperNumber('2026-01-01'), '202601011000');
|
|
|
|
|
|
const next = save({ date: '2026-01-01' });
|
|
|
|
|
|
assert.equal(l.detail(next).paper_number, '202601011000');
|
|
|
|
|
|
assert.equal(l.detail(old).paper_number, original);
|
|
|
|
|
|
act(next, 'print');
|
|
|
|
|
|
act(next, 'print');
|
|
|
|
|
|
assert.equal(l.paperNumber('2026-02-01'), '202602011001');
|
|
|
|
|
|
const nextMonth = save({ date: '2026-02-01' });
|
|
|
|
|
|
assert.equal(l.detail(nextMonth).paper_number, '202602011001');
|
|
|
|
|
|
});
|
|
|
|
|
|
test('快递包装按整单登记一次;各付款人分开;退货保留实际已付费用', t => {
|
|
|
|
|
|
const { l, save, act, run } = fixture(t);
|
|
|
|
|
|
const id = save({
|
|
|
|
|
|
payment_account: '助理支付宝',
|
|
|
|
|
|
fees_date: D,
|
|
|
|
|
|
fee_items: [
|
|
|
|
|
|
{ key: 'shipping', amount: '8', payer: 'assistant' },
|
|
|
|
|
|
{ key: 'box', amount: '3', payer: 'owner' },
|
|
|
|
|
|
],
|
|
|
|
|
|
});
|
|
|
|
|
|
assert.equal(l.detail(id).draftFees, 1100);
|
|
|
|
|
|
assert.equal(l.detail(id).estimatedProfit, 13200);
|
|
|
|
|
|
assert.equal(l.detail(id).expenses.length, 0);
|
|
|
|
|
|
assert.throws(() => act(id, 'confirm'), /实际支付/);
|
|
|
|
|
|
const p = { date: D, feesVerified: true },
|
|
|
|
|
|
key = randomUUID();
|
|
|
|
|
|
run(p, () => historicalAction(l, id, 'confirm', p, actor), key);
|
|
|
|
|
|
run(p, () => historicalAction(l, id, 'confirm', p, actor), key);
|
|
|
|
|
|
assert.equal(l.detail(id).expenses.length, 2);
|
|
|
|
|
|
assert.equal(l.detail(id).draftFees, 0);
|
|
|
|
|
|
assert.equal(l.detail(id).directCost, 1100);
|
|
|
|
|
|
act(id, 'receipt', { amount: '2167', account: '助理支付宝', verified: true });
|
|
|
|
|
|
act(id, 'deliver', { verified: true });
|
|
|
|
|
|
assert.equal(l.detail(id).profit, 13200);
|
|
|
|
|
|
assert.equal(l.detail(id).ownerCost, 202700);
|
|
|
|
|
|
act(id, 'return', { date: D2, reason: '合成退货', verified: true });
|
|
|
|
|
|
assert.equal(l.detail(id).profit, -1100);
|
|
|
|
|
|
assert.equal(l.report(D2).profit, -14300);
|
|
|
|
|
|
});
|
|
|
|
|
|
test('取消未确认草稿不把暂填费用记成已支出;费用重复及日期错误被拒绝', t => {
|
|
|
|
|
|
const { l, save, act } = fixture(t);
|
|
|
|
|
|
const fees = [{ key: 'shipping', amount: '8', payer: 'assistant' }];
|
|
|
|
|
|
const id = save({ fee_items: fees });
|
|
|
|
|
|
act(id, 'cancel', { reason: '测试取消' });
|
|
|
|
|
|
assert.equal(l.detail(id).profit, 0);
|
|
|
|
|
|
assert.equal(l.detail(id).expenses.length, 0);
|
|
|
|
|
|
assert.throws(() => save({ fee_items: [...fees, ...fees] }), /重复/);
|
|
|
|
|
|
assert.throws(() => save({ fee_items: fees, fees_date: '2026-10-15' }), /业务日期/);
|
|
|
|
|
|
});
|
|
|
|
|
|
test('银饰不会扣黄金库存,其他商品可手填名称并按件报价', t => {
|
|
|
|
|
|
const { l, save } = fixture(t);
|
|
|
|
|
|
assert.throws(() => save({ lines: [line({ name: '足银999', pocket: '现货' })] }), /银饰/);
|
|
|
|
|
|
const id = save({
|
|
|
|
|
|
lines: [
|
|
|
|
|
|
line({ name: '纯银925' }),
|
|
|
|
|
|
line({
|
|
|
|
|
|
name: '定制吊坠',
|
|
|
|
|
|
pricingMode: 'manual',
|
|
|
|
|
|
unit: '件',
|
|
|
|
|
|
quantity: '1',
|
|
|
|
|
|
sale: '500',
|
|
|
|
|
|
priceNote: '定制一口价',
|
|
|
|
|
|
}),
|
|
|
|
|
|
],
|
|
|
|
|
|
});
|
|
|
|
|
|
assert.equal(l.detail(id).lines[1].sale, 50000);
|
|
|
|
|
|
});
|
|
|
|
|
|
test('打印独立显示金价、工费、纸单号和实际付款方式,内部费用不泄露', t => {
|
|
|
|
|
|
const { l, save, act } = fixture(t);
|
|
|
|
|
|
const id = save({
|
|
|
|
|
|
lines: [
|
|
|
|
|
|
line({ laborMode: 'piece', costParts: { material: '1876.54', labor: '32.19', accessories: '6.78', other: '0' } }),
|
|
|
|
|
|
],
|
|
|
|
|
|
fee_items: [{ key: 'other', amount: '9.87', payer: 'assistant', note: '内部供应商费用' }],
|
|
|
|
|
|
});
|
|
|
|
|
|
act(id, 'confirm', { feesVerified: true });
|
|
|
|
|
|
act(id, 'receipt', { amount: '100', account: '助理支付宝', verified: true });
|
|
|
|
|
|
const p = l.publicOrder(id),
|
|
|
|
|
|
html = receipt(p),
|
|
|
|
|
|
json = JSON.stringify(p);
|
|
|
|
|
|
for (const k of ['costParts', 'fee_items', 'payment_account', 'payer', 'profit'])
|
|
|
|
|
|
assert.ok(!json.includes('"' + k + '"'));
|
|
|
|
|
|
for (const value of ['1876.54', '32.19', '6.78', '9.87', '内部供应商']) assert.ok(!html.includes(value));
|
|
|
|
|
|
for (const value of ['20261016001', '940.00元/克', '45.00元/件 × 1件', '销售工费', '支付宝'])
|
|
|
|
|
|
assert.ok(html.includes(value));
|
|
|
|
|
|
});
|
|
|
|
|
|
test('打印沿用开单收款方式,不虚增流水;实际混合收款及更正以有效账户显示', t => {
|
|
|
|
|
|
const { l, save, act } = fixture(t),
|
|
|
|
|
|
id = save({ payment_account: '助理微信' });
|
|
|
|
|
|
for (const confirmed of [false, true]) {
|
|
|
|
|
|
if (confirmed) act(id, 'confirm');
|
|
|
|
|
|
const before = l.detail(id),
|
|
|
|
|
|
p = l.publicOrder(id),
|
|
|
|
|
|
html = receipt(p);
|
|
|
|
|
|
assert.deepEqual(p.paymentMethods, ['微信']);
|
|
|
|
|
|
assert.ok(html.includes('收款方式:微信'));
|
|
|
|
|
|
assert.ok(!html.includes('收款方式:尚未登记'));
|
|
|
|
|
|
assert.ok(!html.includes('助理微信'));
|
|
|
|
|
|
assert.deepEqual(l.detail(id), before);
|
|
|
|
|
|
assert.equal(p.received, 0);
|
2026-10-17 19:50:24 +08:00
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
act(id, 'receipt', { amount: '300', account: '助理支付宝', verified: true });
|
|
|
|
|
|
assert.deepEqual(l.publicOrder(id).paymentMethods, ['支付宝']);
|
|
|
|
|
|
act(id, 'receipt', { amount: '100', account: '麦麦微信', verified: true });
|
|
|
|
|
|
assert.deepEqual(new Set(l.publicOrder(id).paymentMethods), new Set(['支付宝', '微信']));
|
|
|
|
|
|
const old = l.detail(id).payments.find(p => p.account === '助理支付宝');
|
|
|
|
|
|
act(id, 'correct', {
|
|
|
|
|
|
type: 'payment',
|
|
|
|
|
|
target: old.id,
|
|
|
|
|
|
amount: '300',
|
|
|
|
|
|
account: '助理现金',
|
|
|
|
|
|
reason: '测试更正收款渠道',
|
|
|
|
|
|
verified: true,
|
|
|
|
|
|
});
|
|
|
|
|
|
assert.deepEqual(new Set(l.publicOrder(id).paymentMethods), new Set(['现金', '微信']));
|
|
|
|
|
|
assert.equal(l.detail(id).received, 40000);
|
|
|
|
|
|
});
|
|
|
|
|
|
test('旧版库升级保留历史金额和纸单;旧草稿编辑保留原总成本', t => {
|
|
|
|
|
|
const { l, save, file } = fixture(t);
|
|
|
|
|
|
const id = save({
|
|
|
|
|
|
lines: [{ name: '旧版', unit: 'g', quantity: '1', pocket: '', payer: 'owner', cost: '900', sale: '1000' }],
|
|
|
|
|
|
});
|
|
|
|
|
|
l.run('UPDATE orders SET paper_number=? WHERE id=?', '原纸号-88', id);
|
|
|
|
|
|
const before = l.rawOrder(id);
|
|
|
|
|
|
for (const name of ['paper_date', 'payment_account', 'fee_items', 'fees_date'])
|
|
|
|
|
|
l.db.exec('ALTER TABLE orders DROP COLUMN ' + name);
|
|
|
|
|
|
l.close();
|
|
|
|
|
|
const upgraded = new Ledger(file);
|
|
|
|
|
|
const after = upgraded.rawOrder(id);
|
|
|
|
|
|
assert.equal(after.paper_number, before.paper_number);
|
|
|
|
|
|
assert.deepEqual(after.lines, before.lines);
|
|
|
|
|
|
assert.equal(after.total, before.total);
|
|
|
|
|
|
assert.equal(after.fee_items.length, 0);
|
|
|
|
|
|
upgraded.close();
|
|
|
|
|
|
});
|
|
|
|
|
|
test('HTTP并行开单编号不同,同操作重试同单号;端点预览不占号', async t => {
|
|
|
|
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'maimai-entry-http-'));
|
|
|
|
|
|
const app = createApp({ mode: 'demo', dataDir: dir });
|
|
|
|
|
|
await new Promise(r => app.server.listen(0, '127.0.0.1', r));
|
|
|
|
|
|
t.after(async () => {
|
|
|
|
|
|
await new Promise(r => {
|
|
|
|
|
|
app.server.closeAllConnections();
|
|
|
|
|
|
app.server.close(r);
|
|
|
|
|
|
});
|
|
|
|
|
|
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, key = randomUUID()) =>
|
|
|
|
|
|
fetch(base + route, {
|
|
|
|
|
|
method: data ? 'POST' : 'GET',
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
Cookie: cookie,
|
|
|
|
|
|
...(data
|
|
|
|
|
|
? {
|
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
|
Origin: base,
|
|
|
|
|
|
'X-Requested-With': 'maimai-ledger',
|
|
|
|
|
|
'Idempotency-Key': key,
|
|
|
|
|
|
}
|
|
|
|
|
|
: {}),
|
|
|
|
|
|
},
|
|
|
|
|
|
...(data ? { body: JSON.stringify(data) } : {}),
|
|
|
|
|
|
});
|
|
|
|
|
|
assert.equal((await call('/api/paper-number')).status, 401);
|
|
|
|
|
|
cookie = (await call('/api/demo-login', { role: 'owner' })).headers.get('set-cookie').split(';')[0];
|
|
|
|
|
|
const p = {
|
|
|
|
|
|
date: D,
|
|
|
|
|
|
customer: '合成客户',
|
|
|
|
|
|
internal_note: '测试内部备注',
|
|
|
|
|
|
salesperson: '合成开单人',
|
|
|
|
|
|
lines: [line()],
|
|
|
|
|
|
},
|
|
|
|
|
|
key = randomUUID();
|
|
|
|
|
|
const responses = await Promise.all([
|
|
|
|
|
|
call('/api/orders', p, key),
|
|
|
|
|
|
call('/api/orders', p, key),
|
|
|
|
|
|
call('/api/orders', p),
|
|
|
|
|
|
]);
|
|
|
|
|
|
assert.ok(responses.every(r => r.status === 200));
|
|
|
|
|
|
const ids = await Promise.all(responses.map(r => r.json()));
|
|
|
|
|
|
assert.equal(ids[0].id, ids[1].id);
|
|
|
|
|
|
assert.notEqual(ids[0].id, ids[2].id);
|
|
|
|
|
|
assert.notEqual(app.ledger.detail(ids[0].id).paper_number, app.ledger.detail(ids[2].id).paper_number);
|
|
|
|
|
|
const preview = await (await call('/api/paper-number?date=' + D)).json();
|
|
|
|
|
|
assert.equal(preview.number, app.ledger.paperNumber(D));
|
|
|
|
|
|
assert.equal((await call('/calculations.js')).status, 200);
|
2026-10-17 19:50:24 +08:00
|
|
|
|
});
|