ERPNext Conference 2019* ERPNext.com Blog

Autorepeat in Sales Invoice

customization
erpnext

#1

Hey guys,
I added two columns : Service Start Date and Service End Date in the table

def get_auto_repeat_schedule(self):
schedule_details = []
start_date = getdate(self.start_date)
end_date = getdate(self.end_date)
today = frappe.utils.datetime.date.today()

	if start_date < today:
		start_date = today

	if not self.end_date:
		start_date = get_next_schedule_date(start_date, self.frequency, self.repeat_on_day, self.repeat_on_last_day)
		row = {
			"reference_document": self.reference_document,
			"frequency": self.frequency,
			"next_scheduled_date": start_date,
			"service_start_date" : start_date + relativedelta(months=+1),
			"service_end_date" : start_date + relativedelta(day=31)
		}
		schedule_details.append(row)
		start_date = get_next_schedule_date(start_date, self.frequency, self.repeat_on_day, self.repeat_on_last_day)

	if self.end_date:
		start_date = get_next_schedule_date(start_date, self.frequency, self.repeat_on_day, self.repeat_on_last_day)
		while (getdate(start_date) < getdate(end_date)):
			row = {
				"reference_document" : self.reference_document,
				"frequency" : self.frequency,
				"next_scheduled_date" : start_date,
				"service_start_date" : start_date + relativedelta(months=+1),
				"service_end_date" : start_date + relativedelta(months=+2) + timedelta(days=-1)
			}
			schedule_details.append(row)
			start_date = get_next_schedule_date(start_date, self.frequency, self.repeat_on_day, self.repeat_on_last_day, end_date)


	return schedule_details

This is my code.
Problem with the code is, it works fine when frequency:“monthly” is selected but not with other frequency.

Thanks in advance.


#2

I found this solution which is working fine, but service_start_date and service_end_date is not stored in database…

if self.end_date:
start_date = get_next_schedule_date(start_date, self.frequency, self.repeat_on_day, self.repeat_on_last_day)
while (getdate(start_date) < getdate(end_date)):
if self.frequency == ‘Daily’:
row = {
“reference_document” : self.reference_document,
“frequency” : self.frequency,
“next_scheduled_date” : start_date,
“service_start_date” : start_date + relativedelta(months=+1),
“service_end_date” : start_date + relativedelta(months=+1)
}

			elif self.frequency == 'Weekly':
				row = {
					"reference_document" : self.reference_document,
					"frequency" : self.frequency,
					"next_scheduled_date" : start_date,
					"service_start_date" : start_date + relativedelta(months=+1),
					"service_end_date" : start_date + relativedelta(months=+1) + timedelta(days=+6)
				}

			elif self.frequency == 'Monthly':
				row = {
					"reference_document" : self.reference_document,
					"frequency" : self.frequency,
					"next_scheduled_date" : start_date,
					"service_start_date" : start_date + relativedelta(months=+1),
					"service_end_date" : start_date + relativedelta(months=+2) + timedelta(days=-1)
				}

			elif self.frequency == 'Quarterly':
				row = {
					"reference_document" : self.reference_document,
					"frequency" : self.frequency,
					"next_scheduled_date" : start_date,
					"service_start_date" : start_date + relativedelta(months=+1),
					"service_end_date" : start_date + relativedelta(months=+4) + timedelta(days=-1)
				}

			elif self.frequency == 'Half-yearly':
				row = {
					"reference_document" : self.reference_document,
					"frequency" : self.frequency,
					"next_scheduled_date" : start_date,
					"service_start_date" : start_date + relativedelta(months=+1),
					"service_end_date" : start_date + relativedelta(months=+7) + timedelta(days=-1)
				}

			elif self.frequency == 'Yearly':
				row = {
					"reference_document" : self.reference_document,
					"frequency" : self.frequency,
					"next_scheduled_date" : start_date,
					"service_start_date" : start_date + relativedelta(months=+1),
					"service_end_date" : start_date + relativedelta(months=+13) + timedelta(days=-1)
				}

			schedule_details.append(row)
			start_date = get_next_schedule_date(start_date, self.frequency, self.repeat_on_day, self.repeat_on_last_day, end_date)

#3