perf: reduce no of queries for checking if future sl entry exists (#24881)

This commit is contained in:
Sagar Vora 2021-03-27 16:10:20 +05:30 committed by GitHub
parent ab673d9e4b
commit 868c0bf45b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 23 deletions

View File

@ -495,7 +495,7 @@ class StockController(AccountsController):
"voucher_no": self.name,
"company": self.company
})
if check_if_future_sle_exists(args):
if future_sle_exists(args):
create_repost_item_valuation_entry(args)
elif not is_reposting_pending():
check_if_stock_and_account_balance_synced(self.posting_date,
@ -506,37 +506,42 @@ def is_reposting_pending():
{'docstatus': 1, 'status': ['in', ['Queued','In Progress']]})
def check_if_future_sle_exists(args):
sl_entries = frappe.db.get_all("Stock Ledger Entry",
def future_sle_exists(args):
sl_entries = frappe.get_all("Stock Ledger Entry",
filters={"voucher_type": args.voucher_type, "voucher_no": args.voucher_no},
fields=["item_code", "warehouse"],
order_by="creation asc")
distinct_item_warehouses = list(set([(d.item_code, d.warehouse) for d in sl_entries]))
if not sl_entries:
return
sle_exists = False
for item_code, warehouse in distinct_item_warehouses:
args.update({
"item_code": item_code,
"warehouse": warehouse
})
if get_sle(args):
sle_exists = True
break
return sle_exists
warehouse_items_map = {}
for entry in sl_entries:
if entry.warehouse not in warehouse_items_map:
warehouse_items_map[entry.warehouse] = set()
warehouse_items_map[entry.warehouse].add(entry.item_code)
or_conditions = []
for warehouse, items in warehouse_items_map.items():
or_conditions.append(
"warehouse = '{}' and item_code in ({})".format(
warehouse,
", ".join(frappe.db.escape(item) for item in items)
)
)
def get_sle(args):
return frappe.db.sql("""
select name
from `tabStock Ledger Entry`
where
item_code=%(item_code)s
and warehouse=%(warehouse)s
and timestamp(posting_date, posting_time) >= timestamp(%(posting_date)s, %(posting_time)s)
({})
and timestamp(posting_date, posting_time)
>= timestamp(%(posting_date)s, %(posting_time)s)
and voucher_no != %(voucher_no)s
and is_cancelled = 0
limit 1
""", args)
""".format(" or ".join(or_conditions)), args)
def create_repost_item_valuation_entry(args):
args = frappe._dict(args)
@ -554,4 +559,4 @@ def create_repost_item_valuation_entry(args):
repost_entry.allow_zero_rate = args.allow_zero_rate
repost_entry.flags.ignore_links = True
repost_entry.save()
repost_entry.submit()
repost_entry.submit()

View File

@ -207,11 +207,11 @@ class update_entries_after(object):
def build(self):
from erpnext.controllers.stock_controller import check_if_future_sle_exists
from erpnext.controllers.stock_controller import future_sle_exists
if self.args.get("sle_id"):
self.process_sle_against_current_timestamp()
if not check_if_future_sle_exists(self.args):
if not future_sle_exists(self.args):
self.update_bin()
else:
entries_to_fix = self.get_future_entries_to_fix()
@ -856,4 +856,4 @@ def get_future_sle_with_negative_qty(args):
and qty_after_transaction < 0
order by timestamp(posting_date, posting_time) asc
limit 1
""", args, as_dict=1)
""", args, as_dict=1)