The personal finance app for everyone
Fix incorrect totals calculation when family has loan payments (#1984)
Zach Gollwitzerf363fd4a4e6d
diff --git a/app/models/income_statement.rb b/app/models/income_statement.rbindex cacc3f33..fba114e4 100644--- a/app/models/income_statement.rb+++ b/app/models/income_statement.rb@@ -18,7 +18,7 @@ class IncomeStatementtotal_expense = result.select { |t| t.classification == "expense" }.sum(&:total)ScopeTotals.new(- transactions_count: transactions_scope.count,+ transactions_count: result.sum(&:transactions_count),income_money: Money.new(total_income, family.currency),expense_money: Money.new(total_expense, family.currency),missing_exchange_rates?: result.any?(&:missing_exchange_rates?)diff --git a/app/models/income_statement/base_query.rb b/app/models/income_statement/base_query.rbindex baa09659..d2b17b81 100644--- a/app/models/income_statement/base_query.rb+++ b/app/models/income_statement/base_query.rb@@ -8,6 +8,7 @@ module IncomeStatement::BaseQuerydate_trunc(:interval, ae.date) as date,CASE WHEN ae.amount < 0 THEN 'income' ELSE 'expense' END as classification,SUM(ae.amount * COALESCE(er.rate, 1)) as total,+ COUNT(ae.id) as transactions_count,BOOL_OR(ae.currency <> :target_currency AND er.rate IS NULL) as missing_exchange_ratesFROM (#{transactions_scope.to_sql}) atJOIN account_entries ae ON ae.entryable_id = at.id AND ae.entryable_type = 'Account::Transaction'@@ -29,7 +30,7 @@ module IncomeStatement::BaseQuery)WHERE (transfer_info.transfer_id IS NULL OR- (ae.amount < 0 AND transfer_info.accountable_type = 'Loan')+ (ae.amount > 0 AND transfer_info.accountable_type = 'Loan'))GROUP BY 1, 2, 3, 4SQLdiff --git a/app/models/income_statement/totals.rb b/app/models/income_statement/totals.rbindex acf2017b..88161e41 100644--- a/app/models/income_statement/totals.rb+++ b/app/models/income_statement/totals.rb@@ -13,13 +13,14 @@ class IncomeStatement::Totalscategory_id: row["category_id"],classification: row["classification"],total: row["total"],+ transactions_count: row["transactions_count"],missing_exchange_rates?: row["missing_exchange_rates"])endendprivate- TotalsRow = Data.define(:parent_category_id, :category_id, :classification, :total, :missing_exchange_rates?)+ TotalsRow = Data.define(:parent_category_id, :category_id, :classification, :total, :transactions_count, :missing_exchange_rates?)def query_sqlbase_sql = base_query_sql(family: @family, interval: "day", transactions_scope: @transactions_scope)@@ -33,7 +34,8 @@ class IncomeStatement::Totalscategory_id,classification,ABS(SUM(total)) as total,- BOOL_OR(missing_exchange_rates) as missing_exchange_rates+ BOOL_OR(missing_exchange_rates) as missing_exchange_rates,+ SUM(transactions_count) as transactions_countFROM base_totalsGROUP BY 1, 2, 3;SQLdiff --git a/app/views/transactions/_summary.html.erb b/app/views/transactions/_summary.html.erbindex 19c27346..428e425d 100644--- a/app/views/transactions/_summary.html.erb+++ b/app/views/transactions/_summary.html.erb@@ -2,7 +2,7 @@<div class="grid grid-cols-3 bg-white rounded-xl shadow-border-xs divide-x divide-alpha-black-100"><div class="p-4 space-y-2"><p class="text-sm text-secondary">Total transactions</p>- <p class="text-primary font-medium text-xl" id="total-transactions"><%= totals.transactions_count %></p>+ <p class="text-primary font-medium text-xl" id="total-transactions"><%= totals.transactions_count.round(0) %></p></div><div class="p-4 space-y-2"><p class="text-sm text-secondary">Income</p>