PhoenixKitWarehouse.Inventories (PhoenixKitWarehouse v0.1.0)

Copy Markdown View Source

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. 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.

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

correct_document(doc, attrs)

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}.

create_draft(attrs)

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.

document_total(inventory_document)

Sums line_total/1 across all lines in the document.

get_document(uuid)

Returns {:ok, doc} or {:error, :not_found}.

get_document!(uuid)

Returns the document or raises.

line_total(line)

Computes counted_quantity * unit_value for a single line map.

list_documents(opts)

Lists non-deleted inventory documents. Ordered by number descending (newest first).

new_draft(locale, opts \\ [])

Builds an unsaved inventory document struct pre-seeded with lines from the current stock. 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.

post_document(doc, performed_by_uuid)

Posts an inventory document in an Ecto.Multi transaction.

  • Reads current stock_map() once up front for audit previous_* 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_at and performed_by_uuid.
  • Returns {:error, :not_draft} if the document is not in draft status.
  • Rolls back on any failure.

repost_document(doc, performed_by_uuid)

Re-applies ABSOLUTE stock quantities for an already-posted document.

Mirrors post_document/2 stock math exactly: reads current stock 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.

seed_lines(locale)

Builds seed lines for a new inventory draft.

One line per stock row 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).

set_storage_folder(doc, storage_folder_uuid)

Sets the storage_folder_uuid on an inventory document.

Works on documents in any status; returns {:ok, doc} or {:error, changeset}.

soft_delete_document(doc, actor_uuid)

Soft-deletes a draft document. Returns {:error, :not_draft} for posted documents.

update_draft(inventory_document, attrs)

Updates a draft document. Returns {:error, :not_draft} if the document is not in draft status.

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.

update_responsibility(doc, attrs)

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}.