# CloseProductionOrder

## Permission Scope

productionOrder

## Overview

CloseProductionOrder performs the final administrative close after technical completion and downstream cost settlement are done. The command is the last lifecycle step for the production order.

## Business Rules

- Target order must exist and be in `TECHNICALLY_COMPLETE`.
- All open work orders and execution exceptions must already be resolved.
- The linked manufacturing cost summary must be in `SETTLED`.
- Closed orders reject further execution, reopen, or rescheduling commands.

## Process Flow

```mermaid
flowchart TD
    A[Receive close request] --> B{Order exists and is TECHNICALLY_COMPLETE?}
    B -->|No| C[Return PRODUCTION_ORDER_NOT_CLOSABLE]
    B -->|Yes| D{Open work or exceptions remain?}
    D -->|Yes| E[Return OPEN_WORK_REMAINS]
    D -->|No| F{Cost summary settled?}
    F -->|No| G[Return COST_SUMMARY_NOT_SETTLED]
    F -->|Yes| H[Set order status to CLOSED]
    H --> I[Return closed order]
```

## External Dependencies

- [ManufacturingCostSummary](../model/ManufacturingCostSummary.md) - Administrative close depends on settled variance review.

## Error Scenarios

- **PRODUCTION_ORDER_NOT_FOUND**: Referenced production order does not exist
- **PRODUCTION_ORDER_NOT_CLOSABLE**: The order is not in `TECHNICALLY_COMPLETE`.
- **OPEN_WORK_REMAINS**: Open work orders or execution exceptions still exist.
- **COST_SUMMARY_NOT_SETTLED**: The linked cost summary is not in `SETTLED`.

## Test Cases

- closes a technically complete order with a settled cost summary
- returns error when the order does not exist
- returns error when the order is not technically complete
- returns error when open work or execution exceptions remain
- returns error when the linked cost summary is not settled
- blocks further execution commands after close
