Skip to content

Commit

Permalink
fix: Made minor changes in logic to pass all test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
mihir-kandoi committed Dec 18, 2024
1 parent 893ea16 commit cb977ce
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 23 deletions.
3 changes: 2 additions & 1 deletion erpnext/buying/doctype/purchase_order/purchase_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,8 @@ def on_submit(self):
if self.is_against_so():
self.update_status_updater()

self.update_prevdoc_status(source_field="fg_item_qty")
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)
if not self.is_subcontracted or self.is_old_subcontracting_flow:
self.update_requested_qty()

Expand Down
13 changes: 11 additions & 2 deletions erpnext/controllers/buying_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,17 @@ 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 = all([item.material_request for item in self.items]) if self.doctype == "Purchase Order" else False
if self.meta.get_field("supplier") and not self.supplier and (frappe.flags.args.default_supplier if is_scpo_from_mr else True):
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
):
for d in self.get("items"):
supplier = frappe.db.get_value(
"Item Default", {"parent": d.item_code, "company": self.company}, "default_supplier"
Expand Down
2 changes: 1 addition & 1 deletion erpnext/controllers/status_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +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):
def update_prevdoc_status(self, source_field=None):
if source_field:
for item in self.status_updater:
item.update({"source_field": source_field})
Expand Down
71 changes: 52 additions & 19 deletions erpnext/stock/doctype/material_request/material_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from erpnext.manufacturing.doctype.work_order.work_order import get_item_details
from erpnext.stock.doctype.item.item import get_item_defaults
from erpnext.stock.stock_balance import get_indented_qty, update_bin_qty

from erpnext.subcontracting.doctype.subcontracting_bom.subcontracting_bom import (
get_subcontracting_boms_for_finished_goods,
)
Expand All @@ -33,16 +32,24 @@ class MaterialRequest(BuyingController):
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from erpnext.stock.doctype.material_request_item.material_request_item import MaterialRequestItem
from frappe.types import DF

from erpnext.stock.doctype.material_request_item.material_request_item import MaterialRequestItem

amended_from: DF.Link | None
company: DF.Link
customer: DF.Link | None
items: DF.Table[MaterialRequestItem]
job_card: DF.Link | None
letter_head: DF.Link | None
material_request_type: DF.Literal["Purchase", "Material Transfer", "Material Issue", "Manufacture", "Subcontracting", "Customer Provided"]
material_request_type: DF.Literal[
"Purchase",
"Material Transfer",
"Material Issue",
"Manufacture",
"Subcontracting",
"Customer Provided",
]
naming_series: DF.Literal["MAT-MR-.YYYY.-"]
per_ordered: DF.Percent
per_received: DF.Percent
Expand All @@ -51,7 +58,20 @@ class MaterialRequest(BuyingController):
select_print_heading: DF.Link | None
set_from_warehouse: DF.Link | None
set_warehouse: DF.Link | None
status: DF.Literal["", "Draft", "Submitted", "Stopped", "Cancelled", "Pending", "Partially Ordered", "Partially Received", "Ordered", "Issued", "Transferred", "Received"]
status: DF.Literal[
"",
"Draft",
"Submitted",
"Stopped",
"Cancelled",
"Pending",
"Partially Ordered",
"Partially Received",
"Ordered",
"Issued",
"Transferred",
"Received",
]
tc_name: DF.Link | None
terms: DF.TextEditor | None
title: DF.Data | None
Expand Down Expand Up @@ -378,7 +398,11 @@ 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",
)
target.qty = target.fg_item_qty * sc_bom.conversion_factor
target.stock_qty = target.qty / sc_bom.conversion_factor
else:
Expand Down Expand Up @@ -415,15 +439,17 @@ def make_purchase_order(source_name, target_doc=None, args=None):
if isinstance(args, str):
args = json.loads(args)

is_subcontracted = frappe.db.get_value("Material Request", source_name, "material_request_type") == "Subcontracting"
is_subcontracted = (
frappe.db.get_value("Material Request", source_name, "material_request_type") == "Subcontracting"
)

def postprocess(source, target_doc):
target_doc.is_subcontracted = is_subcontracted
if frappe.flags.args and frappe.flags.args.default_supplier:
# items only for given default supplier
supplier_items = []
for d in target_doc.items:
if not d.item_code:
if is_subcontracted and not d.item_code:
continue
default_supplier = get_item_defaults(d.item_code, target_doc.company).get("default_supplier")
if frappe.flags.args.default_supplier == default_supplier:
Expand All @@ -446,21 +472,28 @@ def select_item(d):
{
"Material Request": {
"doctype": "Purchase Order",
"validation": {"docstatus": ["=", 1], "material_request_type": ["in", ["Purchase", "Subcontracting"]]},
"validation": {
"docstatus": ["=", 1],
"material_request_type": ["in", ["Purchase", "Subcontracting"]],
},
},
"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": [
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_no_map": ["item_code", "item_name", "qty"] if is_subcontracted else [],
"postprocess": update_item,
"condition": select_item,
Expand Down

0 comments on commit cb977ce

Please sign in to comment.