The personal finance app for everyone (by everyone)
Ruby
Keep bills banner and notices to the bills the page lists (#3922)
Guillem Arias Fauste1eb7a999131c
diff --git a/app/controllers/bills_controller.rb b/app/controllers/bills_controller.rbindex 3fa97f8566..770a1e7e7d 100644--- a/app/controllers/bills_controller.rb+++ b/app/controllers/bills_controller.rb@@ -88,7 +88,7 @@ class BillsController < ApplicationController# iterates, which would otherwise be separate queries.@suggested_series = accessible_suggested_series.includes(:merchant).order(next_expected_date: :asc).load@has_transaction_history = Current.family.entries.where(entryable_type: "Transaction").exists?- @suggested_allocations = suggested_allocations+ @suggested_allocations = suggested_allocations(occurrences)# A row waiting on a match decision offers Review rather than Find.# Already loaded for the queue above, so indexing is free.@suggestions_by_occurrence = @suggested_allocations.index_by(&:recurring_occurrence_id)@@ -542,7 +542,9 @@ class BillsController < ApplicationControllerdef collect_noticestoday = Date.currentwindow = today..(today + 14)- series_scope = Current.family.recurring_transactions.accessible_by(Current.user).active+ # The bills the overview lists, so no notice speaks for a series the+ # page doesn't show (income, transfers, suggestions, ended bills).+ series_scope = Current.family.recurring_transactions.where(id: payable_series_ids).activenotices = []series_scope.where(trial_ends_on: window).find_each do |series|@@ -551,9 +553,7 @@ class BillsController < ApplicationControllerseries_scope.where(renews_on: window).find_each do |series|notices << Notice.new(kind: :renewal, series: series, date: series.renews_on, detail: nil)end- RecurringPriceChange.joins(:recurring_transaction)- .merge(RecurringTransaction.accessible_by(Current.user))- .where(recurring_transactions: { family_id: Current.family.id })+ RecurringPriceChange.where(recurring_transaction_id: series_scope.select(:id)).where("effective_on >= ?", today - 30).includes(:recurring_transaction).find_each do |change|@@ -579,7 +579,9 @@ class BillsController < ApplicationController.where(recurring_occurrences: { family_id: Current.family.id })return 0 if user_touched.exists?- series.where(manual: false, status: :active).count+ # Only bills the overview lists: the banner points at its totals, which+ # leave detected income and transfers out.+ series.where(manual: false, status: :active, id: payable_series_ids).countenddef accessible_suggested_series@@ -597,12 +599,17 @@ class BillsController < ApplicationControllerendend- def suggested_allocations+ # Only occurrences the overview lists: open rows and this month's paid ones.+ # Ending a bill leaves its scheduled occurrences behind and the matcher+ # still scores them, and skipping closes an occurrence but keeps its+ # suggestion, so neither may ask about a row the page doesn't show.+ def suggested_allocations(occurrences)+ listed_ids = occurrences.select { |occurrence| occurrence.scheduled? || occurrence.paid? }.map(&:id)+RecurringAllocation.suggested.joins(recurring_occurrence: :recurring_transaction)- .where(recurring_occurrences: { family_id: Current.family.id })- .merge(RecurringTransaction.accessible_by(Current.user))+ .where(recurring_occurrence_id: listed_ids)# Income never reviews here: the matcher no longer suggests it, and# this filter also retires any suggestion written before that rule..merge(RecurringTransaction.where.not(bill_type: "income"))diff --git a/test/controllers/bills_controller_test.rb b/test/controllers/bills_controller_test.rbindex 6f7dca78be..f5f2cb3ff7 100644--- a/test/controllers/bills_controller_test.rb+++ b/test/controllers/bills_controller_test.rb@@ -811,6 +811,25 @@ class BillsControllerTest < ActionDispatch::IntegrationTestassert_no_match I18n.t("bills.index.detected_review", count: 1), response.bodyend+ # The banner asks the user to check "the totals here", which leave income and+ # internal transfers out, so neither may raise it over an empty overview.+ test "detected income and savings transfers don't raise the review banner" do+ create_bill(name: "ACME PAYROLL", amount: -2400, bill_type: "income", manual: false)+ create_bill(name: "TO SAVINGS", amount: 500, destination_account: accounts(:connected), manual: false)++ get bills_url++ assert_response :success+ assert_match I18n.t("bills.index.empty.title"), response.body+ assert_no_match I18n.t("bills.index.detected_review", count: 1), response.body+ assert_no_match I18n.t("bills.index.detected_review", count: 2), response.body++ create_bill(name: "Rent", amount: 2150, manual: false)+ get bills_url++ assert_match I18n.t("bills.index.detected_review", count: 1), response.body+ end+# The page's whole job. A single "Bills $695.60" against $357.48 of visible# rows is a number nothing on screen can account for, so due and reserved# are stated apart and their sum is never shown at all.@@ -1140,6 +1159,33 @@ class BillsControllerTest < ActionDispatch::IntegrationTestassert_match "changed price", response.bodyend+ # Notices speak for the bills the overview lists. A suggestion's price isn't+ # news until it's a bill, and an income change belongs on the Income plan.+ test "ended, suggested and income series raise no price notice" do+ change_price = ->(series) do+ series.recurring_price_changes.create!(+ effective_on: Date.current - 5, previous_amount: series.amount - 5, new_amount: series.amount,+ currency: "USD", source: "detected"+ )+ end+ change_price.(create_bill(name: "Old Streaming", amount: 20, status: "ended", manual: false))+ change_price.(create_bill(name: "ACME PAYROLL", amount: -2400, bill_type: "income"))++ get bills_url++ assert_response :success+ assert_match I18n.t("bills.index.empty.title"), response.body+ assert_no_match "changed price", response.body++ # A suggestion takes the empty state's place with its own review strip, so+ # it gets a page of its own.+ @family.recurring_transactions.destroy_all+ change_price.(create_suggested(name: "Maybe Gym", account: accounts(:depository)))+ get bills_url++ assert_no_match "changed price", response.body+ end+test "the ical feed serves upcoming occurrences with a member token and rejects garbage" docreate_bill(name: "Rent", amount: 2150)@@ -1645,6 +1691,45 @@ class BillsControllerTest < ActionDispatch::IntegrationTestresponse.bodyend+ # Ending a bill leaves its scheduled occurrences behind, and the matcher+ # scores every scheduled occurrence, so it can still suggest a payment for a+ # bill the user deleted. The queue asks only about bills the page lists.+ test "a suggestion against an ended bill stays out of the payment review queue" do+ due = Date.current - 3+ bill = declare_bill(name: "OLD GYM", amount: 40, due: due)+ RecurringTransaction::OccurrenceGenerator.new(bill).generate!+ charge = create_transaction_entry(name: "OLD GYM", amount: 40, date: due)+ RecurringTransaction::Allocator.new(bill.recurring_occurrences.order(:due_on).first).allocate_matched!(+ entry: charge, state: "suggested", confidence: 0.7, signals: { name: 0.35 }+ )+ bill.update!(status: "ended")++ get bills_url++ assert_response :success+ assert_match I18n.t("bills.index.empty.title"), response.body+ assert_no_match I18n.t("bills.index.suggestion_line", entry: "OLD GYM", bill: "OLD GYM"), response.body+ end++ # Skipping closes the occurrence but keeps its suggestion. Before this month+ # the overview no longer lists the row, so the queue mustn't ask about it.+ test "a suggestion against a skipped occurrence from last month stays out of the payment review queue" do+ due = Date.current.beginning_of_month - 10+ bill = declare_bill(name: "SKIPPED GYM", amount: 40, due: due)+ RecurringTransaction::OccurrenceGenerator.new(bill).generate!+ occurrence = bill.recurring_occurrences.find_by!(due_on: due)+ charge = create_transaction_entry(name: "SKIPPED GYM", amount: 40, date: due)+ RecurringTransaction::Allocator.new(occurrence).allocate_matched!(+ entry: charge, state: "suggested", confidence: 0.7, signals: { name: 0.35 }+ )+ occurrence.skip!++ get bills_url++ assert_response :success+ assert_no_match I18n.t("bills.index.suggestion_line", entry: "SKIPPED GYM", bill: "SKIPPED GYM"), response.body+ end+# The overview groups by calendar month, which is the wrong unit for anyone# paid weekly: four paychecks and four rent payments land in one list. The