From 69c80eba3e76bef5c73644f789007798191294fa Mon Sep 17 00:00:00 2001 From: Philipp Rothmann Date: Fri, 17 Feb 2023 12:40:26 +0100 Subject: [PATCH] feat(finance): show sum of ordergroup balances --- .../finance/ordergroups_controller.rb | 5 +- .../ordergroups/_ordergroups.html.haml | 9 ++++ .../finance/ordergroups_controller_spec.rb | 50 +++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 spec/controllers/finance/ordergroups_controller_spec.rb diff --git a/app/controllers/finance/ordergroups_controller.rb b/app/controllers/finance/ordergroups_controller.rb index cb661571..d334f223 100644 --- a/app/controllers/finance/ordergroups_controller.rb +++ b/app/controllers/finance/ordergroups_controller.rb @@ -11,7 +11,10 @@ class Finance::OrdergroupsController < Finance::BaseController @ordergroups = Ordergroup.undeleted.order(sort) @ordergroups = @ordergroups.include_transaction_class_sum @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 } + end end end diff --git a/app/views/finance/ordergroups/_ordergroups.html.haml b/app/views/finance/ordergroups/_ordergroups.html.haml index 83a05ed2..3e0c99fc 100644 --- a/app/views/finance/ordergroups/_ordergroups.html.haml +++ b/app/views/finance/ordergroups/_ordergroups.html.haml @@ -22,3 +22,12 @@ %td = link_to t('.new_transaction'), new_finance_ordergroup_transaction_path(ordergroup), class: 'btn btn-mini' = link_to t('.account_statement'), finance_ordergroup_transactions_path(ordergroup), class: 'btn btn-mini' + %thead + %tr + %th= t 'Total' + %th + - FinancialTransactionClass.sorted.each do |c| + - name = FinancialTransactionClass.has_multiple_classes ? c.display : heading_helper(Ordergroup, :account_balance) + %th.numeric= format_currency @total_balances[c.id] + %th.numeric + = format_currency @total_balances.values.reduce(:+) \ No newline at end of file diff --git a/spec/controllers/finance/ordergroups_controller_spec.rb b/spec/controllers/finance/ordergroups_controller_spec.rb new file mode 100644 index 00000000..f960c61d --- /dev/null +++ b/spec/controllers/finance/ordergroups_controller_spec.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Finance::OrdergroupsController do + let(:user) { create(:user, :role_finance, :role_orders, :ordergroup) } + let(:fin_trans_type1) { create(:financial_transaction_type) } + let(:fin_trans_type2) { create(:financial_transaction_type) } + let(:fin_trans1) do + create(:financial_transaction, + user: user, + ordergroup: user.ordergroup, + financial_transaction_type: fin_trans_type1) + end + let(:fin_trans2) do + create(:financial_transaction, + user: user, + ordergroup: user.ordergroup, + financial_transaction_type: fin_trans_type1) + end + let(:fin_trans3) do + create(:financial_transaction, + user: user, + ordergroup: user.ordergroup, + financial_transaction_type: fin_trans_type2) + end + + before { login user } + + describe 'GET index' do + before do + fin_trans1 + fin_trans2 + fin_trans3 + end + + it 'renders index page' do + get_with_defaults :index + expect(response).to have_http_status(:success) + expect(response).to render_template('finance/ordergroups/index') + end + + it 'calculates total balance sums correctly' do + get_with_defaults :index + expect(assigns(:total_balances).size).to eq(2) + expect(assigns(:total_balances)[fin_trans_type1.id]).to eq(fin_trans1.amount + fin_trans2.amount) + expect(assigns(:total_balances)[fin_trans_type2.id]).to eq(fin_trans3.amount) + end + end +end