每日对账逐日展示麦麦垫付明细,区分历史累计往来

This commit is contained in:
summer committed 2026-11-02 21:34:17 +08:00
1 parent a72f4a276e
commit d8daf90b00
6 files changed
+141 -18

No files matched your search

+41 -5
View File
@@ -10,7 +10,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,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??{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}}};
}
function syncBusiness(response){response.ownershipBusiness=Object.fromEntries(Object.entries(response.ownershipReceipts).map(([member,person])=>[member,businessPerson(person,response.date,response.from)]));return response;}
function report(date=D,from=date,empty=false){
@@ -179,7 +179,7 @@ test('老板全部保留售后成本only、0元真实售后和纯待转卡,没
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';for(const b of Object.values(response.ownershipBusiness)){b.from=response.from;b.settlement.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';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>/);
});
test('成本待补只显示已知付款和待核算,已核算利润不能冒充完整利润或结清',async()=>{
@@ -196,17 +196,24 @@ test('经营投影缺失、付款不平、利润等式或待转构成错误均
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|已结清/);
});
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};}
function advanceDay(date,items){
const recorded=items.filter(item=>item.status==='recorded'),missing=items.filter(item=>item.status==='missing'),sum=rows=>rows.reduce((total,item)=>total+item.amount,0),knownAmount=sum(recorded),knownPoolAmount=sum(recorded.filter(item=>item.poolMaterial)),knownOtherAmount=knownAmount-knownPoolAmount;
return {date,items:items.map(item=>({...item,date})),knownAmount,amount:missing.length?null:knownAmount,missingCount:missing.length,pendingCount:items.filter(item=>item.status==='pending').length,poolAmount:missing.some(item=>item.poolMaterial)?null:knownPoolAmount,knownPoolAmount,otherAmount:missing.some(item=>!item.poolMaterial)?null:knownOtherAmount,knownOtherAmount};
}
function advances(days,from=D,date=D){return {basis:'orderDate',from,date,days};}
function poolBusinessReport(){
const response=businessReport(),byPayer={owner:{amount:103333,knownAmount:103333},assistant:{amount:90000,knownAmount:90000},assistant2:{amount:0,knownAmount:0}},day={poolId:D,date:D,mg:2000,averageRate:96667,amount:193333,knownAmount:193333,missingCount:0,byPayer};
response.ownershipBusiness.assistant=businessPerson(response.ownershipReceipts.assistant,D,D,{costByPayer:{owner:{totalCost:106933,knownTotalCost:106933},assistant:{totalCost:90300,knownTotalCost:90300}},poolMaterial:{mg:2000,amount:193333,knownAmount:193333,missingCount:0,hasPriceLocks:false,byPayer,days:[day],otherCosts:{amount:3900,knownAmount:3900}},settlement:settlement([counterpart('owner',50000,86933)])});
response.ownershipBusiness.assistant.ownerAdvances=advances([advanceDay(D,[advanceItem('pool','料池料费 · 麦麦垫付部分',103333,{component:'material',poolId:D,poolMaterial:true}),advanceItem('fee','实际工费与运费',3600)])]);
return response;
}
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,/麦麦垫付合计:料费 ¥1,033\.33 + 其他成本 ¥36\.00 = ¥1,069\.33/);assert.doesNotMatch(html,/麦麦垫付合计:料费 ¥1,033\.33 + 其他成本 ¥39\.00/);
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/);
assert.match(text,/麦麦垫付合计:料池料费 ¥1,033\.33 + 其他成本 ¥36\.00 = ¥1,069\.33/);
});
test('多日料池各列原均价与耗用额,跨日锁价说明不把交付料费当作本日新往来',async()=>{
@@ -219,11 +226,40 @@ test('待补料费与未知FIFO付款人显示已知额和待补,其他已核
const response=poolBusinessReport(),pool=response.ownershipBusiness.assistant.poolMaterial;
pool.amount=null;pool.knownAmount=0;pool.missingCount=1;pool.byPayer=Object.fromEntries(['owner','assistant','assistant2'].map(key=>[key,{amount:null,knownAmount:0}]));pool.days=[{poolId:D,date:D,mg:2000,averageRate:null,amount:null,knownAmount:0,missingCount:1,byPayer:pool.byPayer}];
response.ownershipBusiness.assistant=businessPerson(response.ownershipReceipts.assistant,D,D,{costByPayer:{owner:{totalCost:null,knownTotalCost:3600,missingCount:1},assistant:{totalCost:300,knownTotalCost:300}},poolMaterial:pool,settlement:settlement([],D,D,{partial:true,pendingCount:1})});
response.ownershipBusiness.assistant.ownerAdvances=advances([advanceDay(D,[advanceItem('missing-pool','料池料费 · 麦麦份额待补',null,{component:'material',poolId:D,poolMaterial:true,allocationPending:true}),advanceItem('paid-fee','实际工费与运费',3600)])]);
const h=businessHarness(response);await h.run('renderReport()');const html=memberCard(h.content.innerHTML,'assistant');assert.match(html,/2\.000g × 待补料价 = 料费待补(已知 ¥0\.00)/);assert.match(html,/其中麦麦垫付 <b>已知 ¥0\.00/);assert.match(html,/其他成本 <b>¥39\.00/);assert.match(html,/待核算/);assert.doesNotMatch(html,/2\.000g × ¥0\.00/);
assert.match(html,/麦麦垫付合计:料费 待补(已知 ¥0\.00) + 其他成本 待补(已知 ¥36\.00) = 待补(已知 ¥36\.00)/);
assert.match(html,/麦麦垫付合计:料池料费 待补(已知 ¥0\.00) + 其他成本 ¥36\.00 = 待补(已知 ¥36\.00)/);
});
test('料费分项、克重、付款归属和其他成本必须守恒,缺失投影不会把料费隐藏成0',async()=>{
const changes=[r=>delete r.ownershipBusiness.assistant.poolMaterial,r=>r.ownershipBusiness.assistant.poolMaterial.mg++,r=>r.ownershipBusiness.assistant.poolMaterial.days[0].amount++,r=>r.ownershipBusiness.assistant.poolMaterial.byPayer.owner.knownAmount++,r=>r.ownershipBusiness.assistant.poolMaterial.otherCosts.knownAmount++];
for(const change of changes){const response=poolBusinessReport();change(response);const h=businessHarness(response);await h.run('renderReport()');assert.equal(h.run('currentReport'),null);assert.match(h.content.innerHTML,/所属订单成本、利润或待转资料未完整返回/);assert.doesNotMatch(h.content.innerHTML,/料池已用料费<\/span>/);}
});
test('麦麦逐笔垫付默认展开且客户可打开原单,多人池料子行只显示分摊金额,所有文字转义',async()=>{
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,/<section class="report-owner-advances"[^>]*><h3>当日麦麦垫付明细<\/h3>/);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/);
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)/);
});
test('月份垫付明细只列实际记录日期及各日小计,不把月合计或历史余额当每天垫付',async()=>{
const response=poolBusinessReport(),from='2026-10-01',prior='2026-10-21',old=response.ownershipBusiness.assistant;
response.from=from;for(const b of Object.values(response.ownershipBusiness)){b.from=from;b.ownerAdvances.from=from;b.settlement.from=from;}
response.ownershipBusiness.assistant=businessPerson(response.ownershipReceipts.assistant,D,from,{costByPayer:{owner:{totalCost:116933,knownTotalCost:116933},assistant:{totalCost:90300,knownTotalCost:90300}},poolMaterial:{...old.poolMaterial,otherCosts:{amount:13900,knownAmount:13900}},ownerAdvances:advances([advanceDay(prior,[advanceItem('yesterday','昨日实际配件',10000)]),...old.ownerAdvances.days],from,D),settlement:settlement([counterpart('owner',50000,96933)],D,from)});
const h=businessHarness(response);h.range(D,true);await h.run('renderReport()');const card=memberCard(h.content.innerHTML,'assistant');assert.match(card,/<h3>按日核对麦麦垫付明细<\/h3>/);assert.match(card,/<h4>2026-10-21<\/h4><span>垫付小计 <strong>¥100\.00/);assert.match(card,/<h4>2026-10-22<\/h4><span>垫付小计 <strong>¥1,069\.33/);assert.doesNotMatch(card,/垫付小计 <strong>¥1,169\.33|<h4>2026-10-01/);assert.equal((card.match(/class="report-owner-advance-day"/g)||[]).length,2);
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()=>{
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,/预估金额不计垫付小计/);
});
test('垫付明细缺失、日期越界、小计不平或付款项目错误时阻止回执和下载',async()=>{
const changes=[r=>delete r.ownershipBusiness.assistant.ownerAdvances,r=>r.ownershipBusiness.assistant.ownerAdvances.days[0].date='2026-10-21',r=>r.ownershipBusiness.assistant.ownerAdvances.days[0].amount++,r=>r.ownershipBusiness.assistant.ownerAdvances.days[0].items[0].amount++,r=>r.ownershipBusiness.assistant.ownerAdvances.days[0].items[0].status='pending'];
for(const change of changes){const response=poolBusinessReport();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()'),/正在读取或已失效/);}
});