Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: slow stock reposting #31631

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions erpnext/stock/stock_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,12 @@ def repost_future_sle(
via_landed_cost_voucher=False,
doc=None,
):
if not args and voucher_type and voucher_no:
args = get_items_to_be_repost(voucher_type, voucher_no, doc)

items_to_be_repost = get_items_to_be_repost(
voucher_type=voucher_type, voucher_no=voucher_no, doc=doc
)
if items_to_be_repost:
args = items_to_be_repost

distinct_item_warehouses = get_distinct_item_warehouse(args, doc)
affected_transactions = get_affected_transactions(doc)
Expand Down Expand Up @@ -286,17 +290,21 @@ def update_args_in_repost_item_valuation(
)


def get_items_to_be_repost(voucher_type, voucher_no, doc=None):
def get_items_to_be_repost(voucher_type=None, voucher_no=None, doc=None):
items_to_be_repost = []
if doc and doc.items_to_be_repost:
return json.loads(doc.items_to_be_repost) or []
items_to_be_repost = json.loads(doc.items_to_be_repost) or []

if not items_to_be_repost and voucher_type and voucher_no:
items_to_be_repost = frappe.db.get_all(
"Stock Ledger Entry",
filters={"voucher_type": voucher_type, "voucher_no": voucher_no},
fields=["item_code", "warehouse", "posting_date", "posting_time", "creation"],
order_by="creation asc",
group_by="item_code, warehouse",
)

return frappe.db.get_all(
"Stock Ledger Entry",
filters={"voucher_type": voucher_type, "voucher_no": voucher_no},
fields=["item_code", "warehouse", "posting_date", "posting_time", "creation"],
order_by="creation asc",
group_by="item_code, warehouse",
)
return items_to_be_repost


def get_distinct_item_warehouse(args=None, doc=None):
Expand Down Expand Up @@ -496,7 +504,8 @@ def get_dependent_entries_to_fix(self, entries_to_fix, sle):
elif dependant_sle.item_code == self.item_code and dependant_sle.warehouse in self.data:
return entries_to_fix
else:
self.append_future_sle_for_dependant(dependant_sle, entries_to_fix)
self.initialize_previous_data(dependant_sle)
self.update_distinct_item_warehouses(dependant_sle)
return entries_to_fix

def update_distinct_item_warehouses(self, dependant_sle):
Expand All @@ -514,14 +523,6 @@ def update_distinct_item_warehouses(self, dependant_sle):
self.distinct_item_warehouses[key] = val
self.new_items_found = True

def append_future_sle_for_dependant(self, dependant_sle, entries_to_fix):
self.initialize_previous_data(dependant_sle)
self.distinct_item_warehouses[(self.item_code, dependant_sle.warehouse)] = frappe._dict(
{"sle": dependant_sle}
)

self.new_items_found = True

def process_sle(self, sle):
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos

Expand Down