每日对账按当天的垫付、利润与代收计算应转金额

This commit is contained in:
summer committed 2026-11-07 00:30:02 +08:00
1 parent 39f80fbab7
commit 1cbcdc728c
7 files changed
+192 -45

No files matched your search

+51 -25
View File
@@ -1,6 +1,7 @@
'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');
const {currentAccount}=require('../ownership-business');
const D='2026-10-22',NEXT='2026-10-23',user={id:'receipts-ui-user',name:'合成麦麦',role:'owner'};
const payment=(id,kind,amount,account,extra={})=>({id,kind,amount,account,date:NEXT,order_date:D,order_id:'order-'+id,price_lock_id:null,number:'XS-'+id,paper_number:'纸单-'+id,customer:'客户-'+id,reference:'',created:NEXT+'T01:00:00Z',...extra});
function ownershipPerson(member,name,items){
@@ -10,7 +11,7 @@ function ownershipPerson(member,name,items){
}
function businessPerson(person,date=D,from=date,extra={}){
const costByPayer=Object.fromEntries(['owner','assistant','assistant2'].map(member=>[member,{member,name:{owner:'麦麦',assistant:'助理1',assistant2:'助理2'}[member],totalCost:0,knownTotalCost:0,missingCount:0,pendingCount:0,...extra.costByPayer?.[member]}])),payers=Object.values(costByPayer),others=payers.filter(row=>row.member!==person.member),knownTotalCost=payers.reduce((total,row)=>total+row.knownTotalCost,0),totalCost=payers.some(row=>row.totalCost===null)?null:knownTotalCost,sales=extra.sales??person.net,profit=Object.hasOwn(extra,'profit')?extra.profit:totalCost===null?null:sales-totalCost,incomplete=extra.incomplete??(totalCost===null||profit===null);
const result={member:person.member,name:person.name,basis:'orderDate',from,date,sales,netReceipts:person.net,totalCost,knownTotalCost,selfCost:costByPayer[person.member].totalCost,knownSelfCost:costByPayer[person.member].knownTotalCost,otherCost:others.some(row=>row.totalCost===null)?null:others.reduce((total,row)=>total+row.totalCost,0),knownOtherCost:others.reduce((total,row)=>total+row.knownTotalCost,0),profit,knownProfit:profit??0,incomplete,missingCostCount:payers.reduce((total,row)=>total+row.missingCount,0),pendingCostCount:payers.reduce((total,row)=>total+row.pendingCount,0),unknownProfitCount:profit===null?1:0,draftCount:0,recordCount:person.items.length,orderCount:person.items.length,afterSaleCount:0,hasActivity:person.items.length>0,costByPayer,difference:{amount:person.net-sales,reconciled:person.net===sales,reasons:person.net===sales?[]:[{code:'payment_sales_difference',text:'净收款与销售不同,按原账核对利润。'}]},equation:{basis:person.net===sales&&!incomplete?'netReceipts':'sales',revenue:person.net===sales&&!incomplete?person.net:sales,totalCost,profit,complete:!incomplete},settlement:{available:false,basis:'cumulative-through-date',from,date,dueOut:null,dueIn:null,partial:false,pendingCount:0,legacyCount:0,notice:'历史结算请核对原单。',counterparts:[]},...extra,costByPayer};return {...result,ownerAdvances:extra.ownerAdvances??{basis:'orderDate',from,date,days:[]},poolMaterial:extra.poolMaterial??{mg:0,amount:0,knownAmount:0,missingCount:0,hasPriceLocks:false,byPayer:Object.fromEntries(['owner','assistant','assistant2'].map(key=>[key,{amount:0,knownAmount:0}])),days:[],otherCosts:{amount:result.totalCost,knownAmount:result.knownTotalCost}}};
const result={member:person.member,name:person.name,basis:'orderDate',from,date,sales,netReceipts:person.net,totalCost,knownTotalCost,selfCost:costByPayer[person.member].totalCost,knownSelfCost:costByPayer[person.member].knownTotalCost,otherCost:others.some(row=>row.totalCost===null)?null:others.reduce((total,row)=>total+row.totalCost,0),knownOtherCost:others.reduce((total,row)=>total+row.knownTotalCost,0),profit,knownProfit:profit??0,incomplete,missingCostCount:payers.reduce((total,row)=>total+row.missingCount,0),pendingCostCount:payers.reduce((total,row)=>total+row.pendingCount,0),unknownProfitCount:profit===null?1:0,draftCount:0,recordCount:person.items.length,orderCount:person.items.length,afterSaleCount:0,hasActivity:person.items.length>0,costByPayer,difference:{amount:person.net-sales,reconciled:person.net===sales,reasons:person.net===sales?[]:[{code:'payment_sales_difference',text:'净收款与销售不同,按原账核对利润。'}]},equation:{basis:person.net===sales&&!incomplete?'netReceipts':'sales',revenue:person.net===sales&&!incomplete?person.net:sales,totalCost,profit,complete:!incomplete},settlement:{available:false,basis:'cumulative-through-date',from,date,dueOut:null,dueIn:null,partial:false,pendingCount:0,legacyCount:0,notice:'历史结算请核对原单。',counterparts:[]},...extra,costByPayer};return {...result,ownerAdvances:extra.ownerAdvances??advances(costByPayer.owner.knownTotalCost||costByPayer.owner.missingCount||costByPayer.owner.pendingCount?[advanceDay(date,[...(costByPayer.owner.knownTotalCost?[advanceItem('owner-paid','原订单成本',costByPayer.owner.knownTotalCost,{component:'unallocated'})]:[]),...Array.from({length:costByPayer.owner.missingCount},(_,i)=>advanceItem('missing-'+i,'成本待补',null)),...Array.from({length:costByPayer.owner.pendingCount},(_,i)=>advanceItem('pending-'+i,'费用待核对',null,{status:'pending'}))])]:[],from,date),poolMaterial:extra.poolMaterial??{mg:0,amount:0,knownAmount:0,missingCount:0,hasPriceLocks:false,byPayer:Object.fromEntries(['owner','assistant','assistant2'].map(key=>[key,{amount:0,knownAmount:0}])),days:[],otherCosts:{amount:result.totalCost,knownAmount:result.knownTotalCost}}};
}
function emptyDailyProfits(date,from=date){return {basis:'orderDate',date,from,rows:[],summary:{recordCount:0,orderCount:0,afterSaleCount:0,draftCount:0,sales:0,knownTotalCost:0,knownProfit:0,totalCost:0,profit:0,unknownCostCount:0,unknownProfitCount:0,incomplete:false}};}
function syncBusiness(response){response.dailyProfits=emptyDailyProfits(response.date,response.from);response.ownershipBusiness=Object.fromEntries(Object.entries(response.ownershipReceipts).map(([member,person])=>[member,businessPerson(person,response.date,response.from)]));return response;}
@@ -36,16 +37,25 @@ function report(date=D,from=date,empty=false){
orderRows:[],accounts,receiptsByOrderDate:{receipt,refund,net:receipt-refund},recipientReceipts,ownershipReceipts,payerCosts:{assistant:cost('助理'),owner:cost('麦麦')},
goods:0,expense:0,customerBalance:0,settlementDue:0,inventory:[],unsettled:[],warnings:[],close:null,priceLockActivity:null,materialPoolActivity:null});
}
function attachCurrentAccounts(response){
for(const b of Object.values(response.ownershipBusiness||{})){
if(b.currentAccount||!response.ownershipReceipts?.[b.member]||!b.ownerAdvances)continue;
const rows=Array.from({length:b.recordCount},(_,index)=>({date:b.date,profit:index<b.unknownProfitCount?null:0,known:{profit:index===0?b.knownProfit:0},incomplete:index===0&&b.incomplete}));
b.currentAccount=currentAccount(rows,response.ownershipReceipts[b.member],b.ownerAdvances,b.from,b.date);
}
return response;
}
function harness(initial=report()){
attachCurrentAccounts(initial);
const nodes=new Map(),handlers={},calls=[],saved=new Map(),content={innerHTML:'',attributes:{},setAttribute(name,value){this.attributes[name]=value;},insertAdjacentHTML(position,html){assert.equal(position,'beforeend');this.innerHTML+=html;}};
for(const [selector,node] of [['#main',{innerHTML:''}],['#report-content',content],['#report-daily-profits-content',{innerHTML:''}],['#report-date',{value:D}],['#report-month',{checked:false}]])nodes.set(selector,node);
const context=vm.createContext({console,URLSearchParams,crypto:{randomUUID},setInterval(){},setTimeout(){},clearTimeout(){},
document:{addEventListener(type,fn){(handlers[type]??=[]).push(fn);},querySelector:selector=>nodes.get(selector)||null,querySelectorAll:()=>[]},window:{scrollTo(){}},
localStorage:{getItem:key=>saved.get(key)??null,setItem:(key,value)=>saved.set(key,String(value)),removeItem:key=>saved.delete(key)},
__response:initial,__user:user,__calls:calls});
__response:initial,__user:user,__calls:calls,__attachCurrentAccounts:attachCurrentAccounts});
const run=code=>vm.runInContext(code,context);
for(const file of ['price-locks-ui.js','material-pools-ui.js','team-funds-ui.js','app.js'])run(fs.readFileSync(path.join(__dirname,'../public',file),'utf8').replace(/\ninit\(\);\s*$/,''));
run(`session={user:__user};state={user:__user,today:'${NEXT}'};tab='report';reportDate='${D}';api=async function(url){__calls.push({url});return __response;};openOrder=async function(id){__calls.push({openOrder:id});};openPriceLock=async function(id){__calls.push({openPriceLock:id});};`);
run(`session={user:__user};state={user:__user,today:'${NEXT}'};tab='report';reportDate='${D}';api=async function(url){__calls.push({url});return __attachCurrentAccounts(__response);};openOrder=async function(id){__calls.push({openOrder:id});};openPriceLock=async function(id){__calls.push({openPriceLock:id});};`);
const range=(date,month=false)=>{context.__date=date;context.__month=month;run('reportDate=__date;reportMonth=__month;');nodes.get('#report-date').value=date;nodes.get('#report-month').checked=month;};
return {context,run,nodes,handlers,calls,content,range};
}
@@ -86,7 +96,7 @@ test('单号与锁价单沿用现有只读打开动作,客户和备注安全
test('老板全部成员空日显示简洁空态,只有退款时仍显示退款及负净收入',async()=>{
const h=harness(report(D,D,true));await h.run('renderReport()');const html=h.content.innerHTML;
assert.equal((html.match(/class="report-recipient-total">¥0\.00/g)||[]).length,0);
assert.match(html,/所选范围暂无所属订单收入、成本或待转记录/);assert.doesNotMatch(html,/aria-label="助理所属订单退款明细"/);
assert.match(html,/所选范围暂无所属订单收入、成本或当日往来记录/);assert.doesNotMatch(html,/aria-label="助理所属订单退款明细"/);
const response=report(D,D,true),refund=payment('only-refund','refund',100,'助理现金');response.recipientReceipts.assistant={recipient:'assistant',name:'助理',receipt:0,refund:100,net:-100,items:[refund]};response.receiptsByOrderDate={receipt:0,refund:100,net:-100};response.ownershipReceipts.assistant=ownershipPerson('assistant','助理',[refund]);response.accounts=[{account:'助理现金',receipt:0,refund:100,net:-100}];syncBusiness(response);
h.context.__response=response;await h.run('renderReport()');assert.match(h.content.innerHTML,/<header class="report-recipient-summary"><h2 id="report-ownership-assistant-title">助理所属订单净收入<\/h2><div class="report-recipient-total">¥-1\.00/);assert.match(h.content.innerHTML,/实收 ¥0\.00<\/span><span>实退 ¥1\.00/);assert.match(table(h.content.innerHTML,'助理所属订单收款逐笔明细'),/所选范围没有助理所属订单收款记录/);assert.match(table(h.content.innerHTML,'助理所属订单退款逐笔明细'),/客户-only-refund/);
});
@@ -95,8 +105,8 @@ test('切换日月立即清空旧明细,晚到响应和失败不恢复旧收
const h=harness();await h.run('renderReport()');assert.equal(h.run('requireCurrentReport().date'),D);
const pending=[];h.context.__pending=pending;h.run('api=function(url){__calls.push({url});return new Promise((resolve,reject)=>__pending.push({resolve,reject}));};');
h.range(NEXT);const first=h.run('renderReport()');assert.equal(h.run('currentReport'),null);assert.equal(h.content.attributes['aria-busy'],'true');assert.doesNotMatch(h.content.innerHTML,/收款明细|133\.45/);assert.throws(()=>h.run('requireCurrentReport()'),/正在读取或已失效/);
h.range(NEXT,true);const second=h.run('renderReport()'),month=report(NEXT,'2026-10-01',true);pending[1].resolve(month);await second;
assert.equal(h.run('requireCurrentReport().from'),'2026-10-01');assert.match(h.content.innerHTML,/2026-10-01 至 2026-10-23/);assert.match(h.content.innerHTML,/所选范围暂无所属订单收入、成本或待转记录/);
h.range(NEXT,true);const second=h.run('renderReport()'),month=attachCurrentAccounts(report(NEXT,'2026-10-01',true));pending[1].resolve(month);await second;
assert.equal(h.run('requireCurrentReport().from'),'2026-10-01');assert.match(h.content.innerHTML,/2026-10-01 至 2026-10-23/);assert.match(h.content.innerHTML,/所选范围暂无所属订单收入、成本或当日往来记录/);
pending[0].resolve(report(NEXT));await first;assert.equal(h.run('currentReport.from'),'2026-10-01');assert.doesNotMatch(h.content.innerHTML,/客户-a/);
const failure=h.run('renderReport()');pending[2].reject(new Error('合成网络失败'));await failure;assert.equal(h.run('currentReport'),null);assert.equal(h.content.attributes['aria-busy'],'false');assert.match(h.content.innerHTML,/合成网络失败/);assert.doesNotMatch(h.content.innerHTML,/收款明细/);assert.throws(()=>h.run('requireCurrentReport()'),/正在读取或已失效/);
const wrongUser=h.run('renderReport()');h.run("state={...state,user:{id:'different-user'}};");pending[3].resolve(month);await wrongUser;assert.equal(h.run('currentReport'),null);assert.doesNotMatch(h.content.innerHTML,/收款明细/);
@@ -167,34 +177,35 @@ function businessReport(){
function memberCard(html,member){const match=html.match(new RegExp('<article[^>]*data-member="'+member+'"[^>]*>([\\s\\S]*?)</article>'));assert.ok(match,'应显示 '+member+' 个人经营卡');return match[1];}
function businessHarness(response=businessReport()){const h=harness(response);h.run("state={...state,user:{...state.user,member:'owner'}};");return h;}
test('个人经营卡闭环1500收入、680麦麦付款、820利润;本期480与累计2280分别展示',async()=>{
const h=businessHarness();await h.run('renderReport()');const html=memberCard(h.content.innerHTML,'assistant');assert.match(html,/report-recipient-total">¥1,500\.00/);assert.match(html,/所属订单总成本<\/h3><strong>¥680\.00/);assert.match(html,/本人付款 <b>¥0\.00/);assert.match(html,/他人付款 <b>¥680\.00/);assert.match(html,/麦麦 ¥680\.00/);assert.match(html,/所属订单净收入 <b>¥1,500\.00<\/b>/);assert.match(html,/订单总成本 <b>¥680\.00<\/b>/);assert.match(html,/<strong class="">¥820\.00/);
assert.match(html,/本人应转他人<\/span><strong>¥2,280\.00/);assert.match(html,/此前 本人→麦麦 ¥1,800\.00 · 本期新增 本人→麦麦 ¥480\.00/);assert.match(html,/本期实际已转 <b>¥0\.00/);assert.match(html,/截至所选日待转 <strong>¥2,280\.00/);assert.doesNotMatch(html,/本人应转他人<\/span><strong>¥680\.00/);assert.equal(h.calls.length,1);
const lines=h.run('reportOwnershipReceiptsText(currentReport).join(String.fromCharCode(10))');assert.match(lines,/所属订单总成本:¥680\.00;本人付款 ¥0\.00;他人付款 ¥680\.00/);assert.match(lines,/本期利润:¥820\.00/);assert.match(lines,/此前 ¥1,800\.00 + 本期新增 ¥480\.00 − 本期实际已转 ¥0\.00 = 截至所选日 ¥2,280\.00/);
test('当日往来用680垫付加820净利润减200代收得到1300,历史2280不进入页面或导出',async()=>{
const h=businessHarness();await h.run('renderReport()');const html=memberCard(h.content.innerHTML,'assistant');assert.match(html,/report-recipient-total">¥1,500\.00/);assert.match(html,/所属订单总成本<\/h3><strong>¥680\.00/);assert.match(html,/本人付款 <b>¥0\.00/);assert.match(html,/他人付款 <b>¥680\.00/);assert.match(html,/<strong class="">¥820\.00/);
assert.match(html,/<h3>当日往来账<\/h3>/);assert.match(html,/<h4>2026-10-22<\/h4><span>助理1应转麦麦<\/span><strong>¥1,300\.00/);assert.match(html,/麦麦垫付已用料费 ¥0\.00 + 麦麦垫付其他订单成本 ¥680\.00 + 本人当日订单净利润 ¥820\.00 − 麦麦代收净额 ¥200\.00 = ¥1,300\.00/);
assert.doesNotMatch(h.content.innerHTML,/累计往来|逐人核对待转|历史两人结算|¥2,280\.00|此前待转/);assert.equal(h.calls.length,1);
const lines=h.run('reportOwnershipReceiptsText(currentReport).join(String.fromCharCode(10))');assert.match(lines,/当日往来账/);assert.match(lines,/助理1应转麦麦 ¥1,300\.00/);assert.doesNotMatch(lines,/累计往来|逐人核对待转|此前|¥2,280\.00/);
});
test('老板全部保留售后成本only、0元真实售后和纯待转卡,没有收入不漏经营活动',async()=>{
const response=businessReport();response.ownershipBusiness.owner=businessPerson(response.ownershipReceipts.owner,D,D,{hasActivity:true,recordCount:1,afterSaleCount:1,costByPayer:{assistant2:{totalCost:1200,knownTotalCost:1200}}});response.ownershipBusiness.assistant2=businessPerson(response.ownershipReceipts.assistant2,D,D,{hasActivity:true,settlement:settlement([counterpart('owner',0,-1200)])});const h=businessHarness(response);await h.run('renderReport()');const owner=memberCard(h.content.innerHTML,'owner'),helper=memberCard(h.content.innerHTML,'assistant2');assert.match(owner,/含售后 1 笔/);assert.match(owner,/助理2 ¥12\.00/);assert.match(owner,/<strong class="danger-text">¥-12\.00/);assert.doesNotMatch(owner,/<h3>本人收款<\/h3>/);assert.match(helper,/他人应转本人<\/span><strong>¥12\.00/);
test('老板全部保留真实售后和0元售后,不因历史余额生成无当日活动卡',async()=>{
const response=businessReport();response.ownershipBusiness.owner=businessPerson(response.ownershipReceipts.owner,D,D,{hasActivity:true,recordCount:1,afterSaleCount:1,costByPayer:{assistant2:{totalCost:1200,knownTotalCost:1200}}});response.ownershipBusiness.assistant2=businessPerson(response.ownershipReceipts.assistant2,D,D,{hasActivity:true,settlement:settlement([counterpart('owner',0,-1200)])});const h=businessHarness(response);await h.run('renderReport()');const owner=memberCard(h.content.innerHTML,'owner');assert.match(owner,/含售后 1 笔/);assert.match(owner,/助理2 ¥12\.00/);assert.match(owner,/<strong class="danger-text">¥-12\.00/);assert.doesNotMatch(owner,/<h3>本人收款<\/h3>|当日往来账|麦麦应转麦麦/);assert.doesNotMatch(h.content.innerHTML,/report-ownership-assistant2-title/);
response.ownershipBusiness.owner=businessPerson(response.ownershipReceipts.owner,D,D,{hasActivity:true,recordCount:1,afterSaleCount:1});await h.run('renderReport()');assert.match(memberCard(h.content.innerHTML,'owner'),/含售后 1 笔/);
});
test('现金净收与销售不同使用原销售利润式,差异说明转义,月汇总名称准确',async()=>{
const response=businessReport();response.ownershipBusiness.assistant=businessPerson(response.ownershipReceipts.assistant,D,D,{sales:170000,costByPayer:{owner:{totalCost:68000,knownTotalCost:68000}},difference:{amount:-20000,reconciled:false,reasons:[{code:'price_lock_timing',text:'锁价跨日与<原单>分开核对。'}]}});const h=businessHarness(response);await h.run('renderReport()');const html=memberCard(h.content.innerHTML,'assistant');assert.match(html,/销售净额 <b>¥1,700\.00/);assert.match(html,/<strong class="">¥1,020\.00/);assert.match(html,/差额 ¥-200\.00/);assert.match(html,/锁价跨日与&lt;原单&gt;分开核对/);assert.doesNotMatch(html,/<原单>|<strong class="">¥820\.00/);
response.from='2026-10-01';response.dailyProfits.from=response.from;for(const b of Object.values(response.ownershipBusiness)){b.from=response.from;b.settlement.from=response.from;b.ownerAdvances.from=response.from;}h.range(D,true);await h.run('renderReport()');assert.match(memberCard(h.content.innerHTML,'assistant'),/<h3>本期利润<\/h3>/);
response.from='2026-10-01';response.dailyProfits.from=response.from;for(const b of Object.values(response.ownershipBusiness)){b.from=response.from;b.settlement.from=response.from;b.ownerAdvances.from=response.from;delete b.currentAccount;}h.range(D,true);await h.run('renderReport()');assert.match(memberCard(h.content.innerHTML,'assistant'),/<h3>本期利润<\/h3>/);
});
test('成本待补只显示已知付款和待核算,已核算利润不能冒充完整利润或结清',async()=>{
const response=businessReport();response.ownershipBusiness.assistant=businessPerson(response.ownershipReceipts.assistant,D,D,{knownProfit:82000,profit:null,costByPayer:{owner:{totalCost:null,knownTotalCost:68000,missingCount:1}},settlement:settlement([] ,D,D,{partial:true,pendingCount:1,notice:'成本待补,仅列已知往来。'})});const h=businessHarness(response);await h.run('renderReport()');const html=memberCard(h.content.innerHTML,'assistant');assert.match(html,/已知 ¥680\.00/);assert.match(html,/成本待补齐/);assert.match(html,/report-business-profit-pending">待核算/);assert.match(html,/已知本人应转他人/);assert.match(html,/成本待补,仅列已知往来/);assert.doesNotMatch(html,/¥820\.00|已结清/);const text=h.run('reportOwnershipReceiptsText(currentReport).join(String.fromCharCode(10))');assert.match(text,/本期利润:待核算/);assert.doesNotMatch(text,/本期利润:¥820\.00/);
test('成本待补保留已知垫付与已核算利润,但不输出完整应转或已结清',async()=>{
const response=businessReport();response.ownershipBusiness.assistant=businessPerson(response.ownershipReceipts.assistant,D,D,{knownProfit:82000,profit:null,costByPayer:{owner:{totalCost:null,knownTotalCost:68000,missingCount:1}},settlement:settlement([] ,D,D,{partial:true,pendingCount:1,notice:'成本待补,仅列已知往来。'})});const h=businessHarness(response);await h.run('renderReport()');const html=memberCard(h.content.innerHTML,'assistant');assert.match(html,/已知 ¥680\.00/);assert.match(html,/成本待补齐/);assert.match(html,/report-business-profit-pending">待核算/);assert.match(html,/应转金额待核算/);assert.match(html,/本人当日订单净利润 待核算(已核算 ¥820\.00)/);assert.match(html,/暂不显示完整应转金额/);assert.doesNotMatch(html,/助理1应转麦麦|已结清/);const text=h.run('reportOwnershipReceiptsText(currentReport).join(String.fromCharCode(10))');assert.match(text,/本期利润:待核算/);assert.doesNotMatch(text,/本期利润:¥820\.00|应转麦麦 ¥/);
});
test('逐人待转保留双向余额与负数转入,期初加本期减实转,不抵销成一个净数字',async()=>{
const response=businessReport();response.ownershipBusiness.assistant.settlement=settlement([counterpart('owner',-5000,8000,10000),counterpart('assistant2',10000,-3000,-2000)]);const h=businessHarness(response);await h.run('renderReport()');const html=memberCard(h.content.innerHTML,'assistant');assert.match(html,/本人应转他人<\/span><strong>¥90\.00/);assert.match(html,/他人应转本人<\/span><strong>¥70\.00/);assert.match(html,/此前 麦麦→本人 ¥50\.00/);assert.match(html,/本期已转 助理2→本人 ¥20\.00/);assert.match(html,/本期实际已转 <b>(−¥20\.00)/);assert.match(html,/麦麦应转本人 ¥70\.00/);assert.match(html,/本人应转助理2 ¥90\.00/);assert.doesNotMatch(html,/本人应转他人<\/span><strong>¥20\.00/);
test('当日计算为负时明确麦麦转给归属人,历史双向余额和已转均不混入',async()=>{
const response=businessReport();response.ownershipBusiness.assistant=businessPerson(response.ownershipReceipts.assistant,D,D,{sales:13000,costByPayer:{owner:{totalCost:68000,knownTotalCost:68000}},settlement:settlement([counterpart('owner',-5000,8000,10000),counterpart('assistant2',10000,-3000,-2000)])});const h=businessHarness(response);await h.run('renderReport()');const html=memberCard(h.content.innerHTML,'assistant');assert.match(html,/麦麦应转助理1<\/span><strong>¥70\.00/);assert.match(html,/本人当日订单净利润 ¥-550\.00 − 麦麦代收净额 ¥200\.00 = ¥-70\.00/);assert.doesNotMatch(html,/逐人核对待转|此前待转|累计往来|本人应转他人/);assert.match(h.run('reportOwnershipReceiptsText(currentReport).join(String.fromCharCode(10))'),/麦麦应转助理1 ¥70\.00/);
});
test('经营投影缺失、付款不平、利润等式或待转构成错误均阻止保存;历史待转不冒充0',async()=>{
const changes=[r=>delete r.ownershipBusiness,r=>r.ownershipBusiness.assistant.netReceipts++,r=>r.ownershipBusiness.assistant.knownTotalCost++,r=>r.ownershipBusiness.assistant.equation.profit++,r=>r.ownershipBusiness.assistant.settlement.counterparts[0].closingDue++,r=>delete r.ownershipBusiness.assistant.costByPayer.owner];
for(const change of changes){const response=businessReport();change(response);const h=businessHarness(response);await h.run('renderReport()');assert.equal(h.run('currentReport'),null);assert.match(h.content.innerHTML,/所属订单成本、利润或待转资料未完整返回/);assert.throws(()=>h.run('requireCurrentReport()'),/正在读取或已失效/);}
const legacy=report();legacy.ownershipBusiness.assistant.settlement.partial=true;legacy.ownershipBusiness.assistant.settlement.legacyCount=1;const h=harness(legacy);await h.run('renderReport()');const html=memberCard(h.content.innerHTML,'assistant');assert.match(html,/待核对历史结算,暂不显示待转金额/);assert.doesNotMatch(html,/本人应转他人<\/span><strong>¥0\.00|已结清/);
const legacy=report();legacy.ownershipBusiness.assistant.settlement.partial=true;legacy.ownershipBusiness.assistant.settlement.legacyCount=1;const h=harness(legacy);await h.run('renderReport()');const html=memberCard(h.content.innerHTML,'assistant');assert.match(html,/当日往来账/);assert.doesNotMatch(html,/待核对历史结算|累计往来/);assert.doesNotMatch(html,/本人应转他人<\/span><strong>¥0\.00|已结清/);
});
function advanceItem(id,name,amount,extra={}){return {date:D,kind:'order',id,number:'XS-'+id,paperNumber:'纸单-'+id,customer:'垫付客户-'+id,name,source:'合成真实成本',component:'fee',poolId:null,poolMaterial:false,allocationPending:false,amount,estimatedAmount:null,status:amount===null?'missing':'recorded',...extra};}
@@ -211,7 +222,7 @@ function poolBusinessReport(){
}
test('料池用料在所属订单卡直接可见,保留原分币金额、麦麦垫付和其他成本,待转不再加一次',async()=>{
const h=businessHarness(poolBusinessReport());await h.run('renderReport()');const html=memberCard(h.content.innerHTML,'assistant');
assert.match(html,/所属订单总成本<\/h3><strong>¥1,972\.33/);assert.match(html,/料池已用料费<\/span><strong>¥1,933\.33/);assert.match(html,/2\.000g × ¥966\.67\/g = ¥1,933\.33/);assert.match(html,/其中麦麦垫付 <b>¥1,033\.33/);assert.match(html,/其他成本 <b>¥39\.00/);assert.match(html,/本人应转他人<\/span><strong>¥1,369\.33/);assert.match(html,/已记入待转的部分不再另加/);assert.match(html,/未四舍五入均价/);assert.doesNotMatch(html,/1,933\.34|3,302\.66/);
assert.match(html,/所属订单总成本<\/h3><strong>¥1,972\.33/);assert.match(html,/料池已用料费<\/span><strong>¥1,933\.33/);assert.match(html,/2\.000g × ¥966\.67\/g = ¥1,933\.33/);assert.match(html,/其中麦麦垫付 <b>¥1,033\.33/);assert.match(html,/其他成本 <b>¥39\.00/);assert.match(html,/助理1应转麦麦<\/span><strong>¥397\.00/);assert.match(html,/公式中计入一次/);assert.match(html,/未四舍五入均价/);assert.doesNotMatch(html,/1,933\.34|3,302\.66/);
assert.match(html,/麦麦垫付合计:料池料费 ¥1,033\.33 + 其他成本 ¥36\.00 = ¥1,069\.33/);assert.doesNotMatch(html,/麦麦垫付合计:料池料费 ¥1,033\.33 + 其他成本 ¥39\.00/);
const text=h.run('reportOwnershipReceiptsText(currentReport).join(String.fromCharCode(10))');assert.match(text,/料池已用料费:¥1,933\.33/);assert.match(text,/2\.000g × ¥966\.67\/g = ¥1,933\.33/);assert.match(text,/其中麦麦垫付:¥1,033\.33/);assert.match(text,/其他成本:¥39\.00/);
assert.match(text,/麦麦垫付合计:料池料费 ¥1,033\.33 + 其他成本 ¥36\.00 = ¥1,069\.33/);
@@ -239,7 +250,7 @@ test('同日不同锁价原均价分别显示,不合成新料价;来源不
test('多日料池各列原均价与耗用额,跨日锁价说明不把交付料费当作本日新往来',async()=>{
const response=poolBusinessReport(),pool=response.ownershipBusiness.assistant.poolMaterial,prior='2026-10-21';
pool.hasPriceLocks=true;pool.days=[{...pool.days[0],poolId:prior,date:prior,mg:1000,averageRate:90000,amount:90000,knownAmount:90000,byPayer:{owner:{amount:0,knownAmount:0},assistant:{amount:90000,knownAmount:90000},assistant2:{amount:0,knownAmount:0}}},{...pool.days[0],mg:1000,averageRate:103333,amount:103333,knownAmount:103333,byPayer:{owner:{amount:103333,knownAmount:103333},assistant:{amount:0,knownAmount:0},assistant2:{amount:0,knownAmount:0}}}];
const h=businessHarness(response);await h.run('renderReport()');const html=memberCard(h.content.innerHTML,'assistant');assert.match(html,/2026-10-21 料池/);assert.match(html,/1\.000g × ¥900\.00\/g = ¥900\.00/);assert.match(html,/2026-10-22 料池/);assert.match(html,/1\.000g × ¥1,033\.33\/g = ¥1,033\.33/);assert.match(html,/原预留与回料日期记往来,交付日不再加一次/);assert.match(html,/本人应转他人<\/span><strong>¥1,369\.33/);
const h=businessHarness(response);await h.run('renderReport()');const html=memberCard(h.content.innerHTML,'assistant');assert.match(html,/2026-10-21 料池/);assert.match(html,/1\.000g × ¥900\.00\/g = ¥900\.00/);assert.match(html,/2026-10-22 料池/);assert.match(html,/1\.000g × ¥1,033\.33\/g = ¥1,033\.33/);assert.match(html,/交付日的当日往来中列示,不重复记实际采购支出/);assert.match(html,/助理1应转麦麦<\/span><strong>¥397\.00/);
});
test('待补料费与未知FIFO付款人显示已知额和待补,其他已核实成本继续显示',async()=>{
@@ -268,9 +279,9 @@ test('麦麦逐笔垫付默认收起且客户可打开原单,多人池料子
const response=poolBusinessReport(),day=response.ownershipBusiness.assistant.ownerAdvances.days[0];day.items[0].customer='<img src=x>';day.items[1].name='<script>实际运费</script>';day.items[1].source='<b>付款备注</b>';
const h=businessHarness(response);await h.run('renderReport()');const card=memberCard(h.content.innerHTML,'assistant'),html=table(card,D+' 麦麦垫付明细');
assert.match(card,/<details class="report-owner-advances"[^>]*><summary>当日麦麦垫付明细<\/summary>/);assert.doesNotMatch(card,/<details[^>]*report-owner-advances[^>]*\sopen(?:\s|=|>)/);assert.match(card,/垫付小计 <strong>¥1,069\.33/);assert.match(html,/<th>日期<\/th><th>客户/单号<\/th><th>成本项目<\/th><th class="number">麦麦承担金额/);assert.match(html,/料池料费 · 麦麦垫付部分/);assert.match(html,/¥1,033\.33/);assert.doesNotMatch(html,/2\.000g|×/);assert.match(html,/&lt;img src=x&gt;/);assert.match(html,/&lt;script&gt;实际运费&lt;\/script&gt;/);assert.match(html,/&lt;b&gt;付款备注&lt;\/b&gt;/);assert.doesNotMatch(html,/<img|<script|<b>/);
assert.ok(card.indexOf('report-owner-advances')<card.indexOf('report-business-history'));assert.match(card,/<details class="report-business-settlement report-business-history"><summary>累计往来(含历史/);assert.doesNotMatch(card,/<details[^>]*report-business-history[^>]* open/);
assert.ok(card.indexOf('report-owner-advances')<card.indexOf('report-business-current'));assert.match(card,/<section class="report-business-settlement report-business-current"/);assert.doesNotMatch(card,/report-business-history|累计往来|逐人核对待转/);
const button={dataset:{action:'open-order',id:'pool'},closest(){return null;}};for(const handler of h.handlers.click)await handler({target:{closest:()=>button}});assert.deepEqual(JSON.parse(JSON.stringify(h.calls.slice(1))),[{openOrder:'pool'}]);
const text=h.run('reportOwnershipReceiptsText(currentReport).join(String.fromCharCode(10))');assert.match(text,/2026-10-22 垫付小计:¥1,069\.33/);assert.match(text,/累计往来(含历史,截至 2026-10-22)/);
const text=h.run('reportOwnershipReceiptsText(currentReport).join(String.fromCharCode(10))');assert.match(text,/2026-10-22 垫付小计:¥1,069\.33/);assert.match(text,/2026-10-22 \/ 助理1应转麦麦 ¥397\.00/);assert.doesNotMatch(text,/累计往来|逐人核对待转/);
});
test('月份垫付明细只列实际记录日期及各日小计,不把月合计或历史余额当每天垫付',async()=>{
@@ -281,10 +292,10 @@ test('月份垫付明细只列实际记录日期及各日小计,不把月合
const text=h.run('reportOwnershipReceiptsText(currentReport).join(String.fromCharCode(10))');assert.match(text,/2026-10-21 垫付小计:¥100\.00/);assert.match(text,/2026-10-22 垫付小计:¥1,069\.33/);assert.doesNotMatch(text,/垫付小计:¥1,169\.33/);
});
test('只有历史余额的空日垫付显示0且不暗示结清,预估未付款仍不计垫付小计',async()=>{
test('只有历史余额的空日不显示历史卡,所选成员显示当日0,预估未付不计垫付',async()=>{
const response=report(D,D,true);response.ownershipBusiness.assistant.hasActivity=true;response.ownershipBusiness.assistant.settlement=settlement([counterpart('owner',50000,0)]);
const h=harness(response);await h.run('renderReport()');let card=memberCard(h.content.innerHTML,'assistant');assert.match(card,/垫付小计 <strong>¥0\.00/);assert.match(card,/当天没有已登记的麦麦垫付明细/);assert.match(card,/累计往来(含历史/);assert.doesNotMatch(card,/已结清|垫付小计 <strong>¥500\.00/);
response.ownershipBusiness.assistant.ownerAdvances=advances([advanceDay(D,[advanceItem('estimate','预估未付运费',null,{status:'pending',estimatedAmount:100000})])]);await h.run('renderReport()');card=memberCard(h.content.innerHTML,'assistant');assert.match(card,/垫付小计 <strong>¥0\.00/);assert.match(table(card,D+' 麦麦垫付明细'),/待核对(预估 ¥1,000\.00),未计垫付/);assert.match(card,/预估金额不计垫付小计/);
const h=harness(response);await h.run('renderReport()');assert.doesNotMatch(h.content.innerHTML,/report-ownership-assistant-title|累计往来/);h.run("reportMember='assistant';");await h.run('renderReport()');let card=memberCard(h.content.innerHTML,'assistant');assert.match(card,/垫付小计 <strong>¥0\.00/);assert.match(card,/当日无需转款<\/span><strong>¥0\.00/);assert.doesNotMatch(card,/已结清|¥500\.00|累计往来/);
const b=response.ownershipBusiness.assistant;b.recordCount=1;b.unknownProfitCount=1;b.profit=null;b.incomplete=true;b.equation.profit=null;b.equation.complete=false;b.ownerAdvances=advances([advanceDay(D,[advanceItem('estimate','预估未付运费',null,{status:'pending',estimatedAmount:100000})])]);delete b.currentAccount;await h.run('renderReport()');card=memberCard(h.content.innerHTML,'assistant');assert.match(card,/垫付小计 <strong>¥0\.00/);assert.match(table(card,D+' 麦麦垫付明细'),/待核对(预估 ¥1,000\.00),未计垫付/);assert.match(card,/预估金额不计垫付小计/);assert.match(card,/应转金额待核算/);
});
test('垫付明细缺失、日期越界、小计不平或付款项目错误时阻止回执和下载',async()=>{
@@ -312,3 +323,18 @@ test('每日利润缺失、越权归属、利润错误、日期越界或合计
const changes=[r=>delete r.dailyProfits,r=>r.dailyProfits.rows[0].member='assistant2',r=>r.dailyProfits.rows[0].profit++,r=>r.dailyProfits.rows[0].date='2026-10-21',r=>r.dailyProfits.summary.knownProfit++];
for(const change of changes){const response=dailyProfitReport();change(response);const h=harness(response);await h.run('renderReport()');assert.equal(h.run('currentReport'),null);assert.match(h.content.innerHTML,/每日利润表未完整返回或与当前对账金额不一致/);assert.throws(()=>h.run('requireCurrentReport()'),/正在读取或已失效/);}
});
test('月模式当日往来逐日列日期方向和金额,不将月利润或合计贴在某一天',async()=>{
const response=businessReport(),from='2026-10-01',prior='2026-10-21';response.from=from;response.dailyProfits.from=from;
for(const b of Object.values(response.ownershipBusiness)){b.from=from;b.ownerAdvances.from=from;b.settlement.from=from;}
const b=businessPerson(response.ownershipReceipts.assistant,D,from,{recordCount:2,orderCount:2,costByPayer:{owner:{totalCost:78000,knownTotalCost:78000}},ownerAdvances:advances([advanceDay(prior,[advanceItem('prior','昨日已付成本',10000)]),advanceDay(D,[advanceItem('today','今日已付成本',68000)])],from,D),settlement:settlement([counterpart('owner',500000,78000)],D,from)});
b.currentAccount=currentAccount([{date:prior,known:{profit:1000},profit:1000,incomplete:false},{date:D,known:{profit:71000},profit:71000,incomplete:false}],response.ownershipReceipts.assistant,b.ownerAdvances,from,D);response.ownershipBusiness.assistant=b;
const h=businessHarness(response);h.range(D,true);await h.run('renderReport()');const html=memberCard(h.content.innerHTML,'assistant'),section=html.slice(html.indexOf('report-business-current'));
assert.match(section,/按所选范围逐日核对/);assert.match(section,/<h4>2026-10-21<\/h4><span>助理1应转麦麦<\/span><strong>¥110\.00/);assert.match(section,/<h4>2026-10-22<\/h4><span>助理1应转麦麦<\/span><strong>¥1,190\.00/);assert.doesNotMatch(section,/<strong>¥1,300\.00|累计往来|逐人核对待转/);assert.equal((section.match(/class="report-current-day"/g)||[]).length,2);
const text=h.run('reportOwnershipReceiptsText(currentReport).join(String.fromCharCode(10))');assert.match(text,/2026-10-21 \/ 助理1应转麦麦 ¥110\.00/);assert.match(text,/2026-10-22 \/ 助理1应转麦麦 ¥1,190\.00/);
});
test('当日往来缺失、日期越界、成本分类或等式不平时不输出伪造0或允许下载',async()=>{
const changes=[b=>delete b.currentAccount,b=>b.currentAccount.days[0].date='2026-10-21',b=>b.currentAccount.days[0].amount++,b=>b.currentAccount.days[0].knownMaterialCost++,b=>b.currentAccount.days[0].ownerNetReceipts++,b=>b.currentAccount.days=[]];
for(const change of changes){const response=poolBusinessReport(),h=businessHarness(response);change(response.ownershipBusiness.assistant);h.run('api=async function(){return __response;};');await h.run('renderReport()');assert.equal(h.run('currentReport'),null);assert.match(h.content.innerHTML,/所属订单成本、利润或待转资料未完整返回/);assert.doesNotMatch(h.content.innerHTML,/report-business-current/);assert.throws(()=>h.run('requireCurrentReport()'),/正在读取或已失效/);}
});