# DeleteAccountingPeriod

## Permission Scope

periodManagement

## Overview

deleteAccountingPeriod permanently removes an accounting period record. Deletion is only permitted when the period has no associated journal entries, regardless of their status (DRAFT, POSTED, or REVERSED). This prevents orphaned financial data and maintains referential integrity within the general ledger.

## Business Rules

- Accounting period must exist and be identified by `id`
- Accounting period cannot be deleted if it has any associated journal entries regardless of status
- Accounting period cannot be deleted if it has an active PeriodClose record
- Deletion is a hard delete — the record is permanently removed
- Emits an audit event via the audit module recording the acting user, timestamp, and deleted period details

## Process Flow

```mermaid
flowchart TD
    A[Receive delete accounting period request] --> B{Accounting period exists?}
    B -->|No| C[Return error: period not found]
    B -->|Yes| D{Journal entries exist for this period?}
    D -->|Yes| E[Return error: has associated journal entries]
    D -->|No| F{Active PeriodClose exists?}
    F -->|Yes| G[Return error: has active close in progress]
    F -->|No| H[Delete accounting period record]
    H --> I[Emit audit event]
    I --> J[Return success]
```

## External Dependencies

- None (same-module read for period, journal entry, and period close lookup)

## Error Scenarios

- **ACCOUNTING_PERIOD_NOT_FOUND**: Referenced accounting period does not exist
- **HAS_ASSOCIATED_JOURNAL_ENTRIES**: Accounting period has one or more journal entries and cannot be deleted
- **HAS_ACTIVE_PERIOD_CLOSE**: Accounting period has an active period close record in progress

## Test Cases

- returns error when accounting period does not exist
- returns error when accounting period has associated journal entries
- returns error when accounting period has an active period close record
- deletes accounting period with no associated journal entries or period close
- emits audit event recording acting user, timestamp, and deleted period details
