pub struct GeneralLedger { /* private fields */ }Expand description
General Ledger interface.
Implementations§
Source§impl GeneralLedger
impl GeneralLedger
Sourcepub fn create_account(&self, input: CreateGlAccount) -> Result<GlAccount>
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()
})?;Sourcepub fn get_account_by_number(
&self,
account_number: &str,
) -> Result<Option<GlAccount>>
pub fn get_account_by_number( &self, account_number: &str, ) -> Result<Option<GlAccount>>
Get a GL account by account number.
Sourcepub fn update_account(
&self,
id: Uuid,
input: UpdateGlAccount,
) -> Result<GlAccount>
pub fn update_account( &self, id: Uuid, input: UpdateGlAccount, ) -> Result<GlAccount>
Update a GL account.
Sourcepub fn list_accounts(&self, filter: GlAccountFilter) -> Result<Vec<GlAccount>>
pub fn list_accounts(&self, filter: GlAccountFilter) -> Result<Vec<GlAccount>>
List GL accounts with filtering.
Sourcepub fn get_account_hierarchy(&self) -> Result<Vec<GlAccount>>
pub fn get_account_hierarchy(&self) -> Result<Vec<GlAccount>>
Get the full account hierarchy.
Sourcepub fn delete_account(&self, id: Uuid) -> Result<()>
pub fn delete_account(&self, id: Uuid) -> Result<()>
Delete a GL account (only if no transactions).
Sourcepub fn initialize_chart_of_accounts(&self) -> Result<Vec<GlAccount>>
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());Sourcepub fn create_period(&self, input: CreateGlPeriod) -> Result<GlPeriod>
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(),
})?;Sourcepub fn get_current_period(&self) -> Result<Option<GlPeriod>>
pub fn get_current_period(&self) -> Result<Option<GlPeriod>>
Get the current open period.
Sourcepub fn get_period_for_date(&self, date: NaiveDate) -> Result<Option<GlPeriod>>
pub fn get_period_for_date(&self, date: NaiveDate) -> Result<Option<GlPeriod>>
Get the period for a specific date.
Sourcepub fn list_periods(&self, filter: GlPeriodFilter) -> Result<Vec<GlPeriod>>
pub fn list_periods(&self, filter: GlPeriodFilter) -> Result<Vec<GlPeriod>>
List periods with filtering.
Sourcepub fn open_period(&self, id: Uuid) -> Result<GlPeriod>
pub fn open_period(&self, id: Uuid) -> Result<GlPeriod>
Open a period (transition from future to open).
Sourcepub fn close_period(&self, id: Uuid, closed_by: &str) -> Result<GlPeriod>
pub fn close_period(&self, id: Uuid, closed_by: &str) -> Result<GlPeriod>
Close a period (no more postings allowed except adjustments).
Sourcepub fn lock_period(&self, id: Uuid, locked_by: &str) -> Result<GlPeriod>
pub fn lock_period(&self, id: Uuid, locked_by: &str) -> Result<GlPeriod>
Lock a period (permanently closed, no changes allowed).
Sourcepub fn reopen_period(&self, id: Uuid) -> Result<GlPeriod>
pub fn reopen_period(&self, id: Uuid) -> Result<GlPeriod>
Reopen a closed period (for adjustments).
Sourcepub fn create_journal_entry(
&self,
input: CreateJournalEntry,
) -> Result<JournalEntry>
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()
})?;Sourcepub fn get_journal_entry(&self, id: Uuid) -> Result<Option<JournalEntry>>
pub fn get_journal_entry(&self, id: Uuid) -> Result<Option<JournalEntry>>
Get a journal entry by ID.
Sourcepub fn get_journal_entry_by_number(
&self,
number: &str,
) -> Result<Option<JournalEntry>>
pub fn get_journal_entry_by_number( &self, number: &str, ) -> Result<Option<JournalEntry>>
Get a journal entry by entry number.
Sourcepub fn list_journal_entries(
&self,
filter: JournalEntryFilter,
) -> Result<Vec<JournalEntry>>
pub fn list_journal_entries( &self, filter: JournalEntryFilter, ) -> Result<Vec<JournalEntry>>
List journal entries with filtering.
Sourcepub fn post_journal_entry(
&self,
id: Uuid,
posted_by: &str,
) -> Result<JournalEntry>
pub fn post_journal_entry( &self, id: Uuid, posted_by: &str, ) -> Result<JournalEntry>
Post a journal entry (update account balances).
Sourcepub fn void_journal_entry(&self, id: Uuid) -> Result<JournalEntry>
pub fn void_journal_entry(&self, id: Uuid) -> Result<JournalEntry>
Void a posted journal entry.
Sourcepub fn reverse_journal_entry(
&self,
id: Uuid,
reversal_date: NaiveDate,
) -> Result<JournalEntry>
pub fn reverse_journal_entry( &self, id: Uuid, reversal_date: NaiveDate, ) -> Result<JournalEntry>
Reverse a journal entry (create an offsetting entry).
Sourcepub fn get_journal_entry_lines(
&self,
journal_entry_id: Uuid,
) -> Result<Vec<JournalEntryLine>>
pub fn get_journal_entry_lines( &self, journal_entry_id: Uuid, ) -> Result<Vec<JournalEntryLine>>
Get journal entry lines for an entry.
Sourcepub fn get_auto_posting_config(&self) -> Result<Option<AutoPostingConfig>>
pub fn get_auto_posting_config(&self) -> Result<Option<AutoPostingConfig>>
Get the current auto-posting configuration.
Sourcepub fn set_auto_posting_config(
&self,
input: CreateAutoPostingConfig,
) -> Result<AutoPostingConfig>
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()
})?;Sourcepub fn auto_post_invoice(&self, invoice_id: Uuid) -> Result<JournalEntry>
pub fn auto_post_invoice(&self, invoice_id: Uuid) -> Result<JournalEntry>
Auto-post a customer invoice (debit AR, credit Revenue).
Sourcepub fn auto_post_payment_received(
&self,
payment_id: Uuid,
) -> Result<JournalEntry>
pub fn auto_post_payment_received( &self, payment_id: Uuid, ) -> Result<JournalEntry>
Auto-post a payment received (debit Cash, credit AR).
Sourcepub fn auto_post_bill(&self, bill_id: Uuid) -> Result<JournalEntry>
pub fn auto_post_bill(&self, bill_id: Uuid) -> Result<JournalEntry>
Auto-post a supplier bill (debit Inventory/Expense, credit AP).
Sourcepub fn auto_post_bill_payment(&self, payment_id: Uuid) -> Result<JournalEntry>
pub fn auto_post_bill_payment(&self, payment_id: Uuid) -> Result<JournalEntry>
Auto-post a bill payment (debit AP, credit Cash).
Sourcepub fn auto_post_inventory_cost(
&self,
cost_transaction_id: Uuid,
) -> Result<JournalEntry>
pub fn auto_post_inventory_cost( &self, cost_transaction_id: Uuid, ) -> Result<JournalEntry>
Auto-post inventory cost (COGS on sale).
Sourcepub fn auto_post_write_off(&self, write_off_id: Uuid) -> Result<JournalEntry>
pub fn auto_post_write_off(&self, write_off_id: Uuid) -> Result<JournalEntry>
Auto-post a write-off (debit Bad Debt Expense, credit AR).
Sourcepub fn get_trial_balance(&self, as_of_date: NaiveDate) -> Result<TrialBalance>
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);Sourcepub fn get_balance_sheet(&self, as_of_date: NaiveDate) -> Result<BalanceSheet>
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);Sourcepub fn get_income_statement(
&self,
start_date: NaiveDate,
end_date: NaiveDate,
) -> Result<IncomeStatement>
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);Sourcepub fn get_account_balance(
&self,
account_id: Uuid,
as_of_date: Option<NaiveDate>,
) -> Result<Option<Decimal>>
pub fn get_account_balance( &self, account_id: Uuid, as_of_date: Option<NaiveDate>, ) -> Result<Option<Decimal>>
Get the current balance of an account.
Sourcepub fn get_account_transactions(
&self,
account_id: Uuid,
filter: JournalEntryFilter,
) -> Result<Vec<JournalEntryLine>>
pub fn get_account_transactions( &self, account_id: Uuid, filter: JournalEntryFilter, ) -> Result<Vec<JournalEntryLine>>
Get all transactions for an account.
Sourcepub fn revalue(
&self,
as_of_date: NaiveDate,
base_currency: Option<Currency>,
) -> Result<RevaluationResult>
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.
Sourcepub fn run_period_close(
&self,
period_id: Uuid,
closed_by: &str,
) -> Result<JournalEntry>
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:
- Generate income statement for the period
- Create closing entries to zero out revenue/expense accounts
- Transfer net income to retained earnings
- Close the period
Sourcepub fn close_month(
&self,
period_id: Uuid,
options: CloseMonthOptions,
) -> Result<CloseMonthReport>
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:
- Post scheduled depreciation due through period end for all in-service assets.
- Recognize deferred revenue through period end for active contracts.
- 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).
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.
Sourcepub fn create_accounts_batch(
&self,
inputs: Vec<CreateGlAccount>,
) -> Result<BatchResult<GlAccount>>
pub fn create_accounts_batch( &self, inputs: Vec<CreateGlAccount>, ) -> Result<BatchResult<GlAccount>>
Create multiple accounts in batch.