PhoenixKitWarehouse.StockLedger (PhoenixKitWarehouse v0.1.0)

Copy Markdown View Source

Context for managing warehouse stock balances.

Provides functions to read stock levels and upsert quantities. Decimal coercion helpers ensure callers passing jsonb-origin strings or floats are handled safely.

Summary

Functions

UUID of the default warehouse Location stock is held at (setting), or nil.

Returns the current quantity for the given item UUID as a Decimal. Returns Decimal.new("0") if no row exists.

Conditionally decrements warehouse stock for item_uuid.

Returns all stock rows.

Additively increases the stock quantity for item_uuid.

Sets the default warehouse Location UUID. Pass nil to clear.

Sets the LocationType UUID that marks warehouses. Pass nil to clear.

Returns stock rows for the given list of item UUIDs.

Returns a map of item_uuid => %{quantity: Decimal, unit_value: Decimal | nil} for fast tree annotation.

Coerces a value to Decimal. nil and "" become Decimal.new("0").

Coerces a value to Decimal or nil. nil, blank strings, and empty strings return nil. All other values convert like to_decimal/1.

Returns the total stock value: Σ (quantity * unit_value), skipping rows where unit_value is nil.

Upserts the stock quantity for item_uuid.

UUID of the LocationType that marks warehouses (admin-configurable setting), or nil.

Functions

default_location_uuid()

UUID of the default warehouse Location stock is held at (setting), or nil.

get_quantity(item_uuid)

Returns the current quantity for the given item UUID as a Decimal. Returns Decimal.new("0") if no row exists.

issue_quantity(item_uuid, quantity, opts \\ [])

Conditionally decrements warehouse stock for item_uuid.

Performs an atomic UPDATE with WHERE quantity >= qty to guard against driving stock negative. Never inserts a row — if no stock row exists for the item/location, the WHERE predicate matches 0 rows and the function returns {:error, {:insufficient_stock, item_uuid}}.

Options:

Returns:

  • {:ok, new_quantity} on success (Decimal).
  • {:error, {:insufficient_stock, item_uuid}} when stock row is missing OR when quantity < qty (covers both cases atomically via the WHERE guard).

list_stock()

Returns all stock rows.

receive_quantity(item_uuid, quantity, opts \\ [])

Additively increases the stock quantity for item_uuid.

Unlike upsert_quantity/3 which does an absolute SET, this function performs an additive INSERT … ON CONFLICT DO UPDATE SET quantity = quantity + EXCLUDED.quantity.

Options:

  • :unit_value — when not nil, also sets the unit_value; when nil, leaves existing value intact.
  • :repo — override the repo (default from PhoenixKit.RepoHelper.repo/0), used by Ecto.Multi transactions.
  • :location_uuid — warehouse location (default: configured default warehouse).

Returns {:ok, %Stock{}}.

set_default_location_uuid(uuid)

Sets the default warehouse Location UUID. Pass nil to clear.

set_warehouse_location_type_uuid(uuid)

Sets the LocationType UUID that marks warehouses. Pass nil to clear.

stock_for_items(item_uuids, target_repo \\ nil)

Returns stock rows for the given list of item UUIDs.

stock_map()

Returns a map of item_uuid => %{quantity: Decimal, unit_value: Decimal | nil} for fast tree annotation.

to_decimal(v)

Coerces a value to Decimal. nil and "" become Decimal.new("0").

to_decimal_or_nil(s)

Coerces a value to Decimal or nil. nil, blank strings, and empty strings return nil. All other values convert like to_decimal/1.

total_value()

Returns the total stock value: Σ (quantity * unit_value), skipping rows where unit_value is nil.

upsert_quantity(item_uuid, quantity, opts \\ [])

Upserts the stock quantity for item_uuid.

Options:

  • :unit_value — when not nil, also sets the unit_value; when nil, leaves existing value intact.
  • :repo — override the repo (default from PhoenixKit.RepoHelper.repo/0), used by Ecto.Multi transactions.

Returns {:ok, %Stock{}}.

warehouse_location_type_uuid()

UUID of the LocationType that marks warehouses (admin-configurable setting), or nil.