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; 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) => ( ); 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 (
{amount(key, material ? '料费成本合计(元)' : '进货总工费(元)', value, gram)}

{formula}

{material && ( )}
); } let summary; try { summary = costBreakdown(line, quantity, parts, modes, rates).total; } catch { summary = null; } return (

{line.name} · {line.unit === 'g' ? grams(line.quantity) + 'g' : line.quantity + '件'}

{choice('配件及其他成本付款人', 'goodsPayer', members(), line.payer || 'owner')}
{!line.costParts && line.cost !== null && (

原记录未分项,总额 ¥{yuan(line.cost)};请按凭据拆分,不要把总额再填入每个项目。

)}
{group('material')} {group('labor')} {amount('accessories', '配件成本(元,本行合计)', parts.accessories)} {amount('other', '其他货品成本(元,本行合计)', parts.other)}
本行货品成本合计:{summary == null ? '请补齐(没有的项目填0)' : '¥' + yuan(summary)}
); }