116 lines
6.7 KiB
TypeScript
116 lines
6.7 KiB
TypeScript
import { useCallback, useEffect, useState } from 'react';
|
|
import { RefreshCw, Send, Save } from 'lucide-react';
|
|
import { useT } from '../../i18n';
|
|
import { notify } from '../../utils/notify';
|
|
import {
|
|
listProcessingRawPurchasesApi,
|
|
submitProcessingRawPurchaseApi,
|
|
updateProcessingRawPurchaseApi,
|
|
type ProcessingRawPurchaseRow,
|
|
} from '../../utils/workflowApi';
|
|
|
|
const STATUS_LABEL: Record<number, string> = { 10: '待采购', 20: '已提交待入库', 30: '已入库', 40: '已取消' };
|
|
|
|
export function ProcessingRawMaterialPanel({ refreshToken = 0 }: { refreshToken?: number }) {
|
|
const t = useT();
|
|
const [rows, setRows] = useState<ProcessingRawPurchaseRow[]>([]);
|
|
const [loading, setLoading] = useState(false);
|
|
const [drafts, setDrafts] = useState<Record<string, { qty: string; cost: string }>>({});
|
|
const [savingId, setSavingId] = useState<string | null>(null);
|
|
const [submittingId, setSubmittingId] = useState<string | null>(null);
|
|
|
|
const load = useCallback(async () => {
|
|
setLoading(true);
|
|
try {
|
|
const result = await listProcessingRawPurchasesApi({ statusGroup: 'all' });
|
|
const nextRows = result.data || [];
|
|
setRows(nextRows);
|
|
setDrafts((current) => {
|
|
const next: Record<string, { qty: string; cost: string }> = {};
|
|
for (const row of nextRows) next[row.id] = current[row.id] || { qty: String(row.planned_qty ?? ''), cost: String(row.unit_cost ?? '') };
|
|
return next;
|
|
});
|
|
} catch (error: any) {
|
|
notify.error(error?.message || t('purchase.rawMaterial.loadFail'));
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, [t]);
|
|
|
|
useEffect(() => { void load(); }, [load, refreshToken]);
|
|
|
|
const save = async (row: ProcessingRawPurchaseRow) => {
|
|
const draft = drafts[row.id] || { qty: String(row.planned_qty), cost: String(row.unit_cost) };
|
|
const qty = Number(draft.qty);
|
|
const cost = draft.cost === '' ? undefined : Number(draft.cost);
|
|
if (!(qty > 0)) { notify.error(t('purchase.rawMaterial.qtyRequired')); return; }
|
|
setSavingId(row.id);
|
|
try {
|
|
await updateProcessingRawPurchaseApi(row.id, { planned_qty: qty, ...(cost !== undefined ? { unit_cost: cost } : {}) });
|
|
notify.success(t('purchase.rawMaterial.saved'));
|
|
await load();
|
|
} catch (error: any) {
|
|
notify.error(error?.message || t('purchase.rawMaterial.saveFail'));
|
|
} finally { setSavingId(null); }
|
|
};
|
|
|
|
const submit = async (row: ProcessingRawPurchaseRow) => {
|
|
setSubmittingId(row.id);
|
|
try {
|
|
await submitProcessingRawPurchaseApi(row.id, { actual_qty: Number(drafts[row.id]?.qty ?? row.planned_qty), unit_cost: Number(drafts[row.id]?.cost ?? row.unit_cost) });
|
|
notify.success(t('purchase.rawMaterial.submitted'));
|
|
await load();
|
|
} catch (error: any) {
|
|
notify.error(error?.message || t('purchase.rawMaterial.submitFail'));
|
|
} finally { setSubmittingId(null); }
|
|
};
|
|
|
|
return (
|
|
<div className="app-surface app-border rounded-xl border p-4">
|
|
<div className="mb-3 flex items-center justify-between gap-2">
|
|
<div>
|
|
<h2 className="text-base font-semibold">{t('purchase.rawMaterial.title')}</h2>
|
|
<p className="mt-0.5 text-xs app-foreground-muted">{t('purchase.rawMaterial.hint')}</p>
|
|
</div>
|
|
<button type="button" onClick={() => void load()} disabled={loading} className="app-button-secondary inline-flex items-center gap-1 rounded-lg border px-3 py-1.5 text-sm disabled:opacity-50">
|
|
<RefreshCw className={`h-4 w-4 ${loading ? 'animate-spin' : ''}`} />
|
|
{t('common.refresh')}
|
|
</button>
|
|
</div>
|
|
{rows.length === 0 ? <p className="py-6 text-center text-sm app-foreground-muted">{t('purchase.rawMaterial.empty')}</p> : (
|
|
<div className="overflow-auto">
|
|
<table className="min-w-full text-sm">
|
|
<thead className="app-table-head"><tr>
|
|
<th className="px-2 py-2 text-left">{t('purchase.rawMaterial.output')}</th>
|
|
<th className="px-2 py-2 text-left">{t('purchase.rawMaterial.material')}</th>
|
|
<th className="px-2 py-2 text-right">{t('purchase.rawMaterial.qty')}</th>
|
|
<th className="px-2 py-2 text-right">{t('purchase.rawMaterial.cost')}</th>
|
|
<th className="px-2 py-2 text-center">{t('purchase.rawMaterial.status')}</th>
|
|
<th className="px-2 py-2 text-center">{t('purchase.rawMaterial.actions')}</th>
|
|
</tr></thead>
|
|
<tbody>
|
|
{rows.map((row) => {
|
|
const draft = drafts[row.id] || { qty: '', cost: '' };
|
|
const editable = Number(row.purchase_status) === 10 || Number(row.purchase_status) === 20;
|
|
return (
|
|
<tr key={row.id} className="border-t app-border">
|
|
<td className="px-2 py-2"><div className="font-medium">{row.output_name || row.output_sku}</div><div className="text-xs app-foreground-muted">{row.processing_method} · {row.task_code || '-'}</div></td>
|
|
<td className="px-2 py-2"><div>{row.material_name || row.material_sku}</div><div className="text-xs app-foreground-muted">{row.unit || ''}</div></td>
|
|
<td className="px-2 py-2 text-right">{editable ? <input type="number" min={0} step="0.001" value={draft.qty} onChange={(e) => setDrafts((prev) => ({ ...prev, [row.id]: { ...draft, qty: e.target.value } }))} className="app-input w-24 rounded border px-2 py-1 text-right" /> : row.planned_qty}</td>
|
|
<td className="px-2 py-2 text-right">{editable ? <input type="number" min={0} step="0.0001" value={draft.cost} onChange={(e) => setDrafts((prev) => ({ ...prev, [row.id]: { ...draft, cost: e.target.value } }))} className="app-input w-24 rounded border px-2 py-1 text-right" /> : row.unit_cost}</td>
|
|
<td className="px-2 py-2 text-center">{STATUS_LABEL[Number(row.purchase_status)] || row.purchase_status}</td>
|
|
<td className="px-2 py-2"><div className="flex justify-center gap-1">
|
|
{editable ? <button type="button" onClick={() => void save(row)} disabled={savingId === row.id} className="app-button-secondary inline-flex items-center gap-1 rounded border px-2 py-1 text-xs disabled:opacity-50"><Save className="h-3.5 w-3.5" />{t('common.save')}</button> : null}
|
|
{Number(row.purchase_status) === 10 ? <button type="button" onClick={() => void submit(row)} disabled={submittingId === row.id} className="app-button-primary inline-flex items-center gap-1 rounded px-2 py-1 text-xs disabled:opacity-50"><Send className="h-3.5 w-3.5" />{t('purchase.rawMaterial.submit')}</button> : null}
|
|
</div></td>
|
|
</tr>
|
|
);
|
|
})}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|