From ec3e4463ca4c57fa3f20a3b60f6ee760e5ac14db Mon Sep 17 00:00:00 2001 From: chiu Date: Tue, 19 May 2020 22:50:45 +0800 Subject: [PATCH] fix error --- app/models/property.rb | 56 +++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 33 deletions(-) diff --git a/app/models/property.rb b/app/models/property.rb index 6353bba..f2656ab 100644 --- a/app/models/property.rb +++ b/app/models/property.rb @@ -83,45 +83,35 @@ class Property end def is_already_hired?(stime, etime, interval, recurring_end_date) - bookings = self.p_hires.where(:end_time.gte => stime, :recurring => false) + bookings = self.p_hires.where(:start_time.lte => stime,:end_time.gte => stime,:recurring => false) + .or(:start_time.gte => stime,:end_time.lte => etime,:recurring => false) + .or(:start_time.lte => etime,:end_time.gte => etime,:recurring => false) available = true - bookings.each do |booking| - common_time = (booking.start_time..booking.end_time) & (stime..etime) - if !common_time.nil? - available = false - break - end + if bookings.count != 0 + available = false end if available - case interval - when "week" - stepu = 1.week - when "month" - stepu = 1.month - else - stepu = 0 - end bookings = self.p_hires.where(:recurring_end_date.gte => stime, :recurring => true) bookings.each do |booking| - booking.time_iterate do |st,et| - tst = stime - tet = etime - if stepu != 0 - begin - common_time = (tst..tet) & (st..et) - available = false if !common_time.nil? - tet += stepu - break if !available - break if tst > st - end while (tst += stepu) <= recurring_end_date - else - common_time = (tst..tet) & (st..et) - available = false if !common_time.nil? - break if !available - end - break if !available + interval = booking.recurring_interval + if interval == 'month' + sdata = stime.day*10000 + stime.hour*100 + stime.minute + edata = etime.day*10000 + etime.hour*100 + etime.minute + elsif interval == 'week' + sdata = stime.wday*10000 + stime.hour*100 + stime.minute + edata = etime.wday*10000 + etime.hour*100 + etime.minute end - break if !available + if interval == 'month' + b_sdata = booking.start_time.day*10000 + booking.start_time.hour*100 + booking.start_time.minute + b_edata = booking.end_time.day*10000 + booking.end_time.hour*100 + booking.end_time.minute + elsif interval == 'week' + b_sdata = booking.start_time.wday*10000 + booking.start_time.hour*100 + booking.start_time.minute + b_edata = booking.end_time.wday*10000 + booking.end_time.hour*100 + booking.end_time.minute + end + if (b_sdata <= sdata && b_edata >= sdata) || (b_sdata >= sdata && b_edata <= edata) || (b_sdata <= edata && b_edata >= edata) + available = false + end + break if available == false end end return available