From d95998c70bc392c1d5f06cc292caa7f765816a8c Mon Sep 17 00:00:00 2001
From: bohung <bohung@rulingcom.com>
Date: Thu, 24 Nov 2022 23:15:17 +0800
Subject: [PATCH] Fix bug.

---
 app/assets/javascripts/calendar.js | 14 ++++++++++----
 app/models/event.rb                | 12 ++++++------
 2 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/app/assets/javascripts/calendar.js b/app/assets/javascripts/calendar.js
index 7c51dfa..289b92e 100644
--- a/app/assets/javascripts/calendar.js
+++ b/app/assets/javascripts/calendar.js
@@ -341,6 +341,11 @@ var Calendar = function(dom){
 		c.success_event = success_event;
 		var dview = (c.currentView == "agenda" ? "dayGridMonth" : c.currentView);
 		c.calendar_dom.css("overflow","visible");
+		// try{
+		// 	c.source_url = window.location.href.replace(/.(?=\?|$)/, function(f){return f + '.json'});
+		// }catch(e){
+		// 	c.source_url = window.location.origin + window.location.pathname + '.json' + window.location.search;
+		// }
 		c.calendar_dom.fullCalendar({
 			themeSystem: 'bootstrap',
 			editable: false,
@@ -584,8 +589,8 @@ var Calendar = function(dom){
 	}
 	this.get_single_event = function(id, callback){
 		var activeRange = c.calendar_dom.calendar.currentData.dateProfile.activeRange;
-		var start = activeRange.start,
-			end = activeRange.end;
+		var start = correct_date(activeRange.start),
+			end = correct_date(activeRange.end);
 		$.get("/admin/calendars/get_single_event", {
 			start: Math.round(start.getTime() / 1000),
 			end: Math.round(end.getTime() / 1000),
@@ -597,13 +602,14 @@ var Calendar = function(dom){
 		})
 	}
 	this.renderEvent = function(eventStick){
+		var event_source = this.calendar_dom.calendar.getEventSources()[0];
 		if(eventStick.recurring === true){
 			this.get_single_event(eventStick.id, function(eventData){
 				eventData = c.eventDataTransform(eventData);
-				c.calendar_dom.calendar.addEvent(eventData);
+				c.calendar_dom.calendar.addEvent(eventData, event_source);
 			});
 		}else{
-			c.calendar_dom.calendar.addEvent(eventStick);
+			c.calendar_dom.calendar.addEvent(eventStick, event_source);
 		}
 	};
 	this.updateEvent = function(eventStick){
diff --git a/app/models/event.rb b/app/models/event.rb
index 088b040..bdf7646 100644
--- a/app/models/event.rb
+++ b/app/models/event.rb
@@ -256,19 +256,18 @@ class Event
         tmp_hide_start =  re.hide_start.clone
         if is_month
           add_interval = ((start_date.year * 12 + start_date.month) - (@start_date.year * 12 + @start_date.month))
+          add_interval = 0 if add_interval < 0
         elsif is_year
           add_interval = (start_date.year - @start_date.year)
+          add_interval = 0 if add_interval < 0
         else
           add_interval = (start_date_utc_mjd - @start_date.new_offset(0).mjd)
           if add_interval < 0
-            add_interval = -(-add_interval / days)
+            add_interval = 0
           else
             add_interval = add_interval / days
           end
         end
-        if add_interval < 0
-          add_interval = add_interval % freq
-        end
         rest = add_interval % freq
         if rest != 0
           add_interval += (freq - rest)
@@ -316,13 +315,14 @@ class Event
                 next
               end
               if new_start != org_start
+                new_end = @end_date + w.day
                 hide = need_check_hide && tmp_hide_start2.include?(new_start.strftime("%Y-%m-%d"))
                 if hide
                   if preserve_hide
-                    @recurring << data.merge({:start => new_start.to_json.gsub('"',''), :end => (@end_date + w.day).to_json.gsub('"',''), :hide=>true, :color=>"#c0c0c0"})
+                    @recurring << data.merge({:start => new_start.to_json.gsub('"',''), :end => new_end.to_json.gsub('"',''), :hide=>true, :color=>"#c0c0c0"})
                   end
                 else
-                  @recurring << data.merge({:start => new_start.to_json.gsub('"',''), :end => (@end_date + w.day).to_json.gsub('"',''), :hide=>false, :color => data[:org_color]})
+                  @recurring << data.merge({:start => new_start.to_json.gsub('"',''), :end => new_end.to_json.gsub('"',''), :hide=>false, :color => data[:org_color]})
                 end
               end
             end