# TechnicallyCompleteProductionOrder

## Permission Scope

productionOrder

## Overview

TechnicallyCompleteProductionOrder freezes the production order after physical completion and moves the linked manufacturing cost summary into variance review. It marks the point where no more normal execution or rescheduling is expected.

## Business Rules

- Target order must exist and be in `COMPLETED`.
- No additional execution, rescheduling, or material issue may still be expected.
- Open execution exceptions must be resolved before technical completion.
- Pending material issue requests or unresolved backflush issue intents must be cleared before technical completion.
- Technical completion moves the linked manufacturing cost summary to `PENDING_VARIANCE_REVIEW`.
- Further shop-floor reporting is blocked until a controlled reopen occurs.

## Process Flow

```mermaid
flowchart TD
    A[Receive technical-complete request] --> B{Order exists and is COMPLETED?}
    B -->|No| C[Return PRODUCTION_ORDER_NOT_TECHNICALLY_COMPLETABLE]
    B -->|Yes| D{Open execution exceptions remain?}
    D -->|Yes| E[Return EXECUTION_EXCEPTION_REMAINS]
    D -->|No| F{Pending material issue requests remain?}
    F -->|Yes| G[Return PENDING_MATERIAL_ISSUE_REQUESTS]
    F -->|No| H[Move cost summary to PENDING_VARIANCE_REVIEW]
    H --> I[Set order status to TECHNICALLY_COMPLETE]
    I --> J[Return technically complete order]
```

## External Dependencies

- [ManufacturingCostSummary](../model/ManufacturingCostSummary.md) - Technical completion opens variance review on the linked summary.

## Error Scenarios

- **PRODUCTION_ORDER_NOT_FOUND**: Referenced production order does not exist
- **PRODUCTION_ORDER_NOT_TECHNICALLY_COMPLETABLE**: The order is not in `COMPLETED`.
- **EXECUTION_EXCEPTION_REMAINS**: Unresolved execution exceptions still exist.
- **PENDING_MATERIAL_ISSUE_REQUESTS**: Material issue requests or backflush intents are still unresolved for the order.
- **COST_SUMMARY_NOT_READY**: The linked cost summary cannot move to review.

## Test Cases

- technically completes a completed order and opens variance review
- returns error when the order does not exist
- returns error when the order is not in `COMPLETED`
- returns error when unresolved execution exceptions remain
- returns error when pending material issue requests or unresolved backflush intents remain
- returns error when the linked cost summary is not ready for review
- blocks further shop-floor reporting after technical completion
