# Report margin

Use this recipe to report the margin on what you sell in another system,
such as a business intelligence (BI) tool or a spreadsheet. You read the
cost and margin that Omono saves on each sales order line item, and the
cost and profit on each invoice.

Every request in this recipe sends your API key in an
`Authorization: Bearer <key>` header. Paths are relative to
`https://<host>/api/v1`, and the examples show the full path. This recipe
only reads, but `GET /sales-order-lines` and `GET /invoices` are in beta,
so they can change in any way, at any time, until they're marked stable.
Learn more about
[beta operations](/docs/compatibility#stable-and-beta-operations).

You need the scopes `sales:read` and `stock:read`. Only a key that also
has `stock:read` (or `stock:write`, which includes it) gets costs and
margins. For any other key, Omono leaves the cost fields out of the
response, rather than sending `null`.

## How cost and margin work in Omono

When a line item is added to a quote or sales order, Omono saves the cost
of one unit on it, in the document's currency. It's the **Last Cost** in
the line item window in Omono. Omono takes it from the latest purchase
order for the stock item, or from the stock item's cost, and for an
assembly it adds up the costs of its components. Staff can change it on
the line item, and it doesn't follow later changes to the stock item's
cost.

Each line item has two figures:

- `lastCost`: the cost of one unit, as the line item saved it.
- `margin`: (net price − `lastCost`) ÷ net price × 100, where the net price
  is `price` reduced by the `discount` percentage. A net price of 80 and a
  cost of 60 give `25`. It's `0` when the price is 0 or the discount is
  100%, because the net price is then 0. It's below 0 when the cost is
  more than a net price above 0.

Both are `null` if none is saved. A `lastCost` of 0 means no cost is
known, so the line item's `margin` is then `null`: a margin worked out
against no cost means nothing.

Each invoice adds up the cost of the goods on it:

- `costOfGoodsSold`: for each line item on the invoice, its quantity
  multiplied by the `lastCost` of the order's line item, added up.
- `grossProfit`: `totalValue` minus `costOfGoodsSold`.
- `grossMargin`: `grossProfit` ÷ `totalValue` × 100, or `null` when
  `costOfGoodsSold` is 0 or `null`.

They're in the invoice's currency, and `costOfGoodsSoldBase` and
`grossProfitBase` give the first two in your base currency. Once a line
item is on an invoiced dispatch, its cost can't change, so they're the
figures the invoice was raised at.

## Report the margin on orders

List the line items of your sales orders, for example everything sold of
one stock item since the start of October:

```http
GET /api/v1/sales-order-lines?stockItemId=<id>&created_gte=2026-10-01T00:00:00.000Z&limit=200
```

Follow `nextCursor` until `hasMore` is `false`. Each line item has its
`quantity`, `unitPrice`, `lastCost` and `margin`, and the `orderNo`,
`companyId`, `orderStatus`, `currencyId` and `exchangeRate` of its order.
The list includes the line items of Draft and Cancelled orders. To leave
them out, skip the line items whose `orderStatus` is `draft` or
`cancelled`. The profit on a line item is its `quantity` multiplied by
`unitPrice` minus `lastCost`.

A line item's prices and costs are in its order's currency, `currencyId`.
To add up line items from orders in different currencies, divide each
amount by the line item's `exchangeRate` to get your base currency, as
[money](/docs/conventions#money) explains.

You can also filter by `salesOrderId`. There's no `companyId` filter,
because a line item doesn't store its customer. For one customer, read
their orders with `GET /sales-orders?companyId=<id>&include=lineItems`,
whose line items have the same `lastCost` and `margin`. Quotes have them
too, with `GET /quotes?include=lineItems`.

The list is for reports. A line item that staff remove from an order
leaves the list, and no event tells you. To keep a copy of your orders,
use `GET /sales-orders` with `updated_since`, as
[Mirror your data in another system](/docs/recipes/mirror-data) shows.

## Report the margin on invoices

To report the margin on what you've invoiced, read your invoices, for
example the ones created since the start of October:

```http
GET /api/v1/invoices?created_gte=2026-10-01T00:00:00.000Z&limit=200
```

Add up `grossProfitBase` and `totalValueBase` across the invoices, and
divide the first by the second, to get one margin in your base currency.
Don't average each invoice's `grossMargin`, because that gives a small
invoice the same weight as a large one. To keep a copy of your invoices,
follow [Export invoices to accounting or BI](/docs/recipes/export-invoices).

## Know the limits

- A line item with no cost has a `lastCost` of 0 or `null`, and its
  `margin` is `null`. A line item that the API adds when Omono finds no
  cost saves a `lastCost` of 0, not `null`. Leave these line items out of
  your margin, or ask your staff to fill in the cost.
- An invoice's cost counts a line item with no cost as 0. An invoice whose
  goods have no cost at all has a `grossMargin` of `null`, but one with
  some uncosted goods has a margin higher than it should be.
- The figures are the ones Omono saved. A line item's `margin` stays as
  it was saved until the line item changes.
- The cost is the saved Last Cost, not what a build actually cost.
- The cost fields are `null` on some old records.

Learn more about [costs and margins](/docs/reading#costs).

## Errors to handle

| Status   | Code                                                                                                           | What to do                                                                                                                                                          |
| -------- | -------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 400      | [`malformed-request`](/docs/errors/malformed-request)                                                          | You sent a parameter that the list doesn't take, such as `companyId` on `GET /sales-order-lines`. Remove it.                                                        |
| 422      | [`validation-failed`](/docs/errors/validation-failed)                                                          | A query parameter is wrong, for example a `created_gte` that isn't a date and time. Fix the request. If `errors[].field` is `cursor`, start again without a cursor. |
| 429, 503 | [`rate-limited`](/docs/errors/rate-limited), [`temporarily-unavailable`](/docs/errors/temporarily-unavailable) | Retry after the number of seconds in `Retry-After`.                                                                                                                 |
