Validate stock exists against template item (#11305)
diff --git a/erpnext/stock/doctype/item/item.js b/erpnext/stock/doctype/item/item.js
index 03b93c0..d8f1f30 100644
--- a/erpnext/stock/doctype/item/item.js
+++ b/erpnext/stock/doctype/item/item.js
@@ -103,6 +103,11 @@
frappe.set_route("Form", "Item Variant Settings");
}, __("View"));
}
+
+ if(frm.doc.__onload && frm.doc.__onload.stock_exists) {
+ // Hide variants section if stock exists
+ frm.toggle_display("variants_section", 0);
+ }
},
validate: function(frm){
diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py
index a810665..41a23a4 100644
--- a/erpnext/stock/doctype/item/item.py
+++ b/erpnext/stock/doctype/item/item.py
@@ -17,6 +17,7 @@
make_variant_item_code, validate_item_variant_attributes, ItemVariantExistsError)
class DuplicateReorderRows(frappe.ValidationError): pass
+class StockExistsForTemplate(frappe.ValidationError): pass
class Item(WebsiteGenerator):
website = frappe._dict(
@@ -28,11 +29,15 @@
def onload(self):
super(Item, self).onload()
+
self.set_onload('sle_exists', self.check_if_sle_exists())
if self.is_fixed_asset:
asset = frappe.db.get_all("Asset", filters={"item_code": self.name, "docstatus": 1}, limit=1)
self.set_onload("asset_exists", True if asset else False)
+ if frappe.db.get_value('Stock Ledger Entry', {'item_code': self.name}):
+ self.set_onload('stock_exists', True)
+
def autoname(self):
if frappe.db.get_default("item_naming_by")=="Naming Series":
if self.variant_of:
@@ -85,6 +90,7 @@
self.synced_with_hub = 0
self.validate_has_variants()
+ self.validate_stock_exists_for_template_item()
self.validate_attributes()
self.validate_variant_attributes()
self.validate_website_image()
@@ -631,6 +637,12 @@
if frappe.db.exists("Item", {"variant_of": self.name}):
frappe.throw(_("Item has variants."))
+ def validate_stock_exists_for_template_item(self):
+ if self.has_variants and \
+ frappe.db.get_value('Stock Ledger Entry', {'item_code': self.name}):
+ frappe.throw(_("As stock exists against an item {0}, you can not enable has variants property")
+ .format(self.name), StockExistsForTemplate)
+
def validate_uom(self):
if not self.get("__islocal"):
check_stock_uom_with_bin(self.name, self.stock_uom)
diff --git a/erpnext/stock/doctype/item/test_item.py b/erpnext/stock/doctype/item/test_item.py
index c3f399a..673d0f9 100644
--- a/erpnext/stock/doctype/item/test_item.py
+++ b/erpnext/stock/doctype/item/test_item.py
@@ -8,6 +8,7 @@
from frappe.test_runner import make_test_records
from erpnext.controllers.item_variant import (create_variant, ItemVariantExistsError,
InvalidItemAttributeValueError, get_variant)
+from erpnext.stock.doctype.item.item import StockExistsForTemplate
from frappe.model.rename_doc import rename_doc
from erpnext.stock.doctype.stock_entry.stock_entry_utils import make_stock_entry
@@ -263,6 +264,15 @@
self.assertEquals(variant.manufacturer, 'MSG1')
self.assertEquals(variant.manufacturer_part_no, '007')
+ def test_stock_exists_against_template_item(self):
+ stock_item = frappe.get_all('Stock Ledger Entry', fields = ["item_code"], limit=1)
+ if stock_item:
+ item_code = stock_item[0].item_code
+
+ item_doc = frappe.get_doc('Item', item_code)
+ item_doc.has_variants = 1
+ self.assertRaises(StockExistsForTemplate, item_doc.save)
+
def set_item_variant_settings(fields):
doc = frappe.get_doc('Item Variant Settings')
doc.set('fields', fields)