fix: BOM name issue (backport #44586) (#44589)

fix: BOM name issue (#44586)

(cherry picked from commit d871e21a40)

Co-authored-by: rohitwaghchaure <rohitw1991@gmail.com>
This commit is contained in:
mergify[bot]
2024-12-07 12:58:34 +05:30
committed by GitHub
parent 0b268279cf
commit a5cc307417
2 changed files with 21 additions and 27 deletions

View File

@@ -253,6 +253,7 @@ frappe.ui.form.on("BOM", {
}; };
}, },
}); });
}
fields.push({ fields.push({
fieldtype: "Check", fieldtype: "Check",
@@ -260,7 +261,6 @@ frappe.ui.form.on("BOM", {
fieldname: "use_multi_level_bom", fieldname: "use_multi_level_bom",
default: 1, default: 1,
}); });
}
if (!skip_qty_field) { if (!skip_qty_field) {
fields.push({ fields.push({

View File

@@ -179,15 +179,7 @@ class BOM(WebsiteGenerator):
"BOM", filters={"name": search_key, "amended_from": ["is", "not set"]}, pluck="name" "BOM", filters={"name": search_key, "amended_from": ["is", "not set"]}, pluck="name"
) )
if not existing_boms: index = self.get_index_for_bom(existing_boms)
existing_boms = frappe.get_all(
"BOM", filters={"name": ("like", search_key), "amended_from": ["is", "not set"]}, pluck="name"
)
if existing_boms:
index = self.get_next_version_index(existing_boms)
else:
index = 1
prefix = self.doctype prefix = self.doctype
suffix = "%.3i" % index # convert index to string (1 -> "001") suffix = "%.3i" % index # convert index to string (1 -> "001")
@@ -205,21 +197,23 @@ class BOM(WebsiteGenerator):
name = f"{prefix}-{truncated_item_name}-{suffix}" name = f"{prefix}-{truncated_item_name}-{suffix}"
if frappe.db.exists("BOM", name): if frappe.db.exists("BOM", name):
conflicting_bom = frappe.get_doc("BOM", name) existing_boms = frappe.get_all(
"BOM", filters={"name": ("like", search_key), "amended_from": ["is", "not set"]}, pluck="name"
if conflicting_bom.item != self.item:
msg = _("A BOM with name {0} already exists for item {1}.").format(
frappe.bold(name), frappe.bold(conflicting_bom.item)
) )
frappe.throw( index = self.get_index_for_bom(existing_boms)
_("{0}{1} Did you rename the item? Please contact Administrator / Tech support").format( suffix = "%.3i" % index
msg, "<br>" name = f"{prefix}-{self.item}-{suffix}"
)
)
self.name = name self.name = name
def get_index_for_bom(self, existing_boms):
index = 1
if existing_boms:
index = self.get_next_version_index(existing_boms)
return index
@staticmethod @staticmethod
def get_next_version_index(existing_boms: list[str]) -> int: def get_next_version_index(existing_boms: list[str]) -> int:
# split by "/" and "-" # split by "/" and "-"