Repository: frappe/frappe
Pull request: feat: bulk rename allow merge option
This is more a limitation when using ERPNext Bulk Renaming tool.
This PR should close https://github.com/frappe/erpnext/issues/8790

 **Previous Behavior** 
If you attempted to merge a document using bulk rename tool you would get an error the target doc name already exists.

 **After PR** 
Uploader has option to add third column as a flag signaling if this record should be merged. If the value  is 1 or "true" it will be merged. If left blank or any other value the record will not be merged.

 **TODO** 

- [x] Update ERPNext Bulk Renaming Tool Docs.

Docs: https://docs.erpnext.com/docs/v13/user/manual/en/using-erpnext/articles/bulk-rename/edit?wiki_page_patch=d63cd055ad

<!-- no-docs -->

Problem statement:
Rename tool should have option to Merge with Existing
Hi,

The rename tool both in the bench and client side does not have an option to merge items.
Suppose if I want to rename 10 items to the same name then the only option left is to individually rename these items since rename tool cannot handle the situation where the name exists both in bench and on client side.

Hints from issue discussion:
Reference https://discuss.erpnext.com/t/is-there-a-bulk-item-merge-with-existing-functionality/20038/
+1
In frappe-bench/apps/frappe/frappe/model/rename_doc.py, **rename_doc** function call inside **bulk_rename** function needs to be changed.
Adding, `merge=row[2]` as a third parameter worked for me. And in the excel file which we upload, there needs to be 3 columns - old_name,new_name,merge(0/1)

> if rename_doc(doctype, row[0], row[1]**, merge=row[2]**):

`*If anyone finds that this is not the right path to follow, please let us know the best way.`
> In frappe-bench/apps/frappe/frappe/model/rename_doc.py, **rename_doc** function call inside **bulk_rename** function needs to be changed.
> Adding, `merge=row[2]` as a third parameter worked for me. And in the excel file which we upload, there needs to be 3 columns - old_name,new_name,merge(0/1)
> 
> > if rename_doc(doctype, row[0], row[1]**, merge=row[2]**):
> 
> `*If anyone finds that this is not the right path to follow, please let us know the best way.`

Thanks @vwithv1602 !

I have tried this in a test VM and it works.  I was wondering if you or anyone else has encountered any issues with this "path further down the line"?  It seems to be a simple fix and I am wondering why this functionality has not been incorporated as yet?
> > In frappe-bench/apps/frappe/frappe/model/rename_doc.py, **rename_doc** function call inside **bulk_rename** function needs to be changed.
> > Adding, `merge=row[2]` as a third parameter worked for me. And in the excel file which we upload, there needs to be 3 columns - old_name,new_name,merge(0/1)
> > > if rename_doc(doctype, row[0], row[1]**, merge=row[2]**):
> > 
> > 
> > `*If anyone finds that this is not the right path to follow, please let us know the best way.`
> 
> Thanks @vwithv1602 !
> 
> I have tried this in a test VM and it works. I was wondering if you or anyone else has encountered any issues with this "path further down the line"? It seems to be a simple fix and I am wondering why this functionality has not been incorporated as yet?

I need to use this workaround and would really appreciate comments or confirmation that this is safe to do so before I proceed.

Commit message: Merge branch 'frappe:develop' into feat-bulk-rename-merge

Patch to review:
diff --git a/frappe/model/rename_doc.py b/frappe/model/rename_doc.py
index 14f1dbf2b082..9377ad74f89f 100644
--- a/frappe/model/rename_doc.py
+++ b/frappe/model/rename_doc.py
@@ -458,7 +458,7 @@ def bulk_rename(doctype, rows=None, via_console = False):
 	"""Bulk rename documents
 
 	:param doctype: DocType to be renamed
-	:param rows: list of documents as `((oldname, newname), ..)`"""
+	:param rows: list of documents as `((oldname, newname, merge(optional)), ..)`"""
 	if not rows:
 		frappe.throw(_("Please select a valid csv file with data"))
 
@@ -471,8 +471,9 @@ def bulk_rename(doctype, rows=None, via_console = False):
 	for row in rows:
 		# if row has some content
 		if len(row) > 1 and row[0] and row[1]:
+			merge = True if len(row) > 2 and (row[2] == "1" or row[2].lower() == "true") else False
 			try:
-				if rename_doc(doctype, row[0], row[1]):
+				if rename_doc(doctype, row[0], row[1], merge=merge):
 					msg = _("Successful: {0} to {1}").format(row[0], row[1])
 					frappe.db.commit()
 				else:


Code review comments:
diff_hunk: @@ -471,8 +471,9 @@ def bulk_rename(doctype, rows=None, via_console = False):
 	for row in rows:
 		# if row has some content
 		if len(row) > 1 and row[0] and row[1]:
+			merge = True if len(row) > 2 and (row[2] == "1" or row[2].lower() == "true") else False
path: frappe/model/rename_doc.py
text: @user1:
```suggestion
			merge = len(row) > 2 and (row[2] == "1" or row[2].lower() == "true")
```