# ProductionOrder

## Description

ProductionOrder is the manufacturing control document that turns one planning decision into executable shop-floor work. It owns the produced item, planned quantity and dates, company or site scope, released snapshots of BOM and routing content, execution rollup from work orders, and the lifecycle that governs release, completion, technical completion, reopening, and administrative close.

## Domain Model Definitions

### Model type

Stateful

#### State Transitions

```mermaid
stateDiagram-v2
    [*] --> DRAFT: createProductionOrder
    DRAFT --> DRAFT: updateProductionOrder
    DRAFT --> RELEASED: releaseProductionOrder
    DRAFT --> CANCELLED: cancelProductionOrder
    RELEASED --> DRAFT: unreleaseProductionOrder
    RELEASED --> RELEASED: rescheduleProductionOrder
    RELEASED --> IN_PROGRESS: startWorkOrder
    RELEASED --> CANCELLED: cancelProductionOrder
    IN_PROGRESS --> IN_PROGRESS: reportWorkOrderProgress
    IN_PROGRESS --> COMPLETED: completeProductionOrder
    COMPLETED --> TECHNICALLY_COMPLETE: technicallyCompleteProductionOrder
    TECHNICALLY_COMPLETE --> IN_PROGRESS: reopenProductionOrder
    TECHNICALLY_COMPLETE --> CLOSED: closeProductionOrder
```

| Operation | From | To | Command |
|-----------|------|----|---------|
| release | DRAFT | RELEASED | [releaseProductionOrder](../commands/ReleaseProductionOrder.md) |
| cancel | DRAFT, RELEASED | CANCELLED | [cancelProductionOrder](../commands/CancelProductionOrder.md) |
| unrelease | RELEASED | DRAFT | [unreleaseProductionOrder](../commands/UnreleaseProductionOrder.md) |
| startWorkOrder | RELEASED | IN_PROGRESS | [startWorkOrder](../commands/StartWorkOrder.md) |
| complete | IN_PROGRESS | COMPLETED | [completeProductionOrder](../commands/CompleteProductionOrder.md) |
| technicallyComplete | COMPLETED | TECHNICALLY_COMPLETE | [technicallyCompleteProductionOrder](../commands/TechnicallyCompleteProductionOrder.md) |
| reopen | TECHNICALLY_COMPLETE | IN_PROGRESS | [reopenProductionOrder](../commands/ReopenProductionOrder.md) |
| close | TECHNICALLY_COMPLETE | CLOSED | [closeProductionOrder](../commands/CloseProductionOrder.md) |

### Command Definitions

- [createProductionOrder](../commands/CreateProductionOrder.md) - Create a draft production request with planning data.
- [updateProductionOrder](../commands/UpdateProductionOrder.md) - Revise mutable draft planning fields before release.
- [releaseProductionOrder](../commands/ReleaseProductionOrder.md) - Freeze BOM and routing, create work orders, and open execution.
- [unreleaseProductionOrder](../commands/UnreleaseProductionOrder.md) - Return a released order to draft when no execution evidence exists.
- [rescheduleProductionOrder](../commands/RescheduleProductionOrder.md) - Move planned dates on a released order before execution starts.
- [cancelProductionOrder](../commands/CancelProductionOrder.md) - Abandon the order before meaningful execution makes cancellation invalid.
- [completeProductionOrder](../commands/CompleteProductionOrder.md) - Mark physical production complete after final reporting and receipt handoff.
- [technicallyCompleteProductionOrder](../commands/TechnicallyCompleteProductionOrder.md) - Freeze execution and move the linked cost summary into review.
- [reopenProductionOrder](../commands/ReopenProductionOrder.md) - Re-enable controlled execution after a technically complete or reviewed state.
- [closeProductionOrder](../commands/CloseProductionOrder.md) - Perform final administrative close once the linked cost summary is settled.

### Query Definitions

- [getProductionOrder](../queries/GetProductionOrder.md) - Retrieve one production order with snapshots, progress, and closeout status.
- [listProductionOrdersByStatus](../queries/ListProductionOrdersByStatus.md) - List production orders for planning, execution, or closeout queues.

### Models

- ProductionOrder
- ProductionOrderMaterialRequirement
- ProductionOrderBomSnapshot
- ProductionOrderRoutingSnapshot
- ProductionOrderCostBaseline

### Invariants

- Every production order belongs to exactly one company and one execution site.
- The ordered item must be an ACTIVE manufacturable item and planned quantity must remain greater than zero.
- BOM and routing references selected at release must belong to the same company and site scope as the production order.
- Orders in `RELEASED`, `IN_PROGRESS`, `COMPLETED`, `TECHNICALLY_COMPLETE`, or `CLOSED` always carry immutable BOM and routing snapshots.
- `CANCELLED` and `CLOSED` orders reject further shop-floor execution updates.
- Entering `COMPLETED` requires the final output-reporting and receipt-handoff evidence required by policy.
- Entering `TECHNICALLY_COMPLETE` requires that no additional execution, rescheduling, or material issue is still expected.
- Entering `CLOSED` requires the linked [ManufacturingCostSummary](./ManufacturingCostSummary.md) to be in `SETTLED`.

### Relationships

- **References Item (cross-module)**: the produced item is an [Item](../../../item-management/docs/models/Item.md).
- **Belongs To Company and Site**: execution scope is defined by [Company](../../../organization/docs/models/Company.md) and [Site](../../../organization/docs/models/Site.md).
- **Snapshots BillOfMaterial**: release freezes one [BillOfMaterial](./BillOfMaterial.md) version and exploded component requirements.
- **Snapshots Routing**: release freezes one [Routing](./Routing.md) revision and operation plan.
- **Has Many WorkOrders**: released routing operations materialize into [WorkOrder](./WorkOrder.md) records.
- **Owns One ManufacturingCostSummary**: cost collection and variance review are tracked in [ManufacturingCostSummary](./ManufacturingCostSummary.md).
