Recording Stock Movements
Every change to stock — a transfer validated, a count adjusted, goods scrapped, a till
sale, a repair consuming a part — goes through one call: TransferMove.record. It writes
the immutable move, updates stock levels, writes the cost layers that value the move, the
stock ledger entry, lot traceability, and (with perpetual valuation on) the journal entry.
A module that moves stock calls it; it never writes stock levels or cost layers itself.
TransferMove = get_model("TransferMove")move = await TransferMove.record( product_variant=variant, quantity=5, # in the product's own unit source_location=supplier_loc, dest_location=stock_loc, move_type="Receipt", # Receipt, Delivery, Internal, Adjustment, Scrap, Dropship unit_cost=12.40, # what an incoming move cost, in company currency origin="PO-0042", move_date=arrived_at, # optional: when it happened (naive UTC); now by default)quantity is always in the product’s stock unit, and so is the move: its unit of measure
is the product’s, set by the move itself — there is no unit to pass. A document that works
in another unit (a line in Dozens, a pack of 6) converts first, then records the stock-unit
quantity; the move, its cost layers and its stock ledger entries all state that quantity in
the stock unit.
The effective date: move_date
Section titled “The effective date: move_date”move_date is when the goods moved, and it is the only date the stock side of the
system reads. Cost layers are valued at it, the journal entry is dated on it, the stock
ledger orders by it, and every report keyed on stock dates (valuation, aging, turnover,
move history) reads it. When the move was recorded is the record’s ordinary
created_date. A transfer passes its Effective Date (blank = the moment it is
validated) for every line.
record refuses a move_date:
- in the future;
- on or before the company’s lock date — the same date that closes its journal entries
(
JournalEntry.locked_through(company_id), below); - that would leave a storage location short at that date or at any later movement, unless negative stock is allowed (the rule the stock level applies to now, carried back to the date given).
Each refusal raises UserError before anything is written.
Back-dated movements are valued in date order
Section titled “Back-dated movements are valued in date order”A move_date earlier than movements already recorded for the same product re-costs the
moves after it, so the result is exactly what recording them in date order would have
produced:
| Costing method | What changes |
|---|---|
| FIFO | Later issues are valued against the layers open at their own dates: a back-dated receipt is consumed before anything received after it. Relocations are followed, so goods picked to an output location and shipped from there are re-costed too. |
| Average | The running average is rebuilt from the back date; every later issue takes the average of its own day. |
| Standard | Nothing but the move’s own date — every unit is worth the standard. |
A re-costed move keeps its identity: its old cost layers are archived (kept, visible under the Archived filter, counted by nothing) and replacement layers take their place, and the difference is posted as one correcting journal entry dated on that move’s own date. Because a back date inside a locked period is refused and every re-costed move is dated after it, a correction never lands in a closed period. Negative stock at the back date follows the ordinary rule (the shortfall is valued at the product cost and trued up by the next receipt). Value added to goods outside a move — a landed cost, a vendor bill’s price difference — stays with those goods.
A back date is refused when, after it, the product was revalued: its category’s costing method changed, a standard cost was changed, or a production run consuming or making it was settled. Each of those measured the stock on hand on its own date, and re-costing past it would mean re-running it.
Keeping a derived figure in step: CostLayer._after_recost
Section titled “Keeping a derived figure in step: CostLayer._after_recost”If your module stores something derived from what a move was valued at (a recognized cost
of goods sold, a commission on margin), refresh it when moves are re-costed. Extend
CostLayer and override _after_recost(company, moves); it is called once per back-dated
movement with every move whose value was rebuilt, after their correcting entries are
posted. Call super() first — other modules answer it too.
class CostLayerMyModule(Model): __inherit__ = "CostLayer"
async def _after_recost(cls, company, moves): await super()._after_recost(company, moves) for move in moves: await get_model("MyMarginLine").refresh_for_move(move)The lock date: JournalEntry.locked_through
Section titled “The lock date: JournalEntry.locked_through”await get_model("JournalEntry").locked_through(company_id) returns the last day of the
company’s closed books (a date), or None when nothing is locked. It is the one answer to
“how far are this company’s books closed”: the journal entry lock uses it, stock movements
use it, and any module that writes into an accounting period should ask it before doing so
rather than reading a setting directly. The tax lock date is separate — it closes a filed
tax return and only refuses entries that carry a tax line.