Commit graph

100 commits

Author SHA1 Message Date
DEls
7b8fe71db3 feat(plant-hours): GET /api/plant-hours/template route
Emits the canonical xlsx with a sample row derived from the first
vehicle in PocketBase. Falls back to a hardcoded sample if the
vehicles collection is empty.
2026-08-28 12:57:29 +02:00
DEls
c36eaeed51 fix(plant-hours-template): ESM-safe __dirname + freeze note
Two follow-up fixes from the Task 5 code-quality review:

- scripts/generate-plant-hours-template.ts uses
  fileURLToPath(import.meta.url) instead of __dirname. The
  project's tsconfig has module: esnext, so the moment the
  package flips to ESM the original would throw
  ReferenceError: __dirname is not defined.
- src/lib/plant-hours-template.ts documents that the frozen
  header row is a best-effort request — SheetJS Community
  Edition silently drops the !freeze property (only Pro emits
  frozen-pane XML). The function still produces the canonical
  13-column workbook, but the doc comment no longer claims a
  behaviour that doesn't happen.
2026-08-28 12:55:08 +02:00
DEls
70de32cd62 feat(fuel): Dismissed pill + Restore on vehicle detail page
Threaded attentionDismissedAt from the page mapper into the view.
When set, the header shows a pill with the timestamp and a Restore
button that DELETEs /api/vehicles/{id}/attention-dismiss and
refreshes the page. Errors render inline below the pill.

Slug-to-prose map for restore failures mirrors the dismiss-side
pattern from commit c633681 — RESTORE_ERROR_COPY at module scope,
friendly copy in window-less inline error slot.
2026-08-28 12:54:34 +02:00
DEls
e0e46b1c46 feat(lib): add partitionFixedCosts helper 2026-08-28 12:53:38 +02:00
DEls
c633681789 fix(fuel): review fixes for dismiss button
- Disable all dismiss buttons while any is in flight so rapid
  clicks on a second vehicle don't silently no-op.
- Map API error slugs (bad_id, not_found, update_failed) to
  user-facing copy so window.alert shows prose, not machine tags.
- Add aria-busy to the dismiss buttons.
- Drop redundant e.stopPropagation(); preventDefault() is
  sufficient to suppress the wrapping <Link>.
2026-08-28 12:52:29 +02:00
DEls
1e212a7339 feat(plant-hours): template workbook generator + seed file
buildPlantHoursTemplateWorkbook() emits the canonical 13-column xlsx
with a frozen header row, typed cells (date/time/number), a sample
row using the supplied vehicle, and a Notes column widened for
readability. Used by both /api/plant-hours/template and the seed
script that writes RAW_DOCS/Plant_Hours_Template_2.xlsx.
2026-08-28 12:51:14 +02:00
DEls
f629e4b369 test(partition-fixed-costs): add failing tests 2026-08-28 12:50:36 +02:00
DEls
3c379de5c4 test(plant-hours): cover unknown-QR deviation + value rounding
Adds two tests that lock in behaviour the previous test file relied on
implicitly:
- validatePlantHoursRow rejects when the supplied vehicle's qr_code_id
  doesn't match row.vehicle_qr (the guard the schema extends beyond
  the original spec)
- computePlantHoursFields rounds value to 2 decimal places (a
  fractional-input case the integer-only assertion didn't exercise)
2026-08-28 12:48:42 +02:00
DEls
4b5c93da37 feat(types): add is_recurring to FixedCostRecord 2026-08-28 12:47:40 +02:00
DEls
840c18cf6e feat(plant-hours): row validator + derived field helpers
Pure validator covering all the error/warning cases from the spec:
unknown QR, bad date range, end_hours < start_hours, bad time format,
missing cost (cell + vehicle), duplicate week, and the type-mismatch
warning. computePlantHoursFields() derives hours/value/over_budget/
hours_mismatch and the vehicle_unit_type_at_import snapshot.
2026-08-28 12:46:13 +02:00
DEls
29524b362d feat(fuel): Dismiss button on attention rows + optimistic overlay
Click on an AlertCard or AlertListRow to hide the vehicle from
'Attention required' instantly. POST /api/vehicles/{id}/attention-dismiss
stamps the field; optimistic state mirrors that locally so the row
disappears before the network roundtrip. Error path reverts the
overlay and shows window.alert (same pattern as inline-edit).
2026-08-28 12:45:59 +02:00
DEls
077dc35cf2 refactor(imports): extract shared import-parse helpers + add parseTime
Moves normHeader/resolveColumn/parseDate/toNumber out of fuel-import-parse
into a new src/lib/import-parse.ts so plant-hours can reuse them. Adds
parseTime (HH:MM, 24h, single-digit hour normalised). Adds the
PLANT_COL_ALIASES table for the plant-hours route. fuel-import-parse
becomes a thin re-export shim — the fuel-logs route and its existing
test file are unchanged.
2026-08-28 12:41:59 +02:00
DEls
4fe2870f42 feat(fuel): filter attention list through isAlertAcknowledged
Server mapper now emits attentionDismissedAt and maxFlaggedDate on
each VehicleSummary. The flaggedVehicles memo uses the new helper
to hide vehicles whose current flagged logs are all at-or-before
the user's most recent dismissal. Auto-undismiss is implicit: a
new fuel log with date_logged > attentionDismissedAt brings the
vehicle back on the next page load.
2026-08-28 12:39:45 +02:00
DEls
272e23b925 feat(types): VehicleRecord.unit_type + PlantHoursLogRecord
Adds UnitType union ("fuel" | "plant"), the new field on VehicleRecord,
the new PlantHoursLogRecord, and "plant_hours" to BatchType.
2026-08-28 12:37:28 +02:00
DEls
207a13244e feat(fuel): isAlertAcknowledged helper for dismiss filter
Pure predicate: returns true when a vehicle has been dismissed and
no flagged log is strictly newer than the dismissal. Six Vitest cases
covering each branch and the auto-undismiss boundary.
2026-08-28 12:35:36 +02:00
DEls
6cdcb4fa9f feat(fuel): POST/DELETE /api/vehicles/[id]/attention-dismiss
Sets or clears vehicles.attention_dismissed_at via pbAdmin().
15-char PB id pattern, 404 when PB reports the record is missing,
500 on any other write failure. Test mocks pbAdmin() so no live PB
needed.
2026-08-28 12:31:23 +02:00
DEls
3f1ef9dab7 feat(purchasing): collapse received items in DeliveryNoteCard
Wrap the received-items list in <details>/<summary>. Default
collapsed; click to expand. Hidden when dn.receivedItems is
empty (preserved). The 'Raw OCR data' <details> lower in the
same card is independent and unaffected.
2026-08-27 00:58:58 +02:00
DEls
caa5a1261b feat(purchasing): wire SupplierEditor into PO detail view
PoDetailView now mirrors its detail prop into local state and
passes a handleSupplierSaved callback that updates state.supplier
and triggers the existing refresh(). SupplierEditor replaces the
static Badge when the supplier is unknown.
2026-08-27 00:58:24 +02:00
DEls
c687af49d9 feat(purchasing): SupplierEditor component (not wired yet)
Badge-as-button + Input + Save/Cancel client component. Only
renders when isUnknownSupplier(currentSupplier) is true. Save
POSTs to /api/purchase-orders/{poId}/supplier and invokes
onSaved(next). Esc cancels; Enter saves when valid.

Not yet rendered — Task 3 wires it into PoDetailView.
2026-08-27 00:57:06 +02:00
DEls
b004121360 feat(purchasing): POST /api/purchase-orders/[id]/supplier route
Validates { supplier } (trimmed, 1-200 chars) and PATCHes the
purchase_orders.supplier field via pbAdmin(). 400/404/500 with
explicit error codes; mirrors the retry-route pattern.
2026-08-27 00:53:07 +02:00
DEls
9897f477b6 feat(purchasing): show 'Supplier unknown' badge when OCR couldn't extract supplier 2026-08-27 00:41:10 +02:00
DEls
e3c2e0396d refactor(purchasing): include orphan IDs in console.warn, single rollup pass 2026-08-27 00:15:39 +02:00
DEls
cb17760e5a fix(purchasing): use getFullList for received_items to avoid silent 500-row cap 2026-08-27 00:12:33 +02:00
DEls
b6fccac5a7 fix(po-detail): scope SlipPreview URL to correct PB collection 2026-08-27 00:10:45 +02:00
DEls
80a270d4be feat(po-detail): add View PO PDF button using existing SlipPreview 2026-08-27 00:07:28 +02:00
DEls
2f3505b5f8 fix(purchasing): DnStatusPill checks actual completed statuses (auto-verified/manual-verified) 2026-08-27 00:05:41 +02:00
DEls
85f3bdcbab feat(purchasing): amber treatment for POs/DNs with pending status 2026-08-27 00:01:41 +02:00
DEls
c9f3d61fc6 feat(purchasing): shorten upload button labels (Upload PO / Upload DN) 2026-08-26 23:59:21 +02:00
DEls
d2a4efa6e8 feat(needs-review): shrink modal to max-w-md for glance-and-pick flow 2026-08-26 23:26:26 +02:00
DEls
aa76470446 fix: pass recordId to SlipPreview from both callsites 2026-08-26 23:24:39 +02:00
DEls
ce3de2f9fb fix(slip-preview): build PB URL from recordId (was parsing filename) 2026-08-26 23:22:33 +02:00
DEls
0c2e092a3a feat(needs-review): compact modal with sticky header above slip 2026-08-26 23:10:05 +02:00
DEls
9ed7e55139 feat(purchasing): enhance purchasing page with received items tracking
Updated the PurchasingPage component to include received items data, allowing for accurate fulfillment percentage calculations based on authoritative data. The implementation now sums received quantities per purchase order item, replacing the previous denormalized field. Additionally, the needs-review delivery notes component has been improved with a modal for reviewing slips and assigning purchase orders, enhancing user interaction. The SlipPreview component has been integrated for better visual representation of delivery slips.
2026-08-26 23:05:05 +02:00
DEls
20d12f46d9 feat(slip-preview): render PDFs in an iframe instead of download-only 2026-08-26 23:03:41 +02:00
DEls
d645a39d9d feat(ocr-result): enhance PO validation and tier resolution logic
Updated the OCR result processing to include validation of matched PO items before determining the verification tier. This change ensures that if the local PO items fetch is empty but n8n supplied matched IDs, the tier will downgrade to draft, requiring user review. Additionally, the `resolveOcrTier` function has been modified to account for item verifiability, improving the accuracy of tier assignments based on confidence levels and PO resolution status. Unit tests have been added to cover these new scenarios.
2026-08-26 22:07:42 +02:00
DEls
d03df384ea feat(ocr-result, po-items): enhance PO matching logic with fuzzy search
Updated the OCR result processing to include a more robust PO matching mechanism. The new implementation prioritizes exact matches, followed by normalized comparisons, and finally employs a Levenshtein-based fuzzy match for improved accuracy. This change ensures that delivery notes are correctly linked to purchase orders, even when OCR introduces minor errors. Additionally, the behavior for handling confidence levels and PO resolution has been clarified in the documentation, enhancing overall processing reliability.
2026-08-26 21:39:59 +02:00
DEls
2fcd7550de feat(po-items): server-side Levenshtein fuzzy match against all POs
OCR is noisy. Instead of exact-then-fuzzy-fallback, always fetch
all ~15 POs from PocketBase, normalise both sides (strip non-
alphanumeric, lowercase), score each with Levenshtein similarity,
and return the best match above 0.75 threshold. Single-char
insert/delete (MEGCP09239 vs MEGCPO9239) scores ~0.9 — matches.
Completely different POs (similarity <0.5) don't match.

Plus the existing 'return 200 with empty items' fallback if no
match — keeps the workflow loop alive even when OCR is bad.
2026-08-26 18:32:54 +02:00
DEls
901d7f2841 fix(po-items): return 200 with empty items on PO not found
OCR sometimes mis-reads PO numbers (e.g. 'MEGCP09239' instead of
'MEGCPO9239') — when that happens the PO lookup 404'd, which
terminated the n8n workflow loop even with onError set on the
HTTP node.

Return 200 with {po: null, items: [], note: 'po_not_found'} instead.
The downstream 'Match items to PO' Code node already treats an
empty items array as 'no PO to match against' and leaves every
OCR item unmatched. The OCR result route then marks the DN as
needs-review and the user can fix the PO link manually.
2026-08-26 17:54:51 +02:00
DEls
32049a67cc fix(po-items): sanitise poNumber param (strip leading # from OCR)
OCR sometimes prefixes PO numbers with '#' from the slip header
(e.g. "#MEGCPO9165"). The workflow URL would otherwise break on
the # being a URL fragment separator, and the exact-match PB
filter would also fail (the fuzzy fallback worked, but a clean
sanitise is cheaper).
2026-08-26 17:23:21 +02:00
DEls
a129affe38 fix(po-items): use double cast for RecordModel -> typed po
TS strict-mode rejects direct cast from RecordModel because the
two types don't sufficiently overlap. The fix is the standard
'as unknown as' double-cast. Was previously caught only by the
Docker build's stricter typecheck, not by the local dev
typecheck (which was polluted by .next/dev/types stale cache).
2026-08-26 16:53:04 +02:00
DEls
187d2a44ee feat(po-items): add route for n8n to fetch PO line items by number
GET /api/purchase-orders/by-number/[poNumber]/items
- Auth via shared x-ingest-secret
- Returns { po: {id, purchase_order_number, supplier}, items: [{id, description, qty, qty_outstanding, unit}] }
- Exact lookup first, then fuzzy fallback (strip non-alphanumeric + lowercase)
- Used by the Delivery Note OCR workflow to enrich the LLM extraction
  with PO context so each line item can be linked back to a
  purchase_order_items row.
2026-08-26 16:41:55 +02:00
DEls
a100f742ac fix(ocr-result): use && not AND in batch filter
PB's filter parser only accepts && as the boolean AND operator; the
literal word AND returns HTTP 400 'Something went wrong while processing
your request'. The rest of the codebase already uses && (see
purchase-orders/upload/route.ts:81).

Caused updateBatchRow to silently no-op after every successful OCR
run — the batch row never transitioned to 'processed'.
2026-08-26 16:34:54 +02:00
DEls
1606bd2cc7 feat(delivery-notes): own batch lifecycle in OCR pipeline
- upload: create data_import_batches row when DN is uploaded
  (batch_type=delivery_notes, processed_record_id=<dnId>)
- ocr-result: mark the batch processed on success, failed/dead on
  failure (uses nextBatchState from batch-state.ts for retries)
- new /api/delivery-notes/[id]/slip proxy route: lets n8n fetch the
  slip file via shared secret instead of embedding the PocketBase
  admin JWT in workflow headers (which was leaking in the workflow JSON)

Backfill: existing pending DN ekko6xkhynoekas needs a manually
created data_import_batches row until it's reprocessed.
2026-08-26 15:50:44 +02:00
DEls
8dbf393446 feat(delivery-notes): enhance delivery note upload and processing
- Updated the delivery note upload API to include site ID for better project association.
- Modified the delivery note schema to support additional fields such as processing status, OCR confidence, and received by name.
- Enhanced the UI components for uploading delivery notes to allow file drop functionality and improved user feedback during file selection.
- Refactored purchasing page to integrate delivery notes that require review, providing better visibility and management of pending items.
- Added new KPIs to track active purchase orders and fulfillment percentages, improving overall dashboard insights.
2026-08-26 15:33:30 +02:00
DEls
2ace4365e6 feat(timesheets): enhance import functionality with site linking and employee creation
- Updated the timesheet import route to support optional metadata fields for site linking and auto-creation of missing employees.
- Enhanced parsing to detect multiple file formats, including Hills Casual Matrix and long-format CSV/XLSX.
- Modified response structure to include detected format and created employees in the JSON response.
- Improved UI components to allow project selection and options for creating new workers during import.
- Refactored import logic to ensure a seamless user experience when handling timesheet uploads.
2026-08-26 12:44:02 +02:00
DEls
f5c063765d feat(fuel-view): enhance inline editing and UI components
- Integrated inline editing functionality for vehicle descriptions, allowing users to edit directly within the FuelView.
- Added a Topbar with EditModeToggle and ImportFuelLogsButton for improved user interaction.
- Implemented EditModeBanner to provide feedback during editing sessions.
- Updated FleetMobileCard and FleetRow components to support inline editing, enhancing the overall user experience.
- Refactored FuelView to accept title and subtitle props for better contextual information.
2026-08-26 08:54:57 +02:00
DEls
2158538782 fix(employee-details): enhance detail drawer scrolling and layout
- Improved the layout of the detail drawer using flexbox for better responsiveness.
- Implemented a scrollable container for the timesheet table to prevent overflow.
- Ensured a clean and organized appearance for displaying employee details.
2026-08-25 23:38:35 +02:00
DEls
ac21f8a214 fix(employees-view): improve detail drawer layout and scrolling behavior
- Adjusted the DialogContent to use flex layout and set a maximum height for better responsiveness.
- Wrapped the timesheet table in a scrollable div to prevent overflow and enhance user experience.
- Ensured the layout maintains a clean and organized appearance while displaying employee details.
2026-08-25 22:47:26 +02:00
DEls
472be99fd8 feat(dashboard): add job start date to site records and enhance site management
- Introduced a new optional field `job_start_date` in the site schema to track project start dates.
- Updated the SitesPage component to utilize `job_start_date` for determining site status and fallback logic.
- Enhanced the SitesView component to display the new status "upcoming" for sites with future start dates.
- Modified the SiteDetailPage to include `job_start_date` in the site details and added an EditSiteButton for site management.
- Updated the site templates to include `job_start_date` for better pre-filling in the site creation form.
2026-08-25 19:01:56 +02:00
DEls
1b9779f47e feat(timesheets): enhance import functionality to support XLSX and CSV formats
- Updated the import route to accept both long-format CSV and Hills-style wide-format XLSX files.
- Implemented a new parsing mechanism that detects file type and processes accordingly.
- Enhanced error handling to provide more informative messages during file parsing failures.
- Modified the response structure to include total rows processed and detailed error reporting.
- Updated the UI components to reflect changes in file selection and user instructions for importing timesheets.
2026-08-25 18:38:02 +02:00