fix: consider child nodes while getting bin details
diff --git a/erpnext/controllers/selling_controller.py b/erpnext/controllers/selling_controller.py
index 8b073a4..cd1168d 100644
--- a/erpnext/controllers/selling_controller.py
+++ b/erpnext/controllers/selling_controller.py
@@ -23,7 +23,7 @@
super(SellingController, self).onload()
if self.doctype in ("Sales Order", "Delivery Note", "Sales Invoice"):
for item in self.get("items"):
- item.update(get_bin_details(item.item_code, item.warehouse))
+ item.update(get_bin_details(item.item_code, item.warehouse, include_child_warehouses=True))
def validate(self):
super(SellingController, self).validate()
diff --git a/erpnext/public/js/controllers/buying.js b/erpnext/public/js/controllers/buying.js
index 09779d8..b0e08cc 100644
--- a/erpnext/public/js/controllers/buying.js
+++ b/erpnext/public/js/controllers/buying.js
@@ -225,7 +225,8 @@
args: {
item_code: item.item_code,
warehouse: item.warehouse,
- company: doc.company
+ company: doc.company,
+ include_child_warehouses: true
}
});
}
diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py
index 1741d65..dc00999 100644
--- a/erpnext/stock/get_item_details.py
+++ b/erpnext/stock/get_item_details.py
@@ -102,9 +102,11 @@
elif out.get("warehouse"):
if doc and doc.get("doctype") == "Purchase Order":
# calculate company_total_stock only for po
- bin_details = get_bin_details(args.item_code, out.warehouse, args.company)
+ bin_details = get_bin_details(
+ args.item_code, out.warehouse, args.company, include_child_warehouses=True
+ )
else:
- bin_details = get_bin_details(args.item_code, out.warehouse)
+ bin_details = get_bin_details(args.item_code, out.warehouse, include_child_warehouses=True)
out.update(bin_details)
@@ -1060,7 +1062,9 @@
res[fieldname] = pos_profile.get(fieldname)
if res.get("warehouse"):
- res.actual_qty = get_bin_details(args.item_code, res.warehouse).get("actual_qty")
+ res.actual_qty = get_bin_details(
+ args.item_code, res.warehouse, include_child_warehouses=True
+ ).get("actual_qty")
return res
@@ -1171,14 +1175,26 @@
@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=False):
+ bin_details = {"projected_qty": 0, "actual_qty": 0, "reserved_qty": 0}
+
+ if warehouse:
+ from erpnext.stock.doctype.warehouse.warehouse import get_child_warehouses
+
+ warehouses = get_child_warehouses(warehouse) if include_child_warehouses else [warehouse]
+ bin_details = frappe.db.get_value(
+ "Bin",
+ filters={"item_code": item_code, "warehouse": ["in", warehouses]},
+ fieldname=[
+ "sum(projected_qty) as projected_qty",
+ "sum(actual_qty) as actual_qty",
+ "sum(reserved_qty) as reserved_qty",
+ ],
+ as_dict=True,
+ cache=True,
+ )
+ bin_details = {k: 0 if not v else v for k, v in bin_details.items()}
+
if company:
bin_details["company_total_stock"] = get_company_total_stock(item_code, company)
return bin_details