2026-11-04 22:18:56 +08:00
|
|
|
'use strict';
|
|
|
|
|
const { test } = require('node:test'),
|
|
|
|
|
assert = require('node:assert/strict'),
|
|
|
|
|
{ randomUUID } = require('node:crypto'),
|
|
|
|
|
fs = require('node:fs'),
|
|
|
|
|
os = require('node:os'),
|
|
|
|
|
path = require('node:path');
|
2026-12-04 13:01:19 -08:00
|
|
|
const { Ledger, today } = require('../core/ledger'),
|
2026-11-04 22:18:56 +08:00
|
|
|
{ createApp } = require('../server');
|
|
|
|
|
const D = '2026-10-20',
|
|
|
|
|
D2 = '2026-10-21',
|
|
|
|
|
old = (grams = '10', purity = '100') => [{ name: '合成旧料', incoming_weight: grams, melted_weight: grams, purity }];
|
|
|
|
|
const direct = (extra = {}) => ({
|
|
|
|
|
mode: 'direct',
|
|
|
|
|
date: D,
|
|
|
|
|
customer: '合成回收客户',
|
|
|
|
|
salesperson: '合成经办',
|
|
|
|
|
old_lines: old(),
|
|
|
|
|
buy_rate: '900',
|
|
|
|
|
payer: 'owner',
|
|
|
|
|
payment_account: '麦麦微信',
|
|
|
|
|
verified: true,
|
|
|
|
|
...extra,
|
|
|
|
|
});
|
|
|
|
|
const exchange = (extra = {}, line = {}) =>
|
|
|
|
|
direct({
|
|
|
|
|
mode: 'exchange',
|
|
|
|
|
old_lines: old('20'),
|
|
|
|
|
exchange: {
|
|
|
|
|
name: '足金合成新货',
|
|
|
|
|
quantity: '30',
|
|
|
|
|
pocket: '现货',
|
|
|
|
|
gold_price: '1000',
|
|
|
|
|
labor_rate: '20',
|
|
|
|
|
labor_mode: 'gram',
|
|
|
|
|
depreciation_rate: '10',
|
|
|
|
|
cost_material: { mode: 'gram', rate: '900' },
|
|
|
|
|
cost_labor: { mode: 'gram', rate: '5' },
|
|
|
|
|
cost_payer: 'owner',
|
|
|
|
|
discount: '0',
|
|
|
|
|
rounding_mode: 'cent',
|
|
|
|
|
...line,
|
|
|
|
|
},
|
|
|
|
|
...extra,
|
|
|
|
|
});
|
|
|
|
|
function fixture(t) {
|
|
|
|
|
const l = new Ledger(':memory:'),
|
|
|
|
|
actors = {};
|
|
|
|
|
t.after(() => l.close());
|
|
|
|
|
for (const member of ['owner', 'assistant', 'assistant2']) {
|
|
|
|
|
const actor = { id: randomUUID(), name: '合成' + member, role: member === 'owner' ? 'owner' : 'assistant' };
|
|
|
|
|
actors[member] = actor;
|
|
|
|
|
l.run('INSERT INTO users VALUES (?,?,?,?,?)', actor.id, actor.name, actor.role, 'synthetic-salt', 'synthetic-hash');
|
|
|
|
|
l.bindMemberUser(actor.id, member);
|
|
|
|
|
}
|
|
|
|
|
l.enableTeamMode();
|
|
|
|
|
const run = (body, fn, member = 'owner', key = randomUUID()) =>
|
|
|
|
|
l.scope(actors[member], () => l.transact(key, actors[member], body, fn));
|
|
|
|
|
const create = (body = direct(), member = 'assistant') =>
|
|
|
|
|
run(body, () => l.createRecovery(body, actors[member]), member);
|
|
|
|
|
const info = (id, member = 'owner') => l.recoveryEdit(id, actors[member]);
|
|
|
|
|
const update = (id, extra = {}, member = 'owner', key = randomUUID()) => {
|
|
|
|
|
const value = { ...info(id, member).inputs, revision: info(id, member).revision, reason: '合成更正原因', ...extra };
|
|
|
|
|
return run({ id, value }, () => l.updateRecovery(id, value, actors[member]), member, key);
|
|
|
|
|
};
|
|
|
|
|
const cancel = (id, extra = {}, member = 'owner') => {
|
|
|
|
|
const value = { revision: info(id, member).revision, reason: '合成录错取消', verified: true, ...extra };
|
|
|
|
|
return run({ id, value }, () => l.cancelRecovery(id, value, actors[member]), member);
|
|
|
|
|
};
|
|
|
|
|
const approve = (request, extra = {}, key = randomUUID()) => {
|
|
|
|
|
const value = { revision: request.revision, verified: true, ...extra };
|
|
|
|
|
return run({ id: request.id, value }, () => l.approveRecoveryChange(request.id, value, actors.owner), 'owner', key);
|
|
|
|
|
};
|
|
|
|
|
const reject = request =>
|
|
|
|
|
run({ reject: request.id }, () =>
|
|
|
|
|
l.rejectRecoveryChange(request.id, { revision: request.revision, note: '合成驳回' }, actors.owner),
|
|
|
|
|
);
|
|
|
|
|
const stock = (date = D) =>
|
|
|
|
|
run({ date }, () =>
|
|
|
|
|
l.stock({ date, kind: 'opening', pocket: '现货', grams: '100', note: '合成库存' }, actors.owner),
|
2026-12-04 08:58:53 -08:00
|
|
|
);
|
2026-11-04 22:18:56 +08:00
|
|
|
const funds = () => l.teamFunds({ from: D, to: today() }, actors.owner),
|
|
|
|
|
report = date => l.asActor(actors.owner).reportFor({ date, basis: 'orderDate' }, actors.owner);
|
|
|
|
|
return { l, actors, run, create, info, update, cancel, approve, reject, stock, funds, report };
|
|
|
|
|
}
|
|
|
|
|
const contents = (
|
|
|
|
|
l,
|
|
|
|
|
tables = l.rows("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name").map(r => r.name),
|
|
|
|
|
) => JSON.stringify(tables.map(table => [table, l.rows('SELECT * FROM "' + table + '"')]));
|
|
|
|
|
const financial = [
|
|
|
|
|
'recoveries',
|
|
|
|
|
'material_purchases',
|
|
|
|
|
'recovery_purchase_sources',
|
|
|
|
|
'material_pool_usages',
|
|
|
|
|
'payments',
|
|
|
|
|
'expenses',
|
|
|
|
|
'journal',
|
|
|
|
|
'movements',
|
|
|
|
|
'team_fund_sources',
|
|
|
|
|
'team_fund_entries',
|
|
|
|
|
'team_fund_cash',
|
|
|
|
|
'closes',
|
|
|
|
|
'inventory_snapshots',
|
|
|
|
|
];
|
2026-12-04 08:58:53 -08:00
|
|
|
|
2026-11-24 21:11:32 +08:00
|
|
|
test('旧料可入找金和自定义盘,已耗用个人料只换实物去向不改采购、耗用或付款', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
r = f.create(direct({ old_pocket: '找金', old_lines: old('10', '99') })),
|
|
|
|
|
inv = key => f.l.inventory().find(row => row.pocket === key);
|
|
|
|
|
assert.equal(r.oldPocket, '找金');
|
|
|
|
|
assert.equal(inv('找金').mg, 9900);
|
|
|
|
|
assert.equal(inv('回收旧料').mg, 0);
|
|
|
|
|
assert.equal(f.l.pocket('找金').self, false);
|
|
|
|
|
const use = {
|
|
|
|
|
date: D,
|
|
|
|
|
member: 'assistant',
|
|
|
|
|
customer: '合成用料客户',
|
|
|
|
|
salesperson: '合成开单人',
|
|
|
|
|
payment_account: '麦麦微信',
|
|
|
|
|
lines: [
|
2026-12-04 08:58:53 -08:00
|
|
|
{
|
2026-11-24 21:11:32 +08:00
|
|
|
name: '足金999',
|
|
|
|
|
unit: 'g',
|
|
|
|
|
quantity: '5',
|
|
|
|
|
pocket: '',
|
|
|
|
|
payer: 'owner',
|
|
|
|
|
pricingMode: 'manual',
|
|
|
|
|
sale: '5000',
|
|
|
|
|
costParts: { material: '', labor: '0', accessories: '0', other: '0' },
|
|
|
|
|
costMaterial: { mode: 'pool', poolId: D },
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
|
|
|
|
],
|
2026-11-24 21:11:32 +08:00
|
|
|
fee_items: [],
|
|
|
|
|
};
|
|
|
|
|
f.run(use, () => f.l.issueOrder(use, f.actors.owner));
|
|
|
|
|
const pocket = f.run({}, () => f.l.createPocket({ name: '合成回收存放盘' }, f.actors.owner)).pocket;
|
|
|
|
|
const tables = [
|
|
|
|
|
'orders',
|
|
|
|
|
'material_purchases',
|
|
|
|
|
'recovery_purchase_sources',
|
|
|
|
|
'material_pool_usages',
|
|
|
|
|
'payments',
|
|
|
|
|
'expenses',
|
|
|
|
|
'journal',
|
|
|
|
|
'team_fund_entries',
|
|
|
|
|
'team_fund_cash',
|
|
|
|
|
],
|
|
|
|
|
before = contents(f.l, tables),
|
|
|
|
|
pool = f.l.materialPoolDetail(D).personalPools.assistant;
|
|
|
|
|
assert.equal(pool.usedMg, 5000);
|
|
|
|
|
f.update(r.id, { old_pocket: pocket.key });
|
|
|
|
|
assert.equal(inv('找金').mg, 0);
|
|
|
|
|
assert.equal(inv(pocket.key).mg, 9900);
|
|
|
|
|
assert.equal(contents(f.l, tables), before);
|
|
|
|
|
assert.equal(f.l.materialPoolDetail(D).personalPools.assistant.remainingMg, pool.remainingMg);
|
|
|
|
|
assert.equal(f.info(r.id).inputs.old_pocket, pocket.key);
|
|
|
|
|
assert.equal(f.info(r.id).history[0].before.old_pocket, '找金');
|
|
|
|
|
assert.equal(f.info(r.id).history[0].after.old_pocket, pocket.key);
|
|
|
|
|
f.cancel(r.id);
|
|
|
|
|
assert.equal(inv(pocket.key).mg, 0);
|
|
|
|
|
assert.equal(inv('找金').mg, 0);
|
|
|
|
|
assert.equal(inv('回收旧料').mg, 0);
|
|
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
|
2026-11-24 21:11:32 +08:00
|
|
|
test('置换旧料入库与新货出库独立,改旧料盘不重记财务,取消两侧回到原库存', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.stock();
|
|
|
|
|
const r = f.create(exchange({ old_pocket: '找金' })),
|
|
|
|
|
inv = key => f.l.inventory().find(row => row.pocket === key).mg;
|
|
|
|
|
assert.equal(inv('找金'), 20000);
|
|
|
|
|
assert.equal(inv('现货'), 70000);
|
|
|
|
|
assert.equal(inv('回收旧料'), 0);
|
|
|
|
|
const tables = [
|
|
|
|
|
'orders',
|
|
|
|
|
'material_purchases',
|
|
|
|
|
'recovery_purchase_sources',
|
|
|
|
|
'material_pool_usages',
|
|
|
|
|
'payments',
|
|
|
|
|
'expenses',
|
|
|
|
|
'journal',
|
|
|
|
|
'team_fund_entries',
|
|
|
|
|
'team_fund_cash',
|
|
|
|
|
],
|
|
|
|
|
before = contents(f.l, tables);
|
|
|
|
|
f.update(r.id, { old_pocket: '金料' });
|
|
|
|
|
assert.equal(inv('找金'), 0);
|
|
|
|
|
assert.equal(inv('金料'), 20000);
|
|
|
|
|
assert.equal(inv('现货'), 70000);
|
|
|
|
|
assert.equal(contents(f.l, tables), before);
|
|
|
|
|
f.cancel(r.id);
|
|
|
|
|
assert.equal(inv('金料'), 0);
|
|
|
|
|
assert.equal(inv('找金'), 0);
|
|
|
|
|
assert.equal(inv('现货'), 100000);
|
|
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
|
2026-11-24 21:11:32 +08:00
|
|
|
test('历史缺字段仍属回收旧料且只读不迁移,停用原盘可沿用,历史助理改盘和取消须审批', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
r = f.create(direct({ old_lines: old('10', '99') })),
|
|
|
|
|
row = f.l.one('SELECT data FROM recoveries WHERE id=?', r.id),
|
|
|
|
|
legacy = JSON.parse(row.data);
|
|
|
|
|
delete legacy.oldPocket;
|
|
|
|
|
f.l.run('UPDATE recoveries SET data=? WHERE id=?', JSON.stringify(legacy), r.id);
|
|
|
|
|
const saved = f.l.one('SELECT data FROM recoveries WHERE id=?', r.id).data;
|
|
|
|
|
assert.equal(f.l.recovery(r.id, f.actors.owner).oldPocket, '回收旧料');
|
|
|
|
|
assert.equal(f.info(r.id).inputs.old_pocket, '回收旧料');
|
|
|
|
|
assert.equal(f.l.one('SELECT data FROM recoveries WHERE id=?', r.id).data, saved);
|
|
|
|
|
f.run({}, () =>
|
|
|
|
|
f.l.updatePocket('回收旧料', { revision: f.l.pocket('回收旧料').revision, active: false }, f.actors.owner),
|
|
|
|
|
);
|
|
|
|
|
assert.throws(() => f.create(), /停用/);
|
|
|
|
|
assert.throws(() => f.create(direct({ old_pocket: '不存在' })), /货盘/);
|
|
|
|
|
const movements = f.l.rows('SELECT * FROM movements');
|
|
|
|
|
f.update(r.id, { customer: '只改客户' });
|
|
|
|
|
assert.deepEqual(f.l.rows('SELECT * FROM movements'), movements);
|
|
|
|
|
const applied = f.update(r.id, { old_pocket: '找金' }, 'assistant');
|
|
|
|
|
assert.equal(applied.pending, true);
|
|
|
|
|
assert.equal(f.l.inventory().find(row => row.pocket === '回收旧料').mg, 9900);
|
|
|
|
|
f.approve(applied.request);
|
|
|
|
|
assert.equal(f.l.inventory().find(row => row.pocket === '回收旧料').mg, 0);
|
|
|
|
|
assert.equal(f.l.inventory().find(row => row.pocket === '找金').mg, 9900);
|
|
|
|
|
const cancel = f.cancel(r.id, {}, 'assistant');
|
|
|
|
|
assert.equal(cancel.pending, true);
|
|
|
|
|
assert.equal(f.l.inventory().find(row => row.pocket === '找金').mg, 9900);
|
|
|
|
|
f.approve(cancel.request);
|
|
|
|
|
assert.equal(f.l.inventory().find(row => row.pocket === '找金').mg, 0);
|
|
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
|
2026-11-24 21:11:32 +08:00
|
|
|
test('计对旧料盘独立校验入库数量,修改冲回原对数,置换两侧数量分别记录和取消', t => {
|
|
|
|
|
t.mock.timers.enable({ apis: ['Date'], now: Date.parse(D + 'T04:00:00Z') });
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
pocket = f.run({}, () =>
|
|
|
|
|
f.l.createPocket(
|
|
|
|
|
{ name: '合成按对回收盘', countUnit: 'pair', initialCount: 0, countVerified: true },
|
|
|
|
|
f.actors.owner,
|
2026-12-04 08:58:53 -08:00
|
|
|
),
|
2026-11-24 21:11:32 +08:00
|
|
|
).pocket,
|
|
|
|
|
inv = () => f.l.inventory().find(row => row.pocket === pocket.key);
|
|
|
|
|
for (const old_stock_count of [undefined, null, '', 0, 1.5, '2.0']) {
|
|
|
|
|
const before = contents(f.l);
|
|
|
|
|
assert.throws(() => f.create(direct({ old_pocket: pocket.key, old_stock_count })), /数量|整数/);
|
|
|
|
|
assert.equal(contents(f.l), before);
|
|
|
|
|
}
|
|
|
|
|
assert.throws(() => f.create(direct({ date: '2026-10-19', old_pocket: pocket.key, old_stock_count: 1 })), /盘点基准/);
|
|
|
|
|
const r = f.create(direct({ old_pocket: pocket.key, old_stock_count: 3 }));
|
|
|
|
|
assert.equal(inv().mg, 10000);
|
|
|
|
|
assert.equal(inv().count, 3);
|
|
|
|
|
assert.equal(f.info(r.id).inputs.old_stock_count, 3);
|
|
|
|
|
const purchase = f.l.one('SELECT * FROM material_purchases WHERE id=?', r.purchase_id);
|
|
|
|
|
f.update(r.id, { old_stock_count: 2 });
|
|
|
|
|
assert.equal(inv().count, 2);
|
|
|
|
|
assert.deepEqual(f.l.one('SELECT * FROM material_purchases WHERE id=?', r.purchase_id), purchase);
|
|
|
|
|
f.update(r.id, { old_pocket: '找金', old_stock_count: '' });
|
|
|
|
|
assert.equal(inv().count, 0);
|
|
|
|
|
assert.equal(inv().mg, 0);
|
|
|
|
|
f.cancel(r.id);
|
|
|
|
|
assert.equal(f.l.inventory().find(row => row.pocket === '找金').mg, 0);
|
|
|
|
|
const swap = f.create(
|
|
|
|
|
exchange(
|
|
|
|
|
{ old_lines: old('10'), old_pocket: pocket.key, old_stock_count: 3 },
|
|
|
|
|
{ quantity: '5', pocket: pocket.key, stockCount: 2 },
|
2026-12-04 08:58:53 -08:00
|
|
|
),
|
2026-11-24 21:11:32 +08:00
|
|
|
);
|
|
|
|
|
assert.equal(inv().mg, 5000);
|
|
|
|
|
assert.equal(inv().count, 1);
|
|
|
|
|
assert.equal(swap.oldStockCount, 3);
|
|
|
|
|
assert.equal(swap.newProduct.stockCount, 2);
|
|
|
|
|
f.cancel(swap.id);
|
|
|
|
|
assert.equal(inv().mg, 0);
|
|
|
|
|
assert.equal(inv().count, 0);
|
|
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
|
2026-11-04 22:18:56 +08:00
|
|
|
test('直接回收原位改重量、金额、日期和付款人,库存与现金仅保留新有效值,原身份和流水保留', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
r = f.create(direct({ old_lines: old('10', '99'), buy_total: '8999.91' })),
|
|
|
|
|
original = f.l.one('SELECT * FROM recoveries WHERE id=?', r.id),
|
|
|
|
|
movement = f.l.one('SELECT * FROM movements WHERE group_id=?', original.inventory_group);
|
|
|
|
|
for (const date of [D, D2]) f.run({ date }, () => f.l.closeDay(date, f.actors.owner, 'orderDate'));
|
|
|
|
|
const updated = f.update(r.id, {
|
|
|
|
|
date: D2,
|
|
|
|
|
old_lines: old('10.100', '99'),
|
|
|
|
|
buy_total: '9000.21',
|
|
|
|
|
payer: 'assistant2',
|
|
|
|
|
payment_account: '助理2现金',
|
|
|
|
|
}),
|
|
|
|
|
current = f.l.one('SELECT * FROM recoveries WHERE id=?', r.id);
|
|
|
|
|
assert.equal(updated.applied, true);
|
|
|
|
|
assert.equal(updated.pending, false);
|
|
|
|
|
assert.equal(current.id, original.id);
|
|
|
|
|
assert.equal(current.number, original.number);
|
|
|
|
|
assert.equal(current.created, original.created);
|
|
|
|
|
assert.equal(current.actor, original.actor);
|
|
|
|
|
assert.equal(current.member, 'assistant');
|
|
|
|
|
assert.equal(current.revision, 2);
|
|
|
|
|
assert.equal(current.purchase_id, r.purchase_id);
|
|
|
|
|
assert.equal(f.l.inventory().find(x => x.pocket === '回收旧料').mg, 9999);
|
|
|
|
|
assert.deepEqual(f.l.one('SELECT * FROM movements WHERE id=?', movement.id), movement);
|
|
|
|
|
assert.equal(f.l.rows('SELECT * FROM orders').length, 0);
|
|
|
|
|
const purchase = f.l.one('SELECT * FROM material_purchases WHERE id=?', r.purchase_id);
|
|
|
|
|
assert.equal(purchase.amount, 900021);
|
|
|
|
|
assert.equal(purchase.mg, 9999);
|
|
|
|
|
assert.equal(purchase.payer, 'assistant2');
|
|
|
|
|
assert.equal(purchase.date, D2);
|
|
|
|
|
assert.equal(f.report(D).outlayTotal, 0);
|
|
|
|
|
assert.equal(f.report(D2).outlayTotal, 900021);
|
|
|
|
|
const balances = f.funds().balances;
|
|
|
|
|
assert.equal(balances.find(x => x.member === 'owner').outlays, 0);
|
|
|
|
|
assert.equal(balances.find(x => x.member === 'assistant2').outlays, 900021);
|
|
|
|
|
assert.equal(f.report(D).close.stale, true);
|
|
|
|
|
assert.equal(f.report(D2).close.stale, true);
|
|
|
|
|
assert.equal(f.info(r.id).history.length, 1);
|
|
|
|
|
assert.equal(f.info(r.id).history[0].before.buy_total, '8999.91');
|
|
|
|
|
assert.equal(f.info(r.id).history[0].after.buy_total, '9000.21');
|
|
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
|
2026-11-04 22:18:56 +08:00
|
|
|
test('直接回收取消保留原单和软删除采购,库存现金归零,有效列表统计撤回且不可重复取消', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
r = f.create(),
|
|
|
|
|
before = f.l.rows('SELECT * FROM movements'),
|
|
|
|
|
result = f.cancel(r.id);
|
|
|
|
|
assert.equal(result.recovery.status, 'cancelled');
|
|
|
|
|
assert.equal(result.recovery.canEdit, false);
|
|
|
|
|
assert.equal(f.l.inventory().find(x => x.pocket === '回收旧料').mg, 0);
|
|
|
|
|
assert.ok(f.l.one('SELECT deleted_at FROM material_purchases WHERE id=?', r.purchase_id).deleted_at);
|
|
|
|
|
assert.equal(f.report(D).outlayTotal, 0);
|
|
|
|
|
assert.equal(f.funds().balances.find(x => x.member === 'owner').outlays, 0);
|
|
|
|
|
for (const row of before) assert.deepEqual(f.l.one('SELECT * FROM movements WHERE id=?', row.id), row);
|
|
|
|
|
const list = f.l.recoveries({ date: D }, f.actors.owner);
|
|
|
|
|
assert.equal(list.rows.length, 1);
|
|
|
|
|
assert.equal(list.summary.count, 0);
|
|
|
|
|
assert.equal(list.summary.cancelledCount, 1);
|
|
|
|
|
assert.equal(f.l.orderBook({ date: D }).recoveryCount, 0);
|
|
|
|
|
assert.equal(f.l.publicRecovery(r.id, f.actors.owner).status, 'cancelled');
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.cancel(r.id),
|
|
|
|
|
error => error.status === 409,
|
|
|
|
|
);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.update(r.id, { buy_total: '1' }),
|
|
|
|
|
error => error.status === 409,
|
|
|
|
|
);
|
|
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
|
2026-11-04 22:18:56 +08:00
|
|
|
test('置换更正重算收款、成本、利润和两侧库存,同一关联订单不重开,现金可从应收变应付再恢复', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.stock();
|
|
|
|
|
const r = f.create(exchange()),
|
|
|
|
|
order = f.l.detail(r.order_id),
|
|
|
|
|
originalIds = f.l.rows('SELECT * FROM payments WHERE order_id=?', r.order_id);
|
|
|
|
|
const initial = f.info(r.id).inputs,
|
|
|
|
|
updated = f.update(r.id, {
|
|
|
|
|
old_lines: old('22'),
|
|
|
|
|
exchange: { ...initial.exchange, quantity: '32', depreciation_weight: '22' },
|
|
|
|
|
}),
|
|
|
|
|
o = f.l.detail(r.order_id);
|
|
|
|
|
assert.equal(updated.recovery.order_id, r.order_id);
|
|
|
|
|
assert.equal(o.paper_number, order.paper_number);
|
|
|
|
|
assert.equal(o.number, order.number);
|
|
|
|
|
assert.equal(o.total, 1086000);
|
|
|
|
|
assert.equal(o.netReceived, 1086000);
|
|
|
|
|
assert.equal(o.goodsCost, 916000);
|
|
|
|
|
assert.equal(o.profit, 170000);
|
|
|
|
|
assert.equal(o.balance, 0);
|
|
|
|
|
assert.equal(f.l.inventory().find(x => x.pocket === '现货').mg, 68000);
|
|
|
|
|
assert.equal(f.l.inventory().find(x => x.pocket === '回收旧料').mg, 22000);
|
|
|
|
|
for (const row of originalIds) assert.deepEqual(f.l.one('SELECT * FROM payments WHERE id=?', row.id), row);
|
|
|
|
|
const pay = f.update(r.id, {
|
|
|
|
|
old_lines: old('35'),
|
|
|
|
|
buy_rate: '850',
|
|
|
|
|
buy_total: '12750',
|
|
|
|
|
exchange: { ...f.info(r.id).inputs.exchange, quantity: '20', depreciation_weight: '20' },
|
|
|
|
|
}).recovery;
|
|
|
|
|
assert.equal(pay.cashReceipt, 0);
|
|
|
|
|
assert.equal(pay.cashPayment, 1215000);
|
|
|
|
|
assert.equal(f.l.detail(r.order_id).netReceived, 0);
|
|
|
|
|
assert.equal(f.l.detail(r.order_id).nonCashOffset, 60000);
|
|
|
|
|
assert.equal(f.l.detail(r.order_id).balance, 0);
|
|
|
|
|
assert.equal(f.l.detail(r.order_id).profit, 50000);
|
|
|
|
|
assert.ok(pay.purchase_id);
|
|
|
|
|
const less = f.update(r.id, { old_lines: old('30'), buy_total: '8500' }).recovery;
|
|
|
|
|
assert.equal(less.purchase_id, pay.purchase_id);
|
|
|
|
|
assert.equal(less.cashPayment, 790000);
|
|
|
|
|
const back = f.update(r.id, {
|
|
|
|
|
old_lines: old('20'),
|
|
|
|
|
buy_total: '0',
|
|
|
|
|
exchange: {
|
|
|
|
|
...f.info(r.id).inputs.exchange,
|
|
|
|
|
quantity: '30',
|
|
|
|
|
depreciation_weight: '20',
|
|
|
|
|
cost_material: { mode: 'gram', rate: '900' },
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
2026-11-04 22:18:56 +08:00
|
|
|
}).recovery;
|
|
|
|
|
assert.equal(back.cashReceipt, 1080000);
|
|
|
|
|
assert.equal(back.cashPayment, 0);
|
|
|
|
|
assert.ok(f.l.one('SELECT deleted_at FROM material_purchases WHERE id=?', pay.purchase_id).deleted_at);
|
|
|
|
|
assert.equal(f.l.rows('SELECT * FROM orders').length, 1);
|
|
|
|
|
assert.equal(f.l.rows('SELECT * FROM material_purchases').length, 1);
|
|
|
|
|
assert.equal(f.report(D).sales, 1080000);
|
|
|
|
|
assert.equal(f.report(D).profit, 165000);
|
|
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
|
2026-11-04 22:18:56 +08:00
|
|
|
test('置换取消撤销关联费用、信用卡费、销售和收款,恢复库存且保留原支付费用及真实转账', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.stock();
|
|
|
|
|
const r = f.create(
|
|
|
|
|
exchange(
|
|
|
|
|
{ payment_account: '麦麦信用卡' },
|
|
|
|
|
{ fee_items: [{ key: 'other', note: '合成实际费用', amount: '3.21', payer: 'assistant2' }] },
|
|
|
|
|
),
|
2026-12-04 08:58:53 -08:00
|
|
|
),
|
2026-11-04 22:18:56 +08:00
|
|
|
oldRows = contents(f.l, ['payments', 'expenses']);
|
|
|
|
|
assert.ok(f.l.detail(r.order_id).directCost > 321);
|
|
|
|
|
const transfer = {
|
|
|
|
|
date: D,
|
|
|
|
|
fromMember: 'owner',
|
|
|
|
|
toMember: 'assistant',
|
|
|
|
|
amount: '1000',
|
|
|
|
|
verified: true,
|
|
|
|
|
revision: f.l.teamFunds({ date: D }, f.actors.owner).revision,
|
|
|
|
|
};
|
|
|
|
|
f.run(transfer, () => f.l.transferTeamFunds(transfer, f.actors.owner));
|
|
|
|
|
const transfers = contents(f.l, ['team_fund_transfers']);
|
|
|
|
|
f.cancel(r.id);
|
|
|
|
|
const o = f.l.detail(r.order_id);
|
|
|
|
|
assert.equal(o.status, 'cancelled');
|
|
|
|
|
assert.equal(o.netReceived, 0);
|
|
|
|
|
assert.equal(o.directCost, 0);
|
|
|
|
|
assert.equal(o.profit, 0);
|
|
|
|
|
assert.equal(f.l.inventory().find(x => x.pocket === '现货').mg, 100000);
|
|
|
|
|
assert.equal(f.l.inventory().find(x => x.pocket === '回收旧料').mg, 0);
|
|
|
|
|
assert.equal(f.report(D).sales, 0);
|
|
|
|
|
assert.equal(f.report(D).profit, 0);
|
|
|
|
|
assert.equal(contents(f.l, ['team_fund_transfers']), transfers);
|
|
|
|
|
for (const [table, rows] of JSON.parse(oldRows))
|
|
|
|
|
for (const row of rows) assert.deepEqual({ ...f.l.one('SELECT * FROM ' + table + ' WHERE id=?', row.id) }, row);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() =>
|
|
|
|
|
f.run({}, () =>
|
|
|
|
|
f.l.action(
|
|
|
|
|
r.order_id,
|
|
|
|
|
'receipt',
|
|
|
|
|
{ date: D, amount: '1', account: '麦麦微信', verified: true },
|
|
|
|
|
f.actors.owner,
|
|
|
|
|
),
|
2026-12-04 08:58:53 -08:00
|
|
|
),
|
2026-11-04 22:18:56 +08:00
|
|
|
/关联黄金回收/,
|
|
|
|
|
);
|
|
|
|
|
const pair = f.funds().pairs.find(row => row.id === 'owner:assistant');
|
|
|
|
|
assert.equal(pair.fromMember, 'assistant');
|
|
|
|
|
assert.equal(pair.due, 100000);
|
|
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
|
2026-11-04 22:18:56 +08:00
|
|
|
test('差重用料更正只调整差重而非新货全重,取消释放关联料池且不重复采购或支出', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.stock();
|
|
|
|
|
f.run({}, () =>
|
|
|
|
|
f.l.createMaterialPurchase(
|
|
|
|
|
{ date: D, grams: '50', total: '45000', payer: 'owner', note: '合成共享旧池' },
|
|
|
|
|
f.actors.owner,
|
2026-12-04 08:58:53 -08:00
|
|
|
),
|
2026-11-04 22:18:56 +08:00
|
|
|
);
|
|
|
|
|
const r = f.create(exchange({}, { cost_material: { mode: 'pool', poolId: D } }));
|
|
|
|
|
assert.equal(f.l.one('SELECT mg FROM material_pool_usages WHERE order_id=?', r.order_id).mg, 10000);
|
|
|
|
|
f.update(r.id, {
|
|
|
|
|
old_lines: old('21'),
|
|
|
|
|
exchange: { ...f.info(r.id).inputs.exchange, quantity: '32', depreciation_weight: '21' },
|
|
|
|
|
});
|
|
|
|
|
const usage = f.l.one('SELECT * FROM material_pool_usages WHERE order_id=?', r.order_id),
|
|
|
|
|
o = f.l.detail(r.order_id);
|
|
|
|
|
assert.equal(usage.mg, 11000);
|
|
|
|
|
assert.equal(o.lines[0].quantity, 32000);
|
|
|
|
|
assert.equal(o.lines[0].costParts.material, 990000);
|
|
|
|
|
assert.equal(o.goodsCost, 1006000);
|
|
|
|
|
assert.equal(o.profit, 179000);
|
|
|
|
|
assert.equal(f.l.materialPoolDetail(D).usedMg, 11000);
|
|
|
|
|
f.cancel(r.id);
|
|
|
|
|
assert.equal(f.l.one('SELECT active FROM material_pool_usages WHERE id=?', usage.id).active, 0);
|
|
|
|
|
assert.equal(f.l.materialPoolDetail(D).usedMg, 0);
|
|
|
|
|
assert.equal(f.l.materialPoolDetail(D).totalMg, 50000);
|
|
|
|
|
assert.equal(f.l.rows('SELECT * FROM material_purchases').length, 1);
|
|
|
|
|
assert.equal(f.l.detail(r.order_id).profit, 0);
|
|
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
|
2026-11-04 22:18:56 +08:00
|
|
|
test('助理原日及目标日均为北京今天可直接更正,否则申请,审批前有效账不变并拒绝重复审批', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
d = today(),
|
|
|
|
|
r = f.create(direct({ date: d }));
|
|
|
|
|
assert.equal(f.info(r.id, 'assistant').permissions.canDirect, true);
|
|
|
|
|
assert.equal(f.update(r.id, { buy_total: '9100' }, 'assistant').applied, true);
|
|
|
|
|
const before = contents(f.l, financial),
|
|
|
|
|
request = f.update(r.id, { date: D, buy_total: '9200' }, 'assistant').request;
|
|
|
|
|
assert.equal(request.status, 'pending');
|
|
|
|
|
assert.equal(contents(f.l, financial), before);
|
|
|
|
|
assert.equal(f.info(r.id).permissions.canEdit, false);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.update(r.id, { buy_total: '9300' }, 'assistant'),
|
|
|
|
|
error => error.status === 409,
|
|
|
|
|
);
|
|
|
|
|
const key = randomUUID(),
|
|
|
|
|
approved = f.approve(request, {}, key);
|
|
|
|
|
assert.equal(approved.status, 'approved');
|
|
|
|
|
assert.deepEqual(f.approve(request, {}, key), approved);
|
|
|
|
|
assert.equal(f.info(r.id).revision, 3);
|
|
|
|
|
assert.equal(f.info(r.id).record.date, D);
|
|
|
|
|
assert.equal(f.info(r.id).record.purchaseAmount, 920000);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.approve(request),
|
|
|
|
|
error => error.status === 409,
|
|
|
|
|
);
|
|
|
|
|
const historical = f.cancel(r.id, {}, 'assistant');
|
|
|
|
|
assert.equal(historical.pending, true);
|
|
|
|
|
assert.equal(f.info(r.id).record.status, 'active');
|
|
|
|
|
f.reject(historical.request);
|
|
|
|
|
assert.equal(f.info(r.id).record.status, 'active');
|
|
|
|
|
assert.equal(f.info(r.id).history.length, 3);
|
|
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
|
2026-11-04 22:18:56 +08:00
|
|
|
test('版本并发、原关联变化和伪造字段拒绝;助理按固定归属隔离详情、申请及编辑入口', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
r = f.create(),
|
|
|
|
|
foreign = f.create(direct({ customer: '其他成员私有客户' }), 'assistant2');
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.info(r.id, 'assistant2'),
|
|
|
|
|
error => error.status === 404,
|
|
|
|
|
);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.update(foreign.id, {}, 'assistant'),
|
|
|
|
|
error => error.status === 404,
|
|
|
|
|
);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.cancel(foreign.id, {}, 'assistant'),
|
|
|
|
|
error => error.status === 404,
|
|
|
|
|
);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.update(r.id, { member: 'assistant2' }),
|
|
|
|
|
error => error.status === 403,
|
|
|
|
|
);
|
|
|
|
|
assert.throws(() => f.update(r.id, { mode: 'exchange' }), /不能直接互转/);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.update(r.id, { revision: '1' }),
|
|
|
|
|
error => error.status === 409,
|
|
|
|
|
);
|
|
|
|
|
assert.throws(() => f.update(r.id, { verified: false }), /核实/);
|
|
|
|
|
const request = f.update(r.id, { buy_total: '9100' }, 'assistant').request;
|
|
|
|
|
assert.equal(f.l.recoveryChanges({}, f.actors.assistant).requests.length, 1);
|
|
|
|
|
assert.equal(f.l.recoveryChanges({}, f.actors.assistant2).requests.length, 0);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.l.recoveryChange(request.id, f.actors.assistant2),
|
|
|
|
|
error => error.status === 404,
|
|
|
|
|
);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.l.approveRecoveryChange(request.id, { revision: 1, verified: true }, f.actors.assistant),
|
|
|
|
|
error => error.status === 403,
|
|
|
|
|
);
|
|
|
|
|
f.l.run('UPDATE material_purchases SET revision=revision+1 WHERE id=?', r.purchase_id);
|
|
|
|
|
assert.equal(f.l.recoveryChange(request.id, f.actors.owner).stale, true);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.approve(request),
|
|
|
|
|
error => error.status === 409,
|
|
|
|
|
);
|
|
|
|
|
f.reject(request);
|
|
|
|
|
assert.equal(f.info(r.id).record.purchaseAmount, 900000);
|
|
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
|
2026-11-04 22:18:56 +08:00
|
|
|
test('库存历史不足与末尾异常完整回滚,审批失败保持pending与原账,metadata修改不重记现金', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
r = f.create(),
|
|
|
|
|
payments = contents(f.l, ['team_fund_entries', 'team_fund_cash', 'material_purchases', 'movements']);
|
|
|
|
|
f.update(r.id, { customer: '修正合成称呼', note: '修正资料' });
|
|
|
|
|
assert.equal(contents(f.l, ['team_fund_entries', 'team_fund_cash', 'material_purchases', 'movements']), payments);
|
|
|
|
|
f.run({}, () =>
|
|
|
|
|
f.l.stock({ date: D2, kind: 'decrease', pocket: '回收旧料', grams: '9', note: '合成后续出料' }, f.actors.owner),
|
|
|
|
|
);
|
|
|
|
|
let before = contents(f.l);
|
|
|
|
|
assert.throws(() => f.cancel(r.id), /库存不足/);
|
|
|
|
|
assert.equal(contents(f.l), before);
|
|
|
|
|
assert.throws(() => f.update(r.id, { date: D2, old_lines: old('1'), buy_total: '900' }), /库存不足/);
|
|
|
|
|
assert.equal(contents(f.l), before);
|
|
|
|
|
const request = f.update(r.id, { buy_total: '9100' }, 'assistant').request,
|
|
|
|
|
original = f.l.audit;
|
|
|
|
|
before = contents(f.l);
|
|
|
|
|
f.l.audit = function (entity, action, ...rest) {
|
|
|
|
|
if (action === '修改回收/置换记录') throw new Error('合成末尾异常');
|
|
|
|
|
return original.call(this, entity, action, ...rest);
|
|
|
|
|
};
|
|
|
|
|
try {
|
|
|
|
|
assert.throws(() => f.approve(request), /合成末尾异常/);
|
|
|
|
|
assert.equal(contents(f.l), before);
|
|
|
|
|
} finally {
|
|
|
|
|
f.l.audit = original;
|
2026-12-04 08:58:53 -08:00
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
assert.equal(f.info(r.id).pending.status, 'pending');
|
|
|
|
|
f.approve(request);
|
|
|
|
|
assert.equal(f.info(r.id).record.purchaseAmount, 910000);
|
|
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
|
2026-11-05 00:04:01 +08:00
|
|
|
test('已耗用回收料允许取消但减至已用量以下被拦,列出用料订单;改金额重估但不重复现金', t => {
|
2026-11-04 22:18:56 +08:00
|
|
|
const f = fixture(t),
|
|
|
|
|
r = f.create(),
|
|
|
|
|
input = {
|
|
|
|
|
date: D,
|
|
|
|
|
member: 'assistant',
|
|
|
|
|
customer: '合成用料订单',
|
|
|
|
|
salesperson: '合成经办',
|
|
|
|
|
payment_account: '助理微信',
|
|
|
|
|
rounding_mode: 'cent',
|
|
|
|
|
fee_items: [],
|
|
|
|
|
lines: [
|
2026-12-04 08:58:53 -08:00
|
|
|
{
|
2026-11-04 22:18:56 +08:00
|
|
|
name: '足金合成耗用',
|
|
|
|
|
unit: 'g',
|
|
|
|
|
quantity: '5',
|
|
|
|
|
pocket: '',
|
|
|
|
|
payer: 'owner',
|
|
|
|
|
sale: '10000',
|
|
|
|
|
costParts: { material: '', labor: '0', accessories: '0', other: '0' },
|
|
|
|
|
costMaterial: { mode: 'pool', poolId: D },
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
|
|
|
|
],
|
2026-11-04 22:18:56 +08:00
|
|
|
};
|
|
|
|
|
const order = f.run(input, () => f.l.issueOrder(input, f.actors.assistant), 'assistant').id,
|
|
|
|
|
before = contents(f.l);
|
2026-11-05 00:04:01 +08:00
|
|
|
assert.ok(f.info(r.id).blockedUsages.some(row => row.orderId === order));
|
2026-11-04 22:18:56 +08:00
|
|
|
assert.equal(contents(f.l), before);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.update(r.id, { old_lines: old('3'), buy_total: '2700' }),
|
|
|
|
|
error => error.status === 409 && error.blockedUsages.some(row => row.mg === 5000),
|
|
|
|
|
);
|
2026-11-05 00:04:01 +08:00
|
|
|
assert.equal(contents(f.l), before);
|
|
|
|
|
assert.equal(f.info(r.id).permissions.canCancel, true);
|
|
|
|
|
f.update(r.id, { buy_total: '12000' });
|
|
|
|
|
assert.equal(f.l.detail(order).goodsCost, 600000);
|
|
|
|
|
assert.equal(f.l.detail(order).profit, 400000);
|
|
|
|
|
assert.equal(f.report(D).outlayGoods, 1200000);
|
|
|
|
|
assert.equal(f.funds().balances.find(row => row.member === 'owner').outlays, 1200000);
|
2026-11-04 22:18:56 +08:00
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
|
2026-11-04 22:18:56 +08:00
|
|
|
test('滚动个人回收料更正沿原重建链重算本人订单成本,实际收款和转账不重写', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
d = today(),
|
|
|
|
|
r = f.create(direct({ date: d })),
|
|
|
|
|
preview = f.l.rollingMaterialPreview({ date: d }, f.actors.owner);
|
|
|
|
|
f.run(preview, () =>
|
|
|
|
|
f.l.enableRollingMaterial({ date: d, revision: preview.revision, verified: true }, f.actors.owner),
|
|
|
|
|
);
|
|
|
|
|
const input = {
|
|
|
|
|
date: d,
|
|
|
|
|
member: 'assistant',
|
|
|
|
|
customer: '合成滚动耗料',
|
|
|
|
|
salesperson: '合成经办',
|
|
|
|
|
payment_account: '助理微信',
|
|
|
|
|
rounding_mode: 'cent',
|
|
|
|
|
fee_items: [],
|
|
|
|
|
lines: [
|
2026-12-04 08:58:53 -08:00
|
|
|
{
|
2026-11-04 22:18:56 +08:00
|
|
|
name: '足金合成滚动',
|
|
|
|
|
unit: 'g',
|
|
|
|
|
quantity: '5',
|
|
|
|
|
pocket: '',
|
|
|
|
|
payer: 'owner',
|
|
|
|
|
sale: '10000',
|
|
|
|
|
costParts: { material: '', labor: '0', accessories: '0', other: '0' },
|
|
|
|
|
costMaterial: { mode: 'pool', poolId: d },
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
|
|
|
|
],
|
2026-11-04 22:18:56 +08:00
|
|
|
};
|
|
|
|
|
const id = f.run(input, () => f.l.issueOrder(input, f.actors.assistant), 'assistant').id,
|
|
|
|
|
payments = contents(f.l, ['payments', 'team_fund_transfers']);
|
|
|
|
|
f.update(r.id, { buy_total: '10000' });
|
|
|
|
|
assert.equal(f.l.detail(id).goodsCost, 500000);
|
|
|
|
|
assert.equal(f.l.detail(id).profit, 500000);
|
|
|
|
|
assert.equal(contents(f.l, ['payments', 'team_fund_transfers']), payments);
|
|
|
|
|
assert.equal(f.l.materialPoolDetail(d).personalPools.assistant.usedMg, 5000);
|
|
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
|
2026-11-04 22:18:56 +08:00
|
|
|
test('历史置换用料可只读试算并申请,麦麦批准沿旧池及结转重建,不把自己的原耗用计算两次', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.stock();
|
|
|
|
|
f.run({}, () =>
|
|
|
|
|
f.l.createMaterialPurchase(
|
|
|
|
|
{ date: D, grams: '50', total: '45000', payer: 'owner', note: '合成旧日采购' },
|
|
|
|
|
f.actors.owner,
|
2026-12-04 08:58:53 -08:00
|
|
|
),
|
2026-11-04 22:18:56 +08:00
|
|
|
);
|
|
|
|
|
const r = f.create(exchange({}, { cost_material: { mode: 'pool', poolId: D } })),
|
|
|
|
|
d = today(),
|
|
|
|
|
preview = f.l.rollingMaterialPreview({ date: d }, f.actors.owner);
|
|
|
|
|
f.run(preview, () =>
|
|
|
|
|
f.l.enableRollingMaterial({ date: d, revision: preview.revision, verified: true }, f.actors.owner),
|
|
|
|
|
);
|
|
|
|
|
const inputs = {
|
|
|
|
|
...f.info(r.id, 'assistant').inputs,
|
|
|
|
|
revision: 1,
|
|
|
|
|
exchange: { ...f.info(r.id).inputs.exchange, quantity: '31' },
|
|
|
|
|
reason: '合成历史差重更正',
|
|
|
|
|
},
|
|
|
|
|
before = contents(f.l),
|
|
|
|
|
changes = f.l.one('SELECT total_changes() AS n').n;
|
|
|
|
|
const q = f.l.recoveryEditQuote(r.id, inputs, f.actors.assistant);
|
|
|
|
|
assert.equal(q.differenceMg, 11000);
|
|
|
|
|
assert.equal(q.materialCost, 990000);
|
|
|
|
|
assert.equal(contents(f.l), before);
|
|
|
|
|
assert.equal(f.l.one('SELECT total_changes() AS n').n, changes);
|
|
|
|
|
assert.ok(!f.l.rollingHistorical);
|
|
|
|
|
const requested = f.run(inputs, () => f.l.updateRecovery(r.id, inputs, f.actors.assistant), 'assistant');
|
|
|
|
|
assert.equal(requested.pending, true);
|
|
|
|
|
assert.equal(f.l.one('SELECT mg FROM material_pool_usages WHERE order_id=?', r.order_id).mg, 10000);
|
|
|
|
|
f.approve(requested.request);
|
|
|
|
|
assert.equal(f.l.one('SELECT mg FROM material_pool_usages WHERE order_id=?', r.order_id).mg, 11000);
|
|
|
|
|
assert.equal(f.l.detail(r.order_id).profit, 176500);
|
|
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
|
2026-11-04 22:18:56 +08:00
|
|
|
test('回收编辑GET与列表只读,旧核对升级兼容,纯资料改动也让本日重新核对', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
r = f.create();
|
|
|
|
|
f.run({}, () => f.l.closeDay(D, f.actors.owner, 'orderDate'));
|
|
|
|
|
const revision = f.report(D).reportRevision,
|
|
|
|
|
before = contents(f.l),
|
|
|
|
|
changes = f.l.one('SELECT total_changes() AS n').n;
|
|
|
|
|
const first = f.info(r.id);
|
|
|
|
|
f.info(r.id);
|
|
|
|
|
f.l.recoveryChanges({ status: 'all' }, f.actors.owner);
|
|
|
|
|
f.l.recoveries({ date: D }, f.actors.owner);
|
|
|
|
|
f.report(D);
|
|
|
|
|
assert.equal(contents(f.l), before);
|
|
|
|
|
assert.equal(f.l.one('SELECT total_changes() AS n').n, changes);
|
|
|
|
|
assert.equal(first.revision, 1);
|
|
|
|
|
assert.equal(first.pending, null);
|
|
|
|
|
f.update(r.id, { customer: '合成修正称呼' });
|
|
|
|
|
assert.notEqual(f.report(D).reportRevision, revision);
|
|
|
|
|
assert.equal(f.report(D).close.stale, true);
|
|
|
|
|
assert.equal(f.report(D).outlayTotal, 900000);
|
|
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
|
2026-11-04 22:18:56 +08:00
|
|
|
test('HTTP回收专用更正保留会话、CSRF、幂等与审批权限,旧普通订单入口仍被保护', async t => {
|
2026-12-04 08:52:50 -08:00
|
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ledger-recovery-changes-http-')),
|
|
|
|
|
app = createApp({ mode: 'demo', ledger: new Ledger(':memory:'), dataDir: dir, feishuConfig: { enabled: false } });
|
|
|
|
|
await new Promise(resolve => app.server.listen(0, '127.0.0.1', resolve));
|
|
|
|
|
t.after(async () => {
|
|
|
|
|
await new Promise(resolve => {
|
|
|
|
|
app.server.closeAllConnections();
|
|
|
|
|
app.server.close(resolve);
|
|
|
|
|
});
|
|
|
|
|
app.ledger.close();
|
|
|
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
|
|
|
});
|
|
|
|
|
const base = 'http://127.0.0.1:' + app.server.address().port,
|
|
|
|
|
cookies = {};
|
2026-11-04 22:18:56 +08:00
|
|
|
const call = async (member, route, data, key = randomUUID(), origin = base) => {
|
|
|
|
|
const response = await fetch(base + route, {
|
|
|
|
|
method: data === undefined ? 'GET' : 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
...(cookies[member] ? { Cookie: cookies[member] } : {}),
|
|
|
|
|
...(data === undefined
|
|
|
|
|
? {}
|
2026-12-04 08:58:53 -08:00
|
|
|
: {
|
2026-11-04 22:18:56 +08:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
Origin: origin,
|
|
|
|
|
'X-Requested-With': 'maimai-ledger',
|
|
|
|
|
'Idempotency-Key': key,
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
...(data === undefined ? {} : { body: JSON.stringify(data) }),
|
|
|
|
|
});
|
|
|
|
|
return { status: response.status, body: await response.json(), response };
|
|
|
|
|
};
|
|
|
|
|
assert.equal((await call('anonymous', '/api/recovery-changes')).status, 401);
|
|
|
|
|
for (const member of ['owner', 'assistant', 'assistant2']) {
|
|
|
|
|
const result = await call(member, '/api/demo-login', { member });
|
|
|
|
|
cookies[member] = result.response.headers.get('set-cookie').split(';')[0];
|
|
|
|
|
}
|
|
|
|
|
const r = (await call('assistant', '/api/recoveries', direct({ date: today() }))).body,
|
|
|
|
|
info = (await call('assistant', '/api/recoveries/' + r.id + '/edit')).body,
|
|
|
|
|
input = { ...info.inputs, revision: 1, buy_total: '9100', reason: '合成更正' },
|
|
|
|
|
route = '/api/recoveries/' + r.id + '/update';
|
|
|
|
|
assert.equal((await call('assistant2', '/api/recoveries/' + r.id + '/edit')).status, 404);
|
|
|
|
|
assert.equal((await call('assistant', route, input, randomUUID(), 'https://invalid.example')).status, 403);
|
|
|
|
|
const key = randomUUID(),
|
|
|
|
|
results = await Promise.all([call('assistant', route, input, key), call('assistant', route, input, key)]);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
results.map(x => x.status),
|
|
|
|
|
[200, 200],
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(results[0].body, results[1].body);
|
|
|
|
|
assert.equal((await call('assistant', route, input)).status, 409);
|
|
|
|
|
const request = (await call('assistant', route, { ...input, revision: 2, date: D, buy_total: '9200' })).body.request;
|
|
|
|
|
assert.equal(request.status, 'pending');
|
|
|
|
|
assert.equal((await call('assistant2', '/api/recovery-changes/' + request.id)).status, 404);
|
|
|
|
|
assert.equal(
|
|
|
|
|
(await call('assistant', '/api/recovery-changes/' + request.id + '/approve', { revision: 1, verified: true }))
|
|
|
|
|
.status,
|
|
|
|
|
403,
|
|
|
|
|
);
|
|
|
|
|
const approval = await call('owner', '/api/recovery-changes/' + request.id + '/approve', {
|
|
|
|
|
revision: 1,
|
|
|
|
|
verified: true,
|
|
|
|
|
});
|
|
|
|
|
assert.equal(approval.status, 200);
|
|
|
|
|
assert.equal(approval.body.status, 'approved');
|
|
|
|
|
assert.equal(
|
|
|
|
|
(await call('owner', '/api/recovery-changes/' + request.id + '/approve', { revision: 1, verified: true })).status,
|
|
|
|
|
409,
|
2026-12-04 08:58:53 -08:00
|
|
|
);
|
2026-11-04 22:18:56 +08:00
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
|
2026-11-24 21:11:32 +08:00
|
|
|
test('外配置换新货不扣库存,多商品分别出库、计成本,修改取消完整冲回', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.stock();
|
|
|
|
|
const products = [
|
2026-12-04 08:58:53 -08:00
|
|
|
{
|
2026-11-24 21:11:32 +08:00
|
|
|
name: '足金手链',
|
|
|
|
|
quantity: '12',
|
|
|
|
|
pocket: '现货',
|
|
|
|
|
labor_mode: 'gram',
|
|
|
|
|
labor_rate: '20',
|
|
|
|
|
cost_labor: { mode: 'gram', rate: '5' },
|
|
|
|
|
cost_labor_payer: 'owner',
|
|
|
|
|
},
|
2026-12-04 08:58:53 -08:00
|
|
|
{
|
2026-11-24 21:11:32 +08:00
|
|
|
name: '足金外配镯',
|
|
|
|
|
quantity: '18',
|
|
|
|
|
pocket: '',
|
|
|
|
|
labor_mode: 'gram',
|
|
|
|
|
labor_rate: '30',
|
|
|
|
|
cost_labor: { mode: 'total', amount: '90' },
|
|
|
|
|
cost_labor_payer: 'assistant2',
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
2026-11-24 21:11:32 +08:00
|
|
|
];
|
|
|
|
|
const r = f.create(exchange({}, { products })),
|
|
|
|
|
order = f.l.detail(r.order_id),
|
|
|
|
|
inv = key => f.l.inventory().find(row => row.pocket === key).mg;
|
|
|
|
|
assert.equal(r.newMg, 30000);
|
|
|
|
|
assert.equal(r.differenceMg, 10000);
|
|
|
|
|
assert.equal(r.receivable, 1098000);
|
|
|
|
|
assert.equal(r.materialCost, 900000);
|
|
|
|
|
assert.equal(r.laborCost, 15000);
|
|
|
|
|
assert.equal(r.profit, 183000);
|
|
|
|
|
assert.equal(order.lines.length, 2);
|
|
|
|
|
assert.equal(order.lines[1].pocket, '');
|
|
|
|
|
assert.equal(order.lines[1].costPayers.labor, 'assistant2');
|
|
|
|
|
assert.equal(inv('现货'), 88000);
|
|
|
|
|
assert.equal(inv('回收旧料'), 20000);
|
|
|
|
|
assert.equal(f.l.rows("SELECT * FROM movements WHERE pocket='' ").length, 0);
|
|
|
|
|
assert.equal(
|
|
|
|
|
order.lines.reduce((n, row) => n + row.recoveryMaterialMg, 0),
|
|
|
|
|
10000,
|
|
|
|
|
);
|
|
|
|
|
assert.equal(
|
|
|
|
|
order.lines.reduce((n, row) => n + row.costParts.material, 0),
|
|
|
|
|
900000,
|
|
|
|
|
);
|
|
|
|
|
const inputs = f.info(r.id).inputs;
|
|
|
|
|
assert.equal(inputs.exchange.products.length, 2);
|
|
|
|
|
assert.equal(inputs.exchange.products[1].pocket, '');
|
|
|
|
|
const edited = {
|
|
|
|
|
...inputs.exchange,
|
|
|
|
|
products: inputs.exchange.products.map((p, i) => (i ? { ...p, quantity: '8' } : p)),
|
|
|
|
|
};
|
|
|
|
|
f.update(r.id, { exchange: edited });
|
|
|
|
|
let q = f.info(r.id).record;
|
|
|
|
|
assert.equal(q.newMg, 20000);
|
|
|
|
|
assert.equal(q.materialCost, 0);
|
|
|
|
|
assert.equal(inv('现货'), 88000);
|
|
|
|
|
assert.equal(inv('回收旧料'), 20000);
|
|
|
|
|
f.cancel(r.id);
|
|
|
|
|
assert.equal(inv('现货'), 100000);
|
|
|
|
|
assert.equal(inv('回收旧料'), 0);
|
|
|
|
|
assert.equal(f.l.detail(r.order_id).profit, 0);
|
|
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
|
2026-11-24 21:11:32 +08:00
|
|
|
test('外配单件可保存修改取消;缺失货盘和无效盘仍拒绝,后续商品库存不足整单回滚', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
r = f.create(exchange({}, { pocket: '' }));
|
|
|
|
|
assert.equal(f.l.detail(r.order_id).lines[0].pocket, '');
|
|
|
|
|
f.update(r.id, { note: '外配更正' });
|
|
|
|
|
f.cancel(r.id);
|
|
|
|
|
assert.equal(f.l.inventory().find(row => row.pocket === '回收旧料').mg, 0);
|
|
|
|
|
for (const pocket of [undefined, null, '不存在']) assert.throws(() => f.create(exchange({}, { pocket })), /货盘/);
|
|
|
|
|
const products = [
|
2026-12-04 08:58:53 -08:00
|
|
|
{
|
2026-11-24 21:11:32 +08:00
|
|
|
name: '足金外配',
|
|
|
|
|
quantity: '10',
|
|
|
|
|
pocket: '',
|
|
|
|
|
labor_rate: '10',
|
|
|
|
|
cost_labor: { mode: 'total', amount: '0' },
|
|
|
|
|
cost_labor_payer: 'owner',
|
|
|
|
|
},
|
2026-12-04 08:58:53 -08:00
|
|
|
{
|
2026-11-24 21:11:32 +08:00
|
|
|
name: '足金现货',
|
|
|
|
|
quantity: '20',
|
|
|
|
|
pocket: '现货',
|
|
|
|
|
labor_rate: '10',
|
|
|
|
|
cost_labor: { mode: 'total', amount: '0' },
|
|
|
|
|
cost_labor_payer: 'owner',
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
2026-11-24 21:11:32 +08:00
|
|
|
];
|
|
|
|
|
const before = contents(f.l);
|
|
|
|
|
assert.throws(() => f.create(exchange({}, { products })), /库存不足/);
|
|
|
|
|
assert.equal(contents(f.l), before);
|
|
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
|
2026-11-24 21:11:32 +08:00
|
|
|
test('多商品差重合计只耗用一次料池,余料只生成一笔购料;客户打印不带内部字段', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.stock();
|
|
|
|
|
f.run({}, () => f.l.createMaterialPurchase({ date: D, grams: '20', total: '18000', payer: 'owner' }, f.actors.owner));
|
|
|
|
|
const products = [
|
2026-12-04 08:58:53 -08:00
|
|
|
{
|
2026-11-24 21:11:32 +08:00
|
|
|
name: '足金A',
|
|
|
|
|
quantity: '10.001',
|
|
|
|
|
pocket: '现货',
|
|
|
|
|
labor_rate: '10',
|
|
|
|
|
cost_labor: { mode: 'total', amount: '0' },
|
|
|
|
|
cost_labor_payer: 'owner',
|
|
|
|
|
},
|
2026-12-04 08:58:53 -08:00
|
|
|
{
|
2026-11-24 21:11:32 +08:00
|
|
|
name: '足金B',
|
|
|
|
|
quantity: '19.999',
|
|
|
|
|
pocket: '',
|
|
|
|
|
labor_rate: '20',
|
|
|
|
|
cost_labor: { mode: 'total', amount: '0' },
|
|
|
|
|
cost_labor_payer: 'assistant',
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
2026-11-24 21:11:32 +08:00
|
|
|
];
|
|
|
|
|
const r = f.create(exchange({}, { products, cost_material: { mode: 'pool', poolId: D } }));
|
|
|
|
|
assert.equal(f.l.materialPoolDetail(D).usedMg, 10000);
|
|
|
|
|
assert.equal(f.l.detail(r.order_id).goodsCost, 900000);
|
2026-12-04 13:01:19 -08:00
|
|
|
const dto = require('../domains/recoveries/recoveries').publicDetail(f.l, r.id, f.actors.owner, f.l.businessEnv());
|
2026-11-24 21:11:32 +08:00
|
|
|
assert.equal(dto.new_lines.length, 2);
|
|
|
|
|
assert.doesNotMatch(JSON.stringify(dto), /costLabor|laborPayer|pocket|materialCost/);
|
2026-12-04 13:01:19 -08:00
|
|
|
const html = require('../domains/printing/recovery-printing').exchangeCustomerPrintDocument(dto);
|
2026-11-24 21:11:32 +08:00
|
|
|
assert.match(html, /足金A/);
|
|
|
|
|
assert.match(html, /足金B/);
|
|
|
|
|
assert.doesNotMatch(html, /costLabor|laborPayer/);
|
|
|
|
|
f.cancel(r.id);
|
|
|
|
|
assert.equal(f.l.materialPoolDetail(D).usedMg, 0);
|
|
|
|
|
const surplus = f.create(exchange({ old_lines: old('40'), buy_rate: '850' }, { products }));
|
|
|
|
|
assert.equal(surplus.purchaseMg, 10000);
|
|
|
|
|
assert.equal(surplus.purchaseAmount, 850000);
|
|
|
|
|
assert.equal(f.l.rows('SELECT * FROM recovery_purchase_sources WHERE recovery_id=?', surplus.id).length, 1);
|
|
|
|
|
});
|