# DeleteCostElement

## Permission Scope

accounting

## Overview

deleteCostElement permanently removes a cost element that is in DRAFT status. Cost elements in ACTIVE or INACTIVE status cannot be deleted because they may be referenced by existing cost postings, allocations, or budget line items.

## Business Rules

- Cost element must exist
- Only DRAFT cost elements can be deleted
- ACTIVE and INACTIVE cost elements cannot be deleted
- Deletion permanently removes the cost element record
- Emits an audit event recording the deletion and acting user

## Process Flow

```mermaid
flowchart TD
    A[Receive delete cost element request] --> B{Cost element exists?}
    B -->|No| C[Return error: COST_ELEMENT_NOT_FOUND]
    B -->|Yes| D{Status is DRAFT?}
    D -->|No| E[Return error: INVALID_STATE]
    D -->|Yes| F[Delete cost element]
    F --> G[Emit audit event]
    G --> H[Return success]
```

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

- deletes a DRAFT cost element successfully
- returns error when cost element does not exist
- returns error when cost element is in ACTIVE status
- returns error when cost element is in INACTIVE status
- emits audit event recording the deletion and acting user
