486 lines
15 KiB
TypeScript
486 lines
15 KiB
TypeScript
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
|
|
import { type LedgerClient, type ApiError } from '../../lib/api';
|
|||
|
|
import { useClientSnapshot } from '../../lib/useClient';
|
|||
|
|
import { searchParam, useLocation } from '../../lib/router';
|
|||
|
|
import type { LedgerState } from '../../shared';
|
|||
|
|
import { PoolList, RollingSummary } from './PoolTables';
|
|||
|
|
import { PurchaseForm, PurchaseChange, Settlement } from './PurchaseForms';
|
|||
|
|
import { ChangePanel, EnableForm, ReviewForm } from './Review';
|
|||
|
|
import {
|
|||
|
|
draftKey,
|
|||
|
|
validatePools,
|
|||
|
|
validatePurchaseResponse,
|
|||
|
|
validateEnable,
|
|||
|
|
validateChange,
|
|||
|
|
statuses,
|
|||
|
|
type DialogSpec,
|
|||
|
|
type PoolResult,
|
|||
|
|
type WriteResult,
|
|||
|
|
type Save,
|
|||
|
|
type Change,
|
|||
|
|
type Purchase,
|
|||
|
|
type EnablePreview,
|
|||
|
|
} from './model';
|
|||
|
|
|
|||
|
|
interface Props {
|
|||
|
|
client: LedgerClient;
|
|||
|
|
state: LedgerState;
|
|||
|
|
onState?: (state: LedgerState) => void;
|
|||
|
|
onRecoverOther?: () => Promise<void>;
|
|||
|
|
}
|
|||
|
|
type DialogData =
|
|||
|
|
| { kind: 'edit' | 'delete'; p: Purchase }
|
|||
|
|
| { kind: 'enable'; preview: EnablePreview }
|
|||
|
|
| { kind: 'review'; change: Change };
|
|||
|
|
function MaterialDialog({
|
|||
|
|
spec,
|
|||
|
|
client,
|
|||
|
|
state,
|
|||
|
|
rolling,
|
|||
|
|
save,
|
|||
|
|
close,
|
|||
|
|
refresh,
|
|||
|
|
retry,
|
|||
|
|
reload,
|
|||
|
|
}: {
|
|||
|
|
spec: DialogSpec;
|
|||
|
|
rolling: boolean;
|
|||
|
|
save: Save;
|
|||
|
|
close: () => void;
|
|||
|
|
refresh: () => void;
|
|||
|
|
retry: () => Promise<void>;
|
|||
|
|
reload: () => void;
|
|||
|
|
} & Props) {
|
|||
|
|
const ref = useRef<HTMLDialogElement>(null),
|
|||
|
|
[data, setData] = useState<DialogData | null>(null),
|
|||
|
|
[readError, setReadError] = useState(''),
|
|||
|
|
[error, setError] = useState<ApiError | null>(null);
|
|||
|
|
useEffect(() => {
|
|||
|
|
const el = ref.current!;
|
|||
|
|
el.showModal();
|
|||
|
|
return () => el.close();
|
|||
|
|
}, []);
|
|||
|
|
useEffect(() => {
|
|||
|
|
let active = true;
|
|||
|
|
async function read(): Promise<DialogData | null> {
|
|||
|
|
switch (spec.kind) {
|
|||
|
|
case 'edit':
|
|||
|
|
case 'delete':
|
|||
|
|
return {
|
|||
|
|
kind: spec.kind,
|
|||
|
|
p: validatePurchaseResponse(
|
|||
|
|
await client.api('/api/material-purchases/' + encodeURIComponent(spec.id)),
|
|||
|
|
spec.id,
|
|||
|
|
state,
|
|||
|
|
),
|
|||
|
|
};
|
|||
|
|
case 'enable':
|
|||
|
|
return {
|
|||
|
|
kind: 'enable',
|
|||
|
|
preview: validateEnable(await client.api('/api/material-pools/rolling-preview'), state),
|
|||
|
|
};
|
|||
|
|
case 'review':
|
|||
|
|
return {
|
|||
|
|
kind: 'review',
|
|||
|
|
change: validateChange(
|
|||
|
|
await client.api('/api/material-purchase-changes/' + encodeURIComponent(spec.id)),
|
|||
|
|
spec.id,
|
|||
|
|
state,
|
|||
|
|
),
|
|||
|
|
};
|
|||
|
|
case 'settlement':
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
void read()
|
|||
|
|
.then(r => {
|
|||
|
|
if (active) setData(r);
|
|||
|
|
})
|
|||
|
|
.catch(err => {
|
|||
|
|
if (active) setReadError(err.message);
|
|||
|
|
});
|
|||
|
|
return () => {
|
|||
|
|
active = false;
|
|||
|
|
};
|
|||
|
|
}, [client, spec, state]);
|
|||
|
|
const buying = spec.kind === 'edit' || spec.kind === 'delete',
|
|||
|
|
settling = spec.kind === 'settlement',
|
|||
|
|
prefix = buying ? 'material-purchase' : settling ? 'material-settlement' : 'material-rolling',
|
|||
|
|
pending = !!client.pending(),
|
|||
|
|
blocked = client.busy || pending;
|
|||
|
|
const stale =
|
|||
|
|
!!error && !settling && (pending || (buying ? error.status === 409 : [403, 404, 409].includes(error.status ?? 0)));
|
|||
|
|
const reloadButton = (
|
|||
|
|
<button
|
|||
|
|
type="button"
|
|||
|
|
className="quiet"
|
|||
|
|
data-action={
|
|||
|
|
buying
|
|||
|
|
? 'reload-material-purchase'
|
|||
|
|
: spec.kind === 'enable'
|
|||
|
|
? 'material-rolling-enable-preview'
|
|||
|
|
: 'material-change-open'
|
|||
|
|
}
|
|||
|
|
data-id={'id' in spec ? spec.id : undefined}
|
|||
|
|
data-kind={buying ? spec.kind : undefined}
|
|||
|
|
onClick={reload}
|
|||
|
|
>
|
|||
|
|
{buying ? '重新读取这笔购料' : spec.kind === 'enable' ? '重新核对' : '重新读取申请'}
|
|||
|
|
</button>
|
|||
|
|
);
|
|||
|
|
const actions = {
|
|||
|
|
blocked,
|
|||
|
|
stale,
|
|||
|
|
close,
|
|||
|
|
save,
|
|||
|
|
error: error && (
|
|||
|
|
<div className="error">
|
|||
|
|
{error.message}{' '}
|
|||
|
|
{pending ? (
|
|||
|
|
<button
|
|||
|
|
type="button"
|
|||
|
|
className="danger"
|
|||
|
|
data-action="retry"
|
|||
|
|
disabled={client.busy}
|
|||
|
|
onClick={() => void retry().catch(setError)}
|
|||
|
|
>
|
|||
|
|
恢复原操作
|
|||
|
|
</button>
|
|||
|
|
) : (
|
|||
|
|
(stale || (!buying && !settling)) && reloadButton
|
|||
|
|
)}
|
|||
|
|
</div>
|
|||
|
|
),
|
|||
|
|
submit: async (job: () => Promise<void>) => {
|
|||
|
|
if (blocked || stale) return;
|
|||
|
|
setError(null);
|
|||
|
|
try {
|
|||
|
|
await job();
|
|||
|
|
} catch (err) {
|
|||
|
|
setError(err as ApiError);
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
};
|
|||
|
|
const title = buying
|
|||
|
|
? spec.kind === 'edit'
|
|||
|
|
? '修改这笔购料'
|
|||
|
|
: '删除这笔购料'
|
|||
|
|
: spec.kind === 'settlement'
|
|||
|
|
? spec.pool.date + ' · 本批金料实际交款'
|
|||
|
|
: spec.kind === 'enable'
|
|||
|
|
? readError
|
|||
|
|
? '跨日结转预览未读到'
|
|||
|
|
: data
|
|||
|
|
? '核对并启用跨日结转'
|
|||
|
|
: '核对跨日结转'
|
|||
|
|
: data?.kind === 'review'
|
|||
|
|
? (data.change.action === 'delete' ? '删除' : '修改') + '历史购料 · ' + statuses[data.change.status]
|
|||
|
|
: readError
|
|||
|
|
? '更正申请未读到'
|
|||
|
|
: '读取购料更正申请';
|
|||
|
|
return (
|
|||
|
|
<dialog
|
|||
|
|
ref={ref}
|
|||
|
|
id={prefix + '-dialog'}
|
|||
|
|
aria-labelledby={prefix + '-title'}
|
|||
|
|
onCancel={e => {
|
|||
|
|
e.preventDefault();
|
|||
|
|
close();
|
|||
|
|
}}
|
|||
|
|
>
|
|||
|
|
<button
|
|||
|
|
type="button"
|
|||
|
|
className="dialog-close"
|
|||
|
|
data-action={
|
|||
|
|
buying ? 'close-material-purchase' : settling ? 'close-material-settlement' : 'material-rolling-close'
|
|||
|
|
}
|
|||
|
|
aria-label={
|
|||
|
|
buying
|
|||
|
|
? '关闭购料' + (spec.kind === 'delete' ? '删除' : '修改')
|
|||
|
|
: settling
|
|||
|
|
? '关闭本批料款结算'
|
|||
|
|
: '关闭材料复核'
|
|||
|
|
}
|
|||
|
|
onClick={close}
|
|||
|
|
>
|
|||
|
|
×
|
|||
|
|
</button>
|
|||
|
|
<h2 id={prefix + '-title'}>{title}</h2>
|
|||
|
|
{readError ? (
|
|||
|
|
<div className="error">
|
|||
|
|
{readError} {reloadButton}
|
|||
|
|
</div>
|
|||
|
|
) : spec.kind === 'settlement' ? (
|
|||
|
|
<Settlement pool={spec.pool} state={state} actions={actions} />
|
|||
|
|
) : data?.kind === 'edit' || data?.kind === 'delete' ? (
|
|||
|
|
<PurchaseChange
|
|||
|
|
state={state}
|
|||
|
|
purchase={data.p}
|
|||
|
|
kind={data.kind}
|
|||
|
|
rolling={rolling}
|
|||
|
|
actions={actions}
|
|||
|
|
refresh={refresh}
|
|||
|
|
/>
|
|||
|
|
) : data?.kind === 'enable' ? (
|
|||
|
|
<EnableForm p={data.preview} state={state} actions={actions} reload={reload} />
|
|||
|
|
) : data?.kind === 'review' ? (
|
|||
|
|
<ReviewForm r={data.change} state={state} actions={actions} />
|
|||
|
|
) : (
|
|||
|
|
<p className="small muted" aria-live="polite">
|
|||
|
|
{buying
|
|||
|
|
? '正在读取这笔购料…'
|
|||
|
|
: spec.kind === 'enable'
|
|||
|
|
? '正在核对结转材料与今日订单…'
|
|||
|
|
: '正在读取原购料记录及订单影响…'}
|
|||
|
|
</p>
|
|||
|
|
)}
|
|||
|
|
</dialog>
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
const lastPool = new WeakMap<LedgerClient, { user: string; date: string }>();
|
|||
|
|
export function MaterialPoolsPage({ client, state, onState, onRecoverOther }: Props) {
|
|||
|
|
useClientSnapshot(client);
|
|||
|
|
useLocation();
|
|||
|
|
const param = searchParam('pool'),
|
|||
|
|
[openDate, selectDate] = useState(
|
|||
|
|
() => param || (lastPool.get(client)?.user === state.user.id ? lastPool.get(client)!.date : ''),
|
|||
|
|
),
|
|||
|
|
[data, setData] = useState<PoolResult | null>(null),
|
|||
|
|
[seq, setSeq] = useState(0),
|
|||
|
|
[dialog, setDialog] = useState<DialogSpec | null>(null),
|
|||
|
|
[readError, setReadError] = useState(''),
|
|||
|
|
[notice, setNotice] = useState(''),
|
|||
|
|
[saved, setSaved] = useState(''),
|
|||
|
|
[savedFailure, setSavedFailure] = useState(false),
|
|||
|
|
[draftGeneration, resetDraft] = useState(0),
|
|||
|
|
[dialogKey, setDialogKey] = useState(0);
|
|||
|
|
function setOpenDate(date: string) {
|
|||
|
|
lastPool.set(client, { user: state.user.id, date });
|
|||
|
|
selectDate(date);
|
|||
|
|
}
|
|||
|
|
const readId = useRef(0),
|
|||
|
|
mounted = useRef(false);
|
|||
|
|
const read = useCallback(async () => {
|
|||
|
|
const id = ++readId.current;
|
|||
|
|
try {
|
|||
|
|
const result = validatePools(await client.api<PoolResult>('/api/material-pools'));
|
|||
|
|
if (mounted.current && id === readId.current) {
|
|||
|
|
setData(result);
|
|||
|
|
setReadError('');
|
|||
|
|
setSeq(n => n + 1);
|
|||
|
|
setSaved('');
|
|||
|
|
setSavedFailure(false);
|
|||
|
|
}
|
|||
|
|
} catch (err) {
|
|||
|
|
if (mounted.current && id === readId.current) setReadError((err as Error).message);
|
|||
|
|
throw err;
|
|||
|
|
}
|
|||
|
|
}, [client]);
|
|||
|
|
useEffect(() => {
|
|||
|
|
mounted.current = true;
|
|||
|
|
void read().catch(() => {
|
|||
|
|
/* 页面呈现读取错误和重试入口。 */
|
|||
|
|
});
|
|||
|
|
return () => {
|
|||
|
|
mounted.current = false;
|
|||
|
|
};
|
|||
|
|
}, [read, state.user.id]);
|
|||
|
|
const [previousParam, setPreviousParam] = useState(param);
|
|||
|
|
if (param !== previousParam) {
|
|||
|
|
setPreviousParam(param);
|
|||
|
|
selectDate(param);
|
|||
|
|
setDialog(null);
|
|||
|
|
}
|
|||
|
|
async function refresh() {
|
|||
|
|
setDialog(null);
|
|||
|
|
setReadError('');
|
|||
|
|
try {
|
|||
|
|
await read();
|
|||
|
|
} catch {
|
|||
|
|
/* 已在 read 中呈现错误。 */
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
async function confirmed(url: string, input: Record<string, unknown>, result: WriteResult) {
|
|||
|
|
const create = url === '/api/material-purchases',
|
|||
|
|
action = url.split('/').at(-1),
|
|||
|
|
settlement = action === 'settlement';
|
|||
|
|
if (create) localStorage.removeItem(draftKey(state));
|
|||
|
|
if (!mounted.current) return;
|
|||
|
|
if (create) resetDraft(n => n + 1);
|
|||
|
|
if (create || action === 'edit' || action === 'delete')
|
|||
|
|
setOpenDate(result.poolId || result.previousPoolId || String(input.date || ''));
|
|||
|
|
if (settlement) setOpenDate(decodeURIComponent(url.split('/')[3]));
|
|||
|
|
const title = create
|
|||
|
|
? '购料已保存'
|
|||
|
|
: settlement
|
|||
|
|
? '本批料款交款已保存'
|
|||
|
|
: {
|
|||
|
|
edit: '购料修改已保存',
|
|||
|
|
delete: '购料已删除',
|
|||
|
|
change: '购料更正申请已提交',
|
|||
|
|
'rolling-enable': '跨日结转已启用',
|
|||
|
|
approve: '购料更正已批准',
|
|||
|
|
reject: '购料更正申请已驳回',
|
|||
|
|
}[action || ''] || '原操作已确认';
|
|||
|
|
setSaved(title);
|
|||
|
|
setNotice('');
|
|||
|
|
setSavedFailure(false);
|
|||
|
|
setDialog(null);
|
|||
|
|
setReadError('');
|
|||
|
|
++readId.current;
|
|||
|
|
try {
|
|||
|
|
const next = await client.api<LedgerState>('/api/state');
|
|||
|
|
if (!mounted.current) return;
|
|||
|
|
onState?.(next);
|
|||
|
|
await read();
|
|||
|
|
if (mounted.current) setNotice(title);
|
|||
|
|
} catch {
|
|||
|
|
if (mounted.current) setSavedFailure(true);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
const save: Save = async (url, input) => {
|
|||
|
|
const result = await client.mutate<WriteResult>(url, input);
|
|||
|
|
await confirmed(url, input, result);
|
|||
|
|
};
|
|||
|
|
async function retry() {
|
|||
|
|
const job = client.pending();
|
|||
|
|
if (
|
|||
|
|
job &&
|
|||
|
|
!/^\/api\/(?:material-purchases|material-pools|material-purchase-changes)(?:\/|$)/.test(job.url) &&
|
|||
|
|
onRecoverOther
|
|||
|
|
)
|
|||
|
|
return onRecoverOther();
|
|||
|
|
const recovered = await client.retryPending<WriteResult>();
|
|||
|
|
if (recovered) await confirmed(recovered.job.url, recovered.job.data as Record<string, unknown>, recovered.result);
|
|||
|
|
}
|
|||
|
|
function open(spec: DialogSpec) {
|
|||
|
|
if (client.pending() || client.busy) {
|
|||
|
|
setNotice('请先恢复上次未确认的操作,再核对材料记录。');
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
setNotice('');
|
|||
|
|
setDialogKey(n => n + 1);
|
|||
|
|
setDialog(spec);
|
|||
|
|
}
|
|||
|
|
const rolling = !!data?.rolling?.enabled,
|
|||
|
|
pending = !!client.pending();
|
|||
|
|
return (
|
|||
|
|
<div className="business-pages">
|
|||
|
|
<div id="main">
|
|||
|
|
{pending && !client.busy && (
|
|||
|
|
<div className="note" role="alert">
|
|||
|
|
上次提交结果尚未确认,请先恢复原操作。{' '}
|
|||
|
|
<button
|
|||
|
|
type="button"
|
|||
|
|
data-action="retry"
|
|||
|
|
className="danger"
|
|||
|
|
onClick={() => void retry().catch(e => setNotice(e.message))}
|
|||
|
|
>
|
|||
|
|
恢复原操作
|
|||
|
|
</button>
|
|||
|
|
</div>
|
|||
|
|
)}
|
|||
|
|
{saved ? (
|
|||
|
|
<>
|
|||
|
|
<div className="page-heading">
|
|||
|
|
<h1>{saved}</h1>
|
|||
|
|
</div>
|
|||
|
|
<section className="panel">
|
|||
|
|
<p id="material-saved-status" aria-live="polite">
|
|||
|
|
{savedFailure
|
|||
|
|
? saved + ',最新材料记录暂未读到。请重新读取查看,不要重复提交。'
|
|||
|
|
: '正在读取最新材料记录…'}
|
|||
|
|
</p>
|
|||
|
|
<div className="actions">
|
|||
|
|
<button
|
|||
|
|
type="button"
|
|||
|
|
data-action="refresh-material-pools"
|
|||
|
|
className="primary"
|
|||
|
|
onClick={() => void refresh()}
|
|||
|
|
>
|
|||
|
|
重新读取每日料价
|
|||
|
|
</button>
|
|||
|
|
{saved === '购料已保存' && (
|
|||
|
|
<button
|
|||
|
|
type="button"
|
|||
|
|
data-action="new-material-purchase"
|
|||
|
|
onClick={() => {
|
|||
|
|
setSaved('');
|
|||
|
|
void refresh();
|
|||
|
|
}}
|
|||
|
|
>
|
|||
|
|
继续登记另一笔购料
|
|||
|
|
</button>
|
|||
|
|
)}
|
|||
|
|
</div>
|
|||
|
|
</section>
|
|||
|
|
</>
|
|||
|
|
) : (
|
|||
|
|
<>
|
|||
|
|
<div className="page-heading material-heading">
|
|||
|
|
<div>
|
|||
|
|
<div className="eyebrow">DAILY MATERIAL</div>
|
|||
|
|
<h1>每日购料</h1>
|
|||
|
|
<p>购料付款当天记支出,余料结转;订单按实际耗用自动计料费。</p>
|
|||
|
|
</div>
|
|||
|
|
<button type="button" data-action="refresh-material-pools" onClick={() => void refresh()}>
|
|||
|
|
刷新
|
|||
|
|
</button>
|
|||
|
|
</div>
|
|||
|
|
{notice && (
|
|||
|
|
<div role="status" className="note">
|
|||
|
|
{notice}
|
|||
|
|
</div>
|
|||
|
|
)}
|
|||
|
|
{rolling && <RollingSummary pool={data!.current!} cutover={data!.rolling!.cutover} />}
|
|||
|
|
<PurchaseForm
|
|||
|
|
key={draftGeneration}
|
|||
|
|
state={state}
|
|||
|
|
rolling={rolling}
|
|||
|
|
save={save}
|
|||
|
|
blocked={client.busy || pending}
|
|||
|
|
/>
|
|||
|
|
<div id="material-pools-content" aria-live="polite">
|
|||
|
|
{readError ? (
|
|||
|
|
<div className="error">
|
|||
|
|
{readError}{' '}
|
|||
|
|
<button type="button" data-action="refresh-material-pools" onClick={() => void refresh()}>
|
|||
|
|
重新读取
|
|||
|
|
</button>
|
|||
|
|
</div>
|
|||
|
|
) : data ? (
|
|||
|
|
<>
|
|||
|
|
<PoolList state={state} data={data} openDate={openDate} toggle={setOpenDate} openDialog={open} />
|
|||
|
|
{rolling && (
|
|||
|
|
<ChangePanel
|
|||
|
|
state={state}
|
|||
|
|
client={client}
|
|||
|
|
refresh={seq}
|
|||
|
|
open={id => open({ kind: 'review', id })}
|
|||
|
|
/>
|
|||
|
|
)}
|
|||
|
|
</>
|
|||
|
|
) : (
|
|||
|
|
'正在读取每日料价…'
|
|||
|
|
)}
|
|||
|
|
</div>
|
|||
|
|
</>
|
|||
|
|
)}
|
|||
|
|
{dialog && (
|
|||
|
|
<MaterialDialog
|
|||
|
|
key={dialogKey}
|
|||
|
|
spec={dialog}
|
|||
|
|
client={client}
|
|||
|
|
state={state}
|
|||
|
|
rolling={rolling}
|
|||
|
|
save={save}
|
|||
|
|
close={() => setDialog(null)}
|
|||
|
|
refresh={() => void refresh()}
|
|||
|
|
reload={() => setDialogKey(n => n + 1)}
|
|||
|
|
retry={retry}
|
|||
|
|
/>
|
|||
|
|
)}
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
);
|
|||
|
|
}
|