fix: Serial No Rename does not affect  Stock Ledger Entry (#22746)

* Revert "fix: Remove rename related code from Serial No (#22627)"

This reverts commit 1ec2d962dbcc7ccee30e0d0a35727aa890910203.

* fix: Rename fails on Stock Ledger Entry

* fix: Allow Rename in Serial No
diff --git a/erpnext/stock/doctype/serial_no/serial_no.json b/erpnext/stock/doctype/serial_no/serial_no.json
index 2be14c8..3acf3a9 100644
--- a/erpnext/stock/doctype/serial_no/serial_no.json
+++ b/erpnext/stock/doctype/serial_no/serial_no.json
@@ -1,6 +1,7 @@
 {
  "actions": [],
  "allow_import": 1,
+ "allow_rename": 1,
  "autoname": "field:serial_no",
  "creation": "2013-05-16 10:59:15",
  "description": "Distinct unit of an Item",
@@ -426,7 +427,7 @@
  "icon": "fa fa-barcode",
  "idx": 1,
  "links": [],
- "modified": "2020-06-25 15:53:50.900855",
+ "modified": "2020-07-20 20:50:16.660433",
  "modified_by": "Administrator",
  "module": "Stock",
  "name": "Serial No",
diff --git a/erpnext/stock/doctype/serial_no/serial_no.py b/erpnext/stock/doctype/serial_no/serial_no.py
index 90f0f58..153ce2f 100644
--- a/erpnext/stock/doctype/serial_no/serial_no.py
+++ b/erpnext/stock/doctype/serial_no/serial_no.py
@@ -190,6 +190,23 @@
 		if sle_exists:
 			frappe.throw(_("Cannot delete Serial No {0}, as it is used in stock transactions").format(self.name))
 
+	def before_rename(self, old, new, merge=False):
+		if merge:
+			frappe.throw(_("Sorry, Serial Nos cannot be merged"))
+
+	def after_rename(self, old, new, merge=False):
+		"""rename serial_no text fields"""
+		for dt in frappe.db.sql("""select parent from tabDocField
+			where fieldname='serial_no' and fieldtype in ('Text', 'Small Text', 'Long Text')"""):
+
+			for item in frappe.db.sql("""select name, serial_no from `tab%s`
+				where serial_no like %s""" % (dt[0], frappe.db.escape('%' + old + '%'))):
+
+				serial_nos = map(lambda i: new if i.upper()==old.upper() else i, item[1].split('\n'))
+				frappe.db.sql("""update `tab%s` set serial_no = %s
+					where name=%s""" % (dt[0], '%s', '%s'),
+					('\n'.join(list(serial_nos)), item[0]))
+
 	def update_serial_no_reference(self, serial_no=None):
 		last_sle = self.get_last_sle(serial_no)
 		self.set_purchase_details(last_sle.get("purchase_sle"))