2026-10-26 17:07:26 +08:00
|
|
|
'use strict';
|
|
|
|
|
const { test } = require('node:test'),
|
|
|
|
|
assert = require('node:assert/strict'),
|
|
|
|
|
{ randomUUID } = require('node:crypto');
|
|
|
|
|
const { Ledger, today, day, money, weight, text, fail } = require('../ledger'),
|
|
|
|
|
{ createUser } = require('../server'),
|
|
|
|
|
Rolling = require('../rolling-material-pools');
|
|
|
|
|
const D = today(),
|
|
|
|
|
P = new Date(Date.parse(D + 'T12:00:00Z') - 86400000).toISOString().slice(0, 10),
|
|
|
|
|
P2 = new Date(Date.parse(D + 'T12:00:00Z') - 172800000).toISOString().slice(0, 10);
|
|
|
|
|
function fixture(t) {
|
|
|
|
|
const l = new Ledger(':memory:');
|
|
|
|
|
t.after(() => l.close());
|
|
|
|
|
const actors = {};
|
|
|
|
|
for (const member of ['owner', 'assistant', 'assistant2']) {
|
|
|
|
|
const role = member === 'owner' ? 'owner' : 'assistant',
|
|
|
|
|
name = '合成' + member;
|
|
|
|
|
actors[member] = { id: createUser(l, name, role, 'synthetic-rolling-password', member), name, role, member };
|
2026-12-04 08:58:53 -08:00
|
|
|
}
|
2026-10-26 17:07:26 +08:00
|
|
|
l.enableTeamMode();
|
|
|
|
|
const run = (data, fn, member = 'owner', key = randomUUID()) =>
|
|
|
|
|
l.scope(actors[member], () => l.transact(key, actors[member], data, fn));
|
|
|
|
|
run({}, () => l.stock({ date: P2, kind: 'opening', pocket: '现货', grams: '1000', note: '合成库存' }, actors.owner));
|
|
|
|
|
const buy = (grams, total, date = D, payer = 'owner', member = 'owner') => {
|
|
|
|
|
const input = { date, grams, total, payer, note: '合成购料' };
|
|
|
|
|
return run(input, () => l.createMaterialPurchase(input, actors[member]), member);
|
2026-11-30 15:40:40 +08:00
|
|
|
};
|
|
|
|
|
const issue = (grams, date = D, poolId = date, member = 'assistant', manual = false) => {
|
|
|
|
|
const input = {
|
|
|
|
|
date,
|
|
|
|
|
customer: '合成' + member + '客户',
|
|
|
|
|
internal_note: '测试内部备注',
|
|
|
|
|
salesperson: '合成开单人',
|
|
|
|
|
payment_account: '麦麦微信',
|
|
|
|
|
rounding_mode: 'cent',
|
|
|
|
|
lines: [
|
2026-12-04 08:58:53 -08:00
|
|
|
{
|
2026-11-30 15:40:40 +08:00
|
|
|
name: '足金999',
|
|
|
|
|
unit: 'g',
|
|
|
|
|
quantity: grams,
|
|
|
|
|
pocket: '现货',
|
|
|
|
|
payer: 'owner',
|
|
|
|
|
sale: String(Number(grams) * 1000),
|
|
|
|
|
costParts: { material: manual ? String(Number(grams) * 900) : '', labor: '0', accessories: '0', other: '0' },
|
|
|
|
|
...(manual ? {} : { costMaterial: { mode: 'pool', poolId } }),
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
|
|
|
|
],
|
2026-11-30 15:40:40 +08:00
|
|
|
fee_items: [],
|
|
|
|
|
};
|
|
|
|
|
return run(input, () => l.issueOrder(input, actors[member]), member).id;
|
2026-10-26 17:07:26 +08:00
|
|
|
};
|
|
|
|
|
const enable = () => {
|
|
|
|
|
const p = l.rollingMaterialPreview({}, actors.owner);
|
|
|
|
|
return run(p, () => l.enableRollingMaterial({ date: D, revision: p.revision, verified: true }, actors.owner));
|
2026-12-04 08:58:53 -08:00
|
|
|
};
|
2026-10-26 17:07:26 +08:00
|
|
|
const action = (id, kind, extra = {}) =>
|
|
|
|
|
run({ id, kind, extra }, () => l.action(id, kind, { date: D, verified: true, ...extra }, actors.owner));
|
|
|
|
|
const report = date => l.scope(actors.owner, () => l.reportByOrderDate(date, date, actors.owner));
|
|
|
|
|
return { l, actors, run, buy, issue, enable, action, report };
|
|
|
|
|
}
|
|
|
|
|
function contents(l) {
|
|
|
|
|
return JSON.stringify(
|
2026-12-04 08:58:53 -08:00
|
|
|
l
|
2026-10-26 17:07:26 +08:00
|
|
|
.rows("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name")
|
|
|
|
|
.map(({ name }) => [name, l.rows('SELECT * FROM "' + name + '" ORDER BY rowid')]),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
function financial(report) {
|
|
|
|
|
return Object.fromEntries(
|
2026-12-04 08:58:53 -08:00
|
|
|
[
|
2026-10-26 17:07:26 +08:00
|
|
|
'sales',
|
|
|
|
|
'goods',
|
|
|
|
|
'expense',
|
|
|
|
|
'profit',
|
|
|
|
|
'totalCost',
|
|
|
|
|
'summary',
|
|
|
|
|
'orderRows',
|
|
|
|
|
'payerCosts',
|
|
|
|
|
'orderPayerCosts',
|
|
|
|
|
'accounts',
|
|
|
|
|
'payments',
|
|
|
|
|
'journal',
|
|
|
|
|
'receiptsByOrderDate',
|
|
|
|
|
'reportRevision',
|
|
|
|
|
'close',
|
|
|
|
|
].map(key => [key, report[key]]),
|
2026-11-01 23:40:44 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
function actualFlows(l) {
|
|
|
|
|
return Object.fromEntries(
|
|
|
|
|
['payments', 'expenses', 'movements'].map(table => [table, l.rows('SELECT * FROM ' + table + ' ORDER BY rowid')]),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
function poolFacts(l) {
|
|
|
|
|
return Object.fromEntries(
|
2026-12-04 08:58:53 -08:00
|
|
|
[
|
2026-11-01 23:40:44 +08:00
|
|
|
'material_purchases',
|
|
|
|
|
'material_pool_usages',
|
|
|
|
|
'material_usage_parties',
|
|
|
|
|
'material_rolling_days',
|
|
|
|
|
'orders',
|
|
|
|
|
'journal',
|
|
|
|
|
'team_fund_sources',
|
|
|
|
|
'team_fund_entries',
|
|
|
|
|
'team_fund_cash',
|
|
|
|
|
].map(table => [table, l.rows('SELECT * FROM ' + table + ' ORDER BY rowid')]),
|
2026-12-04 08:58:53 -08:00
|
|
|
);
|
|
|
|
|
}
|
2026-11-01 23:40:44 +08:00
|
|
|
function enableYesterday(f) {
|
|
|
|
|
const env = {
|
|
|
|
|
day,
|
|
|
|
|
money,
|
|
|
|
|
weight,
|
|
|
|
|
text,
|
|
|
|
|
fail,
|
|
|
|
|
today: () => P,
|
|
|
|
|
now: () => P + 'T10:00:00.000Z',
|
|
|
|
|
pockets: ['现货', '金料', '回收旧料'],
|
|
|
|
|
selfPockets: ['现货', '金料', '回收旧料'],
|
|
|
|
|
};
|
|
|
|
|
const preview = Rolling.previewEnable(f.l, { date: P }, f.actors.owner, env);
|
|
|
|
|
return f.run({}, () =>
|
|
|
|
|
Rolling.enable(f.l, { date: P, revision: preview.revision, verified: true }, f.actors.owner, env),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
test('滚动池删除购料先重算旧料费,整数分归属守恒,失败全表回滚且重试不重复', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.buy('1', '800');
|
|
|
|
|
const removed = f.buy('0.620', '542');
|
|
|
|
|
f.enable();
|
|
|
|
|
const id = f.issue('1'),
|
|
|
|
|
flows = actualFlows(f.l),
|
|
|
|
|
order = f.l.detail(id);
|
|
|
|
|
assert.equal(order.lines[0].costParts.material, 82840);
|
|
|
|
|
assert.equal(f.l.materialPools().current.totalAmount, 134200);
|
|
|
|
|
const before = contents(f.l),
|
|
|
|
|
audit = f.l.audit;
|
|
|
|
|
f.l.audit = function (entity, action, ...rest) {
|
|
|
|
|
audit.call(this, entity, action, ...rest);
|
|
|
|
|
if (action === '删除实际购料') throw new Error('合成删除末尾异常');
|
|
|
|
|
};
|
|
|
|
|
assert.throws(() => f.l.deleteMaterialPurchase(removed.id, { revision: 1 }, f.actors.owner), /合成删除末尾异常/);
|
|
|
|
|
assert.equal(contents(f.l), before);
|
|
|
|
|
f.l.audit = audit;
|
|
|
|
|
assert.equal(f.l.materialPools().current.remainingCost, 51360);
|
|
|
|
|
assert.equal(f.l.detail(id).lines[0].costParts.material, 82840);
|
|
|
|
|
const input = { revision: 1 },
|
|
|
|
|
key = randomUUID(),
|
|
|
|
|
result = f.run(input, () => f.l.deleteMaterialPurchase(removed.id, input, f.actors.owner), 'owner', key);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
f.run(input, () => f.l.deleteMaterialPurchase(removed.id, input, f.actors.owner), 'owner', key),
|
|
|
|
|
result,
|
|
|
|
|
);
|
|
|
|
|
const pool = f.l.materialPools().current,
|
|
|
|
|
usage = pool.usages.find(u => u.order_id === id),
|
|
|
|
|
after = f.l.detail(id);
|
|
|
|
|
assert.equal(pool.totalMg, 1000);
|
|
|
|
|
assert.equal(pool.totalAmount, 80000);
|
|
|
|
|
assert.equal(pool.consumedCost, 80000);
|
|
|
|
|
assert.equal(pool.remainingCost, 0);
|
|
|
|
|
assert.equal(pool.remainingMg, 0);
|
|
|
|
|
assert.deepEqual(pool.remainingAllocations, []);
|
|
|
|
|
assert.equal(usage.amount, 80000);
|
|
|
|
|
assert.equal(usage.owner_amount, 80000);
|
|
|
|
|
assert.equal(after.lines[0].costParts.material, 80000);
|
|
|
|
|
assert.equal(after.lines[0].sale, order.lines[0].sale);
|
|
|
|
|
assert.equal(after.lines[0].quantity, order.lines[0].quantity);
|
|
|
|
|
assert.deepEqual(actualFlows(f.l), flows);
|
|
|
|
|
assert.equal(after.profit, order.profit + 2840);
|
|
|
|
|
assert.equal(after.total, order.total);
|
|
|
|
|
assert.equal(after.netReceived, order.netReceived);
|
|
|
|
|
const purchase = f.l.one('SELECT * FROM material_purchases WHERE id=?', removed.id);
|
|
|
|
|
assert.ok(purchase.deleted_at);
|
|
|
|
|
assert.equal(purchase.amount, 54200);
|
|
|
|
|
assert.equal(purchase.revision, 2);
|
|
|
|
|
assert.equal(
|
|
|
|
|
f.l.one(
|
|
|
|
|
"SELECT SUM(amount) AS amount FROM team_fund_cash WHERE source_kind='material_purchase' AND source_id=? AND kind='outlay'",
|
|
|
|
|
removed.id,
|
|
|
|
|
).amount,
|
2026-12-04 08:58:53 -08:00
|
|
|
0,
|
2026-11-01 23:40:44 +08:00
|
|
|
);
|
|
|
|
|
const saved = contents(f.l);
|
|
|
|
|
f.l.materialPoolDetail(D);
|
|
|
|
|
f.l.materialPools();
|
|
|
|
|
assert.equal(contents(f.l), saved, '恢复后普通读取不写入');
|
|
|
|
|
f.l.db.exec('SAVEPOINT synthetic_broken_usage');
|
|
|
|
|
try {
|
|
|
|
|
f.l.run('UPDATE material_pool_usages SET amount=80001 WHERE id=?', usage.id);
|
|
|
|
|
const broken = contents(f.l);
|
|
|
|
|
assert.throws(() => f.l.materialPoolDetail(D), /料池结转资金不守恒/);
|
|
|
|
|
assert.equal(contents(f.l), broken, '普通读取仍严格拒绝损坏金额,不静默重算');
|
|
|
|
|
} finally {
|
|
|
|
|
f.l.db.exec('ROLLBACK TO synthetic_broken_usage;RELEASE synthetic_broken_usage');
|
2026-12-04 08:58:53 -08:00
|
|
|
}
|
2026-11-01 23:40:44 +08:00
|
|
|
assert.equal(contents(f.l), saved);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('滚动池降价跨过旧耗用金额仍重算,三人付款FIFO与每一分余款可核对', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
first = f.buy('1', '800');
|
|
|
|
|
f.buy('0.620', '542', D, 'assistant', 'assistant');
|
|
|
|
|
f.buy('0.380', '300', D, 'assistant2', 'assistant2');
|
|
|
|
|
f.enable();
|
|
|
|
|
const a = f.issue('1'),
|
|
|
|
|
b = f.issue('0.5'),
|
|
|
|
|
flows = actualFlows(f.l);
|
|
|
|
|
assert.equal(f.l.materialPools().current.consumedCost, 123150);
|
|
|
|
|
const input = { revision: 1, date: D, grams: '1', total: '300', payer: 'owner', note: '合成核实降价' };
|
|
|
|
|
f.run(input, () => f.l.editMaterialPurchase(first.id, input, f.actors.owner));
|
|
|
|
|
const pool = f.l.materialPools().current,
|
|
|
|
|
ua = pool.usages.find(u => u.order_id === a),
|
|
|
|
|
ub = pool.usages.find(u => u.order_id === b);
|
|
|
|
|
assert.equal(pool.totalAmount, 114200);
|
|
|
|
|
assert.equal(pool.consumedCost, 85650);
|
|
|
|
|
assert.equal(pool.remainingCost, 28550);
|
|
|
|
|
assert.equal(pool.remainingMg, 500);
|
|
|
|
|
assert.deepEqual([ua.amount, ua.owner_amount, ua.assistant_amount, ua.assistant2_amount], [57100, 30000, 27100, 0]);
|
|
|
|
|
assert.deepEqual([ub.amount, ub.owner_amount, ub.assistant_amount, ub.assistant2_amount], [28550, 0, 27100, 1450]);
|
|
|
|
|
assert.deepEqual(pool.byPayer, {
|
|
|
|
|
owner: { purchased: 30000, consumed: 30000, remaining: 0 },
|
|
|
|
|
assistant: { purchased: 54200, consumed: 54200, remaining: 0 },
|
|
|
|
|
assistant2: { purchased: 30000, consumed: 1450, remaining: 28550 },
|
|
|
|
|
});
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
pool.remainingAllocations.map(({ payer, amount }) => ({ payer, amount })),
|
|
|
|
|
[{ payer: 'assistant2', amount: 28550 }],
|
|
|
|
|
);
|
|
|
|
|
assert.equal(f.l.detail(a).lines[0].costParts.material, 57100);
|
|
|
|
|
assert.equal(f.l.detail(b).lines[0].costParts.material, 28550);
|
|
|
|
|
assert.deepEqual(actualFlows(f.l), flows);
|
|
|
|
|
assert.equal(
|
|
|
|
|
f.l.one(
|
|
|
|
|
"SELECT SUM(amount) AS amount FROM team_fund_cash WHERE source_kind='material_purchase' AND source_id=? AND kind='outlay'",
|
|
|
|
|
first.id,
|
|
|
|
|
).amount,
|
|
|
|
|
30000,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('滚动池删除造成缺料仍转待补,补购后恢复真实成本和付款归属', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.buy('0.5', '400');
|
|
|
|
|
const removed = f.buy('0.5', '450', D, 'assistant');
|
|
|
|
|
f.enable();
|
|
|
|
|
const id = f.issue('1'),
|
|
|
|
|
flows = actualFlows(f.l);
|
|
|
|
|
f.run({}, () => f.l.deleteMaterialPurchase(removed.id, { revision: 1 }, f.actors.owner));
|
|
|
|
|
let pool = f.l.materialPools().current;
|
|
|
|
|
assert.equal(pool.status, 'pending');
|
|
|
|
|
assert.equal(pool.shortageMg, 500);
|
|
|
|
|
assert.equal(pool.totalAmount, 40000);
|
|
|
|
|
assert.equal(pool.remainingCost, null);
|
|
|
|
|
assert.equal(pool.consumedCost, null);
|
|
|
|
|
assert.deepEqual(pool.remainingAllocations, []);
|
|
|
|
|
assert.equal(f.l.detail(id).lines[0].costParts.material, null);
|
|
|
|
|
assert.equal(f.l.detail(id).materialCostPending, true);
|
|
|
|
|
assert.equal(pool.usages[0].owner_amount, null);
|
|
|
|
|
f.buy('0.5', '300', D, 'assistant2');
|
|
|
|
|
pool = f.l.materialPools().current;
|
|
|
|
|
assert.equal(pool.status, 'exhausted');
|
|
|
|
|
assert.equal(pool.consumedCost, 70000);
|
|
|
|
|
assert.equal(pool.remainingCost, 0);
|
|
|
|
|
assert.equal(pool.shortageMg, 0);
|
|
|
|
|
assert.equal(f.l.detail(id).lines[0].costParts.material, 70000);
|
|
|
|
|
assert.equal(f.l.detail(id).materialCostPending, false);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
[pool.usages[0].owner_amount, pool.usages[0].assistant_amount, pool.usages[0].assistant2_amount],
|
|
|
|
|
[40000, 0, 30000],
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(actualFlows(f.l), flows);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('今日删除重算不改昨日冻结快照、订单和日报,期初按原值承接', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.buy('1', '800', P);
|
|
|
|
|
enableYesterday(f);
|
|
|
|
|
const prior = Rolling.withHistorical(f.l, () => f.issue('0.25', P, P));
|
|
|
|
|
const removed = f.buy('0.620', '542');
|
|
|
|
|
const id = f.issue('0.75'),
|
|
|
|
|
yesterday = f.l.detail(prior),
|
|
|
|
|
report = financial(f.report(P)),
|
|
|
|
|
frozen = f.l.one('SELECT * FROM material_rolling_days WHERE date=?', P);
|
|
|
|
|
assert.equal(frozen.frozen, 1);
|
|
|
|
|
assert.ok(f.l.detail(id).lines[0].costParts.material > 60000);
|
|
|
|
|
f.run({}, () => f.l.deleteMaterialPurchase(removed.id, { revision: 1 }, f.actors.owner));
|
|
|
|
|
const pool = f.l.materialPools().current;
|
|
|
|
|
assert.equal(pool.opening.mg, 750);
|
|
|
|
|
assert.equal(pool.opening.amount, 60000);
|
|
|
|
|
assert.equal(pool.consumedCost, 60000);
|
|
|
|
|
assert.equal(pool.remainingCost, 0);
|
|
|
|
|
assert.equal(f.l.detail(id).lines[0].costParts.material, 60000);
|
|
|
|
|
assert.deepEqual(f.l.detail(prior), yesterday);
|
|
|
|
|
assert.deepEqual(financial(f.report(P)), report);
|
|
|
|
|
assert.deepEqual(f.l.one('SELECT * FROM material_rolling_days WHERE date=?', P), frozen);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (const action of ['edit', 'delete'])
|
|
|
|
|
test(
|
|
|
|
|
'已结转购料' + (action === 'edit' ? '降价' : '删除') + '的申请试算回滚,批准重建后续结转,故障恢复且保留审批规则',
|
|
|
|
|
t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
first = f.buy('1', '800', P),
|
|
|
|
|
second = f.buy('0.620', '542', P, 'assistant');
|
|
|
|
|
enableYesterday(f);
|
|
|
|
|
const old = Rolling.withHistorical(f.l, () => f.issue('1', P, P)),
|
|
|
|
|
next = f.issue('0.5'),
|
|
|
|
|
flows = actualFlows(f.l);
|
|
|
|
|
const target = action === 'edit' ? first : second,
|
|
|
|
|
input = {
|
|
|
|
|
action,
|
|
|
|
|
revision: 1,
|
|
|
|
|
reason: '合成历史核实',
|
|
|
|
|
verified: true,
|
|
|
|
|
...(action === 'edit' ? { date: P, grams: '1', total: '200', payer: 'owner', note: '合成降价' } : {}),
|
|
|
|
|
};
|
|
|
|
|
const original = contents(f.l);
|
|
|
|
|
assert.throws(() => f.l.deleteMaterialPurchase(target.id, { revision: 1 }, f.actors.owner), /冻结/);
|
|
|
|
|
assert.equal(contents(f.l), original);
|
|
|
|
|
const before = poolFacts(f.l),
|
|
|
|
|
request = f.run(input, () => f.l.requestMaterialPurchaseChange(target.id, input, f.actors.owner));
|
|
|
|
|
assert.deepEqual(poolFacts(f.l), before, '申请只保留试算结果,原账所有资金与快照不变');
|
|
|
|
|
assert.equal(request.status, 'pending');
|
|
|
|
|
assert.ok(request.preview.relatedOrders.some(o => o.id === old));
|
|
|
|
|
assert.ok(request.preview.relatedOrders.some(o => o.id === next));
|
|
|
|
|
const pending = contents(f.l),
|
|
|
|
|
audit = f.l.audit;
|
|
|
|
|
f.l.audit = function (entity, kind, ...rest) {
|
|
|
|
|
audit.call(this, entity, kind, ...rest);
|
|
|
|
|
if (kind === '批准历史购料更正') throw new Error('合成历史降价删除末尾异常');
|
|
|
|
|
};
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.l.approveMaterialPurchaseChange(request.id, { revision: 1, verified: true }, f.actors.owner),
|
|
|
|
|
/合成历史降价删除末尾异常/,
|
|
|
|
|
);
|
|
|
|
|
assert.equal(contents(f.l), pending);
|
|
|
|
|
assert.equal(f.l.rollingHistorical, undefined);
|
|
|
|
|
assert.equal(f.l.rollingRebuilding, undefined);
|
|
|
|
|
f.l.audit = audit;
|
|
|
|
|
assert.equal(f.l.materialPools().current.totalAmount, 51360);
|
|
|
|
|
assert.equal(f.l.detail(old).lines[0].costParts.material, 82840);
|
|
|
|
|
const approved = { revision: 1, verified: true, note: '合成核对通过' },
|
|
|
|
|
key = randomUUID(),
|
|
|
|
|
result = f.run(
|
|
|
|
|
approved,
|
|
|
|
|
() => f.l.approveMaterialPurchaseChange(request.id, approved, f.actors.owner),
|
|
|
|
|
'owner',
|
|
|
|
|
key,
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
f.run(approved, () => f.l.approveMaterialPurchaseChange(request.id, approved, f.actors.owner), 'owner', key),
|
|
|
|
|
result,
|
2026-12-04 08:58:53 -08:00
|
|
|
);
|
2026-11-01 23:40:44 +08:00
|
|
|
assert.equal(result.status, 'approved');
|
|
|
|
|
assert.deepEqual(actualFlows(f.l), flows);
|
|
|
|
|
const pool = f.l.materialPools().current,
|
|
|
|
|
yesterday = f.l.materialPoolDetail(P);
|
|
|
|
|
assert.equal(yesterday.frozen, true);
|
|
|
|
|
if (action === 'delete') {
|
|
|
|
|
assert.equal(f.l.detail(old).lines[0].costParts.material, 80000);
|
|
|
|
|
assert.equal(yesterday.totalAmount, 80000);
|
|
|
|
|
assert.equal(yesterday.remainingCost, 0);
|
|
|
|
|
assert.equal(pool.opening.mg, 0);
|
|
|
|
|
assert.equal(pool.opening.amount, 0);
|
|
|
|
|
assert.equal(pool.status, 'pending');
|
|
|
|
|
assert.equal(pool.shortageMg, 500);
|
|
|
|
|
assert.equal(f.l.detail(next).lines[0].costParts.material, null);
|
|
|
|
|
} else {
|
|
|
|
|
assert.equal(f.l.detail(old).lines[0].costParts.material, 45802);
|
|
|
|
|
assert.equal(yesterday.totalAmount, 74200);
|
|
|
|
|
assert.equal(yesterday.remainingCost, 28398);
|
|
|
|
|
assert.equal(pool.opening.mg, 620);
|
|
|
|
|
assert.equal(pool.opening.amount, 28398);
|
|
|
|
|
assert.equal(pool.consumedCost, 22902);
|
|
|
|
|
assert.equal(pool.remainingCost, 5496);
|
|
|
|
|
assert.equal(f.l.detail(next).lines[0].costParts.material, 22902);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
pool.remainingAllocations.map(({ payer, amount }) => ({ payer, amount })),
|
|
|
|
|
[{ payer: 'assistant', amount: 5496 }],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
const stable = contents(f.l);
|
|
|
|
|
f.l.materialPools();
|
|
|
|
|
f.l.materialPoolDetail(P);
|
|
|
|
|
f.report(D);
|
|
|
|
|
assert.equal(contents(f.l), stable, '恢复后的普通读取不写入');
|
|
|
|
|
},
|
|
|
|
|
);
|
2026-10-26 17:07:26 +08:00
|
|
|
|
|
|
|
|
test('首次切换迁入今日旧耗用,昨日三人订单日报复核转账冻结,今日补购仅改今日', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.buy('100', '90000', P, 'owner');
|
|
|
|
|
const old = f.issue('10', P, P);
|
|
|
|
|
const current = f.issue('20', D, P);
|
|
|
|
|
f.buy('100', '100000', D, 'assistant');
|
|
|
|
|
f.run({}, () =>
|
|
|
|
|
f.l.transferTeamFunds(
|
2026-12-04 08:58:53 -08:00
|
|
|
{
|
2026-10-26 17:07:26 +08:00
|
|
|
date: P,
|
|
|
|
|
fromMember: 'owner',
|
|
|
|
|
toMember: 'assistant',
|
|
|
|
|
amount: '10',
|
|
|
|
|
revision: f.l.teamFunds({ date: P }, f.actors.owner).revision,
|
|
|
|
|
note: '合成历史实转',
|
|
|
|
|
verified: true,
|
|
|
|
|
},
|
|
|
|
|
f.actors.owner,
|
2026-12-04 08:58:53 -08:00
|
|
|
),
|
2026-10-26 17:07:26 +08:00
|
|
|
);
|
|
|
|
|
f.run({}, () => f.l.closeDay(P, f.actors.owner, 'orderDate'));
|
|
|
|
|
const oldOrder = f.l.detail(old),
|
|
|
|
|
oldReport = f.report(P),
|
|
|
|
|
close = f.l.one('SELECT * FROM closes WHERE date=?', P),
|
|
|
|
|
before = contents(f.l),
|
|
|
|
|
preview = f.l.rollingMaterialPreview({}, f.actors.owner);
|
|
|
|
|
assert.equal(preview.canEnable, true);
|
|
|
|
|
assert.equal(preview.affectedOrderCount, 1);
|
|
|
|
|
assert.equal(preview.opening.mg, 90000);
|
|
|
|
|
assert.equal(preview.opening.amount, 8100000);
|
|
|
|
|
assert.ok(preview.relatedOrders.some(r => r.id === current));
|
|
|
|
|
assert.equal(contents(f.l), before, '启用试算完全回滚');
|
|
|
|
|
const result = f.enable();
|
|
|
|
|
assert.equal(result.current.totalMg, 190000);
|
|
|
|
|
assert.equal(f.l.detail(current).lines[0].costMaterial.poolId, D);
|
|
|
|
|
assert.deepEqual(f.l.detail(old), oldOrder);
|
|
|
|
|
assert.deepEqual(financial(f.report(P)), financial(oldReport));
|
|
|
|
|
assert.deepEqual(f.l.one('SELECT * FROM closes WHERE date=?', P), close);
|
|
|
|
|
f.buy('20', '16000', D, 'assistant2');
|
|
|
|
|
f.buy('10', '9500', D, 'owner');
|
|
|
|
|
assert.deepEqual(f.l.detail(old), oldOrder);
|
|
|
|
|
assert.deepEqual(financial(f.report(P)), financial(oldReport));
|
|
|
|
|
assert.deepEqual(f.l.one('SELECT * FROM closes WHERE date=?', P), close);
|
|
|
|
|
assert.equal(f.l.rows('SELECT * FROM team_fund_transfers').length, 1);
|
|
|
|
|
assert.equal(f.l.rows('SELECT * FROM material_purchases').length, 4);
|
|
|
|
|
const snapshot = contents(f.l);
|
|
|
|
|
for (let i = 0; i < 3; i++) {
|
|
|
|
|
f.l.materialPools();
|
|
|
|
|
f.l.materialPoolDetail(D);
|
|
|
|
|
f.report(P);
|
|
|
|
|
f.report(D);
|
2026-12-04 08:58:53 -08:00
|
|
|
}
|
2026-10-26 17:07:26 +08:00
|
|
|
assert.equal(contents(f.l), snapshot, 'GET投影无持久写入');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('跨日结转冻结昨日滚动快照,今日无采购也能用料,三方FIFO与整数分守恒', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.buy('1', '600', P2, 'owner');
|
|
|
|
|
const env = {
|
|
|
|
|
day,
|
|
|
|
|
money,
|
|
|
|
|
weight,
|
|
|
|
|
text,
|
|
|
|
|
fail,
|
|
|
|
|
today: () => P,
|
|
|
|
|
now: () => P + 'T10:00:00.000Z',
|
|
|
|
|
pockets: ['现货', '金料', '回收旧料'],
|
|
|
|
|
selfPockets: ['现货', '金料', '回收旧料'],
|
|
|
|
|
};
|
|
|
|
|
const p = Rolling.previewEnable(f.l, { date: P }, f.actors.owner, env);
|
|
|
|
|
f.run({}, () => Rolling.enable(f.l, { date: P, revision: p.revision, verified: true }, f.actors.owner, env));
|
|
|
|
|
const prior = Rolling.withHistorical(f.l, () => f.issue('0.1', P, P));
|
|
|
|
|
const priorOrder = f.l.detail(prior),
|
|
|
|
|
priorReport = f.report(P);
|
|
|
|
|
f.buy('1', '700', D, 'assistant');
|
|
|
|
|
f.buy('1', '800', D, 'assistant2');
|
|
|
|
|
const id = f.issue('2.9');
|
|
|
|
|
const pool = f.l.materialPools().current,
|
|
|
|
|
o = f.l.detail(id),
|
|
|
|
|
u = pool.usages.find(u => u.order_id === id);
|
|
|
|
|
assert.equal(pool.opening.mg, 900);
|
|
|
|
|
assert.equal(pool.opening.amount, 54000);
|
|
|
|
|
assert.equal(pool.remainingMg, 0);
|
|
|
|
|
assert.equal(pool.remainingCost, 0);
|
|
|
|
|
assert.equal(u.amount, 204000);
|
|
|
|
|
assert.equal(u.owner_amount, 54000);
|
|
|
|
|
assert.equal(u.assistant_amount, 70000);
|
|
|
|
|
assert.equal(u.assistant2_amount, 80000);
|
|
|
|
|
assert.equal(o.lines[0].costParts.material, 204000);
|
|
|
|
|
assert.deepEqual(f.l.detail(prior), priorOrder);
|
|
|
|
|
assert.deepEqual(financial(f.report(P)), financial(priorReport));
|
|
|
|
|
assert.equal(f.l.materialPoolDetail(P).frozen, true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('同日池料销售退回内部净抵销,跨日回料与手填回料按原成本且不新增付款', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.buy('1', '600', P);
|
|
|
|
|
const old = f.issue('0.4', P, P);
|
|
|
|
|
f.enable();
|
|
|
|
|
const before = f.l.materialPools().current;
|
|
|
|
|
f.action(old, 'return', { reason: '合成跨日退回' });
|
|
|
|
|
let pool = f.l.materialPools().current;
|
|
|
|
|
assert.equal(pool.todayReturns.mg, 400);
|
|
|
|
|
assert.equal(pool.todayReturns.amount, 24000);
|
|
|
|
|
assert.equal(pool.totalMg, 1000);
|
|
|
|
|
assert.equal(pool.totalAmount, 60000);
|
|
|
|
|
assert.equal(pool.returns[0].cashAmount, 0);
|
|
|
|
|
const id = f.issue('0.5'),
|
|
|
|
|
sold = f.l.detail(id).lines[0].costParts.material;
|
|
|
|
|
f.action(id, 'return', { reason: '合成同日全退' });
|
|
|
|
|
pool = f.l.materialPools().current;
|
|
|
|
|
assert.equal(pool.totalMg, 1000);
|
|
|
|
|
assert.equal(pool.totalAmount, 60000);
|
|
|
|
|
assert.equal(pool.usedMg, 0);
|
|
|
|
|
assert.equal(pool.remainingMg, 1000);
|
|
|
|
|
assert.equal(pool.remainingCost, 60000);
|
|
|
|
|
assert.equal(pool.averageRate, 60000);
|
|
|
|
|
assert.ok(pool.returns.some(r => r.order_id === id && r.sameDay && r.amount === sold));
|
|
|
|
|
const manual = f.issue('0.2', P, P, 'owner', true);
|
|
|
|
|
f.action(manual, 'return', { reason: '合成手填料费回料' });
|
|
|
|
|
pool = f.l.materialPools().current;
|
|
|
|
|
assert.equal(pool.totalMg, 1200);
|
|
|
|
|
assert.equal(pool.totalAmount, 78000);
|
|
|
|
|
assert.equal(pool.todayReturns.amount, 42000);
|
|
|
|
|
assert.equal(f.l.rows('SELECT * FROM material_purchases').length, 1);
|
|
|
|
|
assert.equal(
|
|
|
|
|
f.l.rows("SELECT * FROM team_fund_cash WHERE kind='outlay'").length,
|
|
|
|
|
2,
|
|
|
|
|
'仅原真实采购和手填实际成本付款',
|
|
|
|
|
);
|
|
|
|
|
assert.equal(before.opening.mg, 600);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('手填退供应商及银饰不入自有料池,回料来源在后续普通补购中保持原值', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.buy('1', '600', P);
|
|
|
|
|
f.enable();
|
2026-11-30 15:40:40 +08:00
|
|
|
const input = {
|
|
|
|
|
date: P,
|
|
|
|
|
customer: '合成供应商退货',
|
|
|
|
|
internal_note: '测试内部备注',
|
|
|
|
|
salesperson: '合成开单人',
|
|
|
|
|
payment_account: '麦麦微信',
|
|
|
|
|
lines: [
|
2026-12-04 08:58:53 -08:00
|
|
|
{
|
2026-11-30 15:40:40 +08:00
|
|
|
name: '足金999',
|
|
|
|
|
unit: 'g',
|
|
|
|
|
quantity: '0.2',
|
|
|
|
|
pocket: '',
|
|
|
|
|
payer: 'owner',
|
|
|
|
|
sale: '200',
|
|
|
|
|
costParts: { material: '120', labor: '0', accessories: '0', other: '0' },
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
|
|
|
|
],
|
2026-11-30 15:40:40 +08:00
|
|
|
fee_items: [],
|
|
|
|
|
};
|
2026-10-26 17:07:26 +08:00
|
|
|
const id = f.run(input, () => f.l.issueOrder(input, f.actors.owner));
|
|
|
|
|
f.action(id.id, 'return', { reason: '已退供应商', returnPockets: [''] });
|
|
|
|
|
assert.equal(f.l.materialPools().current.returns.length, 0);
|
|
|
|
|
for (const pocket of ['臻金汇', '金金', '定制工厂', '找金', '君宝汇']) {
|
|
|
|
|
const saved = f.run(input, () => f.l.issueOrder(input, f.actors.owner)),
|
|
|
|
|
order = f.l.rawOrder(saved.id);
|
|
|
|
|
order.lines[0].pocket = pocket;
|
|
|
|
|
f.l.run('UPDATE orders SET lines=? WHERE id=?', JSON.stringify(order.lines), saved.id);
|
|
|
|
|
f.action(saved.id, 'return', { reason: '退回供应商原货盘' });
|
|
|
|
|
assert.equal(f.l.materialPools().current.returns.length, 0, pocket);
|
|
|
|
|
}
|
|
|
|
|
const own = f.issue('0.2', D, D, 'owner', true);
|
|
|
|
|
f.action(own, 'return', { reason: '回自有库存' });
|
|
|
|
|
const returns = JSON.stringify(f.l.rows('SELECT * FROM material_rolling_returns ORDER BY rowid'));
|
|
|
|
|
f.buy('1', '900');
|
|
|
|
|
assert.equal(JSON.stringify(f.l.rows('SELECT * FROM material_rolling_returns ORDER BY rowid')), returns);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('历史更正直接调用在末尾审计异常时全表回滚,已冻结滚动日回料成本与付款人同步追溯', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
purchase = f.buy('1', '600', P2),
|
|
|
|
|
env = {
|
|
|
|
|
day,
|
|
|
|
|
money,
|
|
|
|
|
weight,
|
|
|
|
|
text,
|
|
|
|
|
fail,
|
|
|
|
|
today: () => P,
|
|
|
|
|
now: () => P + 'T10:00:00.000Z',
|
|
|
|
|
pockets: ['现货', '金料', '回收旧料'],
|
|
|
|
|
selfPockets: ['现货', '金料', '回收旧料'],
|
|
|
|
|
};
|
|
|
|
|
const preview = Rolling.previewEnable(f.l, { date: P }, f.actors.owner, env);
|
|
|
|
|
f.run({}, () => Rolling.enable(f.l, { date: P, revision: preview.revision, verified: true }, f.actors.owner, env));
|
|
|
|
|
const old = Rolling.withHistorical(f.l, () => f.issue('0.4', P, P));
|
|
|
|
|
f.action(old, 'return', { reason: '次日回料' });
|
|
|
|
|
const next = f.issue('0.5');
|
|
|
|
|
const data = {
|
|
|
|
|
action: 'edit',
|
|
|
|
|
revision: 1,
|
|
|
|
|
reason: '历史原料价更正',
|
|
|
|
|
verified: true,
|
|
|
|
|
date: P2,
|
|
|
|
|
grams: '1',
|
|
|
|
|
total: '700',
|
|
|
|
|
payer: 'assistant2',
|
|
|
|
|
note: '',
|
|
|
|
|
};
|
|
|
|
|
const request = f.run(data, () => f.l.requestMaterialPurchaseChange(purchase.id, data, f.actors.owner));
|
|
|
|
|
const before = contents(f.l),
|
|
|
|
|
audit = f.l.audit;
|
|
|
|
|
f.l.audit = function (entity, action, ...rest) {
|
|
|
|
|
audit.call(this, entity, action, ...rest);
|
|
|
|
|
if (action === '批准历史购料更正') throw new Error('合成批准末尾异常');
|
|
|
|
|
};
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.l.approveMaterialPurchaseChange(request.id, { revision: 1, verified: true, note: '' }, f.actors.owner),
|
|
|
|
|
/合成批准末尾异常/,
|
|
|
|
|
);
|
|
|
|
|
assert.equal(contents(f.l), before);
|
|
|
|
|
f.l.audit = audit;
|
|
|
|
|
f.run({}, () =>
|
|
|
|
|
f.l.approveMaterialPurchaseChange(request.id, { revision: 1, verified: true, note: '' }, f.actors.owner),
|
|
|
|
|
);
|
|
|
|
|
assert.equal(f.l.one('SELECT amount FROM material_rolling_returns WHERE order_id=?', old).amount, 28000);
|
|
|
|
|
assert.equal(f.l.detail(next).lines[0].costParts.material, 35000);
|
|
|
|
|
assert.equal(f.l.materialPools().current.opening.amount, 42000);
|
|
|
|
|
assert.equal(f.l.materialPools().current.totalAmount, 70000);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('历史购料申请试算回滚,麦麦批准同步已退来源和后续成本/付款人,快照保留过期', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
purchase = f.buy('1', '600', P2);
|
|
|
|
|
const old = f.issue('0.4', P, P2);
|
|
|
|
|
f.enable();
|
|
|
|
|
f.action(old, 'return', { reason: '合成跨日退回' });
|
|
|
|
|
const next = f.issue('0.5');
|
|
|
|
|
f.run({}, () => f.l.closeDay(P, f.actors.owner, 'orderDate'));
|
|
|
|
|
const before = contents(f.l),
|
|
|
|
|
data = {
|
|
|
|
|
action: 'edit',
|
|
|
|
|
revision: 1,
|
|
|
|
|
reason: '核对原购料',
|
|
|
|
|
verified: true,
|
|
|
|
|
date: P2,
|
|
|
|
|
grams: '1',
|
|
|
|
|
total: '700',
|
|
|
|
|
payer: 'assistant2',
|
|
|
|
|
note: '合成更正',
|
|
|
|
|
};
|
|
|
|
|
const request = f.run(data, () => f.l.requestMaterialPurchaseChange(purchase.id, data, f.actors.owner));
|
|
|
|
|
assert.equal(request.stale, false);
|
|
|
|
|
assert.ok(request.preview.relatedOrders.some(o => o.id === old));
|
|
|
|
|
assert.ok(request.preview.relatedOrders.some(o => o.id === next));
|
|
|
|
|
assert.equal(f.l.detail(next).lines[0].costParts.material, 30000);
|
|
|
|
|
assert.equal(f.l.one('SELECT amount FROM material_purchases WHERE id=?', purchase.id).amount, 60000);
|
|
|
|
|
assert.equal(f.l.one('SELECT amount FROM material_rolling_returns WHERE order_id=?', old).amount, 24000);
|
|
|
|
|
f.run({}, () =>
|
|
|
|
|
f.l.approveMaterialPurchaseChange(request.id, { revision: 1, verified: true, note: '核实通过' }, f.actors.owner),
|
|
|
|
|
);
|
|
|
|
|
assert.equal(f.l.one('SELECT amount FROM material_rolling_returns WHERE order_id=?', old).amount, 28000);
|
|
|
|
|
assert.equal(f.l.detail(next).lines[0].costParts.material, 35000);
|
|
|
|
|
assert.equal(
|
|
|
|
|
f.l.one(
|
|
|
|
|
'SELECT assistant2_amount FROM material_usage_parties WHERE usage_id=(SELECT id FROM material_pool_usages WHERE order_id=?)',
|
|
|
|
|
next,
|
|
|
|
|
).assistant2_amount,
|
|
|
|
|
35000,
|
|
|
|
|
);
|
|
|
|
|
assert.equal(f.l.materialPurchaseChange(request.id, f.actors.owner).status, 'approved');
|
|
|
|
|
assert.equal(f.l.one('SELECT stale FROM closes WHERE date=?', P).stale, 1);
|
|
|
|
|
assert.ok(f.l.one('SELECT report FROM closes WHERE date=?', P).report);
|
|
|
|
|
assert.notEqual(contents(f.l), before);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('麦麦核实后仍可直接修改自己或助理历史池料订单,重建结转并恢复权限,助理仍须审批', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.buy('10', '6000', P2);
|
|
|
|
|
const old = f.issue('1', P, P2);
|
|
|
|
|
f.enable();
|
|
|
|
|
const current = f.issue('1');
|
|
|
|
|
const info = f.l.orderEdit(old, f.actors.owner),
|
|
|
|
|
input = {
|
|
|
|
|
...info.inputs,
|
|
|
|
|
revision: info.revision,
|
|
|
|
|
reason: '核对原实称克重',
|
|
|
|
|
verified: true,
|
|
|
|
|
lines: info.inputs.lines.map(line => ({ ...line, quantity: '2' })),
|
|
|
|
|
};
|
|
|
|
|
const before = contents(f.l);
|
|
|
|
|
assert.throws(() => f.l.editOrder(old, { ...input, verified: false }, f.actors.owner), /核实/);
|
|
|
|
|
assert.equal(contents(f.l), before);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.l.editOrder(old, input, f.actors.assistant),
|
|
|
|
|
error => error.status === 403 && /提交修改申请/.test(error.message),
|
|
|
|
|
);
|
|
|
|
|
assert.equal(contents(f.l), before);
|
|
|
|
|
const audit = f.l.audit;
|
|
|
|
|
f.l.audit = function (entity, action, ...rest) {
|
|
|
|
|
audit.call(this, entity, action, ...rest);
|
|
|
|
|
if (action === '麦麦直接复核历史订单修改') throw new Error('合成历史改单末尾异常');
|
|
|
|
|
};
|
|
|
|
|
assert.throws(() => f.l.editOrder(old, input, f.actors.owner), /合成历史改单末尾异常/);
|
|
|
|
|
assert.equal(contents(f.l), before);
|
|
|
|
|
assert.equal(f.l.rollingHistorical, undefined);
|
|
|
|
|
assert.equal(f.l.rollingRebuilding, undefined);
|
|
|
|
|
f.l.audit = audit;
|
|
|
|
|
f.run(input, () => f.l.editOrder(old, input, f.actors.owner));
|
|
|
|
|
const pool = f.l.materialPools().current;
|
|
|
|
|
assert.equal(f.l.detail(old).lines[0].quantity, 2000);
|
|
|
|
|
assert.equal(f.l.detail(old).lines[0].costParts.material, 120000);
|
|
|
|
|
assert.equal(pool.totalMg, 8000);
|
|
|
|
|
assert.equal(pool.totalAmount, 480000);
|
|
|
|
|
assert.equal(pool.remainingMg, 7000);
|
|
|
|
|
assert.equal(pool.remainingCost, 420000);
|
|
|
|
|
assert.equal(f.l.detail(current).lines[0].costParts.material, 60000);
|
|
|
|
|
assert.equal(f.l.rows('SELECT * FROM material_purchases').length, 1);
|
|
|
|
|
assert.equal(f.l.rows("SELECT * FROM audit WHERE action='麦麦直接复核历史订单修改'").length, 1);
|
|
|
|
|
assert.equal(f.l.rollingHistorical, undefined);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('待批申请遇新购料立即过期,助理不能批准历史,启用幂等与中断全回滚', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
purchase = f.buy('1', '600', P, 'owner', 'assistant');
|
|
|
|
|
const preview = f.l.rollingMaterialPreview({}, f.actors.owner),
|
|
|
|
|
input = { date: D, revision: preview.revision, verified: true },
|
|
|
|
|
key = randomUUID();
|
|
|
|
|
const original = f.l.audit;
|
|
|
|
|
f.l.audit = function (entity, ...args) {
|
|
|
|
|
if (entity === 'system:rolling-material') throw new Error('合成启用故障');
|
|
|
|
|
return original.call(this, entity, ...args);
|
|
|
|
|
};
|
|
|
|
|
const before = contents(f.l);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.run(input, () => f.l.enableRollingMaterial(input, f.actors.owner), 'owner', key),
|
|
|
|
|
/合成启用故障/,
|
|
|
|
|
);
|
|
|
|
|
assert.equal(contents(f.l), before);
|
|
|
|
|
f.l.audit = original;
|
|
|
|
|
const first = f.run(input, () => f.l.enableRollingMaterial(input, f.actors.owner), 'owner', key);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
f.run(input, () => f.l.enableRollingMaterial(input, f.actors.owner), 'owner', key),
|
|
|
|
|
first,
|
|
|
|
|
);
|
|
|
|
|
const change = {
|
|
|
|
|
action: 'edit',
|
|
|
|
|
revision: 1,
|
|
|
|
|
reason: '合成申请',
|
|
|
|
|
verified: true,
|
|
|
|
|
date: P,
|
|
|
|
|
grams: '1',
|
|
|
|
|
total: '650',
|
|
|
|
|
payer: 'owner',
|
|
|
|
|
note: '',
|
|
|
|
|
};
|
|
|
|
|
const request = f.run(
|
|
|
|
|
change,
|
|
|
|
|
() => f.l.requestMaterialPurchaseChange(purchase.id, change, f.actors.assistant),
|
|
|
|
|
'assistant',
|
|
|
|
|
);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() =>
|
|
|
|
|
f.run(
|
|
|
|
|
{},
|
|
|
|
|
() => f.l.approveMaterialPurchaseChange(request.id, { revision: 1, verified: true }, f.actors.assistant),
|
|
|
|
|
'assistant',
|
|
|
|
|
),
|
|
|
|
|
/麦麦/,
|
|
|
|
|
);
|
|
|
|
|
f.buy('1', '700');
|
|
|
|
|
assert.equal(f.l.materialPurchaseChange(request.id, f.actors.owner).stale, true);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() =>
|
|
|
|
|
f.run({}, () => f.l.approveMaterialPurchaseChange(request.id, { revision: 1, verified: true }, f.actors.owner)),
|
|
|
|
|
/过期/,
|
|
|
|
|
);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.run({}, () => f.l.editMaterialPurchase(purchase.id, { ...change, total: '800' }, f.actors.owner)),
|
|
|
|
|
/冻结/,
|
2026-12-04 08:58:53 -08:00
|
|
|
);
|
2026-10-26 17:07:26 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('隔离HTTP启用、麦麦历史直改和购料申请复核完整贯通,权限与重复提交保护有效', async t => {
|
|
|
|
|
const fs = require('node:fs'),
|
|
|
|
|
os = require('node:os'),
|
|
|
|
|
path = require('node:path'),
|
|
|
|
|
{ createApp } = require('../server');
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
purchase = f.buy('10', '6000', P2, 'owner', 'assistant'),
|
|
|
|
|
old = f.issue('1', P, P2),
|
|
|
|
|
current = f.issue('1', D, P2);
|
|
|
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'maimai-rolling-http-')),
|
|
|
|
|
app = createApp({ mode: 'trial', port: 0, dataDir: dir, ledger: f.l, feishuConfig: { enabled: false } });
|
2026-12-04 08:52:50 -08:00
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
|
|
|
});
|
2026-10-26 17:07:26 +08:00
|
|
|
const base = 'http://127.0.0.1:' + app.server.address().port,
|
|
|
|
|
cookies = {};
|
|
|
|
|
const call = async (member, route, data, key = randomUUID()) => {
|
|
|
|
|
const response = await fetch(base + route, {
|
|
|
|
|
method: data === undefined ? 'GET' : 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
Cookie: cookies[member] ?? '',
|
|
|
|
|
...(data === undefined
|
|
|
|
|
? {}
|
2026-12-04 08:58:53 -08:00
|
|
|
: {
|
2026-10-26 17:07:26 +08:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
Origin: base,
|
|
|
|
|
'X-Requested-With': 'maimai-ledger',
|
|
|
|
|
'Idempotency-Key': key,
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
...(data === undefined ? {} : { body: JSON.stringify(data) }),
|
|
|
|
|
});
|
|
|
|
|
return {
|
|
|
|
|
status: response.status,
|
|
|
|
|
body: await response.json(),
|
|
|
|
|
cookie: response.headers.get('set-cookie')?.split(';')[0],
|
|
|
|
|
};
|
2026-12-04 08:58:53 -08:00
|
|
|
};
|
2026-10-26 17:07:26 +08:00
|
|
|
for (const member of ['owner', 'assistant']) {
|
|
|
|
|
const login = await call(member, '/api/login', {
|
|
|
|
|
name: member === 'owner' ? f.actors.owner.name : '助理1',
|
|
|
|
|
password: 'synthetic-rolling-password',
|
|
|
|
|
});
|
|
|
|
|
assert.equal(login.status, 200);
|
|
|
|
|
cookies[member] = login.cookie;
|
|
|
|
|
}
|
|
|
|
|
assert.equal((await call('assistant', '/api/material-pools/rolling-preview')).status, 403);
|
|
|
|
|
const before = contents(f.l),
|
|
|
|
|
preview = await call('owner', '/api/material-pools/rolling-preview');
|
|
|
|
|
assert.equal(preview.status, 200);
|
|
|
|
|
assert.equal(contents(f.l), before);
|
|
|
|
|
const enable = { date: D, revision: preview.body.revision, verified: true },
|
|
|
|
|
enableKey = randomUUID(),
|
|
|
|
|
enabled = await call('owner', '/api/material-pools/rolling-enable', enable, enableKey);
|
|
|
|
|
assert.equal(enabled.status, 200, JSON.stringify(enabled.body));
|
|
|
|
|
assert.deepEqual((await call('owner', '/api/material-pools/rolling-enable', enable, enableKey)).body, enabled.body);
|
|
|
|
|
const route = '/api/orders/' + old + '/edit',
|
|
|
|
|
info = (await call('owner', route)).body,
|
|
|
|
|
input = {
|
|
|
|
|
...info.inputs,
|
|
|
|
|
revision: info.revision,
|
|
|
|
|
reason: 'HTTP复核原克重',
|
|
|
|
|
verified: true,
|
|
|
|
|
lines: info.inputs.lines.map(line => ({ ...line, quantity: '2' })),
|
|
|
|
|
};
|
|
|
|
|
assert.equal((await call('assistant', route, input)).status, 403);
|
|
|
|
|
const editKey = randomUUID(),
|
|
|
|
|
edited = await call('owner', route, input, editKey);
|
|
|
|
|
assert.equal(edited.status, 200, JSON.stringify(edited.body));
|
|
|
|
|
assert.deepEqual((await call('owner', route, input, editKey)).body, edited.body);
|
|
|
|
|
assert.equal((await call('owner', route, input)).status, 409);
|
|
|
|
|
assert.equal((await call('owner', '/api/material-pools')).body.current.totalMg, 8000);
|
|
|
|
|
assert.equal(f.l.detail(current).lines[0].costParts.material, 60000);
|
|
|
|
|
const correction = {
|
|
|
|
|
action: 'edit',
|
|
|
|
|
revision: 1,
|
|
|
|
|
reason: 'HTTP核实原购料金額',
|
|
|
|
|
verified: true,
|
|
|
|
|
date: P2,
|
|
|
|
|
grams: '10',
|
|
|
|
|
total: '7000',
|
|
|
|
|
payer: 'owner',
|
|
|
|
|
note: '原金额录错',
|
|
|
|
|
};
|
|
|
|
|
assert.equal((await call('owner', '/api/material-purchases/' + purchase.id + '/edit', correction)).status, 409);
|
|
|
|
|
const requested = await call('assistant', '/api/material-purchases/' + purchase.id + '/change', correction);
|
|
|
|
|
assert.equal(requested.status, 200, JSON.stringify(requested.body));
|
|
|
|
|
const id = requested.body.id;
|
|
|
|
|
const list = await call('owner', '/api/material-purchase-changes');
|
|
|
|
|
assert.equal(list.body.counts.pending, 1);
|
|
|
|
|
assert.equal(list.body.rows[0].id, id);
|
|
|
|
|
assert.ok(list.body.rows[0].preview.relatedOrders.some(row => row.id === current));
|
|
|
|
|
const approve = { revision: 1, verified: true, note: 'HTTP核对凭据和影响' },
|
|
|
|
|
approveKey = randomUUID();
|
|
|
|
|
assert.equal((await call('assistant', '/api/material-purchase-changes/' + id + '/approve', approve)).status, 403);
|
|
|
|
|
const approved = await call('owner', '/api/material-purchase-changes/' + id + '/approve', approve, approveKey);
|
|
|
|
|
assert.equal(approved.status, 200, JSON.stringify(approved.body));
|
|
|
|
|
assert.equal(approved.body.status, 'approved');
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
(await call('owner', '/api/material-purchase-changes/' + id + '/approve', approve, approveKey)).body,
|
|
|
|
|
approved.body,
|
|
|
|
|
);
|
|
|
|
|
const pool = (await call('owner', '/api/material-pools')).body.current;
|
|
|
|
|
assert.equal(pool.totalAmount, 560000);
|
|
|
|
|
assert.equal(pool.remainingCost, 490000);
|
|
|
|
|
assert.equal(f.l.detail(old).lines[0].costParts.material, 140000);
|
|
|
|
|
assert.equal(f.l.detail(current).lines[0].costParts.material, 70000);
|
|
|
|
|
const next = await call('assistant', '/api/material-purchases/' + purchase.id + '/change', {
|
|
|
|
|
...correction,
|
|
|
|
|
revision: 2,
|
|
|
|
|
total: '7500',
|
|
|
|
|
});
|
|
|
|
|
assert.equal(next.status, 200);
|
|
|
|
|
const rejected = await call('owner', '/api/material-purchase-changes/' + next.body.id + '/reject', {
|
|
|
|
|
note: '核对后保留当前金额',
|
|
|
|
|
});
|
|
|
|
|
assert.equal(rejected.status, 200);
|
|
|
|
|
assert.equal(rejected.body.status, 'rejected');
|
|
|
|
|
assert.equal(f.l.rows('SELECT * FROM material_purchases').length, 1);
|
|
|
|
|
});
|