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

some more MR review changes

parent 5f52dacd
Pipeline #22662 passed with stage
in 44 minutes and 1 second
......@@ -78,7 +78,7 @@ class BillingExpense < ApplicationRecord
last_semester = semester_from_hours(scoped_hours)
semesters = [semester_filter_hash(last_semester)]
semester_back_count(first_semester, last_semester).times do
semester_back_count(first_semester.to_time, last_semester.to_time).times do
last_semester = last_semester.advance(months: -SEMESTER_LENGTH)
next if scoped_hours.semester(last_semester).blank?
semesters << semester_filter_hash(last_semester)
......
......@@ -33,28 +33,27 @@ module BillingExpenseSemesterUtils
end
def self.billing_expense_semester(date)
if date.blank?
current_semester_start
else
if date
billable_semester_date(date)
else
current_semester_start
end
end
def self.billable_semester_date(date)
return if date.blank?
unless date.respond_to?(:beginning_of_time)
date = Time.zone.parse(date)
end
date = Time.zone.parse(date) if date.is_a? String
date.beginning_of_day
end
def self.dates_semester_start(date)
year = date.year
date = date.to_time unless date.respond_to?(:to_f)
if semester_of_year(date) == 2
Time.zone.local(year, 6, 1).beginning_of_day
Time.zone.local(date.year, 6, 1).beginning_of_day
elsif date.month == 12
Time.zone.local(date.year, 12, 1).beginning_of_day
else
year -= 1 if date.month.eql?(12)
Time.zone.local(year, 12, 1).beginning_of_day
Time.zone.local(date.year - 1, 12, 1).beginning_of_day
end
end
......
......@@ -207,14 +207,14 @@ class Volunteer < ApplicationRecord
end
scope :with_billable_hours_meeting_date_semester, lambda { |date|
all unless date
return all if date.blank?
where('hours.meeting_date < :end_date AND hours.meeting_date >= :start_date',
start_date: date,
end_date: date.advance(months: BillingExpense::SEMESTER_LENGTH))
}
scope :with_billable_hours_no_expense_in_semester, lambda { |date|
all unless date
return all if date.blank?
where(last_billing_expense_on: nil).or(
where.not('volunteers.last_billing_expense_on = ?', date)
)
......
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20180716084125) do
ActiveRecord::Schema.define(version: 20180713084814) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......@@ -731,7 +731,7 @@ ActiveRecord::Schema.define(version: 20180716084125) do
t.text "additional_comments"
t.boolean "teenager"
t.bigint "department_id"
t.date "last_billing_expense"
t.datetime "last_billing_expense_on"
t.index ["acceptance"], name: "index_volunteers_on_acceptance"
t.index ["accepted_at"], name: "index_volunteers_on_accepted_at"
t.index ["active"], name: "index_volunteers_on_active"
......
......@@ -17,20 +17,20 @@ class BillingExpenseTest < ActiveSupport::TestCase
end
test 'create_for' do # rubocop:disable Metrics/BlockLength
travel_to tz_parse('2017-07-12')
travel_to time_z(2017, 7, 12)
volunteer1 = create :volunteer_with_user, bank: 'Bank 1'
other_creator = volunteer1.registrar
assignment1 = create :assignment, volunteer: volunteer1, creator: other_creator
hour1a = create :hour, volunteer: volunteer1, meeting_date: tz_parse('2017-04-02'), hourable: assignment1
hour1b = create :hour, volunteer: volunteer1, meeting_date: tz_parse('2017-05-12'), hourable: assignment1
hour1c = create :hour, volunteer: volunteer1, meeting_date: tz_parse('2017-01-18'), hourable: assignment1
hour1a = create :hour, volunteer: volunteer1, meeting_date: time_z(2017, 4, 2), hourable: assignment1
hour1b = create :hour, volunteer: volunteer1, meeting_date: time_z(2017, 5, 12), hourable: assignment1
hour1c = create :hour, volunteer: volunteer1, meeting_date: time_z(2017, 1, 18), hourable: assignment1
create :billing_expense, volunteer: volunteer1, hours: [hour1c], user: other_creator
volunteer2 = create :volunteer, bank: 'Bank 2'
creator = volunteer2.registrar
group_assignment1 = create :group_assignment, volunteer: volunteer2, creator: creator
hour2 = create :hour, volunteer: volunteer2, hours: 75, meeting_date: tz_parse('2017-03-22'), hourable: group_assignment1
hour2 = create :hour, volunteer: volunteer2, hours: 75, meeting_date: time_z(2017, 3, 22), hourable: group_assignment1
assert_equal 1, BillingExpense.count
assert_equal 1, volunteer1.billing_expenses.count
......@@ -88,39 +88,37 @@ class BillingExpenseTest < ActiveSupport::TestCase
end
test 'generate_semester_filters_with_hours' do
travel_to tz_parse('2018-06-30')
create :hour, meeting_date: '2017-06-30'
create :hour, meeting_date: '2012-06-30'
create :billing_expense, hours: [create(:hour, meeting_date: '2014-02-03'),
create(:hour, meeting_date: '2015-06-30')]
semesters = BillingExpense.generate_semester_filters(:billed)
really_destroy_with_deleted(BillingExpense, Hour)
create :billing_expense, hours: [
create(:hour, meeting_date: time_z(2014, 2, 3)),
create(:hour, meeting_date: time_z(2015, 2, 3))
]
assert_equal [
{ q: :semester, value: '2015-06-01', text: '2. Semester 2015' },
{ q: :semester, value: '2014-12-01', text: '1. Semester 2015' },
{ q: :semester, value: '2013-12-01', text: '1. Semester 2014' }
], semesters
semesters = BillingExpense.generate_semester_filters(:billable)
], BillingExpense.generate_semester_filters(:billed)
create :hour, meeting_date: '2017-06-30'
create :hour, meeting_date: '2012-06-30'
assert_equal [
{ q: :semester, value: '2017-06-01', text: '2. Semester 2017' },
{ q: :semester, value: '2012-06-01', text: '2. Semester 2012' }
], semesters
], BillingExpense.generate_semester_filters(:billable)
end
test 'semester_scope' do
billing_expense1 = create :billing_expense,
hours: [create(:hour, meeting_date: tz_parse('2017-01-12'))]
hours: [create(:hour, meeting_date: time_z(2017, 1, 12))]
billing_expense2 = create :billing_expense,
hours: [
create(:hour, meeting_date: tz_parse('2017-02-01')),
create(:hour, meeting_date: tz_parse('2017-05-12'))
create(:hour, meeting_date: time_z(2017, 2, 1)),
create(:hour, meeting_date: time_z(2017, 5, 12))
]
_billing_expense3 = create :billing_expense,
hours: [create(:hour, meeting_date: tz_parse('2016-11-30'))]
hours: [create(:hour, meeting_date: time_z(2016, 11, 30))]
assert_includes BillingExpense.semester('2016-12-01'), billing_expense1
assert_includes BillingExpense.semester('2016-12-01'), billing_expense2
......
......@@ -11,13 +11,13 @@ class HourTest < ActiveSupport::TestCase
end
test 'semester returns hours for a billing_expense semester' do
travel_to tz_parse('2018-05-25')
travel_to time_z(2018, 5, 25)
volunteer = create :volunteer
prev_hour1 = create :hour, volunteer: volunteer, hours: 1, meeting_date: tz_parse('2016-11-15')
prev_hour2 = create :hour, volunteer: volunteer, hours: 1, meeting_date: tz_parse('2016-10-01')
this_hour1 = create :hour, volunteer: volunteer, hours: 1, meeting_date: tz_parse('2016-12-01')
this_hour2 = create :hour, volunteer: volunteer, hours: 1, meeting_date: tz_parse('2017-05-11')
other_hour = create :hour, volunteer: volunteer, hours: 1, meeting_date: tz_parse('2013-11-21')
prev_hour1 = create :hour, volunteer: volunteer, hours: 1, meeting_date: time_z(2016, 11, 15)
prev_hour2 = create :hour, volunteer: volunteer, hours: 1, meeting_date: time_z(2016, 10, 1)
this_hour1 = create :hour, volunteer: volunteer, hours: 1, meeting_date: time_z(2016, 12, 1)
this_hour2 = create :hour, volunteer: volunteer, hours: 1, meeting_date: time_z(2017, 5, 11)
other_hour = create :hour, volunteer: volunteer, hours: 1, meeting_date: time_z(2013, 11, 21)
current_semester_hours = Hour.semester '2016-12-01'
last_semester_hours = Hour.semester '2016-06-01'
......
......@@ -330,19 +330,19 @@ class VolunteerScopesTest < ActiveSupport::TestCase
refute query.include? volunteer_will_inactive
end
test 'with_billable_hours returns volunteers with billable hours for an optional semester' do
travel_to tz_parse('2017-07-01')
test 'with_billable_hours_returns_volunteers_with_billable_hours_for_an_optional_semester' do
travel_to time_z(2017, 7, 1)
volunteers_in_current_semester_assertion = [@group_offer_member, @has_assignments]
volunteers_in_last_semester_assertion = [@has_multiple, @has_active_and_inactive]
volunteers_in_current_semester_assertion.each do |volunteer|
create :hour, hours: 1, volunteer: volunteer, meeting_date: tz_parse('2017-01-01')
create :hour, hours: 2, volunteer: volunteer, meeting_date: tz_parse('2017-02-02')
create :hour, hours: 1, volunteer: volunteer, meeting_date: time_z(2017, 1, 1)
create :hour, hours: 2, volunteer: volunteer, meeting_date: time_z(2017, 2, 2)
end
volunteers_in_last_semester_assertion.each do |volunteer|
create :hour, hours: 3, volunteer: volunteer, meeting_date: tz_parse('2016-09-01')
create :hour, hours: 4, volunteer: volunteer, meeting_date: tz_parse('2016-11-11')
create :hour, hours: 3, volunteer: volunteer, meeting_date: time_z(2016, 9, 1)
create :hour, hours: 4, volunteer: volunteer, meeting_date: time_z(2016, 11, 11)
end
volunteers_with_billable_hours = Volunteer.with_billable_hours
......@@ -371,13 +371,13 @@ class VolunteerScopesTest < ActiveSupport::TestCase
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')
travel_to time_z(2017, 7, 5)
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),
create(:hour, volunteer: volunteer, hourable: assignment, meeting_date: time_z(date),
hours: index + 1)
end
assert_includes Volunteer.with_billable_hours('2016-12-01'), volunteer
......@@ -386,7 +386,7 @@ class VolunteerScopesTest < ActiveSupport::TestCase
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),
create(:hour, volunteer: volunteer, hourable: assignment, meeting_date: time_z(date),
hours: index + 1)
end
assert_not_includes Volunteer.with_billable_hours('2016-12-01'), volunteer
......
......@@ -3,7 +3,7 @@ require 'application_system_test_case'
class BillingExpensesTest < ApplicationSystemTestCase
def setup
superadmin = create :user
@date = tz_parse('2018-01-01')
@date = time_z(2018, 1, 1)
@volunteer1 = create :volunteer, bank: 'UBS'
@assignment1 = create :assignment, volunteer: @volunteer1
......@@ -30,7 +30,7 @@ class BillingExpensesTest < ApplicationSystemTestCase
group_assignment4 = create :group_assignment, volunteer: @volunteer4
billed_hour4 = create :hour, volunteer: @volunteer4,
hourable: group_assignment4.group_offer,
hours: 5.5, meeting_date: tz_parse('2017-11-01')
hours: 5.5, meeting_date: time_z(2017, 11, 1)
@billing_expense4 = create :billing_expense, volunteer: @volunteer4, hours: [billed_hour4],
created_at: 1.hour.ago
......@@ -95,16 +95,16 @@ class BillingExpensesTest < ApplicationSystemTestCase
test 'new_billing_expense_respects_the_semester_filter' do
volunteer1 = create :volunteer
create :hour, volunteer: volunteer1, hours: 10, meeting_date: tz_parse('2017-11-01')
create :hour, volunteer: volunteer1, hours: 16, meeting_date: tz_parse('2017-10-01')
create :hour, volunteer: volunteer1, hours: 10, meeting_date: time_z(2017, 11, 1)
create :hour, volunteer: volunteer1, hours: 16, meeting_date: time_z(2017, 10, 1)
volunteer2 = create :volunteer, iban: 'pick_out_volunteer'
create :hour, volunteer: volunteer2, hours: 26, meeting_date: tz_parse('2018-02-01')
create :hour, volunteer: volunteer2, hours: 15, meeting_date: tz_parse('2017-11-01')
create :hour, volunteer: volunteer2, hours: 26, meeting_date: time_z(2018, 2, 1)
create :hour, volunteer: volunteer2, hours: 15, meeting_date: time_z(2017, 11, 1)
volunteer3 = create :volunteer
create :hour, volunteer: volunteer3, hours: 1, meeting_date: tz_parse('2018-02-01')
create :hour, volunteer: volunteer3, hours: 2, meeting_date: tz_parse('2018-04-01')
create :hour, volunteer: volunteer3, hours: 1, meeting_date: time_z(2018, 2, 1)
create :hour, volunteer: volunteer3, hours: 2, meeting_date: time_z(2018, 4, 1)
visit billing_expenses_path
......@@ -135,8 +135,8 @@ class BillingExpensesTest < ApplicationSystemTestCase
test 'creating_a_billing_expense_should_respect_semester_filter' do
volunteer = create :volunteer
create :hour, volunteer: volunteer, hours: 26, meeting_date: tz_parse('2017-11-01')
create :hour, volunteer: volunteer, hours: 16, meeting_date: tz_parse('2018-02-01')
create :hour, volunteer: volunteer, hours: 26, meeting_date: time_z(2017, 11, 1)
create :hour, volunteer: volunteer, hours: 16, meeting_date: time_z(2018, 2, 1)
# creating billing_expense for hours in the current semester
visit billing_expenses_path
......@@ -174,8 +174,8 @@ class BillingExpensesTest < ApplicationSystemTestCase
# creating billing_expense for all hours in multiple semesters
volunteer = create :volunteer
create :hour, volunteer: volunteer, hours: 26, meeting_date: tz_parse('2017-11-01')
create :hour, volunteer: volunteer, hours: 16, meeting_date: tz_parse('2018-02-01')
create :hour, volunteer: volunteer, hours: 26, meeting_date: time_z(2017, 11, 1)
create :hour, volunteer: volunteer, hours: 16, meeting_date: time_z(2018, 2, 1)
visit billing_expenses_path
click_link 'Semester: 1. Semester 2018'
......
......@@ -66,7 +66,8 @@ class ActiveSupport::TestCase
end
end
def tz_parse(date_string)
Time.zone.parse(date_string)
def time_z(year, month = nil, day = nil)
year, month, day = year.split('-').map(&:to_i) if month.blank?
Time.zone.local(year, month, day)
end
end
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