2026-10-17 19:50:24 +08:00
|
|
|
|
'use strict';
|
|
|
|
|
|
// 只生成可修改的预估值,不写支付流水。金额输入与表单一致,使用元的字符串。
|
|
|
|
|
|
(function(root){
|
|
|
|
|
|
const defaults={'证书':'4','包装':'3','快递':'6'};
|
|
|
|
|
|
const normalize=name=>({'包装盒':'包装','快递费':'快递','运费':'快递'}[name]||name);
|
2026-11-19 02:23:28 +08:00
|
|
|
|
// 勾选成本与客户收费分开。保留旧项目,只有明确勾选的新项目使用这些固定金额。
|
|
|
|
|
|
function checkboxCategory(name){
|
|
|
|
|
|
const value=String(name||'').trim();
|
|
|
|
|
|
if(['快递','快递费','运费','京东快递','圆通快递'].includes(value))return 'shipping';
|
|
|
|
|
|
if(['包装','包装盒','盒子'].includes(value)||/^盒子(\d+个 × 3元\/个)$/.test(value))return 'box';
|
|
|
|
|
|
if(['钢绳','钢绳(店里)','钢绳(店里)'].includes(value))return 'braiding';
|
2026-11-19 13:41:58 +08:00
|
|
|
|
if(['银链','银链(家里)','银链(家里)'].includes(value))return 'silverChain';
|
2026-11-19 02:23:28 +08:00
|
|
|
|
return '';
|
|
|
|
|
|
}
|
|
|
|
|
|
function boxCount(value){
|
|
|
|
|
|
const text=String(value??'').trim(),count=Number(text);
|
|
|
|
|
|
if(!/^\d+$/.test(text)||!Number.isSafeInteger(count)||count<1||count>333333333)throw new Error('盒子数量须为支持范围内的正整数');
|
|
|
|
|
|
return count;
|
|
|
|
|
|
}
|
|
|
|
|
|
function presetKind(item){
|
|
|
|
|
|
if(!item||item.expense_id||item.funding)return '';
|
|
|
|
|
|
if(item.key==='shipping'&&item.note==='京东快递'&&Number(item.amount)===8&&item.payer==='owner')return 'jd';
|
|
|
|
|
|
if(item.key==='shipping'&&item.note==='圆通快递'&&Number(item.amount)===6&&['owner','assistant','assistant2'].includes(item.payer))return 'yt';
|
|
|
|
|
|
if(item.key==='braiding'&&item.note==='钢绳(店里)'&&Number(item.amount)===7&&item.payer==='owner')return 'steel';
|
2026-11-19 13:41:58 +08:00
|
|
|
|
if(item.key==='other'&&item.note==='银链(家里)'&&Number(item.amount)===18&&item.payer==='owner')return 'silver';
|
2026-11-19 02:23:28 +08:00
|
|
|
|
const match=item.key==='box'&&String(item.note).match(/^盒子(([^)]*)个 × 3元\/个)$/);
|
|
|
|
|
|
if(match&&item.payer==='owner'){if(item.amount==='')return 'box';try{if(Number(item.amount)===boxCount(match[1])*3)return 'box';}catch{}}
|
|
|
|
|
|
return '';
|
|
|
|
|
|
}
|
2026-11-19 13:41:58 +08:00
|
|
|
|
function checkboxFees({shipping='',box=false,quantity='1',steel=false,silver=false}={},member='assistant'){
|
2026-11-19 02:23:28 +08:00
|
|
|
|
const items=[],add=(key,note,amount,payer)=>items.push({key,note,amount:String(amount),payer,status:'estimated',edited:true});
|
|
|
|
|
|
if(shipping==='jd')add('shipping','京东快递',8,'owner');
|
|
|
|
|
|
else if(shipping==='yt')add('shipping','圆通快递',6,['owner','assistant','assistant2'].includes(member)?member:'assistant');
|
|
|
|
|
|
if(box){const count=boxCount(quantity);add('box',`盒子(${count}个 × 3元/个)`,count*3,'owner');}
|
|
|
|
|
|
if(steel)add('braiding','钢绳(店里)',7,'owner');
|
2026-11-19 13:41:58 +08:00
|
|
|
|
// 沿用其他直接费用的兼容键,由名称区分家里银链。
|
|
|
|
|
|
if(silver)add('other','银链(家里)',18,'owner');
|
2026-11-19 02:23:28 +08:00
|
|
|
|
return items;
|
|
|
|
|
|
}
|
|
|
|
|
|
function sync(existing,lines,{addShipping=false,defaultPayer='assistant',shopDefaults={},checkboxCosts=false}={}){
|
2026-11-16 23:03:36 +08:00
|
|
|
|
const configured={'证书':shopDefaults.certificate??defaults['证书'],'包装':shopDefaults.packaging??defaults['包装'],'快递':shopDefaults.shipping??defaults['快递']};
|
|
|
|
|
|
const packagingPayer=['owner','assistant','assistant2'].includes(shopDefaults.packagingPayer)?shopDefaults.packagingPayer:'owner';
|
2026-10-17 19:50:24 +08:00
|
|
|
|
const remaining=existing.map(r=>({...r})),linked=[];
|
|
|
|
|
|
for(const [index,line] of lines.entries())for(const charge of line.charges||[]){
|
|
|
|
|
|
const name=String(charge.name||'').trim();if(!charge.id||!name)continue;
|
2026-11-20 00:43:55 +08:00
|
|
|
|
if(charge.costRemoved===true)continue;
|
2026-10-17 19:50:24 +08:00
|
|
|
|
const normalized=normalize(name);
|
2026-11-19 02:23:28 +08:00
|
|
|
|
if(checkboxCosts&&checkboxCategory(name))continue;
|
2026-11-16 23:03:36 +08:00
|
|
|
|
// 快递默认按整单一笔,不能因多件商品重复生成运费。
|
2026-10-17 19:50:24 +08:00
|
|
|
|
if(normalized==='快递'){
|
2026-11-16 23:03:36 +08:00
|
|
|
|
if(!remaining.some(r=>r.key==='shipping'))remaining.push({key:'shipping',note:'快递费',amount:configured['快递'],payer:defaultPayer,status:'estimated',edited:false});
|
2026-10-17 19:50:24 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
const key='charge:'+charge.id;
|
|
|
|
|
|
let found=remaining.findIndex(r=>r.key===key);
|
|
|
|
|
|
if(found<0)found=remaining.findIndex(r=>!r.key.startsWith('charge:')&&r.key!=='shipping'&&normalize(r.note)===normalized);
|
|
|
|
|
|
const previous=found<0?null:remaining.splice(found,1)[0];
|
2026-11-16 23:03:36 +08:00
|
|
|
|
const amount=previous&&(previous.edited||previous.sourceName===name||!previous.sourceName)?previous.amount:(configured[normalized]??'');
|
2026-10-17 19:50:24 +08:00
|
|
|
|
// 新项目及未手改的项目切换采用默认付款人;同名旧成本和手改选择保留。
|
2026-10-25 14:19:24 +08:00
|
|
|
|
const useDefaultPayer=!previous||(!previous.edited&&previous.sourceName&&normalize(previous.sourceName)!==normalized);
|
2026-11-16 23:03:36 +08:00
|
|
|
|
const payer=useDefaultPayer?(normalized==='包装'?packagingPayer:defaultPayer):(previous.payer||defaultPayer);
|
2026-10-17 19:50:24 +08:00
|
|
|
|
linked.push({...previous,key,note:name,sourceName:name,lineNumber:index+1,amount,payer,status:previous?.status||'estimated',edited:previous?.edited||false});
|
|
|
|
|
|
}
|
2026-11-19 02:23:28 +08:00
|
|
|
|
const managedKeys=new Set(lines.flatMap(line=>(line.charges||[]).filter(charge=>checkboxCategory(charge.name)).map(charge=>'charge:'+charge.id)));
|
|
|
|
|
|
const manual=remaining.filter(r=>!r.key.startsWith('charge:')||checkboxCosts&&(checkboxCategory(r.sourceName||r.note)||managedKeys.has(r.key)));
|
2026-11-20 00:43:55 +08:00
|
|
|
|
const removedShipping=lines.some(line=>(line.charges||[]).some(charge=>charge.costRemoved===true&&normalize(String(charge.name||'').trim())==='快递'));
|
|
|
|
|
|
if(addShipping&&!checkboxCosts&&!removedShipping&&!manual.some(r=>r.key==='shipping'))manual.unshift({key:'shipping',note:'快递费',amount:configured['快递'],payer:defaultPayer,status:'estimated',edited:false});
|
2026-10-17 19:50:24 +08:00
|
|
|
|
return [...manual,...linked];
|
|
|
|
|
|
}
|
2026-11-19 02:23:28 +08:00
|
|
|
|
const api={sync,defaults,normalize,checkboxCategory,boxCount,presetKind,checkboxFees};if(typeof module==='object'&&module.exports)module.exports=api;else root.FeeEstimates=api;
|
2026-10-17 19:50:24 +08:00
|
|
|
|
})(typeof globalThis==='object'?globalThis:this);
|