Skip to main content

RunningBalanceTracker

Struct RunningBalanceTracker 

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

Tracks running balances for all accounts across companies.

Implementations§

Source§

impl RunningBalanceTracker

Source

pub fn new_with_currency_and_framework( config: BalanceTrackerConfig, currency: String, framework: &str, ) -> Self

Creates a new balance tracker with the specified currency and accounting framework.

Source

pub fn new_with_currency(config: BalanceTrackerConfig, currency: String) -> Self

Creates a new balance tracker with the specified currency (defaults to US GAAP).

Source

pub fn new(config: BalanceTrackerConfig) -> Self

Creates a new balance tracker (defaults to USD and US GAAP).

Source

pub fn new_with_framework(config: BalanceTrackerConfig, framework: &str) -> Self

Creates a new balance tracker for a specific accounting framework (defaults to USD).

Source

pub fn with_defaults() -> Self

Creates a tracker with default configuration (US GAAP).

Source

pub fn register_account_type( &mut self, account_code: &str, account_type: AccountType, )

Registers an account type for balance tracking.

Source

pub fn register_account_types(&mut self, types: &[(String, AccountType)])

Registers multiple account types.

Source

pub fn register_from_chart_prefixes(&mut self, prefixes: &[(&str, AccountType)])

Registers account types from a chart of accounts prefix pattern.

Source

pub fn initialize_from_snapshot(&mut self, snapshot: &BalanceSnapshot)

Initializes balances from opening balance snapshot.

Source

pub fn apply_entry( &mut self, entry: &JournalEntry, ) -> Result<(), ValidationError>

Applies a journal entry to the running balances.

Source

pub fn apply_entries( &mut self, entries: &[JournalEntry], ) -> Vec<ValidationError>

Applies a batch of entries.

Source

pub fn validate_balance_sheet( &mut self, company_code: &str, date: NaiveDate, entry_id: Option<&str>, ) -> Result<(), ValidationError>

Validates the balance sheet equation for a company.

Source

pub fn get_snapshot( &self, company_code: &str, as_of_date: NaiveDate, ) -> Option<BalanceSnapshot>

Gets the current snapshot for a company.

Source

pub fn get_all_snapshots(&self, as_of_date: NaiveDate) -> Vec<BalanceSnapshot>

Gets snapshots for all companies.

Source

pub fn get_balance_changes( &self, company_code: &str, from_date: NaiveDate, to_date: NaiveDate, ) -> Vec<AccountPeriodActivity>

Gets balance changes for a period.

Source

pub fn get_account_balance( &self, company_code: &str, account_code: &str, ) -> Option<&AccountBalance>

Gets balance for a specific account.

Source

pub fn with_tb_anchor(self, anchor: TbAnchorPrior) -> Self

SP4.1 — Attach a TB anchor prior to this tracker. When set, enables account_drift() and drift_correction_needed(). Does not change the existing balance-tracking behaviour — purely additive.

Source

pub fn set_tb_anchor(&mut self, anchor: TbAnchorPrior)

SP4.1 — Set the TB anchor prior on this tracker (mutable version).

Source

pub fn account_drift(&self, company_code: &str) -> Vec<(String, f64)>

SP4.1 — Returns the per-account drift (current closing balance − target closing balance) for every account that appears in the TB anchor prior.

Positive drift means the synthetic account balance is higher than the target; negative means it is lower.

Returns an empty Vec when no TB anchor is loaded or no company has been tracked yet.

Source

pub fn drift_correction_needed(&self, company_code: &str) -> bool

SP4.1 / SP5.1 — Returns true when the TB anchor is loaded and any single account’s absolute drift exceeds 2 × closing_stdev (or a fallback of 2% of |closing_balance| when stdev is zero), OR the aggregate absolute drift across all tracked accounts exceeds 0.5% of total_assets.

Thresholds were tuned to 2σ / 0.5% in SP5.1 (previously 3σ / 1%) so that the drift-correction pass fires on realistic synthetic runs where balances are shaped by priors but not pinned to the corpus median.

Returns false when no TB anchor is loaded (backwards-compatible behaviour — caller does not emit drift-correction entries).

Source

pub fn build_drift_correction_je<R: RngExt>( &self, company_code: &str, posting_date: NaiveDate, rng: &mut R, ) -> Option<JournalEntry>

SP4.1 W8.1 — Build a balanced drift-correction JE that nudges the most-drifted accounts back toward their TB anchor targets.

The emitted JE:

  • Has document_type = "SA" and source = Adjustment (period-end style).
  • Includes at most 8 account lines (the worst drifters), plus one balancing line posted to suspense account “9999” when the selected lines don’t net to zero.
  • Always satisfies total_debit == total_credit (mandatory for apply_entry).

Returns None when no TB anchor is loaded, when no drifts exceed the noise floor, or when the resulting JE would have fewer than 2 lines.

SP5.1: The net / balancing amount is now computed in Decimal (not f64) so that f64→Decimal precision loss cannot produce an “unbalanced” JE.

Source

pub fn get_validation_errors(&self) -> &[ValidationError]

Gets all validation errors.

Source

pub fn clear_validation_errors(&mut self)

Clears validation errors.

Source

pub fn get_statistics(&self) -> &TrackerStatistics

Gets tracker statistics.

Source

pub fn roll_forward(&mut self, _new_period_start: NaiveDate)

Rolls forward balances to a new period.

Source

pub fn export_balances(&self, company_code: &str) -> Vec<(String, Decimal)>

Exports balances to a simple format.

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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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