2026-10-23 00:38:04 +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, createUser } = require('../server'),
|
|
|
|
|
Calc = require('../public/calculations');
|
|
|
|
|
const { FeishuSync } = require('../sync');
|
|
|
|
|
const D0 = '2026-10-15',
|
|
|
|
|
D = '2026-10-16',
|
|
|
|
|
D2 = '2026-10-17',
|
|
|
|
|
D3 = '2026-10-18';
|
|
|
|
|
const owner = { id: 'cancel-owner', name: '测试麦麦', role: 'owner' },
|
|
|
|
|
assistant = { id: 'cancel-assistant', name: '测试助理', role: 'assistant' };
|
|
|
|
|
const line = (extra = {}) => ({
|
|
|
|
|
name: '足金999',
|
|
|
|
|
unit: 'g',
|
|
|
|
|
quantity: '2',
|
|
|
|
|
pocket: '现货',
|
|
|
|
|
payer: 'owner',
|
|
|
|
|
sale: '2000',
|
|
|
|
|
costParts: { material: '1700', labor: '20', accessories: '0', other: '0' },
|
|
|
|
|
...extra,
|
|
|
|
|
});
|
|
|
|
|
const input = (extra = {}) => ({
|
|
|
|
|
date: D,
|
|
|
|
|
customer: '测试客户',
|
|
|
|
|
internal_note: '测试内部备注',
|
|
|
|
|
salesperson: '测试开单人',
|
|
|
|
|
note: '原备注',
|
|
|
|
|
rounding_mode: 'cent',
|
|
|
|
|
payment_account: '助理微信',
|
|
|
|
|
lines: [line()],
|
|
|
|
|
fee_items: [{ key: 'shipping', note: '快递费', amount: '6', payer: 'assistant' }],
|
|
|
|
|
...extra,
|
|
|
|
|
});
|
|
|
|
|
const all = ledger =>
|
|
|
|
|
ledger
|
|
|
|
|
.rows("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
|
|
|
|
|
.map(({ name }) => [name, ledger.rows('SELECT * FROM "' + name + '"')]);
|
|
|
|
|
const business = ledger =>
|
|
|
|
|
Object.fromEntries(
|
|
|
|
|
[
|
|
|
|
|
'orders',
|
|
|
|
|
'payments',
|
|
|
|
|
'expenses',
|
|
|
|
|
'settlements',
|
|
|
|
|
'movements',
|
|
|
|
|
'journal',
|
|
|
|
|
'corrections',
|
|
|
|
|
'closes',
|
|
|
|
|
'inventory_snapshots',
|
|
|
|
|
'sync',
|
|
|
|
|
'material_purchases',
|
|
|
|
|
'material_pool_usages',
|
|
|
|
|
'material_pool_settlements',
|
|
|
|
|
].map(table => [table, ledger.rows('SELECT * FROM ' + table)]),
|
|
|
|
|
);
|
|
|
|
|
function fixture(t) {
|
|
|
|
|
const l = new Ledger(':memory:');
|
|
|
|
|
t.after(() => l.close());
|
|
|
|
|
const run = (payload, fn, actor = assistant, key = randomUUID()) => l.transact(key, actor, payload, fn);
|
|
|
|
|
for (const pocket of ['现货', '金料'])
|
|
|
|
|
run({ pocket }, () => l.stock({ kind: 'opening', date: D0, pocket, grams: '20', note: '合成库存' }, owner), owner);
|
|
|
|
|
const issue = (p = input()) => run(p, () => l.issueOrder(p, assistant)).id;
|
|
|
|
|
const save = (p = input()) => run(p, () => l.saveOrder(p, assistant)).id;
|
|
|
|
|
const act = (id, kind, extra = {}, actor = owner) =>
|
|
|
|
|
run({ id, kind, extra }, () => l.action(id, kind, { date: D, ...extra }, actor), actor);
|
|
|
|
|
const request = (id, extra = {}, actor = assistant, key = randomUUID()) => {
|
|
|
|
|
const p = { revision: l.detail(id).revision, reason: '原订单重复录入,应作废', ...extra };
|
|
|
|
|
return run({ request: id, p }, () => l.requestOrderCancellation(id, p, actor), actor, key);
|
|
|
|
|
};
|
|
|
|
|
const approve = (request, extra = {}, actor = owner, key = randomUUID()) => {
|
|
|
|
|
const p = { revision: request.request.order_revision, verified: true, note: '已核实原始订单', ...extra };
|
|
|
|
|
return run(
|
|
|
|
|
{ approve: request.requestId, p },
|
|
|
|
|
() => l.approveOrderCancellation(request.requestId, p, actor),
|
|
|
|
|
actor,
|
|
|
|
|
key,
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
const reject = (request, note = '原订单有效,请继续交付', actor = owner) =>
|
|
|
|
|
run(
|
|
|
|
|
{ reject: request.requestId, note },
|
|
|
|
|
() => l.rejectOrderCancellation(request.requestId, { note }, actor),
|
|
|
|
|
actor,
|
|
|
|
|
);
|
|
|
|
|
const edit = (id, change = {}) => {
|
|
|
|
|
const p = {
|
|
|
|
|
...l.orderEdit(id, assistant).inputs,
|
|
|
|
|
revision: l.detail(id).revision,
|
|
|
|
|
reason: '核对后更正',
|
|
|
|
|
verified: true,
|
|
|
|
|
...change,
|
|
|
|
|
};
|
|
|
|
|
return run({ edit: id, p }, () => l.editOrder(id, p, owner), owner);
|
|
|
|
|
};
|
|
|
|
|
const buy = (grams = '10', total = '9000', payer = 'owner') => {
|
|
|
|
|
const p = { date: D, grams, total, payer, note: '真实购料原凭据' };
|
|
|
|
|
return run(p, () => l.createMaterialPurchase(p, assistant));
|
|
|
|
|
};
|
|
|
|
|
return { l, run, issue, save, act, request, approve, reject, edit, buy };
|
2026-10-23 00:38:04 +08:00
|
|
|
}
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('申请与驳回不改原状态财务库存汇总;每单仅一待审,可全局跨日查看并重新申请', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
id = f.issue(),
|
|
|
|
|
original = business(f.l),
|
|
|
|
|
report = f.l.reportByOrderDate(D),
|
|
|
|
|
key = randomUUID();
|
|
|
|
|
const application = f.request(id, {}, assistant, key),
|
|
|
|
|
same = f.request(id, {}, assistant, key);
|
|
|
|
|
assert.equal(JSON.stringify(same), JSON.stringify(application));
|
|
|
|
|
assert.deepEqual(business(f.l), original);
|
|
|
|
|
assert.equal(f.l.detail(id).status, 'delivered');
|
|
|
|
|
assert.equal(f.l.reportByOrderDate(D).sales, report.sales);
|
|
|
|
|
assert.equal(f.l.orderCancellation(id, assistant).pending.id, application.requestId);
|
|
|
|
|
assert.equal(f.l.orderCancellations({}, owner).requests[0].order_date, D);
|
|
|
|
|
assert.equal(f.l.orderCancellations({}, owner).counts.pending, 1);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.request(id),
|
|
|
|
|
error => error.status === 409 && /待审批/.test(error.message),
|
|
|
|
|
);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.reject(application, '不同意', assistant),
|
|
|
|
|
error => error.status === 403,
|
|
|
|
|
);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.approve(application, {}, assistant),
|
|
|
|
|
error => error.status === 403,
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(business(f.l), original);
|
|
|
|
|
f.reject(application);
|
|
|
|
|
assert.deepEqual(business(f.l), original);
|
|
|
|
|
assert.equal(f.l.detail(id).cancellationPending, null);
|
|
|
|
|
const next = f.request(id);
|
|
|
|
|
assert.notEqual(next.requestId, application.requestId);
|
|
|
|
|
assert.equal(f.l.orderCancellation(id, owner).requests.length, 2);
|
|
|
|
|
assert.equal(f.l.orderCancellations({ status: 'all' }, owner).counts.rejected, 1);
|
2026-10-23 00:38:04 +08:00
|
|
|
});
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('批准录错作废原日撤销销售成本收款并恢复库存,原单原流水真实交款与审计完整保留', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
id = f.issue(),
|
|
|
|
|
original = f.l.detail(id);
|
|
|
|
|
f.act(id, 'settlement', { bucket: 'cost', amount: '1000', direction: 'toOwner', verified: true, note: '已实际转款' });
|
|
|
|
|
for (const date of [D, D2]) f.run({ close: date }, () => f.l.closeDay(date, owner, 'orderDate'), owner);
|
|
|
|
|
const before = business(f.l),
|
|
|
|
|
request = f.request(id),
|
|
|
|
|
key = randomUUID(),
|
|
|
|
|
result = f.approve(request, {}, owner, key);
|
|
|
|
|
assert.equal(JSON.stringify(f.approve(request, {}, owner, key)), JSON.stringify(result));
|
|
|
|
|
const order = f.l.detail(id);
|
|
|
|
|
for (const field of ['id', 'number', 'paper_number', 'created', 'lines', 'total', 'date'])
|
|
|
|
|
assert.deepEqual(order[field], original[field]);
|
|
|
|
|
assert.equal(order.status, 'cancelled');
|
|
|
|
|
assert.equal(order.netReceived, 0);
|
|
|
|
|
assert.equal(order.directCost, 0);
|
|
|
|
|
assert.equal(order.profit, 0);
|
|
|
|
|
assert.equal(order.settlementDue, -100000);
|
|
|
|
|
assert.equal(order.reviewed_revision, 0);
|
|
|
|
|
assert.equal(order.cancellationApproved.id, request.requestId);
|
|
|
|
|
assert.equal(order.cancellationPending, null);
|
|
|
|
|
assert.equal(f.l.inventory(D).find(row => row.pocket === '现货').mg, 20000);
|
|
|
|
|
assert.equal(f.l.inventory().find(row => row.pocket === '现货').reserved, 0);
|
|
|
|
|
assert.deepEqual(f.l.rows('SELECT * FROM settlements'), before.settlements);
|
|
|
|
|
for (const table of ['payments', 'expenses', 'movements', 'journal'])
|
|
|
|
|
for (const row of before[table]) assert.deepEqual(f.l.one('SELECT * FROM ' + table + ' WHERE id=?', row.id), row);
|
|
|
|
|
assert.ok(order.payments.every(row => row.kind === 'receipt'));
|
|
|
|
|
assert.equal(f.l.report(D).returns, 0);
|
|
|
|
|
assert.equal(f.l.report(D).delivered, 0);
|
|
|
|
|
const report = f.l.reportByOrderDate(D);
|
|
|
|
|
for (const key of ['sales', 'goods', 'expense', 'profit', 'outlayTotal']) assert.equal(report[key], 0, key);
|
|
|
|
|
assert.equal(report.receiptsByOrderDate.net, 0);
|
|
|
|
|
assert.equal(report.orderRows[0].cancellationApproved.id, request.requestId);
|
|
|
|
|
for (const date of [D, D2]) {
|
|
|
|
|
assert.equal(f.l.inventorySnapshot(date).stale, true);
|
|
|
|
|
assert.equal(f.l.one('SELECT stale FROM closes WHERE date=?', date).stale, 1);
|
|
|
|
|
}
|
|
|
|
|
assert.equal(f.l.one('SELECT revision FROM sync WHERE entity=?', 'order:' + id).revision, order.revision);
|
|
|
|
|
const mirror = new FeishuSync(f.l, null).fields('order:' + id, order.revision);
|
|
|
|
|
assert.equal(mirror['销售金额'], 0);
|
|
|
|
|
assert.equal(mirror['订单利润'], 0);
|
|
|
|
|
assert.equal(mirror['已收净额'], 0);
|
|
|
|
|
assert.equal(JSON.parse(mirror['明细']).total, original.total);
|
|
|
|
|
assert.ok(
|
|
|
|
|
order.audit.some(row => row.action === '同意取消订单(录错作废)' && row.detail.requestId === request.requestId),
|
|
|
|
|
);
|
|
|
|
|
f.act(id, 'settlement', {
|
|
|
|
|
date: D2,
|
|
|
|
|
bucket: 'cost',
|
|
|
|
|
amount: '1000',
|
|
|
|
|
direction: 'toAssistant',
|
|
|
|
|
verified: true,
|
|
|
|
|
note: '退回原已交款',
|
|
|
|
|
});
|
|
|
|
|
assert.equal(f.l.detail(id).settlementDue, 0);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.approve(request),
|
|
|
|
|
error => error.status === 409 && /已经处理/.test(error.message),
|
|
|
|
|
);
|
2026-10-23 00:38:04 +08:00
|
|
|
});
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('多次改克重货盘日期后按本单有效库存冲回;跨日更正的收支各日期账户付款人净额均归零', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
id = f.issue();
|
|
|
|
|
f.act(id, 'correct', {
|
|
|
|
|
date: D2,
|
|
|
|
|
type: 'payment',
|
|
|
|
|
target: f.l.detail(id).payments[0].id,
|
|
|
|
|
amount: '2000',
|
|
|
|
|
account: '麦麦支付宝',
|
|
|
|
|
verified: true,
|
|
|
|
|
reason: '收款渠道录错',
|
|
|
|
|
});
|
|
|
|
|
f.act(id, 'correct', {
|
|
|
|
|
date: D2,
|
|
|
|
|
type: 'expense',
|
|
|
|
|
target: f.l.detail(id).expenses[0].id,
|
|
|
|
|
amount: '8',
|
|
|
|
|
payer: 'owner',
|
|
|
|
|
verified: true,
|
|
|
|
|
reason: '原费用录错',
|
|
|
|
|
});
|
|
|
|
|
f.act(id, 'correct', {
|
|
|
|
|
date: D3,
|
|
|
|
|
type: 'expense',
|
|
|
|
|
target: f.l.detail(id).expenses.find(row => row.amount === 800).id,
|
|
|
|
|
amount: '5',
|
|
|
|
|
payer: 'assistant',
|
|
|
|
|
verified: true,
|
|
|
|
|
reason: '最终费用核实',
|
|
|
|
|
});
|
|
|
|
|
f.edit(id, {
|
|
|
|
|
date: D2,
|
|
|
|
|
lines: [
|
|
|
|
|
line({
|
|
|
|
|
quantity: '3',
|
|
|
|
|
pocket: '金料',
|
|
|
|
|
sale: '3000',
|
|
|
|
|
costParts: { material: '2600', labor: '25', accessories: '0', other: '0' },
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
f.edit(id, {
|
|
|
|
|
date: D3,
|
|
|
|
|
lines: [
|
|
|
|
|
line({
|
|
|
|
|
quantity: '1.123',
|
|
|
|
|
pocket: '现货',
|
|
|
|
|
sale: '1800',
|
|
|
|
|
costParts: { material: '1500', labor: '11', accessories: '0', other: '0' },
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
const before = business(f.l),
|
|
|
|
|
application = f.request(id);
|
|
|
|
|
assert.deepEqual(application.request.summary.inventory, [{ date: D3, pocket: '现货', mg: 1123 }]);
|
2026-10-23 00:38:04 +08:00
|
|
|
f.approve(application);
|
2026-12-04 08:58:53 -08:00
|
|
|
for (const table of ['payments', 'expenses', 'movements', 'journal'])
|
|
|
|
|
for (const row of before[table]) assert.deepEqual(f.l.one('SELECT * FROM ' + table + ' WHERE id=?', row.id), row);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
f.l.rows(
|
|
|
|
|
'SELECT date,kind,account,SUM(amount) AS amount FROM payments WHERE order_id=? GROUP BY date,kind,account HAVING SUM(amount)<>0',
|
|
|
|
|
id,
|
|
|
|
|
),
|
|
|
|
|
[],
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
f.l.rows(
|
|
|
|
|
'SELECT date,payer,SUM(amount) AS amount FROM expenses WHERE order_id=? GROUP BY date,payer HAVING SUM(amount)<>0',
|
|
|
|
|
id,
|
|
|
|
|
),
|
|
|
|
|
[],
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
f.l.rows(
|
|
|
|
|
'SELECT date,pocket,SUM(mg) AS mg FROM movements WHERE order_id=? GROUP BY date,pocket HAVING SUM(mg)<>0',
|
|
|
|
|
id,
|
|
|
|
|
),
|
|
|
|
|
[],
|
|
|
|
|
);
|
|
|
|
|
for (const date of [D, D2, D3]) {
|
|
|
|
|
const r = f.l.report(date);
|
|
|
|
|
for (const key of ['sales', 'goods', 'expense', 'profit']) assert.equal(r[key], 0, date + key);
|
|
|
|
|
assert.equal(f.l.inventory(date).find(row => row.pocket === '现货').mg, 20000);
|
|
|
|
|
assert.equal(f.l.inventory(date).find(row => row.pocket === '金料').mg, 20000);
|
|
|
|
|
}
|
|
|
|
|
const costs = Calc.orderCosts(f.l.detail(id));
|
|
|
|
|
assert.equal(costs.total.total, 0);
|
|
|
|
|
assert.equal(costs.rows.filter(row => row.kind === 'paid').length, 0);
|
|
|
|
|
const report = f.l.reportByOrderDate(D3);
|
|
|
|
|
assert.equal(report.payerCosts.owner.total, 0);
|
|
|
|
|
assert.equal(report.payerCosts.assistant.total, 0);
|
|
|
|
|
assert.equal(report.outlayTotal, 0);
|
2026-10-23 00:38:04 +08:00
|
|
|
});
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('日购料作废释放有效占用并重排后续FIFO,原采购和料池实际交款不变,也不产生虚构退货入库', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.buy('4', '3600', 'owner');
|
|
|
|
|
f.buy('6', '5400', 'assistant');
|
|
|
|
|
const pooled = quantity =>
|
|
|
|
|
line({
|
|
|
|
|
quantity,
|
|
|
|
|
pocket: '',
|
|
|
|
|
costMaterial: { mode: 'pool', poolId: D },
|
|
|
|
|
costParts: { material: '', labor: '10', accessories: '5', other: '0' },
|
|
|
|
|
});
|
|
|
|
|
const id = f.issue(input({ lines: [pooled('6')], fee_items: [] })),
|
|
|
|
|
later = f.issue(input({ lines: [pooled('3')], fee_items: [] }));
|
|
|
|
|
const transfer = {
|
|
|
|
|
date: D,
|
|
|
|
|
revision: f.l.materialPoolDetail(D).revision,
|
|
|
|
|
amount: '900',
|
|
|
|
|
direction: 'toAssistant',
|
|
|
|
|
verified: true,
|
|
|
|
|
};
|
|
|
|
|
f.run(transfer, () => f.l.settleMaterialPool(D, transfer, assistant));
|
|
|
|
|
assert.equal(Calc.orderCosts(f.l.detail(later)).byPayer.assistant.goods, 270000);
|
|
|
|
|
const before = business(f.l),
|
|
|
|
|
application = f.request(id);
|
|
|
|
|
assert.deepEqual(application.request.summary.inventory, []);
|
|
|
|
|
f.approve(application);
|
|
|
|
|
assert.deepEqual(f.l.rows('SELECT * FROM material_purchases'), before.material_purchases);
|
|
|
|
|
assert.deepEqual(f.l.rows('SELECT * FROM material_pool_settlements'), before.material_pool_settlements);
|
|
|
|
|
assert.deepEqual(f.l.rows('SELECT * FROM movements'), before.movements);
|
|
|
|
|
assert.equal(f.l.materialPoolDetail(D).usedMg, 3000);
|
|
|
|
|
assert.equal(Calc.orderCosts(f.l.detail(later)).byPayer.owner.goods, 271500);
|
|
|
|
|
assert.equal(Calc.orderCosts(f.l.detail(later)).byPayer.assistant.goods, 0);
|
|
|
|
|
assert.ok(f.l.rows('SELECT * FROM material_pool_usages WHERE order_id=?', id).every(row => row.active === 0));
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
f.l
|
|
|
|
|
.rows('SELECT kind FROM material_pool_events WHERE order_id=? AND kind LIKE ? ORDER BY rowid', id, '%释放')
|
|
|
|
|
.map(row => row.kind),
|
|
|
|
|
['订单录错作废释放'],
|
|
|
|
|
);
|
|
|
|
|
const r = f.l.reportByOrderDate(D);
|
|
|
|
|
assert.equal(r.outlayGoods, 901500);
|
|
|
|
|
assert.equal(r.outlayExpense, 0);
|
|
|
|
|
assert.equal(r.orderRows.find(row => row.id === id).totalCost, 0);
|
|
|
|
|
assert.equal(r.orderRows.find(row => row.id === id).costItems.filter(row => row.kind === 'paid').length, 0);
|
|
|
|
|
assert.equal(f.l.detail(id).profit, 0);
|
2026-10-23 00:38:04 +08:00
|
|
|
});
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('待审批统一阻止账务及metadata;购料重算使申请过期,可驳回后重申', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
id = f.issue(),
|
|
|
|
|
application = f.request(id),
|
|
|
|
|
before = all(f.l);
|
|
|
|
|
for (const [kind, p] of [
|
|
|
|
|
['return', { verified: true, reason: '不得混成退货' }],
|
|
|
|
|
['expense', { amount: '1', payer: 'owner', note: '新费用' }],
|
|
|
|
|
['receipt', {}],
|
|
|
|
|
['refund', {}],
|
|
|
|
|
['fees', {}],
|
|
|
|
|
['cost', {}],
|
|
|
|
|
['complete', {}],
|
|
|
|
|
['deliver', {}],
|
|
|
|
|
['material', {}],
|
|
|
|
|
['settlement', {}],
|
|
|
|
|
['correct', { type: 'expense' }],
|
|
|
|
|
]) {
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.act(id, kind, p),
|
|
|
|
|
error => error.status === 409 && /待审批/.test(error.message),
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(all(f.l), before);
|
2026-10-23 00:38:04 +08:00
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
assert.throws(() => f.edit(id, { lines: [line({ sale: '9999' })] }), /待审批/);
|
|
|
|
|
assert.deepEqual(all(f.l), before);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.edit(id, { customer: '仅更正称呼' }),
|
|
|
|
|
error => error.status === 409 && /待审批/.test(error.message),
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(all(f.l), before);
|
|
|
|
|
f.reject(application, '先核对资料后重新申请');
|
|
|
|
|
f.edit(id, { customer: '核对后更正称呼' });
|
|
|
|
|
const next = f.request(id);
|
|
|
|
|
f.approve(next);
|
|
|
|
|
assert.equal(f.l.detail(id).status, 'cancelled');
|
|
|
|
|
f.buy();
|
|
|
|
|
const pooled = f.issue(
|
|
|
|
|
input({ lines: [line({ pocket: '', costMaterial: { mode: 'pool', poolId: D } })], fee_items: [] }),
|
|
|
|
|
),
|
|
|
|
|
old = f.request(pooled);
|
|
|
|
|
f.buy('1', '1000', 'assistant');
|
|
|
|
|
assert.equal(f.l.orderCancellation(pooled, owner).pending.stale, true);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.approve(old),
|
|
|
|
|
error => error.status === 409 && /已过期/.test(error.message),
|
|
|
|
|
);
|
|
|
|
|
f.reject(old, '购料已重算');
|
|
|
|
|
f.approve(f.request(pooled));
|
2026-10-23 00:38:04 +08:00
|
|
|
});
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('版本未touch但原凭据变化仍使审批过期;草稿和待交付可申请且申请期间不能保存或开单', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
id = f.issue(),
|
|
|
|
|
application = f.request(id),
|
|
|
|
|
revision = f.l.detail(id).revision;
|
|
|
|
|
f.l.run('UPDATE payments SET account=? WHERE order_id=?', '麦麦现金', id);
|
|
|
|
|
assert.equal(f.l.detail(id).revision, revision);
|
|
|
|
|
const before = all(f.l);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.approve(application),
|
|
|
|
|
error => error.status === 409 && /已过期/.test(error.message),
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(all(f.l), before);
|
|
|
|
|
f.reject(application, '原始凭据发生变化');
|
|
|
|
|
for (const status of ['draft', 'confirmed']) {
|
|
|
|
|
const p = input({ fee_items: [] }),
|
|
|
|
|
draft = f.save(p);
|
|
|
|
|
if (status === 'confirmed') {
|
|
|
|
|
f.act(draft, 'confirm');
|
|
|
|
|
f.act(draft, 'receipt', { amount: '300', account: '助理微信', verified: true });
|
|
|
|
|
}
|
|
|
|
|
const requested = f.request(draft),
|
|
|
|
|
state = all(f.l);
|
|
|
|
|
for (const fn of [
|
|
|
|
|
() => f.l.saveOrder({ ...p, id: draft, revision: f.l.detail(draft).revision }, assistant),
|
|
|
|
|
() => f.l.issueOrder({ ...p, id: draft, revision: f.l.detail(draft).revision }, assistant),
|
|
|
|
|
]) {
|
|
|
|
|
assert.throws(fn, /待审批/);
|
|
|
|
|
assert.deepEqual(all(f.l), state);
|
|
|
|
|
}
|
|
|
|
|
f.approve(requested);
|
|
|
|
|
assert.equal(f.l.detail(draft).netReceived, 0);
|
|
|
|
|
assert.equal(f.l.detail(draft).status, 'cancelled');
|
2026-10-23 00:38:04 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('批准后财务入口均拒绝恢复销售,metadata保持可改,真实双方转款余额可以处理', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
id = f.issue();
|
|
|
|
|
f.act(id, 'settlement', { bucket: 'cost', amount: '50', direction: 'toOwner', verified: true, note: '原真实转账' });
|
|
|
|
|
f.approve(f.request(id));
|
|
|
|
|
const before = business(f.l);
|
|
|
|
|
for (const [kind, p] of [
|
|
|
|
|
['confirm', {}],
|
|
|
|
|
['receipt', {}],
|
|
|
|
|
['refund', {}],
|
|
|
|
|
['expense', {}],
|
|
|
|
|
['fees', {}],
|
|
|
|
|
['cost', {}],
|
|
|
|
|
['complete', {}],
|
|
|
|
|
['deliver', {}],
|
|
|
|
|
['return', {}],
|
|
|
|
|
['material', {}],
|
|
|
|
|
['correct', { type: 'payment' }],
|
|
|
|
|
['correct', { type: 'expense' }],
|
|
|
|
|
]) {
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.act(id, kind, p),
|
|
|
|
|
error => error.status === 409 && /已作废/.test(error.message),
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(business(f.l), before);
|
2026-10-23 00:38:04 +08:00
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
assert.throws(() => f.edit(id, { lines: [line({ sale: '9999' })] }), /仅支持修改/);
|
|
|
|
|
assert.deepEqual(business(f.l), before);
|
|
|
|
|
f.edit(id, { customer: '资料更正', internal_note: '测试内部备注', salesperson: '麦麦', note: '保留审核后资料' });
|
|
|
|
|
assert.equal(f.l.detail(id).customer, '资料更正');
|
|
|
|
|
assert.equal(f.l.detail(id).status, 'cancelled');
|
|
|
|
|
for (const table of ['payments', 'expenses', 'settlements', 'movements', 'journal'])
|
|
|
|
|
assert.deepEqual(f.l.rows('SELECT * FROM ' + table), before[table]);
|
|
|
|
|
f.act(id, 'settlement', {
|
|
|
|
|
date: D2,
|
|
|
|
|
bucket: 'cost',
|
|
|
|
|
amount: '50',
|
|
|
|
|
direction: 'toAssistant',
|
|
|
|
|
verified: true,
|
|
|
|
|
note: '返还原交款',
|
|
|
|
|
});
|
|
|
|
|
assert.equal(f.l.detail(id).settlementDue, 0);
|
2026-10-23 00:38:04 +08:00
|
|
|
});
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('所有锁价关联单与已退货已取消不能申请;旧cancel动作不能绕过审批,输入校验无副作用', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
id = f.issue(),
|
|
|
|
|
returned = f.issue();
|
|
|
|
|
f.act(returned, 'return', { verified: true, reason: '真实退货' });
|
|
|
|
|
const lockInput = {
|
|
|
|
|
date: D,
|
|
|
|
|
customer: '锁价客户',
|
|
|
|
|
internal_note: '测试内部备注',
|
|
|
|
|
salesperson: '合成开单人',
|
|
|
|
|
line: { name: '足金999', quantity: '1', goldPrice: '1000', laborRate: '0', laborMode: 'gram' },
|
|
|
|
|
payment: { amount: '1000', account: '助理微信', verified: true },
|
|
|
|
|
material: { rate: '900', payer: 'assistant', verified: true },
|
|
|
|
|
};
|
|
|
|
|
const lock = f.run(lockInput, () => f.l.createPriceLock(lockInput, assistant));
|
|
|
|
|
const delivery = {
|
|
|
|
|
date: D2,
|
|
|
|
|
revision: 1,
|
|
|
|
|
verified: true,
|
|
|
|
|
line: {
|
|
|
|
|
quantity: '1',
|
|
|
|
|
laborRate: '0',
|
|
|
|
|
laborMode: 'gram',
|
|
|
|
|
payer: 'owner',
|
|
|
|
|
costParts: { labor: '0', accessories: '0', other: '0' },
|
|
|
|
|
},
|
|
|
|
|
inventory: { pocket: '现货', original: 'receive' },
|
|
|
|
|
fee_items: [],
|
|
|
|
|
};
|
|
|
|
|
const locked = f.run(delivery, () => f.l.deliverPriceLock(lock.id, delivery, assistant)).id;
|
|
|
|
|
for (const order of [returned, locked]) {
|
|
|
|
|
assert.equal(f.l.orderCancellation(order, owner).eligible, false);
|
|
|
|
|
const before = all(f.l);
|
|
|
|
|
assert.throws(() => f.request(order));
|
|
|
|
|
assert.deepEqual(all(f.l), before);
|
|
|
|
|
}
|
|
|
|
|
for (const actor of [owner, assistant])
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.act(id, 'cancel', { reason: '跳过审批' }, actor),
|
|
|
|
|
error => error.status === 409 && /须先提交取消申请/.test(error.message),
|
|
|
|
|
);
|
|
|
|
|
for (const p of [
|
|
|
|
|
{ reason: '' },
|
|
|
|
|
{ revision: 0 },
|
|
|
|
|
{ revision: String(f.l.detail(id).revision) },
|
|
|
|
|
{ reason: '长'.repeat(201) },
|
|
|
|
|
]) {
|
|
|
|
|
const before = all(f.l);
|
|
|
|
|
assert.throws(() => f.request(id, p));
|
|
|
|
|
assert.deepEqual(all(f.l), before);
|
|
|
|
|
}
|
|
|
|
|
const requested = f.request(id),
|
|
|
|
|
before = all(f.l);
|
|
|
|
|
assert.throws(() => f.approve(requested, { verified: false }), /请核实/);
|
|
|
|
|
assert.throws(() => f.reject(requested, ''), /驳回意见/);
|
|
|
|
|
assert.deepEqual(all(f.l), before);
|
|
|
|
|
f.approve(requested);
|
|
|
|
|
assert.throws(() => f.request(id), /不能重复/);
|
2026-10-23 00:38:04 +08:00
|
|
|
});
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('审批后置失败原子回滚包含他单FIFO重算快照和操作号,直接调用同样不留下半笔', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.buy('10', '9000', 'assistant');
|
|
|
|
|
const p = input({ lines: [line({ costMaterial: { mode: 'pool', poolId: D } })] }),
|
|
|
|
|
id = f.issue(p);
|
|
|
|
|
f.issue(p);
|
|
|
|
|
f.run({ close: D }, () => f.l.closeDay(D, owner, 'orderDate'), owner);
|
|
|
|
|
const requested = f.request(id),
|
|
|
|
|
before = all(f.l),
|
|
|
|
|
audit = f.l.audit;
|
|
|
|
|
f.l.audit = function (entity, action, ...args) {
|
|
|
|
|
if (action === '同意取消订单(录错作废)') throw new Error('模拟审批末尾写入故障');
|
|
|
|
|
return audit.call(this, entity, action, ...args);
|
|
|
|
|
};
|
|
|
|
|
assert.throws(() => f.approve(requested), /模拟审批末尾/);
|
|
|
|
|
assert.deepEqual(all(f.l), before);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() =>
|
|
|
|
|
f.l.approveOrderCancellation(
|
|
|
|
|
requested.requestId,
|
|
|
|
|
{ revision: requested.request.order_revision, verified: true },
|
|
|
|
|
owner,
|
|
|
|
|
),
|
|
|
|
|
/模拟审批末尾/,
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(all(f.l), before);
|
|
|
|
|
f.l.audit = audit;
|
|
|
|
|
f.approve(requested);
|
|
|
|
|
assert.equal(f.l.detail(id).status, 'cancelled');
|
2026-10-23 00:38:04 +08:00
|
|
|
});
|
|
|
|
|
|
2026-12-04 08:58:53 -08:00
|
|
|
test('HTTP审批保护登录CSRF权限和版本,同key并发只审批一次,跨日全局待审不受订单筛选影响', async t => {
|
|
|
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'maimai-cancellation-http-')),
|
|
|
|
|
l = new Ledger(path.join(dir, 'ledger.sqlite'));
|
|
|
|
|
createUser(l, '麦麦', 'owner', 'synthetic-owner-password');
|
|
|
|
|
createUser(l, '助理', 'assistant', 'synthetic-assistant-password');
|
|
|
|
|
const app = createApp({ mode: 'trial', port: 0, dataDir: dir, ledger: l });
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
l.close();
|
|
|
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
|
|
|
});
|
|
|
|
|
const base = 'http://127.0.0.1:' + app.server.address().port,
|
|
|
|
|
cookies = {};
|
|
|
|
|
const call = (who, route, body, key = randomUUID(), extra = {}) =>
|
|
|
|
|
fetch(base + route, {
|
|
|
|
|
method: body === undefined ? 'GET' : 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
...(cookies[who] ? { Cookie: cookies[who] } : {}),
|
|
|
|
|
...(body === undefined
|
|
|
|
|
? {}
|
|
|
|
|
: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'X-Requested-With': 'maimai-ledger',
|
|
|
|
|
Origin: base,
|
|
|
|
|
'Idempotency-Key': key,
|
|
|
|
|
}),
|
|
|
|
|
...extra,
|
|
|
|
|
},
|
|
|
|
|
...(body === undefined ? {} : { body: JSON.stringify(body) }),
|
|
|
|
|
});
|
|
|
|
|
assert.equal((await call('none', '/api/order-cancellations')).status, 401);
|
|
|
|
|
for (const [who, name] of [
|
|
|
|
|
['owner', '麦麦'],
|
|
|
|
|
['assistant', '助理'],
|
|
|
|
|
]) {
|
|
|
|
|
const response = await call(who, '/api/login', {
|
|
|
|
|
name,
|
|
|
|
|
password: who === 'owner' ? 'synthetic-owner-password' : 'synthetic-assistant-password',
|
|
|
|
|
});
|
|
|
|
|
cookies[who] = response.headers.get('set-cookie').split(';')[0];
|
|
|
|
|
}
|
|
|
|
|
const payload = input({ lines: [line({ pocket: '' })] }),
|
|
|
|
|
issued = await (await call('assistant', '/api/orders/issue', payload)).json(),
|
|
|
|
|
id = issued.id;
|
|
|
|
|
const info = await (await call('assistant', '/api/orders/' + id + '/cancellation')).json(),
|
|
|
|
|
body = { revision: info.revision, reason: '重复录入' };
|
|
|
|
|
assert.equal(
|
|
|
|
|
(
|
|
|
|
|
await call('assistant', '/api/orders/' + id + '/cancellation', body, undefined, {
|
|
|
|
|
Origin: 'https://evil.example',
|
|
|
|
|
})
|
|
|
|
|
).status,
|
|
|
|
|
403,
|
|
|
|
|
);
|
|
|
|
|
const key = randomUUID(),
|
|
|
|
|
responses = await Promise.all([
|
|
|
|
|
call('assistant', '/api/orders/' + id + '/cancellation', body, key),
|
|
|
|
|
call('assistant', '/api/orders/' + id + '/cancellation', body, key),
|
|
|
|
|
]);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
responses.map(row => row.status),
|
|
|
|
|
[200, 200],
|
|
|
|
|
);
|
|
|
|
|
const [application, retry] = await Promise.all(responses.map(row => row.json()));
|
|
|
|
|
assert.equal(application.requestId, retry.requestId);
|
|
|
|
|
const requestPath = '/api/order-cancellations/' + application.requestId,
|
|
|
|
|
decision = { revision: info.revision, verified: true, note: '核实后作废' };
|
|
|
|
|
assert.equal((await call('assistant', requestPath + '/approve', decision)).status, 403);
|
|
|
|
|
assert.equal((await call('assistant', requestPath + '/reject', { note: '不允许助理审批' })).status, 403);
|
|
|
|
|
assert.equal((await call('owner', '/api/orders/' + id + '/cancel', { reason: '旧客户端' })).status, 409);
|
|
|
|
|
const pending = await (await call('owner', '/api/order-cancellations')).json();
|
|
|
|
|
assert.equal(pending.counts.pending, 1);
|
|
|
|
|
assert.equal(pending.requests[0].order_date, D);
|
|
|
|
|
const approveKey = randomUUID(),
|
|
|
|
|
approved = await Promise.all([
|
|
|
|
|
call('owner', requestPath + '/approve', decision, approveKey),
|
|
|
|
|
call('owner', requestPath + '/approve', decision, approveKey),
|
|
|
|
|
]);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
approved.map(row => row.status),
|
|
|
|
|
[200, 200],
|
|
|
|
|
);
|
|
|
|
|
assert.equal((await call('owner', requestPath + '/approve', decision)).status, 409);
|
|
|
|
|
assert.equal((await call('owner', requestPath + '/reject', { note: '已经同意不能驳回' })).status, 409);
|
|
|
|
|
assert.equal(l.rows("SELECT * FROM audit WHERE action='同意取消订单(录错作废)'").length, 1);
|
|
|
|
|
assert.equal(l.rows('SELECT * FROM orders').length, 1);
|
|
|
|
|
assert.equal((await (await call('owner', '/api/orders/' + id)).json()).status, 'cancelled');
|
|
|
|
|
assert.equal((await (await call('owner', '/api/order-cancellations?status=all')).json()).counts.approved, 1);
|
|
|
|
|
const printable = await (await call('assistant', '/api/orders/' + id + '/public')).json();
|
|
|
|
|
assert.equal(printable.status, 'cancelled');
|
|
|
|
|
assert.ok(!JSON.stringify(printable).includes('cancellationApproved'));
|
|
|
|
|
const second = new Ledger(path.join(dir, 'ledger.sqlite'));
|
|
|
|
|
assert.equal(second.orderCancellation(id, owner).requests[0].status, 'approved');
|
|
|
|
|
second.close();
|
2026-10-23 00:38:04 +08:00
|
|
|
});
|