From 1b0fc0541be45eddff8bfef357eeaedaef6e53a3 Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Thu, 25 Sep 2025 15:10:57 +0530 Subject: [PATCH] perf: reposting for backdated transactions --- erpnext/stock/stock_ledger.py | 39 ++++++++++++++--------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/erpnext/stock/stock_ledger.py b/erpnext/stock/stock_ledger.py index 4e4fd754678..10477638a03 100644 --- a/erpnext/stock/stock_ledger.py +++ b/erpnext/stock/stock_ledger.py @@ -729,32 +729,16 @@ class update_entries_after: self.distinct_item_warehouses[key] = val self.new_items_found = True else: - # Check if the dependent voucher is reposted - # If not, then do not add it to the list - if not self.is_dependent_voucher_reposted(dependant_sle): - return - - existing_sle_posting_date = self.distinct_item_warehouses[key].get("sle", {}).get("posting_date") - - dependent_voucher_detail_nos = self.get_dependent_voucher_detail_nos(key) - if getdate(dependant_sle.posting_date) < getdate(existing_sle_posting_date): - if dependent_voucher_detail_nos and dependant_sle.voucher_detail_no in set( - dependent_voucher_detail_nos - ): - return - - val.sle_changed = True - dependent_voucher_detail_nos.append(dependant_sle.voucher_detail_no) - val.dependent_voucher_detail_nos = dependent_voucher_detail_nos + existing_sle = self.distinct_item_warehouses[key].get("sle", {}) + if getdate(existing_sle.get("posting_date")) > getdate(dependant_sle.posting_date): self.distinct_item_warehouses[key] = val self.new_items_found = True - elif dependant_sle.voucher_detail_no not in set(dependent_voucher_detail_nos): - # Future dependent voucher needs to be repost to get the correct stock value - # If dependent voucher has not reposted, then add it to the list - dependent_voucher_detail_nos.append(dependant_sle.voucher_detail_no) - self.new_items_found = True - val.dependent_voucher_detail_nos = dependent_voucher_detail_nos + elif dependant_sle.voucher_type == "Stock Entry" and is_transfer_stock_entry( + dependant_sle.voucher_no + ): + print(dependant_sle.voucher_no) self.distinct_item_warehouses[key] = val + self.new_items_found = True def is_dependent_voucher_reposted(self, dependant_sle) -> bool: # Return False if the dependent voucher is not reposted @@ -1785,6 +1769,8 @@ def get_sle_by_voucher_detail_no(voucher_detail_no, excluded_sle=None): "posting_time", "voucher_detail_no", "posting_datetime as timestamp", + "voucher_type", + "voucher_no", ], as_dict=1, ) @@ -2291,3 +2277,10 @@ def get_stock_value_difference(item_code, warehouse, posting_date, posting_time, difference_amount = query.run() return flt(difference_amount[0][0]) if difference_amount else 0 + + +@frappe.request_cache +def is_transfer_stock_entry(voucher_no): + purpose = frappe.get_cached_value("Stock Entry", voucher_no, "purpose") + + return purpose in ["Material Transfer", "Material Transfer for Manufacture", "Send to Subcontractor"]