Merge pull request #27884 from nextchamp-saqib/invalid-gl-entry-patch-fix

fix: patch fails if accounts are frozen
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index c55fb0a..f15c65e 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -299,7 +299,7 @@
 erpnext.patches.v13_0.create_accounting_dimensions_in_pos_doctypes
 erpnext.patches.v13_0.trim_sales_invoice_custom_field_length
 erpnext.patches.v13_0.create_custom_field_for_finance_book
-erpnext.patches.v13_0.modify_invalid_gain_loss_gl_entries
+erpnext.patches.v13_0.modify_invalid_gain_loss_gl_entries #2
 erpnext.patches.v13_0.fix_additional_cost_in_mfg_stock_entry
 erpnext.patches.v13_0.set_status_in_maintenance_schedule_table
 erpnext.patches.v13_0.add_default_interview_notification_templates
diff --git a/erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py b/erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py
index fa8a864..3af7dac 100644
--- a/erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py
+++ b/erpnext/patches/v13_0/modify_invalid_gain_loss_gl_entries.py
@@ -17,7 +17,7 @@
 		where
 			ref_exchange_rate = 1
 			and docstatus = 1
-			and ifnull(exchange_gain_loss, '') != ''
+			and ifnull(exchange_gain_loss, 0) != 0
 		group by
 			parent
 	""", as_dict=1)
@@ -30,7 +30,7 @@
 		where
 			ref_exchange_rate = 1
 			and docstatus = 1
-			and ifnull(exchange_gain_loss, '') != ''
+			and ifnull(exchange_gain_loss, 0) != 0
 		group by
 			parent
 	""", as_dict=1)
@@ -38,12 +38,24 @@
 	if purchase_invoices + sales_invoices:
 		frappe.log_error(json.dumps(purchase_invoices + sales_invoices, indent=2), title="Patch Log")
 
+	acc_frozen_upto = frappe.db.get_value('Accounts Settings', None, 'acc_frozen_upto')
+	if acc_frozen_upto:
+		frappe.db.set_value('Accounts Settings', None, 'acc_frozen_upto', None)
+
 	for invoice in purchase_invoices + sales_invoices:
-		doc = frappe.get_doc(invoice.type, invoice.name)
-		doc.docstatus = 2
-		doc.make_gl_entries()
-		for advance in doc.advances:
-			if advance.ref_exchange_rate == 1:
-				advance.db_set('exchange_gain_loss', 0, False)
-		doc.docstatus = 1
-		doc.make_gl_entries()
\ No newline at end of file
+		try:
+			doc = frappe.get_doc(invoice.type, invoice.name)
+			doc.docstatus = 2
+			doc.make_gl_entries()
+			for advance in doc.advances:
+				if advance.ref_exchange_rate == 1:
+					advance.db_set('exchange_gain_loss', 0, False)
+			doc.docstatus = 1
+			doc.make_gl_entries()
+			frappe.db.commit()
+		except Exception:
+			frappe.db.rollback()
+			print(f'Failed to correct gl entries of {invoice.name}')
+
+	if acc_frozen_upto:
+		frappe.db.set_value('Accounts Settings', None, 'acc_frozen_upto', acc_frozen_upto)
\ No newline at end of file