# CompleteProductionOrder

## Permission Scope

productionOrder

## Overview

CompleteProductionOrder marks physical production complete once required work orders are finished and final receipt obligations are satisfied. The command freezes production execution while still allowing later technical completion and review.

## Business Rules

- Target order must exist and be in `IN_PROGRESS`.
- All mandatory work orders must be completed.
- Final output reporting must exist for the required good quantity.
- Finished-goods or intermediate receipt handoff required by policy must already be submitted.
- Completing the order blocks additional operational production work except through controlled reopen.

## Process Flow

```mermaid
flowchart TD
    A[Receive complete request] --> B{Order exists and is IN_PROGRESS?}
    B -->|No| C[Return PRODUCTION_ORDER_NOT_COMPLETABLE]
    B -->|Yes| D{All required work orders complete?}
    D -->|No| E[Return OPEN_WORK_ORDER_REMAINS]
    D -->|Yes| F{Final reporting and receipt handoff complete?}
    F -->|No| G[Return FINAL_RECEIPT_REQUIRED]
    F -->|Yes| H[Set status to COMPLETED]
    H --> I[Return completed order]
```

## External Dependencies

- [WorkOrder](../models/WorkOrder.md) - Completion requires the operation-level plan to be done.

## Error Scenarios

- **PRODUCTION_ORDER_NOT_FOUND**: Referenced production order does not exist
- **PRODUCTION_ORDER_NOT_COMPLETABLE**: The order is not in `IN_PROGRESS`.
- **OPEN_WORK_ORDER_REMAINS**: One or more required work orders are still open.
- **FINAL_OUTPUT_REQUIRED**: Final output reporting is incomplete.
- **FINAL_RECEIPT_REQUIRED**: Required receipt handoff evidence is missing.

## Test Cases

- completes an in-progress order after all required work orders finish
- returns error when the order does not exist
- returns error when the order is not in `IN_PROGRESS`
- returns error when required work orders remain open
- returns error when final output reporting is incomplete
- returns error when required receipt handoff evidence is missing
