# P1 patch 2 — Warehouse foundation

Date: 2026-10-19
Environment: Primary ERP development installation
Status: Applied; migration completed successfully.

## Delivered
- Warehouses owned by one company, optionally linked to one branch.
- Composite foreign key rejects branches from another company at database level.
- Unique warehouse code within each company.
- Warehouse create, edit, activate/deactivate through the Warehouses page.
- Explicit user grants, with grant and revoke administration.
- New permissions: warehouses.view and warehouses.manage.
- Company-wide warehouse management permission controls location administration.
- Grant administration additionally requires access.manage.
- Ordinary readers need both warehouses.view and a warehouse grant.
- A branch-scoped reader cannot use a grant to read warehouses in another branch.
- Membership revocation, warehouse deactivation and grant revocation are checked on reads.
- Company owner and system administrator retain privileged administration.
- Create, edit, grant and revoke operations use database transactions and audit events.
- No warehouse deletion endpoint; historical identities are retained.
- Sidebar entry added, scoped to the active company's permissions.
- Fixed a remaining first-company lookup in the Roles & Permissions navigation entry.

## Database
Applied migration: 2026_10_19_010000_create_warehouse_foundation.
New tables: warehouses, warehouse_user.
New index: branches_id_company_unique.
Existing rows were not deleted or rewritten.

## Validation
17 tests, 79 assertions pass using SQLite in memory.
Warehouse-specific checks cover create/update and audit, cross-company isolation,
composite FK rejection, explicit grants, immediate revocation, duplicate grants,
missing permission, inactive warehouses and branch scope.
The migration also ran successfully on the configured development database.

## Backup
Before changes:
- /home/snyt/backups/snyt-erp/2026-10-19-p0/before-warehouses-files.tar.gz
- /home/snyt/backups/snyt-erp/2026-10-19-p0/before-warehouses.sql
Database snapshot SHA256:
7fbe97b53d9387353c0057f718a82f1e921a402b127bc871bdc6c8b6fb5d4618

## Rollback and next steps
Prefer a corrective forward migration if records exist.
The down migration removes warehouse data and must not run without explicit authorization.
Full restore rehearsal and visual browser QA remain pending.
No inventory ledger, stock quantities, receiving or transfers are implemented by this patch.
Stock-operation authorization must call the warehouse access layer before posting.
P1 is still incomplete: reusable policy layer, full row scopes, account/session revocation,
approval limits, audit hardening and API foundation remain.
No Git push was made.
