From 4f559b6df24ac5d92c1d6ea3498892d29463611e Mon Sep 17 00:00:00 2001 From: Prateek Karamchandani <40106895+prateekkaramchandani@users.noreply.github.com> Date: Wed, 5 Mar 2025 16:37:42 +0530 Subject: [PATCH] fix: allow variant uom to differ from template uom (#45850) --- erpnext/stock/doctype/item/item.py | 16 +++++--- erpnext/stock/doctype/item/test_item.py | 39 +++++++++++++++++++ .../item_variant_settings.json | 9 ++++- .../item_variant_settings.py | 1 + 4 files changed, 58 insertions(+), 7 deletions(-) diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py index ee3aba14404..c800a93c66d 100644 --- a/erpnext/stock/doctype/item/item.py +++ b/erpnext/stock/doctype/item/item.py @@ -896,13 +896,17 @@ class Item(Document): for d in frappe.db.get_all("Item", filters={"variant_of": self.name}): check_stock_uom_with_bin(d.name, self.stock_uom) if self.variant_of: - template_uom = frappe.db.get_value("Item", self.variant_of, "stock_uom") - if template_uom != self.stock_uom: - frappe.throw( - _("Default Unit of Measure for Variant '{0}' must be same as in Template '{1}'").format( - self.stock_uom, template_uom + allow_different_uom = frappe.get_cached_value( + "Item Variant Settings", "Item Variant Settings", "allow_different_uom" + ) + if not allow_different_uom: + template_uom = frappe.db.get_value("Item", self.variant_of, "stock_uom") + if template_uom != self.stock_uom: + frappe.throw( + _( + "Default Unit of Measure for Variant '{0}' must be same as in Template '{1}'" + ).format(self.stock_uom, template_uom) ) - ) def validate_uom_conversion_factor(self): if self.uoms: diff --git a/erpnext/stock/doctype/item/test_item.py b/erpnext/stock/doctype/item/test_item.py index e17a8c8eada..d13456c249e 100644 --- a/erpnext/stock/doctype/item/test_item.py +++ b/erpnext/stock/doctype/item/test_item.py @@ -933,6 +933,45 @@ class TestItem(IntegrationTestCase): self.assertRaises(frappe.ValidationError, item_doc.save) + def test_variant_uom_mismatch_throws_error(self): + frappe.db.set_single_value("Item Variant Settings", "allow_different_uom", 0) + + template_item = frappe.get_doc( + { + "doctype": "Item", + "item_code": "_Test Template UOM", + "item_name": "_Test Template UOM", + "item_group": "_Test Item Group", + "stock_uom": "Kg", + "is_stock_item": 1, + "has_variants": 1, + "attributes": [ + {"attribute": "Test Size"}, + ], + } + ).insert() + + with self.assertRaises(frappe.ValidationError) as ve: + frappe.get_doc( + { + "doctype": "Item", + "item_code": "_Test Variant UOM", + "item_name": "_Test Variant UOM", + "item_group": "_Test Item Group", + "stock_uom": "Litre", + "is_stock_item": 1, + "variant_of": template_item.name, + "attributes": [ + {"attribute": "Test Size", "attribute_value": "Small"}, + ], + } + ).insert() + + self.assertTrue( + "must be same as in Template" in str(ve.exception), + msg="Different Variant UOM should not be allowed when `allow_different_uom` is disabled.", + ) + def set_item_variant_settings(fields): doc = frappe.get_doc("Item Variant Settings") diff --git a/erpnext/stock/doctype/item_variant_settings/item_variant_settings.json b/erpnext/stock/doctype/item_variant_settings/item_variant_settings.json index 8aea8a46778..7d7a2b9fc3d 100644 --- a/erpnext/stock/doctype/item_variant_settings/item_variant_settings.json +++ b/erpnext/stock/doctype/item_variant_settings/item_variant_settings.json @@ -8,6 +8,7 @@ "section_break_3", "do_not_update_variants", "allow_rename_attribute_value", + "allow_different_uom", "copy_fields_to_variant", "fields" ], @@ -40,11 +41,17 @@ "fieldtype": "Table", "label": "Fields", "options": "Variant Field" + }, + { + "default": "0", + "fieldname": "allow_different_uom", + "fieldtype": "Check", + "label": "Allow Variant UOM to be different from Template UOM" } ], "issingle": 1, "links": [], - "modified": "2024-03-27 13:09:56.095684", + "modified": "2025-02-10 16:13:47.435148", "modified_by": "Administrator", "module": "Stock", "name": "Item Variant Settings", diff --git a/erpnext/stock/doctype/item_variant_settings/item_variant_settings.py b/erpnext/stock/doctype/item_variant_settings/item_variant_settings.py index 277042c9d6f..dbaf35fbf4e 100644 --- a/erpnext/stock/doctype/item_variant_settings/item_variant_settings.py +++ b/erpnext/stock/doctype/item_variant_settings/item_variant_settings.py @@ -18,6 +18,7 @@ class ItemVariantSettings(Document): from erpnext.stock.doctype.variant_field.variant_field import VariantField + allow_different_uom: DF.Check allow_rename_attribute_value: DF.Check do_not_update_variants: DF.Check fields: DF.Table[VariantField]