Verified Commit 5d8f439d authored by Kaspar Vollenweider's avatar Kaspar Vollenweider 👻
Browse files

use aggregated hour ids in create for method

- if the query result delivers hour_ids array, query Hours directly by ids
- in case there is no hour_ids in the query result get hours the same way as before
parent e61adcd5
......@@ -54,12 +54,11 @@ class BillingExpense < ApplicationRecord
end
def self.create_for!(volunteers, creator, date = nil)
billing_semester = billable_semester_date(date)
transaction do
volunteers.find_each do |volunteer|
hours = volunteer.hours.billable.semester(date)
hours.find_each do |hour|
hour.update!(reviewer: creator)
end
hours = volunteer.hours.billable.semester(billing_semester)
hours.find_each { |hour| hour.update!(reviewer: creator) }
create!(
volunteer: volunteer,
......@@ -68,7 +67,7 @@ class BillingExpense < ApplicationRecord
bank: volunteer.bank,
iban: volunteer.iban
)
volunteer.update(last_billing_expense: billing_expense_semester(date).to_date)
volunteer.update(last_billing_expense: billing_expense_semester(billing_semester))
end
end
end
......
......@@ -196,13 +196,14 @@ class Volunteer < ApplicationRecord
def self.with_billable_hours(date = nil)
date = billable_semester_date(date)
need_refunds
.left_joins(:contact)
.left_joins(:hours)
.left_joins(:billing_expenses)
.where('hours.id IS NOT NULL AND hours.billing_expense_id IS NULL')
.hours_meeting_date_semester(date)
.no_billing_expense_in_semester(date)
.where('hours.billing_expense_id IS NULL')
.billable_hours_select
.group(:id, 'contacts.full_name')
.order("(CASE WHEN COALESCE(volunteers.iban, '') = '' THEN 2 ELSE 1 END), contacts.full_name")
......@@ -211,8 +212,6 @@ class Volunteer < ApplicationRecord
scope :billable_hours_select, lambda {
select(
'SUM(hours.hours) AS total_hours, ' \
'ARRAY_AGG(hours.meeting_date) AS meeting_dates, ' \
'ARRAY_AGG(hours.id) AS hour_ids, ' \
'contacts.full_name AS full_name, ' \
'volunteers.*'
)
......
......@@ -6,6 +6,7 @@ FactoryBot.define do
acceptance :accepted
group_offer_categories { |category| [category.association(:group_offer_category)] }
iban { generate :iban }
association :registrar, factory: :user
trait :with_language_skills do
language_skills do |language_skill|
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment