WorkOrderDetailPage.tsx
7.56 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Work-order detail screen — read-only header + start/complete
// action verbs that drive the v2 state machine. The shop-floor
// dashboard at /shop-floor handles the per-operation walk for v3
// routing-equipped orders. v1 SPA keeps this screen simple: start a
// DRAFT, complete an IN_PROGRESS into a warehouse.
import { useCallback, useEffect, useState } from 'react'
import { useNavigate, useParams } from 'react-router-dom'
import { inventory, production } from '@/api/client'
import type { Location, WorkOrder } from '@/types/api'
import { PageHeader } from '@/components/PageHeader'
import { Loading } from '@/components/Loading'
import { ErrorBox } from '@/components/ErrorBox'
import { StatusBadge } from '@/components/StatusBadge'
export function WorkOrderDetailPage() {
const { id = '' } = useParams<{ id: string }>()
const navigate = useNavigate()
const [order, setOrder] = useState<WorkOrder | null>(null)
const [locations, setLocations] = useState<Location[]>([])
const [outputLocation, setOutputLocation] = useState<string>('')
const [loading, setLoading] = useState(true)
const [acting, setActing] = useState(false)
const [error, setError] = useState<Error | null>(null)
const [actionMessage, setActionMessage] = useState<string | null>(null)
const refresh = useCallback(async () => {
const o = await production.getWorkOrder(id)
setOrder(o)
}, [id])
useEffect(() => {
let active = true
setLoading(true)
Promise.all([production.getWorkOrder(id), inventory.listLocations()])
.then(([o, locs]: [WorkOrder, Location[]]) => {
if (!active) return
setOrder(o)
setLocations(locs.filter((l) => l.active))
const firstWarehouse = locs.find((l) => l.active && l.type === 'WAREHOUSE')
setOutputLocation(firstWarehouse?.code ?? locs[0]?.code ?? '')
})
.catch((e: unknown) => {
if (active) setError(e instanceof Error ? e : new Error(String(e)))
})
.finally(() => active && setLoading(false))
return () => {
active = false
}
}, [id])
if (loading) return <Loading />
if (error) return <ErrorBox error={error} />
if (!order) return <ErrorBox error="Work order not found" />
const onStart = async () => {
setActing(true)
setError(null)
setActionMessage(null)
try {
await production.startWorkOrder(order.id)
await refresh()
setActionMessage('Started. Operations can now be walked from the Shop Floor screen.')
} catch (e: unknown) {
setError(e instanceof Error ? e : new Error(String(e)))
} finally {
setActing(false)
}
}
const onComplete = async () => {
if (!outputLocation) {
setError(new Error('Pick an output location first.'))
return
}
setActing(true)
setError(null)
setActionMessage(null)
try {
await production.completeWorkOrder(order.id, outputLocation)
await refresh()
setActionMessage(
`Completed. Materials issued, finished goods credited to ${outputLocation}.`,
)
} catch (e: unknown) {
setError(e instanceof Error ? e : new Error(String(e)))
} finally {
setActing(false)
}
}
const canStart = order.status === 'DRAFT'
const canComplete = order.status === 'IN_PROGRESS'
return (
<div>
<PageHeader
title={`Work Order ${order.code}`}
subtitle={`Output: ${order.outputItemCode} × ${order.outputQuantity}${
order.sourceSalesOrderCode ? ' · from SO ' + order.sourceSalesOrderCode : ''
}`}
actions={
<button className="btn-secondary" onClick={() => navigate('/work-orders')}>
← Back
</button>
}
/>
<div className="card mb-6 p-5">
<div className="flex items-center gap-3">
<span className="text-sm text-slate-500">Status:</span>
<StatusBadge status={order.status} />
</div>
{actionMessage && (
<div className="mt-4 rounded-md border border-emerald-200 bg-emerald-50 px-4 py-2 text-sm text-emerald-800">
{actionMessage}
</div>
)}
</div>
<div className="card mb-6 p-5">
<h2 className="mb-3 text-base font-semibold text-slate-800">Actions</h2>
<div className="flex flex-wrap items-center gap-3">
<button className="btn-primary" disabled={!canStart || acting} onClick={onStart}>
Start
</button>
<div className="flex items-center gap-2">
<select
className="rounded-md border border-slate-300 px-2 py-1 text-sm shadow-sm focus:border-brand-500 focus:ring-brand-500"
value={outputLocation}
onChange={(e) => setOutputLocation(e.target.value)}
disabled={!canComplete || acting}
>
{locations.map((l) => (
<option key={l.id} value={l.code}>
{l.code} — {l.name}
</option>
))}
</select>
<button className="btn-primary" disabled={!canComplete || acting} onClick={onComplete}>
Complete
</button>
</div>
</div>
</div>
<div className="grid gap-6 md:grid-cols-2">
<div className="card">
<div className="border-b border-slate-200 px-5 py-3">
<h2 className="text-base font-semibold text-slate-800">BOM inputs</h2>
</div>
{order.inputs.length === 0 ? (
<div className="p-5 text-sm text-slate-400">No BOM lines.</div>
) : (
<table className="table-base">
<thead className="bg-slate-50">
<tr>
<th>#</th>
<th>Item</th>
<th>Qty / unit</th>
<th>Source loc</th>
</tr>
</thead>
<tbody className="divide-y divide-slate-100">
{order.inputs.map((i) => (
<tr key={i.id}>
<td>{i.lineNo}</td>
<td className="font-mono">{i.itemCode}</td>
<td className="font-mono tabular-nums">{String(i.quantityPerUnit)}</td>
<td className="font-mono">{i.sourceLocationCode}</td>
</tr>
))}
</tbody>
</table>
)}
</div>
<div className="card">
<div className="border-b border-slate-200 px-5 py-3">
<h2 className="text-base font-semibold text-slate-800">Routing operations</h2>
</div>
{order.operations.length === 0 ? (
<div className="p-5 text-sm text-slate-400">No routing.</div>
) : (
<table className="table-base">
<thead className="bg-slate-50">
<tr>
<th>#</th>
<th>Operation</th>
<th>Work center</th>
<th>Std min</th>
<th>Status</th>
</tr>
</thead>
<tbody className="divide-y divide-slate-100">
{order.operations.map((o) => (
<tr key={o.id}>
<td>{o.lineNo}</td>
<td className="font-mono">{o.operationCode}</td>
<td className="font-mono">{o.workCenter}</td>
<td className="font-mono tabular-nums">{String(o.standardMinutes)}</td>
<td>
<StatusBadge status={o.status} />
</td>
</tr>
))}
</tbody>
</table>
)}
</div>
</div>
</div>
)
}