# UnreleaseProductionOrder

## Permission Scope

productionOrder

## Overview

UnreleaseProductionOrder returns a released order to `DRAFT` when execution has not meaningfully started. It removes the executable release artifacts so the planner can safely revise the order.

## Business Rules

- Target order must exist and be in `RELEASED`.
- No work order may have started or received execution evidence.
- No irreversible inventory handoff may exist for the order.
- Unrelease removes or supersedes work orders, material requirements, and the unrevised release snapshot.
- Orders with actual material or execution evidence must use controlled reopening instead of unrelease.

## Process Flow

```mermaid
flowchart TD
    A[Receive unrelease request] --> B{Order exists and is RELEASED?}
    B -->|No| C[Return PRODUCTION_ORDER_NOT_UNRELEASABLE]
    B -->|Yes| D{Any execution or inventory evidence?}
    D -->|Yes| E[Return EXECUTION_ALREADY_STARTED]
    D -->|No| F[Remove release artifacts]
    F --> G[Set status to DRAFT]
    G --> H[Return draft order]
```

## External Dependencies

- [WorkOrder](../model/WorkOrder.md) - Unrelease is blocked once execution evidence exists on any child work order.

## Error Scenarios

- **PRODUCTION_ORDER_NOT_FOUND**: Referenced production order does not exist
- **PRODUCTION_ORDER_NOT_UNRELEASABLE**: The order is not in `RELEASED`.
- **EXECUTION_ALREADY_STARTED**: Work-order execution evidence already exists for this production order
- **INVENTORY_HANDOFF_EXISTS**: Inventory issue or receipt evidence already exists for this production order

## Test Cases

- unreleases a released order with no execution evidence
- returns error when the order does not exist
- returns error when the order is not in `RELEASED`
- returns error when a work order has already started
- returns error when inventory handoff evidence already exists
- removes work orders and material requirements when unrelease succeeds
