diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py
index deace33f343..4d5fabb9702 100644
--- a/erpnext/stock/doctype/item/item.py
+++ b/erpnext/stock/doctype/item/item.py
@@ -111,6 +111,7 @@ class Item(WebsiteGenerator):
self.synced_with_hub = 0
self.validate_has_variants()
+ self.validate_attributes_in_variants()
self.validate_stock_exists_for_template_item()
self.validate_attributes()
self.validate_variant_attributes()
@@ -806,6 +807,76 @@ class Item(WebsiteGenerator):
if frappe.db.exists("Item", {"variant_of": self.name}):
frappe.throw(_("Item has variants."))
+ def validate_attributes_in_variants(self):
+ if not self.has_variants or self.get("__islocal"):
+ return
+
+ old_doc = self.get_doc_before_save()
+ old_doc_attributes = set([attr.attribute for attr in old_doc.attributes])
+ own_attributes = [attr.attribute for attr in self.attributes]
+
+ # Check if old attributes were removed from the list
+ # Is old_attrs is a subset of new ones
+ # that means we need not check any changes
+ if old_doc_attributes.issubset(set(own_attributes)):
+ return
+
+ from collections import defaultdict
+
+ # get all item variants
+ items = [item["name"] for item in frappe.get_all("Item", {"variant_of": self.name})]
+
+ # get all deleted attributes
+ deleted_attribute = list(old_doc_attributes.difference(set(own_attributes)))
+
+ # fetch all attributes of these items
+ item_attributes = frappe.get_all(
+ "Item Variant Attribute",
+ filters={
+ "parent": ["in", items],
+ "attribute": ["in", deleted_attribute]
+ },
+ fields=["attribute", "parent"]
+ )
+ not_included = defaultdict(list)
+
+ for attr in item_attributes:
+ if attr["attribute"] not in own_attributes:
+ not_included[attr["parent"]].append(attr["attribute"])
+
+ if not len(not_included):
+ return
+
+ def body(docnames):
+ docnames.sort()
+ return "
".join(docnames)
+
+ def table_row(title, body):
+ return """
| {1} | +{2} | + + {3} +