# CreateAccountReceivableDocument

## Permission Scope

accountReceivableDocumentOperation

## Overview

createAccountReceivableDocument creates a DRAFT AR document with commercial lines, accounting distributions, and an optional due schedule.

## Business Rules

- At least one line is required
- Line net/gross amounts, distributions, due amounts, and totalAmount must be positive
- SALES_ORDER source lines require salesOrderLineId, quantity, unitPrice, and unitId
- Line netAmount must equal quantity multiplied by unitPrice for SALES_ORDER lines
- Line gross amounts must sum to totalAmount
- Each line's distributions must sum to its grossAmount
- A supplied due schedule must sum to totalAmount
- Creation does not affect the ledger or sales-order billing progress

## Process Flow

```mermaid
flowchart TD
    A[Validate header, lines, distributions, and due schedule] --> B[Insert DRAFT invoice]
    B --> C[Insert invoice lines]
    C --> D[Insert distributions and due schedule]
    D --> E[Return invoice]
```

## External Dependencies

- [sales::SalesOrder](../../../sales/docs/model/SalesOrder.md) - Supplies the sales-order line identifiers retained for billing projection

## Error Scenarios

- **INVALID_CUSTOMER_ACCOUNT**: Business partner is unavailable for transactions or belongs to another company

- **MINIMUM_LINES_NOT_MET**: No invoice lines were supplied
- **INVALID_AMOUNT**: A line or header amount is not positive
- **LINE_SOURCE_INCONSISTENT**: Sales-order source fields are incomplete or inconsistent
- **LINE_AMOUNT_MISMATCH**: Net amount differs from quantity times unit price
- **LINE_TOTAL_MISMATCH**: Invoice line total does not equal the document total
- **DUE_SCHEDULE_INVALID**: Due schedule must be positive and equal the document total

## Test Cases

- creates a draft invoice with distributions and due schedule
- rejects quantity and unit-price mismatch
- rejects a header and line total mismatch
