Merge pull request #3386 from neilLasrado/item-varients

[redesign] Manage Item varients
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index 0f32a6d..80965ec 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -168,4 +168,4 @@
 erpnext.patches.v5_0.update_material_transferred_for_manufacturing_again
 erpnext.patches.v5_0.index_on_account_and_gl_entry
 execute:frappe.db.sql("""delete from `tabProject Task`""")
-
+erpnext.patches.v5_0.item_variants
diff --git a/erpnext/patches/v5_0/item_variants.py b/erpnext/patches/v5_0/item_variants.py
new file mode 100644
index 0000000..62e9ac9
--- /dev/null
+++ b/erpnext/patches/v5_0/item_variants.py
@@ -0,0 +1,16 @@
+import frappe
+
+def execute():
+	frappe.reload_doctype("Item")
+	for dt in ["manage_variants", "manage_variants_item", "variant_attribute"]:
+		frappe.reload_doc("stock", "doctype", dt)
+
+	for d in  frappe.get_list("Item", filters={"has_variants":1}):
+		manage_variant = frappe.new_doc("Manage Variants")
+		manage_variant.item_code = d.name
+		manage_variant.attributes = frappe.db.sql("select item_attribute as attribute, item_attribute_value as attribute_value \
+			from `tabItem Variant` where parent = %s", d.name, as_dict=1)
+		if manage_variant.attributes:
+			manage_variant.generate_combinations()
+			manage_variant.create_variants()
+	frappe.delete_doc("DocType", "Item Variant")
\ No newline at end of file
diff --git a/erpnext/projects/doctype/project/test_project.py b/erpnext/projects/doctype/project/test_project.py
index f69ce80..f1be544 100644
--- a/erpnext/projects/doctype/project/test_project.py
+++ b/erpnext/projects/doctype/project/test_project.py
@@ -5,3 +5,4 @@
 
 import frappe
 test_records = frappe.get_test_records('Project')
+test_ignore = ["Sales Order"]
diff --git a/erpnext/stock/doctype/item/item.js b/erpnext/stock/doctype/item/item.js
index 5da3d35..58b1adb 100644
--- a/erpnext/stock/doctype/item/item.js
+++ b/erpnext/stock/doctype/item/item.js
@@ -5,40 +5,6 @@
 
 frappe.ui.form.on("Item", {
 	onload: function(frm) {
-		var df = frappe.meta.get_docfield("Item Variant", "item_attribute_value");
-		df.on_make = function(field) {
-			$(field.input_area).addClass("ui-front");
-			field.$input.autocomplete({
-				minLength: 0,
-				minChars: 0,
-				source: function(request, response) {
-					frappe.call({
-						method:"frappe.client.get_list",
-						args:{
-							doctype:"Item Attribute Value",
-							filters: [
-								["parent","=", field.doc.item_attribute],
-								["attribute_value", "like", request.term + "%"]
-							],
-							fields: ["attribute_value"]
-						},
-						callback: function(r) {
-							response($.map(r.message, function(d) { return d.attribute_value; }));
-						}
-					});
-				},
-				select: function(event, ui) {
-					field.$input.val(ui.item.value);
-					field.$input.trigger("change");
-				},
-				focus: function( event, ui ) {
-					if(ui.item.action) {
-						return false;
-					}
-				},
-			});
-		}
-
 		erpnext.item.setup_queries(frm);
 	},
 
@@ -114,8 +80,14 @@
 			method: "copy_specification_from_item_group"
 		});
 	},
+	
 	is_stock_item: function(frm) {
 		erpnext.item.toggle_reqd(frm);
+	},
+	
+	manage_variants: function(frm) {
+		frappe.route_options = {"item_code": frm.doc.name };
+		frappe.set_route("List", "Manage Variants");
 	}
 });
 
diff --git a/erpnext/stock/doctype/item/item.json b/erpnext/stock/doctype/item/item.json
index 8b10319..acf2764 100644
--- a/erpnext/stock/doctype/item/item.json
+++ b/erpnext/stock/doctype/item/item.json
@@ -12,7 +12,7 @@
   {
    "fieldname": "name_and_description_section", 
    "fieldtype": "Section Break", 
-   "label": "Name and Description", 
+   "label": "", 
    "no_copy": 0, 
    "oldfieldtype": "Section Break", 
    "options": "icon-flag", 
@@ -167,16 +167,17 @@
    "search_index": 0
   }, 
   {
-   "depends_on": "eval:!!!doc.variant_of", 
+   "depends_on": "eval:!doc.variant_of", 
    "fieldname": "variants_section", 
    "fieldtype": "Section Break", 
-   "label": "Variants", 
+   "label": "Variant", 
    "permlevel": 0, 
    "precision": ""
   }, 
   {
    "default": "0", 
-   "description": "Automatically set. If this item has variants, then it cannot be selected in sales orders etc.", 
+   "depends_on": "", 
+   "description": "If this item has variants, then it cannot be selected in sales orders etc.", 
    "fieldname": "has_variants", 
    "fieldtype": "Check", 
    "label": "Has Variants", 
@@ -186,16 +187,38 @@
    "read_only": 0
   }, 
   {
-   "depends_on": "has_variants", 
-   "description": "A new variant (Item) will be created for each attribute value combination", 
-   "fieldname": "variants", 
-   "fieldtype": "Table", 
-   "label": "Variants", 
-   "options": "Item Variant", 
+   "fieldname": "column_break_18", 
+   "fieldtype": "Column Break", 
    "permlevel": 0, 
    "precision": ""
   }, 
   {
+   "depends_on": "has_variants", 
+   "fieldname": "manage_variants", 
+   "fieldtype": "Button", 
+   "label": "Manage Variants", 
+   "permlevel": 0, 
+   "precision": ""
+  }, 
+  {
+   "fieldname": "section_break_20", 
+   "fieldtype": "Section Break", 
+   "permlevel": 0, 
+   "precision": ""
+  }, 
+  {
+   "depends_on": "variant_of", 
+   "fieldname": "attributes", 
+   "fieldtype": "Table", 
+   "hidden": 0, 
+   "label": "Attributes", 
+   "no_copy": 1, 
+   "options": "Variant Attribute", 
+   "permlevel": 0, 
+   "precision": "", 
+   "read_only": 0
+  }, 
+  {
    "fieldname": "inventory", 
    "fieldtype": "Section Break", 
    "label": "Inventory", 
@@ -877,9 +900,9 @@
   }
  ], 
  "icon": "icon-tag", 
- "idx": 1, 
+ "idx": 1,
  "max_attachments": 1, 
- "modified": "2015-06-26 17:20:18.204558", 
+ "modified": "2015-07-01 17:20:18.204558", 
  "modified_by": "Administrator", 
  "module": "Stock", 
  "name": "Item", 
diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py
index aa463ee..a24fc04 100644
--- a/erpnext/stock/doctype/item/item.py
+++ b/erpnext/stock/doctype/item/item.py
@@ -9,10 +9,9 @@
 from erpnext.setup.doctype.item_group.item_group import invalidate_cache_for, get_parent_item_groups
 from frappe.website.render import clear_cache
 from frappe.website.doctype.website_slideshow.website_slideshow import get_slideshow
-import copy
+from erpnext.stock.doctype.manage_variants.manage_variants import update_variant
 
 class WarehouseNotSet(frappe.ValidationError): pass
-class DuplicateVariant(frappe.ValidationError): pass
 class ItemTemplateCannotHaveStock(frappe.ValidationError): pass
 
 class Item(WebsiteGenerator):
@@ -48,9 +47,6 @@
 		if self.image and not self.website_image:
 			self.website_image = self.image
 
-		if self.variant_of:
-			self.copy_attributes_to_variant(frappe.get_doc("Item", self.variant_of), self)
-			
 		self.check_warehouse_is_set_for_stock_item()
 		self.check_stock_uom_with_bin()
 		self.add_default_uom_in_conversion_factor_table()
@@ -63,9 +59,10 @@
 		self.cant_change()
 		self.validate_reorder_level()
 		self.validate_warehouse_for_reorder()
-		self.validate_variants()
 		self.update_item_desc()
 		self.synced_with_hub = 0
+		self.validate_has_variants()
+		self.validate_stock_for_template_must_be_zero()
 
 		if not self.get("__islocal"):
 			self.old_item_group = frappe.db.get_value(self.doctype, self.name, "item_group")
@@ -77,7 +74,7 @@
 		invalidate_cache_for_item(self)
 		self.validate_name_with_item_group()
 		self.update_item_price()
-		self.sync_variants()
+		self.update_variants()
 
 	def get_context(self, context):
 		context["parent_groups"] = get_parent_item_groups(self.item_group) + \
@@ -133,142 +130,6 @@
 			if not matched:
 				frappe.throw(_("Default Unit of Measure can not be changed directly because you have already made some transaction(s) with another UOM. To change default UOM, use 'UOM Replace Utility' tool under Stock module."))
 
-	def validate_variants(self):
-		self.validate_variants_are_unique()
-		self.validate_stock_for_template_must_be_zero()
-
-	def validate_stock_for_template_must_be_zero(self):
-		if self.has_variants:
-			stock_in = frappe.db.sql_list("""select warehouse from tabBin
-				where item_code=%s and ifnull(actual_qty, 0) > 0""", self.name)
-			if stock_in:
-				frappe.throw(_("Item Template cannot have stock and varaiants. Please remove stock from warehouses {0}").format(", ".join(stock_in)),
-					ItemTemplateCannotHaveStock)
-
-	def validate_variants_are_unique(self):
-		if not self.has_variants:
-			self.variants = []
-			return
-
-		if self.variants:
-			if self.variant_of:
-				frappe.throw(_("Item cannot be a variant of a variant"))
-	
-			variants, attributes = [], {}
-			for d in self.variants:
-				key = (d.item_attribute, d.item_attribute_value)
-				if key in variants:
-					frappe.throw(_("{0} {1} is entered more than once in Item Variants table")
-						.format(d.item_attribute, d.item_attribute_value), DuplicateVariant)
-				variants.append(key)
-
-				attributes.setdefault(d.item_attribute, [t.attribute_value for t in frappe.db.get_all("Item Attribute Value",
-					fields=["attribute_value"],	filters={"parent": d.item_attribute })])
-				
-				if d.item_attribute_value not in attributes.get(d.item_attribute):
-					frappe.throw(_("Attribute value {0} does not exist in Item Attribute Master.").format(d.item_attribute_value))
-		else:
-			frappe.throw(_("Please enter atleast one attribute row in Item Variants table"))
-
-	def sync_variants(self):
-		variant_item_codes = self.get_variant_item_codes()
-
-		# delete missing variants
-		existing_variants = [d.name for d in frappe.get_all("Item",
-			filters={"variant_of":self.name})]
-
-		updated, deleted = [], []
-		for existing_variant in existing_variants:
-			if existing_variant not in variant_item_codes:
-				frappe.delete_doc("Item", existing_variant)
-				deleted.append(existing_variant)
-			else:
-				self.update_variant(existing_variant)
-				updated.append(existing_variant)
-
-		inserted = []
-		for item_code in variant_item_codes:
-			if item_code not in existing_variants:
-				self.make_variant(item_code)
-				inserted.append(item_code)
-
-		if inserted:
-			frappe.msgprint(_("Item Variants {0} created").format(", ".join(inserted)))
-
-		if updated:
-			frappe.msgprint(_("Item Variants {0} updated").format(", ".join(updated)))
-
-		if deleted:
-			frappe.msgprint(_("Item Variants {0} deleted").format(", ".join(deleted)))
-
-	def get_variant_item_codes(self):
-		"""Get all possible suffixes for variants"""
-		if not self.variants:
-			return []
-
-		self.variant_attributes = {}
-		variant_dict = {}
-		variant_item_codes = []
-
-		for d in self.variants:
-			variant_dict.setdefault(d.item_attribute, []).append(d.item_attribute_value)
-
-		all_attributes = [d.name for d in frappe.get_all("Item Attribute", order_by = "priority asc")]
-
-		# sort attributes by their priority
-		attributes = filter(None, map(lambda d: d if d in variant_dict else None, all_attributes))
-
-		def add_attribute_suffixes(item_code, my_attributes, attributes):
-			attr = frappe.get_doc("Item Attribute", attributes[0])
-			for value in attr.item_attribute_values:
-				if value.attribute_value in variant_dict[attr.name]:
-					_my_attributes = copy.deepcopy(my_attributes)
-					_my_attributes.append([attr.name, value.attribute_value])
-					if len(attributes) > 1:
-						add_attribute_suffixes(item_code + "-" + value.abbr, _my_attributes, attributes[1:])
-					else:
-						variant_item_codes.append(item_code + "-" + value.abbr)
-						self.variant_attributes[item_code + "-" + value.abbr] = _my_attributes
-
-		add_attribute_suffixes(self.name, [], attributes)
-
-		return variant_item_codes
-
-	def make_variant(self, item_code):
-		item = frappe.new_doc("Item")
-		item.item_code = item_code
-		self.copy_attributes_to_variant(self, item, insert=True)
-		item.insert()
-
-	def update_variant(self, item_code):
-		item = frappe.get_doc("Item", item_code)
-		item.item_code = item_code
-		self.copy_attributes_to_variant(self, item)
-		item.save()
-
-	def copy_attributes_to_variant(self, template, variant, insert=False):
-		from frappe.model import no_value_fields
-		for field in self.meta.fields:
-			if field.fieldtype not in no_value_fields and (insert or not field.no_copy)\
-				and field.fieldname not in ("item_code", "item_name"):
-				if variant.get(field.fieldname) != template.get(field.fieldname):
-					variant.set(field.fieldname, template.get(field.fieldname))
-					variant.__dirty = True
-
-		variant.description += "\n"
-
-		if not getattr(template, "variant_attributes", None):
-			template.get_variant_item_codes()
-
-		for attr in template.variant_attributes[variant.item_code]:
-			variant.description += "<p>" + attr[0] + ": " + attr[1] + "</p>"
-
-		variant.item_name = self.item_name + variant.item_code[len(self.name):]
-
-		variant.variant_of = template.name
-		variant.has_variants = 0
-		variant.show_in_website = 0
-
 	def update_template_tables(self):
 		template = frappe.get_doc("Item", self.variant_of)
 
@@ -361,7 +222,8 @@
 				vals.has_batch_no != self.has_batch_no or
 				cstr(vals.valuation_method) != cstr(self.valuation_method)):
 					if self.check_if_sle_exists() == "exists":
-						frappe.throw(_("As there are existing stock transactions for this item, you can not change the values of 'Has Serial No', 'Has Batch No', 'Is Stock Item' and 'Valuation Method'"))
+						frappe.throw(_("As there are existing stock transactions for this item, \
+							you can not change the values of 'Has Serial No', 'Has Batch No', 'Is Stock Item' and 'Valuation Method'"))
 
 	def validate_reorder_level(self):
 		if cint(self.apply_warehouse_wise_reorder_level):
@@ -460,9 +322,33 @@
 	def update_item_desc(self):
 		if frappe.db.get_value('BOM',self.name, 'description') != self.description:
 			frappe.db.sql("""update `tabBOM` set description = %s where item = %s and docstatus < 2""",(self.description, self.name))
-			frappe.db.sql("""update `tabBOM Item` set description = %s where item_code = %s and docstatus < 2""",(self.description, self.name))
-			frappe.db.sql("""update `tabBOM Explosion Item` set description = %s where item_code = %s and docstatus < 2""",(self.description, self.name))
+			frappe.db.sql("""update `tabBOM Item` set description = %s where 
+				item_code = %s and docstatus < 2""",(self.description, self.name))
+			frappe.db.sql("""update `tabBOM Explosion Item` set description = %s where 
+				item_code = %s and docstatus < 2""",(self.description, self.name))
+				
+	def update_variants(self):
+		if self.has_variants:
+			updated = []
+			variants = frappe.db.get_all("Item", fields=["item_code"], filters={"variant_of": self.name })
+			for d in variants:
+				update_variant(self.name, d)
+				updated.append(d.item_code)
+			frappe.msgprint(_("Item Variants {0} updated").format(", ".join(updated)))
+				
+	def validate_has_variants(self):
+		if not self.has_variants and frappe.db.get_value("Item", self.name, "has_variants"):
+			if frappe.db.exists("Item", {"variant_of": self.name}):
+				frappe.throw(_("Item has variants."))
 
+	def validate_stock_for_template_must_be_zero(self):
+		if self.has_variants:
+			stock_in = frappe.db.sql_list("""select warehouse from tabBin
+				where item_code=%s and ifnull(actual_qty, 0) > 0""", self.name)
+			if stock_in:
+				frappe.throw(_("Item Template cannot have stock and varaiants. Please remove \
+					stock from warehouses {0}").format(", ".join(stock_in)), ItemTemplateCannotHaveStock)
+	
 def validate_end_of_life(item_code, end_of_life=None, verbose=1):
 	if not end_of_life:
 		end_of_life = frappe.db.get_value("Item", item_code, "end_of_life")
@@ -567,3 +453,4 @@
 
 	if doc.get("old_item_group") and doc.get("old_item_group") != doc.item_group:
 		invalidate_cache_for(doc, doc.old_item_group)
+
diff --git a/erpnext/stock/doctype/item/test_item.py b/erpnext/stock/doctype/item/test_item.py
index 02ff714..9cf3c07 100644
--- a/erpnext/stock/doctype/item/test_item.py
+++ b/erpnext/stock/doctype/item/test_item.py
@@ -6,7 +6,7 @@
 import frappe
 
 from frappe.test_runner import make_test_records
-from erpnext.stock.doctype.item.item import WarehouseNotSet, DuplicateVariant, ItemTemplateCannotHaveStock
+from erpnext.stock.doctype.item.item import WarehouseNotSet, ItemTemplateCannotHaveStock
 from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry
 
 test_ignore = ["BOM"]
@@ -20,60 +20,14 @@
 			item.insert()
 		else:
 			item = frappe.get_doc("Item", item_code)
-
 		return item
-
-	def test_duplicate_variant(self):
-		item = frappe.copy_doc(test_records[11])
-		item.append("variants", {"item_attribute": "Test Size", "item_attribute_value": "Small"})
-		self.assertRaises(DuplicateVariant, item.insert)
-
+	
 	def test_template_cannot_have_stock(self):
-		item = self.get_item(10)
-		
-		se = make_stock_entry(item_code=item.name, target="Stores - _TC", qty=1, incoming_rate=1)
-
-		item.has_variants = 1
-		item.append("variants", {"item_attribute": "Test Size", "item_attribute_value": "Small"})
-		
-		self.assertRaises(ItemTemplateCannotHaveStock, item.save)
-
-	def test_variant_item_codes(self):
-		item = self.get_item(11)
-
-		variants = ['_Test Variant Item-S', '_Test Variant Item-M', '_Test Variant Item-L']
-		self.assertEqual(item.get_variant_item_codes(), variants)
-		for v in variants:
-			self.assertTrue(frappe.db.get_value("Item", {"variant_of": item.name, "name": v}))
-
-		item.append("variants", {"item_attribute": "Test Colour", "item_attribute_value": "Red"})
-		item.append("variants", {"item_attribute": "Test Colour", "item_attribute_value": "Blue"})
-		item.append("variants", {"item_attribute": "Test Colour", "item_attribute_value": "Green"})
-
-		self.assertEqual(item.get_variant_item_codes(), ['_Test Variant Item-S-R',
-			'_Test Variant Item-S-G', '_Test Variant Item-S-B',
-			'_Test Variant Item-M-R', '_Test Variant Item-M-G',
-			'_Test Variant Item-M-B', '_Test Variant Item-L-R',
-			'_Test Variant Item-L-G', '_Test Variant Item-L-B'])
-
-		self.assertEqual(item.variant_attributes['_Test Variant Item-L-R'], [['Test Size', 'Large'], ['Test Colour', 'Red']])
-		self.assertEqual(item.variant_attributes['_Test Variant Item-S-G'], [['Test Size', 'Small'], ['Test Colour', 'Green']])
-
-		# check stock entry cannot be made
-	def test_stock_entry_cannot_be_made_for_template(self):
-		item = self.get_item(11)
-
-		se = frappe.new_doc("Stock Entry")
-		se.purpose = "Material Receipt"
-		se.append("items", {
-			"item_code": item.name,
-			"t_warehouse": "Stores - _TC",
-			"qty": 1,
-			"incoming_rate": 1
-		})
-		se.insert()
-		self.assertRaises(ItemTemplateCannotHaveStock, se.submit)
-
+			item = self.get_item(10)
+			se = make_stock_entry(item_code=item.name, target="Stores - _TC", qty=1, incoming_rate=1)
+			item.has_variants = 1
+			self.assertRaises(ItemTemplateCannotHaveStock, item.save)
+	
 	def test_default_warehouse(self):
 		item = frappe.copy_doc(test_records[0])
 		item.is_stock_item = "Yes"
diff --git a/erpnext/stock/doctype/item/test_records.json b/erpnext/stock/doctype/item/test_records.json
index dc095c6..5a02c6b 100644
--- a/erpnext/stock/doctype/item/test_records.json
+++ b/erpnext/stock/doctype/item/test_records.json
@@ -273,11 +273,6 @@
   "item_name": "_Test Variant Item",
   "stock_uom": "_Test UOM",
   "has_variants": 1,
-  "variants": [
-	  {"item_attribute": "Test Size", "item_attribute_value": "Small"},
-	  {"item_attribute": "Test Size", "item_attribute_value": "Medium"},
-	  {"item_attribute": "Test Size", "item_attribute_value": "Large"}
-  ],
   "apply_warehouse_wise_reorder_level": 1,
   "reorder_levels": [
       {
diff --git a/erpnext/stock/doctype/manage_variants/__init__.py b/erpnext/stock/doctype/manage_variants/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/stock/doctype/manage_variants/__init__.py
diff --git a/erpnext/stock/doctype/manage_variants/manage_variants.js b/erpnext/stock/doctype/manage_variants/manage_variants.js
new file mode 100644
index 0000000..ba5c46e
--- /dev/null
+++ b/erpnext/stock/doctype/manage_variants/manage_variants.js
@@ -0,0 +1,56 @@
+// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
+// License: GNU General Public License v3. See license.txt
+
+frappe.ui.form.on("Manage Variants", {
+	onload: function(frm) {
+		var df = frappe.meta.get_docfield("Variant Attribute", "attribute_value");
+		df.on_make = function(field) {
+			$(field.input_area).addClass("ui-front");
+			field.$input.autocomplete({
+				minLength: 0,
+				minChars: 0,
+				source: function(request, response) {
+					frappe.call({
+						method:"frappe.client.get_list",
+						args:{
+							doctype:"Item Attribute Value",
+							filters: [
+								["parent","=", field.doc.attribute],
+								["attribute_value", "like", request.term + "%"]
+							],
+							fields: ["attribute_value"]
+						},
+						callback: function(r) {
+							response($.map(r.message, function(d) { return d.attribute_value; }));
+						}
+					});
+				},
+				select: function(event, ui) {
+					field.$input.val(ui.item.value);
+					field.$input.trigger("change");
+				}
+			});
+		}
+	},
+
+	refresh: function(frm) {
+		frm.disable_save();
+		frm.page.set_primary_action(__("Create Variants"), function() {
+			frappe.call({
+				method: "create_variants",
+				doc:frm.doc
+			})
+		});
+	},
+
+	item_code:function(frm) {
+		return frappe.call({
+			method: "get_item_details",
+			doc:frm.doc,
+			callback: function(r) {
+				refresh_field('attributes');
+				refresh_field('variants');
+			}
+		})
+	}
+});
diff --git a/erpnext/stock/doctype/manage_variants/manage_variants.json b/erpnext/stock/doctype/manage_variants/manage_variants.json
new file mode 100644
index 0000000..7c61620
--- /dev/null
+++ b/erpnext/stock/doctype/manage_variants/manage_variants.json
@@ -0,0 +1,103 @@
+{
+ "allow_copy": 0, 
+ "allow_import": 0, 
+ "allow_rename": 0, 
+ "creation": "2015-05-19 05:39:59.345901", 
+ "custom": 0, 
+ "docstatus": 0, 
+ "doctype": "DocType", 
+ "document_type": "", 
+ "fields": [
+  {
+   "fieldname": "item_code", 
+   "fieldtype": "Link", 
+   "label": "Item Code", 
+   "options": "Item", 
+   "permlevel": 0, 
+   "precision": "", 
+   "reqd": 1
+  }, 
+  {
+   "fieldname": "section_break_2", 
+   "fieldtype": "Section Break", 
+   "label": "Item Variant Attributes", 
+   "permlevel": 0, 
+   "precision": ""
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "fieldname": "attributes", 
+   "fieldtype": "Table", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "label": "Attributes", 
+   "no_copy": 0, 
+   "options": "Variant Attribute", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0
+  }, 
+  {
+   "fieldname": "generate_combinations", 
+   "fieldtype": "Button", 
+   "label": "Generate Combinations", 
+   "options": "generate_combinations", 
+   "permlevel": 0, 
+   "precision": ""
+  }, 
+  {
+   "fieldname": "section_break_4", 
+   "fieldtype": "Section Break", 
+   "label": "Item Variants", 
+   "permlevel": 0, 
+   "precision": ""
+  }, 
+  {
+   "fieldname": "variants", 
+   "fieldtype": "Table", 
+   "label": "Variants", 
+   "options": "Manage Variants Item", 
+   "permlevel": 0, 
+   "precision": ""
+  }
+ ], 
+ "hide_heading": 0, 
+ "hide_toolbar": 0, 
+ "in_create": 1, 
+ "in_dialog": 0, 
+ "is_submittable": 0, 
+ "issingle": 1, 
+ "istable": 0, 
+ "modified": "2015-06-30 13:40:59.946655", 
+ "modified_by": "Administrator", 
+ "module": "Stock", 
+ "name": "Manage Variants", 
+ "name_case": "", 
+ "owner": "Administrator", 
+ "permissions": [
+  {
+   "create": 1, 
+   "delete": 1, 
+   "email": 1, 
+   "export": 0, 
+   "permlevel": 0, 
+   "print": 1, 
+   "read": 1, 
+   "report": 0, 
+   "role": "Material Master Manager", 
+   "share": 1, 
+   "write": 1
+  }
+ ], 
+ "read_only": 1, 
+ "read_only_onload": 0, 
+ "sort_field": "modified", 
+ "sort_order": "DESC"
+}
\ No newline at end of file
diff --git a/erpnext/stock/doctype/manage_variants/manage_variants.py b/erpnext/stock/doctype/manage_variants/manage_variants.py
new file mode 100644
index 0000000..b6784d3
--- /dev/null
+++ b/erpnext/stock/doctype/manage_variants/manage_variants.py
@@ -0,0 +1,207 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors and contributors
+# For license information, please see license.txt
+
+from __future__ import unicode_literals
+import frappe
+from frappe import _
+from frappe.model.document import Document
+import copy
+import json
+
+class DuplicateAttribute(frappe.ValidationError): pass
+
+class ManageVariants(Document):
+
+	def get_item_details(self):
+		self.clear_tables()
+		if self.item_code:
+			self.get_attributes()
+			self.get_variants()
+		
+	def generate_combinations(self):
+		self.validate_attributes()
+		self.validate_template_item()
+		self.validate_attribute_values()
+		self.validate_attributes_are_unique()
+		self.get_variant_item_codes()
+		
+	def create_variants(self):
+		self.sync_variants()
+	
+	def clear_tables(self):
+		self.set('attributes', [])
+		self.set('variants', [])
+	
+	def get_attributes(self):
+		attributes = {}
+		self.set('attributes', [])
+		for d in frappe.db.sql("""select attribute, attribute_value from `tabVariant Attribute` as attribute, 
+			`tabItem` as item where attribute.parent= item.name and item.variant_of = %s""", self.item_code, as_dict=1):
+				attributes.setdefault(d.attribute, []).append(d.attribute_value)
+		for d in attributes:
+			attribute_values = set(attributes[d])
+			for value in attribute_values:
+				self.append('attributes',{"attribute": d, "attribute_value": value})
+
+	def get_variants(self):
+		variants = [d.name for d in frappe.get_all("Item",
+			filters={"variant_of":self.item_code})]
+		data = frappe.db.sql("""select parent, attribute, attribute_value from `tabVariant Attribute`""", as_dict=1)
+		for d in variants:
+			variant_attributes, attributes = "", []
+			for attribute in data:
+				if attribute.parent == d:
+					variant_attributes += attribute.attribute_value + " | "
+					attributes.append([attribute.attribute, attribute.attribute_value])
+			self.append('variants',{"variant": d, "variant_attributes": variant_attributes[: -3], "attributes": json.dumps(attributes)})
+
+	def validate_attributes(self):
+		if not self.attributes:
+			frappe.throw(_("Enter atleast one Attribute & its Value in Attribute table."))
+
+	def validate_template_item(self):
+		if not frappe.db.get_value("Item", self.item_code, "has_variants"):
+			frappe.throw(_("Selected Item cannot have Variants."))
+
+		if frappe.db.get_value("Item", self.item_code, "variant_of"):
+			frappe.throw(_("Item cannot be a variant of a variant"))
+
+	def validate_attribute_values(self):
+		attributes = {}
+		for t in frappe.db.get_all("Item Attribute Value", fields=["parent", "attribute_value"]):
+			attributes.setdefault(t.parent, []).append(t.attribute_value)
+		
+		for d in self.attributes:
+			if d.attribute_value not in attributes.get(d.attribute):
+				frappe.throw(_("Attribute value {0} does not exist in Item Attribute Master.").format(d.attribute_value))
+
+	def validate_attributes_are_unique(self):
+		attributes = []
+		for d in self.attributes:
+			key = (d.attribute, d.attribute_value)
+			if key in attributes:
+				frappe.throw(_("{0} {1} is entered more than once in Attributes table")
+					.format(d.attribute, d.attribute_value), DuplicateAttribute)
+			attributes.append(key)
+
+	def get_variant_item_codes(self):
+		"""Get all possible suffixes for variants"""
+		variant_dict = {}
+		self.set('variants', [])
+
+		for d in self.attributes:
+			variant_dict.setdefault(d.attribute, []).append(d.attribute_value)
+
+		all_attributes = [d.name for d in frappe.get_all("Item Attribute", order_by = "priority asc")]
+
+		# sort attributes by their priority
+		attributes = filter(None, map(lambda d: d if d in variant_dict else None, all_attributes))
+
+		def add_attribute_suffixes(item_code, my_attributes, attributes):
+			attr = frappe.get_doc("Item Attribute", attributes[0])
+			for value in attr.item_attribute_values:
+				if value.attribute_value in variant_dict[attr.name]:
+					_my_attributes = copy.deepcopy(my_attributes)
+					_my_attributes.append([attr.name, value.attribute_value])
+					if len(attributes) > 1:
+						add_attribute_suffixes(item_code + "-" + value.abbr, _my_attributes, attributes[1:])
+					else:
+						variant_attributes = ""
+						for d in _my_attributes:
+							variant_attributes += d[1] + " | "
+						self.append('variants', {"variant": item_code + "-" + value.abbr, 
+							"attributes": json.dumps(_my_attributes), "variant_attributes": variant_attributes[: -3]})
+		add_attribute_suffixes(self.item_code, [], attributes)
+
+	def sync_variants(self):
+		variant_item_codes = []
+		item_variants_attributes = {}
+		inserted, updated, old_variant_name, new_variant_name, deleted = [], [], [], [], []
+		
+		for v in self.variants:
+			variant_item_codes.append(v.variant)
+
+		existing_variants = [d.name for d in frappe.get_all("Item",
+			filters={"variant_of":self.item_code})]
+		
+		for d in existing_variants:
+			attributes = []
+			for attribute in frappe.db.sql("""select attribute, attribute_value from `tabVariant Attribute` where parent = %s""", d):
+				attributes.append([attribute[0], attribute[1]])
+			item_variants_attributes.setdefault(d, []).append(attributes)
+
+		for existing_variant in existing_variants:
+			if existing_variant not in variant_item_codes:
+				att = item_variants_attributes[existing_variant][0]
+				for variant in self.variants:
+					if sorted(json.loads(variant.attributes) ,key=lambda x: x[0]) == \
+						sorted(att ,key=lambda x: x[0]):
+							rename_variant(existing_variant, variant.variant)
+							old_variant_name.append(existing_variant)
+							new_variant_name.append(variant.variant)
+
+				if existing_variant not in old_variant_name:
+					delete_variant(existing_variant)
+					deleted.append(existing_variant)
+
+		for item_code in variant_item_codes:
+			if item_code not in existing_variants:
+				if item_code not in new_variant_name:
+					make_variant(self.item_code, item_code, self.variants)
+					inserted.append(item_code)
+			else:
+				update_variant(self.item_code, item_code, self.variants)
+				updated.append(item_code)
+
+		if inserted:
+			frappe.msgprint(_("Item Variants {0} created").format(", ".join(inserted)))
+
+		if updated:
+			frappe.msgprint(_("Item Variants {0} updated").format(", ".join(updated)))
+
+		if old_variant_name:
+			frappe.msgprint(_("Item Variants {0} renamed").format(", ".join(old_variant_name)))
+
+		if deleted:
+			frappe.msgprint(_("Item Variants {0} deleted").format(", ".join(deleted)))
+	
+def make_variant(item, variant_code, variant_attribute):
+	variant = frappe.new_doc("Item")
+	variant.item_code = variant_code
+	copy_attributes_to_variant(item, variant, variant_attribute, insert=True)
+	variant.insert()
+
+def update_variant(item, variant_code, variant_attribute=None):
+	variant = frappe.get_doc("Item", variant_code)
+	copy_attributes_to_variant(item, variant, variant_attribute, insert=True)
+	variant.save()
+
+def rename_variant(old_variant_code, new_variant_code):
+	frappe.rename_doc("Item", old_variant_code, new_variant_code)
+
+def delete_variant(variant_code):
+	frappe.delete_doc("Item", variant_code)
+
+def copy_attributes_to_variant(item, variant, variant_attribute=None, insert=False):
+	template = frappe.get_doc("Item", item)
+	from frappe.model import no_value_fields
+	for field in template.meta.fields:
+		if field.fieldtype not in no_value_fields and (insert or not field.no_copy)\
+			and field.fieldname not in ("item_code", "item_name"):
+			if variant.get(field.fieldname) != template.get(field.fieldname):
+				variant.set(field.fieldname, template.get(field.fieldname))
+	variant.item_name = template.item_name + variant.item_code[len(template.name):]
+	variant.variant_of = template.name
+	variant.has_variants = 0
+	variant.show_in_website = 0
+	if variant_attribute:
+		for d in variant_attribute:
+			if d.variant == variant.item_code:
+				variant.attributes= []
+				for a in json.loads(d.attributes):
+					variant.append('attributes', {"attribute": a[0], "attribute_value": a[1]})
+	if variant.attributes:
+		variant.description += "\n"
+		for d in variant.attributes:
+			variant.description += "<p>" + d.attribute + ": " + d.attribute_value + "</p>"
\ No newline at end of file
diff --git a/erpnext/stock/doctype/manage_variants/test_manage_variants.py b/erpnext/stock/doctype/manage_variants/test_manage_variants.py
new file mode 100644
index 0000000..9952ba9
--- /dev/null
+++ b/erpnext/stock/doctype/manage_variants/test_manage_variants.py
@@ -0,0 +1,49 @@
+# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
+# License: GNU General Public License v3. See license.txt
+
+from __future__ import unicode_literals
+import unittest
+import frappe
+
+from erpnext.stock.doctype.manage_variants.manage_variants import DuplicateAttribute
+
+class TestManageVariants(unittest.TestCase):
+	def test_variant_item_codes(self):
+		manage_variant = frappe.new_doc("Manage Variants")
+		manage_variant.update({
+			"item": "_Test Variant Item",
+			"attributes": [
+				{
+					"attribute": "Test Size",
+					"attribute_value": "Small"
+				},
+				{
+					"attribute": "Test Size",
+					"attribute_value": "Large"
+				}
+			]
+		})
+		manage_variant.generate_combinations()
+		self.assertEqual(manage_variant.variants[0].variant, "_Test Variant Item-S")
+		self.assertEqual(manage_variant.variants[1].variant, "_Test Variant Item-L")
+		
+		self.assertEqual(manage_variant.variants[0].variant_attributes, "Small")
+		self.assertEqual(manage_variant.variants[1].variant_attributes, "Large")
+		manage_variant.create_variants()
+
+	def test_attributes_are_unique(self):
+		manage_variant = frappe.new_doc("Manage Variants")
+		manage_variant.update({
+			"item": "_Test Variant Item",
+			"attributes": [
+				{
+					"attribute": "Test Size",
+					"attribute_value": "Small"
+				},
+				{
+					"attribute": "Test Size",
+					"attribute_value": "Small"
+				}
+			]
+		})
+		self.assertRaises(DuplicateAttribute, manage_variant.generate_combinations)
diff --git a/erpnext/stock/doctype/manage_variants_item/__init__.py b/erpnext/stock/doctype/manage_variants_item/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/stock/doctype/manage_variants_item/__init__.py
diff --git a/erpnext/stock/doctype/manage_variants_item/manage_variants_item.json b/erpnext/stock/doctype/manage_variants_item/manage_variants_item.json
new file mode 100644
index 0000000..a8bb61d
--- /dev/null
+++ b/erpnext/stock/doctype/manage_variants_item/manage_variants_item.json
@@ -0,0 +1,76 @@
+{
+ "allow_copy": 0, 
+ "allow_import": 1, 
+ "allow_rename": 0, 
+ "autoname": "", 
+ "creation": "2015-05-19 05:55:31.155672", 
+ "custom": 0, 
+ "docstatus": 0, 
+ "doctype": "DocType", 
+ "document_type": "Other", 
+ "fields": [
+  {
+   "allow_on_submit": 0, 
+   "fieldname": "variant", 
+   "fieldtype": "Data", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 1, 
+   "label": "Variant", 
+   "no_copy": 0, 
+   "options": "", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 1, 
+   "search_index": 0, 
+   "set_only_once": 0
+  }, 
+  {
+   "fieldname": "column_break_2", 
+   "fieldtype": "Column Break", 
+   "permlevel": 0, 
+   "precision": ""
+  }, 
+  {
+   "fieldname": "variant_attributes", 
+   "fieldtype": "Data", 
+   "in_list_view": 1, 
+   "label": "Variant Attributes", 
+   "permlevel": 0, 
+   "precision": "", 
+   "read_only": 1
+  }, 
+  {
+   "fieldname": "attributes", 
+   "fieldtype": "Text", 
+   "hidden": 1, 
+   "label": "attributes", 
+   "permlevel": 0, 
+   "precision": "", 
+   "read_only": 1
+  }
+ ], 
+ "hide_heading": 0, 
+ "hide_toolbar": 0, 
+ "icon": "", 
+ "in_create": 0, 
+ "in_dialog": 0, 
+ "is_submittable": 0, 
+ "issingle": 0, 
+ "istable": 1, 
+ "modified": "2015-06-30 03:19:07.548196", 
+ "modified_by": "Administrator", 
+ "module": "Stock", 
+ "name": "Manage Variants Item", 
+ "name_case": "", 
+ "owner": "Administrator", 
+ "permissions": [], 
+ "read_only": 0, 
+ "read_only_onload": 0, 
+ "sort_field": "modified", 
+ "sort_order": "DESC"
+}
\ No newline at end of file
diff --git a/erpnext/stock/doctype/manage_variants_item/manage_variants_item.py b/erpnext/stock/doctype/manage_variants_item/manage_variants_item.py
new file mode 100644
index 0000000..800888a
--- /dev/null
+++ b/erpnext/stock/doctype/manage_variants_item/manage_variants_item.py
@@ -0,0 +1,10 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors and contributors
+# For license information, please see license.txt
+
+from __future__ import unicode_literals
+import frappe
+from frappe.model.document import Document
+
+class ManageVariantsItem(Document):
+	pass
diff --git a/erpnext/stock/doctype/stock_entry/test_stock_entry.py b/erpnext/stock/doctype/stock_entry/test_stock_entry.py
index ff0b272..8114bef 100644
--- a/erpnext/stock/doctype/stock_entry/test_stock_entry.py
+++ b/erpnext/stock/doctype/stock_entry/test_stock_entry.py
@@ -72,6 +72,18 @@
 		self._test_auto_material_request("_Test Item")
 
 	def test_auto_material_request_for_variant(self):
+		manage_variant = frappe.new_doc("Manage Variants")
+		manage_variant.update({
+			"item": "_Test Variant Item",
+			"attributes": [
+				{
+					"attribute": "Test Size",
+					"attribute_value": "Small"
+				}
+			]
+		})
+		manage_variant.generate_combinations()
+		manage_variant.create_variants()
 		self._test_auto_material_request("_Test Variant Item-S")
 
 	def _test_auto_material_request(self, item_code):
diff --git a/erpnext/stock/doctype/variant_attribute/__init__.py b/erpnext/stock/doctype/variant_attribute/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/stock/doctype/variant_attribute/__init__.py
diff --git a/erpnext/stock/doctype/variant_attribute/variant_attribute.json b/erpnext/stock/doctype/variant_attribute/variant_attribute.json
new file mode 100644
index 0000000..5ab3d73
--- /dev/null
+++ b/erpnext/stock/doctype/variant_attribute/variant_attribute.json
@@ -0,0 +1,78 @@
+{
+ "allow_copy": 0, 
+ "allow_import": 1, 
+ "allow_rename": 0, 
+ "autoname": "", 
+ "creation": "2015-05-19 05:12:30.344797", 
+ "custom": 0, 
+ "docstatus": 0, 
+ "doctype": "DocType", 
+ "document_type": "Other", 
+ "fields": [
+  {
+   "allow_on_submit": 0, 
+   "fieldname": "attribute", 
+   "fieldtype": "Link", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 1, 
+   "label": "Attribute", 
+   "no_copy": 0, 
+   "options": "Item Attribute", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 1, 
+   "search_index": 0, 
+   "set_only_once": 0
+  }, 
+  {
+   "fieldname": "column_break_2", 
+   "fieldtype": "Column Break", 
+   "permlevel": 0, 
+   "precision": ""
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "fieldname": "attribute_value", 
+   "fieldtype": "Data", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 1, 
+   "label": "Attribute Value", 
+   "no_copy": 0, 
+   "options": "", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 1, 
+   "search_index": 0, 
+   "set_only_once": 0
+  }
+ ], 
+ "hide_heading": 0, 
+ "hide_toolbar": 0, 
+ "icon": "", 
+ "in_create": 0, 
+ "in_dialog": 0, 
+ "is_submittable": 0, 
+ "issingle": 0, 
+ "istable": 1, 
+ "modified": "2015-05-20 06:16:16.803578", 
+ "modified_by": "Administrator", 
+ "module": "Stock", 
+ "name": "Variant Attribute", 
+ "name_case": "", 
+ "owner": "Administrator", 
+ "permissions": [], 
+ "read_only": 0, 
+ "read_only_onload": 0, 
+ "sort_field": "modified", 
+ "sort_order": "DESC"
+}
\ No newline at end of file
diff --git a/erpnext/stock/doctype/variant_attribute/variant_attribute.py b/erpnext/stock/doctype/variant_attribute/variant_attribute.py
new file mode 100644
index 0000000..9c35732
--- /dev/null
+++ b/erpnext/stock/doctype/variant_attribute/variant_attribute.py
@@ -0,0 +1,10 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors and contributors
+# For license information, please see license.txt
+
+from __future__ import unicode_literals
+import frappe
+from frappe.model.document import Document
+
+class VariantAttribute(Document):
+	pass