保存工作区合并前的本地开发现场 ledger-migration-typed-54c0587
This commit is contained in:
1 parent
54c05879d5
commit
d18273ac37
82 files changed
+8878
-4757
No files matched your search
@@ -0,0 +1,523 @@
|
||||
import { Fragment, type InputHTMLAttributes, type SelectHTMLAttributes, type ReactNode } from 'react';
|
||||
import type { ChargeState, OrderForm, LineState, PurchaseState, SyncContext, LedgerState } from '../../shared';
|
||||
import type { FormStore } from '../CreateOrder/useOrderForm';
|
||||
|
||||
interface FieldProps {
|
||||
label: ReactNode;
|
||||
help?: ReactNode;
|
||||
onChange?: (value: string) => void;
|
||||
value?: string | number | null;
|
||||
}
|
||||
type InputProps = FieldProps & Omit<InputHTMLAttributes<HTMLInputElement>, keyof FieldProps>;
|
||||
type Option = string | readonly [string, string, boolean?];
|
||||
type ChoiceProps = FieldProps &
|
||||
Omit<SelectHTMLAttributes<HTMLSelectElement>, keyof FieldProps> & { options: readonly Option[] };
|
||||
type UpdateForm = (change: (form: OrderForm) => OrderForm) => void;
|
||||
type TextKey<T> = { [K in keyof T]-?: T[K] extends string ? K : never }[keyof T];
|
||||
|
||||
import { Fees, Model } from '../../shared';
|
||||
import { uuid } from '../CreateOrder/useOrderForm';
|
||||
|
||||
export function Input({ label, name, value, type = 'text', hidden, help, onChange, ...props }: InputProps) {
|
||||
return (
|
||||
<label hidden={hidden}>
|
||||
{label}
|
||||
<input
|
||||
{...props}
|
||||
name={name}
|
||||
type={type}
|
||||
value={value ?? ''}
|
||||
onChange={e => onChange?.(e.target.value)}
|
||||
disabled={hidden || props.disabled}
|
||||
/>
|
||||
{help && <small className="muted">{help}</small>}
|
||||
</label>
|
||||
);
|
||||
}
|
||||
export function Choice({ label, name, value, options, hidden, help, onChange, ...props }: ChoiceProps) {
|
||||
return (
|
||||
<label hidden={hidden}>
|
||||
{label}
|
||||
<select
|
||||
{...props}
|
||||
name={name}
|
||||
value={value ?? ''}
|
||||
onChange={e => onChange?.(e.target.value)}
|
||||
disabled={hidden || props.disabled}
|
||||
>
|
||||
{options.map(option => {
|
||||
const [key, name, disabled] = typeof option === 'string' ? [option, option] : option;
|
||||
return (
|
||||
<option key={key} value={key} disabled={!!disabled}>
|
||||
{name}
|
||||
</option>
|
||||
);
|
||||
})}
|
||||
</select>
|
||||
{help}
|
||||
</label>
|
||||
);
|
||||
}
|
||||
const money = { type: 'number', step: '0.01', min: '0' };
|
||||
const categories: Record<string, string> = {
|
||||
jd: 'shipping',
|
||||
yt: 'shipping',
|
||||
box: 'box',
|
||||
steel: 'braiding',
|
||||
silver: 'silverChain',
|
||||
};
|
||||
const amountPlaceholder = '开单前必填,没有填0';
|
||||
export function EditLine({
|
||||
index,
|
||||
line,
|
||||
state,
|
||||
ctx,
|
||||
form,
|
||||
update,
|
||||
reloadPools,
|
||||
error,
|
||||
}: {
|
||||
index: number;
|
||||
line: LineState;
|
||||
state: LedgerState;
|
||||
ctx: SyncContext;
|
||||
form: OrderForm;
|
||||
update: UpdateForm;
|
||||
reloadPools: () => void;
|
||||
error: (message: string) => void;
|
||||
}) {
|
||||
const v = Model.lineVisibility(line, state),
|
||||
p = line.purchase,
|
||||
pool = line.pool,
|
||||
pooled = pool?.source === 'pool',
|
||||
prefix = 'cost-';
|
||||
const change = (fn: (line: LineState) => LineState) => update(f => Model.updateLine(f, index, fn(f.lines[index])));
|
||||
const prop = (name: TextKey<LineState>) => ({
|
||||
name,
|
||||
value: line[name],
|
||||
onChange: (value: string) => change(l => Model.setLineField(l, name, value)),
|
||||
});
|
||||
const cost = (name: TextKey<PurchaseState>) => ({
|
||||
name: prefix + name,
|
||||
value: p![name],
|
||||
onChange: (value: string) => change(l => Model.setPurchaseField(l, name, value)),
|
||||
});
|
||||
const laborFunding = p?.costFundings?.labor;
|
||||
const members = (value: string) => Model.memberOptions(state, false, { selected: value });
|
||||
function updateCharge(i: number, fn: (charge: ChargeState) => ChargeState) {
|
||||
change(l => ({ ...l, charges: l.charges.map((c, j) => (j === i ? fn(c) : c)) }));
|
||||
}
|
||||
return (
|
||||
<div className="line-item" data-legacy={line.legacy}>
|
||||
<div className="panel-title">
|
||||
<h3>商品 {index + 1}</h3>
|
||||
<button
|
||||
type="button"
|
||||
data-action="remove-line"
|
||||
onClick={() => {
|
||||
try {
|
||||
update(f => Model.removeLine(f, index));
|
||||
} catch (err) {
|
||||
error(err instanceof Error ? err.message : String(err));
|
||||
}
|
||||
}}
|
||||
>
|
||||
移除
|
||||
</button>
|
||||
</div>
|
||||
<div className="grid four">
|
||||
<Choice label="商品名称" {...prop('nameType')} options={[...Model.productNames, '其他']} />
|
||||
<Input label="其他商品名称" {...prop('customName')} maxLength={60} hidden={!v.customName} />
|
||||
<Choice
|
||||
label="计价方式"
|
||||
{...prop('pricingMode')}
|
||||
options={[
|
||||
['auto', '按金价与工费计算'],
|
||||
['manual', '直接填写金额'],
|
||||
]}
|
||||
/>
|
||||
<Choice label="单位" {...prop('unit')} options={['g', '件']} />
|
||||
<Input
|
||||
label="克重/数量"
|
||||
{...prop('quantity')}
|
||||
type="number"
|
||||
min={line.unit === 'g' ? '0.001' : '1'}
|
||||
step={line.unit === 'g' ? '0.001' : '1'}
|
||||
required
|
||||
/>
|
||||
<Input label="金价/银价(元/克)" {...money} {...prop('goldPrice')} hidden={!v.goldPrice} />
|
||||
<Choice
|
||||
label="销售工费方式"
|
||||
{...prop('laborMode')}
|
||||
hidden={!v.laborMode}
|
||||
options={[
|
||||
['gram', '克工费'],
|
||||
['piece', '件工费'],
|
||||
]}
|
||||
/>
|
||||
<Input label="销售工费" {...money} {...prop('laborRate')} hidden={!v.laborRate} />
|
||||
<Input label="计工费件数" type="number" min="1" step="1" {...prop('pieces')} hidden={!v.pieces} />
|
||||
<Input label="销售单价说明(客户可见)" {...prop('priceNote')} maxLength={100} hidden={!v.priceNote} />
|
||||
<Input label="商品基础金额(不含附加项目)" {...prop('baseSale')} {...money} required readOnly={!v.baseSale} />
|
||||
</div>
|
||||
<div className="sale-charges">
|
||||
<div className="panel-title">
|
||||
<strong>工费附加项目(选填)</strong>
|
||||
<button type="button" data-action="add-charge" onClick={() => update(f => Model.addCharge(f, index, uuid))}>
|
||||
+ 添加附加项目
|
||||
</button>
|
||||
</div>
|
||||
{line.charges.map((c, i) => (
|
||||
<div key={c.id} className="charge-row grid four">
|
||||
<Choice
|
||||
label="附加项目"
|
||||
name="chargeType"
|
||||
value={c.type}
|
||||
options={[...Model.chargeNames, '其他']}
|
||||
onChange={type => updateCharge(i, charge => Model.changeChargeType(charge, type))}
|
||||
/>
|
||||
<Input
|
||||
label="附加项目名称"
|
||||
name="chargeName"
|
||||
value={c.customName}
|
||||
maxLength={60}
|
||||
hidden={c.type !== '其他'}
|
||||
onChange={customName => updateCharge(i, charge => ({ ...charge, customName }))}
|
||||
/>
|
||||
<Input
|
||||
label="附加金额(元)"
|
||||
name="chargeAmount"
|
||||
{...money}
|
||||
value={c.amount}
|
||||
onChange={value => updateCharge(i, charge => Model.setChargeAmount(charge, value))}
|
||||
/>
|
||||
<button type="button" onClick={() => change(l => ({ ...l, charges: l.charges.filter((_, j) => j !== i) }))}>
|
||||
移除项目
|
||||
</button>
|
||||
{c.costRemoved && (
|
||||
<button type="button" onClick={() => update(f => Model.restoreChargeCost(f, index, i))}>
|
||||
恢复内部成本
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<div className="grid two">
|
||||
<Input label="本行销售额(元,含附加项目)" name="sale" type="number" value={line.sale} readOnly />
|
||||
<p>{line.view.calculation}</p>
|
||||
</div>
|
||||
<div className="internal">
|
||||
<div className="internal-title">内部成本 · 客户单据不显示</div>
|
||||
<div className="grid two">
|
||||
<Choice
|
||||
label="出库货盘(仅黄金)"
|
||||
{...prop('pocket')}
|
||||
options={Model.pocketOptions(state, line.pocket, line.historicalPocket)}
|
||||
disabled={line.unit !== 'g'}
|
||||
/>
|
||||
<Input
|
||||
label="出库数量(对)"
|
||||
{...prop('stockCount')}
|
||||
type="number"
|
||||
min="1"
|
||||
max="1000000000"
|
||||
step="1"
|
||||
inputMode="numeric"
|
||||
required={v.stockCount && !v.stockCountOptional}
|
||||
placeholder="一对填1"
|
||||
hidden={!v.stockCount}
|
||||
help={
|
||||
line.historicalPocket ? '历史未记录对数,可保留空白;与计工费件数分别填写。' : '与计工费件数分别填写。'
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
{line.legacy ? (
|
||||
<>
|
||||
<Choice label="货品成本付款人" {...prop('payer')} options={members(line.payer)} />
|
||||
<Input label="原单货品成本(旧版未分项)" {...prop('cost')} type="number" readOnly />
|
||||
<button
|
||||
type="button"
|
||||
onClick={() =>
|
||||
change(l => {
|
||||
const fresh = Model.newLine(
|
||||
{
|
||||
costParts: { material: '', labor: '', accessories: '0', other: '0' },
|
||||
payer: l.payer,
|
||||
unit: l.unit,
|
||||
},
|
||||
{ ...ctx, member: form.member },
|
||||
);
|
||||
return { ...l, legacy: false, purchase: fresh.purchase, pool: fresh.pool };
|
||||
})
|
||||
}
|
||||
>
|
||||
拆分这行成本
|
||||
</button>
|
||||
</>
|
||||
) : (
|
||||
<div className="cost-fields grid">
|
||||
{pool && (
|
||||
<div className="grid two">
|
||||
<Choice
|
||||
label="料费成本来源"
|
||||
name="cost-materialSource"
|
||||
options={[
|
||||
['manual', '手动填写成本'],
|
||||
['pool', '当日加权料价'],
|
||||
]}
|
||||
value={pool.source}
|
||||
disabled={!v.poolAllowed}
|
||||
onChange={source => change(l => Model.setPoolSource(l, source as 'manual' | 'pool'))}
|
||||
/>
|
||||
<Choice
|
||||
label="料价日期"
|
||||
name="cost-materialPoolId"
|
||||
value={pool.poolId}
|
||||
options={pool.options.map(x => [x.value, x.label])}
|
||||
hidden={!pooled}
|
||||
onChange={poolId => change(l => ({ ...l, pool: { ...l.pool!, poolId, defaultDate: false } }))}
|
||||
/>
|
||||
<button type="button" onClick={reloadPools}>
|
||||
刷新料价
|
||||
</button>
|
||||
{line.view.poolNote && <p>{line.view.poolNote}</p>}
|
||||
</div>
|
||||
)}
|
||||
<div className="grid purchase-cost-grid">
|
||||
<Choice
|
||||
label="进货料费方式"
|
||||
{...cost('materialMode')}
|
||||
options={[
|
||||
['gram', '按克料价'],
|
||||
['total', '直接填总料费'],
|
||||
]}
|
||||
hidden={!v.materialMode}
|
||||
/>
|
||||
<Input
|
||||
label="进货料价(元/克)"
|
||||
{...money}
|
||||
{...cost('materialRate')}
|
||||
hidden={!v.materialRate}
|
||||
readOnly={pooled}
|
||||
placeholder="填写每克进货料价"
|
||||
/>
|
||||
<Input
|
||||
label="料费成本合计(元)"
|
||||
{...money}
|
||||
{...cost('material')}
|
||||
required={!pooled}
|
||||
readOnly={v.materialReadOnly}
|
||||
placeholder={amountPlaceholder}
|
||||
/>
|
||||
<Choice
|
||||
label="料费付款人"
|
||||
{...cost('materialPayer')}
|
||||
required
|
||||
options={members(p!.materialPayer)}
|
||||
hidden={!v.materialPayer}
|
||||
/>
|
||||
</div>
|
||||
<p>
|
||||
{line.view.materialFormula} {line.view.materialOriginal}
|
||||
</p>
|
||||
<div className="grid purchase-cost-grid">
|
||||
<Choice
|
||||
label="进货工费方式"
|
||||
{...cost('laborMode')}
|
||||
options={[
|
||||
['gram', '克工费'],
|
||||
['total', '总工费'],
|
||||
]}
|
||||
/>
|
||||
<Input
|
||||
label="进货克工费(元/克)"
|
||||
{...money}
|
||||
{...cost('laborRate')}
|
||||
hidden={!v.laborRateInput}
|
||||
placeholder="填写每克工费"
|
||||
/>
|
||||
<Input
|
||||
label="进货总工费(元)"
|
||||
{...money}
|
||||
{...cost('labor')}
|
||||
required
|
||||
readOnly={v.laborReadOnly}
|
||||
placeholder={amountPlaceholder}
|
||||
/>
|
||||
<Choice
|
||||
label="进货工费付款人"
|
||||
{...cost('laborPayer')}
|
||||
required
|
||||
disabled={!!laborFunding}
|
||||
options={members(p!.laborPayer)}
|
||||
/>
|
||||
{laborFunding && (
|
||||
<input
|
||||
type="hidden"
|
||||
className="cost-funding"
|
||||
data-component="labor"
|
||||
data-reference={JSON.stringify(laborFunding)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
<p>{line.view.laborFormula}</p>
|
||||
{v.extraCosts ? (
|
||||
<details open>
|
||||
<summary>原有其他货品成本</summary>
|
||||
<Input label="配件成本(元,本行合计)" {...cost('accessories')} {...money} />
|
||||
<Input label="其他货品成本(元,本行合计)" {...cost('other')} {...money} />
|
||||
<Choice label="配件及其他成本付款人" {...prop('payer')} options={members(line.payer)} />
|
||||
</details>
|
||||
) : (
|
||||
<>
|
||||
{(['accessories', 'other'] as const).map(key => (
|
||||
<input key={key} type="hidden" name={prefix + key} value={p![key]} />
|
||||
))}
|
||||
<input type="hidden" name="payer" value={line.payer} />
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
export function EditFees({ store, state, update }: { store: FormStore; state: LedgerState; update: UpdateForm }) {
|
||||
const { form, summary } = store,
|
||||
presets = form.presets;
|
||||
const hasLinkedCost = (kind: string) =>
|
||||
form.feeRows.some(
|
||||
row => row.funding && (row.key === Fees.presetKey(kind) || Fees.checkboxCategory(row.note) === categories[kind]),
|
||||
);
|
||||
return (
|
||||
<section className="panel internal">
|
||||
<div className="panel-title">
|
||||
<h2>实际费用(内部)</h2>
|
||||
<button type="button" onClick={() => update(f => Model.addManualFee(f, state, uuid))}>
|
||||
+ 添加其他成本
|
||||
</button>
|
||||
</div>
|
||||
<Input
|
||||
label="新增费用默认支付日期"
|
||||
type="date"
|
||||
name="fees_date"
|
||||
value={form.fees_date}
|
||||
required
|
||||
max={state.today}
|
||||
onChange={fees_date => update(f => ({ ...f, fees_date }))}
|
||||
/>
|
||||
{presets && (
|
||||
<div className="preset-cost-grid">
|
||||
{Model.presetCatalogView(form, state).map(item => (
|
||||
<Fragment key={item.kind}>
|
||||
<label className="check">
|
||||
<input
|
||||
type="checkbox"
|
||||
name={'cost_' + item.kind}
|
||||
disabled={hasLinkedCost(item.kind)}
|
||||
checked={!!presets.checked[item.kind]}
|
||||
onChange={e => update(f => Model.togglePreset(f, item.kind, e.target.checked, state, uuid))}
|
||||
/>
|
||||
<strong>{item.name}</strong>
|
||||
<span>{item.price}</span>
|
||||
<small>{item.kind === 'yt' && !presets.configured ? summary.presetNotes.ytPayer : item.payer}</small>
|
||||
</label>
|
||||
{item.byQuantity && (
|
||||
<Input
|
||||
label={item.name + '数量(个)'}
|
||||
help={presets.checked[item.kind] ? summary.presetNotes.boxTotal || '' : '勾选后计费'}
|
||||
name={'cost_' + item.kind + '_quantity'}
|
||||
value={presets.quantity[item.kind] ?? '1'}
|
||||
type="number"
|
||||
min="1"
|
||||
max={Math.floor(1000000000 / Number.parseFloat(item.price))}
|
||||
step="1"
|
||||
inputMode="numeric"
|
||||
disabled={!presets.checked[item.kind]}
|
||||
onChange={q => update(f => Model.setPresetQuantity(f, item.kind, q))}
|
||||
/>
|
||||
)}
|
||||
</Fragment>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
<div id="fee-rows">
|
||||
{form.feeRows.map((row, i) => {
|
||||
const edit = (key: 'note' | 'payer' | 'amount' | 'date', value: string) =>
|
||||
update(f => Model.editFeeRow(f, i, key, value));
|
||||
const ref = row.funding;
|
||||
return (
|
||||
<div key={row.key} className="fee-row grid four">
|
||||
{row.key.startsWith('manual:') && !row.automatic ? (
|
||||
<Input
|
||||
label="费用项目名称"
|
||||
name="feeNote"
|
||||
required
|
||||
maxLength={150}
|
||||
value={row.note}
|
||||
onChange={v => edit('note', v)}
|
||||
/>
|
||||
) : (
|
||||
<strong>{row.note}</strong>
|
||||
)}
|
||||
<span>{Model.feeSourceText(row)}</span>
|
||||
<Input
|
||||
label={row.automatic ? '手续费(元,自动)' : '实际成本(元)'}
|
||||
{...money}
|
||||
name="feeAmount"
|
||||
value={row.amount}
|
||||
required
|
||||
placeholder={row.automatic ? undefined : '没有成本填0'}
|
||||
readOnly={!!row.automatic}
|
||||
onChange={v => edit('amount', v)}
|
||||
/>
|
||||
<Choice
|
||||
label="费用付款人"
|
||||
name="feePayer"
|
||||
value={row.payer}
|
||||
disabled={!!ref}
|
||||
options={Model.memberOptions(state, true, { selected: row.payer })}
|
||||
onChange={v => edit('payer', v)}
|
||||
/>
|
||||
<Input
|
||||
label="本笔费用支付日期"
|
||||
type="date"
|
||||
name="feeDate"
|
||||
required
|
||||
max={state.today}
|
||||
readOnly={!!row.automatic}
|
||||
value={row.date || form.fees_date}
|
||||
onChange={v => edit('date', v)}
|
||||
/>
|
||||
{!row.automatic && (
|
||||
<button type="button" onClick={() => update(f => Model.removeFeeRow(f, i))}>
|
||||
移除费用
|
||||
</button>
|
||||
)}
|
||||
{ref && (
|
||||
<input
|
||||
type="hidden"
|
||||
className="cost-funding"
|
||||
data-component="fee"
|
||||
data-reference={JSON.stringify(ref)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
<div hidden={!summary.card}>
|
||||
<Input label="手续费(元,自动)" {...money} value={summary.card?.amountText || ''} readOnly />
|
||||
<Choice
|
||||
label="手续费付款人"
|
||||
name="credit_card_fee_payer"
|
||||
value={form.credit_card_fee_payer}
|
||||
disabled={!summary.card}
|
||||
options={Model.memberOptions(state)}
|
||||
onChange={v => update(f => ({ ...f, credit_card_fee_payer: v }))}
|
||||
/>
|
||||
</div>
|
||||
<p>{summary.feesText}</p>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user