diff --git a/app/models/financial_report.rb b/app/models/financial_report.rb index 2802d08..c59b1fe 100644 --- a/app/models/financial_report.rb +++ b/app/models/financial_report.rb @@ -20,43 +20,67 @@ def next_year end def checking_data - AccountData.new(FinancialAccount.wf_checking, @year) + accounts = [ + FinancialAccount.usb_checking, + FinancialAccount.wf_checking + ] + AccountData.new(accounts, @year) end def savings_data - AccountData.new(FinancialAccount.wf_savings, @year) + accounts = [ + FinancialAccount.wf_savings + ] + AccountData.new(accounts, @year) end class AccountData - def initialize(account, year) - @account = account + def initialize(accounts, year) + @accounts = accounts @year = year + grouped_statements end def starting_amounts - statements.map(&:starting_amount_cents) + grouped_statements.map do |statements| + amounts = statements.map(&:starting_amount_cents).compact + next if amounts.empty? + + amounts.sum + end end def ending_amounts - statements.map(&:ending_amount_cents) + grouped_statements.map do |statements| + amounts = statements.map(&:ending_amount_cents).compact + next if amounts.empty? + + amounts.sum + end end def net_amounts - statements.map(&:net_amount_cents) + grouped_statements.map do |statements| + amounts = statements.map(&:net_amount_cents).compact + next if amounts.empty? + + amounts.sum + end end private - def compute_statements - account_statements = @account.financial_statements.for_year(@year) + def compute_grouped_statements + account_statements = @accounts.map { |account| account.financial_statements.for_year(@year) }.flatten FinancialReport.months_for_year(@year).map do |date| - statement_for_period = account_statements.find { |statement| statement.period_start_on == date } - statement_for_period || NullStatement.instance + statements_for_period = account_statements.select { |statement| statement.period_start_on == date } + statements_for_period << NullStatement.instance if statements_for_period.empty? + statements_for_period end end - def statements - @statements ||= compute_statements + def grouped_statements + @grouped_statements ||= compute_grouped_statements end end end diff --git a/spec/system/financial_reports/admin_views_financial_report_spec.rb b/spec/system/financial_reports/admin_views_financial_report_spec.rb index b66781d..c477716 100644 --- a/spec/system/financial_reports/admin_views_financial_report_spec.rb +++ b/spec/system/financial_reports/admin_views_financial_report_spec.rb @@ -125,4 +125,36 @@ expect(net_tds.map(&:text).uniq).to eq ["$935.89"] end end + + scenario "with two statements on two accounts for the same period" do + FactoryBot.create( + :financial_statement, + financial_account: FinancialAccount.usb_checking, + period_start_on: Date.parse("#{last_year.year}-05-01"), + starting_amount_cents: 100_00, + ending_amount_cents: 50_00 + ) + + FactoryBot.create( + :financial_statement, + financial_account: FinancialAccount.wf_checking, + period_start_on: Date.parse("#{last_year.year}-05-01"), + starting_amount_cents: 70_00, + ending_amount_cents: 20_00 + ) + + visit "/financial_reports/#{last_year}" + + checking_table = page.all("table").to_a.first + _month_tr, starting_tr, ending_tr, net_tr = checking_table.all("tr").to_a + + may_starting_td = starting_tr.all("td").to_a[5] + expect(may_starting_td.text).to eq "$170.00" + + may_ending_td = ending_tr.all("td").to_a[5] + expect(may_ending_td.text).to eq "$70.00" + + may_net_td = net_tr.all("td").to_a[5] + expect(may_net_td.text).to eq "($100.00)" + end end