# ReleaseProductionOrder

## Permission Scope

productionOrder

## Overview

ReleaseProductionOrder turns a draft plan into executable work by resolving active BOM and routing content, freezing snapshots, creating work orders and material requirements, and opening manufacturing cost collection.

## Business Rules

- Target order must exist and be in `DRAFT`.
- An active BOM and active routing must be explicitly selected or resolvable by default.
- BOM and routing must belong to the same company as the order, and any site-scoped master must match the order site exactly.
- Planned material unit cost for every required component must be resolved from inventory-owned valuation data at release time.
- Release snapshots BOM lines, routing operations, work-center rates, overhead policy, and planned material valuation.
- Release creates work orders, material requirements, and one manufacturing cost summary in `COLLECTING`.

## Process Flow

```mermaid
flowchart TD
    A[Receive release request] --> B{Order exists and is DRAFT?}
    B -->|No| C[Return PRODUCTION_ORDER_NOT_RELEASABLE]
    B -->|Yes| D[Resolve active BOM and routing]
    D --> E[Validate company and site scope of resolved masters]
    E --> F[Query inventory valuation for planned material cost]
    F --> G[Snapshot BOM, routing, and cost baseline]
    G --> H[Create work orders and material requirements]
    H --> I[Set order status to RELEASED]
    I --> J[Return released order]
```

## External Dependencies

- [BillOfMaterial](../model/BillOfMaterial.md) - One active BOM version must be selected or resolved.
- [Routing](../model/Routing.md) - One active routing revision must be selected or resolved.
- [ManufacturingCostSummary](../model/ManufacturingCostSummary.md) - Release opens cost collection and freezes the baseline.

## Error Scenarios

- **PRODUCTION_ORDER_NOT_FOUND**: Referenced production order does not exist
- **PRODUCTION_ORDER_NOT_RELEASABLE**: The order is not in `DRAFT`.
- **BOM_NOT_RESOLVED**: No valid active BOM could be selected for the order scope.
- **ROUTING_NOT_RESOLVED**: No valid active routing could be selected for the order scope.
- **CROSS_COMPANY_MASTER_REFERENCE**: Selected BOM or routing belongs to another company.
- **CROSS_SITE_MASTER_REFERENCE**: Selected BOM or routing belongs to another site than the production order, even if the company matches.
- **PLANNED_MATERIAL_COST_UNAVAILABLE**: Inventory could not resolve planned component cost for release.

## Test Cases

- releases a draft order with valid BOM and routing snapshots
- returns error when the order does not exist
- returns error when the order is not in `DRAFT`
- returns error when no active BOM can be resolved
- returns error when no active routing can be resolved
- returns error when BOM or routing belongs to another company
- returns error when BOM or routing belongs to another site in the same company
- returns error when inventory valuation for a required component is unavailable
- creates work orders and a collecting cost summary at release
