# Expenditure and transfer — business logic

Expenditure writes goods off stock without a sale to a customer: a transfer
between the company's own warehouses, a shipment with no revenue, a
write-off, a return to a supplier. It differs from selling in exactly this
respect — money isn't recorded as revenue on an expenditure document (see
[Selling](selling_en.md) for the opposite case; both share the same
processing).

## Goods are taken only from real stock

An expenditure line isn't a free-form position — it's a "slice" of a
specific warehouse lot at the moment it's picked: the item, quantity, and
lot price are fixed into the document line. You can't write off more than
is in stock — this check is mandatory both when the line is picked and
again during processing. If the lot changed between picking and processing
(someone else processed another document in between) — processing is
rejected, not carried out against stale data. If two documents are
processed against the same lot at the very same moment, they don't
interfere: the second waits for the first to finish and checks the
quantity that actually remains.

If a single lot doesn't hold enough of an item, expenditure is
automatically split across several lots on a "oldest first" basis, with
user confirmation.

## What happens on processing

For each line:

1. Source lot check — the item and price must match what's fixed in the
   line (otherwise processing is rejected).
2. Quantity check — the lot must have enough.
3. Write-off from the lot: quantity is reduced; if the remainder reaches
   zero, the lot is marked inactive but not deleted (lots are never
   deleted, only exhausted — that's what makes up the history).
4. Reservation release — if there was a soft reservation on this item for
   an order or document, the reservation is reduced by the shipped
   quantity (never more than existed).
5. If a destination warehouse is specified — this is a transfer: a new lot
   is created (or topped up) at the destination. The cost basis at the
   destination becomes equal to the shipping price of the source line —
   meaning a transfer can "raise" or "lower" the item's booked cost at the
   new warehouse. The receiving side of the transfer is created
   automatically, already in a processed state — the user doesn't enter it
   separately.

**Why the price can change on a transfer:** cost basis isn't an abstract
number, it's a property of a specific lot. The lot physically moves to
another warehouse at whatever price it was shipped at — if that price
differs from the original (say, it includes a share of shipping costs), the
lot legitimately costs more or less at the new warehouse.

## Reservation isn't always released

Reservation release on expenditure only happens if a partner is specified
("shipment on credit" to a specific counterparty) — an ordinary transfer
between the company's own warehouses doesn't touch reservations, because it
isn't "promised" to anyone. For selling, the reservation is always released
(see [Selling](selling_en.md)).

## Debt or payment

Same logic as receiving, just in the opposite direction: if a partner is
specified but no account is, the document amount is subtracted from their
balance, creating a debt ("shipped on credit"). If an account is specified,
a cash document twin is automatically created on processing for the
expenditure amount — money is considered received right away, the
partner's balance isn't adjusted.

## Rolling back expenditure and transfers

A rollback returns the quantity to the source lot, puts back the
reservations the shipment used up, returns written-off serial numbers and
reduces the order's shipped quantity. For a credit shipment the amount is
added back to the partner's balance; for a paid one the automatic payment
document disappears with it. A transfer is rolled back together with its
receiving side at the destination — but only while the goods are still
there: if something has already been taken from the destination lot, the
rollback is refused until that later document is rolled back.

## Reporting: transfers are counted separately

Ordinary receiving/expenditure registers deliberately exclude transfers —
the goods never left the company, so for a "how much left for good" report
that isn't an event. Transfers have their own register instead, one
warehouse at a time, with "gave" and "received" columns (a single warehouse
can be both source and destination across different documents in a period).
