From 875c3dc580ff65c828649db5ffee73165280c243 Mon Sep 17 00:00:00 2001 From: bohung Date: Sat, 19 Sep 2020 23:11:26 +0800 Subject: [PATCH] Fix recurring hire bug. --- app/models/p_hire.rb | 83 ++++++++++++------- .../property_hires/view_calendar.html.erb | 2 +- 2 files changed, 53 insertions(+), 32 deletions(-) diff --git a/app/models/p_hire.rb b/app/models/p_hire.rb index 81cc13a..aa6291e 100644 --- a/app/models/p_hire.rb +++ b/app/models/p_hire.rb @@ -57,40 +57,61 @@ class PHire 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) end - def self.recurring_event(start_date,end_date,property_id) - @recurring_events = self.where(:property_id => property_id, :recurring_end_date.gte => start_date) + def self.recurring_event(start_date,end_date,property_id) + @property = Property.find(property_id) rescue nil @recurring = [] - @recurring_events.each do |re| - case re.recurring_interval - when "week" - @start_date = re.start_time - @end_date = re.end_time - @i = TimeDifference.between(re.start_time,end_date).in_weeks.to_i - (0..@i).each do |i| - if i > 0 - @start_date += 7 - @end_date += 7 + 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 + unavailable_start_date = DateTime.parse(unavailable_start_date.strftime("%Y-%m-%d " + unavailable_start_time.to_s + unavailable_start_date.zone)) + unavailable_end_date = DateTime.parse(unavailable_end_date.strftime("%Y-%m-%d " + unavailable_end_time.to_s + unavailable_end_date.zone)) + unavailable_weekdays = @property.weekdays.collect{|w| w.to_i} + @recurring_events = self.where(:property_id => property_id, :recurring_end_date.gte => start_date) + @recurring_events.each do |re| + case re.recurring_interval + when "week" + @start_date = re.start_time + @end_date = re.end_time + @i = TimeDifference.between(re.start_time,end_date).in_weeks.to_i + (0..@i).each do |i| + if i > 0 + @start_date += 7 + @end_date += 7 + end + 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? + end + if @start_date < re.recurring_end_date + @recurring << {:id => re.id.to_s, :hiring_person_name => re.hirer_name ,:title=>re.reason_for_hire, :note=>re.reason_for_hire, :start=>@start_date, :end => @end_date, :allDay => false, :recurring => re.recurring, :color => "#FC4040"} + end end - if @start_date < re.recurring_end_date - @recurring << {:id => re.id.to_s, :hiring_person_name => re.hirer_name ,:title=>re.reason_for_hire, :note=>re.reason_for_hire, :start=>@start_date, :end => @end_date, :allDay => false, :recurring => re.recurring, :color => "#FC4040"} + when "month" + # if !(start_date..end_date).cover?(re.start_time) + sd = re.start_time + ed = re.end_time + @i = TimeDifference.between(re.start_time,end_date).in_months.to_i + @start_date = sd + # debugger + sd = sd + @i.month + ed = ed + @i.month + if unavailable && !(unavailable_start_date > ed) && !(unavailable_end_date < sd) && !((sd.strftime("%w") .. ed.strftime("%w")).to_a & unavailable_weekdays).empty? + startt = DateTime.parse(sd.strftime("%Y-%m-%d " + unavailable_start_time + Time.zone.to_s)) + endt = DateTime.parse(ed.strftime("%Y-%m-%d " + unavailable_end_time + Time.zone.to_s)) + next if !((startt..endt) & (sd..ed)).blank? end - end - when "month" - # if !(start_date..end_date).cover?(re.start_time) - sd = re.start_time - ed = re.end_time - @i = TimeDifference.between(re.start_time,end_date).in_months.to_i - @start_date = sd - # debugger - sd = sd >> @i - ed = ed >> @i - if sd < re.recurring_end_date - @recurring << {:id => re.id.to_s, :title=>re.reason_for_hire, :note=>re.reason_for_hire, :start=>sd, :end => ed, :allDay => false, :recurring => re.recurring, :color => "#FC4040"} - end - # end - end - end - @recurring + if sd < re.recurring_end_date + @recurring << {:id => re.id.to_s, :title=>re.reason_for_hire, :note=>re.reason_for_hire, :start=>sd, :end => ed, :allDay => false, :recurring => re.recurring, :color => "#FC4040"} + end + # end + end + end + end + @recurring end def time_iterate(&block) diff --git a/app/views/property_hires/view_calendar.html.erb b/app/views/property_hires/view_calendar.html.erb index d634632..6f15d3c 100644 --- a/app/views/property_hires/view_calendar.html.erb +++ b/app/views/property_hires/view_calendar.html.erb @@ -1,5 +1,5 @@ <% OrbitHelper.render_css_in_head(["property_hire_fullcalendar.css","property_hire_calendar.scss"]) %> - + <%