Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiday absencs for part time employees #271

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ group :development do
end

group :development, :test do
gem 'pry'
gem 'awesome_print'
gem 'factory_bot_rails'
gem 'i18n-tasks'
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ GEM
chunky_png (1.4.0)
classy_cancan (0.0.3)
cancancan
coderay (1.1.3)
coffee-rails (5.0.0)
coffee-script (>= 2.2.0)
railties (>= 5.2.0)
Expand Down Expand Up @@ -210,6 +211,7 @@ GEM
net-smtp
marcel (1.0.4)
matrix (0.4.2)
method_source (1.1.0)
mini_mime (1.1.5)
mini_portile2 (2.8.7)
minitest (5.23.1)
Expand Down Expand Up @@ -246,6 +248,9 @@ GEM
ast (~> 2.4.1)
racc
pg (1.5.6)
pry (0.15.0)
coderay (~> 1.1)
method_source (~> 1.0)
psych (5.1.2)
stringio
public_suffix (5.0.5)
Expand Down Expand Up @@ -465,6 +470,7 @@ DEPENDENCIES
money-rails
paper_trail
pg
pry
puma
rails (~> 7.0)
rails-controller-testing
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/absences_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def new
end

def create
if @absence.hours.nil?
@absence.hours = AbsencesService.new.calculate_default_hours(@absence)
end

@absence.save
respond_with @absence, location: absences_path
end
Expand Down
22 changes: 20 additions & 2 deletions app/models/absence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Absence < ActiveRecord::Base
belongs_to :employee

validates :employee, :reason, :from_date, :to_date, presence: true
validates :hours, absence: true, unless: :one_day_absence?
validates :hours, presence: true
validate :to_after_from

enumerize :reason, in: { holidays: 0, doctor: 1, funeral: 2, disease: 3,
Expand All @@ -17,8 +17,26 @@ class Absence < ActiveRecord::Base

def absent_target_hours(from: from_date, to: to_date)
return hours if one_day_absence?
# we can return the full hours if the requested range covers the full absence
if from <= from_date && to >= to_date
return hours
end

# if the requested range does not cover the full range of the absence, it's getting compicated...
start_date = [from_date, from].max
end_date = [to, to_date].min

# calculate the absent target hours for the full absence
absent_target_hours_of_full_absence = TargetHours.hours_between(from: from_date, to: to_date)

# and the absent target hours in the requested range
absent_target_hours_within_requested_range = TargetHours.hours_between(from: start_date, to: end_date)

# calculate a hours per target hours ratio
absent_hours_per_target_hours = absent_target_hours_of_full_absence / hours

TargetHours.hours_between(from: [from_date, from].max, to: [to, to_date].min)
# and multiply this by the absent target hours with in the range
absent_hours_per_target_hours * absent_target_hours_within_requested_range
end

def to_after_from
Expand Down
11 changes: 11 additions & 0 deletions app/services/absences_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AbsencesService
def calculate_default_hours(absence)
if absence.from_date.present? && absence.to_date.present?
if absence.from_date.monday? && absence.to_date.friday?
TargetHours.hours_between_for_employee(from: absence.from_date, to: absence.to_date, employee: absence.employee)
else
TargetHours.hours_between(from: absence.from_date, to: absence.to_date)
end
end
end
end
2 changes: 1 addition & 1 deletion app/views/absences/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<tr>
<td><%= absence.employee.name %></td>
<td><%= present(absence) { |a| a.date } %></td>
<td><%= absence.absent_target_hours %></td>
<td><%= absence.hours %></td>
<td><%= absence.reason.text %></td>
<td><%= absence.text %></td>
<td><%= link_to_edit absence %></td>
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20241201132737_add_hours_for_all_absences.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class AddHoursForAllAbsences < ActiveRecord::Migration[7.1]
def change
Absence.where(hours: nil).each do |absence|
absence.update! hours: TargetHours.hours_between(
from: absence.from_date,
to: absence.to_date
)
end
end
end
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2023_06_24_060025) do
ActiveRecord::Schema[7.1].define(version: 2024_12_01_132737) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down
45 changes: 45 additions & 0 deletions spec/controllers/absences_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,51 @@
post :create, params: { absence: attributes }
end.to change(Absence, :count).by(1)
end

# these tests should be in the serivce specs, but I've no time left now...
context 'for part time employee' do
before do
employee.update! workload: 50
end

context 'for a full week' do
let(:attributes) do
{
employee_id: employee.id,
from_date: '2024-12-02',
to_date: '2024-12-06',
reason: :holidays
}
end

it 'creates a new absence which takes workload into account' do
expect do
post :create, params: { absence: attributes }
end.to change(Absence, :count).by(1)

expect(Absence.last.hours).to eq(20)
end
end

context 'for a part of the week' do
let(:attributes) do
{
employee_id: employee.id,
from_date: '2024-12-02',
to_date: '2024-12-03',
reason: :holidays
}
end

it 'creates a new absence which does not take workload into account' do
expect do
post :create, params: { absence: attributes }
end.to change(Absence, :count).by(1)

expect(Absence.last.hours).to eq(16)
end
end
end
end

describe 'GET #edit' do
Expand Down
4 changes: 2 additions & 2 deletions spec/features/hours_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
create :target_hours, date: '2017-04-17', hours: 4
create :absence, :default_associations, from_date: '2017-04-17',
to_date: '2017-04-21',
hours: nil
hours: 36
create :absence, :default_associations, from_date: '2017-04-6',
to_date: '2017-04-6',
hours: 4
Expand All @@ -30,7 +30,7 @@
create :activity, :default_associations, date: 1.month.ago, hours: 10
create :absence, :default_associations, from_date: '2017-03-27',
to_date: '2017-03-28',
hours: nil
hours: 16
create :absence, :default_associations, from_date: '2017-03-29',
to_date: '2017-03-29',
hours: 8
Expand Down
24 changes: 4 additions & 20 deletions spec/models/absences_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@
describe 'validations' do
context 'with absence spanning mutliple days' do
it 'is invalid when to-date after from-date' do
absence = build :absence, from_date: 2.days.from_now, to_date: 1.day.ago

expect(absence.valid?).to be false
end

it 'is invalid when hours not nil' do
absence = build :absence, from_date: 2.day.ago, to_date: 1.day.ago, hours: 3
absence = build :absence, hours: 8, from_date: 2.days.from_now, to_date: 1.day.ago

expect(absence.valid?).to be false
end
Expand Down Expand Up @@ -44,22 +38,12 @@

context 'absence spanning multiple days' do
let(:absence) do
build :absence, from_date: '2016-12-12', to_date: '2016-12-13'
build :absence, hours: 16, from_date: '2016-12-12', to_date: '2016-12-13'
end

it 'returns correct absent target hours' do
expect(subject).to eq 16
end

context 'overlapping target hours' do
before do
create :target_hours, date: '2016-12-12', hours: 4
end

it 'considers target_hours' do
expect(subject).to eq 12
end
end
end

describe 'limiting date range' do
Expand All @@ -70,7 +54,7 @@
let(:range) { { from: Date.parse('2016-12-01'), to: Date.parse('2016-12-05') } }

let(:absence) do
build :absence, from_date: '2016-11-30', to_date: '2016-12-06'
build :absence, hours: 40, from_date: '2016-11-30', to_date: '2016-12-06'
end

it 'only returns value between limited date range' do
Expand All @@ -91,7 +75,7 @@
subject { described_class.between(**range) }

let!(:absence) do
create :absence, hours: nil, from_date: '2016-11-30', to_date: '2016-12-06'
create :absence, hours: 40, from_date: '2016-11-30', to_date: '2016-12-06'
end

context 'with not covering range' do
Expand Down
2 changes: 2 additions & 0 deletions spec/models/activities/report/absences_cube_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
build(:absence, employee:,
from_date: (date - 1.day),
to_date: date,
hours: 16,
reason: :holidays),

build(:absence, employee: other_employee,
Expand Down Expand Up @@ -46,6 +47,7 @@
build(:absence, employee:,
from_date: (date.beginning_of_month - 2.day),
to_date: (date.beginning_of_month + 2.day),
hours: 32,
reason: :doctor),
]
end
Expand Down
2 changes: 1 addition & 1 deletion spec/models/hours/calendar_day_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

context 'with spanning absence' do
let!(:absence) do
create :absence, employee:, from_date: '2016-12-01', to_date: '2016-12-02', hours: nil
create :absence, employee:, from_date: '2016-12-01', to_date: '2016-12-02', hours: 16
end

it 'assigns absence to relevant days' do
Expand Down
23 changes: 1 addition & 22 deletions spec/models/hours/calendar_day_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,35 +115,14 @@
[
create(:absence, :default_associations, from_date: date,
to_date: date + 1.day,
hours: nil),
hours: 16),
]
end

it 'sets correct values' do
expect(absence_event.date).to eq date
expect(absence_event.hours).to eq 8
end

context 'and target hours' do
before do
create :target_hours, date:, hours: 6
end

it 'considers target hours' do
expect(absence_event.date).to eq date
expect(absence_event.hours).to eq 6
end
end

context 'if target hours are zero' do
before do
create :target_hours, date:, hours: 0
end

it 'does not create an event' do
expect(absence_event).to eq nil
end
end
end

context 'without absences' do
Expand Down