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

period to semester rename even for nitters... ;)

parent 26132e41
......@@ -14,7 +14,7 @@ class BillingExpense < ApplicationRecord
default_scope { order(created_at: :desc) }
scope :semester, ->(date) { joins(:hours).merge(Hour.period(date)) }
scope :semester, ->(date) { joins(:hours).merge(Hour.semester(date)) }
FINAL_AMOUNT_SQL = "CASE WHEN overwritten_amount IS NULL THEN amount ELSE overwritten_amount END".freeze
scope :sort_by_final_amount_asc, lambda {
......@@ -55,7 +55,7 @@ class BillingExpense < ApplicationRecord
def self.create_for!(volunteers, creator, date = nil)
transaction do
volunteers.find_each do |volunteer|
hours = volunteer.hours.billable.period(date)
hours = volunteer.hours.billable.semester(date)
hours.find_each do |hour|
hour.update!(reviewer: creator)
end
......
......@@ -18,7 +18,7 @@ class Hour < ApplicationRecord
scope :billable, (-> { where(billing_expense: nil) })
scope :billed, (-> { where.not(billing_expense: nil) })
scope :period, lambda { |date = nil|
scope :semester, lambda { |date = nil|
if date.present?
date = Time.zone.parse(date) unless date.is_a? Time
return if date.blank?
......
......@@ -195,7 +195,7 @@ class Volunteer < ApplicationRecord
scope :with_billable_hours, lambda { |date = nil|
hours = Hour.billable
hours = hours.period(date) if date.present?
hours = hours.semester(date) if date.present?
need_refunds
.joins(:contact)
......
......@@ -33,4 +33,4 @@ h1 Spesenformulare erfassen
span.label.label-danger Keine IBAN angegeben
td= link_to format_hours(volunteer.total_hours), volunteer_hours_path(volunteer)
td= format_currency BillingExpense.amount_for(volunteer.total_hours)
td= format_hours_semester volunteer.hours.period(@selected_billing_semester).billable
td= format_hours_semester volunteer.hours.semester(@selected_billing_semester).billable
......@@ -100,7 +100,7 @@ class BillingExpenseTest < ActiveSupport::TestCase
], semesters
end
test 'period scope' do
test 'semester scope' do
date = Time.zone.now.beginning_of_year
billing_expense1 = create :billing_expense,
......
......@@ -10,29 +10,29 @@ class HourTest < ActiveSupport::TestCase
assert hour.valid?
end
test 'period returns hours for a billing_expense period' do
current_period = BillingExpense::SEMESTER_LENGTH
a_period_ago = current_period.ago
last_period_ago = a_period_ago - BillingExpense::SEMESTER_LENGTH
test 'semester returns hours for a billing_expense semester' do
travel_to Time.zone.parse('2014-05-12')
a_semester_ago = BillingExpense::SEMESTER_LENGTH.ago
last_semester_ago = a_semester_ago - BillingExpense::SEMESTER_LENGTH
format = '%Y-%m-%d'
volunteer = create :volunteer
hour1 = create :hour, volunteer: volunteer, hours: 1, meeting_date: a_period_ago - 1.month
hour2 = create :hour, volunteer: volunteer, hours: 1, meeting_date: a_period_ago - 2.months
hour3 = create :hour, volunteer: volunteer, hours: 1, meeting_date: a_period_ago + 1.month
hour4 = create :hour, volunteer: volunteer, hours: 1, meeting_date: a_period_ago + 2.months
hour1 = create :hour, volunteer: volunteer, hours: 1, meeting_date: a_semester_ago - 1.month
hour2 = create :hour, volunteer: volunteer, hours: 1, meeting_date: a_semester_ago - 2.months
hour3 = create :hour, volunteer: volunteer, hours: 1, meeting_date: a_semester_ago + 1.month
hour4 = create :hour, volunteer: volunteer, hours: 1, meeting_date: a_semester_ago + 2.months
current_period_hours = Hour.period a_period_ago.strftime(format)
last_period_hours = Hour.period last_period_ago.strftime(format)
current_semester_hours = Hour.semester a_semester_ago.strftime(format)
last_semester_hours = Hour.semester last_semester_ago.strftime(format)
assert_includes current_period_hours, hour3
assert_includes current_period_hours, hour4
assert_not_includes current_period_hours, hour1
assert_not_includes current_period_hours, hour2
assert_includes current_semester_hours, hour3
assert_includes current_semester_hours, hour4
assert_not_includes current_semester_hours, hour1
assert_not_includes current_semester_hours, hour2
assert_includes last_period_hours, hour1
assert_includes last_period_hours, hour2
assert_not_includes last_period_hours, hour3
assert_not_includes last_period_hours, hour4
assert_includes last_semester_hours, hour1
assert_includes last_semester_hours, hour2
assert_not_includes last_semester_hours, hour3
assert_not_includes last_semester_hours, hour4
end
end
......@@ -330,45 +330,45 @@ class VolunteerScopesTest < ActiveSupport::TestCase
refute query.include? volunteer_will_inactive
end
test 'with_billable_hours returns volunteers with billable hours for an optional period' do
current_period_ago = BillingExpense::SEMESTER_LENGTH.ago
last_period_ago = current_period_ago - BillingExpense::SEMESTER_LENGTH
test 'with_billable_hours returns volunteers with billable hours for an optional semester' do
current_semester_ago = BillingExpense::SEMESTER_LENGTH.ago
last_semester_ago = current_semester_ago - BillingExpense::SEMESTER_LENGTH
format = '%Y-%m-%d'
volunteers_in_current_period_assertion = [@group_offer_member, @has_assignments]
volunteers_in_last_period_assertion = [@has_multiple, @has_active_and_inactive]
volunteers_in_current_semester_assertion = [@group_offer_member, @has_assignments]
volunteers_in_last_semester_assertion = [@has_multiple, @has_active_and_inactive]
volunteers_in_current_period_assertion.each do |volunteer|
create :hour, hours: 1, volunteer: volunteer, meeting_date: current_period_ago + 1.month
create :hour, hours: 2, volunteer: volunteer, meeting_date: current_period_ago + 2.months
volunteers_in_current_semester_assertion.each do |volunteer|
create :hour, hours: 1, volunteer: volunteer, meeting_date: current_semester_ago + 1.month
create :hour, hours: 2, volunteer: volunteer, meeting_date: current_semester_ago + 2.months
end
volunteers_in_last_period_assertion.each do |volunteer|
create :hour, hours: 3, volunteer: volunteer, meeting_date: current_period_ago - 1.month
create :hour, hours: 4, volunteer: volunteer, meeting_date: current_period_ago - 2.months
volunteers_in_last_semester_assertion.each do |volunteer|
create :hour, hours: 3, volunteer: volunteer, meeting_date: current_semester_ago - 1.month
create :hour, hours: 4, volunteer: volunteer, meeting_date: current_semester_ago - 2.months
end
volunteers_with_billable_hours = Volunteer.with_billable_hours
volunteers_in_current_period = Volunteer.with_billable_hours current_period_ago.strftime(format)
volunteers_in_last_period = Volunteer.with_billable_hours last_period_ago.strftime(format)
volunteers_in_current_semester = Volunteer.with_billable_hours current_semester_ago.strftime(format)
volunteers_in_last_semester = Volunteer.with_billable_hours last_semester_ago.strftime(format)
(volunteers_in_current_period + volunteers_in_last_period).each do |volunteer|
(volunteers_in_current_semester + volunteers_in_last_semester).each do |volunteer|
assert_includes volunteers_with_billable_hours, volunteer
end
volunteers_in_current_period.each do |volunteer|
assert_includes volunteers_in_current_period, volunteer
volunteers_in_current_semester.each do |volunteer|
assert_includes volunteers_in_current_semester, volunteer
end
volunteers_in_last_period.each do |volunteer|
assert_not_includes volunteers_in_current_period, volunteer
volunteers_in_last_semester.each do |volunteer|
assert_not_includes volunteers_in_current_semester, volunteer
end
volunteers_in_last_period.each do |volunteer|
assert_includes volunteers_in_last_period, volunteer
volunteers_in_last_semester.each do |volunteer|
assert_includes volunteers_in_last_semester, volunteer
end
volunteers_in_current_period.each do |volunteer|
assert_not_includes volunteers_in_last_period, volunteer
volunteers_in_current_semester.each do |volunteer|
assert_not_includes volunteers_in_last_semester, volunteer
end
end
......
......@@ -94,7 +94,7 @@ class BillingExpensesTest < ApplicationSystemTestCase
refute_text @volunteer4
end
test 'new_billing_expense_respects_the_period_filter' do
test 'new_billing_expense_respects_the_semester_filter' do
volunteer1 = create :volunteer
create :hour, volunteer: volunteer1, hours: 10, meeting_date: Time.zone.parse('2017-11-01')
create :hour, volunteer: volunteer1, hours: 16, meeting_date: Time.zone.parse('2017-10-01')
......@@ -134,12 +134,12 @@ class BillingExpensesTest < ApplicationSystemTestCase
assert_text "#{volunteer3} #{volunteer3.iban} 3 Stunden Fr. 50.00 1. Semester 2018"
end
test 'creating_a_billing_expense_should_respect_period_filter' do
test 'creating_a_billing_expense_should_respect_semester_filter' do
volunteer = create :volunteer
create :hour, volunteer: volunteer, hours: 26, meeting_date: Time.zone.parse('2017-11-01')
create :hour, volunteer: volunteer, hours: 16, meeting_date: Time.zone.parse('2018-02-01')
# creating billing_expense for hours in the current period
# creating billing_expense for hours in the current semester
visit billing_expenses_path
click_link 'Spesenformulare erstellen'
......@@ -174,7 +174,7 @@ class BillingExpensesTest < ApplicationSystemTestCase
click_link 'Alle'
assert_text "#{volunteer} #{volunteer.iban} 26 Stunden Fr. 100.00 2. Semester 2017"
# creating billing_expense for all hours in multiple periods
# creating billing_expense for all hours in multiple semesters
volunteer = create :volunteer
create :hour, volunteer: volunteer, hours: 26, meeting_date: Time.zone.parse('2017-11-01')
create :hour, volunteer: volunteer, hours: 16, meeting_date: Time.zone.parse('2018-02-01')
......
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