property_hire/app/models/p_hire.rb

103 lines
3.3 KiB
Ruby
Raw Normal View History

2017-01-20 09:02:50 +00:00
class PHire
include Mongoid::Document
include Mongoid::Timestamps
2018-01-24 08:29:28 +00:00
INTERVALS = ["week", "month"]
2017-01-20 09:02:50 +00:00
field :start_time, type: DateTime
field :end_time, type: DateTime
field :hiring_person_email
field :hiring_person_number
field :hiring_person_id
field :hiring_person_name
field :reason_for_hire
field :note_for_hire
field :passed, type: Boolean, default: false
2018-01-24 08:29:28 +00:00
field :recurring, type: Boolean, :default => false
field :recurring_end_date, type: DateTime
field :recurring_interval
2017-01-20 09:02:50 +00:00
belongs_to :property
def as_json(options = {})
{
:id => self.id.to_s,
:title => self.reason_for_hire,
2019-07-01 04:12:11 +00:00
:hiring_person_name => self.hiring_person_name,
2017-01-20 09:02:50 +00:00
:note => self.note_for_hire || "",
:start => self.start_time.rfc822,
:end => self.end_time.rfc822,
:allDay => false,
:color => "#FC4040"
}
end
def period
return self.start_time.strftime("%y-%m-%d %H:%M") + " ~ " + self.end_time.strftime("%y-%m-%d %H:%M")
end
def hirer_name
return self.hiring_person_name.nil? ? self.hiring_person_profile.name : self.hiring_person_name
end
def hiring_person_profile
return MemberProfile.find(self.hiring_person_id) rescue nil
end
def self.monthly_event(start_date,end_date,property_id)
2018-01-24 08:29:28 +00:00
self.where(:property_id => property_id, :recurring => false).any_of(:start_time.gte => start_date, :end_time.gte => start_date).and(:start_time.lte => end_date).asc(:start_time)
end
def self.recurring_event(start_date,end_date,property_id)
@recurring_events = self.where(:property_id => property_id, :recurring => true, :recurring_end_date.gte => start_date)
2018-01-24 08:29:28 +00:00
@recurring = []
@recurring_events.each do |re|
case re.recurring_interval
when "week"
@start_date = re.start_time
@end_date = re.end_time
@i = TimeDifference.between(re.start_time,end_date).in_weeks.to_i
(0..@i).each do |i|
if i > 0
@start_date += 7
@end_date += 7
end
if @start_date < re.recurring_end_date
@recurring << {:id => re.id.to_s, :title=>re.reason_for_hire, :note=>re.reason_for_hire, :start=>@start_date, :end => @end_date, :allDay => false, :recurring => re.recurring, :color => "#FC4040"}
end
end
when "month"
# if !(start_date..end_date).cover?(re.start_time)
sd = re.start_time
ed = re.end_time
@i = TimeDifference.between(re.start_time,end_date).in_months.to_i
@start_date = sd
# debugger
sd = sd >> @i
ed = ed >> @i
if sd < re.recurring_end_date
@recurring << {:id => re.id.to_s, :title=>re.reason_for_hire, :note=>re.reason_for_hire, :start=>sd, :end => ed, :allDay => false, :recurring => re.recurring, :color => "#FC4040"}
end
# end
end
end
@recurring
end
def time_iterate(&block)
start_time = self.start_time
end_time = self.end_time
recurring_end_date = self.recurring_end_date
case self.recurring_interval
when "week"
step_interval = 1.week
when "month"
step_interval = 1.month
end
begin
yield(start_time, end_time)
end_time += step_interval
end while (start_time += step_interval) <= recurring_end_date
2017-01-20 09:02:50 +00:00
end
end