fix: Updated Bin Requested Qty logic
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index 8aec8bd..6ff8b90 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -660,3 +660,4 @@
erpnext.patches.v12_0.create_irs_1099_field_united_states
erpnext.patches.v12_0.move_bank_account_swift_number_to_bank
erpnext.patches.v12_0.rename_bank_reconciliation_fields # 2020-01-22
+erpnext.patches.v12_0.recalculate_requested_qty_in_bin
\ No newline at end of file
diff --git a/erpnext/patches/v12_0/recalculate_requested_qty_in_bin.py b/erpnext/patches/v12_0/recalculate_requested_qty_in_bin.py
new file mode 100644
index 0000000..8267df9
--- /dev/null
+++ b/erpnext/patches/v12_0/recalculate_requested_qty_in_bin.py
@@ -0,0 +1,13 @@
+from __future__ import unicode_literals
+import frappe
+from erpnext.stock.stock_balance import update_bin_qty, get_indented_qty
+
+def execute():
+ bin_details = frappe.db.sql("""
+ SELECT item_code, warehouse
+ FROM `tabBin`""",as_dict=1)
+
+ for entry in bin_details:
+ update_bin_qty(entry.get("item_code"), entry.get("warehouse"), {
+ "indented_qty": get_indented_qty(entry.get("item_code"), entry.get("warehouse"))
+ })
\ No newline at end of file
diff --git a/erpnext/stock/stock_balance.py b/erpnext/stock/stock_balance.py
index e5dc6b1..2bdb04e 100644
--- a/erpnext/stock/stock_balance.py
+++ b/erpnext/stock/stock_balance.py
@@ -113,13 +113,24 @@
return flt(reserved_qty[0][0]) if reserved_qty else 0
def get_indented_qty(item_code, warehouse):
- indented_qty = frappe.db.sql("""select sum((mr_item.qty - mr_item.ordered_qty) * mr_item.conversion_factor)
+ inward_qty = frappe.db.sql("""select sum((mr_item.qty - mr_item.ordered_qty) * mr_item.conversion_factor)
+ from `tabMaterial Request Item` mr_item, `tabMaterial Request` mr
+ where mr_item.item_code=%s and mr_item.warehouse=%s
+ and mr.material_request_type in ('Purchase', 'Manufacture')
+ and mr_item.qty > mr_item.ordered_qty and mr_item.parent=mr.name
+ and mr.status!='Stopped' and mr.docstatus=1""", (item_code, warehouse))
+
+ outward_qty = frappe.db.sql("""select sum((mr_item.qty - mr_item.ordered_qty) * mr_item.conversion_factor)
from `tabMaterial Request Item` mr_item, `tabMaterial Request` mr
where mr_item.item_code=%s and mr_item.warehouse=%s
+ and mr.material_request_type in ('Material Issue', 'Material Transfer')
and mr_item.qty > mr_item.ordered_qty and mr_item.parent=mr.name
and mr.status!='Stopped' and mr.docstatus=1""", (item_code, warehouse))
- return flt(indented_qty[0][0]) if indented_qty else 0
+ inward_qty, outward_qty = flt(inward_qty[0][0]) if inward_qty else 0, flt(outward_qty[0][0]) if outward_qty else 0
+ indented_qty = inward_qty - outward_qty
+
+ return indented_qty
def get_ordered_qty(item_code, warehouse):
ordered_qty = frappe.db.sql("""
@@ -145,9 +156,9 @@
from erpnext.stock.utils import get_bin
bin = get_bin(item_code, warehouse)
mismatch = False
- for fld, val in qty_dict.items():
- if flt(bin.get(fld)) != flt(val):
- bin.set(fld, flt(val))
+ for field, value in qty_dict.items():
+ if flt(bin.get(field)) != flt(value):
+ bin.set(field, flt(value))
mismatch = True
if mismatch: