Skip to content

Commit

Permalink
refactor: Made changes suggested by mentor and simplified logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mihir-kandoi committed Dec 18, 2024
1 parent cb977ce commit 0590879
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 40 deletions.
3 changes: 1 addition & 2 deletions erpnext/buying/doctype/purchase_order/purchase_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,7 @@ def on_submit(self):
if self.is_against_so():
self.update_status_updater()

is_scpo_from_mr = self.is_subcontracted and all([item.material_request for item in self.items])
self.update_prevdoc_status(source_field="fg_item_qty" if is_scpo_from_mr else None)
self.update_prevdoc_status()
if not self.is_subcontracted or self.is_old_subcontracting_flow:
self.update_requested_qty()

Expand Down
12 changes: 1 addition & 11 deletions erpnext/controllers/buying_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,7 @@ def set_missing_values(self, for_validate=False):
self.set_missing_item_details(for_validate)

def set_supplier_from_item_default(self):
is_scpo_from_mr = False
if self.doctype == "Purchase Order":
is_scpo_from_mr = all([item.material_request for item in self.items])

is_default_supplier_passed = bool(frappe.flags.args.default_supplier) if frappe.flags.args else False

if (
self.meta.get_field("supplier") and not self.supplier and is_default_supplier_passed
if is_scpo_from_mr
else True
):
if self.meta.get_field("supplier") and not self.supplier:
for d in self.get("items"):
supplier = frappe.db.get_value(
"Item Default", {"parent": d.item_code, "company": self.company}, "default_supplier"
Expand Down
13 changes: 8 additions & 5 deletions erpnext/controllers/status_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,7 @@ class StatusUpdater(Document):
Installation Note: Update Installed Qty, Update Percent Qty and Validate over installation
"""

def update_prevdoc_status(self, source_field=None):
if source_field:
for item in self.status_updater:
item.update({"source_field": source_field})

def update_prevdoc_status(self):
self.update_qty()
self.validate_qty()

Expand Down Expand Up @@ -425,6 +421,13 @@ def _update_children(self, args, update_modified):
if d.doctype != args["source_dt"]:
continue

if (
hasattr(d, "material_request")
and frappe.db.get_value("Material Request", d.material_request, "material_request_type")
== "Subcontracting"
):
args.update({"source_field": "fg_item_qty"})

self._update_modified(args, update_modified)

# updates qty in the child table
Expand Down
47 changes: 25 additions & 22 deletions erpnext/stock/doctype/material_request/material_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,15 +398,16 @@ def update_item(obj, target, source_parent):
if sc_bom := get_subcontracting_boms_for_finished_goods(target.fg_item):
target.item_code = sc_bom.service_item
target.uom = sc_bom.service_item_uom
target.conversion_factor = frappe.db.get_value(
"UOM Conversion Detail",
{"parent": sc_bom.service_item, "uom": sc_bom.service_item_uom},
"conversion_factor",
target.conversion_factor = (
frappe.db.get_value(
"UOM Conversion Detail",
{"parent": sc_bom.service_item, "uom": sc_bom.service_item_uom},
"conversion_factor",
)
or 1
)
target.qty = target.fg_item_qty * sc_bom.conversion_factor
target.stock_qty = target.qty / sc_bom.conversion_factor
else:
target.uom = "Nos"
target.stock_qty = target.qty * target.conversion_factor


def get_list_context(context=None):
Expand Down Expand Up @@ -466,6 +467,22 @@ def select_item(d):

return qty < d.stock_qty and child_filter

def generate_field_map():
field_map = [
["name", "material_request_item"],
["parent", "material_request"],
["sales_order", "sales_order"],
["sales_order_item", "sales_order_item"],
["wip_composite_asset", "wip_composite_asset"],
]

if is_subcontracted:
field_map.extend([["item_code", "fg_item"], ["qty", "fg_item_qty"]])
else:
field_map.extend([["uom", "stock_uom"], ["uom", "uom"]])

return field_map

doclist = get_mapped_doc(
"Material Request",
source_name,
Expand All @@ -479,21 +496,7 @@ def select_item(d):
},
"Material Request Item": {
"doctype": "Purchase Order Item",
"field_map": [
item
for item in [
["name", "material_request_item"],
["parent", "material_request"],
["qty", "fg_item_qty"] if is_subcontracted else [],
["item_code", "fg_item"] if is_subcontracted else [],
["uom", "stock_uom"] if not is_subcontracted else [],
["uom", "uom"] if not is_subcontracted else [],
["sales_order", "sales_order"],
["sales_order_item", "sales_order_item"],
["wip_composite_asset", "wip_composite_asset"],
]
if item
], # this list comprehension will remove all empty lists
"field_map": generate_field_map(),
"field_no_map": ["item_code", "item_name", "qty"] if is_subcontracted else [],
"postprocess": update_item,
"condition": select_item,
Expand Down

0 comments on commit 0590879

Please sign in to comment.