mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-26 16:34:46 +00:00
improve performance of stock_value retrieval using tabBin instead of … (#14584)
* improve performance of stock_value retrieval using tabBin instead of stock ledger * rename fn * rename fn * Update warehouse.py
This commit is contained in:
@@ -139,7 +139,7 @@ class Warehouse(NestedSet):
|
|||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def get_children(doctype, parent=None, company=None, is_root=False):
|
def get_children(doctype, parent=None, company=None, is_root=False):
|
||||||
from erpnext.stock.utils import get_stock_value_on
|
from erpnext.stock.utils import get_stock_value_from_bin
|
||||||
|
|
||||||
if is_root:
|
if is_root:
|
||||||
parent = ""
|
parent = ""
|
||||||
@@ -155,8 +155,7 @@ def get_children(doctype, parent=None, company=None, is_root=False):
|
|||||||
|
|
||||||
# return warehouses
|
# return warehouses
|
||||||
for wh in warehouses:
|
for wh in warehouses:
|
||||||
wh["balance"] = get_stock_value_on(warehouse=wh.value)
|
wh["balance"] = get_stock_value_from_bin(warehouse=wh.value)
|
||||||
|
|
||||||
return warehouses
|
return warehouses
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
@@ -172,4 +171,4 @@ def add_node():
|
|||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def convert_to_group_or_ledger():
|
def convert_to_group_or_ledger():
|
||||||
args = frappe.form_dict
|
args = frappe.form_dict
|
||||||
return frappe.get_doc("Warehouse", args.docname).convert_to_group_or_ledger()
|
return frappe.get_doc("Warehouse", args.docname).convert_to_group_or_ledger()
|
||||||
|
|||||||
@@ -11,21 +11,47 @@ from six import string_types
|
|||||||
|
|
||||||
class InvalidWarehouseCompany(frappe.ValidationError): pass
|
class InvalidWarehouseCompany(frappe.ValidationError): pass
|
||||||
|
|
||||||
|
def get_stock_value_from_bin (warehouse=None, item_code=None):
|
||||||
|
|
||||||
|
values = {}
|
||||||
|
conditions = ""
|
||||||
|
if warehouse:
|
||||||
|
conditions += """ and warehouse in (
|
||||||
|
select w2.name from `tabWarehouse` w1
|
||||||
|
join `tabWarehouse` w2 on
|
||||||
|
w1.name = %(warehouse)s
|
||||||
|
and w2.lft between w1.lft and w1.rgt
|
||||||
|
) """
|
||||||
|
|
||||||
|
values['warehouse'] = warehouse
|
||||||
|
|
||||||
|
if item_code:
|
||||||
|
conditions += " and item_code = %(item_code)s"
|
||||||
|
|
||||||
|
|
||||||
|
values['item_code'] = item_code
|
||||||
|
|
||||||
|
query = "select sum(stock_value) from `tabBin` where 1 = 1" + conditions
|
||||||
|
|
||||||
|
stock_value = frappe.db.sql(query, values)
|
||||||
|
|
||||||
|
return stock_value;
|
||||||
|
|
||||||
def get_stock_value_on(warehouse=None, posting_date=None, item_code=None):
|
def get_stock_value_on(warehouse=None, posting_date=None, item_code=None):
|
||||||
if not posting_date: posting_date = nowdate()
|
if not posting_date: posting_date = nowdate()
|
||||||
|
|
||||||
values, condition = [posting_date], ""
|
values, condition = [posting_date], ""
|
||||||
|
|
||||||
if warehouse:
|
if warehouse:
|
||||||
|
|
||||||
lft, rgt, is_group = frappe.db.get_value("Warehouse", warehouse, ["lft", "rgt", "is_group"])
|
lft, rgt, is_group = frappe.db.get_value("Warehouse", warehouse, ["lft", "rgt", "is_group"])
|
||||||
|
|
||||||
if is_group:
|
if is_group:
|
||||||
values.extend([lft, rgt])
|
values.extend([lft, rgt])
|
||||||
condition += "and exists (\
|
condition += "and exists (\
|
||||||
select name from `tabWarehouse` wh where wh.name = sle.warehouse\
|
select name from `tabWarehouse` wh where wh.name = sle.warehouse\
|
||||||
and wh.lft >= %s and wh.rgt <= %s)"
|
and wh.lft >= %s and wh.rgt <= %s)"
|
||||||
|
|
||||||
else:
|
else:
|
||||||
values.append(warehouse)
|
values.append(warehouse)
|
||||||
condition += " AND warehouse = %s"
|
condition += " AND warehouse = %s"
|
||||||
@@ -45,7 +71,7 @@ def get_stock_value_on(warehouse=None, posting_date=None, item_code=None):
|
|||||||
for sle in stock_ledger_entries:
|
for sle in stock_ledger_entries:
|
||||||
if not (sle.item_code, sle.warehouse) in sle_map:
|
if not (sle.item_code, sle.warehouse) in sle_map:
|
||||||
sle_map[(sle.item_code, sle.warehouse)] = flt(sle.stock_value)
|
sle_map[(sle.item_code, sle.warehouse)] = flt(sle.stock_value)
|
||||||
|
|
||||||
return sum(sle_map.values())
|
return sum(sle_map.values())
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
@@ -75,17 +101,17 @@ def get_latest_stock_qty(item_code, warehouse=None):
|
|||||||
values, condition = [item_code], ""
|
values, condition = [item_code], ""
|
||||||
if warehouse:
|
if warehouse:
|
||||||
lft, rgt, is_group = frappe.db.get_value("Warehouse", warehouse, ["lft", "rgt", "is_group"])
|
lft, rgt, is_group = frappe.db.get_value("Warehouse", warehouse, ["lft", "rgt", "is_group"])
|
||||||
|
|
||||||
if is_group:
|
if is_group:
|
||||||
values.extend([lft, rgt])
|
values.extend([lft, rgt])
|
||||||
condition += "and exists (\
|
condition += "and exists (\
|
||||||
select name from `tabWarehouse` wh where wh.name = tabBin.warehouse\
|
select name from `tabWarehouse` wh where wh.name = tabBin.warehouse\
|
||||||
and wh.lft >= %s and wh.rgt <= %s)"
|
and wh.lft >= %s and wh.rgt <= %s)"
|
||||||
|
|
||||||
else:
|
else:
|
||||||
values.append(warehouse)
|
values.append(warehouse)
|
||||||
condition += " AND warehouse = %s"
|
condition += " AND warehouse = %s"
|
||||||
|
|
||||||
actual_qty = frappe.db.sql("""select sum(actual_qty) from tabBin
|
actual_qty = frappe.db.sql("""select sum(actual_qty) from tabBin
|
||||||
where item_code=%s {0}""".format(condition), values)[0][0]
|
where item_code=%s {0}""".format(condition), values)[0][0]
|
||||||
|
|
||||||
@@ -222,4 +248,3 @@ def validate_warehouse_company(warehouse, company):
|
|||||||
def is_group_warehouse(warehouse):
|
def is_group_warehouse(warehouse):
|
||||||
if frappe.db.get_value("Warehouse", warehouse, "is_group"):
|
if frappe.db.get_value("Warehouse", warehouse, "is_group"):
|
||||||
frappe.throw(_("Group node warehouse is not allowed to select for transactions"))
|
frappe.throw(_("Group node warehouse is not allowed to select for transactions"))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user