Skip to content

Commit

Permalink
fix: BOM replace tool does not update exploded items of root (backport
Browse files Browse the repository at this point in the history
…#39244) (#39249)

fix: BOM replace tool does not update exploded items of root (#39244)

(cherry picked from commit 5e0d017)

Co-authored-by: rohitwaghchaure <rohitw1991@gmail.com>
  • Loading branch information
mergify[bot] and rohitwaghchaure authored Jan 9, 2024
1 parent c9d4c41 commit c2eeeec
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ def get_ancestor_boms(new_bom: str, bom_list: Optional[List] = None) -> List:
if new_bom == d.parent:
frappe.throw(_("BOM recursion: {0} cannot be child of {1}").format(new_bom, d.parent))

bom_list.append(d.parent)
if d.parent not in tuple(bom_list):
bom_list.append(d.parent)

get_ancestor_boms(d.parent, bom_list)

return list(set(bom_list))
return bom_list


def update_new_bom_in_bom_items(unit_cost: float, current_bom: str, new_bom: str) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,68 @@ def test_bom_update_log_completion(self):
log.reload()
self.assertEqual(log.status, "Completed")

def test_bom_replace_for_root_bom(self):
"""
- B-Item A (Root Item)
- B-Item B
- B-Item C
- B-Item D
- B-Item E
- B-Item F
Create New BOM for B-Item E with B-Item G and replace it in the above BOM.
"""

from erpnext.manufacturing.doctype.bom.test_bom import create_nested_bom
from erpnext.stock.doctype.item.test_item import make_item

items = ["B-Item A", "B-Item B", "B-Item C", "B-Item D", "B-Item E", "B-Item F", "B-Item G"]

for item_code in items:
if not frappe.db.exists("Item", item_code):
make_item(item_code)

for item_code in items:
remove_bom(item_code)

bom_tree = {
"B-Item A": {"B-Item B": {"B-Item C": {}}, "B-Item D": {"B-Item E": {"B-Item F": {}}}}
}

root_bom = create_nested_bom(bom_tree, prefix="")

exploded_items = frappe.get_all(
"BOM Explosion Item", filters={"parent": root_bom.name}, fields=["item_code"]
)

exploded_items = [item.item_code for item in exploded_items]
expected_exploded_items = ["B-Item C", "B-Item F"]
self.assertEqual(sorted(exploded_items), sorted(expected_exploded_items))

old_bom = frappe.db.get_value("BOM", {"item": "B-Item E"}, "name")
bom_tree = {"B-Item E": {"B-Item G": {}}}

new_bom = create_nested_bom(bom_tree, prefix="")
enqueue_replace_bom(boms=frappe._dict(current_bom=old_bom, new_bom=new_bom.name))

exploded_items = frappe.get_all(
"BOM Explosion Item", filters={"parent": root_bom.name}, fields=["item_code"]
)

exploded_items = [item.item_code for item in exploded_items]
expected_exploded_items = ["B-Item C", "B-Item G"]
self.assertEqual(sorted(exploded_items), sorted(expected_exploded_items))


def remove_bom(item_code):
boms = frappe.get_all("BOM", fields=["docstatus", "name"], filters={"item": item_code})

for row in boms:
if row.docstatus == 1:
frappe.get_doc("BOM", row.name).cancel()

frappe.delete_doc("BOM", row.name)


def update_cost_in_all_boms_in_test():
"""
Expand Down

0 comments on commit c2eeeec

Please sign in to comment.