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,15 +63,17 @@ 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?
if !available
common_dates = (startt..endt) & (stime..etime) common_dates = (startt..endt) & (stime..etime)
return true if common_dates.nil? return true if common_dates.nil?
time_weekdays = [] time_weekdays = []
@ -84,15 +86,40 @@ class Property
startt = DateTime.parse(stime.strftime("%Y-%m-%d " + self.start_time + Time.zone.to_s)) 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)) endt = DateTime.parse(etime.strftime("%Y-%m-%d " + self.end_time + Time.zone.to_s))
common_dates = (startt..endt) & (stime..etime) common_dates = (startt..endt) & (stime..etime)
if common_dates.nil? available = common_dates.nil?
return true end
if available
if !recurring_end_date.blank?
case interval
when 'week'
d_step = 1.week
when 'month'
d_step = 1.month
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