# `PhoenixKitWarehouse.Inventories`
[🔗](https://github.com/BeamLabEU/phoenix_kit_warehouse/blob/0.4.0/lib/phoenix_kit_warehouse/inventories.ex#L1)

Context for managing warehouse inventory documents.

Provides draft CRUD, count-sheet seeding (active catalogue items only),
and transactional posting via `Ecto.Multi`.

# `correct_document`

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`

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`

Sums `line_total/1` across all lines in the document.

# `get_document`

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

# `get_document!`

Returns the document or raises.

# `line_total`

Computes `counted_quantity * unit_value` for a single line map.

# `list_documents`

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

# `new_draft`

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.

# `post_document`

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

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.

# `seed_lines`

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

# `set_storage_folder`

Sets the `storage_folder_uuid` on an inventory document.

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

# `soft_delete_document`

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

# `update_draft`

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.

# `update_responsibility`

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
