From 67c26325eecb63a81f2839df1287ce8358340d1f Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Sat, 4 Jun 2022 14:46:35 +0530 Subject: [PATCH 1/2] fix: unnecessary GLE reposts In Sales/Purchase invoices credit/debit are flipped and negated while making GLE, this is unflipped while posting them but if we compare the flipped ones it will always result in comparison failure and repost it. --- erpnext/accounts/utils.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index 1869cc7b29..df5e37d83d 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -1124,6 +1124,9 @@ def update_gl_entries_after( 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 @@ -1144,8 +1147,10 @@ def repost_gle_for_stock_vouchers( gle = get_voucherwise_gl_entries(stock_vouchers, posting_date) for voucher_type, voucher_no in 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 From eb53a9727d2e465f74597d987f3f82b9f0530d7c Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Sat, 4 Jun 2022 18:19:44 +0530 Subject: [PATCH 2/2] perf: commit GL reposting periodically If you have a huge list of docs to repost then maintaining transaction throughtout entire GL reposting is not only unnecessary but also creates performance issues. Periodically commiting the changes prevents lost progress and reduces memory usage. --- erpnext/accounts/utils.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index df5e37d83d..8711395d55 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -1145,7 +1145,7 @@ def repost_gle_for_stock_vouchers( 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_doc(voucher_type, voucher_no) # Some transactions post credit as negative debit, this is handled while posting GLE @@ -1160,6 +1160,11 @@ def repost_gle_for_stock_vouchers( 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]]