The personal finance app for everyone (by everyone)
Ruby
Expose settled bill payments from the bill drawer (#4706)
Shibu Me22b68114ded
diff --git a/app/controllers/bills_controller.rb b/app/controllers/bills_controller.rbindex cce0c2b85a..fb0aa26ebe 100644--- a/app/controllers/bills_controller.rb+++ b/app/controllers/bills_controller.rb@@ -185,6 +185,17 @@ class BillsController < ApplicationController# A pending suggestion is the one thing that changes what the drawer# should offer, so it is worth the one query.@drawer_suggestion = @current_occurrence&.allocations&.suggested&.first++ # The summary's verb only reaches the cycle the drawer is scoped to. When+ # that is the next open cycle, a past payment still needs a route in, so+ # name the most recent settled cycle for the secondary action. Allocations+ # are required: a paid row with none has nothing to unlink or review.+ @latest_paid_occurrence = @series.recurring_occurrences+ .paid+ .where(id: RecurringAllocation.confirmed.select(:recurring_occurrence_id))+ .order(due_on: :desc)+ .first+render :drawer, layout: falseenddiff --git a/app/views/bills/_detail.html.erb b/app/views/bills/_detail.html.erbindex 9679e6f375..05989222b7 100644--- a/app/views/bills/_detail.html.erb+++ b/app/views/bills/_detail.html.erb@@ -144,13 +144,16 @@<%# Settled cycles with what paid them: "did I pay it that month" and "whichtransaction paid it" in one list, rather than a cycle list and a payment- list that mostly repeat each other. %>+ list that mostly repeat each other. Every row opens its cycle's payment+ drawer, which is the only place a payment can be unlinked. %><% if history.any? %><%= render DS::Disclosure.new(title: t("bills.detail.history")) do %><div class="bg-container-inset rounded-lg divide-y divide-subdued"><% history.each do |occurrence| %><% payments = occurrence.allocations.select(&:allocation_confirmed?) %>- <div class="flex items-center justify-between gap-3 px-3 py-2">+ <%= link_to recurring_occurrence_path(occurrence),+ data: { turbo_frame: :drawer, turbo_prefetch: false },+ class: "flex items-center justify-between gap-3 px-3 py-2 hover:bg-surface-hover focus-ring -outline-offset-2" do %><div class="min-w-0"><p class="text-sm text-primary"><%= l(occurrence.due_on, format: :long) %></p><p class="text-xs text-secondary truncate">@@ -162,10 +165,13 @@<% end %></p></div>- <p class="text-sm text-primary privacy-sensitive shrink-0">- <%= format_money(occurrence.confirmed_allocated_money) %>- </p>- </div>+ <span class="flex items-center gap-1.5 shrink-0">+ <span class="text-sm text-primary privacy-sensitive">+ <%= format_money(occurrence.confirmed_allocated_money) %>+ </span>+ <%= icon("chevron-right", size: "sm", class: "text-subdued") %>+ </span>+ <% end %><% end %></div><% end %>diff --git a/app/views/bills/_summary.html.erb b/app/views/bills/_summary.html.erbindex 0037ae7448..913a08d223 100644--- a/app/views/bills/_summary.html.erb+++ b/app/views/bills/_summary.html.erb@@ -1,4 +1,4 @@-<%# locals: (series:, current_occurrence:, suggestion: nil) %>+<%# locals: (series:, current_occurrence:, suggestion: nil, settled_occurrence: nil) %><%# INSPECT. What is going on with this bill, and what to do about it?@@ -73,5 +73,20 @@<%# Pay and Add link keep a home here, so the portal never leaves Billsjust because the row got quieter. %><%= render "recurring_transactions/pay_action", recurring_transaction: series %>++ <%# The primary verb only reaches the cycle the drawer is showing. When+ that is a different cycle -- an open one, or a skipped/missed one whose+ own verb has no payment to manage -- offer the latest settled cycle+ here. When the drawer already shows it, the primary action covers it. %>+ <% if settled_occurrence.present? && settled_occurrence != current_occurrence %>+ <%= render DS::Link.new(+ text: t("bills.manage_payments"),+ icon: "receipt-text",+ variant: "outline",+ size: "md",+ href: recurring_occurrence_path(settled_occurrence),+ frame: :drawer+ ) %>+ <% end %></div></div>diff --git a/app/views/bills/drawer.html.erb b/app/views/bills/drawer.html.erbindex 37d7ed286d..f2b6aaa31d 100644--- a/app/views/bills/drawer.html.erb+++ b/app/views/bills/drawer.html.erb@@ -57,7 +57,8 @@<%= render "bills/summary",series: @series,current_occurrence: @current_occurrence,- suggestion: @drawer_suggestion %>+ suggestion: @drawer_suggestion,+ settled_occurrence: @latest_paid_occurrence %><%# The collapsible sections: Overview, Coming up, History and Notes.Lazy, so opening a bill costs no more than its summary;diff --git a/test/controllers/bills_controller_test.rb b/test/controllers/bills_controller_test.rbindex 7a41ede665..08fe978a8a 100644--- a/test/controllers/bills_controller_test.rb+++ b/test/controllers/bills_controller_test.rb@@ -2135,6 +2135,165 @@ class BillsControllerTest < ActionDispatch::IntegrationTestassert_no_match "POWER CO REFUND", response.body, "a pending match paid nothing"end+ # History is the only list of cycles older than this month, and the payment+ # drawer it points into is the only place a payment can be unlinked. Rows+ # were static text, so a past payment was a dead end here.+ test "every settled cycle in history opens the payment drawer for its own cycle" do+ bill = create_bill(name: "Power Co", amount: 80)+ paid_due = 2.months.ago.to_date+ skipped_due = 3.months.ago.to_date+ paid = bill.recurring_occurrences.create!(+ family: @family, original_due_on: paid_due, due_on: paid_due, currency: "USD"+ )+ skipped = bill.recurring_occurrences.create!(+ family: @family, original_due_on: skipped_due, due_on: skipped_due, currency: "USD"+ )+ skipped.skip!+ missed_due = 4.months.ago.to_date+ missed = bill.recurring_occurrences.create!(+ family: @family, original_due_on: missed_due, due_on: missed_due, currency: "USD"+ )+ missed.miss!+ RecurringTransaction::Allocator.new(paid).allocate!(amount: "78.50")+ assert paid.reload.paid?++ get_bill_history(bill)++ assert_response :success+ [ paid, skipped, missed ].each do |occurrence|+ assert_select "a[data-turbo-frame=drawer][href=?]", recurring_occurrence_path(occurrence) do |links|+ assert_select links.first, "p", text: I18n.l(occurrence.due_on, format: :long)+ assert_select links.first, "svg", count: 1+ end+ end+ end++ # The drawer's primary verb only reaches the cycle it was opened on. On an+ # open next cycle that is Find/Add/Review, so the bill's settled payments had+ # no route from the drawer even though Unlink lives one level below it.+ test "an open cycle's drawer offers Manage payments for the latest settled cycle" do+ bill = declare_bill(name: "Power Co", amount: 80, due: Date.current + 5)+ settled = settle_past_cycle(bill, months_ago: 2, amount: "78.50")++ assert bill.reload.current_occurrence.scheduled?, "premise: the drawer shows the next open cycle"++ get_bill_drawer(bill)++ assert_response :success+ assert_select "a[href=?]", recurring_occurrence_path(settled), text: I18n.t("bills.manage_payments")+ end++ test "the latest settled cycle is the one Manage payments opens" do+ bill = declare_bill(name: "Power Co", amount: 80, due: Date.current + 5)+ settle_past_cycle(bill, months_ago: 3, amount: "79")+ latest = settle_past_cycle(bill, months_ago: 2, amount: "78.50")++ get_bill_drawer(bill)++ assert_response :success+ assert_select "a[href=?]", recurring_occurrence_path(latest), text: I18n.t("bills.manage_payments")+ end++ # A skipped or missed cycle has no payment of its own, so its Manage payments+ # verb opened an empty drawer. The earlier payment is still the thing to+ # manage, and it must remain reachable when no cycle is open.+ test "skipped and missed cycles still offer the latest settled payment" do+ { skip!: "skipped", miss!: "missed" }.each do |closing, status|+ bill = declare_bill(name: "Power Co #{status}", amount: 80, due: Date.current + 5)+ settled = settle_past_cycle(bill, months_ago: 2, amount: "80")+ bill.recurring_occurrences.open_status.each { |occurrence| occurrence.public_send(closing) }+ current = bill.recurring_occurrences.order(due_on: :desc).first++ assert_equal status, current.status+ assert_equal current.id, bill.reload.current_occurrence.id,+ "premise: the drawer shows the #{status} cycle"++ get_bill_drawer(bill)++ assert_response :success+ assert_select "a[href=?]", recurring_occurrence_path(settled), text: I18n.t("bills.manage_payments")+ end+ end++ # History stops at the 12 newest closed cycles. A settled payment older than+ # that window must still be reachable from the drawer, or repeating skips and+ # misses locks it away for good.+ test "a settled payment older than the history window is still offered" do+ bill = declare_bill(name: "Power Co", amount: 80, due: Date.current + 5)+ settled = settle_past_cycle(bill, months_ago: 18, amount: "80")+ bill.recurring_occurrences.open_status.each(&:skip!)+ 13.times do |i|+ due = i.months.ago.to_date+ bill.recurring_occurrences.create!(+ family: @family, original_due_on: due, due_on: due, currency: "USD"+ ).skip!+ end++ window = bill.recurring_occurrences.closed.order(due_on: :desc).limit(12).to_a+ assert_not_includes window, settled, "premise: the payment is outside the history window"++ get_bill_drawer(bill)++ assert_response :success+ assert_select "a[href=?]", recurring_occurrence_path(settled), text: I18n.t("bills.manage_payments")+ end++ # A closed cycle's own verb is already Manage payments (or Reopen); repeating+ # the same action for a different cycle would make the drawer ambiguous.+ test "the drawer does not repeat Manage payments when it already shows the settled cycle" do+ bill = declare_bill(name: "Power Co", amount: 80, due: Date.current + 5)+ settled = settle_past_cycle(bill, months_ago: 2, amount: "78.50")++ get_bill_drawer(bill, occurrence: settled.id)++ assert_response :success+ assert_select "a[href=?]", recurring_occurrence_path(settled), text: I18n.t("bills.manage_payments"), count: 1+ end++ test "a bill with no settled payment offers no Manage payments action" do+ bill = declare_bill(name: "Power Co", amount: 80, due: Date.current + 5)++ get_bill_drawer(bill)++ assert_response :success+ assert_select "a", text: I18n.t("bills.manage_payments"), count: 0+ end++ # A pending match is not a payment: it is waiting on review and must not+ # pretend there is a settled cycle to manage.+ test "a suggested allocation does not create a Manage payments action" do+ bill = declare_bill(name: "Power Co", amount: 80, due: Date.current + 5)+ occurrence = bill.recurring_occurrences.open_status.order(:due_on).first+ occurrence.allocations.create!(+ entry: create_transaction_entry(name: "POWER CO", amount: 80, date: occurrence.due_on),+ allocated_amount: 80, currency: "USD", state: "suggested", source: "auto_matched"+ )++ get_bill_drawer(bill)++ assert_response :success+ assert_select "a", text: I18n.t("bills.manage_payments"), count: 0+ end++ # Unlinking the last payment leaves a user-closed cycle paid with no+ # allocations. It has nothing to manage, so the confirmed-allocation filter+ # must keep it out of the shortcut.+ test "a settled cycle with no confirmed allocation offers no Manage payments action" do+ bill = declare_bill(name: "Power Co", amount: 80, due: Date.current + 5)+ settled = bill.recurring_occurrences.create!(+ family: @family, original_due_on: 2.months.ago.to_date,+ due_on: 2.months.ago.to_date, currency: "USD"+ )+ pay(settled)+ RecurringTransaction::Allocator.new(settled).unallocate!(settled.allocations.sole)+ assert settled.reload.paid?++ get_bill_drawer(bill)++ assert_response :success+ assert_select "a", text: I18n.t("bills.manage_payments"), count: 0+ end+# "Something changed" is only useful if the thing you can still act on is# the thing you see first. Notices used to sort by date ascending, so a# month-old one-dollar rise outranked a trial converting tomorrow.@@ -2882,6 +3041,19 @@ class BillsControllerTest < ActionDispatch::IntegrationTestRecurringTransaction::Allocator.new(occurrence).mark_paid!(paid_on: Date.current)end+ # A settled cycle the bill's own generator did not create, for the history+ # and Manage payments cases. The amount must be inside the bill's tolerance+ # or the allocator leaves the cycle partially paid.+ def settle_past_cycle(bill, months_ago:, amount:)+ due = months_ago.months.ago.to_date+ occurrence = bill.recurring_occurrences.create!(+ family: @family, original_due_on: due, due_on: due, currency: "USD"+ )+ RecurringTransaction::Allocator.new(occurrence).allocate!(amount: amount)+ assert occurrence.reload.paid?, "#{amount} against #{bill.amount} must settle the cycle"+ occurrence+ end+def create_transaction_entry(name:, amount:, date:, account: accounts(:depository))account.entries.create!(date: date, amount: amount, currency: "USD", name: name,diff --git a/test/system/bill_drawer_test.rb b/test/system/bill_drawer_test.rbindex 90964207cb..59ec4e31cc 100644--- a/test/system/bill_drawer_test.rb+++ b/test/system/bill_drawer_test.rb@@ -113,4 +113,61 @@ class BillDrawerTest < ApplicationSystemTestCaseassert_no_selector "dialog[open]"assert_selector @row_link, focused: trueend++ # History is the bill drawer's only list of earlier cycles, and the payment+ # drawer it opens is the only place a payment can be unlinked. The rows were+ # static text, so a past payment had no route from here at all.+ test "a settled cycle in history opens its payment drawer and can be unlinked" do+ settled = settle_past_cycle(months_ago: 2, amount: "80")++ visit bills_url+ find(@row_link).click++ within("dialog[open]") do+ scroll_to find("turbo-frame[src*='display=history']")+ assert_text(/#{I18n.t("bills.detail.history_title")}/i)+ # The disclosure's title is uppercased by CSS, and Capybara reads the+ # rendered text, so match the word rather than its stored casing.+ find("summary", text: /#{Regexp.escape(I18n.t("bills.detail.history"))}/i).click+ # The summary's own Manage payments action points at this same cycle, so+ # name the history row by its date instead of the href alone.+ find("a[href='#{recurring_occurrence_path(settled)}']",+ text: I18n.l(settled.due_on, format: :long)).click++ assert_link I18n.t("recurring_occurrences.show.unlink")+ click_on I18n.t("recurring_occurrences.show.unlink")+ end++ assert_text I18n.t("recurring_allocations.destroy.success")+ assert_empty settled.reload.allocations.confirmed+ assert settled.scheduled?, "unlinking the last payment reopens the cycle"+ end++ # The drawer's verb is chosen for the cycle it is showing. With the next+ # cycle open that is Find payment, so the bill's settled payments used to be+ # unreachable from the drawer even though the Manage payments surface is one+ # level below it.+ test "an open cycle's drawer offers Manage payments for the last settled cycle" do+ settled = settle_past_cycle(months_ago: 2, amount: "80")++ visit bills_url(view: "all")+ find("a[data-turbo-frame='drawer'][href='#{bill_path(@bill)}']", match: :first).click++ within("dialog[open]") do+ assert_link I18n.t("bills.manage_payments"), href: recurring_occurrence_path(settled)+ click_on I18n.t("bills.manage_payments")+ assert_link I18n.t("recurring_occurrences.show.unlink")+ end+ end++ private+ def settle_past_cycle(months_ago:, amount:)+ due = months_ago.months.ago.to_date+ occurrence = @bill.recurring_occurrences.create!(+ family: @user.family, original_due_on: due, due_on: due, currency: "USD"+ )+ RecurringTransaction::Allocator.new(occurrence).allocate!(amount: amount)+ assert occurrence.reload.paid?, "#{amount} must settle the cycle for this test"+ occurrence+ endend