property_hire/app/models/p_hire.rb

134 lines
5.7 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
field :organization
field :person_in_charge
field :tel_of_person_in_charge
field :department
field :contact_person
field :tel_of_contact_person
field :mobile_phone_of_contact_person
field :contact_person_Email
field :contact_person_department
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-11-08 04:24:42 +00:00
:hiring_person_id => self.hiring_person_id,
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.to_json.gsub('"',''),
:end => self.end_time.to_json.gsub('"',''),
:allDay => (self.end_time - self.start_time >= 1),
:color => (self.passed ? "#3788d8" : "#FC4040"),
:error_message => (self.passed ? nil : "Not approved")
2017-01-20 09:02:50 +00:00
}
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
2020-09-19 15:11:26 +00:00
def self.recurring_event(start_date,end_date,property_id)
@property = Property.find(property_id) rescue nil
2018-01-24 08:29:28 +00:00
@recurring = []
2020-09-19 15:11:26 +00:00
if @property != nil
unavailable = @property.set_unavailibility && !@property.weekdays.empty? && !@property.start_date.nil? && !@property.end_date.nil?
unavailable_start_date = @property.start_date
unavailable_end_date = @property.end_date
unavailable_start_time = @property.start_time.to_s
unavailable_end_time = @property.end_time.to_s
2020-09-25 13:43:13 +00:00
unavailable_start_date = DateTime.parse(unavailable_start_date.strftime("%Y-%m-%d " + unavailable_start_time.to_s + Time.zone.to_s)) rescue nil
unavailable_end_date = DateTime.parse(unavailable_end_date.strftime("%Y-%m-%d " + unavailable_end_time.to_s + Time.zone.to_s)) rescue nil
2020-09-19 15:11:26 +00:00
unavailable_weekdays = @property.weekdays.collect{|w| w.to_i}
@recurring_events = self.where(:property_id => property_id, :recurring_end_date.gte => start_date)
@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 unavailable && (unavailable_start_date <= @start_date) && (unavailable_end_date >= @end_date) && !((@start_date.strftime("%w").to_i .. @end_date.strftime("%w").to_i).to_a & unavailable_weekdays).empty?
startt = DateTime.parse(@start_date.strftime("%Y-%m-%d " + unavailable_start_time + Time.zone.to_s))
endt = DateTime.parse(@end_date.strftime("%Y-%m-%d " + unavailable_end_time + Time.zone.to_s))
next if !((startt..endt) & (@start_date..@end_date)).blank?
end
if @start_date < re.recurring_end_date
@recurring << {:id => re.id.to_s, :hiring_person_name => re.hirer_name ,:title=>re.reason_for_hire, :note=>re.reason_for_hire, :start=>@start_date, :end => @end_date, :allDay => (re.end_time - re.start_time >= 1), :recurring => re.recurring, :color => (re.passed ? "#3788d8" : "#FC4040"), :error_message => (re.passed ? nil : "Not approved")}
2020-09-19 15:11:26 +00:00
end
2018-01-24 08:29:28 +00:00
end
2020-09-19 15:11:26 +00:00
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.month
ed = ed + @i.month
if unavailable && !(unavailable_start_date > ed) && !(unavailable_end_date < sd) && !((sd.strftime("%w") .. ed.strftime("%w")).to_a & unavailable_weekdays).empty?
startt = DateTime.parse(sd.strftime("%Y-%m-%d " + unavailable_start_time + Time.zone.to_s))
endt = DateTime.parse(ed.strftime("%Y-%m-%d " + unavailable_end_time + Time.zone.to_s))
next if !((startt..endt) & (sd..ed)).blank?
2018-01-24 08:29:28 +00:00
end
2020-09-19 15:11:26 +00:00
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
end
@recurring
2018-01-24 08:29:28 +00:00
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