# UpdateProfitCenter

## Permission Scope

accounting

## Overview

updateProfitCenter modifies fields on an existing profit center. In DRAFT status, all mutable fields can be updated. In ACTIVE status, only limited fields (description, responsible person) can be updated. INACTIVE profit centers cannot be updated.

## Business Rules

- Profit center must exist
- Profit center must be in DRAFT or ACTIVE status
- In DRAFT status: name, description, responsible person, validFrom, validTo can be updated
- In ACTIVE status: only description and responsible person can be updated
- When updating validTo, it must remain on or after validFrom
- INACTIVE profit centers cannot be updated
- Emits an audit event recording previous and new field values

## Process Flow

```mermaid
flowchart TD
    A[Receive update profit center request] --> B{Profit center exists?}
    B -->|No| C[Return error: PROFIT_CENTER_NOT_FOUND]
    B -->|Yes| D{Status is DRAFT or ACTIVE?}
    D -->|No| E[Return error: INVALID_STATE]
    D -->|Yes| F{Status is ACTIVE and restricted field change?}
    F -->|Yes| G[Return error: FIELD_NOT_UPDATABLE]
    F -->|No| H{validTo >= validFrom when both set?}
    H -->|No| I[Return error: INVALID_VALIDITY_PERIOD]
    H -->|Yes| J[Apply updates to profit center]
    J --> K[Emit audit event]
    K --> L[Return updated profit center]
```

## External Dependencies

- None

## Error Scenarios

- **PROFIT_CENTER_NOT_FOUND**: Profit center with the specified ID does not exist
- **INVALID_STATE**: Target entity is not in a valid status for this operation
- **FIELD_NOT_UPDATABLE**: Attempted to change a field that is not updatable in ACTIVE status
- **INVALID_VALIDITY_PERIOD**: validFrom is after validTo

## Test Cases

- updates name on a DRAFT profit center
- updates description on a DRAFT profit center
- updates description on an ACTIVE profit center
- updates responsible person on a DRAFT profit center
- updates responsible person on an ACTIVE profit center
- returns error when changing restricted fields on an ACTIVE profit center
- returns error when profit center does not exist
- returns error when profit center is INACTIVE
- returns error when validTo is before validFrom
- emits audit event recording previous and new field values
