Fix recurring event.

This commit is contained in:
BoHung Chiu 2021-10-05 21:56:45 +08:00
parent c902128062
commit d78aab6408
1 changed files with 18 additions and 3 deletions

View File

@ -85,8 +85,20 @@ class PHire
end end
events events
end end
def convert_datetime(time)
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) def self.recurring_event(start_date,end_date,property_id,date_only=false)
start_date = convert_datetime(start_date)
end_date = convert_datetime(end_date)
@property = Property.find(property_id) rescue nil @property = Property.find(property_id) rescue nil
@recurring = [] @recurring = []
if @property != nil if @property != nil
@ -111,11 +123,14 @@ class PHire
new_end_date = [recurring_end_date,end_date].min new_end_date = [recurring_end_date,end_date].min
@end_date = re.end_time @end_date = re.end_time
if @start_date < start_date if @start_date < start_date
add_interval = nil
if re.recurring_interval == "week" if re.recurring_interval == "week"
@start_date += ((start_date - @start_date).to_i / 7) * interval add_interval = ((start_date - @start_date).to_i / 7).send("week")
else else
@start_date += ((start_date.year * 12 + start_date.month) - (@start_date.year * 12 + @start_date.month) - 1) * interval add_interval = ((start_date.year * 12 + start_date.month) - (@start_date.year * 12 + @start_date.month) - 1).send("month")
end end
@start_date += add_interval
@end_date += add_interval
end end
while @start_date <= new_end_date do while @start_date <= new_end_date do
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? 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?