WFDASH-WEBAPP/scripts/migrations
Dave 7256de8859 feat(pos): refund a whole sale, put the goods back, print a credit slip
A customer who brings goods back needs the sale reversed: the items go
back into stock and the money comes back off the till. There was no way
to mark a POS sale as refunded, so nothing stopped the same sale being
refunded twice — which credits the customer twice and puts the same
stock back on the shelf twice.

Refunds are WHOLE-SALE only by operator decision; no partial refunds.
That is what lets `pos_sales.refunded` be a boolean instead of a
per-line refund table. Three columns: `refunded`, `refunded_at`,
`refund_amount`, stamped from the sale's post-discount total, which is
what the customer actually paid.

DOUBLE-REFUND GUARD, twice over, because this is the one failure that
costs real money:

  - `refunded` is read before anything else, and a flagged sale returns
    409 with no stock writes at all
  - the flag is stamped BEFORE any stock write, so a second concurrent
    tap sees it and bails. Writing stock first and flagging last would
    let both taps through.

A stock write that fails part-way does NOT roll the flag back. The
customer has their money either way, so re-running would double-credit
them; the failed lines come back in `stockErrors` for a manual count
correction, which is strictly better than a second refund.

Stock needs no schema: `return` was already in STOCK_REASON_VALUES,
just never used. Rows are written with POSITIVE change_qty and
reference_type "pos_sale_refund", leaving the original
customer_fulfillment rows untouched so the sale's own audit trail still
shows what was originally rung up.

cashup.html had expected drawer = sum of cash sales, so a cash refund
would have shown as a false variance on every cash-up after it. It now
subtracts cash refunds, and refunded sales contribute nothing to
takings for any tender.

The tender is read off the sale, never off the request — an earlier
version accepted it in the body, which would have let a caller assert
that a cash sale was really an EFT and talk the drawer out of balance.

At the till: tap still reprints, hold a sale row for 600ms to refund —
moving money sits behind a gesture you cannot do by accident. Refunded
sales stay in the list, dimmed and badged, because deleting them would
destroy the audit trail. The sheet names the items and says plainly what
happens next ("Hand back R10.57 in cash. The drawer count for this day
will drop by that amount.").

The credit slip reuses the sale renderer and the whole printer path with
`refund: true`, so there is no second printer to configure — but note
there were THREE render paths to fix (on-screen, Bluetooth text, ePOS
XML), and missing the third would have printed "TOTAL" on a credit note.

Migration applied to live PB and verified idempotent; field shapes
round-tripped on a real sale and restored. 10 new tests, 148 POS tests
pass, tsc clean, build passes. sw.js v34 -> v35 (history, cashup,
receipt and epos-print are all cache-first in the shell).
2026-10-07 23:41:09 +02:00
..
2026-06-23-add-quote-attachments-direction.ts feat: add quote templates, improved email, new statuses 2026-06-24 22:57:01 +02:00
2026-06-23-add-quote-email-composition-fields.ts feat: add quote templates, improved email, new statuses 2026-06-24 22:57:01 +02:00
2026-06-23-add-quote-outbound-email-fields.ts feat: add quote templates, improved email, new statuses 2026-06-24 22:57:01 +02:00
2026-06-23-add-quote-template-fields.ts feat: add quote templates, improved email, new statuses 2026-06-24 22:57:01 +02:00
2026-06-26-add-orders-rep.ts refactor: drop order-level status, shift to item-driven workflow 2026-06-29 13:06:01 +02:00
2026-06-27-add-order-quote-match-fields.ts refactor: drop order-level status, shift to item-driven workflow 2026-06-29 13:06:01 +02:00
2026-06-27-drop-orders-status-and-wipe.ts refactor: drop order-level status, shift to item-driven workflow 2026-06-29 13:06:01 +02:00
2026-06-28-add-companies-coordinates.ts refactor: drop order-level status, shift to item-driven workflow 2026-06-29 13:06:01 +02:00
2026-06-28-add-purchase-order-pdfs.ts refactor: drop order-level status, shift to item-driven workflow 2026-06-29 13:06:01 +02:00
2026-06-28-convert-customer-fields.ts refactor: drop order-level status, shift to item-driven workflow 2026-06-29 13:06:01 +02:00
2026-06-30-add-price-list-search-text.ts feat(pricelist): Catalogue search hits every field (incl. synonyms) 2026-06-30 12:10:27 +02:00
2026-06-30-add-price-list-sku-normalised.ts feat: implement sku-based catalogue pricing matching workflow 2026-06-30 21:20:53 +02:00
2026-06-30-add-price-list-source-file.ts feat(pricelist): Catalogue tab with search + category/brand filters 2026-06-30 11:49:48 +02:00
2026-06-30-add-pricelist-notes.ts feat(pricelist): notes field for AI instructions + file_name on upload 2026-06-30 11:09:27 +02:00
2026-06-30-add-quote-items-sku-and-pricing.ts feat: implement sku-based catalogue pricing matching workflow 2026-06-30 21:20:53 +02:00
2026-06-30-add-raw-price-list.ts feat(management): new Pricelist page with drag-drop upload + queue table 2026-06-30 09:14:36 +02:00
2026-06-30-backfill-price-list-search-text.ts feat: implement sku-based catalogue pricing matching workflow 2026-06-30 21:20:53 +02:00
2026-06-30-create-rfq-outbound.ts feat: implement sku-based catalogue pricing matching workflow 2026-06-30 21:20:53 +02:00
2026-07-02-add-sprint-categories-and-claims.ts feat(sprint): add new sprint pricing workflow and supporting code 2026-07-03 07:29:43 +02:00
2026-07-06-order-items-assigned-at.ts refactor: switch hardcoded UI colors to semantic theme tokens 2026-07-07 17:22:03 +02:00
2026-07-06-order-items-collection-pool-rename.ts refactor: switch hardcoded UI colors to semantic theme tokens 2026-07-07 17:22:03 +02:00
2026-07-06-order-items-nullable-numbers.ts refactor: switch hardcoded UI colors to semantic theme tokens 2026-07-07 17:22:03 +02:00
2026-07-06-order-items-pricing-fields.ts refactor: switch hardcoded UI colors to semantic theme tokens 2026-07-07 17:22:03 +02:00
2026-07-06-purchase-orders-documents-field.ts refactor: switch hardcoded UI colors to semantic theme tokens 2026-07-07 17:22:03 +02:00
2026-07-06-purchase-orders-send-fields.ts refactor: switch hardcoded UI colors to semantic theme tokens 2026-07-07 17:22:03 +02:00
2026-07-06-quote-attachments-quote-item-and-kind.ts refactor: switch hardcoded UI colors to semantic theme tokens 2026-07-07 17:22:03 +02:00
2026-07-07-backfill-assigned-rfc.ts refactor: update pool UI, fix tiles and config 2026-07-07 18:01:43 +02:00
2026-07-07-order-items-collection-photo.ts feat(collection-pool): add unified redesigned collection pool view 2026-07-07 20:49:31 +02:00
2026-07-07-order-items-ready-for-collection.ts refactor: switch hardcoded UI colors to semantic theme tokens 2026-07-07 17:22:03 +02:00
2026-07-09-po-additional-attachments.ts feat(po-attachments): add migration for additional_attachments field 2026-07-09 23:22:23 +02:00
2026-07-09-po-pdf-tokens.ts feat(po-pdf): add token backfill migration script 2026-07-09 22:28:50 +02:00
2026-07-10-restore-counters.ts fix, feat, style: standardize quote numbers, polish UI 2026-07-10 22:21:50 +02:00
2026-07-14-create-stock-purchases.ts feat(stock): implement stock management workflow 2026-07-15 21:53:20 +02:00
2026-07-14-create-stock.ts feat(stock): implement stock management workflow 2026-07-15 21:53:20 +02:00
2026-07-15-add-stock-items-target-qty.ts feat(stock): implement stock management workflow 2026-07-15 21:53:20 +02:00
2026-07-15-add-stock-line-drv-id.ts feat: add stock line support to collection pool 2026-07-16 20:41:42 +02:00
2026-07-15-stock-purchase-line-state.ts feat: add stock line support to collection pool 2026-07-16 20:41:42 +02:00
2026-07-16-add-stock-purchase-documents-history.ts feat(stock-purchases,driver-app): add stock purchase workflow and driver pickup support 2026-07-18 22:43:50 +02:00
2026-07-16-add-stock-purchase-send-statuses.ts feat(stock-purchases,driver-app): add stock purchase workflow and driver pickup support 2026-07-18 22:43:50 +02:00
2026-07-17-add-stock-purchase-problem-fields.ts feat(stock-purchases,driver-app): add stock purchase workflow and driver pickup support 2026-07-18 22:43:50 +02:00
2026-07-19-create-delivery-notes.ts chore: add logos, opencode config and build assets 2026-07-27 08:21:20 +02:00
2026-07-19-delivery-notes-signed-copy.ts chore: add logos, opencode config and build assets 2026-07-27 08:21:20 +02:00
2026-07-19-delivery-notes-v3.ts chore: add logos, opencode config and build assets 2026-07-27 08:21:20 +02:00
2026-07-20-delivery-tracking.ts chore: add logos, opencode config and build assets 2026-07-27 08:21:20 +02:00
2026-07-24-partial-approval.ts chore: add logos, opencode config and build assets 2026-07-27 08:21:20 +02:00
2026-07-25-add-ai-insights-fields.ts chore: add logos, opencode config and build assets 2026-07-27 08:21:20 +02:00
2026-07-26-fix-quote-counter-and-reassign-numbers.ts fix(api,layout): sprint 500 chunk + add 9 missing top-level agent routes + metadataBase; drop unused collection_pool feature; quote counter fix migration 2026-07-26 22:18:49 +02:00
2026-08-03-add-stock-pricing-and-cost-history.ts feat: unify buyer task flows by supporting both customer orders and stock purchases in mark-ready API and UI, and fix PDF table layout responsiveness. 2026-08-03 18:52:30 +02:00
2026-08-04-add-order-items-item-status-index.ts perf(orders): server-side tab filter, slim expand payload, single combined count query 2026-08-04 18:17:59 +02:00
2026-08-04-add-quotes-status-index.ts perf(quotes): server-side tab filter, slim expand, combined counts + bonus bugfix 2026-08-04 19:01:31 +02:00
2026-08-06-add-stock-purchase-booked-fields.ts feat(stock-purchase): split warehouse confirm from accountant booking 2026-08-06 23:01:15 +02:00
2026-08-06-stock-purchases-additional-attachments.ts feat(pb): add stock_purchases.additional_attachments file field (migration) 2026-08-06 23:32:12 +02:00
2026-08-07-flip-stale-unprocessed-items.js fix(sprint): filter stale quote items, add cleanup migrations 2026-08-07 20:15:46 +02:00
2026-08-07-renumber-duplicate-quotes.js fix(sprint): filter stale quote items, add cleanup migrations 2026-08-07 20:15:46 +02:00
2026-08-07-stock-purchase-items-catalogue-fields.ts feat: add split-invoice stock PO workflow and supporting tooling 2026-08-07 00:53:29 +02:00
2026-08-08-add-quotes-ai-status.ts feat: add AI quote processing pipeline and admin settings UI 2026-08-09 16:17:29 +02:00
2026-08-09-add-ai-settings.ts feat: add AI quote processing pipeline and admin settings UI 2026-08-09 16:17:29 +02:00
2026-08-09-add-ai-system-prompt.ts feat: add AI quote processing pipeline and admin settings UI 2026-08-09 16:17:29 +02:00
2026-08-09-backfill-quote-validity-days.ts fix: correct quote expiry handling for PocketBase's 0 default 2026-08-11 08:32:17 +02:00
2026-08-09-repair-stock-double-booking.ts feat: add split views, stock picking slips, and invoice viewer 2026-08-09 23:16:55 +02:00
2026-08-13-fix-delivery-notes-relations.ts feat(delivery-notes, migrations): add tabs, grouping, markup, and fix DN relations 2026-08-14 09:41:14 +02:00
2026-08-17-add-order-items-in-stock.ts feat(collection-confirm): implement confirmation dialog for stock collection 2026-08-17 11:41:55 +02:00
2026-08-29-add-order-delivery-note-match-fields.ts feat(orders): add delivery-note repeat-order match fields 2026-08-30 12:57:16 +02:00
2026-09-04-create-pos-slip-layouts.ts chore(pos): add pos_slip_layouts PB collection for the slip layout editor 2026-09-04 12:46:17 +02:00
2026-09-09-add-quotes-attachment-fields.ts feat(quotes): split attachments into internal vs outbound with operator upload UI 2026-09-09 13:43:56 +02:00
2026-09-09-fix-quote-attachment-fields-maxselect.ts feat(quotes): split attachments into internal vs outbound with operator upload UI 2026-09-09 13:43:56 +02:00
2026-09-09-migrate-quote-attachments-to-fields.ts feat(quotes): split attachments into internal vs outbound with operator upload UI 2026-09-09 13:43:56 +02:00
2026-09-25-backfill-invoice-reconcile.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
2026-09-25-create-audit-log.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
2026-09-28-create-place-overrides.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
2026-09-28-create-rep-trip-legs.ts feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
2026-09-30-create-giellie-chats.ts feat(giellie): chat store and giellie_chats migration 2026-09-30 23:44:55 +02:00
2026-10-03-add-pos-sales-delivery-note.ts feat(pos): raise a system delivery note on an account sale 2026-10-04 13:15:29 +02:00
2026-10-07-add-pos-sales-refund-fields.ts feat(pos): refund a whole sale, put the goods back, print a credit slip 2026-10-07 23:41:09 +02:00
_env-loader.ts feat(delivery-notes, migrations): add tabs, grouping, markup, and fix DN relations 2026-08-14 09:41:14 +02:00
add-pos-sale-discount-fields.ts feat: implement discount functionality in POS system 2026-09-03 12:19:21 +02:00
add-quote-items-user-edited.ts feat: add AI quote item rewriter and supporting UI/UX changes 2026-06-22 18:29:59 +02:00
broaden-quote-attachments-mime-types.ts feat(quotes): add email-to-quote pipeline with attachments and replies 2026-06-16 20:46:28 +02:00
create-order-items-v2-and-collection-pool.ts chore: standardize line endings and add full test + config scaffolding 2026-06-11 20:30:02 +02:00
create-quote-attachments-and-replies.ts feat(quotes): add email-to-quote pipeline with attachments and replies 2026-06-16 20:46:28 +02:00