# TANRAIL ONE — Phase 3.2 Inventory Implementation Plan

## 1. Pre-Requisites
- Await Human Decision regarding the Negative Stock Policy configuration.

## 2. Stage 3.2.1 — Location & Balance Foundation
1. Create `InventoryLocation` Model and Migration (Scoped to `branch_id`).
2. Create `StockBalance` Model and Migration.
3. Apply `BusinessUnitScope` globally to both utilizing Phase 2 ContextManager.
4. Implement `InventoryLocationPolicy`.

## 3. Stage 3.2.2 — The Immutable Ledger
1. Create `StockMovement` Model and Migration.
2. Define Polymorphic relationships (`reference_type`, `reference_id`) for linking to external triggers.
3. Apply Security Scopes and Policies.

## 4. Stage 3.2.3 — Inventory Services (The Engine)
1. Create `InventoryService` class to encapsulate all stock logic.
2. Implement pessimistic locking (`lockForUpdate`) around `StockBalance` mutations.
3. Implement `receive()`, `issue()`, `transfer()`, and `adjust()` methods.
4. Ensure `StockMovement` creation and `StockBalance` updates happen within strict `DB::transaction()` closures.

## 5. Stage 3.2.4 — Verification & Security Gate
1. Author `InventoryArchitectureTest` suite.
2. Test concurrency race conditions (Simulating two simultaneous issues).
3. Test immutability constraints.
4. Test cross-BU isolation.
