Merge pull request #31240 from ankush/repost_match_gle

fix: unnecessary GLE reposts
diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py
index 1869cc7..8711395 100644
--- a/erpnext/accounts/utils.py
+++ b/erpnext/accounts/utils.py
@@ -1124,6 +1124,9 @@
 def repost_gle_for_stock_vouchers(
 	stock_vouchers, posting_date, company=None, warehouse_account=None
 ):
+
+	from erpnext.accounts.general_ledger import toggle_debit_credit_if_negative
+
 	if not stock_vouchers:
 		return
 
@@ -1142,10 +1145,12 @@
 	precision = get_field_precision(frappe.get_meta("GL Entry").get_field("debit")) or 2
 
 	gle = get_voucherwise_gl_entries(stock_vouchers, posting_date)
-	for voucher_type, voucher_no in stock_vouchers:
+	for idx, (voucher_type, voucher_no) in enumerate(stock_vouchers):
 		existing_gle = gle.get((voucher_type, voucher_no), [])
-		voucher_obj = frappe.get_cached_doc(voucher_type, voucher_no)
-		expected_gle = voucher_obj.get_gl_entries(warehouse_account)
+		voucher_obj = frappe.get_doc(voucher_type, voucher_no)
+		# Some transactions post credit as negative debit, this is handled while posting GLE
+		# but while comparing we need to make sure it's flipped so comparisons are accurate
+		expected_gle = toggle_debit_credit_if_negative(voucher_obj.get_gl_entries(warehouse_account))
 		if expected_gle:
 			if not existing_gle or not compare_existing_and_expected_gle(
 				existing_gle, expected_gle, precision
@@ -1155,6 +1160,11 @@
 		else:
 			_delete_gl_entries(voucher_type, voucher_no)
 
+		if idx % 20 == 0:
+			# Commit every 20 documents to avoid losing progress
+			# and reducing memory usage
+			frappe.db.commit()
+
 
 def sort_stock_vouchers_by_posting_date(
 	stock_vouchers: List[Tuple[str, str]]