# Accounting and invoices

## Estimates

Use [Estimate Price](/api/public/accounting#estimate-price) to estimate one course from `latitude,longitude` source and destination coordinates. The result is an estimate in Tomans. It excludes trip-level cost and boost; environmental conditions can affect the final amount. Fetch [Boost Levels](/api/public/delivery#boost-levels) periodically and include the selected amount when estimating a boosted order.

## Proxy invoicing

Proxy invoicing must be enabled for your API account. [Register the seller](/api/public/accounting#register-or-update-seller) before creating a trip and pass its client-scoped identifier as `pickup.seller_id`. Missing or unregistered seller IDs reject trip creation. Both seller creation and update return HTTP 201. Omitted optional fields retain their values on update; null clears them when validation permits.

Use the current trip `batch_id` with [Get Trip Invoice](/api/public/accounting#get-trip-invoice). Omit `revision` for the latest invoice or request revision 0 for the original. Later adjustments increase the revision; `revisions_count` is the highest available revision number. HTTP 202 means generation is in progress: wait for `retry_after` seconds or the `Retry-After` response header before polling. A ready response includes an expiring private download URL valid for five minutes. Invoice PDFs and financial event delivery have separate availability.

## Financial events

Financial events report batch accounting results separately from trip lifecycle events.
They have top-level batch identifiers and financial fields, without a `trip` wrapper.

:::note

Financial notifications must be enabled for your account by the technical team and require a
configured webhook URL. Invoice PDF availability is separate; receiving a financial event does
not mean that a PDF is ready or available for that batch.

:::

### Events

| Event | State | Description |
|-------|-------|-------------|
| **financial_calculated** | `calculated` | Initial financial calculation has completed. Revision is `0` and `changes` is empty. Eligible trips deleted by Miare receive zero total and zero detail amounts. |
| **financial_adjusted** | `changed` | A course has subsequently been marked invalid (`course_faked`) and recalculation changed the batch total. Revision is greater than `0`; `changes` contains the cumulative adjustment history. A recalculation with no change to the total emits no adjustment event. |

All monetary values in financial events are in **Tomans (IRT)**.

| Value | Type | Description |
|-------|------|-------------|
| **event** | string | `financial_calculated` or `financial_adjusted`. |
| **batch_id** | string [UUID] | Stable accounting batch identifier across revisions. It can differ from the original trip IDs after batching. |
| **revision** | number [integer] | `0` for the initial calculation; increasing positive values for adjustments to the same batch. |
| **trip_ids** | array of string [UUID] | Unique client-visible trip IDs; original trip IDs are retained after batching. |
| **course_ids** | array of string [UUID] | Course identifiers. Adjustment payloads include invalidated courses for traceability. |
| **bill_numbers** | array of string | Unique non-empty bill numbers. Not positionally aligned with `trip_ids` or `course_ids`. |
| **state** | string | Financial state: `calculated` or `changed`. This is separate from the trip lifecycle state. |
| **financials.total_cost** | number [integer] | Complete batch total at this revision, including all adjustments so far. Use this as the authoritative total. |
| **financials.details.base_cost** | number [integer] | Base-cost component of the calculation. |
| **financials.details.cancel_cost** | number [integer] | Late-cancellation component. |
| **financials.details.delay_cost** | number [integer] | Pickup-delay component. |
| **financials.details.return_cost** | number [integer] | Return component. |
| **changes** | array of object | Empty at revision `0`; cumulative adjustment history on later revisions. |

The detail fields cover these four pricing components and may not sum to `total_cost`, which
can include other pricing rules, discounts, and rounding. Arrays of identifiers are correlation
fields, not a mapping of individual courses to trips or bills.

### Reconciliation

Deduplicate by `(batch_id, revision)` and retain the highest revision seen. Revision 0 can arrive after a later adjustment. Replace the saved total with `financials.total_cost`; do not add `changes` again because it is cumulative history. Identifier arrays are correlation fields and are not positionally aligned. A recalculation that leaves the total unchanged emits no adjustment event. Eligible trips deleted by Miare have zero totals and zero detail amounts.

Recover the latest snapshot with [Get Financial Detail](/api/public/accounting#get-financial-detail): `GET /api/accounting/financial-detail/{batch_id}/`. It requires your API token and returns the financial event fields without `event`. HTTP 404 means the batch is unknown or financial data is not yet available. Use this endpoint for financial reconciliation and List Trips for lifecycle recovery.

Financial notifications require enablement and a configured webhook URL. They omit `X-Miare-API-Version`; `revision` tracks batch financial changes. Any non-2XX response or network failure triggers retry with a ten-second timeout. Default configurable delays are 30, 120, 300, 900 and 1800 seconds, and every retry preserves the original payload. The [webhook reference](/webhooks) includes both financial schemas and examples.
