Skip to content

Commit

Permalink
fix: consider child nodes while getting bin details
Browse files Browse the repository at this point in the history
  • Loading branch information
s-aga-r committed Dec 27, 2022
1 parent 9858d9d commit 40dde96
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions erpnext/stock/get_item_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ def get_item_details(args, doc=None, for_validate=False, overwrite_warehouse=Tru
args.get("doctype") == "Material Request"
and args.get("material_request_type") == "Material Transfer"
):
out.update(get_bin_details(args.item_code, args.get("from_warehouse")))
out.update(
get_bin_details(args.item_code, args.get("from_warehouse"), include_child_warehouses=False)
)

elif out.get("warehouse"):
if doc and doc.get("doctype") == "Purchase Order":
Expand Down Expand Up @@ -1171,14 +1173,25 @@ def get_projected_qty(item_code, warehouse):


@frappe.whitelist()
def get_bin_details(item_code, warehouse, company=None):
bin_details = frappe.db.get_value(
"Bin",
{"item_code": item_code, "warehouse": warehouse},
["projected_qty", "actual_qty", "reserved_qty"],
as_dict=True,
cache=True,
) or {"projected_qty": 0, "actual_qty": 0, "reserved_qty": 0}
def get_bin_details(item_code, warehouse, company=None, include_child_warehouses=True):
from frappe.query_builder.functions import Sum

from erpnext.stock.doctype.warehouse.warehouse import get_child_warehouses

warehouses = get_child_warehouses(warehouse) if include_child_warehouses else [warehouse]

bin = frappe.qb.DocType("Bin")
bin_details = (
frappe.qb.from_(bin)
.select(
Sum(bin.actual_qty).as_("actual_qty"),
Sum(bin.projected_qty).as_("projected_qty"),
Sum(bin.reserved_qty).as_("reserved_qty"),
)
.where((bin.item_code == item_code) & (bin.warehouse.isin(warehouses)))
).run(as_dict=True)[0]
bin_details = {k: 0 for k, v in bin_details.items() if not v}

if company:
bin_details["company_total_stock"] = get_company_total_stock(item_code, company)
return bin_details
Expand Down

0 comments on commit 40dde96

Please sign in to comment.