Verified Commit 974f84f7 authored by Kaspar Vollenweider's avatar Kaspar Vollenweider 👻
Browse files

tidy volunteers complicated with_billable_hours_scope up and add a test for...

tidy volunteers complicated with_billable_hours_scope up and add a test for not including billed again hours
parent 0cf94295
......@@ -196,40 +196,49 @@ 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)
.hours_meeting_date_semester(date)
.no_billing_expense_in_semester(date)
need_refunds.left_joins(:contact, :hours, :billing_expenses)
.with_billable_hours_meeting_date_semester(date)
.with_billable_hours_no_expense_in_semester(date)
.where('hours.billing_expense_id IS NULL')
.billable_hours_select
.where.not('hours.id IS NULL')
.with_billable_hours_select
.group(:id, 'contacts.full_name')
.order("(CASE WHEN COALESCE(volunteers.iban, '') = '' THEN 2 ELSE 1 END), contacts.full_name")
.with_billable_hours_order
end
scope :billable_hours_select, lambda {
select(
'SUM(hours.hours) AS total_hours, ' \
'contacts.full_name AS full_name, ' \
'volunteers.*'
)
scope :with_billable_hours_meeting_date_semester, lambda { |date|
if date
where('hours.meeting_date < :end_date AND hours.meeting_date >= :start_date',
start_date: date,
end_date: date.advance(months: BillingExpense::SEMESTER_LENGTH))
end
}
scope :no_billing_expense_in_semester, lambda { |date|
scope :with_billable_hours_no_expense_in_semester, lambda { |date|
if date
where(last_billing_expense: nil).or(
where.not('last_billing_expense = ?', date.to_date)
where.not('volunteers.last_billing_expense = ?', date.to_date)
)
end
}
scope :hours_meeting_date_semester, lambda { |date|
if date
where('hours.meeting_date < ?', date.advance(months: 6))
.where('hours.meeting_date >= ?', date)
end
scope :with_billable_hours_select, lambda {
select(<<-SQL.squish)
SUM(hours.hours) AS total_hours,
contacts.full_name AS full_name,
volunteers.*
SQL
}
scope :with_billable_hours_order, lambda {
order(<<-SQL.squish)
(CASE
WHEN COALESCE(volunteers.iban, '') = ''
THEN 2
ELSE 1
END),
contacts.full_name
SQL
}
scope :assignable_to_department, -> { undecided.where(department_id: [nil, '']) }
......
......@@ -370,6 +370,29 @@ class VolunteerScopesTest < ActiveSupport::TestCase
end
end
test 'allready_has_billing_expense_for_semester_adds_hours_in_semester_not_in_billing_list' do
travel_to tz_parse('2017-07-05-30')
volunteer = create :volunteer_with_user
assignment = create :assignment, volunteer: volunteer
creator = assignment.creator
volunteer.reload
hours_before = ['2017-01-10', '2017-04-08', '2017-05-20'].map.with_index do |date, index|
create(:hour, volunteer: volunteer, hourable: assignment, meeting_date: tz_parse(date),
hours: index + 1)
end
assert_includes Volunteer.with_billable_hours('2016-12-01'), volunteer
assert_equal 6.0, Volunteer.with_billable_hours('2016-12-01').to_a.first.total_hours
assert_equal 6, hours_before.map(&:hours).sum
BillingExpense.create_for!(Volunteer.with_billable_hours('2016-12-01'), creator, '2016-12-01')
assert Volunteer.with_billable_hours('2016-12-01').blank?
hours_after = ['2017-01-11', '2017-04-09', '2017-05-21'].map.with_index do |date, index|
create(:hour, volunteer: volunteer, hourable: assignment, meeting_date: tz_parse(date),
hours: index + 1)
end
assert_not_includes Volunteer.with_billable_hours('2016-12-01'), volunteer
assert_includes Volunteer.with_billable_hours, volunteer
end
test 'with_registered_user returns volunteers where password is set for user' do
volunteer_without_user1 = create :volunteer, :external
volunteer_without_user2 = create :volunteer, :external
......
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