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

189 lines
6.6 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 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 (
<option key={String(value)} value={String(value)} disabled={!!disabled}>
{text}
</option>
);
});
}
export function Head({ eye, title, sub, children }: { eye: string; title: string; sub: string; children?: ReactNode }) {
return (
<header className="page-heading">
<div>
<div className="eyebrow">{eye}</div>
<h1>{title}</h1>
<p>{sub}</p>
</div>
<div className="actions">{children}</div>
</header>
);
}
export function Modal({
id,
title,
close,
action,
children,
}: {
id: string;
title: string;
close(): void;
action: string;
children: ReactNode;
}) {
const ref = useRef<HTMLDialogElement>(null);
useEffect(() => {
const d = ref.current!;
d.showModal();
return () => d.close();
}, []);
return (
<dialog
ref={ref}
id={id}
onCancel={e => {
e.preventDefault();
close();
}}
>
<button type="button" className="dialog-close" aria-label={'关闭' + title} data-action={action} onClick={close}>
×
</button>
<h2 id={id === 'recovery-dialog' ? 'recovery-detail-title' : 'recovery-change-title'}>{title}</h2>
{children}
</dialog>
);
}
export function Settlement({ q, draft, input }: { q?: Totals | null; draft?: Draft; input?: Partial<Input> | 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) => (
<section key={i}>
<h3>
置换商品 {i + 1}:{part.newProduct?.name}
</h3>
<Settlement q={part} input={{ ...input?.exchange?.items?.[i], mode: 'exchange' }} />
</section>
))}
<div className="recovery-equations">
{direct ? (
<>
<p>
回收金额:{grams(q.oldMg)}g × {money(Math.round(Number(input?.buy_rate ?? draft?.buy_rate ?? 0) * 100))}
/g = <strong>{money(def)}</strong>
</p>
{def !== q.purchaseAmount && (
<p>
核实后实际回收款:<strong>{money(q.purchaseAmount)}</strong>
</p>
)}
</>
) : (
<>
{q.differenceMg > 0 ? (
<p>
补金差价:({grams(q.newMg)} − {grams(q.oldMg)})g ×{' '}
{money(Math.round(Number(input?.exchange?.gold_price ?? draft?.gold_price ?? 0) * 100))}/g ={' '}
<strong>{money(q.goldAmount)}</strong>
</p>
) : (
<p>无需补金差价。</p>
)}
<p>
新货工费:
{input?.exchange?.products
? '各件工费合计'
: input?.exchange?.labor_mode === 'total'
? '按总额'
: grams(q.newMg) +
'g × ' +
money(Math.round(Number(input?.exchange?.labor_rate || 0) * 100)) +
'/g'}{' '}
= <strong>{money(q.laborAmount)}</strong>
</p>
<p>
折旧费:{grams(q.depreciationMg)}g ×{' '}
{money(Math.round(Number(input?.exchange?.depreciation_rate ?? draft?.depreciation_rate ?? 10) * 100))}/g
= <strong>{money(q.depreciationAmount)}</strong>
</p>
{!!q.chargesAmount && <p>其他收费:{money(q.chargesAmount)}</p>}
{!!q.discount && <p>优惠:−{money(q.discount)}</p>}
{!!q.roundingAdjustment && (
<p>
费用取整调整:{q.roundingAdjustment > 0 ? '+' : '−'}
{money(Math.abs(q.roundingAdjustment))}
</p>
)}
<p>
本次置换费用:<strong>{money(q.receivable)}</strong>
</p>
{q.depositMg > 0 ? (
<p>余料寄存:{grams(q.depositMg)}g,暂不结算。</p>
) : q.surplusMg > 0 ? (
<p>
余料回收:({grams(q.oldMg)} − {grams(q.newMg)})g ×{' '}
{money(Math.round(Number(input?.buy_rate || 0) * 100))}/g = <strong>{money(q.purchaseAmount)}</strong>
</p>
) : (
<p>无需余料回收。</p>
)}
</>
)}
</div>
<div className="recovery-net">
<span>{q.netSettlement > 0 ? '客户补款' : q.netSettlement < 0 ? '店里付款' : '无需现金收付'}</span>
<strong>{money(Math.abs(q.netSettlement))}</strong>
</div>
{!direct && (
<p>
最终结算 = 置换费用 {money(q.receivable)} − 余料回收款 {money(q.purchaseAmount)}。
{!q.netSettlement && '本次无需现金收付款。'}
</p>
)}
</>
);
}
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<T>(key: string): T | null {
try {
return JSON.parse(localStorage.getItem(key) || 'null');
} catch {
return null;
}
}
export function LegacyCount({ value }: { value?: Permission }) {
return value?.requiresLegacyCountVerified ? (
<>
<p>{value.legacyCountNotice || '这笔历史记录未记录对数,当前对数按实际盘点基准保留。'}</p>
<label>
<input type="checkbox" name="legacyCountVerified" required />
已核对当前对数基准,理解本次更正对历史对数的处理
</label>
</>
) : null;
}