WFDASH-WEBAPP/src/components/collection-confirm
Dave 7a1239a187 fix(orders): bill the customer quantity, not the supplier order_qty
`order_items.order_qty` is the SUPPLIER-facing quantity — what prints
on the PO. Five call sites read it as the customer quantity, which is a
money bug in both directions:

  - supplier MOQ (customer 4, we buy 10) → invoiced 10, over-billing 6
  - shorted line (customer 100, supplier covers 40) → invoiced 40,
    under-billing 60 until the operator splits the remainder

The invoicing screen never fetched `quantity` at all (zero occurrences
in page.tsx), so it could not bill the customer quantity even in
principle. Added to the PB field projection, which is load-bearing:
drop it and PB returns undefined and every line silently falls back to
order_qty. A page-level test now fails if the projection drifts.

`maxSurplusFor` measured the surplus as `collected - order_qty`, which
on an MOQ line is always 0 — the overflow was mathematically unbookable
and vanished from inventory by construction, not by operator error.

Four call sites built delivery-note lines by hand; two passed
`quantity` and two forgot it, so the same DN printed the customer's 4
via the service layer and the supplier's 10 via the API routes. All
four now share `buildDeliveryNoteLine`, so they cannot diverge again.

`order_qty` had no doc comment at all next to an 11-line one on
`quantity` — that absence is why five subsystems guessed differently.
Now documented, with a new `src/lib/order-item-qty.ts` holding
`customerQty` / `supplierQty` as the single definition.

driver-stops.ts and collection-confirm/po-card.tsx read `order_qty`
CORRECTLY (they check what the supplier delivered against what we
ordered from them) and carry comments saying so, so the next cleanup
doesn't "fix" them into breaking.

23 new tests. Full suite: no regressions — the 15 failures are
identical before and after (verified by stashing and re-running).

Audit: docs/superpowers/specs/2026-08-30-quantity-handling-audit.md

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-30 18:28:33 +02:00
..
confirm-invoice-dialog.test.tsx test(collection-confirm): add confirm-invoice-dialog tests 2026-08-03 02:32:44 +02:00
confirm-invoice-dialog.tsx chore(collection-confirm): cleanup nice-to-haves from final review 2026-08-03 03:04:23 +02:00
confirm-stock-dialog.test.tsx chore(tests): fix ~60 pre-existing TS errors across 11 test files 2026-08-27 04:43:40 +02:00
confirm-stock-dialog.tsx feat(collection-confirm): implement confirmation dialog for stock collection 2026-08-17 11:41:55 +02:00
invoice-viewer.test.tsx feat(collection-confirm): in-app invoice viewer with zoom + mobile layout 2026-08-04 01:38:25 +02:00
invoice-viewer.tsx feat: add new order, invoice, and dashboard management tools 2026-08-05 23:00:54 +02:00
items-table.test.tsx feat(collection-confirm): add stock item edits 2026-08-14 16:39:46 +02:00
items-table.tsx feat(collection-confirm): add quantity collection handling and deduplication improvements 2026-08-17 02:17:27 +02:00
photo-strip.test.tsx feat: add new order, invoice, and dashboard management tools 2026-08-05 23:00:54 +02:00
photo-strip.tsx feat: add new order, invoice, and dashboard management tools 2026-08-05 23:00:54 +02:00
po-card.test.tsx feat: add split views, stock picking slips, and invoice viewer 2026-08-09 23:16:55 +02:00
po-card.tsx fix(orders): bill the customer quantity, not the supplier order_qty 2026-08-30 18:28:33 +02:00