Fix interval hire bug.

This commit is contained in:
BoHung Chiu 2020-09-19 00:32:56 +08:00
parent 9e670ae11f
commit ce63816765
2 changed files with 46 additions and 19 deletions

View File

@ -20,8 +20,8 @@ module Admin::PropertyHiresHelper
end end
data = {} data = {}
return {"success" => false, "msg" => "Starting time cannot be greater than ending time."} if stime > etime return {"success" => false, "msg" => "Starting time cannot be greater than ending time."} if stime > etime
if property.is_available_for_hire?(stime, etime) if property.is_available_for_hire?(stime, etime, interval, recurring_end_date)
if property.is_already_hired?(stime, etime, interval, recurring_end_date) if property.not_yet_hired?(stime, etime, interval, recurring_end_date)
data = {"success" => true} data = {"success" => true}
else else
data = {"success" => false, "msg" => "Property is already hired during this time."} data = {"success" => false, "msg" => "Property is already hired during this time."}

View File

@ -63,36 +63,63 @@ class Property
MemberProfile.find(self.owners) rescue [] MemberProfile.find(self.owners) rescue []
end end
def is_available_for_hire?(stime, etime) def is_available_for_hire?(stime, etime, interval = nil, recurring_end_date = nil)
available = false
return true if self.set_unavailibility == false return true if self.set_unavailibility == false
return true if self.weekdays.empty? return true if self.weekdays.empty?
return true if !self.start_date.nil? && (self.start_date > stime && self.start_date > etime) available = true if !self.start_date.nil? && (self.start_date > stime && self.start_date > etime)
return true if !self.end_date.nil? && self.end_date < stime available = true if !self.end_date.nil? && self.end_date < stime
startt = self.start_date.nil? ? self.created_at : self.start_date startt = self.start_date.nil? ? self.created_at : self.start_date
endt = self.end_date.nil? && !startt.nil? ? (startt + 5.years) : self.end_date endt = self.end_date.nil? && !startt.nil? ? (startt + 5.years) : self.end_date
weekdays = self.weekdays.collect{|w| w.to_i} weekdays = self.weekdays.collect{|w| w.to_i}
if !startt.nil? if !startt.nil?
common_dates = (startt..endt) & (stime..etime) if !available
return true if common_dates.nil? common_dates = (startt..endt) & (stime..etime)
time_weekdays = [] return true if common_dates.nil?
Property.time_iterate(common_dates.min, common_dates.max, 1.day) do |t| time_weekdays = []
time_weekdays << t.wday Property.time_iterate(common_dates.min, common_dates.max, 1.day) do |t|
time_weekdays << t.wday
end
time_weekdays.uniq!
weekdays = weekdays & time_weekdays
return true if weekdays.blank?
startt = DateTime.parse(stime.strftime("%Y-%m-%d " + self.start_time + Time.zone.to_s))
endt = DateTime.parse(etime.strftime("%Y-%m-%d " + self.end_time + Time.zone.to_s))
common_dates = (startt..endt) & (stime..etime)
available = common_dates.nil?
end end
time_weekdays.uniq! if available
weekdays = weekdays & time_weekdays if !recurring_end_date.blank?
return true if weekdays.blank? case interval
startt = DateTime.parse(stime.strftime("%Y-%m-%d " + self.start_time + Time.zone.to_s)) when 'week'
endt = DateTime.parse(etime.strftime("%Y-%m-%d " + self.end_time + Time.zone.to_s)) d_step = 1.week
common_dates = (startt..endt) & (stime..etime) when 'month'
if common_dates.nil? d_step = 1.month
return true else
d_step = 0
end
if d_step != 0
if etime >= stime
(etime.to_i..recurring_end_date.to_i).step(d_step).to_a.each_with_index do|time_integer,index|
date_time = Time.at(time_integer).to_datetime
new_etime = date_time
new_stime = stime + (new_etime - etime)
available = self.is_available_for_hire?(new_stime, new_etime, nil, nil)
break if !available
end
else
available = false
end
end
end
return available
else else
return false return false
end end
end end
end end
def is_already_hired?(stime, etime, interval, recurring_end_date) def not_yet_hired?(stime, etime, interval, recurring_end_date)
phires = self.p_hires phires = self.p_hires
bookings_count = phires.where(:start_time.lte => stime,:end_time.gte => stime,:recurring => false).count bookings_count = phires.where(:start_time.lte => stime,:end_time.gte => stime,:recurring => false).count
+ phires.where(:start_time.gte => stime,:end_time.lte => etime,:recurring => false).count + phires.where(:start_time.gte => stime,:end_time.lte => etime,:recurring => false).count