property_hire/app/models/p_hire.rb

193 lines
7.1 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 :tmp_reason_for_hire, type: String, default: "" # store reason text from custom fields
2017-01-20 09:02:50 +00:00
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
def time
property_day_setting.title 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.to_s + " "+ self.tmp_reason_for_hire.to_s).html_safe,
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 => false,
:diff_day => (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
2021-10-15 13:10:34 +00:00
return self.hiring_person_name.nil? ? (self.hiring_person_profile.name rescue "") : self.hiring_person_name
2017-01-20 09:02:50 +00:00
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)
2022-02-08 07:26:12 +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
2021-10-05 13:58:43 +00:00
def self.convert_datetime(time)
2021-10-05 13:56:45 +00:00
if time.class == Time
return time.to_datetime
elsif time.class == DateTime
return time
elsif time.class == String
return DateTime.parse(time)
else
return Time.at(time).to_datetime #time is seconds
end
end
def self.recurring_event(start_date,end_date,property_id,date_only=false)
2021-10-05 13:56:45 +00:00
start_date = convert_datetime(start_date)
end_date = convert_datetime(end_date)
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
2022-08-02 13:48:24 +00:00
start_date_utc_mjd = start_date.to_datetime.utc.mjd
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
2022-07-22 01:43:01 +00:00
org_start = re.start_time
2021-09-07 05:07:09 +00:00
@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
2021-10-05 02:15:04 +00:00
if @start_date < start_date
2021-10-05 13:56:45 +00:00
add_interval = nil
2022-07-22 01:43:01 +00:00
period_str = nil
2021-10-05 02:15:04 +00:00
if re.recurring_interval == "week"
2022-07-22 01:43:01 +00:00
period_str = 'week'
2022-08-02 13:48:24 +00:00
add_interval = (start_date_utc_mjd - @start_date.utc.mjd)
days = 7.day
if add_interval < 0
add_interval = -(-add_interval / days)
else
add_interval = add_interval / days
end
2021-10-05 02:15:04 +00:00
else
2022-07-22 01:43:01 +00:00
period_str = 'month'
2022-08-02 13:48:24 +00:00
add_interval = ((start_date.year * 12 + start_date.month) - (@start_date.year * 12 + @start_date.month))
2021-10-05 02:15:04 +00:00
end
2022-07-22 01:43:01 +00:00
add_interval = 0 if add_interval < 0
add_interval = add_interval.send(period_str)
2021-10-05 13:56:45 +00:00
@start_date += add_interval
@end_date += add_interval
2021-10-05 02:15:04 +00:00
end
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))
2021-10-05 14:26:22 +00:00
if !((startt..endt) & (@start_date..@end_date)).blank?
2021-12-09 03:48:22 +00:00
@start_date += interval
@end_date += interval
datet += interval if datet
2021-10-05 14:26:22 +00:00
next
end
2020-09-19 15:11:26 +00:00
end
2022-07-22 01:43:01 +00:00
if @start_date >= start_date && @start_date != org_start
@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