NWCG GARAGE SYSTEM — UPDATE 30 AUGUST 2026  (revision C)
==========================================================================

*** START HERE IF ANYTHING IS BROKEN AFTER AN EARLIER UPLOAD. ***

Revision C fixes two faults in the earlier bundles:

  1. DASHBOARD STOPPED HALF WAY DOWN
     9 files were missing. The bundle only had files changed on 30 Aug, but
     your server had never received the 29 Aug batch either, so the new
     dashboard.php called includes/collection-risk.php which was not there.

  2. MESSAGING PORTAL — ERROR 500
     The messaging portal needs columns on the "enquiries" table (archived,
     archived_at, archived_by, auto_archived, deleted_at, deleted_by,
     converted_to_deal_id). There was no migration for them, so the page
     died as soon as it queried them.

--------------------------------------------------------------------------
DO THIS
--------------------------------------------------------------------------

1. Upload everything inside "files/" over the live site, keeping folders.
   Safe to upload over the top of any earlier revision.

2. Log in as an admin and browse to:
       https://garage.northwestcar.group/db-upgrade.php

   This ADDS anything your database is missing and reports what it did.
   It checks before every change, so it is safe to run more than once and
   will never touch what is already there. Nothing is dropped, no data is
   changed.

   It replaces running the two .sql files by hand. If you already ran them,
   run this anyway — it will say "already there" for those and add the
   messaging columns you are missing.

3. Then browse to:
       https://garage.northwestcar.group/check-install.php

   When both say they are happy, DELETE db-upgrade.php AND
   check-install.php from the server.

4. Set up the crons — see CRON-JOBS.txt.

--------------------------------------------------------------------------

WHAT IS IN HERE
  files/        every changed and new file, in the same folder layout as the
                server. Upload the CONTENTS of "files" into your site root so
                the folders line up (includes/ into includes/, ajax/ into ajax/,
                and so on).
  migrations/   inside files/ — two SQL files to run once against the database.


--------------------------------------------------------------------------
ORDER TO DO IT IN
--------------------------------------------------------------------------

1. BACK UP FIRST
   - cPanel > Backup > download a full backup, or at minimum:
       * a database export (phpMyAdmin > Export > Quick > Go)
       * a zip of the current site folder
   Do not skip this. Some of these files are large rewrites.

2. UPLOAD THE FILES
   Upload everything inside "files/" over the top of the live site, keeping
   the folder structure. Overwrite when asked.

3. RUN THE TWO MIGRATIONS
   phpMyAdmin > select the garage database > SQL tab > paste and run, in order:
       migrations/2026-08-30-finance-payouts.sql
       migrations/2026-08-30-finance-commission.sql
   Both only ADD columns. Nothing is dropped or overwritten.
   If a migration says a column already exists, that part has already been run
   and you can move on.

4. CHECK IT
   Log in as an admin and browse to:
       https://garage.northwestcar.group/check-install.php
   It confirms every column, file and permission this release needs.
   When it says "All checks passed", DELETE check-install.php from the server.

5. SET UP THE CRONS — see CRON-JOBS.txt in this bundle.


--------------------------------------------------------------------------
WHAT CHANGED
--------------------------------------------------------------------------

PDI SHEET (pdi-sheet.php, includes/pdi-checklist.php)
  - Rebuilt to The Motoring Organisation inspection sheet: 12 sections, all
    80 checks, in the TMO layout with the red section bars.
  - Pass / Advisory / Fail tick boxes, plus Yes / N-A on Documentation,
    OK / Replaced on brake pads and discs, and mm/pressure/size boxes on tyres.
  - A new sheet opens pre-ticked as Pass; tick a box again to clear it.
  - TMO header block (Vehicle Model, Reg No, Mileage, Date, No) auto-filled
    from the deal.
  - TMO disclaimer and Buy With Confidence badge footer with signature lines.
  - Prints on ONE A4 page and scales itself to fill it.
  - Print Preview available before the sheet is signed; an unsigned sheet
    prints with an "Incomplete" band so it cannot pass as a finished one.

  NOTE: sheets filled in before this update used the old in-house checklist.
  Their data is still in the database but will not show against the TMO list.
  This was deliberate ("just do from today's").

FINANCE (finance-payouts.php, includes/finance.php, handover.php)
  - New Finance Payouts page, linked directly under Dashboard.
  - Tracks: finance amount, lender, agreement no., PAID OUT (+date),
    PAID TO US (+date), COMMISSION (+ Comms Paid, + monthly total).
  - A car CANNOT BE HANDED OVER until the payout is confirmed. Money not yet
    in our account does not block it. Admins can still override; overrides are
    logged as before.
  - Finance Amount and Finance Comms Amount on both Create Deal and Edit Deal,
    carrying through to the payouts page.
  - Dashboard panel showing anything awaiting payout, plus the month's
    commission total. Blocked cars pulse red.
  - Salesmen: can see and CONFIRM a payout, and see payout status on their own
    dashboard and on each deal. They cannot see or change paid-to-us,
    commission, or the lender.
  - Every payout / receipt / commission change is written to the activity log
    with who did it.

  NOTE: finance tracking starts 30 Aug 2026. The 39 older finance deals were
  all handed over long ago and are excluded so they do not swamp the list.
  They are still visible on the "All finance deals" tab. To include them,
  change FIN_TRACK_FROM in includes/finance.php.

EDIT DEAL
  - FIXED: a field labelled "Finance Comms Amount" was actually bound to the
    finance AMOUNT, so it showed the wrong number and there was no commission
    field at all. Now two separate fields.

DASHBOARD
  - Internal messages chip next to the unread emails chip.
  - Finance panel (above).
  - Weekly summary using NWCG week blocks (1-7, 8-14, 15-21, 22-end) compared
    with the same week of last month.
  - Ready for Handover moved to the top.

ALSO INCLUDED (earlier in the same session)
  - Mail: Outlook-style reading pane, attachments, signature editor.
  - vehicles.php tidy-up, key-number warnings, key numbers syncing to deals.
  - MOT dashboard changes and MOT auto sign-off after 6 months.
  - Cancellation notice chasing, cooling-off countdown, dashboard panel.
  - Web push notifications for parts authorisations.
  - PDI Completed page; customer PDI signature required before handover.
  - Messaging portal bulk actions and 30-day auto-archive.
  - Enquiry-to-deal conversion tracking.


--------------------------------------------------------------------------
IF SOMETHING GOES WRONG
--------------------------------------------------------------------------
Restore the backup from step 1. The two migrations only add columns, so the
old files work fine against the new database — you can roll the files back on
their own without touching the database.
