Class: Shift

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

Overview

Schema Information

Table name: shifts

id          :integer          not null, primary key
day_of_week :integer
start_time  :time
end_time    :time
hours       :time
schedule_id :integer
created_at  :datetime
updated_at  :datetime
due_date    :date
absence     :boolean          default(FALSE)

Instance Method Summary collapse

Instance Method Details

#avail?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'app/models/shift.rb', line 157

def avail?
  self.published? && self.can_edit?
end

#can_edit?Boolean

Returns:

  • (Boolean)


147
148
149
150
151
152
153
154
155
# File 'app/models/shift.rb', line 147

def can_edit?
  edit_possible = true
  if self.swap && swap.approved?
    edit_possible = true
  elsif self.cancellation && self.cancellation.approved?
    edit_possible = true
  end
  edit_possible
end

#formatted_due_dateObject



71
72
73
# File 'app/models/shift.rb', line 71

def formatted_due_date
  "#{due_date.strftime('%m')}/#{due_date.strftime('%d')}/#{due_date.strftime('%Y')}"
end

#get_week_hours(user) ⇒ Object



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

def get_week_hours(user)
  if user and user != 0
    user = User.find(user)
    user.shifts.where("due_date >= ? AND due_date <= ?",self.due_date.beginning_of_week,self.due_date.end_of_week ).sum(:hours).to_i
  else
    self.schedule.shifts.where("due_date >= ? AND due_date <= ?",self.due_date.beginning_of_week,self.due_date.end_of_week ).sum(:hours).to_i
  end
end

#has_issue?Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/shift.rb', line 132

def has_issue?
  if swp = swap and swp.status == "pending" 
    return { "status"=> true, "type"=> "swap pending" }
  elsif unavail = unavailable and unavail.status == "pending" 
    return { "status"=> true, "type"=> "unavailable pending" }
  elsif cancel = cancellation and cancel.status == "pending" 
    return { "status"=> true, "type"=> "cancel pending" }
  elsif Timeoff.where('user_id = ? AND off_date = ? AND status = ?',self.schedule.user_id, self.due_date, "pending").count > 0
    return { "status"=> true, "type"=> "timeoff pending" }
  elsif late = lateness 
    return { "status"=> true, "type"=> "lateness" }
  end
  return { "status"=> false }
end

#if_swap_approved(user) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'app/models/shift.rb', line 61

def if_swap_approved(user)
  if user and user != 0
    swapped = Candidate.find_by_alert_type_id(self.id)
    return (get_week_hours(user) + swapped.swap.shift.hours.strftime('%H').to_i)
  else
    swapped = Candidate.find_by_alert_type_id(self.id)
    return (get_week_hours(0) + swapped.swap.shift.hours.strftime('%H').to_i)
  end
end

#is_cancelled?Boolean

Returns:

  • (Boolean)


105
106
107
108
# File 'app/models/shift.rb', line 105

def is_cancelled? 
  cancellation = Cancellation.where('shift_id = ? AND (status = ? OR status = ?)',self.id,"approved excused","approved unexcused")
  cancellation.size > 0
end

#is_swapped?Boolean

Returns:

  • (Boolean)


110
111
112
113
# File 'app/models/shift.rb', line 110

def is_swapped? 
  swap = Swap.where(:shift_id=>self.id,:status=>"approved")
  swap.size > 0
end

#is_timeoff?Boolean

Returns:

  • (Boolean)


95
96
97
98
# File 'app/models/shift.rb', line 95

def is_timeoff? 
  timeoff = Timeoff.where(:user_id => self.schedule.user_id, :off_date=>self.due_date,:status=>"approved")
  timeoff.size > 0
end

#job_nameObject



44
45
46
# File 'app/models/shift.rb', line 44

def job_name
  self.job.name
end

#published?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'app/models/shift.rb', line 91

def published?
  self.schedule.published?
end

#shift_detailObject



87
88
89
# File 'app/models/shift.rb', line 87

def shift_detail
  "AS #{self.job_name} from #{self.start_time.strftime('%H:%M')} to #{self.end_time.strftime('%H:%M')} on #{self.formatted_due_date}"
end

#statusObject



74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/shift.rb', line 74

def status
  state = 'normal'
  if swap && swap.pending?
    state = 'swap_pending'
  end

  if cancellation && cancellation.pending?
    state = 'cancellation_pending'
  end

  state
end

#swap_shift(dest_sch, org, date) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/models/shift.rb', line 115

def swap_shift dest_sch, org, date
  curr = self.schedule
  dates = Date.parse(dest_sch[:due_date])
  if dest_sch[:schedule_id].present?
    new_schedule = Schedule.find_by_id(dest_sch[:schedule_id])
  else
    new_schedule = Schedule.new(user_id: dest_sch[:employee_id], organization_id: org.id,
         total_hours: '0:0', start_date: curr.start_date, end_date: curr.end_date)
    unless new_schedule.save!
      return new_schedule
    end
  end
  self.update_attributes!(schedule_id: new_schedule.id, day_of_week: dest_sch[:day_of_week], due_date: dates)
  curr.destroy! if curr.shifts.count == 0
  return self
end

#timeoff_reasonObject



100
101
102
103
# File 'app/models/shift.rb', line 100

def timeoff_reason
  timeoff = self.schedule.user.timeoffs.where(:off_date => self.due_date,:status=>"approved").first
  "The Reason : #{timeoff.reason}, All day : #{timeoff.all_day}, Vacation : #{timeoff.vacation} " if timeoff
end

#user_nameObject



48
49
50
# File 'app/models/shift.rb', line 48

def user_name
  self.schedule.user.name
end