Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(finance): ordergroup overview total balances #1051

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/controllers/finance/ordergroups_controller.rb
Expand Up @@ -13,8 +13,8 @@ def index
@ordergroups = @ordergroups.where('groups.name LIKE ?', "%#{params[:query]}%") unless params[:query].nil?
@ordergroups = @ordergroups.page(params[:page]).per(@per_page)

@total_balances = FinancialTransactionClass.sorted.each_with_object({}) do |c, tmp|
tmp[c.id] = c.financial_transactions.reduce(0) { |sum, t| sum + t.amount }
@total_balances = FinancialTransactionClass.sorted.each_with_object({}) do |transaction_class, total_balances|
total_balances[transaction_class.id] = @ordergroups.reduce(0) { |sum, o| o["sum_of_class_#{transaction_class.id}"] + sum }
end
end
end
44 changes: 40 additions & 4 deletions spec/controllers/finance/ordergroups_controller_spec.rb
Expand Up @@ -25,9 +25,13 @@
end
let(:fin_trans3) do
create(:financial_transaction,
user: user,
amount: 42.23,
ordergroup: user.ordergroup,
financial_transaction_type: fin_trans_type2)
end
let(:fin_trans_foodcoop) do
create(:financial_transaction,
amount: 111,
ordergroup: nil,
financial_transaction_type: fin_trans_type2)
end

Expand All @@ -49,9 +53,41 @@
get_with_defaults :index
expect(response).to have_http_status(:success)

assert_select "#total_balance#{fin_trans_type1.financial_transaction_class_id}", number_to_currency(300)
assert_select "#total_balance#{fin_trans_type2.financial_transaction_class_id}", number_to_currency(42.23)
assert_total_balance_of_transaction_type1(300)
assert_total_balance_of_transaction_type2(42.23)
assert_total_balance_sum(342.23)
end

it 'ignores deleted ordergroups' do
user.ordergroup.mark_as_deleted
get_with_defaults :index
assert_total_balance_of_transaction_type1(0)
assert_total_balance_of_transaction_type2(42.23)
assert_select '#total_balance_sum', number_to_currency(42.23)
end

it 'ignores foodcoop transactions' do
fin_trans_foodcoop
get_with_defaults :index
assert_total_balance_of_transaction_type1(300)
assert_total_balance_of_transaction_type2(42.23)
assert_select '#total_balance_sum', number_to_currency(342.23)
end
end

def assert_total_balance_sum(amount)
assert_select '#total_balance_sum', number_to_currency(amount)
end

def assert_total_balance_of_transaction_type1(amount)
assert_total_balanceof_transaction_type(fin_trans_type1.financial_transaction_class_id, amount)
end

def assert_total_balance_of_transaction_type2(amount)
assert_total_balanceof_transaction_type(fin_trans_type2.financial_transaction_class_id, amount)
end

def assert_total_balanceof_transaction_type(type, amount)
assert_select "#total_balance#{type}", number_to_currency(amount)
end
end