9
9
from frappe .utils .jinja import validate_template
10
10
from dateutil .relativedelta import relativedelta
11
11
from frappe .utils .user import get_system_managers
12
- from frappe .utils import cstr , getdate , split_emails , add_days , today , get_last_day , get_first_day
12
+ from frappe .utils import cstr , getdate , split_emails , add_days , today , get_last_day , get_first_day , month_diff
13
13
from frappe .model .document import Document
14
14
from frappe .core .doctype .communication .email import make
15
15
from frappe .utils .background_jobs import get_jobs
@@ -48,7 +48,7 @@ def set_dates(self):
48
48
if self .disabled :
49
49
self .next_schedule_date = None
50
50
else :
51
- self .next_schedule_date = get_next_schedule_date (self .start_date , self .frequency , self .repeat_on_day , self .repeat_on_last_day , self .end_date )
51
+ self .next_schedule_date = get_next_schedule_date (self .start_date , self .frequency , self .start_date , self . repeat_on_day , self .repeat_on_last_day , self .end_date )
52
52
53
53
def unlink_if_applicable (self ):
54
54
if self .status == 'Completed' or self .disabled :
@@ -107,27 +107,27 @@ def get_auto_repeat_schedule(self):
107
107
end_date = getdate (self .end_date )
108
108
109
109
if not self .end_date :
110
- start_date = get_next_schedule_date (start_date , self .frequency , self .repeat_on_day , self .repeat_on_last_day )
110
+ next_date = get_next_schedule_date (start_date , self .frequency , self . start_date , self .repeat_on_day , self .repeat_on_last_day )
111
111
row = {
112
112
"reference_document" : self .reference_document ,
113
113
"frequency" : self .frequency ,
114
- "next_scheduled_date" : start_date
114
+ "next_scheduled_date" : next_date
115
115
}
116
116
schedule_details .append (row )
117
- start_date = get_next_schedule_date (start_date , self .frequency , self .repeat_on_day , self .repeat_on_last_day )
118
117
119
118
if self .end_date :
120
- start_date = get_next_schedule_date (
121
- start_date , self .frequency , self .repeat_on_day , self .repeat_on_last_day , for_full_schedule = True )
122
- while (getdate (start_date ) < getdate (end_date )):
119
+ next_date = get_next_schedule_date (
120
+ start_date , self .frequency , self .start_date , self .repeat_on_day , self .repeat_on_last_day , for_full_schedule = True )
121
+
122
+ while (getdate (next_date ) < getdate (end_date )):
123
123
row = {
124
124
"reference_document" : self .reference_document ,
125
125
"frequency" : self .frequency ,
126
- "next_scheduled_date" : start_date
126
+ "next_scheduled_date" : next_date
127
127
}
128
128
schedule_details .append (row )
129
- start_date = get_next_schedule_date (
130
- start_date , self .frequency , self .repeat_on_day , self .repeat_on_last_day , end_date , for_full_schedule = True )
129
+ next_date = get_next_schedule_date (
130
+ next_date , self .frequency , self . start_date , self .repeat_on_day , self .repeat_on_last_day , end_date , for_full_schedule = True )
131
131
132
132
return schedule_details
133
133
@@ -268,8 +268,12 @@ def notify_error_to_user(self, error_log):
268
268
)
269
269
270
270
271
- def get_next_schedule_date (start_date , frequency , repeat_on_day , repeat_on_last_day = False , end_date = None , for_full_schedule = False ):
272
- month_count = month_map .get (frequency )
271
+ def get_next_schedule_date (schedule_date , frequency , start_date , repeat_on_day = None , repeat_on_last_day = False , end_date = None , for_full_schedule = False ):
272
+ if month_map .get (frequency ):
273
+ month_count = month_map .get (frequency ) + month_diff (schedule_date , start_date ) - 1
274
+ else :
275
+ month_count = 0
276
+
273
277
day_count = 0
274
278
if month_count and repeat_on_last_day :
275
279
next_date = get_next_date (start_date , month_count , 31 )
@@ -288,7 +292,9 @@ def get_next_schedule_date(start_date, frequency, repeat_on_day, repeat_on_last_
288
292
# next schedule date should be after or on current date
289
293
if not for_full_schedule :
290
294
while getdate (next_date ) < getdate (today ()):
291
- next_date = get_next_date (next_date , month_count , day_count )
295
+ if month_count :
296
+ month_count += month_map .get (frequency )
297
+ next_date = get_next_date (start_date , month_count , day_count )
292
298
293
299
return next_date
294
300
@@ -316,8 +322,7 @@ def create_repeated_entries(data):
316
322
317
323
if schedule_date == current_date and not doc .disabled :
318
324
doc .create_documents ()
319
- schedule_date = get_next_schedule_date (schedule_date , doc .frequency , doc .repeat_on_day , doc .repeat_on_last_day , doc .end_date )
320
-
325
+ schedule_date = get_next_schedule_date (schedule_date , doc .frequency , doc .start_date , doc .repeat_on_day , doc .repeat_on_last_day , doc .end_date )
321
326
if schedule_date and not doc .disabled :
322
327
frappe .db .set_value ('Auto Repeat' , doc .name , 'next_schedule_date' , schedule_date )
323
328
0 commit comments