# DeleteInternalOrder

## Permission Scope

accounting

## Overview

deleteInternalOrder permanently removes an internal order that is in CREATED status. Orders in RELEASED, TECHNICALLY_COMPLETED, or CLOSED status cannot be deleted because they may have associated cost postings or budget assignments.

## Business Rules

- Internal order must exist
- Only CREATED orders can be deleted
- RELEASED, TECHNICALLY_COMPLETED, and CLOSED orders cannot be deleted
- Deletion removes the order and any associated budget assignment
- Emits an audit event recording the deletion and acting user

## Process Flow

```mermaid
flowchart TD
    A[Receive delete 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[Delete internal order and associated budget assignment]
    F --> G[Emit audit event]
    G --> H[Return success]
```

## 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

- deletes a CREATED internal order successfully
- deletes a CREATED internal order and removes associated budget assignment
- returns error when internal order does not exist
- returns error when internal order is in RELEASED status
- returns error when internal order is in TECHNICALLY_COMPLETED status
- returns error when internal order is in CLOSED status
- emits audit event recording the deletion and acting user
