74 lines
7.0 KiB
JavaScript
74 lines
7.0 KiB
JavaScript
'use strict';
|
||
// 每个账本独立保存店铺配置;固定成员、登录账号和历史财务不随配置改变。
|
||
const Printing=require('./printing');
|
||
const TeamMembers=require('./team-members');
|
||
const KEY='shop-settings-v1';
|
||
const FEE_DEFAULTS=Object.freeze({certificate:'4',packaging:'3',shipping:'6',packagingPayer:'owner'});
|
||
const own=(value,key)=>Object.hasOwn(value,key);
|
||
function fail(message,status=400){const error=new Error(message);error.status=status;throw error;}
|
||
function object(value,name){if(!value||typeof value!=='object'||Array.isArray(value))fail(name+'格式无效');return value;}
|
||
function only(value,keys,name){for(const key of Object.keys(value))if(!keys.includes(key))fail(name+'包含不支持的字段:'+key);}
|
||
function string(value,name,max,{empty=false}={}){
|
||
if(typeof value!=='string')fail(name+'须填写文字');const result=value.trim().normalize('NFC');
|
||
if((!empty&&!result)||result.length>max||/[\u0000-\u001f\u007f-\u009f]/.test(result))fail(name+'请填写'+(empty?'不超过':'1~')+max+'字');
|
||
return result;
|
||
}
|
||
const monthNow=()=>new Intl.DateTimeFormat('sv-SE',{timeZone:'Asia/Shanghai',year:'numeric',month:'2-digit'}).format(new Date()).slice(0,7);
|
||
function nextMonth(month){const [year,number]=month.split('-').map(Number);return new Date(Date.UTC(year,number,1)).toISOString().slice(0,7);}
|
||
function seed(ledger){
|
||
const printing=Printing.settings(ledger.meta('printing',Printing.defaults));
|
||
return {version:1,revision:0,storeName:printing.store,receiptSubtitle:printing.subtitle,contact:printing.contact,
|
||
creditCardRatePermille:6,profitSharePercent:30,profitShareEffectiveMonth:'2000-01',profitShareHistory:[{month:'2000-01',percent:30}],feeDefaults:{...FEE_DEFAULTS}};
|
||
}
|
||
function read(ledger){
|
||
const raw=ledger.meta(KEY);if(!raw)return seed(ledger);
|
||
return {version:1,revision:raw.revision,storeName:raw.storeName,receiptSubtitle:raw.receiptSubtitle,contact:raw.contact,
|
||
creditCardRatePermille:raw.creditCardRatePermille,profitSharePercent:raw.profitSharePercent,profitShareEffectiveMonth:raw.profitShareEffectiveMonth??'2000-01',
|
||
profitShareHistory:(raw.profitShareHistory??[{month:'2000-01',percent:30}]).map(row=>({...row})),feeDefaults:{...FEE_DEFAULTS,...raw.feeDefaults}};
|
||
}
|
||
function publicSettings(ledger,actor){
|
||
const config=read(ledger);if(actor?.role==='owner')return config;
|
||
const {profitSharePercent,profitShareEffectiveMonth,profitShareHistory,...visible}=config;return visible;
|
||
}
|
||
function integer(value,name,min,max){if(typeof value!=='number'||!Number.isSafeInteger(value)||value<min||value>max)fail(name+'须为'+min+'~'+max+'的整数');return value;}
|
||
function amount(value,name){
|
||
if(typeof value!=='string'||value.length>12||!/^\d+(?:\.\d{1,2})?$/.test(value)||Number(value)>1000000)fail(name+'须为0~1000000元,最多两位小数');return value;
|
||
}
|
||
function atomic(ledger,fn){ledger.db.exec('SAVEPOINT shop_settings_write');try{const result=fn();ledger.db.exec('RELEASE shop_settings_write');return result;}catch(error){ledger.db.exec('ROLLBACK TO shop_settings_write');ledger.db.exec('RELEASE shop_settings_write');throw error;}}
|
||
function update(ledger,input,actor=ledger.actor){return atomic(ledger,()=>{
|
||
if(actor?.role!=='owner')fail('店铺配置仅麦麦账号可修改',403);object(input,'店铺配置');
|
||
only(input,['version','revision','storeName','receiptSubtitle','contact','creditCardRatePermille','profitSharePercent','profitShareEffectiveMonth','feeDefaults'],'店铺配置');
|
||
const before=read(ledger);if(typeof input.revision!=='number'||input.revision!==before.revision)fail('店铺配置已有变动,请刷新后重新核对',409);
|
||
if(own(input,'version')&&input.version!==1)fail('店铺配置版本无效');
|
||
const next={...before,feeDefaults:{...before.feeDefaults},profitShareHistory:before.profitShareHistory.map(row=>({...row}))};
|
||
for(const [field,name,max,empty] of [['storeName','店铺名称',80,false],['receiptSubtitle','单据副标题',80,true],['contact','店铺联系方式',80,true]])if(own(input,field))next[field]=string(input[field],name,max,{empty});
|
||
if(own(input,'creditCardRatePermille'))next.creditCardRatePermille=integer(input.creditCardRatePermille,'信用卡手续费率',0,1000);
|
||
if(own(input,'feeDefaults')){
|
||
object(input.feeDefaults,'费用默认值');only(input.feeDefaults,Object.keys(FEE_DEFAULTS),'费用默认值');
|
||
for(const [key,label] of [['certificate','证书费'],['packaging','包装费'],['shipping','快递费']])if(own(input.feeDefaults,key))next.feeDefaults[key]=amount(input.feeDefaults[key],label);
|
||
if(own(input.feeDefaults,'packagingPayer')){if(!TeamMembers.KEYS.includes(input.feeDefaults.packagingPayer))fail('包装费默认付款人无效');next.feeDefaults.packagingPayer=input.feeDefaults.packagingPayer;}
|
||
}
|
||
if(own(input,'profitSharePercent')||own(input,'profitShareEffectiveMonth')){
|
||
if(!own(input,'profitSharePercent'))fail('请同时填写利润分成比例');
|
||
const percent=integer(input.profitSharePercent,'利润分成比例',0,100),month=own(input,'profitShareEffectiveMonth')?input.profitShareEffectiveMonth:nextMonth(monthNow());
|
||
if(typeof month!=='string'||!/^\d{4}-(0[1-9]|1[0-2])$/.test(month)||month<monthNow()||month>'9998-12')fail('利润分成生效月份须为本月或未来月份');
|
||
next.profitSharePercent=percent;next.profitShareEffectiveMonth=month;
|
||
next.profitShareHistory=[...next.profitShareHistory.filter(row=>row.month!==month),{month,percent}].sort((a,b)=>a.month.localeCompare(b.month));
|
||
}
|
||
next.revision=before.revision+1;ledger.setMeta(KEY,next);
|
||
// 店铺配置是品牌真源,同时兼容旧打印元数据读取;纸张定位参数原样保留。
|
||
ledger.setMeta('printing',Printing.settings({...ledger.meta('printing',Printing.defaults),store:next.storeName,subtitle:next.receiptSubtitle,contact:next.contact}));
|
||
ledger.audit('shop-settings','更新店铺配置',actor,{before,after:next});
|
||
return {ok:true,shopSettings:read(ledger),printing:printingSettings(ledger)};
|
||
});}
|
||
function printingSettings(ledger){const config=read(ledger);return Printing.settings({...ledger.meta('printing',Printing.defaults),store:config.storeName,subtitle:config.receiptSubtitle,contact:config.contact});}
|
||
function updatePrinting(ledger,input,actor=ledger.actor){return atomic(ledger,()=>{
|
||
if(actor?.role!=='owner')fail('打印设置仅麦麦账号可修改',403);object(input,'打印设置');
|
||
only(input,[...Object.keys(Printing.defaults),'shopSettingsRevision'],'打印设置');
|
||
const {shopSettingsRevision,...printInput}=input,current=printingSettings(ledger),formatted=Printing.settings({...current,...printInput}),config=read(ledger),patch={revision:shopSettingsRevision??config.revision};
|
||
for(const [from,to] of [['store','storeName'],['subtitle','receiptSubtitle'],['contact','contact']])if(own(input,from))patch[to]=input[from];
|
||
update(ledger,patch,actor);ledger.setMeta('printing',formatted);ledger.audit('printing','更新打印参数',actor,formatted);
|
||
return {ok:true,shopSettings:read(ledger),printing:printingSettings(ledger)};
|
||
});}
|
||
module.exports={KEY,FEE_DEFAULTS,read,publicSettings,update,printingSettings,updatePrinting};
|