# Site Sharing between sites (Phase 2) - setup and rules

## What it does
- A **sharing group** is a set of sites of ONE company. You choose two switches per group:
  - **Share open jobs** - a truck weighed at one site (1st weight) can be weighed again and completed at another site of the group.
  - **Share reports** - the sites of the group see each other's transactions in Reports (and can view their tickets).
- A site that is in **no group** sees only its own data - exactly as before. Nothing changes until you create a group.
- Data is **never shared between different companies**.
- A site that actually **weighed** a job can always see it, whatever the settings say. Deleting a group never hides history from the sites that handled a truck.

## Who sees what
- **Allowed sites of a login** (Users page): ISVAdmin all, Company Admin all of its company, Supervisor/Operator the sites ticked on the user. Unchanged.
- **What a login may do** at those sites comes from **Role Permissions** (weigh = New Transaction / Pending; view = Reports). A "reports only" login = a role
  with Reports switched on and New Transaction / Pending switched off. Tip: with sharing on, switch **Reports** off for roles that should not see other sites' data.
- **Reports** show the jobs of the login's allowed sites, plus the sites shared with them (groups with "Share reports"), plus any job their sites weighed.
  The site filter lists those sites; filtering "Site B" shows the jobs started at B or weighed at B.
- **Pending / continuing a truck** at a site: that site's open jobs, plus open jobs of its group when "Share open jobs" is on.
  The same truck cannot get a second open ticket at a sister site while the first is pending.
- **Editing / voiding** a completed transaction: only the site that started it or weighed it. Seeing a job in a shared report is not the right to change it.

## What happens when a truck is finished at another site
- The weighing page shows "started at <site>" and offers the **weighbridges and cameras of the site you are working at**.
- Each weighing records the weighbridge (as in Phase 1); photos are taken by the cameras of the site that weighed.
- The ticket keeps the number of the site that started it. The audit log records "weighed at site X" and "completed at site X".
- In Reports the Site column shows the starting site and, for a job weighed at several sites, "weighed at A, B".

## Install, in this order
1. **Database** - run `sql/migrate_section18_site_sharing.sql` once in phpMyAdmin (after migration 17). Existing transactions are filled in automatically.
   (If you upload the web files first, everything keeps working as before; the Site Sharing page then asks you to run the SQL.)
2. **Web files** - upload the files of the patch zip to the same folders under `public_html/webapp/`.
3. **Create a group** - Administration -> Site Sharing (ISVAdmin, or the Company Admin for their own company): name, tick two or more sites, choose the two switches, Create.
4. No agent change is needed (the agent from Phase 1 is used as it is).

## Quick checks
- Start a truck at Site A, open Pending at Site B: it is listed "Started at Site A". Continue it, weigh on Site B's weighbridge, complete.
- Reports at Site B show both jobs; a site outside the group shows neither.
- Turn off "Share open jobs": Site B's Pending no longer lists Site A's open trucks. Turn off "Share reports": Site B's Reports no longer include Site A's own jobs.
