格式化:Prettier统一代码风格(仅格式,无逻辑变化)

仅为 npm run format 的输出;黄金对照、旧库兼容、浏览器流程与单元测试全部通过且零差异。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
黄健铭andClaude Opus 5.5 committed 2026-12-04 08:58:53 -08:00
1 parent 3743d687e7
commit 28c64814f0
221 files changed
+116943 -26708

No files matched your search

+269 -79
View File
@@ -1,94 +1,284 @@
'use strict';
// 只生成可修改的预估值,不写支付流水。金额输入与表单一致,使用元的字符串。
(function(root){
const Calc=typeof module==='object'&&module.exports?require('./calculations'):root.LedgerCalc;
const defaults={'证书':'4','包装':'3','快递':'6'};
const normalize=name=>({'包装盒':'包装','快递费':'快递','运费':'快递'}[name]||name);
(function (root) {
const Calc = typeof module === 'object' && module.exports ? require('./calculations') : root.LedgerCalc;
const defaults = { 证书: '4', 包装: '3', 快递: '6' };
const normalize = name => ({ 包装盒: '包装', 快递费: '快递', 运费: '快递' })[name] || name;
// 勾选成本与客户收费分开。保留旧项目,只有明确勾选的新项目使用这些固定金额。
function checkboxCategory(name){
const value=String(name||'').trim();
if(['快递','快递费','运费','京东快递','圆通快递'].includes(value))return 'shipping';
if(['包装','包装盒','盒子'].includes(value)||/^盒子(\d+个 × [\d.]+元\/个)$/.test(value))return 'box';
if(['钢绳','钢绳(店里)','钢绳(店里)'].includes(value))return 'braiding';
if(['银链','银链(家里)','银链(家里)'].includes(value))return 'silverChain';
function checkboxCategory(name) {
const value = String(name || '').trim();
if (['快递', '快递费', '运费', '京东快递', '圆通快递'].includes(value)) return 'shipping';
if (['包装', '包装盒', '盒子'].includes(value) || /^盒子(\d+个 × [\d.]+元\/个)$/.test(value)) return 'box';
if (['钢绳', '钢绳(店里)', '钢绳(店里)'].includes(value)) return 'braiding';
if (['银链', '银链(家里)', '银链(家里)'].includes(value)) return 'silverChain';
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('盒子数量须为支持范围内的正整数');
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;
}
const presetDefaults=Object.freeze({jd:{amount:'8',payer:'owner'},yt:{amount:'6',payer:'order'},box:{amount:'3',payer:'owner'},steel:{amount:'7',payer:'owner'},silver:{amount:'18',payer:'owner'}});
const validMember=value=>['owner','assistant','assistant2'].includes(value)||/^staff_[a-f0-9-]{36}$/.test(value);
const presetNames={jd:'京东快递',yt:'圆通快递',box:'盒子',steel:'钢绳(店里)',silver:'银链(家里)'};
function presets(values){return Object.fromEntries(Object.entries(values===undefined?presetDefaults:values).filter(([kind])=>Object.hasOwn(presetDefaults,kind)||/^custom_[a-f0-9-]{36}$/.test(kind)).map(([kind,row])=>[kind,{...presetDefaults[kind],...row}]));}
function presetCatalog(values){return Object.fromEntries(Object.entries(presets(values)).map(([kind,row])=>[kind,{name:presetNames[kind]||'',enabled:true,byQuantity:kind==='box',...row}]));}
function customPresets(values){return values!==undefined&&Object.keys(presetDefaults).some(kind=>!Object.hasOwn(values,kind))||Object.entries(values||{}).some(([kind,row])=>!Object.hasOwn(presetDefaults,kind)||['name','enabled','byQuantity'].some(key=>Object.hasOwn(row,key)));}
function presetKey(kind){return ({jd:'shipping',yt:'shipping',box:'box',steel:'braiding',silver:'other'})[kind]||'manual:'+kind.slice(7);}
function presetFee(kind,quantity,member,values){
const row=presetCatalog(values)[kind];if(!row||!row.enabled)throw new Error('成本项目已停用,请重新核对');
const count=row.byQuantity?boxCount(quantity):1,cents=Calc.cents(String(row.amount),'常用成本')*count;
if(!Number.isSafeInteger(cents)||cents>100000000000)throw new Error('常用成本金额超出支持范围');
return {key:presetKey(kind),note:row.byQuantity?`${row.name}(${count}个 × ${row.amount}元/个)`:row.name,amount:(cents/100).toFixed(2).replace(/\.00$/,''),payer:row.payer==='order'?member:row.payer,status:'estimated',edited:true};
const presetDefaults = Object.freeze({
jd: { amount: '8', payer: 'owner' },
yt: { amount: '6', payer: 'order' },
box: { amount: '3', payer: 'owner' },
steel: { amount: '7', payer: 'owner' },
silver: { amount: '18', payer: 'owner' },
});
const validMember = value =>
['owner', 'assistant', 'assistant2'].includes(value) || /^staff_[a-f0-9-]{36}$/.test(value);
const presetNames = { jd: '京东快递', yt: '圆通快递', box: '盒子', steel: '钢绳(店里)', silver: '银链(家里)' };
function presets(values) {
return Object.fromEntries(
Object.entries(values === undefined ? presetDefaults : values)
.filter(([kind]) => Object.hasOwn(presetDefaults, kind) || /^custom_[a-f0-9-]{36}$/.test(kind))
.map(([kind, row]) => [kind, { ...presetDefaults[kind], ...row }]),
);
}
function configuredKind(item,values){
for(const [kind,row] of Object.entries(presetCatalog(values))){
if(!row.enabled||item.key!==presetKey(kind))continue;
const quantity=row.byQuantity?String(item.note).match(/(([^)]*)个 × [\d.]+元\/个)$/)?.[1]:'1';
try{const expected=presetFee(kind,quantity,item.payer,values);if(expected.note===item.note&&expected.payer===item.payer&&(item.amount===''||Calc.cents(String(item.amount))===Calc.cents(expected.amount)))return kind;}catch{}
}return '';
function presetCatalog(values) {
return Object.fromEntries(
Object.entries(presets(values)).map(([kind, row]) => [
kind,
{ name: presetNames[kind] || '', enabled: true, byQuantity: kind === 'box', ...row },
]),
);
}
function presetKind(item,{recorded=false,presets:values}={}){
const config=presets(values),matchPayer=kind=>config[kind].payer==='order'?validMember(item?.payer):item?.payer===config[kind].payer;
if(!item||item.expense_id&&!recorded||item.funding)return '';
if(customPresets(values))return configuredKind(item,values);
if(item.key==='shipping'&&item.note==='京东快递'&&Number(item.amount)===Number(config.jd.amount)&&matchPayer('jd'))return 'jd';
if(item.key==='shipping'&&item.note==='圆通快递'&&Number(item.amount)===Number(config.yt.amount)&&matchPayer('yt'))return 'yt';
if(item.key==='braiding'&&item.note==='钢绳(店里)'&&Number(item.amount)===Number(config.steel.amount)&&matchPayer('steel'))return 'steel';
if(item.key==='other'&&item.note==='银链(家里)'&&Number(item.amount)===Number(config.silver.amount)&&matchPayer('silver'))return 'silver';
const match=item.key==='box'&&String(item.note).match(/^盒子(([^)]*)个 × ([\d.]+)元\/个)$/);
if(match&&Number(match[2])===Number(config.box.amount)&&matchPayer('box')){if(item.amount==='')return 'box';try{if(Calc.cents(String(item.amount),'盒子成本')===boxCount(match[1])*Calc.cents(String(config.box.amount),'盒子单价'))return 'box';}catch{}}
function customPresets(values) {
return (
(values !== undefined && Object.keys(presetDefaults).some(kind => !Object.hasOwn(values, kind))) ||
Object.entries(values || {}).some(
([kind, row]) =>
!Object.hasOwn(presetDefaults, kind) ||
['name', 'enabled', 'byQuantity'].some(key => Object.hasOwn(row, key)),
)
);
}
function presetKey(kind) {
return (
{ jd: 'shipping', yt: 'shipping', box: 'box', steel: 'braiding', silver: 'other' }[kind] ||
'manual:' + kind.slice(7)
);
}
function presetFee(kind, quantity, member, values) {
const row = presetCatalog(values)[kind];
if (!row || !row.enabled) throw new Error('成本项目已停用,请重新核对');
const count = row.byQuantity ? boxCount(quantity) : 1,
cents = Calc.cents(String(row.amount), '常用成本') * count;
if (!Number.isSafeInteger(cents) || cents > 100000000000) throw new Error('常用成本金额超出支持范围');
return {
key: presetKey(kind),
note: row.byQuantity ? `${row.name}(${count}个 × ${row.amount}元/个)` : row.name,
amount: (cents / 100).toFixed(2).replace(/\.00$/, ''),
payer: row.payer === 'order' ? member : row.payer,
status: 'estimated',
edited: true,
};
}
function configuredKind(item, values) {
for (const [kind, row] of Object.entries(presetCatalog(values))) {
if (!row.enabled || item.key !== presetKey(kind)) continue;
const quantity = row.byQuantity ? String(item.note).match(/(([^)]*)个 × [\d.]+元\/个)$/)?.[1] : '1';
try {
const expected = presetFee(kind, quantity, item.payer, values);
if (
expected.note === item.note &&
expected.payer === item.payer &&
(item.amount === '' || Calc.cents(String(item.amount)) === Calc.cents(expected.amount))
)
return kind;
} catch {}
}
return '';
}
function checkboxFees({shipping='',box=false,quantity='1',steel=false,silver=false}={},member='assistant',values){
const config=presets(values),items=[],payer=kind=>config[kind].payer==='order'?(validMember(member)?member:'assistant'):config[kind].payer;
const add=(kind,key,note,count=1)=>{const amount=Calc.cents(String(config[kind].amount),'常用成本')*count;if(!Number.isSafeInteger(amount)||amount>100000000000)throw new Error('常用成本金额超出支持范围');items.push({key,note,amount:(amount/100).toFixed(2).replace(/\.00$/,''),payer:payer(kind),status:'estimated',edited:true});};
if(shipping==='jd')add('jd','shipping','京东快递');
else if(shipping==='yt')add('yt','shipping','圆通快递');
if(box){const count=boxCount(quantity);add('box','box',`盒子(${count}个 × ${config.box.amount}元/个)`,count);}
if(steel)add('steel','braiding','钢绳(店里)');
if(silver)add('silver','other','银链(家里)');
function presetKind(item, { recorded = false, presets: values } = {}) {
const config = presets(values),
matchPayer = kind =>
config[kind].payer === 'order' ? validMember(item?.payer) : item?.payer === config[kind].payer;
if (!item || (item.expense_id && !recorded) || item.funding) return '';
if (customPresets(values)) return configuredKind(item, values);
if (
item.key === 'shipping' &&
item.note === '京东快递' &&
Number(item.amount) === Number(config.jd.amount) &&
matchPayer('jd')
)
return 'jd';
if (
item.key === 'shipping' &&
item.note === '圆通快递' &&
Number(item.amount) === Number(config.yt.amount) &&
matchPayer('yt')
)
return 'yt';
if (
item.key === 'braiding' &&
item.note === '钢绳(店里)' &&
Number(item.amount) === Number(config.steel.amount) &&
matchPayer('steel')
)
return 'steel';
if (
item.key === 'other' &&
item.note === '银链(家里)' &&
Number(item.amount) === Number(config.silver.amount) &&
matchPayer('silver')
)
return 'silver';
const match = item.key === 'box' && String(item.note).match(/^盒子(([^)]*)个 × ([\d.]+)元\/个)$/);
if (match && Number(match[2]) === Number(config.box.amount) && matchPayer('box')) {
if (item.amount === '') return 'box';
try {
if (
Calc.cents(String(item.amount), '盒子成本') ===
boxCount(match[1]) * Calc.cents(String(config.box.amount), '盒子单价')
)
return 'box';
} catch {}
}
return '';
}
function checkboxFees(
{ shipping = '', box = false, quantity = '1', steel = false, silver = false } = {},
member = 'assistant',
values,
) {
const config = presets(values),
items = [],
payer = kind =>
config[kind].payer === 'order' ? (validMember(member) ? member : 'assistant') : config[kind].payer;
const add = (kind, key, note, count = 1) => {
const amount = Calc.cents(String(config[kind].amount), '常用成本') * count;
if (!Number.isSafeInteger(amount) || amount > 100000000000) throw new Error('常用成本金额超出支持范围');
items.push({
key,
note,
amount: (amount / 100).toFixed(2).replace(/\.00$/, ''),
payer: payer(kind),
status: 'estimated',
edited: true,
});
};
if (shipping === 'jd') add('jd', 'shipping', '京东快递');
else if (shipping === 'yt') add('yt', 'shipping', '圆通快递');
if (box) {
const count = boxCount(quantity);
add('box', 'box', `盒子(${count}个 × ${config.box.amount}元/个)`, count);
}
if (steel) add('steel', 'braiding', '钢绳(店里)');
if (silver) add('silver', 'other', '银链(家里)');
return items;
}
function sync(existing,lines,{addShipping=false,defaultPayer='assistant',shopDefaults={},checkboxCosts=false}={}){
const configured={'证书':shopDefaults.certificate??defaults['证书'],'包装':shopDefaults.packaging??defaults['包装'],'快递':shopDefaults.shipping??defaults['快递']};
const packagingPayer=(['owner','assistant','assistant2'].includes(shopDefaults.packagingPayer)||/^staff_[a-f0-9-]{36}$/.test(shopDefaults.packagingPayer))?shopDefaults.packagingPayer:'owner';
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;
if(charge.costRemoved===true)continue;
const normalized=normalize(name);
if(checkboxCosts&&checkboxCategory(name))continue;
// 快递默认按整单一笔,不能因多件商品重复生成运费。
if(normalized==='快递'){
if(!remaining.some(r=>r.key==='shipping'))remaining.push({key:'shipping',note:'快递费',amount:configured['快递'],payer:defaultPayer,status:'estimated',edited:false});
continue;
function sync(
existing,
lines,
{ addShipping = false, defaultPayer = 'assistant', shopDefaults = {}, checkboxCosts = false } = {},
) {
const configured = {
证书: shopDefaults.certificate ?? defaults['证书'],
包装: shopDefaults.packaging ?? defaults['包装'],
快递: shopDefaults.shipping ?? defaults['快递'],
};
const packagingPayer =
['owner', 'assistant', 'assistant2'].includes(shopDefaults.packagingPayer) ||
/^staff_[a-f0-9-]{36}$/.test(shopDefaults.packagingPayer)
? shopDefaults.packagingPayer
: 'owner';
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;
if (charge.costRemoved === true) continue;
const normalized = normalize(name);
if (checkboxCosts && checkboxCategory(name)) continue;
// 快递默认按整单一笔,不能因多件商品重复生成运费。
if (normalized === '快递') {
if (!remaining.some(r => r.key === 'shipping'))
remaining.push({
key: 'shipping',
note: '快递费',
amount: configured['快递'],
payer: defaultPayer,
status: 'estimated',
edited: false,
});
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];
const amount =
previous && (previous.edited || previous.sourceName === name || !previous.sourceName)
? previous.amount
: (configured[normalized] ?? '');
// 新项目及未手改的项目切换采用默认付款人;同名旧成本和手改选择保留。
const useDefaultPayer =
!previous || (!previous.edited && previous.sourceName && normalize(previous.sourceName) !== normalized);
const payer = useDefaultPayer
? normalized === '包装'
? packagingPayer
: defaultPayer
: previous.payer || defaultPayer;
linked.push({
...previous,
key,
note: name,
sourceName: name,
lineNumber: index + 1,
amount,
payer,
status: previous?.status || 'estimated',
edited: previous?.edited || false,
});
}
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];
const amount=previous&&(previous.edited||previous.sourceName===name||!previous.sourceName)?previous.amount:(configured[normalized]??'');
// 新项目及未手改的项目切换采用默认付款人;同名旧成本和手改选择保留。
const useDefaultPayer=!previous||(!previous.edited&&previous.sourceName&&normalize(previous.sourceName)!==normalized);
const payer=useDefaultPayer?(normalized==='包装'?packagingPayer:defaultPayer):(previous.payer||defaultPayer);
linked.push({...previous,key,note:name,sourceName:name,lineNumber:index+1,amount,payer,status:previous?.status||'estimated',edited:previous?.edited||false});
}
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)));
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});
return [...manual,...linked];
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))),
);
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,
});
return [...manual, ...linked];
}
const api={presetCatalog,customPresets,presetKey,presetFee,sync,defaults,normalize,checkboxCategory,boxCount,presetKind,presetDefaults,presets,checkboxFees};if(typeof module==='object'&&module.exports)module.exports=api;else root.FeeEstimates=api;
})(typeof globalThis==='object'?globalThis:this);
const api = {
presetCatalog,
customPresets,
presetKey,
presetFee,
sync,
defaults,
normalize,
checkboxCategory,
boxCount,
presetKind,
presetDefaults,
presets,
checkboxFees,
};
if (typeof module === 'object' && module.exports) module.exports = api;
else root.FeeEstimates = api;
})(typeof globalThis === 'object' ? globalThis : this);