# TANRAIL ONE — Phase 3 Core Workflows

## 1. Transaction Boundaries & Atomicity
Core business workflows require strict database transaction boundaries to prevent orphaned records or financial imbalances.

**Mandatory Transaction Boundaries (`DB::transaction`):**
- **Order Creation:** Creating `Order` + `OrderLine`s must succeed or fail together.
- **Invoice Generation:** Generating `Invoice` + linking to `Order` + locking `Order` state.
- **Payment Posting:** Creating `Payment` + updating `Invoice` balance + updating `Order` status.
- **Inventory Movement:** Creating `StockMovement` + updating `StockBalance` must be completely atomic.
- **Purchase Receiving:** Updating `Purchase` status + triggering Inventory Movement.

## 2. Sales Architecture Loop
A generic loop capable of supporting diverse business extensions.

1. **Initiation:** Customer is identified (or Walk-in).
2. **Drafting:** `Order` is created (`status = Draft`). `OrderLine`s are added.
3. **Commitment:** Order is `Confirmed`.
   - *Extension Hook:* Restaurant sends ticket to Kitchen. Catering schedules event.
4. **Fulfillment:** Service is rendered or physical goods handed over.
   - *Core Action:* Inventory is consumed (if applicable).
5. **Billing:** `Invoice` is generated.
6. **Settlement:** `Payment` is received against the Invoice. `Order` becomes `Completed`.

## 3. Procurement Architecture Loop
1. **Request:** `PurchaseOrder` drafted for a `Supplier`.
2. **Approval:** Internal RBAC approval (Optional based on BU rules).
3. **Receipt:** Goods arrive at `Branch`. 
   - *Core Action:* `StockMovement` records stock in. `StockBalance` increases.
4. **Billing:** Supplier bill is converted to an `Expense` or `Accounts Payable` record.
5. **Settlement:** `ExpensePayment` is issued to the Supplier.

## 4. Event Architecture
To maintain decoupled architecture between Core and Extensions, TANRAIL ONE will utilize **Application Events** (Laravel Event/Listener system).

**Core Domain Events:**
- `OrderConfirmed`
- `OrderFulfilled`
- `InvoiceIssued`
- `PaymentReceived`
- `StockThresholdReached`

**How Extensions Use Events:**
The Core does not know about the Restaurant's Kitchen. Instead:
1. Core fires `OrderConfirmed`.
2. `RestaurantExtension` listens to `OrderConfirmed`.
3. `RestaurantExtension` reads the `OrderLines` and generates KDS Tickets.

This guarantees the Dependency Rule: Extensions depend on Core; Core depends on nothing.
