# Stock Restrict Source Location (Odoo 19 Stable) This module implements a robust, context-aware stock location restriction strategy for **Manufacturing Orders (MO)** and **Inventory Transfers**. It ensures that users only select component lots and quants from authorized warehouse zones while maintaining absolute stability for backend reservations and record saving. ## Key Features - **Dynamic Catalog Restriction**: When "Adding a line" in an MO, the stock catalog is automatically filtered to show only inventory from your permitted source locations (e.g., Packaging, Preparation, Production). - **Sub-location Support**: Uses the `child_of` operator to ensure that stock in all sub-shelves/aisles within an allowed zone is correctly visible. - **Null-Safe Guard**: Includes a critical backend safety patch for `stock.move.line` that prevents "Missing product_id" validation errors during the cleanup of temporary/virtual records. - **RPC Synchronized Loading**: Uses a high-performance Javascript pre-fetch mechanism to retrieve allowed zones from the server before loading the UI. - **Lot Dropdown Filtering**: Restricts the Lot dropdown selection to ensure only lots available in authorized zones are displayed. ## Dependencies - `stock` - `mrp` - `stock_picking_type_m2m` (Provides the `allowed_source_location_ids` configuration on Operation Types) ## Usage 1. **Configure**: On an **Operation Type** (e.g., WHBK/Manufacturing), set the **Allowed Source Locations** (e.g., Packaging, Preparation). 2. **Operation**: Create a Manufacturing Order using that Operation Type. 3. **Component Selection**: In the "Components" tab, click "Add a line" or use the magnifier icon. The catalog will only show stock from the configured zones. 4. **Saving**: Click "Save" on the component line. The system will process reservations normally without any validation errors. ## Technical Implementation (V19 Stable) ### Javascript (Frontend) - Patches the `SMLX2ManyField` component. - Executes an RPC call to `get_allowed_locations_for_mo` to fetch authorized zone IDs. - Injects a custom `domain` and `context` (reinforced with `default_product_id`) into the `selectCreate` modal. - Includes a **fail-safe fallback** that defaults to the standard location if the server is unreachable. ### Python (Backend) - **`StockMoveLine`**: Overrides `unlink()` to skip technical reservation updates for records missing a `product_id`. This prevents crashes during the disposal of temporary "virtual" records. - **`StockLot`**: Overrides `name_search()` and `web_search_read()` to ensure lot-only dropdowns are also restricted to allowed zones. - **`StockLocation`**: Provides a public `get_allowed_locations_for_mo` method for browser RPC accessibility. ## Logging & Debugging - All identification traces are logged at the **ERROR** level under the tag `DEBUG_RESTRICT`. This ensures visibility in remote server consoles.