Validate stock exists against template item (#11305)

This commit is contained in:
rohitwaghchaure 2017-10-25 11:45:51 +05:30 committed by Nabin Hait
parent 6c06e700d6
commit 311823aca1
3 changed files with 27 additions and 0 deletions

View File

@ -103,6 +103,11 @@ frappe.ui.form.on("Item", {
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){

View File

@ -17,6 +17,7 @@ from erpnext.controllers.item_variant import (get_variant, copy_attributes_to_va
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 @@ class Item(WebsiteGenerator):
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 @@ class Item(WebsiteGenerator):
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 @@ class Item(WebsiteGenerator):
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)

View File

@ -8,6 +8,7 @@ import frappe
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 @@ class TestItem(unittest.TestCase):
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)