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

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

68 lines
9.7 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict';
const test=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),path=require('node:path'),vm=require('node:vm');
const {randomUUID}=require('node:crypto'),Calc=require('../public/calculations'),Fees=require('../public/fee-estimates'),Templates=require('../public/order-templates'),Copy=require('../public/order-copy');
const D='2026-10-23',ID='11111111-1111-4111-8111-111111111111';
function draft(extra={}){return {date:D,customer:'合成客户',salesperson:'合成开单人',payment_account:'助理微信',rounding_mode:'cent',discount:'0',note:'',fee_items:[],fee_auto_initialized:true,lines:[{name:'足金999',unit:'g',quantity:'1.000',pocket:'',payer:'owner',pricingMode:'manual',baseSale:'100',sale:'100',charges:[],costParts:{material:'40',labor:'0',accessories:'0',other:'0'},costMaterial:{mode:'total'},costLabor:{mode:'total'},costPayers:{material:'owner',labor:'assistant'}}],...extra};}
function harness(){
const nodes=new Map([['#main',{innerHTML:''}],['#order-error',{innerHTML:''}],['#price-lock-error',{innerHTML:''}]]),saved=new Map(),calls=[];
const context=vm.createContext({console,LedgerCalc:Calc,FeeEstimates:Fees,OrderTemplates:Templates,OrderCopy:Copy,crypto:{randomUUID},setInterval(){},setTimeout(){},clearTimeout(){},
document:{querySelector:selector=>nodes.get(selector)||null,querySelectorAll:()=>[],addEventListener(){}},window:{scrollTo(){}},localStorage:{getItem:key=>saved.get(key)??null,setItem:(key,value)=>saved.set(key,String(value)),removeItem:key=>saved.delete(key)},sessionStorage:{getItem(){return null;},setItem(){}},__calls:calls,__draft:draft()});
const run=code=>vm.runInContext(code,context),json=code=>JSON.parse(JSON.stringify(run(code)));
for(const file of ['order-templates-ui.js','price-locks-ui.js','material-pools-ui.js','order-edit-ui.js','app.js'])run(fs.readFileSync(path.join(__dirname,'../public',file),'utf8').replace(/\ninit\(\);\s*$/,''));
run(`session={user:{id:'fields-owner',name:'麦麦',role:'owner'}};state={user:session.user,today:'${D}',pockets:['现货','金料']};updateTotal=function(){};updatePaperNumber=function(){};loadMaterialPoolChoices=function(){};loadOrderTemplates=function(){};syncPriceLockForm=function(){};toast=function(){};mutate=async function(url,input){__calls.push({url,input});return {id:'${ID}'};};finishOrderEditSave=async function(){};finishPriceLockSave=async function(){};`);
return {nodes,saved,calls,context,run,json};
}
function required(html,name){assert.match(html,new RegExp('<input name="'+name+'"[^>]*\\brequired\\b'));}
test('普通开单、两种修改、锁价创建的客户与开单人都有原生必填提示',()=>{
const h=harness();h.run('editDraft=__draft;renderCreate()');let html=h.nodes.get('#main').innerHTML;required(html,'customer');required(html,'salesperson');assert.match(html,/客户称呼/代号(必填)/);assert.match(html,/开单人(必填)/);
for(const financialEditable of [true,false]){
h.context.__response={eligible:true,revision:1,financialEditable,inputs:draft(),order:{id:ID,revision:1,date:D,number:'XS-001',paper_number:'20261023001',customer:'合成客户',salesperson:'合成开单人',payment_account:'助理微信',rounding_mode:'cent',total:10000,directCost:0,lines:[]}};
h.run(`tab='order-edit';useOrderEditResponse('${ID}',__response,null);renderOrderEditForm()`);html=h.nodes.get('#main').innerHTML;required(html,'customer');required(html,'salesperson');
}
h.run("tab='create';renderPriceLockCreate()");html=h.nodes.get('#main').innerHTML;required(html,'customer');required(html,'salesperson');assert.match(html,/开单人(必填)/);
});
test('客户与开单人不能只填空格,统一trim后提交且保留称呼代号内容',()=>{
const h=harness();
for(const key of ['customer','salesperson'])for(const blank of ['',null,' ','\t\n',' ']){h.context.__draft=draft({[key]:blank});assert.throws(()=>h.run('validateIssueDraft(__draft)'),new RegExp(key==='customer'?'客户称呼/代号必填':'开单人必填'));}
h.context.__draft=draft({customer:' 编号 001 ',salesperson:' 麦麦 '});assert.deepEqual(h.json('validateIssueDraft(__draft).customer'),'编号 001');assert.equal(h.run('validateIssueDraft(__draft).salesperson'),'麦麦');
h.context.__draft=draft({customer:'客'.repeat(61)});assert.throws(()=>h.run('validateIssueDraft(__draft)'),/1~60/);h.context.__draft=draft({salesperson:'名'.repeat(31)});assert.throws(()=>h.run('validateIssueDraft(__draft)'),/1~30/);assert.equal(h.calls.length,0);
});
test('基本资料修改也拒绝空客户或空开单人,正常提交发送trim后的值',async()=>{
const h=harness(),fields={customer:{value:' '},salesperson:{value:'麦麦'},note:{value:''},edit_reason:{value:'补充客户称呼'},edit_verified:{checked:true}};
const form={id:'order-edit-meta-form',dataset:{},querySelector:selector=>fields[selector.match(/name="([^"]+)"/)?.[1]]||null,querySelectorAll:()=>[]};h.nodes.set('#order-edit-meta-form',form);h.context.__form=form;
h.context.__response={eligible:true,revision:1,financialEditable:false,inputs:draft(),order:{id:ID,revision:1,lines:[]}};h.run(`tab='order-edit';useOrderEditResponse('${ID}',__response,null);rememberOrderEditDraft=function(){}`);
await assert.rejects(h.run('submitOrderEdit(__form)'),/客户称呼/代号必填/);fields.customer.value=' 客户A ';fields.salesperson.value=' ';await assert.rejects(h.run('submitOrderEdit(__form)'),/开单人必填/);assert.equal(h.calls.length,0);
fields.salesperson.value=' 麦麦 ';await h.run('submitOrderEdit(__form)');assert.equal(h.calls[0].input.customer,'客户A');assert.equal(h.calls[0].input.salesperson,'麦麦');
});
test('锁价创建的显式空开单人不能靠默认经办人通过,旧锁价交付身份保持原逻辑',async()=>{
const h=harness();h.context.__lock={customer:'客户A',salesperson:' ',line:{name:'足金999'}};h.run('collectPriceLockCreate=function(){return __lock}');h.context.__form={id:'price-lock-create-form'};await assert.rejects(h.run('submitPriceLockForm(__form)'),/开单人必填/);assert.equal(h.calls.length,0);
h.context.__draft=draft();h.run('delete __draft.customer;delete __draft.salesperson');assert.doesNotThrow(()=>h.run('validateIssueDraft(__draft,{requireIdentity:false})'),'只在沿用原锁价身份的交付商品校验中跳过身份,不擅自补写旧客户');
});
test('模板仍可在客户与开单人未填时保存预设,套用后的新订单必须补客户',()=>{
const h=harness();h.context.__draft=draft({customer:'',salesperson:''});assert.doesNotThrow(()=>h.run('OrderTemplates.fromDraft(__draft)'));const preset=h.json('OrderTemplates.fromDraft(__draft)');assert.equal(Object.hasOwn(preset,'customer'),false);h.context.__preset=preset;
h.context.__applied=h.run(`OrderTemplates.apply(__preset,{today:'${D}',salesperson:'麦麦'})`);assert.equal(h.context.__applied.customer,'');assert.equal(h.context.__applied.salesperson,'麦麦');assert.throws(()=>h.run('validateIssueDraft(__applied)'),/客户称呼/代号必填/);
});
test('内部成本和实际费用的强调限定屏幕样式,不修改打印资源',()=>{
const css=fs.readFileSync(path.join(__dirname,'../public/app.css'),'utf8');assert.match(css,/@media screen\{\.line-item>\.internal,form>\.panel\.internal\{[^}]*border-left:4px solid/);assert.match(css,/\.internal-title\{color:#6e4b17;font-size:17px;font-weight:700/);assert.match(css,/\.panel\.internal>\.panel-title h2,\.panel\.internal>h2\{[^}]*font-size:20px;font-weight:700/);
const h=harness();h.context.__draft=draft();assert.match(h.run('feeHTML(__draft)'),/实际费用(内部)/);assert.match(h.run('lineHTML(__draft.lines[0],1)'),/内部成本 · 客户单据不显示/);
});
test('自动手续费不会固化进模板或复制成普通费用,修改输入仍保留原元数据',()=>{
const expenseId='22222222-2222-4222-8222-222222222222',paymentId='33333333-3333-4333-8333-333333333333';
const auto={key:'card_fee:'+paymentId,system:'credit_card',source_kind:'payment',source_id:paymentId,expense_id:expenseId,note:'信用卡手续费',amount:400,payer:'owner',status:'recorded'};
const source={id:ID,date:D,salesperson:'麦麦',payment_account:'助理微信',discount:0,note:'',status:'delivered',pendingFees:[],corrections:[],fee_items:[auto],expenses:[{id:expenseId,date:D,note:auto.note,amount:400,payer:'owner'}],lines:[{name:'足金999',unit:'g',quantity:1000,pocket:'',payer:'owner',pricingMode:'manual',baseSale:10000,sale:10000,charges:[],cost:4000,costParts:{material:4000,labor:0,accessories:0,other:0},costMaterial:{mode:'total'},costLabor:{mode:'total'}}]};
const input=Copy.inputs(source);assert.equal(input.fee_items[0].system,'credit_card');assert.equal(input.fee_items[0].expense_id,expenseId);assert.equal(input.fee_items[0].amount,'4.00');
const copied=Copy.fromOrder(source,{today:D});assert.equal(copied.fee_items.length,0);assert.equal(copied.customer,'');assert.equal(JSON.stringify(copied).includes(expenseId),false);
const preset=Templates.fromDraft({...draft(),fee_items:[{...auto,amount:'4.00'},{key:'manual:'+randomUUID(),expense_id:expenseId,note:'同一自动费的旧引用',amount:'4.00',payer:'owner'}]});assert.equal(preset.fee_items.length,0);assert.equal(JSON.stringify(preset).includes(expenseId),false);
});
test('信用卡选项随服务端账户开放,模板支持信用卡渠道且仍须补客户',()=>{
const h=harness();assert.doesNotMatch(h.run('accountFields()'),/信用卡/);h.context.CreditCardFees=require('../public/credit-card-fees');h.run("state.accounts=['助理微信','麦麦微信','助理信用卡','麦麦信用卡']");assert.match(h.run('accountFields()'),/信用卡/);const preset=Templates.fromDraft(draft({payment_account:'麦麦信用卡'}));assert.equal(preset.payment_account,'麦麦信用卡');assert.equal(Templates.apply(preset,{today:D}).customer,'');
});