Logistiki.Accounting (logistiki v0.1.0)

Copy Markdown View Source

The context for internal accounting artifacts: journals and postings.

Applications should normally publish business events (Logistiki.process/1) rather than calling these functions directly. The functions here are exposed for tests, migration, support tools, and controlled administrative operations.

Summary

Functions

Administrative helper to build a draft journal directly from attrs (for tests, migration, and support tools). Not the normal application-facing API.

Fetches a journal by id.

Fetches a journal by id, raising if not found.

Lists journals, optionally filtered, with postings preloaded.

Lists postings for journal, ordered by sequence.

Posts a draft journal and its postings.

Reverses a posted journal by creating a new posted reversal journal that exactly negates the original postings, and marks the original reversed.

Functions

draft_journal(attrs)

Administrative helper to build a draft journal directly from attrs (for tests, migration, and support tools). Not the normal application-facing API.

get_journal(id)

(since 0.1.0)

Fetches a journal by id.

Arguments

  • idinteger() — the journal primary key.

Returns

  • {:ok, %Journal{}} — the journal with postings preloaded.
  • {:error, :not_found} — no journal with that id.

Examples

iex> {:ok, journal} = Logistiki.Accounting.get_journal(1)
iex> journal.status
"posted"
iex> {:error, :not_found} = Logistiki.Accounting.get_journal(999)

get_journal!(id)

(since 0.1.0)

Fetches a journal by id, raising if not found.

Arguments

  • idinteger() — the journal primary key.

Returns

Examples

iex> journal = Logistiki.Accounting.get_journal!(1)
iex> journal.status
"posted"
iex> journal.postings
[%Posting{...}, %Posting{...}]

list_journals(opts \\ [])

(since 0.1.0)

Lists journals, optionally filtered, with postings preloaded.

Arguments

  • optskeyword() of options:
    • :statusString.t — e.g. "posted", "draft", "reversed"
    • :event_idString.t — filter by originating event id

Returns

  • [Journal.t()] — ordered by inserted_at descending, with :postings preloaded. Empty list if none match.

Examples

iex> Logistiki.Accounting.list_journals(status: "posted")
[%Journal{status: "posted", postings: [%Posting{...}, ...]}, ...]

iex> Logistiki.Accounting.list_journals(event_id: "evt_001")
[%Journal{event_id: "evt_001", ...}]

list_postings(journal)

(since 0.1.0)

Lists postings for journal, ordered by sequence.

Arguments

  • journal%Journal{} — the journal whose postings to list.

Returns

  • [Posting.t()] — ordered by sequence ascending.

Examples

iex> Logistiki.Accounting.list_postings(journal)
[%Posting{sequence: 1, ...}, %Posting{sequence: 2, ...}]

post_journal(journal, postings)

(since 0.1.0)

Posts a draft journal and its postings.

Validates the full invariant set, resolves virtual_account_id for each posting from its account_code, then inserts the journal (status posted) and its postings in a single transaction.

Returns {:ok, posted_journal} or {:error, %Logistiki.Error{}}.

reverse_journal(journal, attrs \\ %{})

Reverses a posted journal by creating a new posted reversal journal that exactly negates the original postings, and marks the original reversed.

Returns {:ok, reversal_journal} or {:error, %Logistiki.Error{}}.