import type { ReactNode } from 'react'; import type { LedgerState } from '../../shared'; import type { Draft, Input, Totals, Permission } from './types'; export type Choice = string | readonly (string | boolean)[]; import { useEffect, useRef } from 'react'; import { Model } from '../../shared'; import { grams, yuan } from '../../lib/format'; export const money = (value?: number | null) => (value == null ? '待填写' : '¥' + yuan(value)); export const rowLabel = (s: LedgerState, key?: string) => s.pocketCatalog?.find(p => p.key === key)?.name || key; export function options(pairs: readonly Choice[]) { return pairs.map(opt => { const [value, text, disabled] = typeof opt === 'string' ? [opt, opt] : opt; return ( ); }); } export function Head({ eye, title, sub, children }: { eye: string; title: string; sub: string; children?: ReactNode }) { return (
{eye}

{title}

{sub}

{children}
); } export function Modal({ id, title, close, action, children, }: { id: string; title: string; close(): void; action: string; children: ReactNode; }) { const ref = useRef(null); useEffect(() => { const d = ref.current!; d.showModal(); return () => d.close(); }, []); return ( { e.preventDefault(); close(); }} >

{title}

{children}
); } export function Settlement({ q, draft, input }: { q?: Totals | null; draft?: Draft; input?: Partial | null }) { if (!q) return null; const direct = input?.mode === 'direct' || draft?.mode === 'direct' || q.mode === 'direct', def = q.defaultPurchaseAmount ?? q.calculatedPurchaseAmount ?? q.purchaseAmount; return ( <> {q.exchangeItems?.map((part, i) => (

置换商品 {i + 1}:{part.newProduct?.name}

))}
{direct ? ( <>

回收金额:{grams(q.oldMg)}g × {money(Math.round(Number(input?.buy_rate ?? draft?.buy_rate ?? 0) * 100))} /g = {money(def)}

{def !== q.purchaseAmount && (

核实后实际回收款:{money(q.purchaseAmount)}

)} ) : ( <> {q.differenceMg > 0 ? (

补金差价:({grams(q.newMg)} − {grams(q.oldMg)})g ×{' '} {money(Math.round(Number(input?.exchange?.gold_price ?? draft?.gold_price ?? 0) * 100))}/g ={' '} {money(q.goldAmount)}

) : (

无需补金差价。

)}

新货工费: {input?.exchange?.products ? '各件工费合计' : input?.exchange?.labor_mode === 'total' ? '按总额' : grams(q.newMg) + 'g × ' + money(Math.round(Number(input?.exchange?.labor_rate || 0) * 100)) + '/g'}{' '} = {money(q.laborAmount)}

折旧费:{grams(q.depreciationMg)}g ×{' '} {money(Math.round(Number(input?.exchange?.depreciation_rate ?? draft?.depreciation_rate ?? 10) * 100))}/g = {money(q.depreciationAmount)}

{!!q.chargesAmount &&

其他收费:{money(q.chargesAmount)}

} {!!q.discount &&

优惠:−{money(q.discount)}

} {!!q.roundingAdjustment && (

费用取整调整:{q.roundingAdjustment > 0 ? '+' : '−'} {money(Math.abs(q.roundingAdjustment))}

)}

本次置换费用:{money(q.receivable)}

{q.depositMg > 0 ? (

余料寄存:{grams(q.depositMg)}g,暂不结算。

) : q.surplusMg > 0 ? (

余料回收:({grams(q.oldMg)} − {grams(q.newMg)})g ×{' '} {money(Math.round(Number(input?.buy_rate || 0) * 100))}/g = {money(q.purchaseAmount)}

) : (

无需余料回收。

)} )}
{q.netSettlement > 0 ? '客户补款' : q.netSettlement < 0 ? '店里付款' : '无需现金收付'} {money(Math.abs(q.netSettlement))}
{!direct && (

最终结算 = 置换费用 {money(q.receivable)} − 余料回收款 {money(q.purchaseAmount)}。 {!q.netSettlement && '本次无需现金收付款。'}

)} ); } export function pocketOptions(state: LedgerState, key: string, historical: boolean, blank?: string) { const active = Model.activePocketKeys(state), result: (string | boolean)[][] = active.map(p => [p, rowLabel(state, p) || p]); if (key && !active.includes(key)) result.push([ key, rowLabel(state, key) + (historical ? '(已停用 · 原货盘)' : '(已停用,请重新选择)'), !historical, ]); return blank ? [['', blank], ...result] : result; } export function loadDraft(key: string): T | null { try { return JSON.parse(localStorage.getItem(key) || 'null'); } catch { return null; } } export function LegacyCount({ value }: { value?: Permission }) { return value?.requiresLegacyCountVerified ? ( <>

{value.legacyCountNotice || '这笔历史记录未记录对数,当前对数按实际盘点基准保留。'}

) : null; }