No description
Find a file
Dave 8232a87f99 feat(pos): ex-VAT totals, ad-hoc custom lines, fixed discount tiers
Three changes to the POS sale pipeline, all in one commit because
pos-service.ts / types.ts / pos-app.js are each touched by all three and
splitting them would only produce intermediate broken states.

VAT is now charged on top, not backed out

Catalogue prices are ex-VAT: `selling_price` is derived as
`cost_price x markup` from the supplier's ex-VAT cost, with no VAT
uplift anywhere (verified — median selling/cost across the catalogue is
exactly 1.8). The till nevertheless treated them as VAT-inclusive and
recovered the net with `total / 1.15`, so every sale was recorded with
an understated net and an understated VAT line.

An R100 ex-VAT line now rings up as R115. The rate comes from the till's
letterhead, read server-side rather than accepted off the wire, and is
stored per sale so a later letterhead rate change cannot rewrite what
was charged. All 3,623 catalogue prices are untouched.

Ad-hoc "custom" lines

A cashier can now ring up something that was never a catalogue item —
"bolt cutting" at R20 agreed at the counter — via a new
"+ Add a custom item" composer. The line prints on the slip and lands in
pos_sale_items, but carries no stock_item relation and takes no stock
movement: no ledger row, no current_qty decrement, no catalogue entry.

pos_sale_items.stock_item is no longer required at the schema level and
an explicit `is_custom` flag marks the line, rather than inferring it
from a blank relation — so "how much did we make on bolt cutting this
month" is a plain query, and a future stock-less line type cannot be
confused with a corrupt one.

Custom lines are refused on account sales: those raise a delivery note
the customer signs for and feed their statement, so an ad-hoc charge
must not slip onto one. Enforced server-side, not just in the UI.

Discounts are now 20% or 40%, nothing else

The R-off / %-off pills and the free-typed number box are gone. The
server treats DISCOUNT_PERCENT_OPTIONS as an allowlist and refuses
anything outside it rather than coercing it, so a stale till cannot
invent a discount. The discount lands on the ex-VAT subtotal, making
"20% off" a true 20% off both the net and the VAT on it.

Two defects found while doing this

pos_sales was missing six fields that checkoutSale has been writing all
along (gross_subtotal, gross_vat, gross_total, discount_amount,
discount_kind, discount_input). PocketBase accepts unknown fields
without error and silently drops them, so all 192 historical sales lack
them and a discounted sale reprinted as though no discount had been
applied. Added, plus vat_percent.

History also passed gross_total (VAT-inclusive) where the receipt
derives the gross VAT from an ex-VAT base — a reprint double-counted
VAT. Corrected.

Verification: 3860 tests pass (14 new); typecheck, lint and production
build clean; live PocketBase probe confirms a pos_sale_items row with
stock_item="" and is_custom=true round-trips, with the probe removed
afterwards.
2026-10-12 22:54:09 +02:00
.agents/skills chore(agents): add wfdash-rep-visit-batch-pin skill 2026-09-20 23:49:58 +02:00
.audit-screenshots feat(tv-dashboard): enhance TV dashboard with driver stops and performance metrics 2026-08-19 10:16:49 +02:00
.claude feat(quotes): split attachments into internal vs outbound with operator upload UI 2026-09-09 13:43:56 +02:00
.codex feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
.design-review
.e2e-tmp
.freebuff feat(quotes): enhance quote creation with new fields and error handling 2026-08-26 23:55:30 +02:00
.kilo feat(delivery-notes): standardize delivery-note letterhead across PDFs 2026-08-18 11:53:25 +02:00
.mavis/plans
.od-skills
.opencode/openwork/inbox/chat-attachments fix: correct quote expiry handling for PocketBase's 0 default 2026-08-11 08:32:17 +02:00
.playwright-mcp feat(quotes): add "Include older" toggle for searching full quote history 2026-08-20 10:18:44 +02:00
.remember fix: resolve PO bug, add sku, improve webhooks 2026-08-06 13:13:57 +02:00
.superpowers/brainstorm/39145-1783449414
.tmp feat: implement discount functionality in POS system 2026-09-03 12:19:21 +02:00
.vscode
docs fix(mcp): validate set_pricing actor and row ids; tighten pricing prompt 2026-10-01 01:09:43 +02:00
e2e feat: collection-map updates, todays-stops, quote/approval rework, buyer-tasks, e2e 2026-08-03 09:33:45 +02:00
n8n_workfows fix(quotes): persist letterhead choice on terminal-status quotes too 2026-09-09 18:09:27 +02:00
pb-exports feat(visits): office pin tool for seeding customer coordinates 2026-09-06 16:58:07 +02:00
plugin-source
pocketbase/pb_hooks feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
public feat(pos): ex-VAT totals, ad-hoc custom lines, fixed discount tiers 2026-10-12 22:54:09 +02:00
reports feat(audit): implement audit trail for delivery notes and orders 2026-09-30 01:48:44 +02:00
scripts chore(backfill): stamp qty_collected on 301 counter delivery-note lines 2026-10-09 12:00:17 +02:00
src feat(pos): ex-VAT totals, ad-hoc custom lines, fixed discount tiers 2026-10-12 22:54:09 +02:00
--full-page feat(deliveries): enhance delivery notes management and UI 2026-08-17 01:27:03 +02:00
.gitignore chore: ignore .tmp/ — scripts cache PB admin tokens there 2026-09-06 21:39:51 +02:00
.mcp.example.json
.tmp-inspect-agent-scan.mjs feat(collection-confirm): add stock item edits 2026-08-14 16:39:46 +02:00
AGENT_BRIEF_AUTOFILL_QUOTES.md
AGENT_BRIEF_AUTOFILL_QUOTES.v1.long.md
AGENTS.md docs(giellie): document args-aware policy, trust semantics, env and PB rules 2026-10-01 00:36:55 +02:00
APP_CAPABILITIES.md chore: add logos, opencode config and build assets 2026-07-27 08:21:20 +02:00
bulk_modal.png
CLAUDE.md
components.json
deno.lock chore: add logos, opencode config and build assets 2026-07-27 08:21:20 +02:00
Dockerfile
eslint.config.mjs
img1_mid2.png chore(scripts): one-time stock-order loaders + source screenshots 2026-08-03 17:33:28 +02:00
img1_skus_v.png chore(scripts): one-time stock-order loaders + source screenshots 2026-08-03 17:33:28 +02:00
img2_skus_v.png chore(scripts): one-time stock-order loaders + source screenshots 2026-08-03 17:33:28 +02:00
INTERACTION_REVIEW_2026-09-01.md chore: update package.json 2026-09-02 00:41:31 +02:00
invoicing-all.png
invoicing-invoiced.png
invoicing-ready.png
invoicing-search-order.png
invoicing-search-po.png
invoicing-search.png
invoicing_loaded.png
invoicing_page.png
modal-current.png fix: resolve PocketBase expand projection bug 2026-08-06 01:33:16 +02:00
modal-top-1785968700.png fix: resolve PocketBase expand projection bug 2026-08-06 01:33:16 +02:00
modal-top.png fix: resolve PocketBase expand projection bug 2026-08-06 01:33:16 +02:00
mr8xsu7z-quotes.pb.js
next.config.ts fix(scan): /scan had no way to log in, and blocked LAN access was stale 2026-10-04 14:44:48 +02:00
nixpacks.toml
nss-hex-bolts-din931-pilo_2t.csv feat(stock): update pricing logic and transform script for fasteners 2026-09-21 12:32:44 +02:00
nss-hex-bolts-din931-pilot.csv feat(stock): update pricing logic and transform script for fasteners 2026-09-21 12:32:44 +02:00
nss-hex-nut-din934-grade8-a2.csv feat(stock): implement auto-markup for selling price and markup on stock item creation and updates 2026-09-16 09:49:52 +02:00
nss-hex-nut-din934.xlsx feat: implement discount functionality in POS system 2026-09-03 12:19:21 +02:00
opencode.jsonc chore: add logos, opencode config and build assets 2026-07-27 08:21:20 +02:00
p1-classified.png chore: add stray p1-classified.png + p1-mask.png 2026-08-04 20:26:55 +02:00
p1-mask.png chore: add stray p1-classified.png + p1-mask.png 2026-08-04 20:26:55 +02:00
package-lock.json feat(delivery-note): print a scannable QR of the note number 2026-10-04 13:15:41 +02:00
package.json feat(pos): raise a system delivery note on an account sale 2026-10-04 13:15:29 +02:00
page-overview.png
pb_quote_attachments.schema.json
pb_schema (1).json
playwright.config.ts
postcss.config.mjs
README.md chore: add logos, opencode config and build assets 2026-07-27 08:21:20 +02:00
repairs-list-mobile.jpg feat(quotes): add "Include older" toggle for searching full quote history 2026-08-20 10:18:44 +02:00
Screenshot 2026-07-09 at 01.12.32.png
single_modal.png
sqlite_mcp_server.db
stock-flow-overview.html feat: implement discount functionality in POS system 2026-09-03 12:19:21 +02:00
stock-orders-after.png chore(scripts): one-time stock-order loaders + source screenshots 2026-08-03 17:33:28 +02:00
tsconfig.json feat(orders): enhance order handling with email_message_id deduplication and displayContactName utility 2026-08-21 19:18:02 +02:00
tv-board-1920-v2.jpeg feat(deliveries): enhance delivery notes management and UI 2026-08-17 01:27:03 +02:00
tv-board-1920.jpeg feat(deliveries): enhance delivery notes management and UI 2026-08-17 01:27:03 +02:00
tv-board-current.jpeg feat(deliveries): enhance delivery notes management and UI 2026-08-17 01:27:03 +02:00
tv-board-v3.jpeg feat(deliveries): enhance delivery notes management and UI 2026-08-17 01:27:03 +02:00
tv-board-v4.jpeg feat(deliveries): enhance delivery notes management and UI 2026-08-17 01:27:03 +02:00
vitest.config.ts feat(stock): seed pricing fields on create + sync price_list to stock_items 2026-09-14 13:04:08 +02:00
wfdash-stock-2026-09-02_4-hex-nuts.xlsx feat: implement discount functionality in POS system 2026-09-03 12:19:21 +02:00
WFDASH_NEXTJS.code-workspace

WFDASH — Business CRM

A modern business CRM (customers, companies, suppliers, quotes, orders, driver collection pool, delivery notes, repairs) built as a Next.js 16 / PocketBase re-platform of the legacy Django app.

  • Frontend: Next.js 16 (App Router) + React 19 + TypeScript
  • UI: shadcn/ui (base-nova style), Tailwind CSS 4, Lucide icons, Sonner toasts
  • Maps: Leaflet + react-leaflet (clustering, marker edit, fullscreen, dark tiles)
  • Backend: PocketBase (database, auth, file storage, realtime)
  • MCP: in-app Model Context Protocol endpoint for n8n / external agents

Note

: This is not the Next.js from older training data. APIs, conventions, and file structure differ. Read the relevant guide in node_modules/next/dist/docs/ before writing code, and watch for deprecation notices.


Prerequisites

  • Node.js >= 20.9.0 (enforced by package.json engines)
  • PocketBase binary (the setup script connects to http://127.0.0.1:8090)but in my .env.local we have added the cloud url that its connecting to now at the moment

Quick Start

# 1. Install deps
npm install

# 2. Start PocketBase (in another terminal)
./pocketbase serve

# 3. Configure env (see "Environment Variables" below)
cp .env.local.example .env.local   # or create manually

# 4. Create collections in PocketBase
npm run setup                              # base collections (customers, companies, suppliers, quotes, orders, …)
npm run pb:migrate:orders-v2               # new order_items_v2 + collection_pool collections
npm run pb:migrate:order-status-in-stock   # switch orders.status to the operational enum + add order_items.in_stock

# 5. Run the app
npm run dev

App runs on http://localhost:3000.


Environment Variables

Create .env.local (gitignored) with:

# Public — exposed to the browser
NEXT_PUBLIC_POCKETBASE_URL=http://127.0.0.1:8090


# Server-only — admin used by setup / migration scripts and agent API
PB_ADMIN_EMAIL=admin@example.com
PB_ADMIN_PASSWORD=your_password

# Agent / MCP API auth (see "API & MCP" below)
AGENT_API_KEY=replace_with_secure_token

# Switch which collection the order-items service writes to
# Set to `order_items_v2` after running the migration
ORDER_ITEMS_COLLECTION=order_items

scripts/setup-pocketbase.ts currently has the admin email/password hard-coded — edit it before running, or set the matching env vars there.


NPM Scripts

Script Purpose
npm run dev Start Next.js dev server (webpack)
npm run build Production build
npm run start Run the production build
npm run lint ESLint over the project
npm run setup Create base PocketBase collections from scripts/setup-pocketbase.ts
npm run pb:migrate:orders-v2 Create order_items_v2 + collection_pool collections
npm test Vitest in watch mode
npm run test:run Vitest single run (CI-friendly)
npm run test:coverage Vitest with V8 coverage
npm run e2e Playwright E2E suite (boots next dev on port 3100)
npm run e2e:ui Playwright UI mode
npm run e2e:install One-time: install Playwright browsers + OS deps

See Testing below.


Features / Routes

All routes are file-based under src/app/.

Public (auth)

  • /login — sign in

Protected ((app))

  • /dashboard — overview stats + recent activity
  • /customers, /customers/[id] — CRUD, search, pagination, CSV export
  • /companies, /companies/[id] — CRUD with detail
  • /suppliers — CRUD with tag support
  • /supplier-map — Leaflet map: clustering, marker editing, fullscreen, dark tiles
  • /quotes — list with claim/process workflow
    • /quotes/[id] — detail
    • /quotes/[id]/process — process view
    • /quotes/approvals — approval queue
  • /orders, /orders/[id] — list + detail, PO generation by supplier
  • /collection-pool — driver work queue (pool of items to collect/deliver)
  • /delivery-notes — delivery note management
  • /repairs — repair requests

Cross-cutting

  • Light/dark theme switcher
  • Collapsible sidebar, mobile card views
  • Service worker for offline (registered client-side)

API & MCP

All agent endpoints require Authorization: Bearer <AGENT_API_KEY>.

Method Path Notes
GET /api/v1/health Liveness probe (no auth)
GET /api/v1/orders?page=1&perPage=50&status=new List orders (expands company,customer,quote)
POST /api/v1/orders/from-quote Body: { "quote_id": "..." } — creates an order + copies quote_items into the configured order_items collection; sets item_status=pending, drv_status=unassigned
GET /api/v1/collection-pool?page=1&perPage=50&status=queued List pool records
PATCH /api/v1/order-items/[id]/status Body: { item_status, drv_status, notes, problem_notes } (also accepts driver_status for backwards compat)
POST /api/mcp JSON-RPC style. Methods: tools/list, tools/call

MCP tools exposed

  • orders.list
  • orders.create_from_quote
  • collection_pool.list
  • order_items.update_status

See docs/api-mcp-layer.md for full payload details.


PocketBase Collections

Created by the setup / migration scripts and tracked in docs/pocketbase/:

  • customers — customer database (incl. alternative_email, buyer_email, coordinates)
  • companies — company database
  • suppliers — supplier network (tags, coordinates, closing time)
  • quotes, quote_items — quotes with line items and status workflow
  • orders, purchase_orders — orders + per-supplier POs
  • order_items — legacy order items
  • order_items_v2 — replacement schema (new item_status + drv_status model)
  • collection_pool — driver work queue
  • delivery_notes, delivery_note_items — delivery records
  • repairs — repair requests
  • stock_items — minimal stock tracking (folded into order_items for now)

Workflow rules and parity targets with the old Django app live in:


Project Structure

src/
├── app/
│   ├── (auth)/login/         # Login page
│   ├── (app)/                # Protected app pages
│   │   ├── dashboard/
│   │   ├── customers/  (+[id])
│   │   ├── companies/  (+[id])
│   │   ├── suppliers/
│   │   ├── supplier-map/
│   │   ├── quotes/           # + [id], [id]/process, approvals
│   │   ├── orders/    (+[id])
│   │   ├── collection-pool/
│   │   ├── delivery-notes/
│   │   └── repairs/
│   ├── api/
│   │   ├── mcp/route.ts                # MCP endpoint
│   │   └── v1/                         # Agent REST endpoints
│   │       ├── health/
│   │       ├── orders/                 # + from-quote
│   │       ├── order-items/[id]/status/
│   │       └── collection-pool/
│   ├── globals.css
│   ├── layout.tsx
│   └── page.tsx
├── components/
│   ├── ui/                   # shadcn/ui (base-nova)
│   ├── sidebar.tsx
│   ├── auth-provider.tsx
│   ├── theme-provider.tsx
│   ├── data-table.tsx
│   ├── line-items-table.tsx
│   ├── customer-form.tsx
│   ├── company-form.tsx
│   ├── supplier-form.tsx
│   ├── quote-form.tsx
│   ├── order-form.tsx
│   ├── delete-dialog.tsx
│   ├── supplier-map.tsx
│   └── service-worker-registration.tsx
└── lib/
    ├── pocketbase.ts         # Client (singleton) + server instances
    ├── pb-helpers.ts
    ├── types.ts              # TypeScript types for all collections
    ├── utils.ts
    ├── quote-status.ts
    └── server/
        ├── agent-auth.ts
        ├── pocketbase-admin.ts
        ├── orders-service.ts
        └── order-item-status.ts

scripts/
├── setup-pocketbase.ts                       # base collections
├── import-data.ts
└── migrations/
    └── create-order-items-v2-and-collection-pool.ts

e2e/                          # Playwright E2E tests (see Testing)
docs/                         # parity matrix, rollout plans, MCP, schema standards
vitest.config.ts              # Vitest config (jsdom, native tsconfig paths)
playwright.config.ts          # Playwright config (boots next dev on :3100)
Dockerfile                    # multi-stage Node 20 alpine build
nixpacks.toml                 # sets Nixpacks Node version to 20

Testing

The project ships with two test suites, both wired into npm scripts.

Vitest (unit + API route tests)

  • Framework: Vitest + jsdom + Testing Library
  • Config: vitest.config.ts — path aliases match tsconfig.json
  • Test files live next to the code they cover: src/**/*.{test,spec}.ts(x)

What gets covered:

File What it tests
src/lib/utils.test.ts cn() (clsx + tailwind-merge)
src/lib/quote-status.test.ts Status class lookup + format helper
src/lib/server/order-item-status.test.ts normalizeOrderItemStatusInput (every item/drv status, the legacy driver_status alias, invalid inputs)
src/lib/server/agent-auth.test.ts assertAgentAuth (bearer token validation, missing env, status code tagging)
src/lib/server/orders-service.test.ts createOrderFromQuote (idempotency, no-items error, payload shape, sequence increment, env-driven collection switch) and listCollectionPool (filter + pagination forwarding). PocketBase is mocked.
src/lib/server/pocketbase-admin.test.ts getAdminPocketBase env validation + auth call
src/app/api/v1/health/route.test.ts Health endpoint shape
src/app/api/v1/orders/route.test.ts Auth, status filter, pagination, error mapping
src/app/api/v1/orders/from-quote/route.test.ts Auth, 400 on missing quote_id, 201 vs 200 (idempotent), upstream error
src/app/api/v1/order-items/[id]/status/route.test.ts Auth, empty/invalid input, normalisation integration, env-driven collection
src/app/api/v1/collection-pool/route.test.ts Auth, param forwarding, error mapping
src/app/api/mcp/route.test.ts tools/list, every tools/call tool, missing-id guard, unknown-tool error, method-not-found

Run:

npm run test:run          # one-shot, CI-friendly
npm test                  # watch mode
npm run test:coverage     # with v8 coverage

The MCP tools/call auth path currently routes auth failures through the JSON-RPC catch (returns 500). The dedicated /api/v1/* endpoints properly return 401. Both are exercised in the tests.

Playwright (E2E)

  • Framework: Playwright
  • Config: playwright.config.ts — boots next dev on port 3100 by default (override with E2E_BASE_URL).
  • Tests live in e2e/*.spec.ts

What's covered:

File Specs
e2e/health.spec.ts GET /api/v1/health returns the right JSON shape — runs with no infra
e2e/agent-api.spec.ts Agent auth (401 without bearer, 200 with valid token) — skips unless E2E_AGENT_API_KEY is set
e2e/auth.spec.ts Login form: happy path + wrong-password — skips unless E2E_TEST_EMAIL + E2E_TEST_PASSWORD are set

The skip behaviour is intentional: with no env vars, the suite reports a clean 1 passed, N skipped so you can run it on a fresh checkout. Once you stand up PocketBase + seed a user, set:

export E2E_BASE_URL=http://localhost:3100
export E2E_POCKETBASE_URL=http://127.0.0.1:8090
export E2E_TEST_EMAIL=ci@example.com
export E2E_TEST_PASSWORD=correct-horse-battery-staple
export E2E_AGENT_API_KEY=$AGENT_API_KEY   # must match what next dev sees
npm run e2e

First-time setup: npm run e2e:install (downloads Chromium + OS deps).

Manual smoke-test checklist

For things that aren't (yet) covered by automated tests, walk through this against a running dev server + PocketBase:

  1. Health — curl http://localhost:3100/api/v1/health → { ok: true, service: "wfdash-api" }
  2. Auth — /login, sign in, get redirected into (app).
  3. Customers — /customers — CRUD, search, paginate, CSV export.
  4. Companies — /companies → /companies/[id] — CRUD + relation on linked quotes/orders.
  5. Suppliers — /suppliers — CRUD with tags; verify on /supplier-map.
  6. Supplier map — /supplier-map — clustering, dark tiles, fullscreen, marker edit persists coords.
  7. Quotes — /quotes — create, line items, claim, process, email metadata persists; approvals.
  8. Quote → Order (API) — POST /api/v1/orders/from-quote with { quote_id }; new order + copied order_items; quote status unchanged (regression from core-recheck-matrix.md).
  9. Orders — /orders/[id] — per-supplier POs, item + drv status updates.
  10. Order item status (API) — PATCH /api/v1/order-items/{id}/status; both drv_status and driver_status accepted.
  11. Collection pool — /collection-pool + GET /api/v1/collection-pool; assignment updates stick.
  12. Delivery notes — /delivery-notes — basic CRUD; no parity guarantees yet (see parity backlog).
  13. Repairs — /repairs — placeholder (not migrated, per parity backlog).
  14. MCP — POST /api/mcp with {"method":"tools/list"} returns 4 tools; tools/call works for each.
  15. Build & lint — npm run lint clean; npm run build completes without type errors.

Deployment

A multi-stage Dockerfile is included (Node 20 alpine, non-root nextjs user, standalone output). A nixpacks.toml pins the Node version for platforms like Railway/Render.

# Docker
docker build -t wfdash .
docker run --env-file .env.local -p 3000:3000 wfdash

# Nixpacks / Railway / Render — just push; nixpacks.toml sets Node 20.