# AI context — Absher operations dashboard

Repo: `C:\Users\lenovo\Projects\absher-dashboard`  
Full human guide: `GUIDE.md`  
Spec PDF: `C:\Users\lenovo\Downloads\Functional Specifications - Dashboard (English, with diagrams).pdf`  
Local only. Next.js **16.3.5** App Router. Read `node_modules/next/dist/docs/` before coding.

## Run

```
npm install && npx prisma generate && npx prisma db push && npm run db:seed && npm run dev
```

http://localhost:3000 — `admin@absher.local` / `absher123`

On Windows, stop `next dev` before `prisma generate` (DLL lock → EPERM). Stale client + `include: { loads: true }` on `DeliveryRound` 500s `/fleet`.

## Architecture

- SQLite Prisma (`prisma/schema.prisma`, `prisma/dev.db`)
- JWT cookie `absher_session` (`src/lib/auth.ts`)
- Gate: `src/proxy.ts` (not middleware)
- Mutations: `src/actions/*.ts` + `actor()` from `src/actions/guard.ts`
- Pages: `src/app/(dash)/...` ; print: `src/app/print/` ; CSV: `src/app/api/export/`
- `params` / `searchParams` are Promises

## Non-negotiable domain

- **Drivers (`DeliveryAgent`) have no login.** Cards only: name, phone, region, address, lat/lng, truck.
- **Two `DeliveryRound.kind` values:**
  - `DELIVERY` — assign existing orders as stops
  - `PROSPECTING` — empty start; `loadTruck` from warehouse; `createFieldOrder` with `FULL_PAID` | `PART_PAID` | `ORDER`; `fromTruck`; `returnTruckStock`
- Money: DZD. Company settings row id = `"company"`.
- Permissions JSON on `Role`. Super Admin bypasses.

## Added since v1

- UI in English or Arabic (RTL) + light/dark theme — `src/lib/i18n/`, `src/lib/theme.ts`, `src/actions/locale.ts`. Print pages always English.
- Price per account type + min/max quantity per product and account type (`PriceList.minQty/maxQty`), enforced in `unitPriceFor()`.
- Edits/deletes require a reason: `ChangeForm` client component → `motif` → `actor(dept, op, formData)` → `ActivityLog.motif`.
- Special permission `editValidatedInvoices` (JSON `special` on the role); Super Admin has it.
- Late payers from `src/lib/receivables.ts` (15 days after the delivery note). Offers removed.
- Stock ledger: always change stock with `moveStock()` (`src/lib/stock.ts`). Suppliers + purchases in `src/actions/purchases.ts` (department `purchases`). Delivery-note validation = SALE.
- Page access: `src/lib/access.ts` + gate in `src/proxy.ts` (typed URLs are refused, not just hidden in the nav).
- Customer location picked on a Leaflet map (`src/components/location-picker.tsx`).
- Rounds: `recordAtStop` records sold quantities + extra products + payment in one step.
- Responsive: sidebar `lg:` only, drawer below; role-aware UI via `pageAccess()`.
- Barcodes: unique `Product.barcode`, read by keyboard-wedge scanners (no driver) through `watchScanner` in `src/lib/scanner.ts`; works wherever the cursor is, never submits the form. Scanning on `/store` adds the product to the basket. Packaging: `SINGLE` or `CARTON` + `unitsPerCarton`, informational — stock is counted in what you sell.
- Products are created with no stock; stock comes in from a received purchase (full circle: product → achat → stock).
- `/guide`: in-app user guide, both languages side by side (`src/lib/guide.ts`), searched across EN+AR, visible to every role.
- Forms are popups (`FormDialog`), triggered from the page header; pages show data only.
- Internal store: POS, receipts, expenses, shelf stock (`src/actions/store.ts`, department `store`). Shelf is fed by STORE_TRANSFER from the warehouse.
- Product photos upload to `uploads/` (served by `/api/uploads/...`).

## Key files

| Need | File |
| --- | --- |
| Schema | `prisma/schema.prisma` |
| Seed | `prisma/seed.ts` |
| Nav | `src/components/shell.tsx` |
| Rounds | `src/actions/rounds.ts`, `src/app/(dash)/rounds/` |
| Drivers | `src/actions/fleet.ts`, `src/app/(dash)/fleet/`, `src/lib/driver-activity.ts` |
| Orders | `src/actions/orders.ts` |
| Prices | `src/lib/pricing.ts` |
| Docs | `src/lib/documents.ts` |

## Not in this repo

No Absher mobile app, no live Absher admin API, no deploy, print-as-PDF only, CSV not xlsx.
