Skip to content

Commit

Permalink
fix: incorrect requested quantity for the subcontracting order (backp…
Browse files Browse the repository at this point in the history
…ort #38455) (#38471)

* fix: incorrect requested quantity for the subcontracting order

(cherry picked from commit 691e3bb)

# Conflicts:
#	erpnext/buying/doctype/purchase_order/purchase_order.py
#	erpnext/subcontracting/doctype/subcontracting_order/subcontracting_order.py
#	erpnext/subcontracting/doctype/subcontracting_order_item/subcontracting_order_item.json
#	erpnext/subcontracting/doctype/subcontracting_order_service_item/subcontracting_order_service_item.json

* chore: fix conflicts

* chore: fix conflicts

* chore: fix conflicts

* chore: fix conflicts

---------

Co-authored-by: Rohit Waghchaure <rohitw1991@gmail.com>
  • Loading branch information
mergify[bot] and rohitwaghchaure authored Nov 30, 2023
1 parent bec7fb5 commit 497049b
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 154 deletions.
36 changes: 15 additions & 21 deletions erpnext/buying/doctype/purchase_order/purchase_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ def validate(self):
self.reset_default_field_value("set_warehouse", "items", "warehouse")

def validate_with_previous_doc(self):
mri_compare_fields = [["project", "="], ["item_code", "="]]
if self.is_subcontracted:
mri_compare_fields = [["project", "="]]

super(PurchaseOrder, self).validate_with_previous_doc(
{
"Supplier Quotation": {
Expand All @@ -108,7 +112,7 @@ def validate_with_previous_doc(self):
},
"Material Request Item": {
"ref_dn_field": "material_request_item",
"compare_fields": [["project", "="], ["item_code", "="]],
"compare_fields": mri_compare_fields,
"is_child_table": True,
},
}
Expand Down Expand Up @@ -282,23 +286,6 @@ def check_on_hold_or_closed_status(self):
check_list.append(d.material_request)
check_on_hold_or_closed_status("Material Request", d.material_request)

def update_requested_qty(self):
material_request_map = {}
for d in self.get("items"):
if d.material_request_item:
material_request_map.setdefault(d.material_request, []).append(d.material_request_item)

for mr, mr_item_rows in material_request_map.items():
if mr and mr_item_rows:
mr_obj = frappe.get_doc("Material Request", mr)

if mr_obj.status in ["Stopped", "Cancelled"]:
frappe.throw(
_("Material Request {0} is cancelled or stopped").format(mr), frappe.InvalidStatusError
)

mr_obj.update_requested_qty(mr_item_rows)

def update_ordered_qty(self, po_item_rows=None):
"""update requested qty (before ordered_qty is updated)"""
item_wh_list = []
Expand Down Expand Up @@ -340,7 +327,9 @@ def on_submit(self):
self.update_status_updater()

self.update_prevdoc_status()
self.update_requested_qty()
if not self.is_subcontracted or self.is_old_subcontracting_flow:
self.update_requested_qty()

self.update_ordered_qty()
self.validate_budget()
self.update_reserved_qty_for_subcontract()
Expand Down Expand Up @@ -372,7 +361,9 @@ def on_cancel(self):

# Must be called after updating ordered qty in Material Request
# bin uses Material Request Items to recalculate & update
self.update_requested_qty()
if not self.is_subcontracted or self.is_old_subcontracting_flow:
self.update_requested_qty()

self.update_ordered_qty()

self.update_blanket_order()
Expand Down Expand Up @@ -679,7 +670,10 @@ def get_mapped_subcontracting_order(source_name, target_doc=None):
},
"Purchase Order Item": {
"doctype": "Subcontracting Order Service Item",
"field_map": {},
"field_map": {
"material_request": "material_request",
"material_request_item": "material_request_item",
},
"field_no_map": [],
},
},
Expand Down
17 changes: 17 additions & 0 deletions erpnext/controllers/subcontracting_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,23 @@ def sub_contracted_items(self):

return self._sub_contracted_items

def update_requested_qty(self):
material_request_map = {}
for d in self.get("items"):
if d.material_request_item:
material_request_map.setdefault(d.material_request, []).append(d.material_request_item)

for mr, mr_item_rows in material_request_map.items():
if mr and mr_item_rows:
mr_obj = frappe.get_doc("Material Request", mr)

if mr_obj.status in ["Stopped", "Cancelled"]:
frappe.throw(
_("Material Request {0} is cancelled or stopped").format(mr), frappe.InvalidStatusError
)

mr_obj.update_requested_qty(mr_item_rows)


def get_item_details(items):
item = frappe.qb.DocType("Item")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@


class SubcontractingOrder(SubcontractingController):
def __init__(self, *args, **kwargs):
super(SubcontractingOrder, self).__init__(*args, **kwargs)

self.status_updater = [
{
"source_dt": "Subcontracting Order Item",
"target_dt": "Material Request Item",
"join_field": "material_request_item",
"target_field": "ordered_qty",
"target_parent_dt": "Material Request",
"target_parent_field": "per_ordered",
"target_ref_field": "stock_qty",
"source_field": "qty",
"percent_join_field": "material_request",
}
]

def before_validate(self):
super(SubcontractingOrder, self).before_validate()

Expand All @@ -26,11 +43,15 @@ def validate(self):
self.reset_default_field_value("set_warehouse", "items", "warehouse")

def on_submit(self):
self.update_prevdoc_status()
self.update_requested_qty()
self.update_ordered_qty_for_subcontracting()
self.update_reserved_qty_for_subcontracting()
self.update_status()

def on_cancel(self):
self.update_prevdoc_status()
self.update_requested_qty()
self.update_ordered_qty_for_subcontracting()
self.update_reserved_qty_for_subcontracting()
self.update_status()
Expand Down Expand Up @@ -164,7 +185,9 @@ def populate_items_table(self):
"qty": si.fg_item_qty,
"stock_uom": item.stock_uom,
"bom": bom,
},
"material_request": si.material_request,
"material_request_item": si.material_request_item,
}
)
else:
frappe.throw(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,62 @@ def test_ordered_qty_for_subcontracting_order(self):

self.assertEqual(ordered_qty + 10, new_ordered_qty)

def test_requested_qty_for_subcontracting_order(self):
from erpnext.stock.doctype.material_request.material_request import make_purchase_order
from erpnext.stock.doctype.material_request.test_material_request import make_material_request

requested_qty = frappe.db.get_value(
"Bin",
filters={"warehouse": "_Test Warehouse - _TC", "item_code": "Subcontracted Item SA8"},
fieldname="indented_qty",
)
requested_qty = flt(requested_qty)

mr = make_material_request(
item_code="Subcontracted Item SA8",
material_request_type="Purchase",
qty=10,
)

self.assertTrue(mr.docstatus == 1)

new_requested_qty = frappe.db.get_value(
"Bin",
filters={"warehouse": "_Test Warehouse - _TC", "item_code": "Subcontracted Item SA8"},
fieldname="indented_qty",
)
new_requested_qty = flt(new_requested_qty)

self.assertEqual(requested_qty + 10, new_requested_qty)

po = make_purchase_order(mr.name)
po.is_subcontracted = 1
po.supplier = "_Test Supplier"
po.items[0].fg_item = "Subcontracted Item SA8"
po.items[0].fg_item_qty = 10
po.items[0].item_code = "Subcontracted Service Item 8"
po.items[0].item_name = "Subcontracted Service Item 8"
po.items[0].qty = 10
po.supplier_warehouse = "_Test Warehouse 1 - _TC"
po.save()
po.submit()

self.assertTrue(po.items[0].material_request)
self.assertTrue(po.items[0].material_request_item)

sco = create_subcontracting_order(po_name=po.name)
self.assertTrue(sco.items[0].material_request)
self.assertTrue(sco.items[0].material_request_item)

new_requested_qty = frappe.db.get_value(
"Bin",
filters={"warehouse": "_Test Warehouse - _TC", "item_code": "Subcontracted Item SA8"},
fieldname="indented_qty",
)
new_requested_qty = flt(new_requested_qty)

self.assertEqual(requested_qty, new_requested_qty)


def create_subcontracting_order(**args):
args = frappe._dict(args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
"manufacture_section",
"manufacturer",
"manufacturer_part_no",
"column_break_impp",
"reference_section",
"material_request",
"column_break_fpyl",
"material_request_item",
"accounting_dimensions_section",
"cost_center",
"dimension_col_break",
Expand Down Expand Up @@ -332,13 +337,44 @@
"fieldtype": "Link",
"label": "Project",
"options": "Project"
},
{
"fieldname": "column_break_impp",
"fieldtype": "Column Break"
},
{
"fieldname": "material_request",
"fieldtype": "Link",
"label": "Material Request",
"no_copy": 1,
"options": "Material Request",
"read_only": 1,
"search_index": 1
},
{
"fieldname": "material_request_item",
"fieldtype": "Data",
"label": "Material Request Item",
"no_copy": 1,
"read_only": 1,
"search_index": 1
},
{
"collapsible": 1,
"fieldname": "reference_section",
"fieldtype": "Section Break",
"label": "Reference"
},
{
"fieldname": "column_break_fpyl",
"fieldtype": "Column Break"
}
],
"idx": 1,
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
"modified": "2023-11-14 18:38:37.640677",
"modified": "2023-11-30 15:29:43.744618",
"modified_by": "Administrator",
"module": "Subcontracting",
"name": "Subcontracting Order Item",
Expand All @@ -351,4 +387,4 @@
"sort_order": "DESC",
"states": [],
"track_changes": 1
}
}
Loading

0 comments on commit 497049b

Please sign in to comment.