Repository navigation
Description
Summary
Cancelling the last remaining active item on an order (via the trash icon on the Orders page) leaves the order in a broken "empty but not cancelled" state that is functionally indistinguishable from the whole order being cancelled — but orders.status is never actually updated, so the order keeps cluttering the Active list, its table (if dine-in) is never freed, and it can still be billed at ₹0.
Verified by code review — not yet reproduced live. PATCH /api/orders/:orderId/items/:itemId/cancel (main/routes/index.ts:123-219) only ever touches the target order_items row and recalculates order totals; it never reads or writes orders.status. There is no code path anywhere in the backend that auto-transitions an order to cancelled when it runs out of active items (confirmed — no references to "all items cancelled" logic exist in main/).
Steps to reproduce
- Place an order with a single item (or cancel down to the last remaining active item on a multi-item order).
- On the Orders page, click the trash icon next to that item and confirm removal.
- Observe the order card.
Expected
Either:
- the app prevents cancelling the last active item and tells the user to cancel the whole order instead, or
- cancelling the last active item automatically transitions the order to
cancelled(freeing the table, etc.) since an order with zero active items isn't a real order anymore.
Actual
order_items.statusis set tocancelledfor that item (main/routes/index.ts:154).- Order totals are recalculated from the now-empty active-items set, so
subtotal/tax_amount/totalall become0(main/routes/index.ts:157-198). orders.statusis left completely untouched — it stays whatever it was (typicallypending).- On screen (
frontend/src/app/(dashboard)/orders/page.tsx:924-967and:999-1010): the "Items" section renders empty (sinceactiveItems = order.items.filter(i => i.status !== 'cancelled')is now[]), and every item shows struck-through under "Cancelled items". Total shows ₹0. Visually and functionally this is identical to the whole order being cancelled — which is almost certainly what's being reported as "cancelling an item cancels the entire order." - But the order is not actually cancelled: it still shows under the "Active" tab (
tabFilter === 'active'only excludesstatus === 'completed' || 'cancelled', and this order's status is still e.g.pending), its table is never freed (table-freeing only happens through the explicit whole-order cancel flow, gated onfree_table), and it's still billable —POST /bills/generatewould generate a ₹0 bill for a phantom order.
Root cause
main/routes/index.ts:123-219 (the item-cancel handler) has no check for activeItems.length === 0 after the cancellation, and never writes to orders.status.
Suggested fix (needs a product decision, not included here)
Inside the withTxn block, after recalculating activeItems:
- Option A (block): if
activeItems.length === 0, reject the cancellation with a 400 telling the user to cancel the whole order instead (via the existingPATCH /orders/:id/statuswithstatus: cancelled, which correctly restores inventory, frees the table, and setscancelled_at/cancellation_reason). - Option B (auto-cascade): if
activeItems.length === 0, also setorders.status = 'cancelled'(and free the table / restore inventory, matching what the whole-order cancel path already does atmain/routes/orders.ts:562-579) so the order doesn't linger as an empty zombie.
I'd lean toward Option A (simpler, avoids silently taking a destructive whole-order action from what looks like an item-level control) but this is a product call.
Also checked while investigating (not filing separately — appears correct)
The report mentioned the trash icon should only show if the order hasn't been marked "preparing." Current visibility condition (frontend/src/app/(dashboard)/orders/page.tsx:944): item.status === 'pending' && isOwnerOrManager && !paid. This is gated at the item level, not the order level. I traced the actual kitchen workflow (frontend/src/app/(dashboard)/kds/page.tsx:113-116, main/routes/order-items.ts) — item status transitions (pending → preparing → ...) happen per-item via PATCH /api/order-items/:id/status, and this already correctly hides the trash icon once that specific item starts being prepared. I could not find any current UI path that transitions the whole orders.status to preparing (PATCH /orders/:id/status is only ever called from the frontend for cancelled) — so item-level gating is actually the operative (and more precise) control today. If there's a scenario where this doesn't hold, please add it here with repro steps.
Activity
itsbkm commentedon Jul 20, 2026
Fixed in cf5ca9e. Cancelling the last active item now transitions the order to
cancelled(status +cancelled_at+cancellation_reason), restores tracked inventory for every item in the order, frees the table if one was assigned, and fires the samenotifyKdsUpdate()/notifyOrderUpdated()the explicit order-level cancel already uses. The existing restore-item endpoint already refused restores on a cancelled order, so that closes the "restore into a broken state" path too, no changes needed there.Extended
tests/issue-24-cancel-item-checkout.test.tsrather than adding a new file — its Scenario C already exercised "cancel the only item" but only checkedtotal == 0, neverstatus. Added: status transitions to cancelled + restore is refused, table gets freed on a dine-in order, and the negative case (cancelling one of several items must leave the order active). 25/25 passing on that file, full suite green.Fix FreeOpenSourcePOS#132: cancelling the last order item leaves it s…