# PostAccountReceivableDocument

## Permission Scope

accountReceivableDocumentOperation

## Overview

postAccountReceivableDocument posts a REGISTERED AR document and creates its journal entry from accounting distribution lines.

## Business Rules

- Only REGISTERED documents can be posted
- The invoice currency must equal the company base currency
- The journal debits accounts receivable control and credits each line's revenue account
- INVOICE debits receivables and credits distributions; CREDIT_MEMO reverses those signs
- Only after accounting succeeds is the document marked POSTED

## Process Flow

```mermaid
flowchart TD
    A[Lock REGISTERED document] --> B[Validate totals, currency, accounts, and period]
    B --> C[Create and post journal entry]
    C --> D[Mark invoice POSTED]
    D --> E[Return posted document]
```

## External Dependencies

- financial-accounting createJournalEntry and postJournalEntry
- organization getCompany
- coa-management listAccounts

## Error Scenarios

- **DOCUMENT_NOT_FOUND**: Referenced AR document does not exist
- **INVALID_DOCUMENT_STATUS**: AR document status does not allow this operation
- **LINE_TOTAL_MISMATCH**: Invoice line total does not equal the document total
- **POSTING_CURRENCY_MISMATCH**: Invoice and base currencies differ
- **ACCOUNTING_PERIOD_NOT_FOUND**: No open accounting period contains the posting date
- **JOURNAL_ENTRY_CREATE_FAILED**: Invoice journal entry could not be created
- **JOURNAL_ENTRY_POST_FAILED**: Invoice journal entry could not be posted

## Test Cases

- posts a registered invoice from distribution lines
