# UpdateInternalOrder

## Overview

updateInternalOrder modifies fields on an existing internal order. Updates are permitted when the order is in CREATED or RELEASED status. Orders in TECHNICALLY_COMPLETED or CLOSED status cannot be updated. The order type cannot be changed after the order has been released.

## Business Rules

- Internal order must exist
- Internal order must be in CREATED or RELEASED status
- Description, responsible person, cost center, and validity dates can be updated in CREATED or RELEASED status
- Order type cannot be changed after the order is released
- Orders in TECHNICALLY_COMPLETED or CLOSED status cannot be updated
- When updating validity dates, validFrom must precede validTo when both are set
- Updated cost center must belong to the same company
- Emits an audit event recording previous and new field values

## Process Flow

```mermaid
flowchart TD
    A[Receive update internal order request] --> B{Internal order exists?}
    B -->|No| C[Return error: INTERNAL_ORDER_NOT_FOUND]
    B -->|Yes| D{Status is CREATED or RELEASED?}
    D -->|No| E[Return error: INVALID_STATE]
    D -->|Yes| F{Order type change attempted after release?}
    F -->|Yes| G[Return error: ORDER_TYPE_CHANGE_NOT_ALLOWED]
    F -->|No| H{Cost center valid if changed?}
    H -->|No| I[Return error: COST_CENTER_NOT_FOUND]
    H -->|Yes| J{validFrom <= validTo when both set?}
    J -->|No| K[Return error: INVALID_VALIDITY_PERIOD]
    J -->|Yes| L[Apply updates to internal order]
    L --> M[Emit audit event]
    M --> N[Return updated internal order]
```

## External Dependencies

- None

## Error Scenarios

- **INTERNAL_ORDER_NOT_FOUND**: Internal order with the specified ID does not exist
- **INVALID_STATE**: Internal order is in TECHNICALLY_COMPLETED or CLOSED status and cannot be updated
- **ORDER_TYPE_CHANGE_NOT_ALLOWED**: Order type cannot be changed after the order is released
- **COST_CENTER_NOT_FOUND**: Updated cost center does not exist or does not belong to the same company
- **INVALID_VALIDITY_PERIOD**: validFrom is after validTo

## Test Cases

- updates description on a CREATED internal order
- updates description on a RELEASED internal order
- updates responsible person on a CREATED internal order
- updates cost center on a CREATED internal order
- updates validity dates on a CREATED internal order
- returns error when internal order does not exist
- returns error when internal order is TECHNICALLY_COMPLETED
- returns error when internal order is CLOSED
- returns error when changing order type after release
- returns error when updated cost center does not exist
- returns error when validFrom is after validTo
- emits audit event recording previous and new field values
