2026-10-17 19:50:24 +08:00
|
|
|
'use strict';
|
2026-12-04 08:58:53 -08:00
|
|
|
const { test } = require('node:test'),
|
|
|
|
|
assert = require('node:assert/strict'),
|
|
|
|
|
{ randomUUID } = require('node:crypto');
|
|
|
|
|
const fs = require('node:fs'),
|
|
|
|
|
os = require('node:os'),
|
|
|
|
|
path = require('node:path');
|
|
|
|
|
const { Ledger } = require('../ledger'),
|
|
|
|
|
{ createApp } = require('../server'),
|
|
|
|
|
{ receipt } = require('../printing');
|
|
|
|
|
const actor = { id: 'paid-test', name: '测试助理', role: 'assistant' },
|
|
|
|
|
owner = { id: 'paid-owner', name: '测试麦麦', role: 'owner' },
|
|
|
|
|
date = '2026-10-17';
|
|
|
|
|
const input = (extra = {}) => ({
|
|
|
|
|
date,
|
|
|
|
|
customer: '合成客户',
|
|
|
|
|
internal_note: '测试内部备注',
|
|
|
|
|
salesperson: '合成开单人',
|
|
|
|
|
discount: '7',
|
|
|
|
|
payment_account: '助理支付宝',
|
|
|
|
|
lines: [
|
|
|
|
|
{
|
|
|
|
|
name: '足金999',
|
|
|
|
|
unit: 'g',
|
|
|
|
|
quantity: '2.2',
|
|
|
|
|
pocket: '现货',
|
|
|
|
|
payer: 'owner',
|
|
|
|
|
pricingMode: 'auto',
|
|
|
|
|
goldPrice: '940',
|
|
|
|
|
laborMode: 'gram',
|
|
|
|
|
laborRate: '45',
|
|
|
|
|
costParts: { material: '1980', labor: '999', accessories: '0', other: '0' },
|
|
|
|
|
costLabor: { mode: 'gram', rate: '20' },
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
fee_items: [{ key: 'shipping', amount: '6', payer: 'assistant', status: 'estimated' }],
|
|
|
|
|
...extra,
|
|
|
|
|
});
|
|
|
|
|
const snapshot = l =>
|
|
|
|
|
Object.fromEntries(
|
|
|
|
|
['orders', 'payments', 'expenses', 'movements', 'journal', 'audit', 'operations', 'meta', 'sync'].map(table => [
|
|
|
|
|
table,
|
|
|
|
|
l.rows('SELECT * FROM ' + table),
|
|
|
|
|
]),
|
|
|
|
|
);
|
|
|
|
|
function fixture(t) {
|
|
|
|
|
const l = new Ledger(':memory:');
|
|
|
|
|
t.after(() => l.close());
|
|
|
|
|
const run = (p, fn, key = randomUUID()) => l.transact(key, actor, p, fn);
|
|
|
|
|
run({ stock: true }, () => l.stock({ kind: 'opening', date, pocket: '现货', grams: '10', note: '测试库存' }, owner));
|
|
|
|
|
const issue = (p = input(), key) => run(p, () => l.issueOrder(p, actor), key).id;
|
|
|
|
|
const save = p => run(p, () => l.saveOrder(p, actor)).id;
|
|
|
|
|
const act = (id, a, p = {}, as = owner) =>
|
|
|
|
|
l.transact(randomUUID(), as, { id, a, p }, () => l.action(id, a, { date, ...p }, as));
|
|
|
|
|
return { l, run, issue, save, act };
|
2026-10-17 19:50:24 +08:00
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
test('一步开单同时收全款、核实费用和实际交付,确认利润且直接扣库存', t => {
|
|
|
|
|
const { l, issue, act } = fixture(t),
|
|
|
|
|
id = issue(),
|
|
|
|
|
o = l.detail(id);
|
|
|
|
|
assert.equal(o.status, 'delivered');
|
|
|
|
|
assert.equal(o.delivered_date, date);
|
|
|
|
|
assert.equal(o.total, 216000);
|
|
|
|
|
assert.equal(o.balance, 0);
|
|
|
|
|
assert.equal(o.payments.length, 1);
|
|
|
|
|
assert.equal(o.payments[0].amount, 216000);
|
|
|
|
|
assert.equal(o.payments[0].account, '助理支付宝');
|
|
|
|
|
assert.equal(o.payments[0].date, date);
|
|
|
|
|
assert.equal(o.goodsCost, 202400);
|
|
|
|
|
assert.equal(o.draftFees, 0);
|
|
|
|
|
assert.equal(o.pendingFees.length, 0);
|
|
|
|
|
assert.equal(o.estimatedProfit, 13000);
|
|
|
|
|
assert.equal(o.expenses.length, 1);
|
|
|
|
|
assert.equal(o.expenses[0].amount, 600);
|
|
|
|
|
assert.equal(o.fee_items[0].status, 'recorded');
|
|
|
|
|
assert.equal(o.profit, 13000);
|
|
|
|
|
assert.equal(o.ownerCost, 202400);
|
|
|
|
|
assert.equal(o.settlementDue, 215400);
|
|
|
|
|
const inv = l.inventory().find(r => r.pocket === '现货');
|
|
|
|
|
assert.equal(inv.mg, 7800);
|
|
|
|
|
assert.equal(inv.reserved, 0);
|
|
|
|
|
assert.equal(inv.available, 7800);
|
|
|
|
|
const report = l.report(date);
|
|
|
|
|
assert.equal(report.sales, 216000);
|
|
|
|
|
assert.equal(report.expense, 600);
|
|
|
|
|
assert.equal(report.profit, 13000);
|
|
|
|
|
assert.equal(report.delivered, 1);
|
|
|
|
|
assert.throws(() => act(id, 'receipt', { amount: '2160', account: '助理支付宝', verified: true }), /待交付/);
|
|
|
|
|
assert.throws(() => act(id, 'deliver', { verified: true }), /待交付/);
|
|
|
|
|
assert.throws(() => act(id, 'fees', { revision: o.revision, verified: true, fee_items: [] }), /没有待核实/);
|
|
|
|
|
const html = receipt(l.publicOrder(id));
|
|
|
|
|
assert.ok(!html.includes('进货'));
|
|
|
|
|
assert.ok(!html.includes('预计订单利润'));
|
|
|
|
|
assert.ok(html.includes('支付宝'));
|
2026-10-17 19:50:24 +08:00
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
test('重复请求只开一单、收款和出库各一次,库存不足时单号与全部流水回滚', t => {
|
|
|
|
|
const { l, issue } = fixture(t),
|
|
|
|
|
p = input(),
|
|
|
|
|
key = randomUUID(),
|
|
|
|
|
id = issue(p, key);
|
|
|
|
|
assert.equal(issue(p, key), id);
|
|
|
|
|
assert.equal(l.rows('SELECT * FROM orders').length, 1);
|
|
|
|
|
assert.equal(l.rows('SELECT * FROM payments').length, 1);
|
|
|
|
|
assert.equal(l.rows('SELECT * FROM expenses').length, 1);
|
|
|
|
|
assert.equal(l.rows("SELECT * FROM movements WHERE kind='sale'").length, 1);
|
|
|
|
|
assert.equal(l.rows('SELECT * FROM journal').length, 1);
|
|
|
|
|
const before = snapshot(l),
|
|
|
|
|
number = l.paperNumber(date),
|
|
|
|
|
tooMuch = input({ lines: [{ ...p.lines[0], quantity: '20' }] });
|
|
|
|
|
assert.throws(() => issue(tooMuch), /库存不足/);
|
|
|
|
|
assert.deepEqual(snapshot(l), before);
|
|
|
|
|
assert.equal(l.paperNumber(date), number);
|
|
|
|
|
const existing = { ...p, id, revision: l.detail(id).revision };
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => issue(existing),
|
|
|
|
|
error => error.status === 403,
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(snapshot(l), before);
|
|
|
|
|
assert.throws(() => l.transact(randomUUID(), owner, existing, () => l.issueOrder(existing, owner)), /不能直接改写/);
|
|
|
|
|
assert.deepEqual(snapshot(l), before);
|
2026-10-17 19:50:24 +08:00
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
test('货品成本、克工费和附加成本缺项均不生成单据,未提交的附加成本不能当0', t => {
|
|
|
|
|
const { l, issue } = fixture(t),
|
|
|
|
|
before = snapshot(l),
|
|
|
|
|
number = l.paperNumber(date);
|
|
|
|
|
const missingMaterial = input();
|
|
|
|
|
missingMaterial.lines[0].costParts.material = '';
|
|
|
|
|
const missingLabor = input();
|
|
|
|
|
missingLabor.lines[0].costLabor.rate = '';
|
|
|
|
|
const missingFee = input();
|
|
|
|
|
missingFee.fee_items[0].amount = '';
|
|
|
|
|
const omittedFee = input();
|
|
|
|
|
omittedFee.lines[0].charges = [{ id: randomUUID(), name: '编绳', amount: '10' }];
|
|
|
|
|
for (const [p, error] of [
|
|
|
|
|
[missingMaterial, /商品1的料费成本/],
|
|
|
|
|
[missingLabor, /商品1的进货工费/],
|
|
|
|
|
[missingFee, /附加成本:快递费/],
|
|
|
|
|
[omittedFee, /商品1的编绳/],
|
|
|
|
|
]) {
|
|
|
|
|
assert.throws(() => issue(p), error);
|
|
|
|
|
assert.deepEqual(snapshot(l), before);
|
|
|
|
|
assert.equal(l.paperNumber(date), number);
|
2026-10-17 19:50:24 +08:00
|
|
|
}
|
|
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
test('旧取消入口不能绕过审批,真实退货仍按原账户退款并保留实际费用', t => {
|
|
|
|
|
const { l, issue, act } = fixture(t),
|
|
|
|
|
id = issue();
|
|
|
|
|
assert.throws(() => act(id, 'cancel', { reason: '测试取消' }), /取消申请/);
|
|
|
|
|
act(id, 'return', { reason: '测试退货', verified: true });
|
|
|
|
|
assert.equal(l.detail(id).balance, -216000);
|
|
|
|
|
assert.equal(l.detail(id).profit, -600);
|
|
|
|
|
assert.equal(l.inventory().find(r => r.pocket === '现货').mg, 10000);
|
|
|
|
|
assert.throws(() => act(id, 'refund', { amount: '2160', account: '助理微信', verified: true }), /原收款账户/);
|
|
|
|
|
act(id, 'refund', { amount: '2160', account: '助理支付宝', verified: true });
|
|
|
|
|
assert.equal(l.detail(id).netReceived, 0);
|
|
|
|
|
assert.equal(l.detail(id).expenses.length, 1);
|
2026-10-17 19:50:24 +08:00
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
test('旧草稿由麦麦生成已交付单并沿用纸号,助理不能重开,新开单不自动处理旧定金单', t => {
|
|
|
|
|
const { l, save, issue, act } = fixture(t),
|
|
|
|
|
p = input({ fee_items: [{ key: 'shipping', amount: '6', payer: 'assistant' }] });
|
|
|
|
|
const id = save(p),
|
|
|
|
|
paper = l.detail(id).paper_number;
|
|
|
|
|
const saved = { ...p, id, revision: l.detail(id).revision },
|
|
|
|
|
beforeDraft = snapshot(l);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => issue(saved),
|
|
|
|
|
error => error.status === 403,
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(snapshot(l), beforeDraft);
|
|
|
|
|
l.transact(randomUUID(), owner, saved, () => l.issueOrder(saved, owner));
|
|
|
|
|
assert.equal(l.detail(id).paper_number, paper);
|
|
|
|
|
assert.equal(l.detail(id).status, 'delivered');
|
|
|
|
|
assert.equal(l.detail(id).payments.length, 1);
|
|
|
|
|
assert.equal(l.detail(id).expenses.length, 1);
|
|
|
|
|
const old = save({ ...p, fee_items: [] });
|
|
|
|
|
act(old, 'confirm');
|
|
|
|
|
act(old, 'receipt', { amount: '300', account: '助理微信', verified: true });
|
|
|
|
|
const before = l.detail(old);
|
|
|
|
|
issue();
|
|
|
|
|
assert.deepEqual(l.detail(old), before);
|
|
|
|
|
assert.throws(() => act(old, 'complete', { verified: true }), /未收齐/);
|
|
|
|
|
assert.deepEqual(l.detail(old), before);
|
2026-10-17 19:50:24 +08:00
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
test('旧已付款订单明确完成后才核实费用并交付,同操作号重试和再次完成不重复记账', t => {
|
|
|
|
|
const { l, run, save, act } = fixture(t),
|
|
|
|
|
p = input(),
|
|
|
|
|
id = save(p);
|
|
|
|
|
act(id, 'confirm');
|
|
|
|
|
act(id, 'receipt', { amount: '2160', account: '助理支付宝', verified: true });
|
|
|
|
|
const before = snapshot(l);
|
|
|
|
|
assert.throws(() => act(id, 'complete'), /核实费用/);
|
|
|
|
|
assert.deepEqual(snapshot(l), before);
|
|
|
|
|
assert.throws(() => act(id, 'complete', { revision: l.detail(id).revision - 1, verified: true }), /新变动/);
|
|
|
|
|
assert.deepEqual(snapshot(l), before);
|
|
|
|
|
const data = { date, revision: l.detail(id).revision, verified: true },
|
|
|
|
|
key = randomUUID(),
|
|
|
|
|
complete = () => l.transact(key, owner, data, () => l.action(id, 'complete', data, owner));
|
|
|
|
|
complete();
|
|
|
|
|
complete();
|
|
|
|
|
const o = l.detail(id);
|
|
|
|
|
assert.equal(o.status, 'delivered');
|
|
|
|
|
assert.equal(o.payments.length, 1);
|
|
|
|
|
assert.equal(o.expenses.length, 1);
|
|
|
|
|
assert.equal(o.profit, 13000);
|
|
|
|
|
assert.equal(o.pendingFees.length, 0);
|
|
|
|
|
assert.equal(l.inventory()[0].mg, 7800);
|
|
|
|
|
assert.throws(() => act(id, 'complete', { verified: true }), /只有待交付/);
|
|
|
|
|
assert.equal(l.rows('SELECT * FROM journal').length, 1);
|
2026-10-17 19:50:24 +08:00
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
test('旧单费用已记账或旧版无status时不重复支出,空成本和空费用仍阻止完成', t => {
|
|
|
|
|
const { l, save, act } = fixture(t),
|
|
|
|
|
paid = save(input());
|
|
|
|
|
act(paid, 'confirm', { feesVerified: true });
|
|
|
|
|
act(paid, 'receipt', { amount: '2160', account: '助理支付宝', verified: true });
|
|
|
|
|
const oldFees = l.rawOrder(paid).fee_items.map(({ status, ...f }) => f);
|
|
|
|
|
l.run('UPDATE orders SET fee_items=? WHERE id=?', JSON.stringify(oldFees), paid);
|
|
|
|
|
act(paid, 'complete', { verified: true });
|
|
|
|
|
assert.equal(l.detail(paid).expenses.length, 1);
|
|
|
|
|
assert.equal(l.detail(paid).profit, 13000);
|
|
|
|
|
const p = input();
|
|
|
|
|
p.lines[0].costParts.material = '';
|
|
|
|
|
p.fee_items[0].amount = '';
|
|
|
|
|
const missing = save(p);
|
|
|
|
|
act(missing, 'confirm');
|
|
|
|
|
act(missing, 'receipt', { amount: '2160', account: '助理支付宝', verified: true });
|
|
|
|
|
let before = snapshot(l);
|
|
|
|
|
assert.throws(() => act(missing, 'complete', { verified: true }), /料费成本/);
|
|
|
|
|
assert.deepEqual(snapshot(l), before);
|
|
|
|
|
act(missing, 'cost', {
|
|
|
|
|
costs: [
|
|
|
|
|
{
|
|
|
|
|
parts: { material: '1980', labor: '0', accessories: '0', other: '0' },
|
|
|
|
|
costLabor: { mode: 'gram', rate: '20' },
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
before = snapshot(l);
|
|
|
|
|
assert.throws(() => act(missing, 'complete', { verified: true }), /附加成本/);
|
|
|
|
|
assert.deepEqual(snapshot(l), before);
|
2026-10-17 19:50:24 +08:00
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
test('新单后置出库失败和旧单费用日期冲突时,收款、费用、库存与订单原状一起回滚', t => {
|
|
|
|
|
const { l, issue, save, act } = fixture(t),
|
|
|
|
|
before = snapshot(l);
|
|
|
|
|
assert.throws(() => issue(input({ date: '2026-10-16' })), /2026-10-16库存不足/);
|
|
|
|
|
assert.deepEqual(snapshot(l), before);
|
|
|
|
|
const id = save(input({ date: '2026-10-16', fees_date: date }));
|
|
|
|
|
act(id, 'confirm', { date: '2026-10-16' });
|
|
|
|
|
act(id, 'receipt', { date: '2026-10-16', amount: '2160', account: '助理支付宝', verified: true });
|
|
|
|
|
const legacyBefore = snapshot(l);
|
|
|
|
|
assert.throws(() => act(id, 'complete', { date: '2026-10-16', verified: true }), /交付日期不能早于/);
|
|
|
|
|
assert.deepEqual(snapshot(l), legacyBefore);
|
2026-10-17 19:50:24 +08:00
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
test('HTTP开单接口受登录和CSRF保护,并发重试只记一次交付,重启后保留', async t => {
|
|
|
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'maimai-paid-http-')),
|
|
|
|
|
app = createApp({ mode: 'demo', dataDir: dir, feishuConfig: { enabled: false } });
|
|
|
|
|
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(), origin = base) =>
|
|
|
|
|
fetch(base + route, {
|
|
|
|
|
method: data ? 'POST' : 'GET',
|
|
|
|
|
headers: {
|
|
|
|
|
Cookie: cookie,
|
|
|
|
|
...(data
|
|
|
|
|
? {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
Origin: origin,
|
|
|
|
|
'X-Requested-With': 'maimai-ledger',
|
|
|
|
|
'Idempotency-Key': key,
|
|
|
|
|
}
|
|
|
|
|
: {}),
|
|
|
|
|
},
|
|
|
|
|
...(data ? { body: JSON.stringify(data) } : {}),
|
|
|
|
|
});
|
|
|
|
|
const p = input({ date: app.ledger.orderBook().today }),
|
|
|
|
|
before = app.ledger.rows('SELECT * FROM orders').length;
|
|
|
|
|
assert.equal((await call('/api/orders/issue', p)).status, 401);
|
|
|
|
|
const login = await call('/api/demo-login', { role: 'assistant' });
|
|
|
|
|
cookie = login.headers.get('set-cookie').split(';')[0];
|
|
|
|
|
assert.equal((await call('/api/orders/issue', p, randomUUID(), 'https://invalid.example')).status, 403);
|
|
|
|
|
const key = randomUUID(),
|
|
|
|
|
rs = await Promise.all([call('/api/orders/issue', p, key), call('/api/orders/issue', p, key)]);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
rs.map(r => r.status),
|
|
|
|
|
[200, 200],
|
|
|
|
|
);
|
|
|
|
|
const a = await rs[0].json(),
|
|
|
|
|
b = await rs[1].json();
|
|
|
|
|
assert.equal(a.id, b.id);
|
|
|
|
|
const detail = await (await call('/api/orders/' + a.id)).json();
|
|
|
|
|
assert.equal(detail.status, 'delivered');
|
|
|
|
|
assert.equal(detail.payments.length, 1);
|
|
|
|
|
assert.equal(detail.netReceived, 216000);
|
|
|
|
|
assert.equal(detail.expenses.length, 1);
|
|
|
|
|
assert.equal(detail.profit, 13000);
|
|
|
|
|
assert.equal(app.ledger.rows('SELECT * FROM orders').length, before + 1);
|
|
|
|
|
const reloaded = new Ledger(path.join(dir, 'ledger.sqlite'));
|
|
|
|
|
try {
|
|
|
|
|
assert.equal(reloaded.detail(a.id).status, 'delivered');
|
|
|
|
|
assert.equal(reloaded.detail(a.id).payments.length, 1);
|
|
|
|
|
assert.equal(reloaded.detail(a.id).profit, 13000);
|
|
|
|
|
assert.equal(reloaded.rows("SELECT * FROM movements WHERE order_id=? AND kind='sale'", a.id).length, 1);
|
|
|
|
|
} finally {
|
|
|
|
|
reloaded.close();
|
|
|
|
|
}
|
2026-10-17 19:50:24 +08:00
|
|
|
});
|