Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Add an All time period to Reports (#5403)

jezmor3027e2236970
diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb
index 52f1921e56..6c12ae20b5 100644
--- a/app/controllers/reports_controller.rb
+++ b/app/controllers/reports_controller.rb
@@ -288,6 +288,8 @@ class ReportsController < ApplicationController
Date.current.beginning_of_year.to_date
when :last_6_months
(Date.current.end_of_month + 1.day - 6.months).beginning_of_month.to_date
+ when :all_time
+ earliest_report_activity_date || Date.current.beginning_of_month.to_date
when :custom
1.month.ago.to_date
else
@@ -295,15 +297,23 @@ class ReportsController < ApplicationController
end
end
+ # The first transaction or trade in the accounts this member's reports read
+ # (their finance accounts, as IncomeStatement uses), never a scheduled one.
+ # An older entry in an account they can't see must not start All time: its
+ # date would show in the label and the URLs built from it.
+ def earliest_report_activity_date
+ Entry.where(account_id: Current.user.finance_accounts.select(:id), entryable_type: %w[Transaction Trade])
+ .excluding_scheduled
+ .minimum(:date)
+ end
+
def default_end_date
case @period_type
when :monthly, :last_6_months
Date.current.end_of_month.to_date
when :quarterly
Date.current.end_of_quarter.to_date
- when :ytd
- Date.current
- when :custom
+ when :ytd, :all_time, :custom
Date.current
else
Date.current.end_of_month.to_date
@@ -333,9 +343,11 @@ class ReportsController < ApplicationController
end
net_savings = current_income - current_expenses
- # Calculate percentage changes
- income_change = calculate_percentage_change(previous_income, current_income)
- expense_change = calculate_percentage_change(previous_expenses, current_expenses)
+ # Calculate percentage changes. All time has no period before it to compare with.
+ unless @period_type == :all_time
+ income_change = calculate_percentage_change(previous_income, current_income)
+ expense_change = calculate_percentage_change(previous_expenses, current_expenses)
+ end
# Get budget performance for current period
budget_percent = calculate_budget_performance
diff --git a/app/views/reports/index.html.erb b/app/views/reports/index.html.erb
index c73e10f29e..4947f6cd4d 100644
--- a/app/views/reports/index.html.erb
+++ b/app/views/reports/index.html.erb
@@ -31,7 +31,7 @@
<%# Period Navigation Tabs %>
<div class="flex flex-wrap items-center justify-between gap-4">
<%= render DS::SegmentedControl.new(aria_label: t("reports.index.period_aria", default: "Report period"), class: "max-w-full overflow-x-auto no-scrollbar") do |sc| %>
- <% %i[monthly quarterly ytd last_6_months custom].each do |ptype| %>
+ <% %i[monthly quarterly ytd last_6_months all_time custom].each do |ptype| %>
<% sc.with_segment(t("reports.index.periods.#{ptype}"),
active: @period_type == ptype,
href: reports_path(period_type: ptype, **reports_filter_params),
@@ -92,6 +92,14 @@
<% end %>
<% end %>
+ <% if @period_type == :all_time %>
+ <p class="text-primary font-medium text-lg lg:text-base mb-4">
+ <%= t("reports.index.period_label.all_time",
+ start: I18n.l(@start_date, format: :short_month_year),
+ end: I18n.l(@end_date, format: :short_month_year)) %>
+ </p>
+ <% end %>
+
<%# Period Display %>
<% if @nav %>
<div class="flex items-center gap-1 mb-4">
diff --git a/config/locales/views/reports/en.yml b/config/locales/views/reports/en.yml
index a1a9ff9f66..82c360b69d 100644
--- a/config/locales/views/reports/en.yml
+++ b/config/locales/views/reports/en.yml
@@ -20,6 +20,7 @@ en:
quarterly: Quarterly
ytd: Year to Date
last_6_months: Last 6 Months
+ all_time: All Time
custom: Custom Range
date_range:
from: From
@@ -34,6 +35,7 @@ en:
ytd: "YTD %{year}"
past_year: "%{year}"
last_6_months: "%{start} – %{end}"
+ all_time: "%{start} – %{end}"
period_picker:
quarter: "Q%{quarter} %{year}"
ytd: "YTD %{year}"
diff --git a/test/controllers/reports_controller_test.rb b/test/controllers/reports_controller_test.rb
index b594b3a59f..64876cccc7 100644
--- a/test/controllers/reports_controller_test.rb
+++ b/test/controllers/reports_controller_test.rb
@@ -379,6 +379,55 @@ class ReportsControllerTest < ActionDispatch::IntegrationTest
assert_response :ok
end
+ test "all time runs from the member's first transaction or trade to today" do
+ first = @family.earliest_activity_date
+ assert first, "fixtures need a transaction or trade"
+
+ get reports_path(period_type: :all_time)
+
+ assert_response :ok
+ assert_select "a[aria-current]", text: I18n.t("reports.index.periods.all_time")
+ assert_includes @response.body, I18n.t("reports.index.period_label.all_time",
+ start: I18n.l(first, format: :short_month_year), end: I18n.l(Date.current, format: :short_month_year))
+ assert_includes @response.body, first.strftime("%b %Y")
+ assert_not_includes @response.body, (first - 1.month).strftime("%b %Y")
+ end
+
+ test "all time doesn't start at activity in an account the member can't see" do
+ member = users(:family_member)
+ hidden = @family.accounts.create!(name: "Admin's old account", balance: 0, currency: "USD",
+ accountable: Depository.new, owner: @user)
+ hidden.entries.create!(date: Date.new(2001, 2, 3), name: "Old", amount: 10, currency: "USD", entryable: Transaction.new)
+ assert_not member.finance_accounts.include?(hidden), "the member must not see the account"
+
+ sign_in member
+ get reports_path(period_type: :all_time)
+
+ assert_response :ok
+ assert_not_includes @response.body, "2001-02-03"
+ assert_not_includes @response.body, I18n.l(Date.new(2001, 2, 3), format: :short_month_year)
+ end
+
+ test "all time ignores scheduled entries after today" do
+ @family.entries.where(entryable_type: %w[Transaction Trade]).delete_all
+ account = @family.accounts.first
+ account.entries.create!(date: 1.month.from_now.to_date, name: "Scheduled", amount: 10, currency: "USD", entryable: Transaction.new)
+
+ get reports_path(period_type: :all_time)
+
+ assert_response :ok
+ assert_nil flash[:alert]
+ assert_includes @response.body, I18n.t("reports.index.period_label.all_time",
+ start: I18n.l(Date.current.beginning_of_month, format: :short_month_year), end: I18n.l(Date.current, format: :short_month_year))
+ end
+
+ test "all time has no previous period to compare with" do
+ get reports_path(period_type: :all_time)
+
+ assert_response :ok
+ assert_not_includes @response.body, I18n.t("reports.summary.vs_previous")
+ end
+
test "index with last 6 months period" do
get reports_path(period_type: :last_6_months)
assert_response :ok