property_hire/app/models/p_hire.rb

143 lines
5.2 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
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
2021-09-07 05:07:09 +00:00
def self.monthly_event(start_date,end_date,property_id,date_ony=false)
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_ony
events = events.where(:date.ne=>nil)
end
events
2018-01-24 08:29:28 +00:00
end
2021-09-07 05:07:09 +00:00
def self.recurring_event(start_date,end_date,property_id,date_ony=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)
2021-09-07 05:07:09 +00:00
if date_ony
@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
end_date = [recurring_end_date,end_date].min
@end_date = re.end_time
while @start_date <= end_date do
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
2021-09-07 05:07:09 +00:00
@recurring << re.as_json({:startt=>@start_date,:endt=>@end_date,:datet=>datet})
@start_date += interval
@end_date += interval
datet += interval if datet
end
2020-09-19 15:11:26 +00:00
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