213 lines
8.2 KiB
TypeScript
213 lines
8.2 KiB
TypeScript
import { useState, type FormEvent } from 'react';
|
||
import type { LedgerClient } from '../../lib/api';
|
||
import { Calc, type MemberRow } from '../../shared';
|
||
import { Choice, Field } from './controls';
|
||
import { errorText, memberOptions, readForm, type Shop, type Notice } from './settingsModel';
|
||
|
||
function shopValues(shop: Shop, today: string) {
|
||
const month = today.slice(0, 7);
|
||
const [year, n] = month.split('-').map(Number);
|
||
const next = (n === 12 ? year + 1 : year) + '-' + String(n === 12 ? 1 : n + 1).padStart(2, '0');
|
||
return {
|
||
storeName: shop.storeName,
|
||
receiptSubtitle: shop.receiptSubtitle || '',
|
||
contact: shop.contact || '',
|
||
creditCardRatePermille: String(shop.creditCardRatePermille),
|
||
profitSharePercent: String(shop.profitSharePercent),
|
||
profitShareEffectiveMonth: shop.profitShareEffectiveMonth >= month ? shop.profitShareEffectiveMonth : next,
|
||
...shop.feeDefaults,
|
||
};
|
||
}
|
||
export function ShopForm({
|
||
client,
|
||
shop,
|
||
today,
|
||
members,
|
||
team,
|
||
busy,
|
||
dirty,
|
||
onDirty,
|
||
onBusy,
|
||
onSaved,
|
||
notice,
|
||
}: {
|
||
client: LedgerClient;
|
||
shop: Shop;
|
||
today: string;
|
||
members: MemberRow[];
|
||
team: boolean;
|
||
busy: boolean;
|
||
dirty: boolean;
|
||
onDirty: (v: boolean) => void;
|
||
onBusy: (v: boolean) => void;
|
||
onSaved: (result: Shop) => Promise<void>;
|
||
notice: Notice;
|
||
}) {
|
||
const baseline = shopValues(shop, today);
|
||
const [error, setError] = useState('');
|
||
const [saved, setSaved] = useState(false);
|
||
const [saving, setSaving] = useState(false);
|
||
const [version, setVersion] = useState(0);
|
||
async function submit(event: FormEvent<HTMLFormElement>) {
|
||
event.preventDefault();
|
||
const values = readForm(event.currentTarget);
|
||
let committed = false;
|
||
try {
|
||
if (busy || client.busy || client.pending()) throw new Error('请先等当前操作完成,再保存店铺配置。');
|
||
const integer = (key: string, max: number, title: string) => {
|
||
if (!/^\d+$/.test(values[key]) || Number(values[key]) > max)
|
||
throw new Error(title + '请填写0~' + max + '之间的整数。');
|
||
return Number(values[key]);
|
||
};
|
||
if (!values.storeName.trim() || values.storeName.trim().length > 80) throw new Error('店名请填写1~80字。');
|
||
for (const key of ['certificate', 'packaging', 'shipping'])
|
||
if (Calc.cents(values[key], '默认费用') > 100000000) throw new Error('默认费用最多1000000元。');
|
||
if (!members.some(m => m.member === values.packagingPayer)) throw new Error('请选择包装默认付款人。');
|
||
if (
|
||
!/^\d{4}-(0[1-9]|1[0-2])$/.test(values.profitShareEffectiveMonth) ||
|
||
values.profitShareEffectiveMonth < today.slice(0, 7)
|
||
)
|
||
throw new Error('分成生效月份请选择本月或以后。');
|
||
const data = {
|
||
revision: shop.revision,
|
||
storeName: values.storeName.trim(),
|
||
receiptSubtitle: values.receiptSubtitle.trim(),
|
||
contact: values.contact.trim(),
|
||
creditCardRatePermille: integer('creditCardRatePermille', 1000, '信用卡费率'),
|
||
...(values.profitSharePercent !== baseline.profitSharePercent ||
|
||
values.profitShareEffectiveMonth !== baseline.profitShareEffectiveMonth
|
||
? {
|
||
profitSharePercent: integer('profitSharePercent', 100, '利润分成比例'),
|
||
profitShareEffectiveMonth: values.profitShareEffectiveMonth,
|
||
}
|
||
: {}),
|
||
feeDefaults: {
|
||
certificate: values.certificate,
|
||
packaging: values.packaging,
|
||
shipping: values.shipping,
|
||
packagingPayer: values.packagingPayer,
|
||
},
|
||
};
|
||
setSaving(true);
|
||
onBusy(true);
|
||
setError('');
|
||
const result = await client.mutate<{ shopSettings: Shop }>('/api/shop-settings', data);
|
||
committed = true;
|
||
await onSaved(result.shopSettings);
|
||
onDirty(false);
|
||
setSaved(true);
|
||
setVersion(v => v + 1);
|
||
notice('店铺配置已保存');
|
||
} catch (err) {
|
||
const message = committed ? '店铺配置已保存,页面更新未完成,请刷新后核对。' : errorText(err);
|
||
setError(
|
||
message +
|
||
((err as { status?: number }).status === 409 ? ' 已填写内容已保留,请刷新读取最新配置后再修改。' : ''),
|
||
);
|
||
notice(message, true);
|
||
} finally {
|
||
setSaving(false);
|
||
onBusy(false);
|
||
}
|
||
}
|
||
const money = { type: 'number', required: true, min: 0, max: 1000000, step: '0.01' };
|
||
return (
|
||
<>
|
||
<div className="panel-title">
|
||
<div>
|
||
<h2>店铺配置</h2>
|
||
<p id="shop-settings-status" aria-live="polite" className={'small ' + (dirty ? 'danger-text' : 'muted')}>
|
||
{saving
|
||
? '正在保存店铺配置…'
|
||
: dirty
|
||
? '修改尚未保存。'
|
||
: saved
|
||
? '已保存,后续开单与登录使用当前配置。'
|
||
: '当前生效配置;仅店主可以修改。'}
|
||
</p>
|
||
</div>
|
||
</div>
|
||
<form
|
||
id="shop-settings-form"
|
||
key={version}
|
||
data-revision={shop.revision}
|
||
onSubmit={submit}
|
||
onChange={event => {
|
||
const values = readForm(event.currentTarget);
|
||
onDirty(Object.entries(baseline).some(([key, v]) => String(v ?? '') !== values[key]));
|
||
}}
|
||
>
|
||
<fieldset className="shop-settings-fields" disabled={saving}>
|
||
<div className="grid three">
|
||
<Field label="店名" name="storeName" defaultValue={baseline.storeName} required maxLength={80} />
|
||
<Field label="单据标题" name="receiptSubtitle" defaultValue={baseline.receiptSubtitle} maxLength={80} />
|
||
<Field
|
||
label="联系电话/客户联系信息(选填)"
|
||
name="contact"
|
||
defaultValue={baseline.contact}
|
||
maxLength={80}
|
||
/>
|
||
</div>
|
||
<h3>开单默认费用</h3>
|
||
<div className="grid four">
|
||
<Field label="证书成本(元)" name="certificate" defaultValue={baseline.certificate} {...money} />
|
||
<Field label="包装成本(元)" name="packaging" defaultValue={baseline.packaging} {...money} />
|
||
<Field label="整单快递成本(元)" name="shipping" defaultValue={baseline.shipping} {...money} />
|
||
<Choice
|
||
label="包装默认付款人"
|
||
name="packagingPayer"
|
||
defaultValue={baseline.packagingPayer}
|
||
options={memberOptions(members, baseline.packagingPayer, team)}
|
||
/>
|
||
</div>
|
||
<p className="small muted">
|
||
默认费用只用于新增项目。已有订单、模板和草稿里已经填写的费用及付款人按原值保留,开单时仍可按实际修改。
|
||
</p>
|
||
<h3>手续费与利润分成</h3>
|
||
<div className="grid three">
|
||
<Field
|
||
label="信用卡手续费率(‰)"
|
||
name="creditCardRatePermille"
|
||
defaultValue={baseline.creditCardRatePermille}
|
||
type="number"
|
||
min={0}
|
||
max={1000}
|
||
step={1}
|
||
required
|
||
/>
|
||
<Field
|
||
label="助理利润分成比例(%)"
|
||
name="profitSharePercent"
|
||
defaultValue={baseline.profitSharePercent}
|
||
type="number"
|
||
min={0}
|
||
max={100}
|
||
step={1}
|
||
required
|
||
/>
|
||
<Field
|
||
label="分成生效月份"
|
||
name="profitShareEffectiveMonth"
|
||
defaultValue={baseline.profitShareEffectiveMonth}
|
||
type="month"
|
||
min={today.slice(0, 7)}
|
||
required
|
||
/>
|
||
</div>
|
||
<p className="small muted">
|
||
新建订单使用当前信用卡费率;修改旧单沿用原费率。分成按生效月份统计,历史月份保留原口径。
|
||
</p>
|
||
</fieldset>
|
||
<div id="shop-settings-error" className={error ? 'error' : undefined} aria-live="polite">
|
||
{error}
|
||
</div>
|
||
<div className="form-actions">
|
||
<button className="primary" type="submit" disabled={busy || client.busy || !!client.pending()}>
|
||
保存店铺配置
|
||
</button>
|
||
</div>
|
||
</form>
|
||
</>
|
||
);
|
||
}
|