# DeactivateAccount

## Permission Scope

accountManagement

## Overview

deactivateAccount transitions a GL account from ACTIVE to INACTIVE status, preventing new postings while preserving the account for history and reporting.

## Business Rules

- Target account must exist
- Target account must be ACTIVE
- A pre-deactivation hook may block the transition when downstream balances are open

## Process Flow

```mermaid
flowchart TD
    A[Receive deactivate request] --> B{Account exists?}
    B -->|No| C[Return not found]
    B -->|Yes| F{Account active?}
    F -->|No| G[Return invalid transition]
    F -->|Yes| H{Hook passes?}
    H -->|No| I[Return open balances]
    H -->|Yes| J[Set INACTIVE]
    J --> K[Return account]
```

## External Dependencies

- None. The GL balance check is an optional hook supplied by the caller.

## Error Scenarios

- **ACCOUNT_NOT_FOUND**: Specified account ID does not exist
- **INVALID_STATE_TRANSITION**: Requested state transition is not allowed from the current status
- **OPEN_BALANCES_EXIST**: Pre-deactivation hook reports open or unreconciled balances

## Test Cases

- returns error when account does not exist
- returns error when account is not active
- deactivates an active account
- returns error when GL hook reports open balances
