`;
+ }).join('');
+ return ``;
+}
+function syncConfiguredCosts(root,target){
+ const config=FeeEstimates.presetCatalog(orderCostConfig()),kind=target?.dataset?.configuredCost||target?.dataset?.configuredQuantity,category=configuredCostCategory(kind);
+ const originals=JSON.parse(root.dataset.costOriginals||'{}'),changed=JSON.parse(root.dataset.costChanged||'{}');
+ if(kind&&config[kind]){
+ changed[category]=true;const checked=root.querySelector(`[name="cost_${kind}"]`)?.checked;
+ if(checked&&category==='shipping')for(const other of ['jd','yt'])if(other!==kind){const checkbox=root.querySelector(`[name="cost_${other}"]`);if(checkbox)checkbox.checked=false;}
+ if(checked)for(const {row,item} of orderCostLegacyRows()){
+ if(configuredCostItemCategory(item)===category){if(item.funding)continue;if(!originals[category])originals[category]=item;row.remove();}
+ else if(item.key===FeeEstimates.presetKey(kind))row.dataset.fee='manual:'+crypto.randomUUID();
+ }
+ }
+ root.dataset.costOriginals=JSON.stringify(originals);root.dataset.costChanged=JSON.stringify(changed);
+ for(const input of root.querySelectorAll('[data-configured-quantity]')){const checkbox=root.querySelector(`[name="cost_${input.dataset.configuredQuantity}"]`);input.disabled=!checkbox?.checked||checkbox.disabled;input.required=!input.disabled;}
+}
+function readConfiguredCosts(root){
+ const config=FeeEstimates.presetCatalog(orderCostConfig()),originals=JSON.parse(root.dataset.costOriginals||'{}'),changed=JSON.parse(root.dataset.costChanged||'{}'),editing=root.dataset.costEditing==='true',member=orderFormMember(),items=[];
+ for(const [kind,row] of Object.entries(config)){
+ const checkbox=root.querySelector(`[name="cost_${kind}"]`),total=root.querySelector(`[data-configured-total="${kind}"]`),category=configuredCostCategory(kind),source=originals[category];
+ if(!row.enabled||!checkbox?.checked||checkbox.disabled&&!(editing&&source&&!changed[category])){if(total)total.textContent='勾选后计费';continue;}
+ const quantity=root.querySelector(`[name="cost_${kind}_quantity"]`)?.value??'1';let item;
+ try{item=FeeEstimates.presetFee(kind,quantity,member,config);if(total)total.textContent=`${quantity}个 × ${row.amount}元 = ${item.amount}元`;}
+ catch(error){item={key:FeeEstimates.presetKey(kind),note:row.name,amount:'',payer:row.payer==='order'?member:row.payer,status:'estimated',edited:true};if(total)total.textContent=error.message;}
+ items.push(editing&&source?(changed[category]?{...source,...item}:{...source}):item);
+ }
+ return items;
+}
diff --git a/新账系统/public/shop-settings-ui.js b/新账系统/public/shop-settings-ui.js
index a0a54d5..e692828 100644
--- a/新账系统/public/shop-settings-ui.js
+++ b/新账系统/public/shop-settings-ui.js
@@ -12,7 +12,7 @@ function shopNextMonth(){
}
function shopSettingsValues(settings=state.shopSettings){
const values={storeName:settings.storeName,receiptSubtitle:settings.receiptSubtitle||'',contact:settings.contact||'',creditCardRatePermille:String(settings.creditCardRatePermille),profitSharePercent:String(settings.profitSharePercent),profitShareEffectiveMonth:settings.profitShareEffectiveMonth>=state.today.slice(0,7)?settings.profitShareEffectiveMonth:shopNextMonth(),certificate:settings.feeDefaults.certificate,packaging:settings.feeDefaults.packaging,shipping:settings.feeDefaults.shipping,packagingPayer:settings.feeDefaults.packagingPayer};
- for(const [kind,row] of Object.entries(FeeEstimates.presets(settings.costPresets))){values['preset_'+kind+'_amount']=row.amount;values['preset_'+kind+'_payer']=row.payer;}
+ for(const [kind,row] of Object.entries(FeeEstimates.presetCatalog(settings.costPresets))){for(const key of ['amount','payer','name','enabled','byQuantity'])values['preset_'+kind+'_'+key]=String(row[key]);}
return values;
}
function shopSettingsFormValues(form){return Object.fromEntries(Object.keys(JSON.parse(form.dataset.baseline)).map(key=>[key,form.elements.namedItem(key)?.value??'']));}
@@ -29,13 +29,19 @@ function syncShopSettingsStatus(saved=false){
function shopSettingsHTML(){
if(state.user.role!=='owner'||!state.shopSettings)return '';
const settings=state.shopSettings,values=shopSettingsValues(settings);
- return `
店铺配置
当前生效配置;仅店主可以修改。
`;
+ return `
店铺配置
当前生效配置;仅店主可以修改。
`;
}
function shopSettingsInput(form){
if(state.user.role!=='owner')throw new Error('店铺配置由店主账号修改。');
const values=shopSettingsFormValues(form),integer=(name,max,title)=>{if(!/^\d+$/.test(values[name])||Number(values[name])>max)throw new Error(title+'请填写0~'+max+'之间的整数。');return Number(values[name]);};
if(!values.storeName.trim()||values.storeName.trim().length>80)throw new Error('店名请填写1~80字。');
- const costPresets={};for(const kind of Object.keys(FeeEstimates.presetDefaults)){const amount=values['preset_'+kind+'_amount'],payer=values['preset_'+kind+'_payer'];if(LedgerCalc.cents(amount,'常用成本')>100000000)throw new Error('常用成本最多1000000元');if(payer!=='order'&&!activeTeamMemberKeys().includes(payer))throw new Error('请选择有效付款人');costPresets[kind]={amount,payer};}
+ const costPresets={};for(const kind of Object.keys(values).filter(key=>/^preset_.+_amount$/.test(key)).map(key=>key.slice(7,-7))){
+ const amount=values['preset_'+kind+'_amount'],payer=values['preset_'+kind+'_payer'],name=values['preset_'+kind+'_name'].trim();
+ if(!name||name.length>40)throw new Error('成本名称请填写1~40字');
+ if(LedgerCalc.cents(amount,'常用成本')>100000000)throw new Error('常用成本最多1000000元');if(payer!=='order'&&!activeTeamMemberKeys().includes(payer))throw new Error('请选择有效付款人');
+ costPresets[kind]={amount,payer,name,enabled:values['preset_'+kind+'_enabled']==='true',byQuantity:values['preset_'+kind+'_byQuantity']==='true'};
+ }
+
for(const name of ['certificate','packaging','shipping']){if(LedgerCalc.cents(values[name],'默认费用')>100000000)throw new Error('默认费用最多1000000元。');}
if(!teamMemberKeys().includes(values.packagingPayer))throw new Error('请选择包装默认付款人。');
if(!/^\d{4}-(0[1-9]|1[0-2])$/.test(values.profitShareEffectiveMonth)||values.profitShareEffectiveMonth{
if(saving||dirty&&!window.confirm('店铺或打印设置尚未保存,离开会丢弃这些修改。继续离开?')){event.preventDefault();event.stopImmediatePropagation();if(saving)toast('设置正在保存,请稍候。',true);}
},true);
window.addEventListener('beforeunload',event=>{if(shopSettingsSaving||shopSettingsDirty()||printSettingsDirty()){event.preventDefault();event.returnValue='';}});
+
+function shopCostCard(kind,values){
+ const key=name=>'preset_'+kind+'_'+name;
+ return `