保存工作区合并前的本地开发现场 ledger-migration-typed-54c0587
This commit is contained in:
1 parent
54c05879d5
commit
d18273ac37
82 files changed
+8878
-4757
No files matched your search
@@ -1,10 +1,30 @@
|
||||
'use strict';
|
||||
// 只生成可修改的预估值,不写支付流水。金额输入与表单一致,使用元的字符串。
|
||||
/**
|
||||
* @typedef {{amount:string,payer:string,name?:string,enabled?:boolean,byQuantity?:boolean}} CostPreset
|
||||
* @typedef {Record<string,CostPreset>} Presets
|
||||
*/
|
||||
/**
|
||||
* @template [Amount=string]
|
||||
* @typedef {{
|
||||
* key: string, note: string, amount: Amount|null, payer: string, edited?: boolean, status?: string,
|
||||
* sourceName?: string, lineNumber?: number, expense_id?: string, funding?: Record<string,unknown>
|
||||
* }} FeeItem
|
||||
*/
|
||||
/**
|
||||
* @typedef {{id?:string,name?:string,costRemoved?:boolean}} SaleCharge
|
||||
* @typedef {{certificate?:string,packaging?:string,shipping?:string,packagingPayer?:string}} ShopCosts
|
||||
*/
|
||||
(function (root) {
|
||||
const Calc = typeof module === 'object' && module.exports ? require('./calculations') : root.LedgerCalc;
|
||||
const Calc =
|
||||
typeof module === 'object' && module.exports
|
||||
? require('./calculations')
|
||||
: /** @type {typeof globalThis} */ (root).LedgerCalc;
|
||||
const defaults = { 证书: '4', 包装: '3', 快递: '6' };
|
||||
const normalize = name => ({ 包装盒: '包装', 快递费: '快递', 运费: '快递' })[name] || name;
|
||||
const normalize = (/** @type {string} */ name) =>
|
||||
/** @type {Record<string,string>} */ ({ 包装盒: '包装', 快递费: '快递', 运费: '快递' })[name] || name;
|
||||
// 勾选成本与客户收费分开。保留旧项目,只有明确勾选的新项目使用这些固定金额。
|
||||
/** @param {unknown} name */
|
||||
function checkboxCategory(name) {
|
||||
const value = String(name || '').trim();
|
||||
if (['快递', '快递费', '运费', '京东快递', '圆通快递'].includes(value)) return 'shipping';
|
||||
@@ -13,6 +33,7 @@
|
||||
if (['银链', '银链(家里)', '银链(家里)'].includes(value)) return 'silverChain';
|
||||
return '';
|
||||
}
|
||||
/** @param {unknown} value */
|
||||
function boxCount(value) {
|
||||
const text = String(value ?? '').trim(),
|
||||
count = Number(text);
|
||||
@@ -20,6 +41,7 @@
|
||||
throw new Error('盒子数量须为支持范围内的正整数');
|
||||
return count;
|
||||
}
|
||||
/** @type {Readonly<Presets>} */
|
||||
const presetDefaults = Object.freeze({
|
||||
jd: { amount: '8', payer: 'owner' },
|
||||
yt: { amount: '6', payer: 'order' },
|
||||
@@ -27,9 +49,11 @@
|
||||
steel: { amount: '7', payer: 'owner' },
|
||||
silver: { amount: '18', payer: 'owner' },
|
||||
});
|
||||
const validMember = value =>
|
||||
const validMember = (/** @type {string} */ value) =>
|
||||
['owner', 'assistant', 'assistant2'].includes(value) || /^staff_[a-f0-9-]{36}$/.test(value);
|
||||
/** @type {Record<string,string>} */
|
||||
const presetNames = { jd: '京东快递', yt: '圆通快递', box: '盒子', steel: '钢绳(店里)', silver: '银链(家里)' };
|
||||
/** @param {Presets} [values] */
|
||||
function presets(values) {
|
||||
return Object.fromEntries(
|
||||
Object.entries(values === undefined ? presetDefaults : values)
|
||||
@@ -37,6 +61,7 @@
|
||||
.map(([kind, row]) => [kind, { ...presetDefaults[kind], ...row }]),
|
||||
);
|
||||
}
|
||||
/** @param {Presets} [values] */
|
||||
function presetCatalog(values) {
|
||||
return Object.fromEntries(
|
||||
Object.entries(presets(values)).map(([kind, row]) => [
|
||||
@@ -45,6 +70,7 @@
|
||||
]),
|
||||
);
|
||||
}
|
||||
/** @param {Presets} [values] */
|
||||
function customPresets(values) {
|
||||
return (
|
||||
(values !== undefined && Object.keys(presetDefaults).some(kind => !Object.hasOwn(values, kind))) ||
|
||||
@@ -55,12 +81,24 @@
|
||||
)
|
||||
);
|
||||
}
|
||||
/** @param {string} kind */
|
||||
function presetKey(kind) {
|
||||
return (
|
||||
{ jd: 'shipping', yt: 'shipping', box: 'box', steel: 'braiding', silver: 'other' }[kind] ||
|
||||
'manual:' + kind.slice(7)
|
||||
/** @type {Record<string,string>} */ ({
|
||||
jd: 'shipping',
|
||||
yt: 'shipping',
|
||||
box: 'box',
|
||||
steel: 'braiding',
|
||||
silver: 'other',
|
||||
})[kind] || 'manual:' + kind.slice(7)
|
||||
);
|
||||
}
|
||||
/**
|
||||
* @param {string} kind
|
||||
* @param {unknown} quantity
|
||||
* @param {string} member
|
||||
* @param {Presets} [values]
|
||||
*/
|
||||
function presetFee(kind, quantity, member, values) {
|
||||
const row = presetCatalog(values)[kind];
|
||||
if (!row || !row.enabled) throw new Error('成本项目已停用,请重新核对');
|
||||
@@ -76,6 +114,10 @@
|
||||
edited: true,
|
||||
};
|
||||
}
|
||||
/**
|
||||
* @param {FeeItem<string|number>} item
|
||||
* @param {Presets} [values]
|
||||
*/
|
||||
function configuredKind(item, values) {
|
||||
for (const [kind, row] of Object.entries(presetCatalog(values))) {
|
||||
if (!row.enabled || item.key !== presetKey(kind)) continue;
|
||||
@@ -92,9 +134,13 @@
|
||||
}
|
||||
return '';
|
||||
}
|
||||
/**
|
||||
* @param {FeeItem<string|number>} item
|
||||
* @param {{recorded?:boolean,presets?:Presets}} [options]
|
||||
*/
|
||||
function presetKind(item, { recorded = false, presets: values } = {}) {
|
||||
const config = presets(values),
|
||||
matchPayer = kind =>
|
||||
matchPayer = (/** @type {string} */ 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);
|
||||
@@ -139,16 +185,21 @@
|
||||
}
|
||||
return '';
|
||||
}
|
||||
/**
|
||||
* @param {{shipping?:string,box?:boolean,quantity?:string,steel?:boolean,silver?:boolean}} [selection]
|
||||
* @param {string} [member]
|
||||
* @param {Presets} [values]
|
||||
*/
|
||||
function checkboxFees(
|
||||
{ shipping = '', box = false, quantity = '1', steel = false, silver = false } = {},
|
||||
member = 'assistant',
|
||||
values,
|
||||
) {
|
||||
const config = presets(values),
|
||||
items = [],
|
||||
payer = kind =>
|
||||
items = /** @type {FeeItem[]} */ ([]),
|
||||
payer = (/** @type {string} */ kind) =>
|
||||
config[kind].payer === 'order' ? (validMember(member) ? member : 'assistant') : config[kind].payer;
|
||||
const add = (kind, key, note, count = 1) => {
|
||||
const add = (/** @type {string} */ kind, /** @type {string} */ key, /** @type {string} */ note, count = 1) => {
|
||||
const amount = Calc.cents(String(config[kind].amount), '常用成本') * count;
|
||||
if (!Number.isSafeInteger(amount) || amount > 100000000000) throw new Error('常用成本金额超出支持范围');
|
||||
items.push({
|
||||
@@ -170,23 +221,30 @@
|
||||
if (silver) add('silver', 'other', '银链(家里)');
|
||||
return items;
|
||||
}
|
||||
/**
|
||||
* @template Amount
|
||||
* @param {FeeItem<Amount>[]} existing
|
||||
* @param {{charges?:SaleCharge[]}[]} lines
|
||||
* @param {{addShipping?:boolean,defaultPayer?:string,shopDefaults?:ShopCosts,checkboxCosts?:boolean}} [options]
|
||||
*/
|
||||
function sync(
|
||||
existing,
|
||||
lines,
|
||||
{ addShipping = false, defaultPayer = 'assistant', shopDefaults = {}, checkboxCosts = false } = {},
|
||||
) {
|
||||
/** @type {Record<string,string>} */
|
||||
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 !== undefined && validMember(shopDefaults.packagingPayer)
|
||||
? shopDefaults.packagingPayer
|
||||
: 'owner';
|
||||
/** @type {FeeItem<string|Amount>[]} */
|
||||
const remaining = existing.map(r => ({ ...r })),
|
||||
linked = [];
|
||||
linked = /** @type {FeeItem<string|Amount>[]} */ ([]);
|
||||
for (const [index, line] of lines.entries())
|
||||
for (const charge of line.charges || []) {
|
||||
const name = String(charge.name || '').trim();
|
||||
@@ -280,5 +338,5 @@
|
||||
checkboxFees,
|
||||
};
|
||||
if (typeof module === 'object' && module.exports) module.exports = api;
|
||||
else root.FeeEstimates = api;
|
||||
else Object.assign(root, { FeeEstimates: api });
|
||||
})(typeof globalThis === 'object' ? globalThis : this);
|
||||
Reference in new issue
Block a user