# Routing

## Description

Routing defines the ordered manufacturing process used to build one produced item or item family. It owns operation sequencing, standard setup and run assumptions, work-center assignment, operator instructions, and the revision that production orders later freeze into executable work orders.

## Domain Model Definitions

### Model type

Stateful

#### State Transitions

```mermaid
stateDiagram-v2
    [*] --> Draft: createRouting
    Draft --> Draft: updateRouting
    Draft --> Active: activateRouting
    Active --> Inactive: deactivateRouting
```

| Operation | From | To | Command |
|-----------|------|----|---------|
| activate | DRAFT | ACTIVE | [activateRouting](../commands/ActivateRouting.md) |
| deactivate | ACTIVE | INACTIVE | [deactivateRouting](../commands/DeactivateRouting.md) |

### Command Definitions

- [createRouting](../commands/CreateRouting.md) - Create a draft routing for one manufactured item or scope.
- [updateRouting](../commands/UpdateRouting.md) - Revise operation order, instructions, and work-center assignments while mutable.
- [activateRouting](../commands/ActivateRouting.md) - Validate ordered operations and make the routing eligible for production-order release.
- [deactivateRouting](../commands/DeactivateRouting.md) - Remove the routing from future selection without mutating released snapshots.

### Query Definitions

- [getRouting](../queries/GetRouting.md) - Retrieve one routing revision with ordered operations.
- [listRoutingsByItem](../queries/ListRoutingsByItem.md) - List routing revisions available for one parent item and scope.

### Models

- Routing
- RoutingOperation

### Invariants

- Every routing belongs to one company and targets one manufactured item or item family within an optional site scope.
- The produced item must be ACTIVE and manufacturable when the routing is activated.
- A draft or active routing contains at least one operation with a unique sequence number inside the routing.
- Standard setup and run time values are always zero or greater.
- Each operation references a work center that is valid for the same company and effective scope.
- An active routing must not reference an inactive work center.
- Active routing operations may only reference ACTIVE work centers.
- Revisions for future orders may be created or activated without mutating any routing snapshot already frozen onto released production orders.
- Deterministic sequencing is part of the routing definition in the initial scope, so operations execute in one ordered path rather than parallel branches.

### Relationships

- **References Item (cross-module)**: the produced parent item is an [Item](../../../item-management/docs/models/Item.md).
- **Has Many RoutingOperations**: ordered step definitions live under the routing aggregate.
- **References WorkCenter**: each operation is assigned to one [WorkCenter](./WorkCenter.md).
- **Referenced By ProductionOrder**: [ProductionOrder](./ProductionOrder.md) snapshots one routing revision at release.
- **Materializes Into WorkOrder**: released operation snapshots become [WorkOrder](./WorkOrder.md) records.
