格式化:Prettier统一代码风格(仅格式,无逻辑变化)
仅为 npm run format 的输出;黄金对照、旧库兼容、浏览器流程与单元测试全部通过且零差异。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3743d687e7
commit
28c64814f0
221 files changed
+116943
-26708
No files matched your search
+226
-55
@@ -1,59 +1,230 @@
|
||||
'use strict';
|
||||
const {test}=require('node:test'),assert=require('node:assert/strict'),{randomUUID}=require('node:crypto');
|
||||
const {Ledger,today}=require('../ledger');
|
||||
const {createUser}=require('../server');
|
||||
const D='2026-10-01',D2='2026-10-02',D3='2026-10-03';
|
||||
const line=(sale='100',cost='60')=>({name:'合成商品',unit:'件',quantity:'1',pocket:'',payer:'owner',sale,cost});
|
||||
function fixture(t){
|
||||
const ledger=new Ledger(':memory:'),actors={};t.after(()=>ledger.close());
|
||||
for(const member of ['owner','assistant','assistant2']){const role=member==='owner'?'owner':'assistant',id=createUser(ledger,'合成'+member,role,'synthetic-month-profit-password',member);actors[member]={id,role,name:'合成'+member,member};}
|
||||
ledger.enableTeamMode();
|
||||
const run=(input,fn)=>ledger.scope(actors.owner,()=>ledger.transact(randomUUID(),actors.owner,input,fn));
|
||||
const issue=(member='assistant',extra={},complete=true)=>{const input={date:D,member,customer:'合成月度客户',salesperson:'合成经办人',payment_account:{owner:'麦麦微信',assistant:'助理微信',assistant2:'助理2微信'}[member],rounding_mode:'cent',fee_items:[],lines:[line()],...extra};return run(input,()=>complete?ledger.issueOrder(input,actors.owner):ledger.saveOrder(input,actors.owner)).id;};
|
||||
const action=(id,kind,extra={})=>{const input={date:D2,...extra};return run(input,()=>ledger.action(id,kind,input,actors.owner));};
|
||||
const monthly=(month='2026-10',member='owner')=>ledger.asActor(actors[member]).teamProfits({month},actors[member]);
|
||||
const after=(member,date,amount)=>{const input={member,date,amount,customer:'合成售后客户',project:'合成维修费',payer:'owner'};return run(input,()=>ledger.saveAfterSale(input,actors.owner));};
|
||||
return {ledger,actors,run,issue,action,monthly,after};
|
||||
}
|
||||
const snapshot=ledger=>JSON.stringify(ledger.rows("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name").map(({name})=>[name,ledger.rows('SELECT * FROM "'+name+'"')]));
|
||||
|
||||
test('月度按两名助理归属汇总原利润与售后,采购、回款及老板业绩不混入',t=>{
|
||||
const f=fixture(t);f.issue('owner',{lines:[line('1000','1')]});f.issue('assistant');f.issue('assistant',{date:D2,lines:[line('200','110')],fee_items:[{key:'shipping',note:'合成运费',amount:'5',payer:'owner'}]});f.issue('assistant2',{date:D2,lines:[line('90','60')]});f.after('assistant2',D3,'12.34');
|
||||
const purchase={date:D3,grams:'10',total:'9000',payer:'owner'};f.run(purchase,()=>f.ledger.createMaterialPurchase(purchase,f.actors.owner));
|
||||
const actual=f.monthly();assert.equal(actual.basis,'orderDate');assert.equal(actual.from,D);assert.equal(actual.date,'2026-10-31');assert.deepEqual(actual.rows.map(row=>[row.member,row.profit,row.recordCount,row.afterSaleCount]),[['assistant',12500,2,0],['assistant2',1766,2,1]]);
|
||||
const original=f.ledger.asActor(f.actors.owner).reportFor({date:'2026-10-31',from:D,basis:'orderDate'},f.actors.owner);
|
||||
for(const row of actual.rows){assert.equal(row.profit,original.ownershipBusiness[row.member].profit);assert.equal(row.knownProfit,original.ownershipBusiness[row.member].knownProfit);assert.equal(row.shareRate,30);}
|
||||
assert.deepEqual(actual.rows.map(row=>row.estimatedShare),[3750,530]);
|
||||
for(const key of ['balances','pairs','transfers','payments','settlement'])assert.ok(!Object.hasOwn(actual,key));
|
||||
assert.deepEqual(f.monthly('2026-09').rows.map(row=>row.profit),[0,0]);
|
||||
const { test } = require('node:test'),
|
||||
assert = require('node:assert/strict'),
|
||||
{ randomUUID } = require('node:crypto');
|
||||
const { Ledger, today } = require('../ledger');
|
||||
const { createUser } = require('../server');
|
||||
const D = '2026-10-01',
|
||||
D2 = '2026-10-02',
|
||||
D3 = '2026-10-03';
|
||||
const line = (sale = '100', cost = '60') => ({
|
||||
name: '合成商品',
|
||||
unit: '件',
|
||||
quantity: '1',
|
||||
pocket: '',
|
||||
payer: 'owner',
|
||||
sale,
|
||||
cost,
|
||||
});
|
||||
|
||||
test('成本待补及旧草稿保留待核算,已核算部分不能当完整利润或分成',t=>{
|
||||
const f=fixture(t);f.issue();f.issue('assistant',{lines:[{name:'合成待补黄金',unit:'g',quantity:'1',pocket:'',payer:'owner',sale:'1000',costParts:{material:'',labor:'30',accessories:'0',other:'0'},costMaterial:{mode:'pool',poolId:D}}]});f.issue('assistant2',{},false);
|
||||
const [first,second]=f.monthly().rows;assert.equal(first.profit,null);assert.equal(first.knownProfit,4000);assert.equal(first.incomplete,true);assert.equal(first.unknownProfitCount,1);assert.equal(first.estimatedShare,null);assert.equal(second.profit,null);assert.equal(second.draftCount,1);assert.equal(second.estimatedShare,null);
|
||||
});
|
||||
|
||||
test('跨月退货冲回原开单月,售后按记录月,锁价未交付不生成利润',t=>{
|
||||
const f=fixture(t),returned=f.issue();f.action(returned,'return',{date:'2026-11-01',reason:'合成退货',verified:true});f.action(returned,'refund',{date:'2026-11-01',amount:'100',account:'助理微信',verified:true});f.after('assistant','2026-11-01','5');
|
||||
const lock={date:D,member:'assistant2',customer:'合成待交付客户',salesperson:'合成经办人',line:{name:'足金合成锁价',quantity:'1',goldPrice:'900',laborRate:'0',laborMode:'gram',pieces:'1',charges:[]},material:{rate:'850',payer:'owner',verified:true},payment:{amount:'900',account:'助理2微信',verified:true}};
|
||||
f.run(lock,()=>f.ledger.createPriceLock(lock,f.actors.owner));
|
||||
assert.deepEqual(f.monthly().rows.map(row=>[row.profit,row.recordCount]),[[0,1],[0,0]]);assert.deepEqual(f.monthly('2026-11').rows.map(row=>[row.profit,row.recordCount,row.estimatedShare]),[[-500,1,-150],[0,0,0]]);
|
||||
});
|
||||
|
||||
test('分成基于月利润合计后取30%,正负半分四舍五入且不按单累加',t=>{
|
||||
const f=fixture(t);f.issue('assistant',{lines:[line('0.05','0')]});f.issue('assistant',{lines:[line('0.05','0')]});f.after('assistant2',D,'0.05');
|
||||
assert.deepEqual(f.monthly().rows.map(row=>[row.profit,row.estimatedShare]),[[10,3],[-5,-2]]);
|
||||
f.after('assistant',D2,'0.05');assert.deepEqual(f.monthly().rows.map(row=>[row.profit,row.estimatedShare]),[[5,2],[-5,-2]]);
|
||||
});
|
||||
|
||||
test('默认本月、历史月末和月份边界明确,助理不能读取月汇总或登记转账',t=>{
|
||||
const f=fixture(t),current=f.ledger.asActor(f.actors.owner).teamProfits();assert.equal(current.month,today().slice(0,7));assert.equal(current.date,today());assert.equal(f.monthly('2024-02').date,'2024-02-29');
|
||||
for(const month of ['','2026-00','2026-13','1999-12','2026-1','2026-10-01','9999-12'])assert.throws(()=>f.monthly(month),error=>error.status===400);
|
||||
const before=snapshot(f.ledger),changes=f.ledger.one('SELECT total_changes() AS count').count;
|
||||
for(const member of ['assistant','assistant2']){
|
||||
assert.throws(()=>f.monthly('2026-10',member),error=>error.status===403);
|
||||
assert.throws(()=>f.ledger.asActor(f.actors[member]).transferTeamFunds({date:D},f.actors[member]),error=>error.status===403);
|
||||
assert.ok(f.ledger.asActor(f.actors[member]).dailyFunds({date:D},f.actors[member]).team,'本人日常往来仍可读取');
|
||||
function fixture(t) {
|
||||
const ledger = new Ledger(':memory:'),
|
||||
actors = {};
|
||||
t.after(() => ledger.close());
|
||||
for (const member of ['owner', 'assistant', 'assistant2']) {
|
||||
const role = member === 'owner' ? 'owner' : 'assistant',
|
||||
id = createUser(ledger, '合成' + member, role, 'synthetic-month-profit-password', member);
|
||||
actors[member] = { id, role, name: '合成' + member, member };
|
||||
}
|
||||
f.monthly();f.monthly('2024-02');assert.equal(f.ledger.one('SELECT total_changes() AS count').count,changes);assert.equal(snapshot(f.ledger),before);
|
||||
ledger.enableTeamMode();
|
||||
const run = (input, fn) => ledger.scope(actors.owner, () => ledger.transact(randomUUID(), actors.owner, input, fn));
|
||||
const issue = (member = 'assistant', extra = {}, complete = true) => {
|
||||
const input = {
|
||||
date: D,
|
||||
member,
|
||||
customer: '合成月度客户',
|
||||
salesperson: '合成经办人',
|
||||
payment_account: { owner: '麦麦微信', assistant: '助理微信', assistant2: '助理2微信' }[member],
|
||||
rounding_mode: 'cent',
|
||||
fee_items: [],
|
||||
lines: [line()],
|
||||
...extra,
|
||||
};
|
||||
return run(input, () => (complete ? ledger.issueOrder(input, actors.owner) : ledger.saveOrder(input, actors.owner)))
|
||||
.id;
|
||||
};
|
||||
const action = (id, kind, extra = {}) => {
|
||||
const input = { date: D2, ...extra };
|
||||
return run(input, () => ledger.action(id, kind, input, actors.owner));
|
||||
};
|
||||
const monthly = (month = '2026-10', member = 'owner') =>
|
||||
ledger.asActor(actors[member]).teamProfits({ month }, actors[member]);
|
||||
const after = (member, date, amount) => {
|
||||
const input = { member, date, amount, customer: '合成售后客户', project: '合成维修费', payer: 'owner' };
|
||||
return run(input, () => ledger.saveAfterSale(input, actors.owner));
|
||||
};
|
||||
return { ledger, actors, run, issue, action, monthly, after };
|
||||
}
|
||||
const snapshot = ledger =>
|
||||
JSON.stringify(
|
||||
ledger
|
||||
.rows("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
|
||||
.map(({ name }) => [name, ledger.rows('SELECT * FROM "' + name + '"')]),
|
||||
);
|
||||
|
||||
test('月度按两名助理归属汇总原利润与售后,采购、回款及老板业绩不混入', t => {
|
||||
const f = fixture(t);
|
||||
f.issue('owner', { lines: [line('1000', '1')] });
|
||||
f.issue('assistant');
|
||||
f.issue('assistant', {
|
||||
date: D2,
|
||||
lines: [line('200', '110')],
|
||||
fee_items: [{ key: 'shipping', note: '合成运费', amount: '5', payer: 'owner' }],
|
||||
});
|
||||
f.issue('assistant2', { date: D2, lines: [line('90', '60')] });
|
||||
f.after('assistant2', D3, '12.34');
|
||||
const purchase = { date: D3, grams: '10', total: '9000', payer: 'owner' };
|
||||
f.run(purchase, () => f.ledger.createMaterialPurchase(purchase, f.actors.owner));
|
||||
const actual = f.monthly();
|
||||
assert.equal(actual.basis, 'orderDate');
|
||||
assert.equal(actual.from, D);
|
||||
assert.equal(actual.date, '2026-10-31');
|
||||
assert.deepEqual(
|
||||
actual.rows.map(row => [row.member, row.profit, row.recordCount, row.afterSaleCount]),
|
||||
[
|
||||
['assistant', 12500, 2, 0],
|
||||
['assistant2', 1766, 2, 1],
|
||||
],
|
||||
);
|
||||
const original = f.ledger
|
||||
.asActor(f.actors.owner)
|
||||
.reportFor({ date: '2026-10-31', from: D, basis: 'orderDate' }, f.actors.owner);
|
||||
for (const row of actual.rows) {
|
||||
assert.equal(row.profit, original.ownershipBusiness[row.member].profit);
|
||||
assert.equal(row.knownProfit, original.ownershipBusiness[row.member].knownProfit);
|
||||
assert.equal(row.shareRate, 30);
|
||||
}
|
||||
assert.deepEqual(
|
||||
actual.rows.map(row => row.estimatedShare),
|
||||
[3750, 530],
|
||||
);
|
||||
for (const key of ['balances', 'pairs', 'transfers', 'payments', 'settlement'])
|
||||
assert.ok(!Object.hasOwn(actual, key));
|
||||
assert.deepEqual(
|
||||
f.monthly('2026-09').rows.map(row => row.profit),
|
||||
[0, 0],
|
||||
);
|
||||
});
|
||||
|
||||
test('成本待补及旧草稿保留待核算,已核算部分不能当完整利润或分成', t => {
|
||||
const f = fixture(t);
|
||||
f.issue();
|
||||
f.issue('assistant', {
|
||||
lines: [
|
||||
{
|
||||
name: '合成待补黄金',
|
||||
unit: 'g',
|
||||
quantity: '1',
|
||||
pocket: '',
|
||||
payer: 'owner',
|
||||
sale: '1000',
|
||||
costParts: { material: '', labor: '30', accessories: '0', other: '0' },
|
||||
costMaterial: { mode: 'pool', poolId: D },
|
||||
},
|
||||
],
|
||||
});
|
||||
f.issue('assistant2', {}, false);
|
||||
const [first, second] = f.monthly().rows;
|
||||
assert.equal(first.profit, null);
|
||||
assert.equal(first.knownProfit, 4000);
|
||||
assert.equal(first.incomplete, true);
|
||||
assert.equal(first.unknownProfitCount, 1);
|
||||
assert.equal(first.estimatedShare, null);
|
||||
assert.equal(second.profit, null);
|
||||
assert.equal(second.draftCount, 1);
|
||||
assert.equal(second.estimatedShare, null);
|
||||
});
|
||||
|
||||
test('跨月退货冲回原开单月,售后按记录月,锁价未交付不生成利润', t => {
|
||||
const f = fixture(t),
|
||||
returned = f.issue();
|
||||
f.action(returned, 'return', { date: '2026-11-01', reason: '合成退货', verified: true });
|
||||
f.action(returned, 'refund', { date: '2026-11-01', amount: '100', account: '助理微信', verified: true });
|
||||
f.after('assistant', '2026-11-01', '5');
|
||||
const lock = {
|
||||
date: D,
|
||||
member: 'assistant2',
|
||||
customer: '合成待交付客户',
|
||||
salesperson: '合成经办人',
|
||||
line: {
|
||||
name: '足金合成锁价',
|
||||
quantity: '1',
|
||||
goldPrice: '900',
|
||||
laborRate: '0',
|
||||
laborMode: 'gram',
|
||||
pieces: '1',
|
||||
charges: [],
|
||||
},
|
||||
material: { rate: '850', payer: 'owner', verified: true },
|
||||
payment: { amount: '900', account: '助理2微信', verified: true },
|
||||
};
|
||||
f.run(lock, () => f.ledger.createPriceLock(lock, f.actors.owner));
|
||||
assert.deepEqual(
|
||||
f.monthly().rows.map(row => [row.profit, row.recordCount]),
|
||||
[
|
||||
[0, 1],
|
||||
[0, 0],
|
||||
],
|
||||
);
|
||||
assert.deepEqual(
|
||||
f.monthly('2026-11').rows.map(row => [row.profit, row.recordCount, row.estimatedShare]),
|
||||
[
|
||||
[-500, 1, -150],
|
||||
[0, 0, 0],
|
||||
],
|
||||
);
|
||||
});
|
||||
|
||||
test('分成基于月利润合计后取30%,正负半分四舍五入且不按单累加', t => {
|
||||
const f = fixture(t);
|
||||
f.issue('assistant', { lines: [line('0.05', '0')] });
|
||||
f.issue('assistant', { lines: [line('0.05', '0')] });
|
||||
f.after('assistant2', D, '0.05');
|
||||
assert.deepEqual(
|
||||
f.monthly().rows.map(row => [row.profit, row.estimatedShare]),
|
||||
[
|
||||
[10, 3],
|
||||
[-5, -2],
|
||||
],
|
||||
);
|
||||
f.after('assistant', D2, '0.05');
|
||||
assert.deepEqual(
|
||||
f.monthly().rows.map(row => [row.profit, row.estimatedShare]),
|
||||
[
|
||||
[5, 2],
|
||||
[-5, -2],
|
||||
],
|
||||
);
|
||||
});
|
||||
|
||||
test('默认本月、历史月末和月份边界明确,助理不能读取月汇总或登记转账', t => {
|
||||
const f = fixture(t),
|
||||
current = f.ledger.asActor(f.actors.owner).teamProfits();
|
||||
assert.equal(current.month, today().slice(0, 7));
|
||||
assert.equal(current.date, today());
|
||||
assert.equal(f.monthly('2024-02').date, '2024-02-29');
|
||||
for (const month of ['', '2026-00', '2026-13', '1999-12', '2026-1', '2026-10-01', '9999-12'])
|
||||
assert.throws(
|
||||
() => f.monthly(month),
|
||||
error => error.status === 400,
|
||||
);
|
||||
const before = snapshot(f.ledger),
|
||||
changes = f.ledger.one('SELECT total_changes() AS count').count;
|
||||
for (const member of ['assistant', 'assistant2']) {
|
||||
assert.throws(
|
||||
() => f.monthly('2026-10', member),
|
||||
error => error.status === 403,
|
||||
);
|
||||
assert.throws(
|
||||
() => f.ledger.asActor(f.actors[member]).transferTeamFunds({ date: D }, f.actors[member]),
|
||||
error => error.status === 403,
|
||||
);
|
||||
assert.ok(
|
||||
f.ledger.asActor(f.actors[member]).dailyFunds({ date: D }, f.actors[member]).team,
|
||||
'本人日常往来仍可读取',
|
||||
);
|
||||
}
|
||||
f.monthly();
|
||||
f.monthly('2024-02');
|
||||
assert.equal(f.ledger.one('SELECT total_changes() AS count').count, changes);
|
||||
assert.equal(snapshot(f.ledger), before);
|
||||
});
|
||||
Reference in new issue
Block a user