# DeactivateCostElement

## Permission Scope

accounting

## Overview

deactivateCostElement transitions a cost element from ACTIVE status to INACTIVE status. Once inactive, the cost element can no longer be used for new cost postings or allocations. Historical postings referencing this element remain intact.

## Business Rules

- Cost element must exist
- Cost element must be in ACTIVE status to be deactivated
- Deactivating a DRAFT or already INACTIVE cost element is rejected as an invalid state transition
- Existing historical postings referencing this cost element are not affected
- Emits an audit event recording the status transition and acting user

## Process Flow

```mermaid
flowchart TD
    A[Receive deactivate cost element request] --> B{Cost element exists?}
    B -->|No| C[Return error: COST_ELEMENT_NOT_FOUND]
    B -->|Yes| D{Status is ACTIVE?}
    D -->|No| E[Return error: INVALID_STATE]
    D -->|Yes| F[Transition status from ACTIVE to INACTIVE]
    F --> G[Emit audit event]
    G --> H[Return deactivated cost element]
```

## External Dependencies

- None

## Error Scenarios

- **COST_ELEMENT_NOT_FOUND**: Referenced cost element does not exist
- **INVALID_STATE**: Target entity is not in a valid status for this operation

## Test Cases

- deactivates an ACTIVE cost element transitioning it to INACTIVE status
- returns error when cost element does not exist
- returns error when cost element is in DRAFT status
- returns error when cost element is already INACTIVE
- emits audit event recording the status transition and acting user
