The personal finance app for everyone (by everyone)
Ruby
Load budget bill reservations once per budget (#4656)
Nikolay Kopylov022a573f5a00
diff --git a/app/models/budget.rb b/app/models/budget.rbindex b3dd5dba75..45324804d8 100644--- a/app/models/budget.rb+++ b/app/models/budget.rb@@ -422,6 +422,33 @@ class Budget < ApplicationRecord[ expense - refund, 0 ].maxend+ # Open bill occurrences due in this period, by category id, loaded once so+ # each BudgetCategory#bills_reserved reads its slice instead of querying.+ def open_bill_occurrences_by_category_id+ @open_bill_occurrences_by_category_id ||= begin+ occurrences = RecurringOccurrence+ .open_status+ .joins(:recurring_transaction)+ .where(recurring_transactions: {+ family_id: family_id,+ status: :active,+ destination_account_id: nil+ })+ .where("recurring_transactions.amount > 0")+ .where(due_on: start_date..end_date)+ .includes(:recurring_transaction)+ .to_a++ sums = RecurringAllocation.confirmed+ .where(recurring_occurrence_id: occurrences.map(&:id))+ .group(:recurring_occurrence_id)+ .sum(:allocated_amount)+ occurrences.each { |occurrence| occurrence.cached_confirmed_allocated = sums.fetch(occurrence.id, 0) }++ occurrences.group_by { |occurrence| occurrence.recurring_transaction.category_id }+ end+ end+def category_median_monthly_expense(category)income_statement.median_expense(category: category)enddiff --git a/app/models/budget_category.rb b/app/models/budget_category.rbindex a80d41fe7f..0cb4c3c929 100644--- a/app/models/budget_category.rb+++ b/app/models/budget_category.rb@@ -419,33 +419,11 @@ class BudgetCategory < ApplicationRecord# is counted.def bills_reservation@bills_reservation ||= begin- occurrences = RecurringOccurrence- .open_status- .joins(:recurring_transaction)- .where(recurring_transactions: {- family_id: budget.family_id,- category_id: category_id,- status: :active,- destination_account_id: nil- })- .where("recurring_transactions.amount > 0")- .where(due_on: budget.start_date..budget.end_date)- .includes(:recurring_transaction)- .to_a-- # One grouped sum for the whole set. Reading remaining_amount straight- # off each row issues a SUM per occurrence, which is a query count that- # grows with the number of bills the user has.- sums = RecurringAllocation.confirmed- .where(recurring_occurrence_id: occurrences.map(&:id))- .group(:recurring_occurrence_id)- .sum(:allocated_amount)+ occurrences = budget.open_bill_occurrences_by_category_id.fetch(category_id, [])unconvertible = 0total = occurrences.reduce(Money.new(0, currency)) do |sum, occurrence|- occurrence.cached_confirmed_allocated = sums.fetch(occurrence.id, 0)-beginsum + occurrence.remaining_amount_money.exchange_to(currency)rescue Money::ConversionErrordiff --git a/test/models/budget_category_test.rb b/test/models/budget_category_test.rbindex e76328a5b1..c469a224a9 100644--- a/test/models/budget_category_test.rb+++ b/test/models/budget_category_test.rb@@ -115,6 +115,32 @@ class BudgetCategoryTest < ActiveSupport::TestCaseassert_equal 0, @parent_budget_category.bills_reserved_unconvertible_countend+ test "bills_reserved loads the budget's obligations once for all its categories" do+ bill = @family.recurring_transactions.create!(+ name: "Groceries plan", account: accounts(:depository), amount: 120,+ currency: "USD", bill_type: "bill", category_id: @subcategory_with_limit.id,+ expected_day_of_month: 3, anchor_date: @budget.start_date,+ last_occurrence_date: @budget.start_date, next_expected_date: @budget.start_date,+ status: "active", manual: true+ )+ bill.recurring_occurrences.destroy_all+ bill.recurring_occurrences.create!(+ family: @family, original_due_on: @budget.start_date + 3,+ due_on: @budget.start_date + 3, currency: "USD", expected_amount: 120+ )++ budget_categories = Budget.find(@budget.id).budget_categories.to_a+ first, *rest = budget_categories+ first.bills_reserved++ assert_queries_count(0) do+ rest.each(&:bills_reserved)+ end+ reserved = budget_categories.to_h { |bc| [ bc.category_id, bc.bills_reserved.amount ] }+ assert_equal 120, reserved[@subcategory_with_limit.id]+ assert_equal 0, reserved[@parent_category.id]+ end+test "subcategory with zero budget inherits from parent" doassert @subcategory_inheriting_bc.inherits_parent_budget?refute @subcategory_with_limit_bc.inherits_parent_budget?