Merge pull request #4023 from saurabh6790/error_report

[Fixes] Check Warehouse for Company Delete and Variant Attribute Value
diff --git a/erpnext/controllers/item_variant.py b/erpnext/controllers/item_variant.py
index 4edf52b..38536cf 100644
--- a/erpnext/controllers/item_variant.py
+++ b/erpnext/controllers/item_variant.py
@@ -60,8 +60,8 @@
 			if not (is_in_range and is_incremental):
 				frappe.throw(_("Value for Attribute {0} must be within the range of {1} to {2} in the increments of {3}")\
 					.format(attribute, from_range, to_range, increment), InvalidItemAttributeValueError)
-
-		elif value not in attribute_values[attribute]:
+				
+		elif value not in attribute_values.get(attribute, []):
 			frappe.throw(_("Value {0} for Attribute {1} does not exist in the list of valid Item Attribute Values").format(
 				value, attribute))
 
diff --git a/erpnext/setup/doctype/company/company.py b/erpnext/setup/doctype/company/company.py
index 804dc4d..785e7aa 100644
--- a/erpnext/setup/doctype/company/company.py
+++ b/erpnext/setup/doctype/company/company.py
@@ -207,12 +207,14 @@
 		frappe.defaults.clear_default("company", value=self.name)
 
 		# clear default accounts, warehouses from item
-		for f in ["default_warehouse", "website_warehouse"]:
-			frappe.db.sql("""update tabItem set %s=NULL where %s in (%s)"""
-				% (f, f, ', '.join(['%s']*len(warehouses))), tuple(warehouses))
+		if warehouses:
+			
+			for f in ["default_warehouse", "website_warehouse"]:
+				frappe.db.sql("""update tabItem set %s=NULL where %s in (%s)"""
+					% (f, f, ', '.join(['%s']*len(warehouses))), tuple(warehouses))
 
-		frappe.db.sql("""delete from `tabItem Reorder` where warehouse in (%s)"""
-			% ', '.join(['%s']*len(warehouses)), tuple(warehouses))
+			frappe.db.sql("""delete from `tabItem Reorder` where warehouse in (%s)"""
+				% ', '.join(['%s']*len(warehouses)), tuple(warehouses))
 
 		for f in ["income_account", "expense_account"]:
 			frappe.db.sql("""update tabItem set %s=NULL where %s in (%s)"""
diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py
index d8d21ff..6eadb83 100644
--- a/erpnext/stock/doctype/item/item.py
+++ b/erpnext/stock/doctype/item/item.py
@@ -341,10 +341,10 @@
 					frappe.throw(_("Please specify Attribute Value for attribute {0}").format(d.attribute))
 				args[d.attribute] = d.attribute_value
 
-			if self.get("__islocal"):
+			if self.variant_of:
 				# test this during insert because naming is based on item_code and we cannot use condition like self.name != variant
 				variant = get_variant(self.variant_of, args)
-				if variant:
+				if variant and self.get("__islocal"):
 					frappe.throw(_("Item variant {0} exists with same attributes").format(variant), ItemVariantExistsError)
 
 def validate_end_of_life(item_code, end_of_life=None, verbose=1):