Commit b13bac52 authored by Zsolt Benke's avatar Zsolt Benke
Browse files

Creating billing_expense respects the period filter

parent e2ebf436
......@@ -64,8 +64,9 @@ class BillingExpensesController < ApplicationController
authorize BillingExpense, :create?
selected_volunteers = params[:selected_volunteers]
selected_period = params[:selected_period]
volunteers = Volunteer.need_refunds.where(id: selected_volunteers)
BillingExpense.create_for!(volunteers, current_user)
BillingExpense.create_for!(volunteers, current_user, selected_period)
redirect_to billing_expenses_url,
notice: 'Spesenformulare wurden erfolgreich erstellt.'
......
......@@ -52,10 +52,10 @@ class BillingExpense < ApplicationRecord
end
end
def self.create_for!(volunteers, creator)
def self.create_for!(volunteers, creator, date = nil)
transaction do
volunteers.find_each do |volunteer|
hours = volunteer.hours.billable
hours = volunteer.hours.billable.period(date)
hours.find_each do |hour|
hour.update!(reviewer: creator)
end
......
......@@ -18,11 +18,13 @@ class Hour < ApplicationRecord
scope :billable, (-> { where(billing_expense: nil) })
scope :billed, (-> { where.not(billing_expense: nil) })
scope :period, lambda { |date|
return unless date.present?
date = Time.zone.parse(date) unless date.is_a? Time
scope :period, lambda { |date = nil|
if date.present?
date = Time.zone.parse(date) unless date.is_a? Time
return if date.blank?
date_between(:meeting_date, date, date + BillingExpense::PERIOD)
date_between(:meeting_date, date, date + BillingExpense::PERIOD)
end
}
scope :since_last_submitted, lambda { |submitted_at|
......
......@@ -3,6 +3,7 @@
h1 Spesenformulare erfassen
= simple_form_for @billing_expense do |f|
= hidden_field_tag 'selected_period', @selected_billing_period
.row
.col-xs-12
=> button_link navigation_glyph(:back), billing_expenses_path
......@@ -21,7 +22,7 @@ h1 Spesenformulare erfassen
tbody
- @volunteers.each do |volunteer|
tr.table-row-selectable
tr.table-row-selectable id=dom_id(volunteer)
td= check_box_tag 'selected_volunteers[]', volunteer.id,
@selected_volunteers.include?(volunteer.id.to_s), disabled: !volunteer.iban?
td= link_to volunteer, edit_volunteer_path(volunteer)
......
......@@ -95,12 +95,6 @@ class BillingExpensesTest < ApplicationSystemTestCase
end
test 'new billing_expense respects the period filter' do
def period_text(start_hour, end_hour)
text = ["#{I18n.l(start_hour.meeting_date)}"]
text << "#{I18n.l(end_hour.meeting_date)}"
text.join(' - ')
end
volunteer1 = create :volunteer
hour1 = create :hour, volunteer: volunteer1, hours: 10, meeting_date: @date - 2.month
hour2 = create :hour, volunteer: volunteer1, hours: 16, meeting_date: @date - 3.month
......@@ -139,6 +133,70 @@ class BillingExpensesTest < ApplicationSystemTestCase
assert_text "#{volunteer3} #{volunteer3.iban} 3 Stunden Fr. 50.00 #{period_text(hour5, hour6)}"
end
test 'creating a billing_expense should respect period filter' do
volunteer = create :volunteer
hour1 = create :hour, volunteer: volunteer, hours: 26, meeting_date: @date - 2.month
hour2 = create :hour, volunteer: volunteer, hours: 16, meeting_date: @date + 1.month
# creating billing_expense for hours in the current period
visit billing_expenses_path
click_link 'Spesenformulare erstellen'
within "##{dom_id(volunteer)}" do
check 'selected_volunteers[]'
end
assert_checked_field 'selected_volunteers[]', count: 1
page.accept_confirm do
click_button 'Spesenformulare erstellen'
end
assert_text "#{volunteer} #{volunteer.iban} 16 Stunden Fr. 50.00 #{period_text(hour2, hour2)}"
# creating billing_expense for the all remaining hours
visit billing_expenses_path
click_link 'Periode: Januar 2018 - Juni 2018'
click_link 'Alle'
click_link 'Spesenformulare erstellen'
within "##{dom_id(volunteer)}" do
check 'selected_volunteers[]'
end
assert_checked_field 'selected_volunteers[]', count: 1
page.accept_confirm do
click_button 'Spesenformulare erstellen'
end
click_link 'Periode: Januar 2018 - Juni 2018'
click_link 'Alle'
assert_text "#{volunteer} #{volunteer.iban} 26 Stunden Fr. 100.00 #{period_text(hour1, hour1)}"
# creating billing_expense for all hours in multiple periods
volunteer = create :volunteer
hour1 = create :hour, volunteer: volunteer, hours: 26, meeting_date: @date - 2.month
hour2 = create :hour, volunteer: volunteer, hours: 16, meeting_date: @date + 1.month
visit billing_expenses_path
click_link 'Periode: Januar 2018 - Juni 2018'
click_link 'Alle'
click_link 'Spesenformulare erstellen'
within "##{dom_id(volunteer)}" do
check 'selected_volunteers[]'
end
assert_checked_field 'selected_volunteers[]', count: 1
page.accept_confirm do
click_button 'Spesenformulare erstellen'
end
click_link 'Periode: Januar 2018 - Juni 2018'
click_link 'Alle'
assert_text "#{volunteer} #{volunteer.iban} 42 Stunden Fr. 100.00 #{period_text(hour1, hour2)}"
end
test 'volunteer profile shows only billing expenses for this volunteer' do
login_as @volunteer1.user
visit volunteer_path(@volunteer1)
......@@ -229,4 +287,12 @@ class BillingExpensesTest < ApplicationSystemTestCase
assert page.has_text? 'Fr. 100'
assert_equal billing_expense.reload.final_amount, 100
end
private
def period_text(start_hour, end_hour)
text = ["#{I18n.l(start_hour.meeting_date)}"]
text << "#{I18n.l(end_hour.meeting_date)}"
text.join(' - ')
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