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; 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) { 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 ( <>

店铺配置

{saving ? '正在保存店铺配置…' : dirty ? '修改尚未保存。' : saved ? '已保存,后续开单与登录使用当前配置。' : '当前生效配置;仅店主可以修改。'}

{ const values = readForm(event.currentTarget); onDirty(Object.entries(baseline).some(([key, v]) => String(v ?? '') !== values[key])); }} >

开单默认费用

默认费用只用于新增项目。已有订单、模板和草稿里已经填写的费用及付款人按原值保留,开单时仍可按实际修改。

手续费与利润分成

新建订单使用当前信用卡费率;修改旧单沿用原费率。分成按生效月份统计,历史月份保留原口径。

{error}
); }