CreateWorkOrderPage.tsx
9.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { useEffect, useState, type FormEvent } from 'react'
import { useNavigate } from 'react-router-dom'
import { catalog, inventory, production } from '@/api/client'
import type { Item, Location } from '@/types/api'
import { PageHeader } from '@/components/PageHeader'
import { ErrorBox } from '@/components/ErrorBox'
import { DynamicExtFields } from '@/components/DynamicExtFields'
import { useT } from '@/i18n/LocaleContext'
interface BomLine { itemCode: string; quantityPerUnit: string; sourceLocationCode: string }
interface OpLine { operationCode: string; workCenter: string; standardMinutes: string }
export function CreateWorkOrderPage() {
const navigate = useNavigate()
const t = useT()
const [code, setCode] = useState('')
const [outputItemCode, setOutputItemCode] = useState('')
const [outputQuantity, setOutputQuantity] = useState('')
const [dueDate, setDueDate] = useState('')
const [bom, setBom] = useState<BomLine[]>([])
const [ops, setOps] = useState<OpLine[]>([])
const [items, setItems] = useState<Item[]>([])
const [locations, setLocations] = useState<Location[]>([])
const [ext, setExt] = useState<Record<string, unknown>>({})
const [submitting, setSubmitting] = useState(false)
const [error, setError] = useState<Error | null>(null)
useEffect(() => {
Promise.all([catalog.listItems(), inventory.listLocations()]).then(([i, l]) => {
setItems(i)
setLocations(l.filter((x) => x.active))
})
}, [])
const addBom = () =>
setBom([...bom, { itemCode: '', quantityPerUnit: '1', sourceLocationCode: locations[0]?.code ?? '' }])
const removeBom = (i: number) => setBom(bom.filter((_, idx) => idx !== i))
const updateBom = (i: number, f: keyof BomLine, v: string) => {
const next = [...bom]; next[i] = { ...next[i], [f]: v }; setBom(next)
}
const addOp = () =>
setOps([...ops, { operationCode: '', workCenter: '', standardMinutes: '30' }])
const removeOp = (i: number) => setOps(ops.filter((_, idx) => idx !== i))
const updateOp = (i: number, f: keyof OpLine, v: string) => {
const next = [...ops]; next[i] = { ...next[i], [f]: v }; setOps(next)
}
const onSubmit = async (e: FormEvent) => {
e.preventDefault()
setError(null)
setSubmitting(true)
try {
const extPayload = Object.keys(ext).length > 0 ? ext : undefined
const created = await production.createWorkOrder({
code, outputItemCode,
outputQuantity: Number(outputQuantity),
dueDate: dueDate || null,
inputs: bom.map((b, i) => ({
lineNo: i + 1, itemCode: b.itemCode,
quantityPerUnit: Number(b.quantityPerUnit),
sourceLocationCode: b.sourceLocationCode,
})),
operations: ops.map((o, i) => ({
lineNo: i + 1, operationCode: o.operationCode,
workCenter: o.workCenter,
standardMinutes: Number(o.standardMinutes),
})),
...(extPayload ? { ext: extPayload } : {}),
})
navigate(`/work-orders/${created.id}`)
} catch (err: unknown) {
setError(err instanceof Error ? err : new Error(String(err)))
} finally {
setSubmitting(false)
}
}
return (
<div>
<PageHeader
title={t('page.createWorkOrder.title')}
subtitle={t('page.createWorkOrder.subtitle')}
actions={<button className="btn-secondary" onClick={() => navigate('/work-orders')}>{t('action.cancel')}</button>}
/>
<form onSubmit={onSubmit} className="card p-6 space-y-5 max-w-3xl">
<div className="grid grid-cols-1 gap-4 sm:grid-cols-3">
<div>
<label className="block text-sm font-medium text-slate-700">{t('label.woCode')}</label>
<input type="text" required value={code} onChange={(e) => setCode(e.target.value)}
placeholder="WO-PRINT-0002" className="mt-1 w-full rounded-md border border-slate-300 px-3 py-2 text-sm" />
</div>
<div>
<label className="block text-sm font-medium text-slate-700">{t('label.outputItem')}</label>
<select required value={outputItemCode} onChange={(e) => setOutputItemCode(e.target.value)}
className="mt-1 w-full rounded-md border border-slate-300 px-3 py-2 text-sm">
<option value="">{t('action.selectItem')}</option>
{items.map((it) => <option key={it.id} value={it.code}>{it.code} — {it.name}</option>)}
</select>
</div>
<div>
<label className="block text-sm font-medium text-slate-700">{t('label.outputQty')}</label>
<input type="number" required min="1" step="1" value={outputQuantity}
onChange={(e) => setOutputQuantity(e.target.value)}
className="mt-1 w-full rounded-md border border-slate-300 px-3 py-2 text-sm text-right" />
</div>
</div>
<div>
<label className="block text-sm font-medium text-slate-700">{t('label.dueDate')}</label>
<input type="date" value={dueDate} onChange={(e) => setDueDate(e.target.value)}
className="mt-1 max-w-xs rounded-md border border-slate-300 px-3 py-2 text-sm" />
</div>
{/* BOM inputs */}
<div>
<div className="flex items-center justify-between mb-2">
<label className="text-sm font-medium text-slate-700">{t('label.bomInputsDesc')}</label>
<button type="button" className="btn-secondary text-xs" onClick={addBom}>{t('action.addInput')}</button>
</div>
{bom.length === 0 && <p className="text-xs text-slate-400">{t('label.noBomHint')}</p>}
<div className="space-y-2">
{bom.map((b, idx) => (
<div key={idx} className="flex items-center gap-2">
<span className="w-6 text-xs text-slate-400 text-right">{idx + 1}</span>
<select value={b.itemCode} onChange={(e) => updateBom(idx, 'itemCode', e.target.value)}
className="flex-1 rounded-md border border-slate-300 px-2 py-1.5 text-sm">
<option value="">{t('label.item')}...</option>
{items.map((it) => <option key={it.id} value={it.code}>{it.code}</option>)}
</select>
<input type="number" min="0.01" step="0.01" placeholder={t('label.qtyPerUnit')} value={b.quantityPerUnit}
onChange={(e) => updateBom(idx, 'quantityPerUnit', e.target.value)}
className="w-24 rounded-md border border-slate-300 px-2 py-1.5 text-sm text-right" />
<select value={b.sourceLocationCode} onChange={(e) => updateBom(idx, 'sourceLocationCode', e.target.value)}
className="w-32 rounded-md border border-slate-300 px-2 py-1.5 text-sm">
{locations.map((l) => <option key={l.id} value={l.code}>{l.code}</option>)}
</select>
<button type="button" className="text-slate-400 hover:text-rose-500" onClick={() => removeBom(idx)}>×</button>
</div>
))}
</div>
</div>
{/* Routing operations */}
<div>
<div className="flex items-center justify-between mb-2">
<label className="text-sm font-medium text-slate-700">{t('label.routingOpsDesc')}</label>
<button type="button" className="btn-secondary text-xs" onClick={addOp}>{t('action.addOperation')}</button>
</div>
{ops.length === 0 && <p className="text-xs text-slate-400">{t('label.noRoutingHint')}</p>}
<div className="space-y-2">
{ops.map((o, idx) => (
<div key={idx} className="flex items-center gap-2">
<span className="w-6 text-xs text-slate-400 text-right">{idx + 1}</span>
<input type="text" required placeholder={t('label.operation')} value={o.operationCode}
onChange={(e) => updateOp(idx, 'operationCode', e.target.value)}
className="w-28 rounded-md border border-slate-300 px-2 py-1.5 text-sm" />
<input type="text" required placeholder={t('label.workCenter')} value={o.workCenter}
onChange={(e) => updateOp(idx, 'workCenter', e.target.value)}
className="flex-1 rounded-md border border-slate-300 px-2 py-1.5 text-sm" />
<input type="number" min="1" step="1" placeholder={t('label.stdMin')} value={o.standardMinutes}
onChange={(e) => updateOp(idx, 'standardMinutes', e.target.value)}
className="w-20 rounded-md border border-slate-300 px-2 py-1.5 text-sm text-right" />
<button type="button" className="text-slate-400 hover:text-rose-500" onClick={() => removeOp(idx)}>×</button>
</div>
))}
</div>
</div>
<DynamicExtFields
entityName="WorkOrder"
values={ext}
onChange={(k, v) => setExt((prev) => ({ ...prev, [k]: v }))}
/>
{error && <ErrorBox error={error} />}
<button type="submit" className="btn-primary" disabled={submitting}>
{submitting ? t('action.creating') : t('page.createWorkOrder.submit')}
</button>
</form>
</div>
)
}