Files
summer a506fb1228 完善订单审批、每日对账与信用卡手续费
加入助理订单变更复核、收款人汇总、每日报销和应转金额;优化库存筛选、订单明细与连续浏览。开单身份必填,信用卡手续费按实收4‰自动计算并默认由麦麦承担,打印优惠显示在人民币大写旁。

验证:484项测试通过;独立HTTP与浏览器覆盖普通订单、锁价、修改审批、断线恢复和打印隔离。旧报表兼容,本机升级原29表295行数据保持一致。
2026-10-23 09:49:36 +08:00

35 lines
4.7 KiB
JavaScript

'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);
});
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}=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),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);
});