# DeleteJournalEntry

## Permission Scope

journalEntry

## Overview

deleteJournalEntry removes a journal entry and all its associated journal lines from the system. Only DRAFT entries can be deleted. Posted entries must be reversed rather than deleted to preserve the audit trail. Reversed entries also cannot be deleted. This ensures that all financial transactions that have affected the general ledger remain traceable.

## Business Rules

- Journal entry must exist and be in DRAFT status
- Only DRAFT entries can be deleted; POSTED and REVERSED entries cannot be deleted
- Deleting a DRAFT entry removes all associated journal lines
- Posted entries must be reversed via reverseJournalEntry instead of deleted
- Emits an audit event recording the deletion, acting user, and timestamp

## Process Flow

```mermaid
flowchart TD
    A[Receive delete journal entry request] --> B{Journal entry exists?}
    B -->|No| C[Return error: journal entry not found]
    B -->|Yes| D{Entry status is DRAFT?}
    D -->|No| E[Return error: cannot delete non-draft entry]
    D -->|Yes| F[Delete all associated journal lines]
    F --> G[Delete journal entry]
    G --> H[Emit audit event]
    H --> I[Return success]
```

## External Dependencies

- None

## Error Scenarios

- **JOURNAL_ENTRY_NOT_FOUND**: Referenced journal entry does not exist
- **INVALID_STATUS_FOR_DELETE**: Journal entry is not in DRAFT status; POSTED and REVERSED entries cannot be deleted

## Test Cases

- returns error when journal entry does not exist
- returns error when journal entry is in POSTED status
- returns error when journal entry is in REVERSED status
- deletes DRAFT journal entry and all associated journal lines
- emits audit event recording the deletion and acting user
