# PostAdjustingEntries

## Overview

postAdjustingEntries records adjusting entries (accruals, deferrals, reclassifications) during the period-end close process and transitions the PeriodClose from SUBLEDGERS_VERIFIED to ADJUSTMENTS_POSTED status. Adjusting entries are flagged with an `adjustingEntryType` to distinguish them from regular operational entries for reporting and audit purposes. All adjusting entries must be balanced before the close can proceed.

## Business Rules

- PeriodClose record must exist and be in SUBLEDGERS_VERIFIED status
- Only SUBLEDGERS_VERIFIED records can transition to ADJUSTMENTS_POSTED
- Adjusting entries created during this step must have a valid `adjustingEntryType` (ACCRUAL, DEFERRAL, or RECLASSIFICATION)
- Adjusting entries are flagged distinctly from regular journal entries for audit trail purposes
- All adjusting entries must be balanced (total debits equal total credits) before the close can proceed
- Adjusting entries follow the same posting rules as regular journal entries (valid period, valid accounts, etc.)
- Updates completionPercentage to reflect progress
- Records the userId of the operator who posted the adjusting entries
- Emits an audit event recording the acting user, timestamp, adjusting entry details, and status transition

## Process Flow

```mermaid
flowchart TD
    A[Receive post adjusting entries request] --> B{PeriodClose record exists?}
    B -->|No| C[Return error: period close not found]
    B -->|Yes| D{PeriodClose in SUBLEDGERS_VERIFIED status?}
    D -->|No| E[Return error: invalid status transition]
    D -->|Yes| F{Adjusting entries have valid types?}
    F -->|No| G[Return error: invalid adjusting entry type]
    F -->|Yes| H{All adjusting entries balanced?}
    H -->|No| I[Return error: unbalanced adjusting entries]
    H -->|Yes| J[Post adjusting entries to the period]
    J --> K[Transition PeriodClose to ADJUSTMENTS_POSTED]
    K --> L[Update completionPercentage]
    L --> M[Emit audit event]
    M --> N[Return updated PeriodClose]
```

## External Dependencies

- [coa-management::getAccount](../../../coa-management/docs/queries/GetAccount.md) - Validates GL accounts referenced in adjusting entries

## Error Scenarios

- **PERIOD_CLOSE_NOT_FOUND**: No PeriodClose record matches the given id
- **INVALID_STATUS_TRANSITION**: PeriodClose is not in SUBLEDGERS_VERIFIED status; only SUBLEDGERS_VERIFIED can transition to ADJUSTMENTS_POSTED
- **INVALID_ADJUSTING_ENTRY_TYPE**: Adjusting entry type is not ACCRUAL, DEFERRAL, or RECLASSIFICATION
- **UNBALANCED_ADJUSTING_ENTRIES**: One or more adjusting entries have total debits not equal to total credits

## Test Cases

- returns error when PeriodClose record does not exist
- returns error when PeriodClose is in IN_PROGRESS status
- returns error when PeriodClose is already in ADJUSTMENTS_POSTED status
- returns error when adjusting entry type is invalid
- returns error when adjusting entries are not balanced
- posts accrual adjusting entries and transitions to ADJUSTMENTS_POSTED
- posts deferral adjusting entries and transitions to ADJUSTMENTS_POSTED
- posts reclassification adjusting entries and transitions to ADJUSTMENTS_POSTED
- adjusting entries are flagged distinctly from regular journal entries
- updates completionPercentage after posting adjusting entries
- emits audit event recording adjusting entry details and status transition
