# createAccountReceivableInvoice

## Overview

Creates a DRAFT customer invoice (an `AccountReceivableDocument` of `documentType` INVOICE) from an existing sales order. The sales order must be in CONFIRMED or CLOSED status. Each sales-order line becomes a `SALES_ORDER`-sourced AR line where `netAmount = quantity × unitPrice`; no tax is applied so `grossAmount = netAmount`. A SALES_ORDER-sourced AR line must carry a unit: the line's own `unitId` is used, falling back to the item's base unit (`Item.unitId`) for older order lines saved without one (only if the item also has no base unit is the invoice rejected). Each line's description defaults to the order line's description, falling back to the item's name so invoice lines are never blank. The header carries `receivableControlAccountId` as the receivable (asset) account. The invoice inherits the sales order's header currency. A one-line due schedule (the whole amount, due on the document date) is always attached because [registerAccountReceivableDocument](./registerAccountReceivableDocument.md) requires a non-empty due schedule. When `documentDate` is omitted it defaults to today.

Unlike the AP side there is no valuation-policy-derived distribution account: AR has no inventory integration, so the caller supplies the revenue account (`revenueAccountId`) that every line is booked to. Amount-only invoices — services, fees, adjustments — go through [createManualAccountReceivableInvoice](./createManualAccountReceivableInvoice.md) instead.

## Modules Commands Used

- [account-receivable] createAccountReceivableDocument

## Exception Handling

| Error Code | Description |
| --- | --- |
| ACCOUNT_RECEIVABLE_COMPANY_NOT_FOUND | The company resolved from the sales order does not exist. |
| ACCOUNT_RECEIVABLE_BUSINESS_PARTNER_NOT_FOUND | The customer (business partner) on the sales order does not exist. |
| ACCOUNT_RECEIVABLE_CURRENCY_NOT_FOUND | The sales order's currency no longer exists. |
| ACCOUNT_RECEIVABLE_ACCOUNT_NOT_FOUND | The receivable control account or revenue account does not exist. |
| ACCOUNT_RECEIVABLE_ACCOUNT_INACTIVE | The receivable control account or revenue account is not ACTIVE. |
| ACCOUNT_RECEIVABLE_INVALID_AMOUNT | A computed line amount is invalid (e.g., non-positive). |
| ACCOUNT_RECEIVABLE_LINE_TOTAL_MISMATCH | The sum of line amounts does not reconcile with the document total. |
| ACCOUNT_RECEIVABLE_LINE_AMOUNT_MISMATCH | A line's net amount does not equal quantity × unit price. |
| ACCOUNT_RECEIVABLE_LINE_SOURCE_INCONSISTENT | A line's sales-order source fields are inconsistent. |
| ACCOUNT_RECEIVABLE_DUE_SCHEDULE_INVALID | The attached due schedule does not reconcile with the document total. |
| ACCOUNT_RECEIVABLE_MINIMUM_LINES_NOT_MET | The document has no lines to invoice. |
