property_hire/app/models/p_hire.rb

148 lines
5.4 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"]
2021-09-07 05:07:09 +00:00
field :date, type: Date
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
2021-09-06 16:45:14 +00:00
field :property_day_setting_id
2017-01-20 09:02:50 +00:00
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
2021-09-06 16:45:14 +00:00
has_many :p_hire_field_values, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :p_hire_field_values, allow_destroy: true
def property_day_setting
PropertyDaySetting.find(self.property_day_setting_id) rescue nil
end
2017-01-20 09:02:50 +00:00
def as_json(options = {})
2021-09-07 05:07:09 +00:00
startt = self.start_time
endt = self.end_time
recurring = false
datet = self.date
if options[:startt]
startt = options[:startt]
end
if options[:endt]
endt = options[:endt]
end
if options[:datet]
datet = options[:datet]
end
if options[:recurring]
recurring = options[:recurring]
end
2017-01-20 09:02:50 +00:00
{
: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 || "",
2021-09-07 05:07:09 +00:00
:start => startt.to_json.gsub('"',''),
:end => endt.to_json.gsub('"',''),
:allDay => (self.end_time - self.start_time >= 1),
:color => (self.passed ? "#3788d8" : "#FC4040"),
2021-09-06 16:45:14 +00:00
:error_message => (self.passed ? nil : "Not approved"),
2021-09-07 05:07:09 +00:00
:s_id=>self.property_day_setting_id.to_s,
:date=>datet,
:recurring=>recurring
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,date_only=false)
2021-09-07 05:07:09 +00:00
events = 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)
if date_only
2021-09-07 05:07:09 +00:00
events = events.where(:date.ne=>nil)
end
events
2018-01-24 08:29:28 +00:00
end
def self.recurring_event(start_date,end_date,property_id,date_only=false)
2020-09-19 15:11:26 +00:00
@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)
if date_only
2021-09-07 05:07:09 +00:00
@recurring_events = @recurring_events.where(:date.ne=>nil)
end
2020-09-19 15:11:26 +00:00
@recurring_events.each do |re|
2021-09-07 05:07:09 +00:00
datet = re.date
interval = 1.send(re.recurring_interval) rescue 0
if interval != 0
@start_date = re.start_time
recurring_end_date = re.recurring_end_date
new_end_date = [recurring_end_date,end_date].min
2021-09-07 05:07:09 +00:00
@end_date = re.end_time
while @start_date <= new_end_date do
2021-09-07 05:07:09 +00:00
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?
2020-09-19 15:11:26 +00:00
end
if @start_date >= start_date
@recurring << re.as_json({:startt=>@start_date,:endt=>@end_date,:datet=>datet})
end
2021-09-07 05:07:09 +00:00
@start_date += interval
@end_date += interval
datet += interval if datet
end
2020-09-19 15:11:26 +00:00
end
end
2020-09-19 15:11:26 +00:00
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