Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: incorrect Reserved Qty for Production Plan in BIN for the multi-uom case (backport #36162) #36168

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1539,7 +1539,7 @@ def get_reserved_qty_for_production_plan(item_code, warehouse):
frappe.qb.from_(table)
.inner_join(child)
.on(table.name == child.parent)
.select(Sum(child.required_bom_qty * IfNull(child.conversion_factor, 1.0)))
.select(Sum(child.quantity * IfNull(child.conversion_factor, 1.0)))
.where(
(table.docstatus == 1)
& (child.item_code == item_code)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,54 @@ 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_material_requests_with_multi_UOM(self):
from erpnext.stock.utils import get_or_make_bin

fg_item = make_item(properties={"is_stock_item": 1, "stock_uom": "_Test UOM 1"}).name
bom_item = make_item(
properties={"is_stock_item": 1, "stock_uom": "_Test UOM 1", "purchase_uom": "Nos"}
).name

if not frappe.db.exists("UOM Conversion Detail", {"parent": bom_item, "uom": "Nos"}):
doc = frappe.get_doc("Item", bom_item)
doc.append("uoms", {"uom": "Nos", "conversion_factor": 25})
doc.save()

make_bom(item=fg_item, raw_materials=[bom_item], source_warehouse="_Test Warehouse - _TC")

bin_name = get_or_make_bin(bom_item, "_Test Warehouse - _TC")
before_qty = flt(frappe.db.get_value("Bin", bin_name, "reserved_qty_for_production_plan"))

pln = create_production_plan(
item_code=fg_item, planned_qty=100, ignore_existing_ordered_qty=1, stock_uom="_Test UOM 1"
)

for row in pln.mr_items:
self.assertEqual(row.uom, "Nos")
self.assertEqual(row.quantity, 4)

reserved_qty = flt(frappe.db.get_value("Bin", bin_name, "reserved_qty_for_production_plan"))
self.assertEqual(reserved_qty - before_qty, 100.0)

pln.submit_material_request = 1
pln.make_work_order()

for work_order in frappe.get_all(
"Work Order",
fields=["name"],
filters={"production_plan": pln.name},
):
wo_doc = frappe.get_doc("Work Order", work_order.name)
wo_doc.source_warehouse = "_Test Warehouse - _TC"
wo_doc.wip_warehouse = "_Test Warehouse 1 - _TC"
wo_doc.fg_warehouse = "_Test Warehouse - _TC"
wo_doc.submit()

reserved_qty_after_mr = flt(
frappe.db.get_value("Bin", bin_name, "reserved_qty_for_production_plan")
)
self.assertEqual(reserved_qty_after_mr, before_qty)

def test_skip_available_qty_for_sub_assembly_items(self):
from erpnext.manufacturing.doctype.bom.test_bom import create_nested_bom

Expand Down
12 changes: 9 additions & 3 deletions erpnext/stock/doctype/material_request/material_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ def set_title(self):
self.title = _("{0} Request for {1}").format(_(self.material_request_type), items)[:100]

def on_submit(self):
self.update_requested_qty()
self.update_requested_qty_in_production_plan()
self.update_requested_qty()
if self.material_request_type == "Purchase":
self.validate_budget()

Expand Down Expand Up @@ -178,8 +178,8 @@ def status_can_change(self, status):
)

def on_cancel(self):
self.update_requested_qty()
self.update_requested_qty_in_production_plan()
self.update_requested_qty()

def get_mr_items_ordered_qty(self, mr_items):
mr_items_ordered_qty = {}
Expand Down Expand Up @@ -270,7 +270,13 @@ def update_requested_qty(self, mr_item_rows=None):
item_wh_list.append([d.item_code, d.warehouse])

for item_code, warehouse in item_wh_list:
update_bin_qty(item_code, warehouse, {"indented_qty": get_indented_qty(item_code, warehouse)})
update_bin_qty(
item_code,
warehouse,
{
"indented_qty": get_indented_qty(item_code, warehouse),
},
)

def update_requested_qty_in_production_plan(self):
production_plans = []
Expand Down