# createAccountPayableDocument

## Overview

Creates a DRAFT vendor invoice (an `AccountPayableDocument`). A thin wrapper over the native `createAccountPayableDocument` command: its input mirrors the command's own input (`header`, `lines`, `dueSchedule`), and the body only bridges date strings (`t.date()`) → `Date` and GraphQL's optional `null` → the `undefined` the command's non-nullable fields expect, then forwards to the command and maps its error result. The resolver performs no validation of its own.

- `header` — company, supplier account, currency, payable control account, document type, `documentDate` / `postingDate`, and the document `totalAmount`.
- `lines` — each with `netAmount` / `grossAmount` (optional `taxAmount`), an optional `description`, and its `distributions`. A `PURCHASE_ORDER`-sourced line carries `sourceType`, `purchaseOrderLineId`, `quantity`, `unitPrice`, and `unitId` with empty `distributions` (the command derives them from the ordered item's valuation policy); an amount-only line carries its own `distributions`.
- `dueSchedule` — the payment schedule; a single installment (the whole amount, due on the document date) is the sane default.

The **caller** builds a consistent request: it resolves the company and currency (supplier `preferredCurrencyId`, falling back to the company `baseCurrencyId`), derives `PURCHASE_ORDER`-sourced lines from the purchase order (or amount-only lines from the entered amounts), computes `totalAmount`, and attaches a reconciling `dueSchedule`. The command owns all validation: company / supplier / currency / GL account existence and activity, line and distribution total reconciliation, due-schedule consistency, and accrual-account resolution for receipt-required PO lines.

## Modules Commands Used

- [account-payable] createAccountPayableDocument

## Exception Handling

| Error Code | Description |
| --- | --- |
| ACCOUNT_PAYABLE_COMPANY_NOT_FOUND | The referenced company does not exist. |
| ACCOUNT_PAYABLE_AP_SUPPLIER_ACCOUNT_NOT_FOUND | The referenced supplier account does not exist. |
| ACCOUNT_PAYABLE_AP_INVALID_SUPPLIER_ACCOUNT | The supplier account is not valid for this company or is inactive. |
| ACCOUNT_PAYABLE_AP_CURRENCY_NOT_FOUND | The referenced currency does not exist. |
| ACCOUNT_PAYABLE_ACCOUNT_NOT_FOUND | A referenced GL account does not exist. |
| ACCOUNT_PAYABLE_ACCOUNT_INACTIVE | A referenced GL account is not ACTIVE. |
| ACCOUNT_PAYABLE_AP_INVALID_AMOUNT | A supplied amount is not positive. |
| ACCOUNT_PAYABLE_AP_LINE_TOTAL_MISMATCH | Line/distribution totals do not reconcile with the document total. |
| ACCOUNT_PAYABLE_AP_LINE_AMOUNT_MISMATCH | A line net amount does not equal quantity times unit price. |
| ACCOUNT_PAYABLE_AP_LINE_SOURCE_INCONSISTENT | A line's source fields are inconsistent. |
| ACCOUNT_PAYABLE_AP_DUE_SCHEDULE_INVALID | The due schedule is invalid or does not reconcile with the total. |
| ACCOUNT_PAYABLE_AP_MINIMUM_LINES_NOT_MET | The invoice has no line or no distribution. |
| ACCOUNT_PAYABLE_AP_ACCRUAL_ACCOUNT_UNRESOLVED | An accrual account could not be resolved for a purchase-order sourced line. |
| ACCOUNT_PAYABLE_AP_DOCUMENT_LINE_NOT_FOUND | A referenced document line does not exist. |
| ACCOUNT_PAYABLE_AP_PURCHASE_ORDER_ACCOUNT_MISMATCH | A referenced purchase order belongs to a different company or supplier account. |
