WFDASH-WEBAPP/src/lib/server
Dave fd3a9296c6 feat(delivery-notes): the note decides the quantity; the quote follows it
A counter note is raised when the customer takes goods off the shelf.
"They said make it five more" is edited on the NOTE. Previously that
edit changed the note and left the quote showing the old number, so
the same physical goods carried two different quantities across two
screens and the invoice was whichever screen the operator opened last.

`updateDeliveryNoteItemPricing` now pushes the note's quantity onto
the quote lines that came from it. A sync failure is logged, not
fatal — the edit already succeeded and the quote can be re-synced by
re-saving.

## Matching without a back-link

`quote_items` has no field pointing at its source `order_item`, so
the operator's call was to make the note authoritative and work around
it. Lines are paired by `sku + description`, and when a key repeats,
by ORDER OF OCCURRENCE.

That is sound because both sides derive from the same ordered list
of note lines: `createQuoteFromDeliveryNote` iterates the note's
items in query order and emits one quote line each, so the Nth of a
repeated key on the note is the Nth on the quote. On the live data
across 500 note lines there are ZERO groups sharing
(note, sku, description) — the key is unique in practice. The
occurrence counter exists so that if two identical lines are ever
added, the second still lands on the right row instead of both
writing to the first.

That is the failure the tests exist for: matching on the key alone
silently writes the wrong number onto a customer invoice. Verified by
removing the occurrence counter — exactly that test fails.

## Also carried through

- The note's COLLECTED qty is the billing qty, not the ordered qty, so
  a short pickup (10 ordered, 8 collected) puts 8 on the quote.
- A cancelled, rejected or declined quote keeps its own numbers.
  Rewriting a quote the customer has already seen is worse than a
  stale number.
- A line that cannot be matched is left alone, never guessed at.
- A note line with neither a collected nor an ordered qty is skipped,
  so this can never reintroduce the zero-quantity invoice line.

8 new tests. Full suite 3832 passing, tsc clean, no new lint errors.
2026-10-09 11:56:48 +02:00
..
agent-auth.test.ts
agent-auth.ts
ai-settings-service.ts feat: add AI quote processing pipeline and admin settings UI 2026-08-09 16:17:29 +02:00
ai-system-prompt.ts feat: add AI quote processing pipeline and admin settings UI 2026-08-09 16:17:29 +02:00
attachment-compressor.test.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
attachment-compressor.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
attachment-extractor.ts feat: add AI quote processing pipeline and admin settings UI 2026-08-09 16:17:29 +02:00
audit-context.test.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
audit-context.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
audit-log-service.test.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
audit-log-service.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
boss-dashboard-service.test.ts test(boss-dashboard): honour expand in mock + assert customer name in rep activity 2026-08-28 00:02:02 +02:00
boss-dashboard-service.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
browser-session.test.ts feat(giellie): service layer and authenticated SSE API routes 2026-09-30 23:54:52 +02:00
browser-session.ts feat(giellie): service layer and authenticated SSE API routes 2026-09-30 23:54:52 +02:00
buyer-tasks-service.test.ts feat(quotes): enhance scoring logic to include price matching 2026-08-24 07:09:39 +02:00
buyer-tasks-service.ts feat(quotes): enhance scoring logic to include price matching 2026-08-24 07:09:39 +02:00
collection-confirm-service.test.ts feat(collection-confirm): update stock item filtering logic in listCollectedItems 2026-08-17 08:27:36 +02:00
collection-confirm-service.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
collection-confirm-upload.test.ts
completed-window.test.ts feat(quotes): enhance scoring logic to include price matching 2026-08-24 07:09:39 +02:00
counter-service.ts feat(counter): add standalone counter order PWA 2026-08-13 09:27:20 +02:00
customers-coordinates.test.ts feat(visits): batch pin tool — tag every customer on a shaft at once 2026-09-07 08:30:23 +02:00
customers-service.test.ts feat(tv): add TV dashboard + refine workflows 2026-08-13 20:55:22 +02:00
customers-service.ts feat(customers): add status toggle and site/location field 2026-09-16 09:47:09 +02:00
dashboard-service.test.ts
dashboard-service.ts
deliveries-service.test.ts feat(deliveries): enhance delivery notes management and UI 2026-08-17 01:27:03 +02:00
deliveries-service.ts fix(delivery-note): signed copy invisible, and a bigger QR in the corner 2026-10-04 17:00:30 +02:00
delivery-note-order-match.test.ts feat(orders): include selling_price in duplicate-detection line match 2026-08-31 09:03:00 +02:00
delivery-note-order-match.ts feat(orders): include selling_price in duplicate-detection line match 2026-08-31 09:03:00 +02:00
delivery-notes-service.test.ts fix(delivery-notes): a counter note's quantity IS its collected quantity 2026-10-09 11:35:01 +02:00
delivery-notes-service.ts feat(delivery-notes): the note decides the quantity; the quote follows it 2026-10-09 11:56:48 +02:00
driver-stops.ts fix(orders): bill the customer quantity, not the supplier order_qty 2026-08-30 18:28:33 +02:00
giellie-agent.test.ts fix(giellie): args-aware tool policy, deny quotes.re_run_ai, harden bridge and agent 2026-10-01 00:36:54 +02:00
giellie-agent.ts fix(giellie): args-aware tool policy, deny quotes.re_run_ai, harden bridge and agent 2026-10-01 00:36:54 +02:00
giellie-attachments.test.ts feat(giellie): system prompt and attachment ingestion 2026-09-30 23:42:35 +02:00
giellie-attachments.ts feat(giellie): system prompt and attachment ingestion 2026-09-30 23:42:35 +02:00
giellie-chat-store.test.ts fix(giellie): stale-tab approval guard, size guard, SSE heartbeat, shared in-flight set 2026-10-01 00:36:55 +02:00
giellie-chat-store.ts fix(giellie): stale-tab approval guard, size guard, SSE heartbeat, shared in-flight set 2026-10-01 00:36:55 +02:00
giellie-http.test.ts fix(giellie): stale-tab approval guard, size guard, SSE heartbeat, shared in-flight set 2026-10-01 00:36:55 +02:00
giellie-http.ts fix(giellie): stale-tab approval guard, size guard, SSE heartbeat, shared in-flight set 2026-10-01 00:36:55 +02:00
giellie-mcp-bridge.test.ts feat(mcp): stock_items.set_pricing tool and pricing fields on stock_items.create 2026-10-01 01:02:44 +02:00
giellie-mcp-bridge.ts fix(giellie): reject non-string and path-shaped ids (incl. arrays) in the MCP bridge 2026-10-01 00:44:20 +02:00
giellie-service.test.ts fix(giellie): stale-tab approval guard, size guard, SSE heartbeat, shared in-flight set 2026-10-01 00:36:55 +02:00
giellie-service.ts fix(giellie): stale-tab approval guard, size guard, SSE heartbeat, shared in-flight set 2026-10-01 00:36:55 +02:00
giellie-system-prompt.test.ts fix(mcp): validate set_pricing actor and row ids; tighten pricing prompt 2026-10-01 01:09:43 +02:00
giellie-system-prompt.ts fix(mcp): validate set_pricing actor and row ids; tighten pricing prompt 2026-10-01 01:09:43 +02:00
giellie-tool-policy.catalogue.test.ts feat(mcp): stock_items.set_pricing tool and pricing fields on stock_items.create 2026-10-01 01:02:44 +02:00
giellie-tool-policy.test.ts feat(mcp): stock_items.set_pricing tool and pricing fields on stock_items.create 2026-10-01 01:02:44 +02:00
giellie-tool-policy.ts fix(giellie): args-aware tool policy, deny quotes.re_run_ai, harden bridge and agent 2026-10-01 00:36:54 +02:00
invoice-backfill.test.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
invoice-backfill.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
item-history-service.test.ts
item-history-service.ts
item-intel-service.ts feat(mcp): implement structured MCP error handling for upstream fetches 2026-08-15 22:32:11 +02:00
management-service.ts
mcp-upstream-fetch.test.ts feat(mcp): implement structured MCP error handling for upstream fetches 2026-08-15 22:32:11 +02:00
mcp-upstream-fetch.ts feat(mcp): implement structured MCP error handling for upstream fetches 2026-08-15 22:32:11 +02:00
order-completion.test.ts
order-completion.ts
order-delivery-note-link-service.test.ts feat(orders): include selling_price in duplicate-detection line match 2026-08-31 09:03:00 +02:00
order-delivery-note-link-service.ts feat(orders): include selling_price in duplicate-detection line match 2026-08-31 09:03:00 +02:00
order-item-context-service.test.ts
order-item-context-service.ts
order-item-margin-approval-service.test.ts feat(orders): bypass margin gate for non-invoiceable extras 2026-08-30 17:21:28 +02:00
order-item-margin-approval-service.ts feat(orders): bypass margin gate for non-invoiceable extras 2026-08-30 17:21:28 +02:00
order-item-status.test.ts
order-item-status.ts
order-items-service.test.ts fix(invoicing): bill only what's collected, support partial pickups + kit splits 2026-09-17 13:53:36 +02:00
order-items-service.ts fix(invoicing): bill only what's collected, support partial pickups + kit splits 2026-09-17 13:53:36 +02:00
order-number-duplicate-link-service.test.ts fix(orders): document PB schema prerequisite + strengthen duplicate-matcher tests 2026-08-30 22:08:24 +02:00
order-number-duplicate-link-service.ts fix(orders): document PB schema prerequisite + strengthen duplicate-matcher tests 2026-08-30 22:08:24 +02:00
order-order-duplicate-link-service.test.ts perf(orders): bulk-fetch candidate items in suggestOrderDuplicate (kill N+1) 2026-08-30 23:57:20 +02:00
order-order-duplicate-link-service.ts feat(orders): include selling_price in duplicate-detection line match 2026-08-31 09:03:00 +02:00
order-quote-link-service.test.ts feat(orders): add quote-match backlog sweep with age-gap reporting 2026-08-30 08:45:23 +02:00
order-quote-link-service.ts feat(orders): add quote-match backlog sweep with age-gap reporting 2026-08-30 08:45:23 +02:00
order-quote-match.test.ts fix(orders): parse PocketBase date format explicitly in the matcher 2026-08-29 13:20:40 +02:00
order-quote-match.ts fix(orders): parse PocketBase date format explicitly in the matcher 2026-08-29 13:20:40 +02:00
orders-service-review.test.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
orders-service.test.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
orders-service.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
po-email-template.ts
po-number.test.ts feat: add server-side quote search & PO number format update 2026-08-14 11:15:38 +02:00
po-number.ts feat: add server-side quote search & PO number format update 2026-08-14 11:15:38 +02:00
pocketbase-admin.test.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
pocketbase-admin.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
pos-auth.test.ts feat(pos): add assertPosAuth for POS_API_KEY-gated routes 2026-08-31 01:30:52 +02:00
pos-auth.ts feat(pos): add assertPosAuth for POS_API_KEY-gated routes 2026-08-31 01:30:52 +02:00
pos-customer-search.test.ts feat(pos): customer picker matches the company name, and stops hiding truncation 2026-10-07 15:53:23 +02:00
pos-letterhead-service.ts fix(pos): print the letterhead's own logo, and match delivery note to the sale slip 2026-09-06 23:03:24 +02:00
pos-refund.test.ts feat(pos): refund a whole sale, put the goods back, print a credit slip 2026-10-07 23:41:09 +02:00
pos-service.test.ts feat(pos): raise a system delivery note on an account sale 2026-10-04 13:15:29 +02:00
pos-service.ts feat(pos): refund a whole sale, put the goods back, print a credit slip 2026-10-07 23:41:09 +02:00
pos-slip-layout-service.test.ts feat(pos): add pos-slip-layout-service for reading/writing per-letterhead slip layouts 2026-09-04 13:23:09 +02:00
pos-slip-layout-service.ts feat(pos): add pos-slip-layout-service for reading/writing per-letterhead slip layouts 2026-09-04 13:23:09 +02:00
price-list-match.test.ts
price-list-match.ts
price-list-service.test.ts feat(stock): seed pricing fields on create + sync price_list to stock_items 2026-09-14 13:04:08 +02:00
price-list-service.ts feat(stock): seed pricing fields on create + sync price_list to stock_items 2026-09-14 13:04:08 +02:00
purchase-order-agent-service.test.ts feat(collection-confirm): add stock item edits 2026-08-14 16:39:46 +02:00
purchase-order-agent-service.ts feat(collection-confirm): add stock item edits 2026-08-14 16:39:46 +02:00
purchase-orders-service.test.ts
purchase-orders-service.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
quote-email-template.ts feat(quotes): add approval page and email updates 2026-08-11 16:09:40 +02:00
quote-items-insights-service.test.ts feat: add AI quote processing pipeline and admin settings UI 2026-08-09 16:17:29 +02:00
quote-items-insights-service.ts feat: add AI quote processing pipeline and admin settings UI 2026-08-09 16:17:29 +02:00
quote-items-service.test.ts feat(mcp): add walk-in quote creation and enhance customer filtering 2026-08-15 11:50:48 +02:00
quote-items-service.ts feat(quotes): enhance quotes.submit functionality and error handling 2026-08-15 23:57:51 +02:00
quote-pdf-service.test.ts fix(delivery-notes): return to the list after quoting; kill the phantom supplier 2026-10-05 19:49:20 +02:00
quote-pdf-service.ts fix(delivery-notes): return to the list after quoting; kill the phantom supplier 2026-10-05 19:49:20 +02:00
quote-quantity-sync.test.ts feat(delivery-notes): the note decides the quantity; the quote follows it 2026-10-09 11:56:48 +02:00
quoted-before-service.test.ts feat(quotes): add quote-level "Quoted before?" panel to the process page 2026-08-29 08:52:19 +02:00
quoted-before-service.ts feat(quotes): add quote-level "Quoted before?" panel to the process page 2026-08-29 08:52:19 +02:00
quotes-ai-processor.ts feat: add AI quote processing pipeline and admin settings UI 2026-08-09 16:17:29 +02:00
quotes-from-email-service.test.ts fix(quotes-intake): clamp email_body to PB's 5000-char cap 2026-09-08 10:28:20 +02:00
quotes-from-email-service.ts fix(quotes-intake): clamp email_body to PB's 5000-char cap 2026-09-08 10:28:20 +02:00
quotes-search-service.test.ts fix(quotes-search): order hits by created desc so the most recent match is on top 2026-09-08 10:24:34 +02:00
quotes-search-service.ts fix(quotes-search): order hits by created desc so the most recent match is on top 2026-09-08 10:24:34 +02:00
quotes-service.test.ts fix(delivery-notes): return to the list after quoting; kill the phantom supplier 2026-10-05 19:49:20 +02:00
quotes-service.ts fix(delivery-notes): return to the list after quoting; kill the phantom supplier 2026-10-05 19:49:20 +02:00
rep-dashboard-service.test.ts feat(dashboard2): trim Row 1 to Selling-only with last-month delta 2026-09-01 01:55:52 +02:00
rep-dashboard-service.ts perf(dashboard2): consolidate 14 PB count queries into 1 fetch 2026-09-01 02:08:52 +02:00
rep-performance-service.test.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
rep-performance-service.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
rep-time-log-service.test.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
rep-time-log-service.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
repairs-service.test.ts feat(stock): update pricing logic and transform script for fasteners 2026-09-21 12:32:44 +02:00
repairs-service.ts feat(stock): update pricing logic and transform script for fasteners 2026-09-21 12:32:44 +02:00
stock-booking-service.test.ts feat: add split views, stock picking slips, and invoice viewer 2026-08-09 23:16:55 +02:00
stock-booking-service.ts feat(stock): implement auto-markup for selling price and markup on stock item creation and updates 2026-09-16 09:49:52 +02:00
stock-cost-service.ts feat(stock): implement auto-markup for selling price and markup on stock item creation and updates 2026-09-16 09:49:52 +02:00
stock-default-markup-service.test.ts feat(stock): implement auto-markup for selling price and markup on stock item creation and updates 2026-09-16 09:49:52 +02:00
stock-default-markup-service.ts feat(stock): implement auto-markup for selling price and markup on stock item creation and updates 2026-09-16 09:49:52 +02:00
stock-items-export.test.ts feat: implement discount functionality in POS system 2026-09-03 12:19:21 +02:00
stock-items-import.test.ts feat: implement discount functionality in POS system 2026-09-03 12:19:21 +02:00
stock-items-service.test.ts feat(stock): implement auto-markup for selling price and markup on stock item creation and updates 2026-09-16 09:49:52 +02:00
stock-items-service.ts feat(stock): implement auto-markup for selling price and markup on stock item creation and updates 2026-09-16 09:49:52 +02:00
stock-out.ts
stock-pick-service.test.ts feat(delivery-notes): see what's on a note, colour-coded progress, POS-aligned stock search 2026-10-04 20:40:13 +02:00
stock-pick-service.ts feat(delivery-notes): see what's on a note, colour-coded progress, POS-aligned stock search 2026-10-04 20:40:13 +02:00
stock-pricing-service.test.ts test(stock): cover validation + not_found + per-row independence 2026-08-31 16:48:40 +02:00
stock-pricing-service.ts fix(stock): skip ledger row on first-time pricing + include cost delta in notes 2026-08-31 16:46:42 +02:00
stock-purchase-invoice-matching-service.test.ts feat(mcp): expand stock purchase invoice matching service with 5 phases 2026-08-09 20:23:36 +02:00
stock-purchase-invoice-matching-service.ts feat: add split views, stock picking slips, and invoice viewer 2026-08-09 23:16:55 +02:00
stock-purchase-items-service.test.ts chore(tests): fix ~60 pre-existing TS errors across 11 test files 2026-08-27 04:43:40 +02:00
stock-purchase-items-service.ts feat(collection-confirm): add quantity collection handling and deduplication improvements 2026-08-17 02:17:27 +02:00
stock-purchases-migration.test.ts
stock-purchases-service.attachments.test.ts
stock-purchases-service.test.ts feat: add split views, stock picking slips, and invoice viewer 2026-08-09 23:16:55 +02:00
stock-purchases-service.ts feat: add split views, stock picking slips, and invoice viewer 2026-08-09 23:16:55 +02:00
stock-quantities-service.test.ts feat(stock): inline qty editing + column sort + pricing-gap operator scripts 2026-09-14 13:04:26 +02:00
stock-quantities-service.ts feat(stock): inline qty editing + column sort + pricing-gap operator scripts 2026-09-14 13:04:26 +02:00
stock-quick-draft-service.test.ts feat(stock): implement auto-markup for selling price and markup on stock item creation and updates 2026-09-16 09:49:52 +02:00
stock-quick-draft-service.ts feat(stock): implement auto-markup for selling price and markup on stock item creation and updates 2026-09-16 09:49:52 +02:00
stock-surplus.test.ts fix(orders): bill the customer quantity, not the supplier order_qty 2026-08-30 18:28:33 +02:00
stock-surplus.ts fix(orders): bill the customer quantity, not the supplier order_qty 2026-08-30 18:28:33 +02:00
tracker-service.test.ts feat(tracker): breadcrumb poller — WFDASH's own vehicle position history 2026-09-06 20:08:20 +02:00
tracker-service.ts feat(tracker): breadcrumb poller — WFDASH's own vehicle position history 2026-09-06 20:08:20 +02:00
travel-history-parser.test.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
travel-history-parser.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
tv-dashboard-service.ts feat(tv-dashboard): enhance TV dashboard with driver stops and performance metrics 2026-08-19 10:16:49 +02:00
tv-session.ts feat(tv): add TV dashboard + refine workflows 2026-08-13 20:55:22 +02:00
vehicle-trail-service.test.ts feat(tracker): breadcrumb poller — WFDASH's own vehicle position history 2026-09-06 20:08:20 +02:00
vehicle-trail-service.ts feat(tracker): breadcrumb poller — WFDASH's own vehicle position history 2026-09-06 20:08:20 +02:00
visits-service.test.ts feat(rep): add /rep/visits map with heatmap, per-customer clusters 2026-09-20 23:50:03 +02:00
visits-service.ts feat(rep): add /rep/visits map with heatmap, per-customer clusters 2026-09-20 23:50:03 +02:00