-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30520 from frappe/mergify/bp/version-13-hotfix/pr…
…-30146 refactor: Add exception handling in background job within BOM Update Tool (backport #30146)
- Loading branch information
Showing
11 changed files
with
485 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
8 changes: 8 additions & 0 deletions
8
erpnext/manufacturing/doctype/bom_update_log/bom_update_log.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and contributors | ||
// For license information, please see license.txt | ||
|
||
frappe.ui.form.on('BOM Update Log', { | ||
// refresh: function(frm) { | ||
|
||
// } | ||
}); |
109 changes: 109 additions & 0 deletions
109
erpnext/manufacturing/doctype/bom_update_log/bom_update_log.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
{ | ||
"actions": [], | ||
"autoname": "BOM-UPDT-LOG-.#####", | ||
"creation": "2022-03-16 14:23:35.210155", | ||
"description": "BOM Update Tool Log with job status maintained", | ||
"doctype": "DocType", | ||
"editable_grid": 1, | ||
"engine": "InnoDB", | ||
"field_order": [ | ||
"current_bom", | ||
"new_bom", | ||
"column_break_3", | ||
"update_type", | ||
"status", | ||
"error_log", | ||
"amended_from" | ||
], | ||
"fields": [ | ||
{ | ||
"fieldname": "current_bom", | ||
"fieldtype": "Link", | ||
"in_list_view": 1, | ||
"in_standard_filter": 1, | ||
"label": "Current BOM", | ||
"options": "BOM" | ||
}, | ||
{ | ||
"fieldname": "new_bom", | ||
"fieldtype": "Link", | ||
"in_list_view": 1, | ||
"in_standard_filter": 1, | ||
"label": "New BOM", | ||
"options": "BOM" | ||
}, | ||
{ | ||
"fieldname": "column_break_3", | ||
"fieldtype": "Column Break" | ||
}, | ||
{ | ||
"fieldname": "update_type", | ||
"fieldtype": "Select", | ||
"in_list_view": 1, | ||
"label": "Update Type", | ||
"options": "Replace BOM\nUpdate Cost" | ||
}, | ||
{ | ||
"fieldname": "status", | ||
"fieldtype": "Select", | ||
"label": "Status", | ||
"options": "Queued\nIn Progress\nCompleted\nFailed" | ||
}, | ||
{ | ||
"fieldname": "amended_from", | ||
"fieldtype": "Link", | ||
"label": "Amended From", | ||
"no_copy": 1, | ||
"options": "BOM Update Log", | ||
"print_hide": 1, | ||
"read_only": 1 | ||
}, | ||
{ | ||
"fieldname": "error_log", | ||
"fieldtype": "Link", | ||
"label": "Error Log", | ||
"options": "Error Log" | ||
} | ||
], | ||
"in_create": 1, | ||
"index_web_pages_for_search": 1, | ||
"is_submittable": 1, | ||
"links": [], | ||
"modified": "2022-03-31 12:51:44.885102", | ||
"modified_by": "Administrator", | ||
"module": "Manufacturing", | ||
"name": "BOM Update Log", | ||
"naming_rule": "Expression (old style)", | ||
"owner": "Administrator", | ||
"permissions": [ | ||
{ | ||
"create": 1, | ||
"delete": 1, | ||
"email": 1, | ||
"export": 1, | ||
"print": 1, | ||
"read": 1, | ||
"report": 1, | ||
"role": "System Manager", | ||
"share": 1, | ||
"submit": 1, | ||
"write": 1 | ||
}, | ||
{ | ||
"create": 1, | ||
"email": 1, | ||
"export": 1, | ||
"print": 1, | ||
"read": 1, | ||
"report": 1, | ||
"role": "Manufacturing Manager", | ||
"share": 1, | ||
"submit": 1, | ||
"write": 1 | ||
} | ||
], | ||
"sort_field": "modified", | ||
"sort_order": "DESC", | ||
"states": [], | ||
"track_changes": 1 | ||
} |
165 changes: 165 additions & 0 deletions
165
erpnext/manufacturing/doctype/bom_update_log/bom_update_log.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and contributors | ||
# For license information, please see license.txt | ||
from typing import Dict, List, Optional | ||
|
||
import frappe | ||
from frappe import _ | ||
from frappe.model.document import Document | ||
from frappe.utils import cstr, flt | ||
from typing_extensions import Literal | ||
|
||
from erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool import update_cost | ||
|
||
|
||
class BOMMissingError(frappe.ValidationError): | ||
pass | ||
|
||
|
||
class BOMUpdateLog(Document): | ||
def validate(self): | ||
if self.update_type == "Replace BOM": | ||
self.validate_boms_are_specified() | ||
self.validate_same_bom() | ||
self.validate_bom_items() | ||
|
||
self.status = "Queued" | ||
|
||
def validate_boms_are_specified(self): | ||
if self.update_type == "Replace BOM" and not (self.current_bom and self.new_bom): | ||
frappe.throw( | ||
msg=_("Please mention the Current and New BOM for replacement."), | ||
title=_("Mandatory"), | ||
exc=BOMMissingError, | ||
) | ||
|
||
def validate_same_bom(self): | ||
if cstr(self.current_bom) == cstr(self.new_bom): | ||
frappe.throw(_("Current BOM and New BOM can not be same")) | ||
|
||
def validate_bom_items(self): | ||
current_bom_item = frappe.db.get_value("BOM", self.current_bom, "item") | ||
new_bom_item = frappe.db.get_value("BOM", self.new_bom, "item") | ||
|
||
if current_bom_item != new_bom_item: | ||
frappe.throw(_("The selected BOMs are not for the same item")) | ||
|
||
def on_submit(self): | ||
if frappe.flags.in_test: | ||
return | ||
|
||
if self.update_type == "Replace BOM": | ||
boms = {"current_bom": self.current_bom, "new_bom": self.new_bom} | ||
frappe.enqueue( | ||
method="erpnext.manufacturing.doctype.bom_update_log.bom_update_log.run_bom_job", | ||
doc=self, | ||
boms=boms, | ||
timeout=40000, | ||
) | ||
else: | ||
frappe.enqueue( | ||
method="erpnext.manufacturing.doctype.bom_update_log.bom_update_log.run_bom_job", | ||
doc=self, | ||
update_type="Update Cost", | ||
timeout=40000, | ||
) | ||
|
||
|
||
def replace_bom(boms: Dict) -> None: | ||
"""Replace current BOM with new BOM in parent BOMs.""" | ||
current_bom = boms.get("current_bom") | ||
new_bom = boms.get("new_bom") | ||
|
||
unit_cost = get_new_bom_unit_cost(new_bom) | ||
update_new_bom_in_bom_items(unit_cost, current_bom, new_bom) | ||
|
||
frappe.cache().delete_key("bom_children") | ||
parent_boms = get_parent_boms(new_bom) | ||
|
||
for bom in parent_boms: | ||
bom_obj = frappe.get_doc("BOM", bom) | ||
# this is only used for versioning and we do not want | ||
# to make separate db calls by using load_doc_before_save | ||
# which proves to be expensive while doing bulk replace | ||
bom_obj._doc_before_save = bom_obj | ||
bom_obj.update_exploded_items() | ||
bom_obj.calculate_cost() | ||
bom_obj.update_parent_cost() | ||
bom_obj.db_update() | ||
if bom_obj.meta.get("track_changes") and not bom_obj.flags.ignore_version: | ||
bom_obj.save_version() | ||
|
||
|
||
def update_new_bom_in_bom_items(unit_cost: float, current_bom: str, new_bom: str) -> None: | ||
bom_item = frappe.qb.DocType("BOM Item") | ||
( | ||
frappe.qb.update(bom_item) | ||
.set(bom_item.bom_no, new_bom) | ||
.set(bom_item.rate, unit_cost) | ||
.set(bom_item.amount, (bom_item.stock_qty * unit_cost)) | ||
.where( | ||
(bom_item.bom_no == current_bom) & (bom_item.docstatus < 2) & (bom_item.parenttype == "BOM") | ||
) | ||
).run() | ||
|
||
|
||
def get_parent_boms(new_bom: str, bom_list: Optional[List] = None) -> List: | ||
bom_list = bom_list or [] | ||
bom_item = frappe.qb.DocType("BOM Item") | ||
|
||
parents = ( | ||
frappe.qb.from_(bom_item) | ||
.select(bom_item.parent) | ||
.where((bom_item.bom_no == new_bom) & (bom_item.docstatus < 2) & (bom_item.parenttype == "BOM")) | ||
.run(as_dict=True) | ||
) | ||
|
||
for d in parents: | ||
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) | ||
get_parent_boms(d.parent, bom_list) | ||
|
||
return list(set(bom_list)) | ||
|
||
|
||
def get_new_bom_unit_cost(new_bom: str) -> float: | ||
bom = frappe.qb.DocType("BOM") | ||
new_bom_unitcost = ( | ||
frappe.qb.from_(bom).select(bom.total_cost / bom.quantity).where(bom.name == new_bom).run() | ||
) | ||
|
||
return flt(new_bom_unitcost[0][0]) | ||
|
||
|
||
def run_bom_job( | ||
doc: "BOMUpdateLog", | ||
boms: Optional[Dict[str, str]] = None, | ||
update_type: Literal["Replace BOM", "Update Cost"] = "Replace BOM", | ||
) -> None: | ||
try: | ||
doc.db_set("status", "In Progress") | ||
if not frappe.flags.in_test: | ||
frappe.db.commit() | ||
|
||
frappe.db.auto_commit_on_many_writes = 1 | ||
|
||
boms = frappe._dict(boms or {}) | ||
|
||
if update_type == "Replace BOM": | ||
replace_bom(boms) | ||
else: | ||
update_cost() | ||
|
||
doc.db_set("status", "Completed") | ||
|
||
except Exception: | ||
frappe.db.rollback() | ||
error_log = frappe.log_error(message=frappe.get_traceback(), title=_("BOM Update Tool Error")) | ||
|
||
doc.db_set("status", "Failed") | ||
doc.db_set("error_log", error_log.name) | ||
|
||
finally: | ||
frappe.db.auto_commit_on_many_writes = 0 | ||
frappe.db.commit() # nosemgrep |
13 changes: 13 additions & 0 deletions
13
erpnext/manufacturing/doctype/bom_update_log/bom_update_log_list.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
frappe.listview_settings['BOM Update Log'] = { | ||
add_fields: ["status"], | ||
get_indicator: function(doc) { | ||
let status_map = { | ||
"Queued": "orange", | ||
"In Progress": "blue", | ||
"Completed": "green", | ||
"Failed": "red" | ||
}; | ||
|
||
return [__(doc.status), status_map[doc.status], "status,=," + doc.status]; | ||
} | ||
}; |
Oops, something went wrong.