2026-10-17 19:50:24 +08:00
|
|
|
'use strict';
|
|
|
|
|
// 只生成可修改的预估值,不写支付流水。金额输入与表单一致,使用元的字符串。
|
|
|
|
|
(function(root){
|
|
|
|
|
const defaults={'证书':'4','包装':'3','快递':'6'};
|
|
|
|
|
const normalize=name=>({'包装盒':'包装','快递费':'快递','运费':'快递'}[name]||name);
|
2026-11-16 23:03:36 +08:00
|
|
|
function sync(existing,lines,{addShipping=false,defaultPayer='assistant',shopDefaults={}}={}){
|
|
|
|
|
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;
|
|
|
|
|
const normalized=normalize(name);
|
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});
|
|
|
|
|
}
|
|
|
|
|
const manual=remaining.filter(r=>!r.key.startsWith('charge:'));
|
2026-11-16 23:03:36 +08:00
|
|
|
if(addShipping&&!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];
|
|
|
|
|
}
|
|
|
|
|
const api={sync,defaults,normalize};if(typeof module==='object'&&module.exports)module.exports=api;else root.FeeEstimates=api;
|
|
|
|
|
})(typeof globalThis==='object'?globalThis:this);
|