# ReopenProductionOrder

## Permission Scope

productionOrder

## Overview

ReopenProductionOrder re-enables execution after a technically complete order needs more shop-floor work. It reverses the execution freeze and returns the linked cost summary to active collection.

## Business Rules

- Target order must exist and be in `TECHNICALLY_COMPLETE`.
- A documented reopen reason is required.
- Reopening is not allowed after the order has been administratively closed.
- Reopening returns the linked manufacturing cost summary from either `PENDING_VARIANCE_REVIEW` or `VARIANCE_REVIEWED` back to `COLLECTING`.
- A linked summary already in `SETTLED` is not reopenable through this workflow.
- Once reopened, execution resumes under the existing released snapshots rather than creating a new release.

## Process Flow

```mermaid
flowchart TD
    A[Receive reopen request] --> B{Order exists and is TECHNICALLY_COMPLETE?}
    B -->|No| C[Return PRODUCTION_ORDER_NOT_REOPENABLE]
    B -->|Yes| D{Reopen reason provided?}
    D -->|No| E[Return REOPEN_REASON_REQUIRED]
    D -->|Yes| F[Resolve linked cost summary state]
    F --> G{Summary pending review or variance reviewed?}
    G -->|No| H[Return COST_SUMMARY_NOT_REOPENABLE]
    G -->|Yes| I[Return cost summary to COLLECTING]
    I --> J[Set order status to IN_PROGRESS]
    J --> K[Return reopened order]
```

## External Dependencies

- [ManufacturingCostSummary](../model/ManufacturingCostSummary.md) - Review state is reversed so additional execution can be costed.

## Error Scenarios

- **PRODUCTION_ORDER_NOT_FOUND**: Referenced production order does not exist
- **PRODUCTION_ORDER_NOT_REOPENABLE**: The order is not in `TECHNICALLY_COMPLETE`.
- **REOPEN_REASON_REQUIRED**: No reopen reason was provided.
- **COST_SUMMARY_NOT_REOPENABLE**: The linked cost summary is not in `PENDING_VARIANCE_REVIEW` or `VARIANCE_REVIEWED`.
- **ORDER_ALREADY_CLOSED**: Administratively closed orders cannot be reopened through this workflow.

## Test Cases

- reopens a technically complete order for additional execution
- returns error when the order does not exist
- returns error when the order is not technically complete
- returns error when no reopen reason is provided
- reopens an order when the linked cost summary is `PENDING_VARIANCE_REVIEW`
- reopens an order when the linked cost summary is `VARIANCE_REVIEWED`
- returns error when the linked cost summary is already `SETTLED` or otherwise not reopenable
- returns error when the order is already closed
- returns the linked cost summary to collecting on reopen
