Files
weiguang-jewelry-ledger/新账系统/web/src/pages/Recoveries/derive.ts
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

134 lines
4.2 KiB
TypeScript
Raw Normal View History

import { grams } from '../../lib/format';
import type { Draft, RecoveryModel, Derived } from './types';
function normalize(draft: Draft) {
const result = { ...draft, customer_type: draft.mode === 'direct' ? '' : draft.customer_type };
for (const [key, value] of Object.entries(result))
if (typeof value === 'string') Object.assign(result, { [key]: value.trim() });
if (draft.old_lines?.length)
result.old_lines = [
{
...draft.old_lines[0],
name: draft.old_name,
incoming_weight: draft.incoming_weight,
melted_weight: draft.melted_weight,
purity: draft.purity,
},
...draft.old_lines.slice(1),
];
if (draft.charges)
result.charges = [
...(draft.charges.length || Number(draft.charge_amount) > 0
? [{ name: draft.charge_name, amount: draft.charge_amount }]
: []),
...draft.charges.slice(1),
];
if (draft.fee_items)
result.fee_items = [
...(draft.fee_items.length || Number(draft.fee_amount) > 0
? [
{
...draft.fee_items[0],
key: draft.fee_items[0]?.key || 'other',
note: draft.fee_note,
amount: draft.fee_amount,
payer: draft.fee_payer,
},
]
: []),
...draft.fee_items.slice(1),
];
return result;
}
export function derive(model: RecoveryModel, raw: Draft): Derived {
if (raw.independent_products && raw.mode === 'exchange') {
const parts = [raw, ...(raw.extra_products || [])].map((item, index) =>
derive(model, {
...raw,
...item,
cost_payer: index === 0 ? raw.cost_payer : item.labor_payer,
independent_products: false,
extra_products: [],
charges: [],
fee_items: [],
charge_amount: '0',
fee_amount: '0',
rounding_mode: 'cent',
discount: '0',
}),
);
const draft = {
...raw,
...parts[0].draft,
charge_amount: raw.charge_amount,
fee_amount: raw.fee_amount,
charges: raw.charges,
fee_items: raw.fee_items,
rounding_mode: raw.rounding_mode,
discount: raw.discount,
independent_products: true,
extra_products: parts.slice(1).map(x => x.draft),
};
try {
const input = model.input(draft, { quote: true });
return { draft, parts, input, q: model.calculate(input), error: '' };
} catch (error) {
return { draft, parts, input: null, q: null, error: (error as Error).message };
}
}
let draft = normalize(raw),
old = null,
newMg = null,
q = null,
input = null,
error;
try {
old = model.calculate(
model.input(
{
...draft,
mode: 'direct',
buy_rate: '1',
buy_total: '1',
buy_total_manual: true,
...(draft.mode === 'exchange' && draft.old_source === 'deposit'
? {
old_lines: [
{
name: '客户寄存料',
incoming_weight: draft.deposit_weight,
melted_weight: draft.deposit_weight,
purity: '100',
},
],
}
: {}),
},
{ quote: true },
),
);
} catch {
// 草稿可以缺少旧料行;下方完整试算会显示对应的输入错误。
}
if (draft.mode === 'exchange') {
try {
newMg = model.newWeight(draft);
} catch {
// 新料未填全时保留空的克重预览,下方完整试算给出提示。
}
}
if (old && newMg !== null && !draft.depreciation_manual)
draft = { ...draft, depreciation_weight: grams(Math.min(old.oldMg, newMg)) };
const hasBuy = draft.mode !== 'exchange' || !!(old && newMg !== null && old.oldMg > newMg),
diff = draft.mode === 'exchange' && !!(old && newMg !== null && newMg > old.oldMg);
if (!hasBuy) draft = { ...draft, buy_total_manual: false, buy_total: '0.00' };
try {
input = model.input(draft, { quote: true });
q = model.calculate(input);
error = '';
if (!draft.buy_total_manual) draft = { ...draft, buy_total: (q.defaultPurchaseAmount / 100).toFixed(2) };
} catch (e) {
error = (e as Error).message;
}
return { draft, old, newMg, q, input, error, hasBuy, diff };
}