From 6d1300640051baa23360846197b54e1e69ae32e3 Mon Sep 17 00:00:00 2001 From: Joris Guyonvarch Date: Sat, 18 Apr 2026 11:04:47 +0200 Subject: Add balancing capabilities If payment are too unbalanced, it’s easier to make a transfer. --- src/controller/balance.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'src/controller/balance.rs') diff --git a/src/controller/balance.rs b/src/controller/balance.rs index 309f15c..6cb937b 100644 --- a/src/controller/balance.rs +++ b/src/controller/balance.rs @@ -7,6 +7,7 @@ use crate::controller::utils; use crate::controller::wallet::Wallet; use crate::db; use crate::model::user::User; +use crate::model::balancing::Balancing; use crate::payer; use crate::templates; @@ -22,7 +23,10 @@ pub async fn get(wallet: &Wallet) -> Response> { get_template_user_incomes(&users, &user_incomes); let total_income: i64 = user_incomes.values().sum(); - let user_payments = db::payments::repartition(&wallet.db_conn).await; + let user_payments = with_balancing( + db::payments::repartition(&wallet.db_conn).await, + db::balancing::list(&wallet.db_conn).await + ); let template_user_payments = get_template_user_payments(&users, &user_payments); let total_payments: i64 = user_payments.iter().map(|p| p.1).sum(); @@ -67,3 +71,17 @@ fn get_template_user_incomes( user_incomes.sort_by_key(|i| i.1); user_incomes } + +fn with_balancing( + user_payments: HashMap, + balancings: Vec +) -> HashMap { + let mut user_payments = user_payments; + for balancing in balancings { + let src = user_payments.entry(balancing.source).or_insert(0); + *src += balancing.amount; + let dest = user_payments.entry(balancing.destination).or_insert(0); + *dest -= balancing.amount; + } + user_payments +} -- cgit v1.2.3