set valid delivery date (#14890)

* set valid delivery date

* calculate delivery date based on diffrence in transation date and delivery date of ref doc
diff --git a/erpnext/selling/doctype/sales_order/sales_order.py b/erpnext/selling/doctype/sales_order/sales_order.py
index ee6b1c7..8d47416 100755
--- a/erpnext/selling/doctype/sales_order/sales_order.py
+++ b/erpnext/selling/doctype/sales_order/sales_order.py
@@ -377,16 +377,26 @@
 		return items
 
 	def on_recurring(self, reference_doc, auto_repeat_doc):
-		self.set("delivery_date", get_next_schedule_date(reference_doc.delivery_date,
-														 auto_repeat_doc.frequency, cint(auto_repeat_doc.repeat_on_day)))
+
+		def _get_delivery_date(ref_doc_delivery_date, red_doc_transaction_date, transaction_date):
+			delivery_date = get_next_schedule_date(ref_doc_delivery_date,
+				auto_repeat_doc.frequency, cint(auto_repeat_doc.repeat_on_day))
+
+			if delivery_date <= transaction_date:
+				delivery_date_diff = frappe.utils.date_diff(ref_doc_delivery_date, red_doc_transaction_date)
+				delivery_date = frappe.utils.add_days(transaction_date, delivery_date_diff)
+
+			return delivery_date
+
+		self.set("delivery_date", _get_delivery_date(reference_doc.delivery_date,
+			reference_doc.transaction_date, self.transaction_date ))
 
 		for d in self.get("items"):
 			reference_delivery_date = frappe.db.get_value("Sales Order Item",
 				{"parent": reference_doc.name, "item_code": d.item_code, "idx": d.idx}, "delivery_date")
 
-			d.set("delivery_date", get_next_schedule_date(reference_delivery_date,
-														  auto_repeat_doc.frequency, cint(auto_repeat_doc.repeat_on_day)))
-
+			d.set("delivery_date", _get_delivery_date(reference_delivery_date,
+				reference_doc.transaction_date, self.transaction_date))
 
 def get_list_context(context=None):
 	from erpnext.controllers.website_list_for_contact import get_list_context