格式化: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
@@ -1,34 +1,138 @@
|
||||
'use strict';
|
||||
const {test}=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),os=require('node:os'),path=require('node:path'),{randomUUID}=require('node:crypto');
|
||||
const Calc=require('../public/calculations'),{Ledger}=require('../ledger'),{receipt}=require('../printing');
|
||||
const actor={id:'labor-test',name:'测试麦麦',role:'owner'},date='2026-10-17';
|
||||
const line=(extra={})=>({name:'足金999',unit:'g',quantity:'2.2',pocket:'',payer:'owner',pricingMode:'manual',baseSale:'2167',costParts:{material:'1980',labor:'9999',accessories:'0',other:'0'},costLabor:{mode:'gram',rate:'20'},...extra});
|
||||
function fixture(t){const dir=fs.mkdtempSync(path.join(os.tmpdir(),'maimai-purchase-labor-'));const l=new Ledger(path.join(dir,'ledger.sqlite'));t.after(()=>{l.close();fs.rmSync(dir,{recursive:true,force:true});});const run=(p,fn,key=randomUUID())=>l.transact(key,actor,p,fn);const save=(data={})=>run(data,()=>l.saveOrder({date,customer:'合成客户',salesperson:'合成开单人',discount:'0',lines:[line()],...data},actor)).id;const act=(id,a,p={})=>run({id,a,p},()=>l.action(id,a,{date,...p},actor));return {l,run,save,act};}
|
||||
test('进货克工费按克重算总额;总工费直接使用,不跟销售工费混算',()=>{
|
||||
const a=Calc.purchaseCosts(line());assert.equal(a.parts.labor,4400);assert.equal(a.total,202400);assert.deepEqual(a.costLabor,{mode:'gram',rate:2000});
|
||||
const b=Calc.purchaseCosts(line({quantity:'3',costLabor:{mode:'total',rate:'999'},costParts:{material:'1980',labor:'45',accessories:'0',other:'0'}}));assert.equal(b.parts.labor,4500);assert.equal(b.total,202500);assert.deepEqual(b.costLabor,{mode:'total'});
|
||||
assert.equal(Calc.purchaseCosts(line({quantity:'3'})).parts.labor,6000);
|
||||
const { test } = require('node:test'),
|
||||
assert = require('node:assert/strict'),
|
||||
fs = require('node:fs'),
|
||||
os = require('node:os'),
|
||||
path = require('node:path'),
|
||||
{ randomUUID } = require('node:crypto');
|
||||
const Calc = require('../public/calculations'),
|
||||
{ Ledger } = require('../ledger'),
|
||||
{ receipt } = require('../printing');
|
||||
const actor = { id: 'labor-test', name: '测试麦麦', role: 'owner' },
|
||||
date = '2026-10-17';
|
||||
const line = (extra = {}) => ({
|
||||
name: '足金999',
|
||||
unit: 'g',
|
||||
quantity: '2.2',
|
||||
pocket: '',
|
||||
payer: 'owner',
|
||||
pricingMode: 'manual',
|
||||
baseSale: '2167',
|
||||
costParts: { material: '1980', labor: '9999', accessories: '0', other: '0' },
|
||||
costLabor: { mode: 'gram', rate: '20' },
|
||||
...extra,
|
||||
});
|
||||
test('进货工费整数分四舍五入,空白和0分别处理,拒绝非法单位与数据',()=>{
|
||||
assert.equal(Calc.purchaseCosts(line({quantity:'0.001',costLabor:{mode:'gram',rate:'5'}})).parts.labor,1);
|
||||
assert.equal(Calc.purchaseCosts(line({quantity:'0.001',costLabor:{mode:'gram',rate:'4'}})).parts.labor,0);
|
||||
assert.equal(Calc.purchaseCosts(line({costLabor:{mode:'gram',rate:''}})).total,null);assert.equal(Calc.purchaseCosts(line({costLabor:{mode:'gram',rate:'0'}})).parts.labor,0);
|
||||
for(const extra of [{unit:'件'},{quantity:'0'},{quantity:'2.2001'},{costLabor:{mode:'gram',rate:'-1'}},{costLabor:{mode:'gram',rate:'1.001'}},{costLabor:{mode:'bad'}},{quantity:'100000',costLabor:{mode:'gram',rate:'100000'}}])assert.throws(()=>Calc.purchaseCosts(line(extra)));
|
||||
function fixture(t) {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'maimai-purchase-labor-'));
|
||||
const l = new Ledger(path.join(dir, 'ledger.sqlite'));
|
||||
t.after(() => {
|
||||
l.close();
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
const run = (p, fn, key = randomUUID()) => l.transact(key, actor, p, fn);
|
||||
const save = (data = {}) =>
|
||||
run(data, () =>
|
||||
l.saveOrder(
|
||||
{ date, customer: '合成客户', salesperson: '合成开单人', discount: '0', lines: [line()], ...data },
|
||||
actor,
|
||||
),
|
||||
).id;
|
||||
const act = (id, a, p = {}) => run({ id, a, p }, () => l.action(id, a, { date, ...p }, actor));
|
||||
return { l, run, save, act };
|
||||
}
|
||||
test('进货克工费按克重算总额;总工费直接使用,不跟销售工费混算', () => {
|
||||
const a = Calc.purchaseCosts(line());
|
||||
assert.equal(a.parts.labor, 4400);
|
||||
assert.equal(a.total, 202400);
|
||||
assert.deepEqual(a.costLabor, { mode: 'gram', rate: 2000 });
|
||||
const b = Calc.purchaseCosts(
|
||||
line({
|
||||
quantity: '3',
|
||||
costLabor: { mode: 'total', rate: '999' },
|
||||
costParts: { material: '1980', labor: '45', accessories: '0', other: '0' },
|
||||
}),
|
||||
);
|
||||
assert.equal(b.parts.labor, 4500);
|
||||
assert.equal(b.total, 202500);
|
||||
assert.deepEqual(b.costLabor, { mode: 'total' });
|
||||
assert.equal(Calc.purchaseCosts(line({ quantity: '3' })).parts.labor, 6000);
|
||||
});
|
||||
test('服务端重新计算工费,保存和再编辑不把总额乘第二次,改变克重跟着更新',t=>{
|
||||
const {l,save}=fixture(t),id=save();assert.equal(l.detail(id).goodsCost,202400);assert.equal(l.detail(id).lines[0].costParts.labor,4400);
|
||||
for(let i=0;i<2;i++)save({id,revision:l.detail(id).revision,lines:[line({costParts:{material:'1980',labor:'44',accessories:'0',other:'0'}})]});assert.equal(l.detail(id).goodsCost,202400);
|
||||
save({id,revision:l.detail(id).revision,lines:[line({quantity:'3'})]});assert.equal(l.detail(id).lines[0].costParts.labor,6000);assert.equal(l.detail(id).goodsCost,204000);
|
||||
test('进货工费整数分四舍五入,空白和0分别处理,拒绝非法单位与数据', () => {
|
||||
assert.equal(Calc.purchaseCosts(line({ quantity: '0.001', costLabor: { mode: 'gram', rate: '5' } })).parts.labor, 1);
|
||||
assert.equal(Calc.purchaseCosts(line({ quantity: '0.001', costLabor: { mode: 'gram', rate: '4' } })).parts.labor, 0);
|
||||
assert.equal(Calc.purchaseCosts(line({ costLabor: { mode: 'gram', rate: '' } })).total, null);
|
||||
assert.equal(Calc.purchaseCosts(line({ costLabor: { mode: 'gram', rate: '0' } })).parts.labor, 0);
|
||||
for (const extra of [
|
||||
{ unit: '件' },
|
||||
{ quantity: '0' },
|
||||
{ quantity: '2.2001' },
|
||||
{ costLabor: { mode: 'gram', rate: '-1' } },
|
||||
{ costLabor: { mode: 'gram', rate: '1.001' } },
|
||||
{ costLabor: { mode: 'bad' } },
|
||||
{ quantity: '100000', costLabor: { mode: 'gram', rate: '100000' } },
|
||||
])
|
||||
assert.throws(() => Calc.purchaseCosts(line(extra)));
|
||||
});
|
||||
test('补录成本也支持两种方式,利润和退货用锁定总额,客户单不泄露进货工费',t=>{
|
||||
const {l,save,act}=fixture(t),id=save({lines:[line({costLabor:{mode:'gram',rate:''}})]});act(id,'confirm');
|
||||
act(id,'cost',{costs:[{parts:{material:'1980',labor:'7777',accessories:'0',other:'0'},costLabor:{mode:'gram',rate:'20'}}]});assert.equal(l.detail(id).goodsCost,202400);
|
||||
act(id,'cost',{costs:[{parts:{material:'1980',labor:'45',accessories:'0',other:'0'},costLabor:{mode:'total'}}]});assert.equal(l.detail(id).goodsCost,202500);
|
||||
act(id,'cost',{costs:[{parts:{material:'1980',labor:'7777',accessories:'0',other:'0'},costLabor:{mode:'gram',rate:'20'}}]});
|
||||
act(id,'receipt',{amount:'2167',account:'助理微信',verified:true});act(id,'deliver',{verified:true});assert.equal(l.detail(id).profit,14300);assert.throws(()=>act(id,'cost',{costs:[{}]}),/锁定/);
|
||||
const p=l.publicOrder(id),html=receipt(p);assert.ok(!JSON.stringify(p).includes('costLabor'));assert.ok(!html.includes('进货'));assert.ok(!html.includes('44.00'));
|
||||
act(id,'return',{reason:'测试退货',verified:true});assert.equal(l.detail(id).profit,0);
|
||||
test('服务端重新计算工费,保存和再编辑不把总额乘第二次,改变克重跟着更新', t => {
|
||||
const { l, save } = fixture(t),
|
||||
id = save();
|
||||
assert.equal(l.detail(id).goodsCost, 202400);
|
||||
assert.equal(l.detail(id).lines[0].costParts.labor, 4400);
|
||||
for (let i = 0; i < 2; i++)
|
||||
save({
|
||||
id,
|
||||
revision: l.detail(id).revision,
|
||||
lines: [line({ costParts: { material: '1980', labor: '44', accessories: '0', other: '0' } })],
|
||||
});
|
||||
assert.equal(l.detail(id).goodsCost, 202400);
|
||||
save({ id, revision: l.detail(id).revision, lines: [line({ quantity: '3' })] });
|
||||
assert.equal(l.detail(id).lines[0].costParts.labor, 6000);
|
||||
assert.equal(l.detail(id).goodsCost, 204000);
|
||||
});
|
||||
test('历史总工费、按件商品和旧版补录接口保持原金额',t=>{
|
||||
const {l,save,act}=fixture(t);const parts={material:'100',labor:'30',accessories:'0',other:'0'};const id=save({lines:[line({unit:'件',quantity:'2',costLabor:undefined,costParts:parts})]});assert.equal(l.detail(id).goodsCost,13000);assert.deepEqual(l.detail(id).lines[0].costLabor,{mode:'total'});act(id,'confirm');act(id,'cost',{costs:[{...parts,labor:'35'}]});assert.equal(l.detail(id).goodsCost,13500);
|
||||
test('补录成本也支持两种方式,利润和退货用锁定总额,客户单不泄露进货工费', t => {
|
||||
const { l, save, act } = fixture(t),
|
||||
id = save({ lines: [line({ costLabor: { mode: 'gram', rate: '' } })] });
|
||||
act(id, 'confirm');
|
||||
act(id, 'cost', {
|
||||
costs: [
|
||||
{
|
||||
parts: { material: '1980', labor: '7777', accessories: '0', other: '0' },
|
||||
costLabor: { mode: 'gram', rate: '20' },
|
||||
},
|
||||
],
|
||||
});
|
||||
assert.equal(l.detail(id).goodsCost, 202400);
|
||||
act(id, 'cost', {
|
||||
costs: [{ parts: { material: '1980', labor: '45', accessories: '0', other: '0' }, costLabor: { mode: 'total' } }],
|
||||
});
|
||||
assert.equal(l.detail(id).goodsCost, 202500);
|
||||
act(id, 'cost', {
|
||||
costs: [
|
||||
{
|
||||
parts: { material: '1980', labor: '7777', accessories: '0', other: '0' },
|
||||
costLabor: { mode: 'gram', rate: '20' },
|
||||
},
|
||||
],
|
||||
});
|
||||
act(id, 'receipt', { amount: '2167', account: '助理微信', verified: true });
|
||||
act(id, 'deliver', { verified: true });
|
||||
assert.equal(l.detail(id).profit, 14300);
|
||||
assert.throws(() => act(id, 'cost', { costs: [{}] }), /锁定/);
|
||||
const p = l.publicOrder(id),
|
||||
html = receipt(p);
|
||||
assert.ok(!JSON.stringify(p).includes('costLabor'));
|
||||
assert.ok(!html.includes('进货'));
|
||||
assert.ok(!html.includes('44.00'));
|
||||
act(id, 'return', { reason: '测试退货', verified: true });
|
||||
assert.equal(l.detail(id).profit, 0);
|
||||
});
|
||||
test('历史总工费、按件商品和旧版补录接口保持原金额', t => {
|
||||
const { l, save, act } = fixture(t);
|
||||
const parts = { material: '100', labor: '30', accessories: '0', other: '0' };
|
||||
const id = save({ lines: [line({ unit: '件', quantity: '2', costLabor: undefined, costParts: parts })] });
|
||||
assert.equal(l.detail(id).goodsCost, 13000);
|
||||
assert.deepEqual(l.detail(id).lines[0].costLabor, { mode: 'total' });
|
||||
act(id, 'confirm');
|
||||
act(id, 'cost', { costs: [{ ...parts, labor: '35' }] });
|
||||
assert.equal(l.detail(id).goodsCost, 13500);
|
||||
});
|
||||
Reference in new issue
Block a user