SalesOrderDetailPage.tsx
11.1 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Sales-order detail screen.
//
// **The headline demo flow.** Confirm a DRAFT, ship a CONFIRMED,
// or cancel either. Each action updates the order in place,
// reloads the journal entry list to show the AR row appearing
// (POSTED → SETTLED), and reloads stock movements to show the
// SALES_SHIPMENT ledger entry appearing.
//
// **Why ship asks for a location.** The framework requires every
// shipment to name the warehouse the goods came from — the
// inventory ledger tags the row with that location and the audit
// trail must always answer "which warehouse shipped this". The UI
// proposes the first WAREHOUSE-typed location it finds; an
// operator can pick another from the dropdown.
import { useCallback, useEffect, useState } from 'react'
import { Link, useNavigate, useParams } from 'react-router-dom'
import { finance, inventory, salesOrders } from '@/api/client'
import type { JournalEntry, Location, SalesOrder, StockMovement } 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 SalesOrderDetailPage() {
const { id = '' } = useParams<{ id: string }>()
const navigate = useNavigate()
const [order, setOrder] = useState<SalesOrder | null>(null)
const [locations, setLocations] = useState<Location[]>([])
const [shippingLocation, setShippingLocation] = useState<string>('')
const [movements, setMovements] = useState<StockMovement[]>([])
const [journalEntries, setJournalEntries] = useState<JournalEntry[]>([])
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 reloadSideEffects = useCallback(async (orderCode: string) => {
const [ms, js] = await Promise.all([
inventory.listMovements(),
finance.listJournalEntries(),
])
setMovements(ms.filter((m) => m.reference?.includes(orderCode) ?? false))
setJournalEntries(js.filter((j) => j.orderCode === orderCode))
}, [])
useEffect(() => {
let active = true
setLoading(true)
Promise.all([salesOrders.get(id), inventory.listLocations()])
.then(async ([o, locs]: [SalesOrder, Location[]]) => {
if (!active) return
setOrder(o)
setLocations(locs.filter((l) => l.active))
const firstWarehouse = locs.find((l) => l.active && l.type === 'WAREHOUSE')
setShippingLocation(firstWarehouse?.code ?? locs[0]?.code ?? '')
await reloadSideEffects(o.code)
})
.catch((e: unknown) => {
if (active) setError(e instanceof Error ? e : new Error(String(e)))
})
.finally(() => active && setLoading(false))
return () => {
active = false
}
}, [id, reloadSideEffects])
if (loading) return <Loading />
if (error) return <ErrorBox error={error} />
if (!order) return <ErrorBox error="Sales order not found" />
const onConfirm = async () => {
setActing(true)
setError(null)
setActionMessage(null)
try {
const updated = await salesOrders.confirm(order.id)
setOrder(updated)
await reloadSideEffects(updated.code)
setActionMessage('Confirmed. pbc-finance has posted an AR journal entry.')
} catch (e: unknown) {
setError(e instanceof Error ? e : new Error(String(e)))
} finally {
setActing(false)
}
}
const onShip = async () => {
if (!shippingLocation) {
setError(new Error('Pick a shipping location first.'))
return
}
setActing(true)
setError(null)
setActionMessage(null)
try {
const updated = await salesOrders.ship(order.id, shippingLocation)
setOrder(updated)
await reloadSideEffects(updated.code)
setActionMessage(
`Shipped from ${shippingLocation}. Stock debited, journal entry settled.`,
)
} catch (e: unknown) {
setError(e instanceof Error ? e : new Error(String(e)))
} finally {
setActing(false)
}
}
const onCancel = async () => {
setActing(true)
setError(null)
setActionMessage(null)
try {
const updated = await salesOrders.cancel(order.id)
setOrder(updated)
await reloadSideEffects(updated.code)
setActionMessage('Cancelled. pbc-finance has reversed any open journal entry.')
} catch (e: unknown) {
setError(e instanceof Error ? e : new Error(String(e)))
} finally {
setActing(false)
}
}
const canConfirm = order.status === 'DRAFT'
const canShip = order.status === 'CONFIRMED'
const canCancel = order.status === 'DRAFT' || order.status === 'CONFIRMED'
return (
<div>
<PageHeader
title={`Sales Order ${order.code}`}
subtitle={`Customer ${order.partnerCode} · ${order.orderDate} · ${order.currencyCode}`}
actions={
<button className="btn-secondary" onClick={() => navigate('/sales-orders')}>
← Back
</button>
}
/>
<div className="card mb-6 p-5">
<div className="flex flex-wrap items-center justify-between gap-4">
<div className="flex items-center gap-3">
<span className="text-sm text-slate-500">Status:</span>
<StatusBadge status={order.status} />
</div>
<div className="text-right">
<div className="text-xs uppercase text-slate-400">Total</div>
<div className="font-mono text-xl font-semibold">
{Number(order.totalAmount).toLocaleString(undefined, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
})}{' '}
{order.currencyCode}
</div>
</div>
</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={!canConfirm || acting} onClick={onConfirm}>
Confirm
</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={shippingLocation}
onChange={(e) => setShippingLocation(e.target.value)}
disabled={!canShip || acting}
>
{locations.map((l) => (
<option key={l.id} value={l.code}>
{l.code} — {l.name}
</option>
))}
</select>
<button className="btn-primary" disabled={!canShip || acting} onClick={onShip}>
Ship
</button>
</div>
<button className="btn-danger" disabled={!canCancel || acting} onClick={onCancel}>
Cancel
</button>
</div>
</div>
<div className="card mb-6">
<div className="border-b border-slate-200 px-5 py-3">
<h2 className="text-base font-semibold text-slate-800">Lines</h2>
</div>
<table className="table-base">
<thead className="bg-slate-50">
<tr>
<th>#</th>
<th>Item</th>
<th>Qty</th>
<th>Unit price</th>
<th>Line total</th>
</tr>
</thead>
<tbody className="divide-y divide-slate-100">
{order.lines.map((l) => (
<tr key={l.id}>
<td>{l.lineNo}</td>
<td className="font-mono">{l.itemCode}</td>
<td className="font-mono tabular-nums">{String(l.quantity)}</td>
<td className="font-mono tabular-nums">
{Number(l.unitPrice).toLocaleString(undefined, {
minimumFractionDigits: 2,
})}{' '}
{l.currencyCode}
</td>
<td className="font-mono tabular-nums">
{Number(l.lineTotal).toLocaleString(undefined, {
minimumFractionDigits: 2,
})}
</td>
</tr>
))}
</tbody>
</table>
</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">Inventory movements</h2>
</div>
{movements.length === 0 ? (
<div className="p-5 text-sm text-slate-400">No movements yet.</div>
) : (
<table className="table-base">
<thead className="bg-slate-50">
<tr>
<th>Item</th>
<th>Δ</th>
<th>Reason</th>
<th>Reference</th>
</tr>
</thead>
<tbody className="divide-y divide-slate-100">
{movements.map((m) => (
<tr key={m.id}>
<td className="font-mono">{m.itemCode}</td>
<td className="font-mono tabular-nums text-rose-600">{String(m.delta)}</td>
<td>{m.reason}</td>
<td className="font-mono text-xs text-slate-500">{m.reference ?? '—'}</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">Journal entries</h2>
</div>
{journalEntries.length === 0 ? (
<div className="p-5 text-sm text-slate-400">No entries yet.</div>
) : (
<table className="table-base">
<thead className="bg-slate-50">
<tr>
<th>Code</th>
<th>Type</th>
<th>Status</th>
<th>Amount</th>
</tr>
</thead>
<tbody className="divide-y divide-slate-100">
{journalEntries.map((j) => (
<tr key={j.id}>
<td className="font-mono text-xs">{j.code}</td>
<td>{j.type}</td>
<td>
<StatusBadge status={j.status} />
</td>
<td className="font-mono tabular-nums">
{Number(j.amount).toLocaleString(undefined, { minimumFractionDigits: 2 })}{' '}
{j.currencyCode}
</td>
</tr>
))}
</tbody>
</table>
)}
<div className="border-t border-slate-200 px-5 py-3 text-xs text-slate-400">
<Link to="/journal-entries" className="hover:underline">View all journal entries →</Link>
</div>
</div>
</div>
</div>
)
}