# ReleaseInternalOrder

## Permission Scope

accounting

## Overview

releaseInternalOrder transitions an internal order from CREATED status to RELEASED status. Once released, the order is enabled for cost postings. Only orders in CREATED status can be released.

## Business Rules

- Internal order must exist
- Internal order must be in CREATED status to be released
- Releasing a RELEASED, TECHNICALLY_COMPLETED, or CLOSED order is rejected as an invalid state transition
- Upon release, cost postings against the order are enabled
- Emits an audit event recording the status transition and acting user

## Process Flow

```mermaid
flowchart TD
    A[Receive release internal order request] --> B{Internal order exists?}
    B -->|No| C[Return error: INTERNAL_ORDER_NOT_FOUND]
    B -->|Yes| D{Status is CREATED?}
    D -->|No| E[Return error: INVALID_STATE]
    D -->|Yes| F[Transition status from CREATED to RELEASED]
    F --> G[Emit audit event]
    G --> H[Return released internal order]
```

## External Dependencies

- None

## Error Scenarios

- **INTERNAL_ORDER_NOT_FOUND**: Internal order with the specified ID does not exist
- **INVALID_STATE**: Target entity is not in a valid status for this operation

## Test Cases

- releases a CREATED internal order transitioning it to RELEASED status
- returns error when internal order does not exist
- returns error when internal order is already RELEASED
- returns error when internal order is TECHNICALLY_COMPLETED
- returns error when internal order is CLOSED
- emits audit event recording the status transition and acting user
