2026-11-04 22:18:56 +08:00
|
|
|
'use strict';
|
|
|
|
|
const { test } = require('node:test'),
|
|
|
|
|
assert = require('node:assert/strict'),
|
|
|
|
|
{ randomUUID } = require('node:crypto');
|
2026-12-04 13:01:19 -08:00
|
|
|
const { Ledger, today } = require('../core/ledger'),
|
2026-11-04 22:18:56 +08:00
|
|
|
{ createUser } = require('../server'),
|
|
|
|
|
Calc = require('../public/calculations');
|
|
|
|
|
const D = '2026-11-01',
|
|
|
|
|
D2 = '2026-11-02',
|
|
|
|
|
D3 = '2026-11-03';
|
|
|
|
|
const tables = l =>
|
|
|
|
|
JSON.stringify(
|
2026-12-04 08:58:53 -08:00
|
|
|
l
|
2026-11-04 22:18:56 +08:00
|
|
|
.rows("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
|
|
|
|
|
.map(({ name }) => [name, l.rows('SELECT * FROM "' + name + '" ORDER BY rowid')]),
|
|
|
|
|
);
|
|
|
|
|
const finances = l =>
|
|
|
|
|
JSON.stringify(
|
2026-12-04 08:58:53 -08:00
|
|
|
[
|
2026-11-04 22:18:56 +08:00
|
|
|
'orders',
|
|
|
|
|
'price_locks',
|
|
|
|
|
'price_lock_events',
|
|
|
|
|
'price_lock_pool_reservations',
|
|
|
|
|
'price_lock_pool_returns',
|
|
|
|
|
'material_purchases',
|
|
|
|
|
'material_pool_usages',
|
|
|
|
|
'material_usage_order',
|
|
|
|
|
'material_usage_parties',
|
|
|
|
|
'material_rolling_days',
|
|
|
|
|
'material_rolling_returns',
|
|
|
|
|
'material_rolling_source_order',
|
|
|
|
|
'payments',
|
|
|
|
|
'expenses',
|
|
|
|
|
'settlements',
|
|
|
|
|
'journal',
|
|
|
|
|
'movements',
|
|
|
|
|
'closes',
|
|
|
|
|
'inventory_snapshots',
|
|
|
|
|
'sync',
|
|
|
|
|
'team_fund_sources',
|
|
|
|
|
'team_fund_entries',
|
|
|
|
|
'team_fund_cash',
|
|
|
|
|
'team_fund_transfers',
|
|
|
|
|
].map(name => [name, l.rows('SELECT * FROM ' + name + ' ORDER BY rowid')]),
|
|
|
|
|
);
|
|
|
|
|
function fixture(t) {
|
|
|
|
|
t.mock.timers.enable({ apis: ['Date'], now: Date.parse(D + 'T04:00:00Z') });
|
|
|
|
|
const l = new Ledger(':memory:'),
|
|
|
|
|
actors = {};
|
|
|
|
|
t.after(() => l.close());
|
|
|
|
|
for (const member of ['owner', 'assistant', 'assistant2']) {
|
|
|
|
|
const name = '合成锁价改单-' + member,
|
|
|
|
|
role = member === 'owner' ? 'owner' : 'assistant';
|
|
|
|
|
actors[member] = { id: createUser(l, name, role, 'synthetic-lock-edit-password', member), name, role, member };
|
|
|
|
|
}
|
|
|
|
|
l.enableTeamMode();
|
|
|
|
|
const run = (payload, fn, member = 'owner', key = randomUUID()) =>
|
|
|
|
|
l.scope(actors[member], () => l.transact(key, actors[member], payload, fn));
|
|
|
|
|
run({}, () => l.stock({ date: D, kind: 'opening', pocket: '现货', grams: '100', note: '合成开账' }, actors.owner));
|
|
|
|
|
const advance = date => t.mock.timers.tick(Date.parse(date + 'T04:00:00Z') - Date.now());
|
|
|
|
|
const buy = (grams, total, payer = 'owner') =>
|
|
|
|
|
run({ grams, total, payer }, () =>
|
|
|
|
|
l.createMaterialPurchase({ date: today(), grams, total, payer, note: '合成真实购料' }, actors.owner),
|
|
|
|
|
);
|
|
|
|
|
const enable = () => {
|
|
|
|
|
const p = l.rollingMaterialPreview({}, actors.owner);
|
|
|
|
|
return run(p, () => l.enableRollingMaterial({ date: today(), revision: p.revision, verified: true }, actors.owner));
|
|
|
|
|
};
|
|
|
|
|
const lock = (source = 'manual', quantity = '2') => {
|
|
|
|
|
const material =
|
|
|
|
|
source === 'pool'
|
|
|
|
|
? { source, poolId: today(), revision: l.materialPoolDetail(today()).revision }
|
|
|
|
|
: { source, rate: '850', payer: 'owner', verified: true };
|
|
|
|
|
const p = {
|
|
|
|
|
date: today(),
|
|
|
|
|
customer: '合成锁价客户',
|
|
|
|
|
salesperson: '原开单人',
|
|
|
|
|
line: { name: '合成足金挂件', unit: 'g', quantity, goldPrice: '1000', laborRate: '0', laborMode: 'gram' },
|
|
|
|
|
material,
|
|
|
|
|
payment: { amount: (Number(quantity) * 1000).toFixed(2), account: '助理微信', verified: true },
|
2026-12-04 08:58:53 -08:00
|
|
|
};
|
2026-11-04 22:18:56 +08:00
|
|
|
return run(p, () => l.createPriceLock(p, actors.assistant), 'assistant').id;
|
|
|
|
|
};
|
2026-11-08 02:22:58 +08:00
|
|
|
const deliver = (lockId, quantity = '2', extra = {}) => {
|
|
|
|
|
const original = l.priceLockDetail(lockId),
|
|
|
|
|
difference = Number(quantity) * 1000 - original.netReceived / 100,
|
2026-12-04 08:58:53 -08:00
|
|
|
p = {
|
2026-11-08 02:22:58 +08:00
|
|
|
date: today(),
|
|
|
|
|
revision: original.revision,
|
|
|
|
|
verified: true,
|
|
|
|
|
line: {
|
|
|
|
|
quantity,
|
|
|
|
|
laborRate: '0',
|
|
|
|
|
laborMode: 'gram',
|
|
|
|
|
payer: 'assistant',
|
|
|
|
|
costPayers: { material: original.buy_payer, labor: 'assistant' },
|
|
|
|
|
costParts: { labor: '20', accessories: '2', other: '1' },
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
2026-11-08 02:22:58 +08:00
|
|
|
inventory: {
|
|
|
|
|
pocket: '现货',
|
|
|
|
|
original: original.material_source === 'pool' ? 'stocked' : 'receive',
|
|
|
|
|
extra: 'receive',
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
2026-11-08 02:22:58 +08:00
|
|
|
extraMaterial: { rate: '900', payer: 'assistant', verified: true },
|
|
|
|
|
fee_items: [{ key: 'shipping', amount: '6', payer: 'assistant', note: '快递费' }],
|
|
|
|
|
...(difference
|
|
|
|
|
? { settlement: { amount: Math.abs(difference).toFixed(2), account: '助理微信', verified: true } }
|
|
|
|
|
: {}),
|
2026-12-04 08:58:53 -08:00
|
|
|
...extra,
|
2026-11-08 02:22:58 +08:00
|
|
|
};
|
|
|
|
|
return run(p, () => l.deliverPriceLock(lockId, p, actors.assistant), 'assistant').id;
|
|
|
|
|
};
|
|
|
|
|
const lockRows = specs => {
|
|
|
|
|
const revision = specs.some(row => row.source === 'pool') ? l.materialPoolDetail(today()).revision : null;
|
|
|
|
|
const lines = specs.map((row, index) => ({
|
|
|
|
|
name: '合成多行黄金' + (index + 1),
|
|
|
|
|
unit: 'g',
|
|
|
|
|
quantity: row.quantity ?? '1',
|
|
|
|
|
goldPrice: row.sellRate ?? '1000',
|
|
|
|
|
laborRate: '0',
|
|
|
|
|
laborMode: 'gram',
|
|
|
|
|
material:
|
|
|
|
|
row.source === 'pool'
|
|
|
|
|
? { source: 'pool', poolId: today(), revision }
|
|
|
|
|
: { source: 'manual', rate: row.rate ?? '850', payer: row.payer ?? 'owner', verified: true },
|
|
|
|
|
}));
|
|
|
|
|
const amount = lines.reduce((total, row) => total + Number(row.quantity) * Number(row.goldPrice), 0).toFixed(2),
|
2026-12-04 08:58:53 -08:00
|
|
|
p = {
|
2026-11-04 22:18:56 +08:00
|
|
|
date: today(),
|
2026-11-08 02:22:58 +08:00
|
|
|
customer: '合成多商品客户',
|
|
|
|
|
salesperson: '原开单人',
|
|
|
|
|
lines,
|
|
|
|
|
payment: { amount, account: '助理微信', verified: true },
|
2026-12-04 08:58:53 -08:00
|
|
|
};
|
2026-11-08 02:22:58 +08:00
|
|
|
return run(p, () => l.createPriceLock(p, actors.assistant), 'assistant').id;
|
2026-12-04 08:58:53 -08:00
|
|
|
};
|
2026-11-08 02:22:58 +08:00
|
|
|
const deliverRows = (lockId, quantities) => {
|
|
|
|
|
const original = l.priceLockDetail(lockId),
|
|
|
|
|
lines = original.lines.map((row, index) => ({
|
|
|
|
|
lineId: row.id,
|
|
|
|
|
quantity: quantities?.[index] ?? (row.locked_mg / 1000).toFixed(3),
|
|
|
|
|
laborRate: '0',
|
|
|
|
|
laborMode: 'gram',
|
|
|
|
|
payer: 'assistant',
|
|
|
|
|
costPayers: { material: row.buy_payer, labor: 'assistant' },
|
|
|
|
|
costParts: { labor: '20', accessories: '2', other: '1' },
|
|
|
|
|
inventory: {
|
|
|
|
|
pocket: '现货',
|
|
|
|
|
original: row.material_source === 'pool' ? 'stocked' : 'receive',
|
|
|
|
|
extra: 'receive',
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
2026-11-08 02:22:58 +08:00
|
|
|
extraMaterial: { rate: '900', payer: 'assistant', verified: true },
|
2026-12-04 08:58:53 -08:00
|
|
|
})),
|
2026-11-08 02:22:58 +08:00
|
|
|
difference =
|
|
|
|
|
lines.reduce((total, row, index) => total + (Number(row.quantity) * original.lines[index].sell_rate) / 100, 0) -
|
|
|
|
|
original.netReceived / 100;
|
|
|
|
|
const p = {
|
|
|
|
|
date: today(),
|
|
|
|
|
revision: original.revision,
|
|
|
|
|
verified: true,
|
|
|
|
|
lines,
|
|
|
|
|
fee_items: [{ key: 'shipping', amount: '6', payer: 'assistant', note: '快递费' }],
|
|
|
|
|
...(difference
|
|
|
|
|
? { settlement: { amount: Math.abs(difference).toFixed(2), account: '助理微信', verified: true } }
|
|
|
|
|
: {}),
|
|
|
|
|
};
|
|
|
|
|
return run(p, () => l.deliverPriceLock(lockId, p, actors.assistant), 'assistant').id;
|
2026-12-04 08:58:53 -08:00
|
|
|
};
|
2026-11-04 22:18:56 +08:00
|
|
|
const payload = (id, member = 'assistant') => {
|
|
|
|
|
const info = l.orderEdit(id, actors[member]);
|
|
|
|
|
return { ...info.inputs, revision: info.revision, reason: '核对原始凭据更正成本', verified: true };
|
2026-12-04 08:58:53 -08:00
|
|
|
};
|
2026-11-04 22:18:56 +08:00
|
|
|
const edit = (id, p = payload(id), member = 'owner', key = randomUUID()) =>
|
|
|
|
|
run(p, () => l.editOrder(id, p, actors[member]), member, key);
|
|
|
|
|
const request = (id, p) => run(p, () => l.requestOrderChange(id, p, actors.assistant), 'assistant');
|
|
|
|
|
const approve = r =>
|
|
|
|
|
run({ request: r.requestId }, () =>
|
|
|
|
|
l.approveOrderChange(r.requestId, { revision: r.request.order_revision, verified: true }, actors.owner),
|
2026-12-04 08:58:53 -08:00
|
|
|
);
|
|
|
|
|
return {
|
|
|
|
|
l,
|
|
|
|
|
actors,
|
|
|
|
|
run,
|
|
|
|
|
advance,
|
|
|
|
|
buy,
|
|
|
|
|
enable,
|
|
|
|
|
lock,
|
|
|
|
|
deliver,
|
|
|
|
|
lockRows,
|
2026-11-08 02:22:58 +08:00
|
|
|
deliverRows,
|
2026-12-04 08:58:53 -08:00
|
|
|
payload,
|
|
|
|
|
edit,
|
|
|
|
|
request,
|
|
|
|
|
approve,
|
2026-11-08 02:22:58 +08:00
|
|
|
};
|
2026-11-04 22:18:56 +08:00
|
|
|
}
|
|
|
|
|
function consistent(l, id) {
|
|
|
|
|
const o = l.detail(id);
|
|
|
|
|
assert.equal(o.total - o.goodsCost - o.directCost, o.profit);
|
|
|
|
|
assert.equal(Calc.orderCosts(o).total.total, o.goodsCost + o.directCost);
|
|
|
|
|
assert.equal(o.balance, 0);
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-24 21:11:32 +08:00
|
|
|
test('已交付锁价支持成本勾选迁移和取消,同日直存历史审批且销售条款不变', t => {
|
|
|
|
|
const Fees = require('../public/fee-estimates'),
|
|
|
|
|
f = fixture(t),
|
|
|
|
|
lock = f.lock(),
|
|
|
|
|
id = f.deliver(lock),
|
|
|
|
|
before = consistent(f.l, id),
|
|
|
|
|
cash = f.l.priceLockDetail(lock).cash_events,
|
|
|
|
|
p = f.payload(id),
|
|
|
|
|
shipping = p.fee_items.find(row => row.key === 'shipping');
|
|
|
|
|
p.fee_cost_mode = 'checkboxes';
|
|
|
|
|
p.fee_items = Fees.checkboxFees({ shipping: 'jd', box: true, quantity: '1', silver: true }).map(row => ({
|
|
|
|
|
...row,
|
|
|
|
|
...(row.key === 'shipping' ? { expense_id: shipping.expense_id, date: shipping.date } : {}),
|
|
|
|
|
}));
|
|
|
|
|
f.edit(id, p, 'assistant');
|
|
|
|
|
const after = consistent(f.l, id);
|
|
|
|
|
assert.equal(after.fee_cost_mode, 'checkboxes');
|
|
|
|
|
assert.equal(after.directCost, 2900);
|
|
|
|
|
assert.equal(after.total, before.total);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
after.lines.map(row => row.charges),
|
|
|
|
|
before.lines.map(row => row.charges),
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(f.l.priceLockDetail(lock).cash_events, cash);
|
|
|
|
|
assert.equal(f.payload(id).fee_items.find(row => row.key === 'shipping').date, D);
|
|
|
|
|
f.advance(D2);
|
|
|
|
|
const cancel = f.payload(id);
|
|
|
|
|
cancel.fee_items = [];
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.edit(id, cancel, 'assistant'),
|
|
|
|
|
error => error.status === 403,
|
|
|
|
|
);
|
|
|
|
|
const request = f.request(id, cancel);
|
|
|
|
|
assert.equal(consistent(f.l, id).directCost, 2900);
|
|
|
|
|
f.approve(request);
|
|
|
|
|
assert.equal(consistent(f.l, id).directCost, 0);
|
|
|
|
|
assert.deepEqual(f.payload(id).fee_items, []);
|
|
|
|
|
assert.deepEqual(f.l.priceLockDetail(lock).cash_events, cash);
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-04 22:18:56 +08:00
|
|
|
test('独立采购锁价改单更正原料、补重和实际成本付款人,购料来源不重记且实际收付日期保留', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
lock = f.lock();
|
|
|
|
|
f.advance(D2);
|
|
|
|
|
const id = f.deliver(lock, '3'),
|
|
|
|
|
before = f.l.detail(id),
|
|
|
|
|
cash = f.l.priceLockDetail(lock).cash_events,
|
|
|
|
|
events = f.l.priceLockDetail(lock).material_events;
|
|
|
|
|
const p = f.payload(id);
|
|
|
|
|
p.price_lock_material.rate = '860';
|
|
|
|
|
p.price_lock_material.payer = 'assistant2';
|
|
|
|
|
p.price_lock_extra_material.rate = '910';
|
|
|
|
|
p.price_lock_extra_material.payer = 'owner';
|
|
|
|
|
p.lines[0].costParts.labor = '30';
|
|
|
|
|
p.lines[0].costPayers.labor = 'owner';
|
|
|
|
|
p.lines[0].costParts.accessories = '5';
|
|
|
|
|
p.lines[0].costParts.other = '2';
|
|
|
|
|
p.fee_items[0].amount = '8';
|
|
|
|
|
p.fee_items[0].payer = 'assistant2';
|
|
|
|
|
f.edit(id, p);
|
|
|
|
|
const after = consistent(f.l, id),
|
|
|
|
|
lockAfter = f.l.priceLockDetail(lock);
|
|
|
|
|
assert.equal(after.goodsCost, 266700);
|
|
|
|
|
assert.equal(after.directCost, 800);
|
|
|
|
|
assert.equal(after.profit, 32500);
|
|
|
|
|
assert.equal(after.total, before.total);
|
|
|
|
|
assert.equal(after.netReceived, before.netReceived);
|
|
|
|
|
assert.deepEqual(lockAfter.cash_events, cash);
|
|
|
|
|
assert.equal(lockAfter.date, D);
|
|
|
|
|
assert.equal(lockAfter.buy_rate, 86000);
|
|
|
|
|
assert.equal(lockAfter.material_amount, 172000);
|
|
|
|
|
assert.equal(lockAfter.buy_payer, 'assistant2');
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
lockAfter.material_events.map(row => [row.id, row.date, row.source]),
|
|
|
|
|
events.map(row => [row.id, row.date, row.source]),
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
lockAfter.material_events.map(row => [row.amount, row.payer]),
|
2026-12-04 08:58:53 -08:00
|
|
|
[
|
2026-11-04 22:18:56 +08:00
|
|
|
[172000, 'assistant2'],
|
|
|
|
|
[91000, 'owner'],
|
2026-12-04 08:58:53 -08:00
|
|
|
],
|
2026-11-04 22:18:56 +08:00
|
|
|
);
|
|
|
|
|
assert.equal(
|
|
|
|
|
f.l.one(
|
|
|
|
|
"SELECT SUM(amount) AS n FROM team_fund_cash WHERE source_kind='price_lock' AND source_id=? AND kind='outlay' AND member='assistant2' AND date=?",
|
|
|
|
|
lock,
|
2026-12-04 08:58:53 -08:00
|
|
|
D,
|
2026-11-04 22:18:56 +08:00
|
|
|
).n,
|
|
|
|
|
172000,
|
|
|
|
|
);
|
|
|
|
|
assert.equal(
|
|
|
|
|
f.l.one(
|
|
|
|
|
"SELECT SUM(amount) AS n FROM team_fund_cash WHERE source_kind='price_lock' AND source_id=? AND kind='outlay' AND member='owner' AND date=?",
|
|
|
|
|
lock,
|
2026-12-04 08:58:53 -08:00
|
|
|
D,
|
2026-11-04 22:18:56 +08:00
|
|
|
).n,
|
2026-12-04 08:58:53 -08:00
|
|
|
0,
|
2026-11-04 22:18:56 +08:00
|
|
|
);
|
|
|
|
|
const stable = finances(f.l);
|
|
|
|
|
f.edit(id, f.payload(id));
|
|
|
|
|
assert.equal(
|
|
|
|
|
finances(f.l).replace(/"revision":\d+/g, '"revision":0'),
|
|
|
|
|
stable.replace(/"revision":\d+/g, '"revision":0'),
|
|
|
|
|
'原样保存只触及订单资料版本',
|
2026-12-04 08:58:53 -08:00
|
|
|
);
|
2026-11-04 22:18:56 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('只更正交付日保留原锁日、精确成本、实收实付与原入库日,销售出库和利润移至新日', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
lock = f.lock();
|
|
|
|
|
f.advance(D2);
|
|
|
|
|
const id = f.deliver(lock, '3'),
|
|
|
|
|
before = consistent(f.l, id),
|
|
|
|
|
cash = f.l.priceLockDetail(lock).cash_events,
|
|
|
|
|
materials = f.l.priceLockDetail(lock).material_events,
|
|
|
|
|
purchases = f.l.rows("SELECT * FROM movements WHERE order_id=? AND kind='lock_purchase'", id);
|
|
|
|
|
f.advance(D3);
|
|
|
|
|
for (const date of [D, D2, D3]) f.run({ date }, () => f.l.closeDay(date, f.actors.owner, 'orderDate'));
|
|
|
|
|
const p = f.payload(id);
|
|
|
|
|
p.date = D3;
|
|
|
|
|
f.edit(id, p);
|
|
|
|
|
const after = consistent(f.l, id),
|
|
|
|
|
updated = f.l.priceLockDetail(lock);
|
|
|
|
|
assert.equal(updated.date, D);
|
|
|
|
|
assert.equal(updated.delivery.date, D3);
|
|
|
|
|
assert.equal(after.date, D3);
|
|
|
|
|
assert.equal(after.delivered_date, D3);
|
|
|
|
|
assert.equal(after.goodsCost, before.goodsCost);
|
|
|
|
|
assert.deepEqual(after.lines, before.lines);
|
|
|
|
|
assert.deepEqual(updated.cash_events, cash);
|
|
|
|
|
assert.deepEqual(updated.material_events, materials);
|
|
|
|
|
assert.deepEqual(f.l.rows("SELECT * FROM movements WHERE order_id=? AND kind='lock_purchase'", id), purchases);
|
|
|
|
|
assert.equal(f.l.reportByOrderDate(D2).sales, 0);
|
|
|
|
|
assert.equal(f.l.reportByOrderDate(D3).sales, before.total);
|
|
|
|
|
assert.equal(f.l.reportByOrderDate(D3).profit, before.profit);
|
|
|
|
|
assert.equal(f.l.reportByOrderDate(D2).receiptsByOrderDate.net, 100000);
|
|
|
|
|
assert.equal(f.l.reportByOrderDate(D3).receiptsByOrderDate.net, 0);
|
|
|
|
|
assert.equal(f.l.one('SELECT SUM(mg) AS n FROM movements WHERE order_id=? AND date=?', id, D2).n, 3000);
|
|
|
|
|
assert.equal(f.l.one('SELECT SUM(mg) AS n FROM movements WHERE order_id=? AND date=?', id, D3).n, -3000);
|
|
|
|
|
for (const date of [D, D2, D3]) assert.equal(f.l.one('SELECT stale FROM closes WHERE date=?', date).stale, 1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('池料转手填释放原锁全部占用与余料,改后只登记一次独立采购,重复保存不重复释放或支出', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.buy('1.001', '800.01');
|
|
|
|
|
f.buy('2.999', '2999.99', 'assistant2');
|
|
|
|
|
f.enable();
|
|
|
|
|
const lock = f.lock('pool');
|
|
|
|
|
f.advance(D2);
|
|
|
|
|
const id = f.deliver(lock, '1.5'),
|
|
|
|
|
original = f.l.detail(id),
|
|
|
|
|
cash = f.l.priceLockDetail(lock).cash_events,
|
|
|
|
|
purchases = f.l.rows('SELECT * FROM material_purchases ORDER BY rowid');
|
|
|
|
|
const p = f.payload(id);
|
|
|
|
|
p.price_lock_material = { source: 'manual', rate: '880', payer: 'assistant2', verified: true };
|
|
|
|
|
f.edit(id, p);
|
|
|
|
|
const after = consistent(f.l, id);
|
|
|
|
|
assert.equal(after.goodsCost, 134300);
|
|
|
|
|
assert.equal(after.profit, 15100);
|
|
|
|
|
assert.equal(after.netReceived, original.netReceived);
|
|
|
|
|
assert.deepEqual(f.l.priceLockDetail(lock).cash_events, cash);
|
|
|
|
|
assert.equal(f.l.priceLockDetail(lock).material_source, 'manual');
|
|
|
|
|
assert.equal(f.l.priceLockDetail(lock).material_events.filter(row => row.source === 'original').length, 1);
|
|
|
|
|
assert.equal(f.l.priceLockDetail(lock).material_amount, 176000);
|
|
|
|
|
assert.equal(f.l.rows('SELECT * FROM price_lock_pool_reservations WHERE price_lock_id=?', lock).length, 0);
|
|
|
|
|
assert.equal(f.l.rows('SELECT * FROM price_lock_pool_returns WHERE price_lock_id=?', lock).length, 0);
|
|
|
|
|
assert.equal(after.lines[0].priceLockPool, undefined);
|
|
|
|
|
assert.ok(after.lines[0].materialAllocations.every(row => !row.weighted && !row.poolId));
|
|
|
|
|
assert.deepEqual(f.l.rows('SELECT * FROM material_purchases ORDER BY rowid'), purchases);
|
|
|
|
|
assert.equal(f.l.materialPoolDetail(D).usedMg, 0);
|
|
|
|
|
assert.equal(f.l.materialPoolDetail(D2).remainingMg, 4000);
|
|
|
|
|
assert.equal(f.l.materialPoolDetail(D2).remainingCost, 380000);
|
|
|
|
|
const oldEvents = f.l.rows('SELECT * FROM price_lock_events ORDER BY rowid'),
|
|
|
|
|
oldCash = f.l.rows('SELECT * FROM team_fund_cash ORDER BY rowid'),
|
|
|
|
|
oldEntries = f.l.rows('SELECT * FROM team_fund_entries ORDER BY rowid');
|
|
|
|
|
f.edit(id, f.payload(id));
|
|
|
|
|
assert.deepEqual(f.l.rows('SELECT * FROM price_lock_events ORDER BY rowid'), oldEvents);
|
|
|
|
|
assert.deepEqual(f.l.rows('SELECT * FROM team_fund_cash ORDER BY rowid'), oldCash);
|
|
|
|
|
assert.deepEqual(f.l.rows('SELECT * FROM team_fund_entries ORDER BY rowid'), oldEntries);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('今日交付涉及历史锁价或料池的成本必须审批,纯客户备注同日可改,预览回滚且批准结果一致', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.buy('4', '3600');
|
|
|
|
|
f.enable();
|
|
|
|
|
const lock = f.lock('pool');
|
|
|
|
|
f.advance(D2);
|
|
|
|
|
const id = f.deliver(lock, '1.5'),
|
|
|
|
|
info = f.l.orderEdit(id, f.actors.assistant);
|
|
|
|
|
assert.equal(info.financialEditable, true);
|
|
|
|
|
assert.equal(info.requiresApproval, false);
|
|
|
|
|
assert.equal(info.priceLock.financeRequiresApproval, true);
|
|
|
|
|
const metadata = f.payload(id);
|
|
|
|
|
metadata.customer = '同日资料更正';
|
|
|
|
|
f.edit(id, metadata, 'assistant');
|
|
|
|
|
assert.equal(f.l.detail(id).customer, metadata.customer);
|
|
|
|
|
const p = f.payload(id);
|
|
|
|
|
p.price_lock_material = { source: 'manual', rate: '880', payer: 'owner', verified: true };
|
|
|
|
|
p.lines[0].costParts.labor = '30';
|
|
|
|
|
const before = tables(f.l);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.edit(id, p, 'assistant'),
|
|
|
|
|
error => error.status === 403 && /历史/.test(error.message),
|
|
|
|
|
);
|
|
|
|
|
assert.equal(tables(f.l), before);
|
|
|
|
|
const financial = finances(f.l),
|
|
|
|
|
r = f.request(id, p);
|
|
|
|
|
assert.equal(finances(f.l), financial);
|
|
|
|
|
assert.equal(r.request.before.priceLockCosts.materialSource, 'pool');
|
|
|
|
|
assert.equal(r.request.after.priceLockCosts.materialSource, 'manual');
|
|
|
|
|
assert.ok(r.request.changes.some(row => row.field === 'priceLockCosts'));
|
|
|
|
|
assert.equal(f.l.priceLockDetail(lock).material_source, 'pool');
|
|
|
|
|
const approved = f.approve(r);
|
|
|
|
|
assert.equal(approved.request.status, 'approved');
|
|
|
|
|
assert.equal(f.l.priceLockDetail(lock).material_source, 'manual');
|
|
|
|
|
assert.equal(consistent(f.l, id).goodsCost, r.request.after.goodsCost);
|
|
|
|
|
assert.equal(f.l.orderChangeRequest(r.requestId, f.actors.owner).after.profit, f.l.detail(id).profit);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('池料跨日交付更正日期的审批保留原冻结料费和分摊,回料只移日一次且客户款不重记', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.buy('1.001', '800.01');
|
|
|
|
|
f.buy('2.999', '2999.99', 'assistant2');
|
|
|
|
|
f.enable();
|
|
|
|
|
const lock = f.lock('pool');
|
|
|
|
|
f.advance(D2);
|
|
|
|
|
const id = f.deliver(lock, '1.5'),
|
|
|
|
|
before = f.l.detail(id),
|
|
|
|
|
reserved = f.l.one('SELECT * FROM price_lock_pool_reservations WHERE price_lock_id=?', lock),
|
|
|
|
|
cash = f.l.priceLockDetail(lock).cash_events,
|
|
|
|
|
returned = f.l.one('SELECT * FROM price_lock_pool_returns WHERE price_lock_id=?', lock);
|
|
|
|
|
f.advance(D3);
|
|
|
|
|
const p = f.payload(id);
|
|
|
|
|
p.date = D3;
|
|
|
|
|
const old = finances(f.l),
|
|
|
|
|
r = f.request(id, p);
|
|
|
|
|
assert.equal(finances(f.l), old);
|
|
|
|
|
assert.equal(r.request.after.goodsCost, before.goodsCost);
|
|
|
|
|
f.approve(r);
|
|
|
|
|
const after = consistent(f.l, id),
|
|
|
|
|
nextReturn = f.l.one('SELECT * FROM price_lock_pool_returns WHERE price_lock_id=?', lock);
|
|
|
|
|
assert.equal(after.date, D3);
|
|
|
|
|
assert.deepEqual(after.lines, before.lines);
|
|
|
|
|
assert.deepEqual(f.l.one('SELECT * FROM price_lock_pool_reservations WHERE price_lock_id=?', lock), reserved);
|
|
|
|
|
assert.equal(nextReturn.id, returned.id);
|
|
|
|
|
assert.equal(nextReturn.date, D3);
|
|
|
|
|
assert.equal(nextReturn.amount, returned.amount);
|
|
|
|
|
assert.deepEqual(f.l.priceLockDetail(lock).cash_events, cash);
|
|
|
|
|
assert.equal(f.l.reportByOrderDate(D2).receiptsByOrderDate.net, -50000);
|
|
|
|
|
assert.equal(f.l.reportByOrderDate(D3).receiptsByOrderDate.net, 0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('同日独立锁价成本可直接改且仅本人可访问,原锁价日期及销售条件不能借改单更换', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
lock = f.lock(),
|
|
|
|
|
id = f.deliver(lock),
|
|
|
|
|
info = f.l.orderEdit(id, f.actors.assistant);
|
|
|
|
|
assert.equal(info.priceLock.financeRequiresApproval, false);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.l.asActor(f.actors.assistant2).orderEdit(id, f.actors.assistant2),
|
|
|
|
|
error => error.status === 404,
|
|
|
|
|
);
|
|
|
|
|
const p = f.payload(id);
|
|
|
|
|
p.price_lock_material.rate = '860';
|
|
|
|
|
f.edit(id, p, 'assistant');
|
|
|
|
|
assert.equal(consistent(f.l, id).goodsCost, 174300);
|
|
|
|
|
const changes = [
|
|
|
|
|
p => (p.lines[0].quantity = '3'),
|
|
|
|
|
p => (p.lines[0].goldPrice = '1001'),
|
|
|
|
|
p => (p.discount = '1'),
|
|
|
|
|
p => (p.payment_account = '麦麦微信'),
|
|
|
|
|
p => (p.price_lock_material = { source: 'pool', rate: '860', payer: 'owner', verified: true }),
|
|
|
|
|
p => {
|
|
|
|
|
p.price_lock_material.rate = '870';
|
|
|
|
|
p.price_lock_material.verified = false;
|
|
|
|
|
},
|
|
|
|
|
p => (p.date = '2026-10-31'),
|
|
|
|
|
];
|
|
|
|
|
for (const mutate of changes) {
|
|
|
|
|
const invalid = f.payload(id);
|
|
|
|
|
mutate(invalid);
|
|
|
|
|
const before = tables(f.l);
|
|
|
|
|
assert.throws(() => f.edit(id, invalid));
|
|
|
|
|
assert.equal(tables(f.l), before);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('已交付锁价信用卡手续费更正付款人沿原费率和实际日,费用及团队现金不重复', t => {
|
|
|
|
|
const f = fixture(t),
|
2026-12-04 08:58:53 -08:00
|
|
|
p = {
|
2026-11-04 22:18:56 +08:00
|
|
|
date: D,
|
|
|
|
|
customer: '合成卡客',
|
|
|
|
|
salesperson: '开单人',
|
|
|
|
|
line: { name: '合成黄金', unit: 'g', quantity: '1', goldPrice: '1000', laborRate: '0', laborMode: 'gram' },
|
|
|
|
|
material: { rate: '850', payer: 'owner', verified: true },
|
|
|
|
|
payment: { amount: '1000', account: '麦麦信用卡', credit_card_fee_payer: 'owner', verified: true },
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
2026-11-04 22:18:56 +08:00
|
|
|
lock = f.run(p, () => f.l.createPriceLock(p, f.actors.assistant), 'assistant').id,
|
|
|
|
|
id = f.deliver(lock, '1'),
|
|
|
|
|
before = consistent(f.l, id),
|
|
|
|
|
input = f.payload(id),
|
|
|
|
|
fee = input.fee_items.find(row => row.system === 'credit_card');
|
|
|
|
|
fee.payer = 'assistant2';
|
|
|
|
|
f.edit(id, input, 'assistant');
|
|
|
|
|
const after = consistent(f.l, id),
|
|
|
|
|
event = f.l.priceLockDetail(lock).cash_events.find(row => row.card_fee_amount !== null);
|
|
|
|
|
assert.equal(after.directCost, before.directCost);
|
|
|
|
|
assert.equal(after.profit, before.profit);
|
|
|
|
|
assert.equal(event.card_fee_payer, 'assistant2');
|
|
|
|
|
assert.equal(event.card_fee_amount, 600);
|
|
|
|
|
assert.equal(after.fee_items.find(row => row.system === 'credit_card').payer, 'assistant2');
|
|
|
|
|
assert.equal(after.fee_items.find(row => row.system === 'credit_card').date, D);
|
|
|
|
|
assert.equal(f.l.reportByOrderDate(D).payerCosts.assistant2.paidFees, 600);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('池料改单审批遇来源变动过期,末尾故障连同池结转、采购更正、库存与快照全量回滚', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.buy('4', '3600');
|
|
|
|
|
f.enable();
|
|
|
|
|
const lock = f.lock('pool'),
|
|
|
|
|
id = f.deliver(lock, '1.5'),
|
|
|
|
|
p = f.payload(id);
|
|
|
|
|
p.price_lock_material = { source: 'manual', rate: '880', payer: 'owner', verified: true };
|
|
|
|
|
const r = f.request(id, p);
|
|
|
|
|
f.buy('1', '950');
|
|
|
|
|
assert.equal(f.l.orderChangeRequest(r.requestId, f.actors.owner).stale, true);
|
|
|
|
|
const pending = tables(f.l);
|
|
|
|
|
assert.throws(() => f.approve(r), /过期/);
|
|
|
|
|
assert.equal(tables(f.l), pending);
|
|
|
|
|
f.run({}, () => f.l.rejectOrderChange(r.requestId, { note: '来源已变,重新核对' }, f.actors.owner));
|
|
|
|
|
const next = f.payload(id);
|
|
|
|
|
next.price_lock_material = { source: 'manual', rate: '880', payer: 'owner', verified: true };
|
|
|
|
|
const fresh = f.request(id, next),
|
|
|
|
|
before = tables(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(fresh), /合成末尾故障/);
|
|
|
|
|
assert.equal(tables(f.l), before);
|
|
|
|
|
f.l.audit = audit;
|
|
|
|
|
assert.equal(f.approve(fresh).request.status, 'approved');
|
|
|
|
|
});
|
2026-11-08 02:22:58 +08:00
|
|
|
|
|
|
|
|
test('多商品逐行更正原料与补重,按稳定来源保留实付日和事件身份且不改整单收款', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
lock = f.lockRows([{ quantity: '1' }, { quantity: '2', rate: '800', payer: 'assistant2', sellRate: '1200' }]);
|
|
|
|
|
f.advance(D2);
|
|
|
|
|
const id = f.deliverRows(lock, ['1.2', '1.7']),
|
|
|
|
|
before = consistent(f.l, id),
|
|
|
|
|
saved = f.l.priceLockDetail(lock),
|
|
|
|
|
events = saved.material_events,
|
|
|
|
|
cash = saved.cash_events;
|
|
|
|
|
const p = f.payload(id);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
p.lines.map(row => row.lineId),
|
|
|
|
|
saved.lines.map(row => row.id),
|
|
|
|
|
);
|
|
|
|
|
assert.equal(p.price_lock_material, undefined);
|
|
|
|
|
p.lines[0].price_lock_material = { source: 'manual', rate: '860', payer: 'assistant2', verified: true };
|
|
|
|
|
p.lines[0].price_lock_extra_material = { rate: '910', payer: 'owner', verified: true };
|
|
|
|
|
p.lines[0].costParts.labor = '30';
|
|
|
|
|
p.lines[1].price_lock_material = { source: 'manual', rate: '810', payer: 'owner', verified: true };
|
|
|
|
|
f.edit(id, p);
|
|
|
|
|
const after = consistent(f.l, id),
|
|
|
|
|
updated = f.l.priceLockDetail(lock);
|
|
|
|
|
assert.equal(after.goodsCost, 247500);
|
|
|
|
|
assert.equal(after.total, before.total);
|
|
|
|
|
assert.equal(after.netReceived, before.netReceived);
|
|
|
|
|
assert.deepEqual(updated.cash_events, cash);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
after.lines.map(row => row.lineId),
|
|
|
|
|
before.lines.map(row => row.lineId),
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
updated.material_events.map(row => [row.id, row.date, row.source, row.line_index]),
|
|
|
|
|
events.map(row => [row.id, row.date, row.source, row.line_index]),
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
updated.material_events
|
|
|
|
|
.filter(row => row.source === 'original')
|
|
|
|
|
.map(row => [row.line_index, row.amount, row.payer]),
|
2026-12-04 08:58:53 -08:00
|
|
|
[
|
2026-11-08 02:22:58 +08:00
|
|
|
[0, 86000, 'assistant2'],
|
|
|
|
|
[1, 162000, 'owner'],
|
2026-12-04 08:58:53 -08:00
|
|
|
],
|
2026-11-08 02:22:58 +08:00
|
|
|
);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
updated.material_events.filter(row => row.source === 'extra').map(row => [row.line_index, row.amount, row.payer]),
|
|
|
|
|
[[0, 18200, 'owner']],
|
|
|
|
|
);
|
|
|
|
|
const stable = f.l.rows('SELECT * FROM price_lock_events ORDER BY rowid'),
|
|
|
|
|
entries = f.l.rows('SELECT * FROM team_fund_entries ORDER BY rowid');
|
|
|
|
|
f.edit(id, f.payload(id));
|
|
|
|
|
assert.deepEqual(f.l.rows('SELECT * FROM price_lock_events ORDER BY rowid'), stable);
|
|
|
|
|
assert.deepEqual(f.l.rows('SELECT * FROM team_fund_entries ORDER BY rowid'), entries);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('同单部分池行转手填只释放该行,保留另一行精确预留、余料与资金来源', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.buy('5', '4500', 'assistant2');
|
|
|
|
|
f.enable();
|
|
|
|
|
const lock = f.lockRows([{ source: 'pool' }, { source: 'pool' }, { rate: '820', payer: 'owner' }]);
|
|
|
|
|
f.advance(D2);
|
|
|
|
|
const id = f.deliverRows(lock, ['0.8', '0.9', '1.1']),
|
|
|
|
|
old = f.l.priceLockDetail(lock),
|
|
|
|
|
cash = old.cash_events,
|
|
|
|
|
otherReservation = f.l.one(
|
|
|
|
|
'SELECT * FROM price_lock_pool_reservations WHERE price_lock_id=? AND line_index=1',
|
|
|
|
|
lock,
|
|
|
|
|
),
|
|
|
|
|
otherReturn = f.l.one('SELECT * FROM price_lock_pool_returns WHERE price_lock_id=? AND line_index=1', lock),
|
|
|
|
|
p = f.payload(id);
|
|
|
|
|
p.lines[0].price_lock_material = { source: 'manual', rate: '880', payer: 'owner', verified: true };
|
|
|
|
|
p.lines[2].price_lock_material.rate = '830';
|
|
|
|
|
f.edit(id, p);
|
|
|
|
|
const after = consistent(f.l, id),
|
|
|
|
|
updated = f.l.priceLockDetail(lock);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
updated.lines.map(row => row.material_source),
|
|
|
|
|
['manual', 'pool', 'manual'],
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(updated.cash_events, cash);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
f.l.one('SELECT * FROM price_lock_pool_reservations WHERE price_lock_id=? AND line_index=1', lock),
|
|
|
|
|
otherReservation,
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
f.l.one('SELECT * FROM price_lock_pool_returns WHERE price_lock_id=? AND line_index=1', lock),
|
|
|
|
|
otherReturn,
|
|
|
|
|
);
|
|
|
|
|
assert.equal(
|
|
|
|
|
f.l.one('SELECT * FROM price_lock_pool_reservations WHERE price_lock_id=? AND line_index=0', lock),
|
|
|
|
|
undefined,
|
|
|
|
|
);
|
|
|
|
|
assert.equal(
|
|
|
|
|
f.l.one('SELECT * FROM price_lock_pool_returns WHERE price_lock_id=? AND line_index=0', lock),
|
|
|
|
|
undefined,
|
|
|
|
|
);
|
|
|
|
|
assert.equal(after.lines[0].priceLockPool, undefined);
|
|
|
|
|
assert.ok(after.lines[1].priceLockPool);
|
|
|
|
|
assert.equal(after.lines[1].costParts.material, 81000);
|
|
|
|
|
assert.equal(after.lines[2].costParts.material, 92000);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
updated.material_events
|
|
|
|
|
.filter(row => row.source === 'original')
|
|
|
|
|
.map(row => row.line_index)
|
|
|
|
|
.sort(),
|
|
|
|
|
[0, 2],
|
|
|
|
|
);
|
|
|
|
|
assert.equal(f.l.materialPoolDetail(D2).remainingMg, 4100);
|
|
|
|
|
assert.equal(f.l.one('PRAGMA integrity_check').integrity_check, 'ok');
|
|
|
|
|
assert.deepEqual(f.l.rows('PRAGMA foreign_key_check'), []);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('多商品锁价拒绝错配、漏行、重复行和未核实成本,失败时逐表回滚', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
lock = f.lockRows([{}, {}]),
|
|
|
|
|
id = f.deliverRows(lock, ['1.1', '1.2']);
|
|
|
|
|
const mutations = [
|
|
|
|
|
p => p.lines.reverse(),
|
|
|
|
|
p => p.lines.pop(),
|
|
|
|
|
p => (p.lines[1].lineId = p.lines[0].lineId),
|
|
|
|
|
p => delete p.lines[1].lineId,
|
|
|
|
|
p => (p.lines[1].quantity = '3'),
|
|
|
|
|
p => (p.lines[1].goldPrice = '1100'),
|
|
|
|
|
p => {
|
|
|
|
|
p.lines[1].price_lock_material.rate = '860';
|
|
|
|
|
p.lines[1].price_lock_material.verified = false;
|
|
|
|
|
},
|
|
|
|
|
p => {
|
|
|
|
|
p.lines[1].price_lock_extra_material.rate = '910';
|
|
|
|
|
p.lines[1].price_lock_extra_material.verified = false;
|
|
|
|
|
},
|
|
|
|
|
p => (p.lines[1].price_lock_material.source = 'pool'),
|
|
|
|
|
];
|
|
|
|
|
for (const mutate of mutations) {
|
|
|
|
|
const p = f.payload(id);
|
|
|
|
|
mutate(p);
|
|
|
|
|
const before = tables(f.l);
|
|
|
|
|
assert.throws(() => f.edit(id, p));
|
|
|
|
|
assert.equal(tables(f.l), before);
|
|
|
|
|
}
|
|
|
|
|
assert.throws(
|
|
|
|
|
() => f.l.asActor(f.actors.assistant2).orderEdit(id, f.actors.assistant2),
|
|
|
|
|
error => error.status === 404,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('多池行跨日审批同时移交付日并只释放一行,余料身份冻结且审批预览不落账', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.buy('5', '4500', 'assistant2');
|
|
|
|
|
f.enable();
|
|
|
|
|
const lock = f.lockRows([{ source: 'pool' }, { source: 'pool' }]);
|
|
|
|
|
f.advance(D2);
|
|
|
|
|
const id = f.deliverRows(lock, ['0.8', '0.9']),
|
|
|
|
|
before = consistent(f.l, id),
|
|
|
|
|
remaining = f.l.one('SELECT * FROM price_lock_pool_reservations WHERE price_lock_id=? AND line_index=1', lock),
|
|
|
|
|
returned = f.l.one('SELECT * FROM price_lock_pool_returns WHERE price_lock_id=? AND line_index=1', lock),
|
|
|
|
|
cash = f.l.priceLockDetail(lock).cash_events;
|
|
|
|
|
f.advance(D3);
|
|
|
|
|
const p = f.payload(id);
|
|
|
|
|
p.date = D3;
|
|
|
|
|
p.lines[0].price_lock_material = { source: 'manual', rate: '880', payer: 'owner', verified: true };
|
|
|
|
|
const old = finances(f.l),
|
|
|
|
|
request = f.request(id, p);
|
|
|
|
|
assert.equal(finances(f.l), old);
|
|
|
|
|
assert.equal(request.request.after.priceLockCosts.lines[0].materialSource, 'manual');
|
|
|
|
|
assert.equal(request.request.after.priceLockCosts.lines[1].materialSource, 'pool');
|
|
|
|
|
f.approve(request);
|
|
|
|
|
const after = consistent(f.l, id),
|
|
|
|
|
updated = f.l.priceLockDetail(lock),
|
|
|
|
|
next = f.l.one('SELECT * FROM price_lock_pool_returns WHERE id=?', returned.id);
|
|
|
|
|
assert.equal(after.date, D3);
|
|
|
|
|
assert.equal(after.total, before.total);
|
|
|
|
|
assert.equal(after.netReceived, before.netReceived);
|
|
|
|
|
assert.deepEqual(updated.cash_events, cash);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
updated.delivery.lines.map(row => row.date),
|
|
|
|
|
[D3, D3],
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
f.l.one('SELECT * FROM price_lock_pool_reservations WHERE price_lock_id=? AND line_index=1', lock),
|
|
|
|
|
remaining,
|
|
|
|
|
);
|
|
|
|
|
assert.equal(next.date, D3);
|
|
|
|
|
assert.equal(next.amount, returned.amount);
|
|
|
|
|
assert.deepEqual(JSON.parse(next.allocations), JSON.parse(returned.allocations));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('同时释放两件池行分别保存独立购料,重建不串行覆盖且末尾异常全量撤回', t => {
|
|
|
|
|
const f = fixture(t);
|
|
|
|
|
f.buy('5', '4500', 'assistant2');
|
|
|
|
|
f.enable();
|
|
|
|
|
const lock = f.lockRows([{ source: 'pool' }, { source: 'pool' }]),
|
|
|
|
|
id = f.deliverRows(lock, ['0.8', '0.9']),
|
|
|
|
|
p = f.payload(id);
|
|
|
|
|
p.lines[0].price_lock_material = { source: 'manual', rate: '880', payer: 'owner', verified: true };
|
|
|
|
|
p.lines[1].price_lock_material = { source: 'manual', rate: '920', payer: 'assistant', verified: true };
|
|
|
|
|
const before = tables(f.l),
|
|
|
|
|
audit = f.l.audit;
|
|
|
|
|
f.l.audit = function (entity, action, ...rest) {
|
|
|
|
|
if (action === '锁价销售单成本修改') throw new Error('合成多行末尾故障');
|
|
|
|
|
return audit.call(this, entity, action, ...rest);
|
|
|
|
|
};
|
|
|
|
|
assert.throws(() => f.edit(id, p), /合成多行末尾故障/);
|
|
|
|
|
assert.equal(tables(f.l), before);
|
|
|
|
|
f.l.audit = audit;
|
|
|
|
|
f.edit(id, p);
|
|
|
|
|
const o = consistent(f.l, id),
|
|
|
|
|
updated = f.l.priceLockDetail(lock);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
o.lines.map(row => row.costParts.material),
|
|
|
|
|
[70400, 82800],
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
updated.lines.map(row => [row.buy_rate, row.material_amount, row.material_source]),
|
2026-12-04 08:58:53 -08:00
|
|
|
[
|
2026-11-08 02:22:58 +08:00
|
|
|
[88000, 88000, 'manual'],
|
|
|
|
|
[92000, 92000, 'manual'],
|
2026-12-04 08:58:53 -08:00
|
|
|
],
|
2026-11-08 02:22:58 +08:00
|
|
|
);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
updated.material_events.map(row => [row.line_index, row.amount]),
|
2026-12-04 08:58:53 -08:00
|
|
|
[
|
2026-11-08 02:22:58 +08:00
|
|
|
[0, 88000],
|
|
|
|
|
[1, 92000],
|
2026-12-04 08:58:53 -08:00
|
|
|
],
|
2026-11-08 02:22:58 +08:00
|
|
|
);
|
|
|
|
|
assert.deepEqual(f.l.rows('SELECT * FROM price_lock_pool_reservations WHERE price_lock_id=?', lock), []);
|
|
|
|
|
assert.deepEqual(f.l.rows('SELECT * FROM price_lock_pool_returns WHERE price_lock_id=?', lock), []);
|
|
|
|
|
assert.equal(f.l.materialPoolDetail(D).remainingMg, 5000);
|
|
|
|
|
});
|