# ANALYZE STOCK – Recipe-Based Stock Alert & Menu Impact

**Page:** `report-inv/stock-minus` (permission `viewAnalyzeStock`, unchanged)
**Data endpoint:** `ReportInvController::actionGetStockAlerts` (merged list). `get-minus-recommendations` is kept as is.
**Engine:** `common\services\RecipeStockAlertService` (read-only, built on `MenuAvailabilityService` snapshot)
**Tests:** `tests/unit/RecipeStockAlertServiceTest.php`

## 1. Scope: Main Kitchen only

Every list, quantity, minimum stock check and menu impact uses **one `subwarehouse` with `type = 'mainKitchen'`** (not deleted).
`general` subwarehouses are never summed in. Several main kitchens (e.g. other outlets) are analysed and shown separately
(one row per item + subwarehouse). Stock of other locations is only read to recommend a Transfer Stock.

* Movement-based analysis: `getMinusStockItems()` only scans `mainKitchen` subwarehouses (rows of other types are removed on the next *Analyze Stock*).
* Latest stock: latest `stok.latest_qty` row per (barang, subwarehouse), `stok.deleted_at IS NULL`.
* Minimum stock: `barang_stok.stok_alert` of (barang, outlet of the subwarehouse). Stored in the base unit like `latest_qty`.
  `NULL` => never Low Stock, no default, no hardcoded threshold.

## 2. Alert types

| Type | Source | Rule |
|---|---|---|
| Stock Minus | Movement (`stok_minus_analysis`, existing logic) | unchanged |
| Recipe Out of Stock | Recipe | raw material used by a menu, `latest_qty <= 0` (no stock row = 0) |
| Recipe Low Stock | Recipe | `0 < latest_qty < stok_alert` (equal is not low) |

Recipe alerts are base materials only (`jenis = Raw`, no recipe of its own) reached from active menus
(`menu -> linked_fg -> barang_recipe`, nested, circular safe, `stok_ignore` skipped). Menus with a missing, incomplete or circular
recipe are not analysed. One row per (barang, subwarehouse): if the item already has a Movement alert the rows are merged
(Source = *Movement + Recipe*, level/recommendation/action stay the existing ones), otherwise Source = *Recipe*.

## 3. Menu impact

Computed with the Menu Availability engine (same BOM, unit conversion and stock rules). Per menu that uses the item:

* **Blocked** - menu total potential is 0 and the item is on its bottleneck path
* **Limited** - menu can still be made (e.g. from WIP / Finish Goods stock) but the item caps its capacity
* **Not limiting** - used by the menu, but stock of WIP / FG or another component already covers it

An empty raw material therefore never marks a menu unavailable while WIP / FG stock still covers it. The system has no
alternative components, the actual recipe is used. *Affected Menus* in the summary = distinct menus Blocked or Limited.

## 4. Recommendation (recipe-only rows)

Reuses `StokMinusAnalysisService::analyzeMinusStock()` and `findSourceSubwarehousesForTransfer()`:
Transfer Stock Available (stock exists in another location; when `stok_alert` is set the shortage must be coverable, otherwise Purchase),
Consignment Required (`barang.is_consignment`), else Purchase Required. Supplier / price only when purchase history exists.
Actions are the existing Purchase Order / Transfer / Consignment Form links. Nothing is written when the list is built.
