# updateAccountPayableDocument

## Overview

Edits a **DRAFT** vendor invoice. A thin wrapper over the native `updateAccountPayableDocument` command: its input mirrors the command's own input, and the body only adapts the payload before forwarding — date strings (`t.date()`) → `Date`, header nulls → the `undefined` the command's fields expect, and each line/distribution `patch` trimmed to just the fields supplied (absent optionals are dropped, never forwarded as `null`) since the command writes a patch straight to the row. It then forwards to the command and maps its error result, performing no validation of its own.

- `headerPatch` — header fields to patch: `documentDate`, `externalDocumentNumber`, `description`, `payableControlAccountId`, and the recomputed `totalAmount`. Omit for a line-only edit.
- `addLines` — lines to add, each with `netAmount`, `grossAmount`, optional `taxAmount` / `description`, and its `distributions`.
- `updateLines` — edits to existing lines by `lineId`: a field `patch`, plus `addDistributions` / `updateDistributions` / `removeDistributionLineIds` to adjust the line's distributions.
- `removeLineIds` — existing lines to remove.
- `dueSchedule` — replacement due schedule; omit to leave the current schedule unchanged.

The **caller** builds a consistent request (the recomputed `totalAmount`, per-line distributions that sum to each line, and — when the total changes — a `dueSchedule` that reconciles). The command owns all validation: DRAFT-only status, line/distribution existence, positive amounts, line and distribution total reconciliation, GL account existence/active/company, due-schedule consistency, and derived-distribution immutability. By convention this resolver edits amount-only invoices; PURCHASE_ORDER-sourced lines are edited on the purchase order, not here.

## Modules Commands Used

- [account-payable] updateAccountPayableDocument

## Exception Handling

| Error Code | Description |
| --- | --- |
| ACCOUNT_PAYABLE_AP_DOCUMENT_NOT_FOUND | No account payable document exists for the given ID. |
| ACCOUNT_PAYABLE_AP_INVALID_DOCUMENT_STATUS | The document is not in DRAFT status; only DRAFT invoices can be edited. |
| ACCOUNT_PAYABLE_ACCOUNT_NOT_FOUND | A referenced GL account (payable control or distribution) does not exist. |
| ACCOUNT_PAYABLE_ACCOUNT_INACTIVE | A referenced GL account is not ACTIVE. |
| ACCOUNT_PAYABLE_ACCOUNT_COMPANY_MISMATCH | A referenced GL account belongs to a different company. |
| ACCOUNT_PAYABLE_AP_CURRENCY_NOT_FOUND | The referenced currency 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 unavailable for transactions or belongs to another company. |
| ACCOUNT_PAYABLE_AP_PURCHASE_ORDER_ACCOUNT_MISMATCH | A referenced purchase order belongs to a different company or supplier account. |
| ACCOUNT_PAYABLE_AP_DERIVED_DISTRIBUTION_IMMUTABLE | A system-derived distribution (accrual / invoice price variance) cannot be edited. |
| ACCOUNT_PAYABLE_AP_DOCUMENT_LINE_NOT_FOUND | A referenced document line does not exist. |
| ACCOUNT_PAYABLE_AP_INVALID_AMOUNT | A supplied amount is not positive. |
| ACCOUNT_PAYABLE_AP_LINE_EDIT_CONFLICT | The same line was both updated and removed, or otherwise conflicts. |
| ACCOUNT_PAYABLE_AP_LINE_TOTAL_MISMATCH | Line/distribution totals do not reconcile with the document total. |
| ACCOUNT_PAYABLE_AP_MINIMUM_LINES_NOT_MET | The resulting invoice has no line or no distribution. |
| ACCOUNT_PAYABLE_AP_LINE_SOURCE_INCONSISTENT | A line's source fields are inconsistent. |
| ACCOUNT_PAYABLE_AP_LINE_AMOUNT_MISMATCH | A line net amount does not equal quantity times unit price. |
| ACCOUNT_PAYABLE_AP_DUE_SCHEDULE_INVALID | The due schedule is invalid or does not reconcile with the total. |
| ACCOUNT_PAYABLE_AP_ACCRUAL_ACCOUNT_UNRESOLVED | An accrual account could not be resolved for a purchase-order sourced line. |
