Files
weiguang-jewelry-ledger/新账系统/web/src/pages/Orders/views/LegacyCostRow.tsx
T

202 lines
7.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useState } from 'react';
import { Calc as LedgerCalc } from '../../../shared';
import type { OrderLine } from '../orderData';
import type { SharedViews } from '../viewTypes';
type CostModeKey = 'material' | 'labor';
type CostInputs = Record<string, string>;
type LineProps = { line: OrderLine; index: number; members: SharedViews['teamMemberOptions']; fallback: string } & Pick<
SharedViews,
'choice' | 'opts' | 'grams' | 'yuan'
>;
const asMoney = (value?: number | null) => (value == null ? '' : (value / 100).toFixed(2));
const costKeys = Object.keys(LedgerCalc.costLabels);
function costBreakdown(
line: OrderLine,
quantity: string | number,
parts: CostInputs,
modes: CostInputs,
rates: CostInputs,
) {
const input: {
unit: string;
quantity: string | number;
costParts: CostInputs;
costMaterial?: { mode: string; rate?: string };
costLabor?: { mode: string; rate?: string };
} = { unit: line.unit, quantity, costParts: parts };
for (const [key, property] of [
['material', 'costMaterial'],
['labor', 'costLabor'],
] as const) {
input[property] = modes[key] === 'gram' ? { mode: 'gram', rate: rates[key] } : { mode: 'total' };
}
return LedgerCalc.purchaseCosts(input);
}
export function LegacyCostRow({ line, index, members, fallback, choice, opts, grams, yuan }: LineProps) {
const prefix = 'cost-' + index + '-';
const quantity = line.unit === 'g' ? (line.quantity / 1000).toFixed(3) : line.quantity;
const initialParts = Object.fromEntries(
costKeys.map(key => [
key,
line.costParts ? asMoney(line.costParts[key]) : ['accessories', 'other'].includes(key) ? '0' : '',
]),
);
const originalMode = ['gram', 'total'].includes(line.costMaterial?.mode ?? '')
? line.costMaterial!.mode
: initialParts.material === ''
? 'gram'
: 'total';
const [parts, setParts] = useState(initialParts);
const [modes, setModes] = useState({
material: line.unit === 'g' ? originalMode : 'total',
labor: line.unit === 'g' ? line.costLabor?.mode || 'total' : 'total',
});
const [rates, setRates] = useState({
material: asMoney(line.costMaterial?.rate),
labor: asMoney(line.costLabor?.rate),
});
const originalTotal = line.unit !== 'g' || originalMode === 'total' ? initialParts.material : '';
const changePart = (key: string, value: string) => setParts(old => ({ ...old, [key]: value }));
const updateMode = (key: CostModeKey, value: string, computed: string) => {
if (value === 'total' && (key === 'labor' || parts.material === '')) changePart(key, computed);
setModes(old => ({ ...old, [key]: value }));
};
const amount = (key: string, title: string, value: string, gram = false) => (
<label>
{title}
<input
type="number"
name={prefix + key}
min="0"
step="0.01"
placeholder="开单前必填,没有填0"
required={!gram}
readOnly={gram}
value={value}
onChange={event => changePart(key, event.target.value)}
/>
</label>
);
function group(key: CostModeKey) {
const material = key === 'material',
gram = modes[key] === 'gram';
const payer = line.costPayers?.[key] ?? line.payer ?? fallback;
let value = parts[key],
formula;
if (gram) {
try {
const result = costBreakdown(
line,
quantity,
{ material: '0', labor: '0', accessories: '0', other: '0' },
{ material: 'total', labor: 'total', [key]: 'gram' },
rates,
).parts[key];
value = asMoney(result);
formula =
result == null
? material
? '填写每克进货料价后,系统自动乘以本行克重。'
: '填写每克工费后自动乘以克重。'
: quantity + 'g × ' + rates[key] + '元/克 = ' + yuan(result) + '元';
} catch (err) {
value = '';
formula = err instanceof Error ? err.message : String(err);
}
} else {
formula = material
? line.unit === 'g'
? '直接填写本行料费合计,不再乘以克重。'
: '按件商品请填写本行料费合计' + (rates.material ? ';每克料价已保留,切回g可恢复' : '') + '。'
: '直接填写本行总工费。';
}
const options = members(false, { selected: payer });
return (
<div className={'purchase-' + key} data-prefix={prefix} data-mode={modes[key]}>
<div className="grid purchase-cost-grid">
<label>
{material ? '进货料费方式' : '进货工费方式'}
<select
name={prefix + key + 'Mode'}
value={modes[key]}
onChange={event => updateMode(key, event.target.value, value)}
>
<option value="gram" disabled={line.unit !== 'g'}>
{material ? '按克料价' : '克工费'}
</option>
<option value="total">{material ? '直接填总料费' : '总工费'}</option>
</select>
</label>
<label hidden={!gram}>
{material ? '进货料价(元/克)' : '进货克工费(元/克)'}
<input
type="number"
name={prefix + key + 'Rate'}
min="0"
step="0.01"
value={rates[key]}
placeholder={material ? '填写每克进货料价' : '填写每克工费'}
disabled={!gram}
required={gram}
onChange={event => setRates(old => ({ ...old, [key]: event.target.value }))}
/>
</label>
{amount(key, material ? '料费成本合计(元)' : '进货总工费(元)', value, gram)}
<label className="purchase-payer">
{material ? '料费付款人' : '进货工费付款人'}
<select
required
name={prefix + key + 'Payer'}
defaultValue={options.some(([id]) => id === payer) ? payer : ''}
>
{options.some(([id]) => id === payer) ? null : (
<option value="" disabled>
请选择
</option>
)}
{opts(options)}
</select>
</label>
</div>
<p className="purchase-formula small muted" aria-live="polite">
{formula}
</p>
{material && (
<p className="purchase-original small muted" hidden={!originalTotal}>
{originalTotal &&
'已保留原料费合计 ' + originalTotal + '元。原金额不会当成每克料价;切换按克后请填写实际进货料价。'}
</p>
)}
</div>
);
}
let summary;
try {
summary = costBreakdown(line, quantity, parts, modes, rates).total;
} catch {
summary = null;
}
return (
<div className="internal cost-editor">
<h3>
{line.name} · {line.unit === 'g' ? grams(line.quantity) + 'g' : line.quantity + '件'}
</h3>
<div className="cost-fallback-payer">
{choice('配件及其他成本付款人', 'goodsPayer', members(), line.payer || 'owner')}
</div>
{!line.costParts && line.cost !== null && (
<p className="small">原记录未分项,总额 ¥{yuan(line.cost)};请按凭据拆分,不要把总额再填入每个项目。</p>
)}
<div className="cost-fields grid">
{group('material')}
{group('labor')}
{amount('accessories', '配件成本(元,本行合计)', parts.accessories)}
{amount('other', '其他货品成本(元,本行合计)', parts.other)}
</div>
<div className="cost-summary small">
本行货品成本合计:{summary == null ? '请补齐(没有的项目填0)' : '¥' + yuan(summary)}
</div>
</div>
);
}