# TANRAIL ONE — Core Sales Implementation Plan

## 1. Phase A: Core Models and Migrations
- Create migrations for `orders`, `order_lines`, and `payments`.
- Create Eloquent Models: `Order`, `OrderLine`, `Payment`.
- Create `OrderScope` to restrict data by Active Business Unit Context.
- Enforce foreign keys and decimal precision (`14, 4`).

## 2. Phase B: Policies and Observers
- Create `OrderPolicy` enforcing Tripartite Phase 2 authorization.
- Create Observers or `booted` events on `OrderLine` to block updates/deletes when `Order->status !== 'DRAFT'`.

## 3. Phase C: The Sales Engine (OrderService)
Create the Core engine responsible for mutating Orders.
- `createDraft()`
- `addItem()` (Verifies cross-BU isolation rules).
- `confirmOrder()` (Fires `OrderConfirmed` event inside DB transaction).
- `cancelOrder()` (Fires `OrderCancelled` event inside DB transaction).

## 4. Phase D: Inventory Integration
- Create `DeductInventoryListener` and `RestoreInventoryListener`.
- Map them to the Events in `EventServiceProvider`.
- Ensure they synchronously call `InventoryService`.

## 5. Phase E: Testing Strategy
- Write cross-BU isolation tests (ensure an Item from BU A cannot be added to an Order in BU B).
- Write concurrent confirmation tests (two requests confirming the same order).
- Write negative stock integration tests (confirming an order that exceeds stock must rollback the order state to DRAFT).
