Skip to content

Commit

Permalink
Merge 9b05013 into 8836697
Browse files Browse the repository at this point in the history
  • Loading branch information
yksflip committed Mar 7, 2024
2 parents 8836697 + 9b05013 commit 6894aa0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
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
1 change: 1 addition & 0 deletions app/models/ordergroup.rb
Expand Up @@ -21,6 +21,7 @@ class Ordergroup < Group
after_create :update_stats!

scope :active, -> { joins(:orders).where(orders: { starts: (Time.now.months_ago(3)..Time.now) }).group(:id) }
scope :not_deleted, -> { where(deleted_at: nil) }

def contact
"#{contact_phone} (#{contact_person})"
Expand Down
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

0 comments on commit 6894aa0

Please sign in to comment.