Skip to content

Commit

Permalink
fix: reserved qty for production plan (backport #37251) (#37253)
Browse files Browse the repository at this point in the history
fix: reserved qty for production plan (#37251)

(cherry picked from commit 0a0d5b3)

Co-authored-by: rohitwaghchaure <[email protected]>
  • Loading branch information
mergify[bot] and rohitwaghchaure authored Sep 26, 2023
1 parent d558ba2 commit 2cca37a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,59 @@ def test_resered_qty_for_production_plan_for_material_requests(self):

self.assertEqual(after_qty, before_qty)

def test_resered_qty_for_production_plan_for_work_order(self):
from erpnext.stock.utils import get_or_make_bin

bin_name = get_or_make_bin("Raw Material Item 1", "_Test Warehouse - _TC")
before_qty = flt(frappe.db.get_value("Bin", bin_name, "reserved_qty_for_production_plan"))

pln = create_production_plan(item_code="Test Production Item 1")

bin_name = get_or_make_bin("Raw Material Item 1", "_Test Warehouse - _TC")
after_qty = flt(frappe.db.get_value("Bin", bin_name, "reserved_qty_for_production_plan"))

self.assertEqual(after_qty - before_qty, 1)

pln.make_work_order()

work_orders = []
for row in frappe.get_all("Work Order", filters={"production_plan": pln.name}, fields=["name"]):
wo_doc = frappe.get_doc("Work Order", row.name)
wo_doc.source_warehouse = "_Test Warehouse - _TC"
wo_doc.wip_warehouse = "_Test Warehouse 1 - _TC"
wo_doc.fg_warehouse = "_Test Warehouse - _TC"
for d in wo_doc.required_items:
d.source_warehouse = "_Test Warehouse - _TC"
make_stock_entry(
item_code=d.item_code,
qty=d.required_qty,
rate=100,
target="_Test Warehouse - _TC",
)

wo_doc.submit()
work_orders.append(wo_doc)

bin_name = get_or_make_bin("Raw Material Item 1", "_Test Warehouse - _TC")
after_qty = flt(frappe.db.get_value("Bin", bin_name, "reserved_qty_for_production_plan"))

self.assertEqual(after_qty, before_qty)

rm_work_order = None
for wo_doc in work_orders:
for d in wo_doc.required_items:
if d.item_code == "Raw Material Item 1":
rm_work_order = wo_doc
break

if rm_work_order:
s = frappe.get_doc(make_se_from_wo(rm_work_order.name, "Material Transfer for Manufacture", 1))
s.submit()
bin_name = get_or_make_bin("Raw Material Item 1", "_Test Warehouse - _TC")
after_qty = flt(frappe.db.get_value("Bin", bin_name, "reserved_qty_for_production_plan"))

self.assertEqual(after_qty, before_qty)

def test_resered_qty_for_production_plan_for_material_requests_with_multi_UOM(self):
from erpnext.stock.utils import get_or_make_bin

Expand Down
15 changes: 8 additions & 7 deletions erpnext/manufacturing/doctype/work_order/work_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -1519,16 +1519,17 @@ def get_reserved_qty_for_production(
wo = frappe.qb.DocType("Work Order")
wo_item = frappe.qb.DocType("Work Order Item")

if check_production_plan:
qty_field = wo_item.required_qty
else:
qty_field = Case()
qty_field = qty_field.when(wo.skip_transfer == 0, wo_item.required_qty - wo_item.transferred_qty)
qty_field = qty_field.else_(wo_item.required_qty - wo_item.consumed_qty)

query = (
frappe.qb.from_(wo)
.from_(wo_item)
.select(
Sum(
Case()
.when(wo.skip_transfer == 0, wo_item.required_qty - wo_item.transferred_qty)
.else_(wo_item.required_qty - wo_item.consumed_qty)
)
)
.select(Sum(qty_field))
.where(
(wo_item.item_code == item_code)
& (wo_item.parent == wo.name)
Expand Down

0 comments on commit 2cca37a

Please sign in to comment.