Context for managing warehouse inventory documents.
Provides draft CRUD, count-sheet seeding (active catalogue items only),
and transactional posting via Ecto.Multi.
Summary
Functions
Corrects the content (:track_value, :note, :lines) of an inventory
document without changing its status or touching stock.
Creates a new draft inventory document.
Sums line_total/1 across all lines in the document.
Returns {:ok, doc} or {:error, :not_found}.
Returns the document or raises.
Computes counted_quantity * unit_value for a single line map.
Lists non-deleted inventory documents. Ordered by number descending (newest first).
Builds an unsaved inventory document struct pre-seeded with lines from the
current stock at the configured default warehouse. Only items whose
catalogue card has status == "active" are included.
Posts an inventory document in an Ecto.Multi transaction.
Re-applies ABSOLUTE stock quantities for an already-posted document.
Builds seed lines for a new inventory draft, scoped to a single warehouse.
Sets the storage_folder_uuid on an inventory document.
Soft-deletes a draft document. Returns {:error, :not_draft} for posted documents.
Updates a draft document. Returns {:error, :not_draft} if the document
is not in draft status.
Updates created_by_uuid and/or performed_by_uuid on an inventory document.
Functions
Corrects the content (:track_value, :note, :lines) of an inventory
document without changing its status or touching stock.
Works on documents in any status. Returns {:ok, doc} or
{:error, changeset}.
Creates a new draft inventory document.
location_uuid is set programmatically — from attrs when given, otherwise
the configured default warehouse (the column is NOT NULL).
performed_by_uuid (the responsible person) defaults to the creator
(created_by_uuid) for a new document, unless given explicitly.
Sums line_total/1 across all lines in the document.
Returns {:ok, doc} or {:error, :not_found}.
Returns the document or raises.
Computes counted_quantity * unit_value for a single line map.
Lists non-deleted inventory documents. Ordered by number descending (newest first).
Builds an unsaved inventory document struct pre-seeded with lines from the
current stock at the configured default warehouse. Only items whose
catalogue card has status == "active" are included.
locale is explicit — do NOT rely on the process Gettext locale inside a
context module.
Posts an inventory document in an Ecto.Multi transaction.
- Reads current stock at the document's own warehouse
(
stock_map_for_location(doc.location_uuid)) once up front for auditprevious_*fields. - For each line: coerces quantities/values to Decimal; captures pre-post
stock as audit fields; upserts the stock row inside the transaction via
Multi.run/3(so all writes happen atomically). - Updates the document status to "posted" with
posted_atandperformed_by_uuid. - Returns
{:error, :not_draft}if the document is not in draft status. - Rolls back on any failure.
Re-applies ABSOLUTE stock quantities for an already-posted document.
Mirrors post_document/2 stock math exactly: reads current stock at the
document's own warehouse for audit previous_* fields, upserts each line
atomically, and re-stamps posted_at + performed_by_uuid.
Returns {:error, :not_posted} when the document is not in posted
status. Rolls back on any failure.
Builds seed lines for a new inventory draft, scoped to a single warehouse.
One line per stock row at location_uuid whose catalogue item exists AND
has status == "active". Fetches items via
PhoenixKitCatalogue.Catalogue.list_items_by_uuids/2 then filters
status == "active" in Elixir (that function only excludes
soft-deleted/status="deleted" items, so inactive/discontinued slip through).
Sets the storage_folder_uuid on an inventory document.
Works on documents in any status; returns {:ok, doc} or {:error, changeset}.
Soft-deletes a draft document. Returns {:error, :not_draft} for posted documents.
Updates a draft document. Returns {:error, :not_draft} if the document
is not in draft status.
Changing :location_uuid to a value different from the document's current
one always re-seeds :lines from that warehouse's current stock (see
seed_lines/2), replacing whatever lines were there before — lines from
the previous warehouse (manually counted or not) don't apply to a
different physical location.
Pass :locale (atom or string key) in attrs to localize the re-seeded
line names; without it, re-seeded lines fall back to each catalogue item's
default (untranslated) name — see seed_lines/2.
Locks the row FOR UPDATE and re-checks status == "draft" in the DB (not just the in-memory struct) so a stale tab cannot overwrite a document that was posted concurrently by another tab/user.
Updates created_by_uuid and/or performed_by_uuid on an inventory document.
Accepts a map with string or atom keys. Works on documents in any status.
Returns {:ok, doc} or {:error, changeset}.