Skip to main content

GeneralLedger

Struct GeneralLedger 

Source
pub struct GeneralLedger { /* private fields */ }
Expand description

General Ledger interface.

Implementations§

Source§

impl GeneralLedger

Source

pub fn create_account(&self, input: CreateGlAccount) -> Result<GlAccount>

Create a new GL account.

§Example
use stateset_embedded::{Commerce, CreateGlAccount, AccountType, AccountSubType, CurrencyCode};

let commerce = Commerce::new(":memory:")?;

let account = commerce.general_ledger().create_account(CreateGlAccount {
    account_number: "1000".into(),
    name: "Cash".into(),
    description: Some("Operating cash account".into()),
    account_type: AccountType::Asset,
    account_sub_type: Some(AccountSubType::Cash),
    is_posting: Some(true),
    currency: Some(CurrencyCode::USD),
    ..Default::default()
})?;
Source

pub fn get_account(&self, id: Uuid) -> Result<Option<GlAccount>>

Get a GL account by ID.

Source

pub fn get_account_by_number( &self, account_number: &str, ) -> Result<Option<GlAccount>>

Get a GL account by account number.

Source

pub fn update_account( &self, id: Uuid, input: UpdateGlAccount, ) -> Result<GlAccount>

Update a GL account.

Source

pub fn list_accounts(&self, filter: GlAccountFilter) -> Result<Vec<GlAccount>>

List GL accounts with filtering.

Source

pub fn get_account_hierarchy(&self) -> Result<Vec<GlAccount>>

Get the full account hierarchy.

Source

pub fn delete_account(&self, id: Uuid) -> Result<()>

Delete a GL account (only if no transactions).

Source

pub fn initialize_chart_of_accounts(&self) -> Result<Vec<GlAccount>>

Initialize the standard chart of accounts.

Creates a default set of accounts for Assets, Liabilities, Equity, Revenue, and Expenses.

§Example
use stateset_embedded::Commerce;

let commerce = Commerce::new(":memory:")?;

let accounts = commerce.general_ledger().initialize_chart_of_accounts()?;
println!("Created {} standard accounts", accounts.len());
Source

pub fn create_period(&self, input: CreateGlPeriod) -> Result<GlPeriod>

Create a new accounting period.

§Example
use stateset_embedded::{Commerce, CreateGlPeriod};
use chrono::NaiveDate;

let commerce = Commerce::new(":memory:")?;

let period = commerce.general_ledger().create_period(CreateGlPeriod {
    period_name: "January 2025".into(),
    fiscal_year: 2025,
    period_number: 1,
    start_date: NaiveDate::from_ymd_opt(2025, 1, 1).unwrap(),
    end_date: NaiveDate::from_ymd_opt(2025, 1, 31).unwrap(),
})?;
Source

pub fn get_period(&self, id: Uuid) -> Result<Option<GlPeriod>>

Get a period by ID.

Source

pub fn get_current_period(&self) -> Result<Option<GlPeriod>>

Get the current open period.

Source

pub fn get_period_for_date(&self, date: NaiveDate) -> Result<Option<GlPeriod>>

Get the period for a specific date.

Source

pub fn list_periods(&self, filter: GlPeriodFilter) -> Result<Vec<GlPeriod>>

List periods with filtering.

Source

pub fn open_period(&self, id: Uuid) -> Result<GlPeriod>

Open a period (transition from future to open).

Source

pub fn close_period(&self, id: Uuid, closed_by: &str) -> Result<GlPeriod>

Close a period (no more postings allowed except adjustments).

Source

pub fn lock_period(&self, id: Uuid, locked_by: &str) -> Result<GlPeriod>

Lock a period (permanently closed, no changes allowed).

Source

pub fn reopen_period(&self, id: Uuid) -> Result<GlPeriod>

Reopen a closed period (for adjustments).

Source

pub fn create_journal_entry( &self, input: CreateJournalEntry, ) -> Result<JournalEntry>

Create a journal entry.

§Example
use stateset_embedded::{Commerce, CreateJournalEntry, CreateJournalEntryLine};
use rust_decimal_macros::dec;
use chrono::NaiveDate;
use uuid::Uuid;

let commerce = Commerce::new(":memory:")?;

// Debit Cash, Credit Sales Revenue
let entry = commerce.general_ledger().create_journal_entry(CreateJournalEntry {
    entry_date: NaiveDate::from_ymd_opt(2025, 1, 15).unwrap(),
    description: "Cash sale".into(),
    lines: vec![
        CreateJournalEntryLine::debit(cash_account_id, dec!(100.00), Some("Cash received".into())),
        CreateJournalEntryLine::credit(sales_account_id, dec!(100.00), Some("Sales revenue".into())),
    ],
    auto_post: Some(true),
    ..Default::default()
})?;
Source

pub fn get_journal_entry(&self, id: Uuid) -> Result<Option<JournalEntry>>

Get a journal entry by ID.

Source

pub fn get_journal_entry_by_number( &self, number: &str, ) -> Result<Option<JournalEntry>>

Get a journal entry by entry number.

Source

pub fn list_journal_entries( &self, filter: JournalEntryFilter, ) -> Result<Vec<JournalEntry>>

List journal entries with filtering.

Source

pub fn post_journal_entry( &self, id: Uuid, posted_by: &str, ) -> Result<JournalEntry>

Post a journal entry (update account balances).

Source

pub fn void_journal_entry(&self, id: Uuid) -> Result<JournalEntry>

Void a posted journal entry.

Source

pub fn reverse_journal_entry( &self, id: Uuid, reversal_date: NaiveDate, ) -> Result<JournalEntry>

Reverse a journal entry (create an offsetting entry).

Source

pub fn get_journal_entry_lines( &self, journal_entry_id: Uuid, ) -> Result<Vec<JournalEntryLine>>

Get journal entry lines for an entry.

Source

pub fn get_auto_posting_config(&self) -> Result<Option<AutoPostingConfig>>

Get the current auto-posting configuration.

Source

pub fn set_auto_posting_config( &self, input: CreateAutoPostingConfig, ) -> Result<AutoPostingConfig>

Set up auto-posting configuration.

The optional auto_post_depreciation and auto_post_revenue_recognition flags (default off) additionally auto-post journal entries when fixed-asset depreciation is posted or deferred revenue is recognized.

§Example
use stateset_embedded::{Commerce, CreateAutoPostingConfig};
use uuid::Uuid;

let commerce = Commerce::new(":memory:")?;

// Set up automatic GL postings for commerce transactions
commerce.general_ledger().set_auto_posting_config(CreateAutoPostingConfig {
    config_name: "Default".into(),
    cash_account_id: cash_id,
    accounts_receivable_account_id: ar_id,
    inventory_account_id: inv_id,
    accounts_payable_account_id: ap_id,
    sales_revenue_account_id: revenue_id,
    cogs_account_id: cogs_id,
    ..Default::default()
})?;
Source

pub fn auto_post_invoice(&self, invoice_id: Uuid) -> Result<JournalEntry>

Auto-post a customer invoice (debit AR, credit Revenue).

Source

pub fn auto_post_payment_received( &self, payment_id: Uuid, ) -> Result<JournalEntry>

Auto-post a payment received (debit Cash, credit AR).

Source

pub fn auto_post_bill(&self, bill_id: Uuid) -> Result<JournalEntry>

Auto-post a supplier bill (debit Inventory/Expense, credit AP).

Source

pub fn auto_post_bill_payment(&self, payment_id: Uuid) -> Result<JournalEntry>

Auto-post a bill payment (debit AP, credit Cash).

Source

pub fn auto_post_inventory_cost( &self, cost_transaction_id: Uuid, ) -> Result<JournalEntry>

Auto-post inventory cost (COGS on sale).

Source

pub fn auto_post_write_off(&self, write_off_id: Uuid) -> Result<JournalEntry>

Auto-post a write-off (debit Bad Debt Expense, credit AR).

Source

pub fn get_trial_balance(&self, as_of_date: NaiveDate) -> Result<TrialBalance>

Generate a trial balance.

§Example
use stateset_embedded::Commerce;
use chrono::NaiveDate;

let commerce = Commerce::new(":memory:")?;

let trial_balance = commerce.general_ledger().get_trial_balance(
    NaiveDate::from_ymd_opt(2025, 1, 31).unwrap()
)?;

println!("Trial Balance as of {}", trial_balance.as_of_date);
println!("Total Debits: ${}", trial_balance.total_debits);
println!("Total Credits: ${}", trial_balance.total_credits);
println!("Is Balanced: {}", trial_balance.is_balanced);
Source

pub fn get_balance_sheet(&self, as_of_date: NaiveDate) -> Result<BalanceSheet>

Generate a balance sheet.

§Example
use stateset_embedded::Commerce;
use chrono::NaiveDate;

let commerce = Commerce::new(":memory:")?;

let balance_sheet = commerce.general_ledger().get_balance_sheet(
    NaiveDate::from_ymd_opt(2025, 1, 31).unwrap()
)?;

println!("Balance Sheet as of {}", balance_sheet.as_of_date);
println!("Total Assets: ${}", balance_sheet.total_assets);
println!("Total Liabilities: ${}", balance_sheet.total_liabilities);
println!("Total Equity: ${}", balance_sheet.total_equity);
Source

pub fn get_income_statement( &self, start_date: NaiveDate, end_date: NaiveDate, ) -> Result<IncomeStatement>

Generate an income statement.

§Example
use stateset_embedded::Commerce;
use chrono::NaiveDate;

let commerce = Commerce::new(":memory:")?;

let income_statement = commerce.general_ledger().get_income_statement(
    NaiveDate::from_ymd_opt(2025, 1, 1).unwrap(),
    NaiveDate::from_ymd_opt(2025, 1, 31).unwrap(),
)?;

println!("Income Statement {} to {}", income_statement.period_start, income_statement.period_end);
println!("Total Revenue: ${}", income_statement.total_revenue);
println!("Total Expenses: ${}", income_statement.total_expenses);
println!("Net Income: ${}", income_statement.net_income);
Source

pub fn get_account_balance( &self, account_id: Uuid, as_of_date: Option<NaiveDate>, ) -> Result<Option<Decimal>>

Get the current balance of an account.

Source

pub fn get_account_transactions( &self, account_id: Uuid, filter: JournalEntryFilter, ) -> Result<Vec<JournalEntryLine>>

Get all transactions for an account.

Source

pub fn revalue( &self, as_of_date: NaiveDate, base_currency: Option<Currency>, ) -> Result<RevaluationResult>

Revalue foreign-currency account balances at the as-of exchange rate.

For every active posting account whose currency differs from the base currency, the outstanding foreign-currency balance is revalued at the current exchange rate and the net unrealized gain/loss is posted as a balanced adjusting journal entry against the configured FX gain/loss account.

base_currency defaults to the store’s configured base currency.

Source

pub fn run_period_close( &self, period_id: Uuid, closed_by: &str, ) -> Result<JournalEntry>

Run period close process (generate closing entries, close period).

This will:

  1. Generate income statement for the period
  2. Create closing entries to zero out revenue/expense accounts
  3. Transfer net income to retained earnings
  4. Close the period
Source

pub fn close_month( &self, period_id: Uuid, options: CloseMonthOptions, ) -> Result<CloseMonthReport>

Close the month: orchestrate every period-end step in order.

Runs, composing existing operations:

  1. Post scheduled depreciation due through period end for all in-service assets.
  2. Recognize deferred revenue through period end for active contracts.
  3. FX revaluation as of period end — skipped silently when there are no foreign-currency accounts or no FX gain/loss account is configured (the close never fails on this step).
  4. run_period_close (closing entries + close period).

With options.dry_run the candidates for each step are computed via read APIs and reported without writing anything. Per-asset and per-obligation failures are collected as warnings on the step report and do not abort the close.

Source

pub fn create_accounts_batch( &self, inputs: Vec<CreateGlAccount>, ) -> Result<BatchResult<GlAccount>>

Create multiple accounts in batch.

Source

pub fn get_accounts_batch(&self, ids: Vec<Uuid>) -> Result<Vec<GlAccount>>

Get multiple accounts by IDs.

Trait Implementations§

Source§

impl Debug for GeneralLedger

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more