The customers getFullList in OrdersReviewModalMount now expands
both 'company' and 'rep'. The Review Order modal's customer
cascade reads selectedCustomer.expand.rep.name to populate the
Rep label and writes it to orders.rep on save.
No other consumer of the customer list reads c.expand.* — the
scout confirmed this is safe.
Co-Authored-By: Claude <noreply@anthropic.com>
Co-authored-by: Orca <help@stably.ai>
Two new tests for the customer -> company/rep cascade:
1. Updates Company and Rep labels when the customer changes
- renders with customer A (company 'Atlas Corp', rep 'Riya Patel')
- clicks the customer-search mock (now picks the next non-selected
customer from the list)
- asserts the labels flip to B's values and A's are gone
2. Marks the modal dirty after a customer change cascades
- asserts the Save button is disabled at baseline
- after customer change, asserts it's enabled (proves the dirty
check picks up the cascaded company/rep divergence)
Also updates the CustomerSearch mock to compute the next non-selected
customer id from the list (via data-next-id) so cascade tests can
drive it without hardcoding 'cust-new'.
Co-Authored-By: Claude <noreply@anthropic.com>
Co-authored-by: Orca <help@stably.ai>
When the operator changes the customer in the Review Order modal,
the Company and Rep labels now reactively reflect the selected
customer's expand.company.company name and expand.rep.name (with
email fallback). On save, the modal writes the new company relation
id and the new rep display name to the order — so a customer
change actually re-parents the order rather than just changing the
display.
Guards in the save body prevent two PB errors:
- company is skipped when the customer is cleared (orders.company
is required, sending '' would 400)
- rep is skipped when the new customer has no rep, so existing
rep values are preserved
The cascade uses useMemo derived from customerId, not a useEffect,
so the label updates in the same render as the dropdown change.
Co-Authored-By: Claude <noreply@anthropic.com>
Co-authored-by: Orca <help@stably.ai>
Destructure company and rep from the request body and forward them
through updateOrderReviewFields' headerPatch. The service layer
(Task 1) already accepts and validates these fields; the route now
just plumbs them.
Co-Authored-By: Claude <noreply@anthropic.com>
Co-authored-by: Orca <help@stably.ai>
Extends updateOrderReviewFields' patch type to accept company
(relation id) and rep (free-text snapshot) in addition to the
existing order_number and customer. The modal cascade writes these
when the operator changes the customer; the service now carries
them through to the PB update.
The order_number collision check is unchanged — it only runs for
order_number changes, so the new fields are unaffected.
Co-Authored-By: Claude <noreply@anthropic.com>
Co-authored-by: Orca <help@stably.ai>
Adds a 'SKU' column between Description and Ordered in the lines
table on /stock-orders/[id]. The column reads from
line.expand.stock_item.sku (the relation was already accessible via
the type, but the page was not expanding it on the fetch).
Falls back to an em-dash when expand is missing (e.g. an item whose
stock_item was deleted) so the row never renders an empty cell.
Co-Authored-By: Claude <noreply@anthropic.com>
Co-authored-by: Orca <help@stably.ai>
The seeding useEffect copies line qty/cost into the per-line edit
buffers when the targeted draft loads (or when the target changes).
The setState is intentional — the buffer is mirrored from the parent
draft's authoritative data — and the same suppress pattern is used
in src/app/(app)/collection-confirm/page.tsx and elsewhere.
Co-Authored-By: Claude <noreply@anthropic.com>
Co-authored-by: Orca <help@stably.ai>
Asserts the Edit button appears on draft rows only, and that clicking
it opens the modal with the right editPurchaseId. The mock stops
forwarding the click to the row's onClick via the existing
e.stopPropagation() pattern in the implementation.
Co-authored-by: Orca <help@stably.ai>
Adds a Pencil-icon ghost button to the trailing cell of every draft
row. Clicking it opens the QuickDraftModal in edit mode for that
purchase, scoped to that draft's lines. e.stopPropagation prevents
the row's detail-page navigation from also firing.
Co-authored-by: Orca <help@stably.ai>
Asserts the modal hides non-targeted drafts, renders qty/cost inputs
per line, PATCHes only changed fields on Save, and that remove still
works in edit mode.
Co-authored-by: Orca <help@stably.ai>
Adds an optional editPurchaseId prop. When set, the modal hides all
other drafts and renders per-line qty/cost inputs + a Save button that
PATCHes /api/v1/stock-quick-draft/line/[lineId]. Remove still works.
Search + add still works to add more lines to the same draft.
Co-authored-by: Orca <help@stably.ai>
Adds a PATCH handler next to the existing DELETE handler. Validates
JSON body, maps service result reasons to HTTP status codes, and uses
admin PB to bypass user-side collection rules.
Co-authored-by: Orca <help@stably.ai>
Adds a TDD'd updateQuickDraftLine function that PATCHes
quantity and/or cost_price on a stock_purchase_items row.
Returns the same discriminated-union shape as its sibling
add/remove functions. No schema changes.
Co-Authored-By: Claude <noreply@anthropic.com>
Co-authored-by: Orca <help@stably.ai>
- orders-service.test.ts: destructure capture-group bug on regex match
(was assigning m[0] to 'field', so filter clauses never matched).
Two pre-seed tests were masked by the bug.
- quotes-service.test.ts: createQuoteFromEmail now returns 0
attachments; the n8n flow uploads them in a follow-up step. Update
the assertion that still expected the old embedded-base64 count.
- collection-pool/page.test.tsx: tests assume ONE supplier group with
multiple rows; supply that shape and scope the selectors. Also wait
for data load in the chevron test and gather list items across all
<ul> elements in the sort control helper.
- pocketbase.ts: wrap authStore.onChange registration defensively so
the test mock (no authStore) doesn't throw at startup.
- stock-orders/page.tsx: drop the re-export of
STOCK_PURCHASE_STATUS_VALUES on the page module — Next.js page
route files only allow the default export.
- collection-confirm/view-model.test.ts: buildConfirmNote input is
Pick<InvoiceDetails, 'date' | 'number'>; drop the extra setBy fields.
Co-authored-by: Orca <help@stably.ai>
The Process Order Item modal already fetched order_items history with
the supplier id, but the rendered row only showed description, order
number, cost, selling, and markup — leaving the operator to guess
which supplier fulfilled each past order.
Now each Recent Orders row also shows the resolved supplier name,
tinted with the same deterministic supplier colour used by the
buyer-tasks cards (djb2 → 18-swatch palette). The id is looked up
locally against the modal's existing `suppliers` prop — no extra PB
expand needed.
Unknown supplier ids (e.g. supplier deleted since the past order) are
silently omitted rather than surfaced as a stale id. Two new tests
cover the resolved-name and missing-id cases.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: Orca <help@stably.ai>
text-warn-foreground is the dark contrasting text colour meant to
sit ON TOP of a warn-tinted background (e.g. an alert panel).
Used as the icon colour on a plain card it renders nearly black.
text-warn is the actual amber/orange hue — what we want for the
Review + Open icons on the order list.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: Orca <help@stably.ai>
Both right-hand action buttons on the order list card now share the
warn-amber treatment, so they pop against the muted card background
and the operator can spot them at a glance. The icons (AlertTriangle
for Review, ExternalLink for Open) keep the actions distinguishable.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: Orca <help@stably.ai>
When the operator clicks "Mark as ready" on a buyer-tasks po_sent card
and fills in the note, the item should land in the collection pool
(`item_status: "collection_pool"` + `ready_for_collection: true`) so
a driver can be assigned. Previously the service set
`item_status: "ready_delivery"`, which was rendered as the word
"Collected" on /orders and on /orders tabs — operators reasonably
read that as "driver already picked it up", and the item never
appeared on /collection-pool where they expected to assign a driver.
Now:
- markGroupReady writes a single PB update with the same shape as
/api/v1/collection-map/items/[id]/assign (collection_pool +
ready_for_collection: true + notes)
- po_sent buyer-tasks filter excludes collection_pool and assigned,
so marked-ready items drop out of buyer-tasks and surface on
/collection-pool instead
- ITEM_STATUS_VISUAL.ready_delivery is labelled "Ready for
Collection" (kept distinct from the actual `collected` state) for
any other flow that legitimately sets this status (e.g. stock-out)
- new item-status-ui.test.ts locks down the label/tab mapping so
the regression can't return
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: Orca <help@stably.ai>
generatePoForGroup was creating purchase_orders records without
`po_number`, which is required by the PB schema. PB rejected the
create with 500 ("Failed to create record.") and the operator never
got a PO. Fix:
- New src/lib/server/po-number.ts — server-side generatePONumber()
that mirrors the client version in pb-helpers.ts (PO-YYYY-MM-NNN
format) but uses getAdminPocketBase(). Per the data-access rules,
don't extend pb-helpers.ts; server business logic lives under
lib/server/.
- generatePoForGroup now generates the PO number, includes
total_amount (sum of costPrice * quantity) and order_item
(back-link to the items) in the create payload — mirrors the
canonical /orders/[id]/process PO-creation flow so the PB admin
row shows related lines without an extra getList call.
- Test updated to assert the new payload (po_number regex,
total_amount computed, order_item back-link).
UI while here:
- BuyerTaskGroupCard now renders a deterministic supplier colour
stripe on the left edge. New src/lib/supplier-color.ts hashes the
supplierId into a small Tailwind 600 palette (18 colours) so the
same supplier always shows the same colour, and the supplier name
gets tinted with the same colour for quick scanning.
- PO number is now shown prominently at the top of the card (in a
warm-toned badge that matches the AI Insights theme) when one
exists — po_draft and po_sent tabs. Needs PO tab omits it.
Co-authored-by: Orca <help@stably.ai>
Three small additions to the process modal:
- Markup cell added to the pricing grid (4 cols now: PO Qty, Cost,
Selling, Markup). Read-only and dashed like Selling — derived from
selling/cost so the operator sees the margin they're pricing at.
Green when selling ≥ cost, destructive when selling < cost.
- Above the PO description textarea, the order's original item
description is now shown as a read-only reference hint. Lets the
operator see what the customer asked for vs what they're sending
to the supplier without having to scroll back to the modal header.
- AI Insights toggle now has an explicit "Show" / "Hide" label next
to the chevron so the affordance is obvious. The chevron direction
was the only signal before; the label makes the toggle
self-documenting.
Co-authored-by: Orca <help@stably.ai>
The AI Insights section was info-blue while the modal's outer border
and the popover's AI card are warn-yellow. The mixed cool/warm
palette felt disjointed. Switching the section to warm warn tones
makes the yellow border feel intentional rather than decorative.
- border-info/20 → border-warn/40 (heavier, with shadow-warn/10 so
the section has a soft glow)
- bg-info/5 → bg-warn/5
- text-info → text-warn-foreground (Sparkles icon, "AI Insights"
label, chevrons, suggestion-available pill, Use AI button)
- Use AI button: border-info/30 → border-warn/40, hover bg-info/10
→ hover bg-warn/10
Co-authored-by: Orca <help@stably.ai>
Popover (the ⓘ AI snapshot card):
- Switch from raw amber-200/amber-50 to the project's warn theme
tokens so it matches the rest of the design system and respects
dark mode.
- Subtle gradient backgrounds (from-warn/10 to-warn/5) so each card
reads as a distinct section without screaming for attention.
- Icons now sit in coloured chips (rounded-md bg-warn/20 etc.) for
better visual hierarchy.
- Freshness badges are pill-shaped with an inset ring instead of a
flat square — softer.
- Fuzzy-match warning box uses warn tokens too so it doesn't clash
with the parent card.
- Supplier / quote rows get a faint hover background and tighter
spacing for easier scanning.
Process modal:
- Outer Card border switched from border-border/60 to
border-2 border-warn/40 with a warm shadow-warn/15. The whole
modal now pops on the dimmed background — fits the "stand out
orange/yellow" ask.
Co-authored-by: Orca <help@stably.ai>