Refactor update_balances_from_transactions
This commit is contained in:
parent
40b0afe492
commit
98018bb4bc
@ -19,6 +19,8 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fmt::Display;
|
||||
|
||||
use crate::transaction::update_balances_from_transactions;
|
||||
|
||||
use super::calculator::{has_step_or_can_build, HasStepOrCanBuild, ReportingGraphDependencies};
|
||||
use super::executor::ReportingExecutionError;
|
||||
use super::types::{
|
||||
@ -319,17 +321,7 @@ impl ReportingStep for GenerateBalances {
|
||||
let mut balances = BalancesAt {
|
||||
balances: HashMap::new(),
|
||||
};
|
||||
|
||||
for transaction in transactions.iter() {
|
||||
for posting in transaction.postings.iter() {
|
||||
// FIXME: Do currency conversion
|
||||
let running_balance =
|
||||
balances.balances.get(&posting.account).unwrap_or(&0) + posting.quantity;
|
||||
balances
|
||||
.balances
|
||||
.insert(posting.account.clone(), running_balance);
|
||||
}
|
||||
}
|
||||
update_balances_from_transactions(&mut balances.balances, transactions.iter());
|
||||
|
||||
// Store result
|
||||
products.insert(
|
||||
@ -546,17 +538,7 @@ impl ReportingStep for UpdateBalancesAt {
|
||||
let mut balances = BalancesAt {
|
||||
balances: opening_balances_at.balances.clone(),
|
||||
};
|
||||
|
||||
for transaction in transactions.iter() {
|
||||
for posting in transaction.postings.iter() {
|
||||
// FIXME: Do currency conversion
|
||||
let running_balance =
|
||||
balances.balances.get(&posting.account).unwrap_or(&0) + posting.quantity;
|
||||
balances
|
||||
.balances
|
||||
.insert(posting.account.clone(), running_balance);
|
||||
}
|
||||
}
|
||||
update_balances_from_transactions(&mut balances.balances, transactions.iter());
|
||||
|
||||
// Store result
|
||||
products.insert(
|
||||
@ -720,17 +702,7 @@ impl ReportingStep for UpdateBalancesBetween {
|
||||
let mut balances = BalancesBetween {
|
||||
balances: opening_balances.clone(),
|
||||
};
|
||||
|
||||
for transaction in transactions.iter() {
|
||||
for posting in transaction.postings.iter() {
|
||||
// FIXME: Do currency conversion
|
||||
let running_balance =
|
||||
balances.balances.get(&posting.account).unwrap_or(&0) + posting.quantity;
|
||||
balances
|
||||
.balances
|
||||
.insert(posting.account.clone(), running_balance);
|
||||
}
|
||||
}
|
||||
update_balances_from_transactions(&mut balances.balances, transactions.iter());
|
||||
|
||||
// Store result
|
||||
products.insert(
|
||||
|
@ -24,6 +24,7 @@ use std::fmt::Display;
|
||||
use chrono::Datelike;
|
||||
|
||||
use crate::reporting::types::{BalancesAt, DateStartDateEndArgs, ReportingProductId, Transactions};
|
||||
use crate::transaction::update_balances_from_transactions;
|
||||
use crate::util::sofy_from_eofy;
|
||||
|
||||
use super::calculator::ReportingGraphDependencies;
|
||||
@ -245,17 +246,7 @@ impl ReportingStep for AllTransactionsIncludingRetainedEarnings {
|
||||
let mut balances = BalancesAt {
|
||||
balances: opening_balances.balances.clone(),
|
||||
};
|
||||
|
||||
for transaction in transactions.transactions.iter() {
|
||||
for posting in transaction.postings.iter() {
|
||||
// FIXME: Do currency conversion
|
||||
let running_balance =
|
||||
balances.balances.get(&posting.account).unwrap_or(&0) + posting.quantity;
|
||||
balances
|
||||
.balances
|
||||
.insert(posting.account.clone(), running_balance);
|
||||
}
|
||||
}
|
||||
update_balances_from_transactions(&mut balances.balances, transactions.transactions.iter());
|
||||
|
||||
// Store result
|
||||
products.insert(
|
||||
|
@ -16,6 +16,8 @@
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use chrono::NaiveDateTime;
|
||||
|
||||
use crate::QuantityInt;
|
||||
@ -42,3 +44,16 @@ pub struct Posting {
|
||||
pub quantity: QuantityInt,
|
||||
pub commodity: String,
|
||||
}
|
||||
|
||||
pub(crate) fn update_balances_from_transactions<'a, I: Iterator<Item = &'a TransactionWithPostings>>(
|
||||
balances: &mut HashMap<String, QuantityInt>,
|
||||
transactions: I,
|
||||
) {
|
||||
for transaction in transactions {
|
||||
for posting in transaction.postings.iter() {
|
||||
// FIXME: Do currency conversion
|
||||
let running_balance = balances.get(&posting.account).unwrap_or(&0) + posting.quantity;
|
||||
balances.insert(posting.account.clone(), running_balance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user