Skip to content

Commit

Permalink
Include both checking accounts in report
Browse files Browse the repository at this point in the history
  • Loading branch information
jonallured authored and github-actions[bot] committed Feb 13, 2024
1 parent 4875ea5 commit 03d4b7c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 13 deletions.
50 changes: 37 additions & 13 deletions app/models/financial_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 32 additions & 0 deletions spec/system/financial_reports/admin_views_financial_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 03d4b7c

Please sign in to comment.