Class: WorkDay

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/work_day.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_or_update(person, date) ⇒ Object



22
23
24
25
26
27
28
29
# File 'app/models/work_day.rb', line 22

def self.create_or_update(person, date)
  work_day = person.work_days.where(:date => date).first
  work_day ||= person.work_days.build(:date => date)

  work_day.update_hours

  work_day.save
end

.create_or_update_upto(person, end_date) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/work_day.rb', line 36

def self.create_or_update_upto(person, end_date)
  # Guard
  if latest_work_day = person.work_days.last
    start_date = latest_work_day.date
  else
    start_date = end_date
  end

  (start_date..end_date).each do |date|
    transaction do
      self.create_or_update(person, date)
    end
  end
end

Instance Method Details

#activitiesObject



6
7
8
# File 'app/models/work_day.rb', line 6

def activities
  person.activities.where(:date => date)
end

#calculate_hours_dueObject

Working hours for this day

Saturday and sunday are off, uses daily workload for all other days.



73
74
75
76
77
78
79
80
81
82
# File 'app/models/work_day.rb', line 73

def calculate_hours_due
  case date.wday
    when 6, 0
      # Saturday and sunday are off
      0.0
    else
      # Assume same working hours during the week
      daily_workload
  end
end

#calculate_hours_workedObject

Hours worked

Calculates hours worked by summing up duration of all logged activities.



88
89
90
# File 'app/models/work_day.rb', line 88

def calculate_hours_worked
  activities.where(:date => date).sum('duration')
end

#daily_workloadObject

Get daily workload

Lookup daily workload for person. Returns 0.0 if no employment is specified.



65
66
67
# File 'app/models/work_day.rb', line 65

def daily_workload
  employment.try(:daily_workload) || 0.0
end

#employmentObject

Get employment

Lookup the employment for this day.



54
55
56
57
58
59
# File 'app/models/work_day.rb', line 54

def employment
  # Guard as only some classes provide .employments
  return nil unless person.respond_to?(:employments)

  person.employments.current(self.date)
end

#overall_overtimeObject

Calculate accumulated overtime



93
94
95
96
97
98
99
# File 'app/models/work_day.rb', line 93

def overall_overtime
  if employment.nil? || employment.hourly_paid?
    person.work_days.where('date BETWEEN ? AND ?', date.beginning_of_month, date).sum('hours_worked - hours_due')
  else
    person.work_days.where('date <= ?', date).sum('hours_worked - hours_due')
  end
end

#overtimeObject



18
19
20
# File 'app/models/work_day.rb', line 18

def overtime
  hours_worked - hours_due
end

#to_sObject

String



14
15
16
# File 'app/models/work_day.rb', line 14

def to_s
  "%s %s: %s/%s" % [date, person, hours_worked, hours_due]
end

#update_hoursObject



31
32
33
34
# File 'app/models/work_day.rb', line 31

def update_hours
  self.hours_due        = calculate_hours_due
  self.hours_worked     = calculate_hours_worked
end