# == Schema Information # # Table name: schedules # # id :integer not null, primary key # publish :boolean # user_id :integer # job_id :integer # created_at :datetime # updated_at :datetime # start_date :date # end_date :date # class Schedule < ActiveRecord::Base belongs_to :user belongs_to :organization has_many :shifts, :dependent => :destroy has_many :cancellations, :through => :shifts has_many :swaps, :through => :shifts has_many :templates validates_presence_of :user validates_presence_of :organization validates_uniqueness_of :user_id, scope: [:start_date, :end_date, :template_id, :organization_id] validates_date :start_date validates_date :end_date, :after => :start_date validate :double_schedule def published? self.publish == true end def self.publish where(:publish => true) end def self.publish! schedule_ids, org_id schedules = where(:id => schedule_ids) schedules.each do |schedule| schedule.notify_employee(org_id) end return schedules.update_all(:publish => true) end def notify_employee org_id Notifier.delay.schedule_published(self, org_id) end def self.schedule_resend(schedules) where(:id => schedules).each do |schedule| schedule.notify_employee end end def get_sum total_hours.try(:round, 1) end def calculate_total_hours sum = 0.0 prop = self.user.member.find_by_organization_id(self.organization_id).in_train ? 'training_hours' : 'hours' shifts.each do |shift| shift[prop] = "2000-01-01 00:00:00 UTC" if shift[prop].nil? sum = sum + shift[prop].hour sum = sum + ( shift[prop].min + 0.00 ) / 60 end self.update_attributes(:total_hours => sum) end def check_timeoff_conflict start_time, end_time, due_date self.shifts.where(:due_date => due_date).each do |shift| if (start_time.strftime("%H:%M") >= shift.start_time.strftime("%H:%M") and start_time.strftime("%H:%M") < shift.end_time.strftime("%H:%M")) or (end_time.strftime("%H:%M") >= shift.start_time.strftime("%H:%M") and end_time.strftime("%H:%M") < shift.end_time.strftime("%H:%M")) return true end end return false end private def double_schedule # schedule = self.class.where(:start_date => self.start_date, :end_date => self.end_date,:template_id => nil).first # if self.id # # existing row # if schedule && schedule.user.id == self.user_id && schedule.id != self.id # errors.add(:start_date, "already scheduled from #{self.start_date} to #{self.end_date}") # end # else # # new row # if schedule && schedule.user.id == self.user_id # errors.add(:start_date, "already scheduled from #{self.start_date} to #{self.end_date}") # end # end end def self.create_with_json(data, date=nil, m_to_s, org_id) if date now_date = DateTime.parse(date) else now_date = Time.zone.now end wday = now_date.wday if m_to_s start_day = now_date - wday.day + 1.day end_day = now_date - wday.day + 7.day else start_day = now_date - wday.day + 0.day end_day = now_date - wday.day + 6.day end data.each do |schedule| if schedule["id"].nil? # create new one new_schedule = self.new(:user_id => schedule["employee_id"], :publish => schedule["publish"], :start_date => start_day, :end_date => end_day, :total_hours => schedule["total_hours"], :organization_id => org_id ) unless new_schedule.save return new_schedule.errors else if new_schedule.publish? new_schedule.notify_employee end end result = Shift.create_with_json(new_schedule, schedule["shifts"],false) unless result == true return result end else # modify existing one ext_schedule = self.find(schedule["id"]) if ext_schedule # ext_schedule.job_id = schedule["job_id"] ext_schedule.user_id = schedule["employee_id"] ext_schedule.publish = schedule["publish"] ext_schedule.start_date = start_day ext_schedule.end_date = end_day ext_schedule.total_hours = schedule["total_hours"] changed = ext_schedule.changed? result = ext_schedule.save unless result return ext_schedule.errors end if ext_schedule.publish? ext_schedule.notify_employee end end result = Shift.create_with_json(ext_schedule, schedule["shifts"],changed) if ext_schedule unless result == true return result end end end true end def self.current_week cur_day = Time.zone.now.strftime('%Y-%m-%d') where("start_date <= '#{cur_day}' and end_date >= '#{cur_day}'") end def self.week_schedule_by_date date where("start_date <= '#{date}' and end_date >= '#{date}'") end def self.not_template where(:template_id => nil) end def self.published_only where(:publish => true) end def self.schedule_by_date(start_date,end_date ) where("start_date >= '#{start_date}' and end_date <= '#{end_date}'") end def self.published where(:publish => true) end # def notify_employee # Notifier.delay.schedule_published(self) # end def self.editable users, org, date, role weeks_schedules = [] users = users.includes(:member) if role === 'employee' schedules = where(:organization_id => org.id, :user_id => users, :publish => true).not_template.week_schedule_by_date(date).group_by(&:user_id) else schedules = where(:organization_id => org.id, :user_id => users).not_template.week_schedule_by_date(date).group_by(&:user_id) end users.each_with_index do |user, i| sch = {} member = user.member.find_by_organization_id(org.id) schedule = schedules[user.id].try(:first) sch["employee_id"] = user.id sch["member_id"] = member.id sch["employee_name"] = user.name sch["role_name"] = member.roles.first.name sch["first_name"] = user.first_name sch["last_name"] = user.last_name sch["priority"] = member.priority sch["trainee"] = member.in_train sch["request_hours"] = member.request_weekly_hours || "0" sch["id"] = schedule.try(:id) sch["start_date"] = schedule.try(:start_date) || Shift.get_day(org.m_to_s, 0, date) sch["end_date"] = schedule.try(:end_date) || Shift.get_day(org.m_to_s, 6, date) sch["publish"] = schedule.try(:publish) sch["schdeule_sum"] = schedule.try(:get_sum) || "0" sch["organization_id"] = org.id sch["shifts"] = Shift.week_shifts schedule, user, org, date weeks_schedules << sch end weeks_schedules = weeks_schedules.sort_by {|sch| sch["priority"]} return weeks_schedules end end