use bigdecimal::{BigDecimal, Zero}; use crate::app::store::records::{EquityChangeEvent, EquitySymbol, FiduciaryAccount, ROUNDING_MODE}; /// An estimated ledger is the effective sum of all the equity change events /// derived from record for a particular fiduciary account and equity symbol. pub struct EstimatedLedger<'a> { pub fiduciary_account: &'a FiduciaryAccount, pub equity_symbol: &'a EquitySymbol, pub equity_change_events: Vec<&'a EquityChangeEvent>, pub account_symbol_quantity: BigDecimal, pub account_symbol_cost_basis_price: BigDecimal, pub account_symbol_cost_basis_total: BigDecimal, pub account_symbol_last_effective_price: BigDecimal, pub account_symbol_last_update_timestamp: chrono::DateTime, } impl EstimatedLedger<'_> { /// Takes a list of all change events, a fiduciary account, and an equity /// symbol for which to create a ledger. /// /// All values are taken by reference, so they need to be maintained elsewhere pub fn compile_change_events<'a>(fiduciary_account: &'a FiduciaryAccount, equity_symbol: &'a EquitySymbol, events: Vec<&'a EquityChangeEvent>) -> EstimatedLedger<'a> { let mut acsym_events = vec![]; let mut quantity = BigDecimal::zero(); let mut cost_basis_total = BigDecimal::zero(); let mut last_event: Option<&EquityChangeEvent> = None; for event in events { if event.fiduciary_account_id != fiduciary_account.fiduciary_account_id || event.equity_symbol_id != equity_symbol.equity_symbol_id { continue; } if last_event.is_none() || last_event.unwrap().equity_change_timestamp < event.equity_change_timestamp { last_event = Some(event); } quantity += event.scored_quantity(); cost_basis_total += event.scored_cost_basis(); acsym_events.push(event); } let cost_basis_price = (cost_basis_total.clone() / quantity.clone()).with_scale_round(2, ROUNDING_MODE); EstimatedLedger { fiduciary_account, equity_symbol, equity_change_events: acsym_events, account_symbol_quantity: quantity, account_symbol_cost_basis_price: cost_basis_price, account_symbol_cost_basis_total: cost_basis_total.with_scale_round(2, ROUNDING_MODE), account_symbol_last_effective_price: last_event.map_or_else(|| BigDecimal::zero().with_scale(2), |ev| ev.estimated_price()), account_symbol_last_update_timestamp: last_event.map_or_else(|| chrono::DateTime::::MIN_UTC, |ev| ev.equity_change_timestamp), } } }