# CloseInternalOrder

## Permission Scope

accounting

## Overview

closeInternalOrder transitions an internal order from TECHNICALLY_COMPLETED status to CLOSED status. Once closed, no further changes or cost postings are permitted. This is the terminal state of the internal order lifecycle.

## Business Rules

- Internal order must exist
- Internal order must be in TECHNICALLY_COMPLETED status to be closed
- Closing a CREATED, RELEASED, or already CLOSED order is rejected as an invalid state transition
- CLOSED orders cannot transition to any other status
- No further modifications are permitted after closure
- Emits an audit event recording the status transition and acting user

## Process Flow

```mermaid
flowchart TD
    A[Receive close internal order request] --> B{Internal order exists?}
    B -->|No| C[Return error: INTERNAL_ORDER_NOT_FOUND]
    B -->|Yes| D{Status is TECHNICALLY_COMPLETED?}
    D -->|No| E[Return error: INVALID_STATE]
    D -->|Yes| F[Transition status from TECHNICALLY_COMPLETED to CLOSED]
    F --> G[Emit audit event]
    G --> H[Return closed 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

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