[enhance] Item Alternative (#13121)

* [enhance] Item Alternative

* Added two way provision in item alternative

* Test Cases

* documentation
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.py b/erpnext/buying/doctype/purchase_order/purchase_order.py
index 1649494..14fb0e0 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.py
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.py
@@ -423,10 +423,7 @@
 
 	if fg_items:
 		items = tuple(set(d["rm_item_code"] for d in rm_items_list))
-		item_wh = frappe._dict(frappe.db.sql("""
-			select item_code, description
-			from `tabItem` where name in ({0})
-		""".format(", ".join(["%s"] * len(items))), items))
+		item_wh = get_item_details(items)
 
 		stock_entry = frappe.new_doc("Stock Entry")
 		stock_entry.purpose = "Subcontract"
@@ -441,13 +438,15 @@
 		for item_code in fg_items:
 			for rm_item_data in rm_items_list:
 				if rm_item_data["item_code"] == item_code:
+					rm_item_code = rm_item_data["rm_item_code"]
 					items_dict = {
-						rm_item_data["rm_item_code"]: {
+						rm_item_code: {
 							"item_name": rm_item_data["item_name"],
-							"description": item_wh.get(rm_item_data["rm_item_code"]),
+							"description": item_wh[rm_item_code].get('description'),
 							'qty': rm_item_data["qty"],
 							'from_warehouse': rm_item_data["warehouse"],
-							'stock_uom': rm_item_data["stock_uom"]
+							'stock_uom': rm_item_data["stock_uom"],
+							'allow_alternative_item': item_wh[rm_item_code].get('allow_alternative_item')
 						}
 					}
 					stock_entry.add_to_stock_entry_detail(items_dict)
@@ -456,6 +455,14 @@
 		frappe.throw(_("No Items selected for transfer"))
 	return purchase_order.name
 
+def get_item_details(items):
+	item_details = {}
+	for d in frappe.db.sql("""select item_code, description, allow_alternative_item from `tabItem`
+		where name in ({0})""".format(", ".join(["%s"] * len(items))), items, as_dict=1):
+		item_details[d.item_code] = d
+
+	return item_details
+
 @frappe.whitelist()
 def update_status(status, name):
 	po = frappe.get_doc("Purchase Order", name)
diff --git a/erpnext/buying/doctype/purchase_order/test_purchase_order.py b/erpnext/buying/doctype/purchase_order/test_purchase_order.py
index 5dacfc2..873fc74 100644
--- a/erpnext/buying/doctype/purchase_order/test_purchase_order.py
+++ b/erpnext/buying/doctype/purchase_order/test_purchase_order.py
@@ -321,6 +321,7 @@
 	po.is_subcontracted = args.is_subcontracted or "No"
 	po.currency = args.currency or frappe.db.get_value("Company", po.company, "default_currency")
 	po.conversion_factor = args.conversion_factor or 1
+	po.supplier_warehouse = args.supplier_warehouse or None
 
 	po.append("items", {
 		"item_code": args.item or args.item_code or "_Test Item",
diff --git a/erpnext/config/stock.py b/erpnext/config/stock.py
index e3b8d82..fe358ed 100644
--- a/erpnext/config/stock.py
+++ b/erpnext/config/stock.py
@@ -73,6 +73,10 @@
 				},
 				{
 					"type": "doctype",
+					"name": "Item Alternative",
+				},
+				{
+					"type": "doctype",
 					"name": "Product Bundle",
 				},
 				{
diff --git a/erpnext/controllers/buying_controller.py b/erpnext/controllers/buying_controller.py
index ff0f0c2..de6ed79 100644
--- a/erpnext/controllers/buying_controller.py
+++ b/erpnext/controllers/buying_controller.py
@@ -10,6 +10,7 @@
 from erpnext.stock.get_item_details import get_conversion_factor
 from erpnext.buying.utils import validate_for_items, update_last_purchase_rate
 from erpnext.stock.stock_ledger import get_valuation_rate
+from erpnext.stock.doctype.stock_entry.stock_entry import get_used_alternative_items
 
 from erpnext.controllers.stock_controller import StockController
 
@@ -200,6 +201,11 @@
 			exploded_item = item.get('include_exploded_items')
 
 		bom_items = get_items_from_bom(item.item_code, item.bom, exploded_item)
+
+		used_alternative_items = []
+		if self.doctype == 'Purchase Receipt' and item.purchase_order:
+			used_alternative_items = get_used_alternative_items(purchase_order = item.purchase_order)
+
 		raw_materials_cost = 0
 		items = list(set([d.item_code for d in bom_items]))
 		item_wh = frappe._dict(frappe.db.sql("""select item_code, default_warehouse
@@ -211,6 +217,16 @@
 				if frappe.db.get_value("Warehouse", reserve_warehouse, "company") != self.company:
 					reserve_warehouse = None
 
+			conversion_factor = item.conversion_factor
+			if (self.doctype == 'Purchase Receipt' and item.purchase_order and
+				bom_item.item_code in used_alternative_items):
+				alternative_item_data = used_alternative_items.get(bom_item.item_code)
+				bom_item.item_code = alternative_item_data.item_code
+				bom_item.item_name = alternative_item_data.item_name
+				bom_item.stock_uom = alternative_item_data.stock_uom
+				conversion_factor = alternative_item_data.conversion_factor
+				bom_item.description = alternative_item_data.description
+
 			# check if exists
 			exists = 0
 			for d in self.get(raw_material_table):
@@ -223,7 +239,7 @@
 				rm = self.append(raw_material_table, {})
 
 			required_qty = flt(flt(bom_item.qty_consumed_per_unit) * (flt(item.qty) + getattr(item, 'rejected_qty', 0)) *
-				flt(item.conversion_factor), rm.precision("required_qty"))
+				flt(conversion_factor), rm.precision("required_qty"))
 			rm.reference_name = item.name
 			rm.bom_detail_no = bom_item.name
 			rm.main_item_code = item.item_code
@@ -233,7 +249,7 @@
 			if self.doctype == "Purchase Order" and not rm.reserve_warehouse:
 				rm.reserve_warehouse = reserve_warehouse
 
-			rm.conversion_factor = item.conversion_factor
+			rm.conversion_factor = conversion_factor
 
 			if self.doctype in ["Purchase Receipt", "Purchase Invoice"]:
 				rm.consumed_qty = required_qty
@@ -463,4 +479,4 @@
 	if not bom_items:
 		msgprint(_("Specified BOM {0} does not exist for Item {1}").format(bom, item_code), raise_exception=1)
 
-	return bom_items
\ No newline at end of file
+	return bom_items
diff --git a/erpnext/docs/assets/img/manufacturing/allow-alternative-item-bom.png b/erpnext/docs/assets/img/manufacturing/allow-alternative-item-bom.png
new file mode 100644
index 0000000..4120881
--- /dev/null
+++ b/erpnext/docs/assets/img/manufacturing/allow-alternative-item-bom.png
Binary files differ
diff --git a/erpnext/docs/assets/img/manufacturing/allow-alternative-item-wo.png b/erpnext/docs/assets/img/manufacturing/allow-alternative-item-wo.png
new file mode 100644
index 0000000..baa732f
--- /dev/null
+++ b/erpnext/docs/assets/img/manufacturing/allow-alternative-item-wo.png
Binary files differ
diff --git a/erpnext/docs/assets/img/manufacturing/allow-alternative-item.png b/erpnext/docs/assets/img/manufacturing/allow-alternative-item.png
new file mode 100644
index 0000000..1771980
--- /dev/null
+++ b/erpnext/docs/assets/img/manufacturing/allow-alternative-item.png
Binary files differ
diff --git a/erpnext/docs/assets/img/manufacturing/item-alternative.png b/erpnext/docs/assets/img/manufacturing/item-alternative.png
new file mode 100644
index 0000000..ff7c66c
--- /dev/null
+++ b/erpnext/docs/assets/img/manufacturing/item-alternative.png
Binary files differ
diff --git a/erpnext/docs/assets/img/manufacturing/purchase_order_item_alternative.gif b/erpnext/docs/assets/img/manufacturing/purchase_order_item_alternative.gif
new file mode 100644
index 0000000..4907e3e
--- /dev/null
+++ b/erpnext/docs/assets/img/manufacturing/purchase_order_item_alternative.gif
Binary files differ
diff --git a/erpnext/docs/assets/img/manufacturing/work_order_item_alternative.gif b/erpnext/docs/assets/img/manufacturing/work_order_item_alternative.gif
new file mode 100644
index 0000000..076f0b1
--- /dev/null
+++ b/erpnext/docs/assets/img/manufacturing/work_order_item_alternative.gif
Binary files differ
diff --git a/erpnext/docs/user/manual/en/manufacturing/index.txt b/erpnext/docs/user/manual/en/manufacturing/index.txt
index 13f0701..9befc66 100644
--- a/erpnext/docs/user/manual/en/manufacturing/index.txt
+++ b/erpnext/docs/user/manual/en/manufacturing/index.txt
@@ -4,6 +4,7 @@
 workstation
 operation
 subcontracting
+item-alternative
 tools
 setup
 articles
diff --git a/erpnext/docs/user/manual/en/manufacturing/item-alternative.md b/erpnext/docs/user/manual/en/manufacturing/item-alternative.md
new file mode 100644
index 0000000..69bc825
--- /dev/null
+++ b/erpnext/docs/user/manual/en/manufacturing/item-alternative.md
@@ -0,0 +1,32 @@
+# Item Alternative
+
+Item alternative feature is very useful in manufacturing industries, if the raw material defined in the BOM is not available during the production process then their respective available alternative item used to complete the production process.
+
+To make item alaternative for an item, kindly enable the "Allow Alternative Item" in the item.
+<img class="screenshot" alt="Item" src="{{docs_base_url}}/assets/img/manufacturing/allow-alternative-item.png">
+
+* To make item alternative, goto module Stock > Items and Pricing > Item Alternative
+<img class="screenshot" alt="Item Alternative" src="{{docs_base_url}}/assets/img/manufacturing/item-alternative.png">
+
+The user can enable Two-Way between an item and their alternative item if both can be used as an alternative to each other
+
+
+### Item Alternative for work order
+
+To allow to use alternative items in the manufacturing process user can configure allow an alternative item in the BOM/Work Order
+
+##### Provision to allow alternative item in the bom
+<img class="screenshot" alt="Item" src="{{docs_base_url}}/assets/img/manufacturing/allow-alternative-item-bom.png">
+
+##### Provision to allow alternative item in the work order
+User can also enable/disable allow alternative item in the work order
+<img class="screenshot" alt="Item" src="{{docs_base_url}}/assets/img/manufacturing/allow-alternative-item-wo.png">
+
+##### How it works for work order
+<img class="screenshot" alt="Item" src="{{docs_base_url}}/assets/img/manufacturing/work_order_item_alternative.gif">
+
+### Item Alternative for subcontract
+In subcontract, the user has to transfer raw materials to the subcontracted supplier to get finished good from them. If the raw material is not available in the stock, with this feature, the user can transfer the alternate item of the subcontracted raw material to the supplier.
+
+##### How it works for subcontract
+<img class="screenshot" alt="Item" src="{{docs_base_url}}/assets/img/manufacturing/purchase_order_item_alternative.gif">
\ No newline at end of file
diff --git a/erpnext/manufacturing/doctype/bom/bom.json b/erpnext/manufacturing/doctype/bom/bom.json
index 30b9683..b2f7bb3 100644
--- a/erpnext/manufacturing/doctype/bom/bom.json
+++ b/erpnext/manufacturing/doctype/bom/bom.json
@@ -42,6 +42,7 @@
    "reqd": 1, 
    "search_index": 1, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -73,6 +74,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -106,6 +108,7 @@
    "reqd": 1, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -136,6 +139,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -168,6 +172,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -196,6 +201,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -228,6 +234,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -260,6 +267,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -290,6 +298,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -320,6 +329,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -351,6 +361,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -382,6 +393,38 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "allow_alternative_item", 
+   "fieldtype": "Check", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Allow Alternative Item", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -412,6 +455,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -443,6 +487,7 @@
    "reqd": 1, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -472,6 +517,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -503,6 +549,7 @@
    "reqd": 1, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -533,6 +580,7 @@
    "reqd": 1, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -565,6 +613,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -597,6 +646,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -627,6 +677,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -659,6 +710,7 @@
    "reqd": 1, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -689,6 +741,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -720,6 +773,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -750,6 +804,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -780,6 +835,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -810,6 +866,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -841,6 +898,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -869,6 +927,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -900,6 +959,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -931,6 +991,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -962,6 +1023,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -991,6 +1053,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1021,6 +1084,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1050,6 +1114,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1081,6 +1146,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1110,6 +1176,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1142,6 +1209,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1172,6 +1240,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1200,6 +1269,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1230,6 +1300,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1259,6 +1330,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1288,6 +1360,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1317,6 +1390,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1347,6 +1421,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1378,6 +1453,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1408,6 +1484,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1440,6 +1517,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1471,6 +1549,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1502,6 +1581,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1532,6 +1612,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1564,6 +1645,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1594,6 +1676,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1626,6 +1709,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1657,6 +1741,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1688,6 +1773,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1719,6 +1805,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }
  ], 
@@ -1733,7 +1820,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2018-02-16 13:43:55.485813", 
+ "modified": "2018-02-26 22:51:40.232456", 
  "modified_by": "Administrator", 
  "module": "Manufacturing", 
  "name": "BOM", 
diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py
index ea33f4e..6942a8c 100644
--- a/erpnext/manufacturing/doctype/bom/bom.py
+++ b/erpnext/manufacturing/doctype/bom/bom.py
@@ -543,6 +543,7 @@
 				item.description,
 				item.image,
 				item.stock_uom,
+				item.allow_alternative_item,
 				item.default_warehouse,
 				item.expense_account as expense_account,
 				item.buying_cost_center as cost_center
diff --git a/erpnext/manufacturing/doctype/work_order/work_order.js b/erpnext/manufacturing/doctype/work_order/work_order.js
index 7d50416..a1ebca0 100644
--- a/erpnext/manufacturing/doctype/work_order/work_order.js
+++ b/erpnext/manufacturing/doctype/work_order/work_order.js
@@ -171,7 +171,7 @@
 						frm.set_value('sales_order', "");
 						frm.trigger('set_sales_order');
 						erpnext.in_production_item_onchange = true;
-						$.each(["description", "stock_uom", "project", "bom_no"], function(i, field) {
+						$.each(["description", "stock_uom", "project", "bom_no", "allow_alternative_item"], function(i, field) {
 							frm.set_value(field, r.message[field]);
 						});
 
diff --git a/erpnext/manufacturing/doctype/work_order/work_order.json b/erpnext/manufacturing/doctype/work_order/work_order.json
index 6f6c4e4..e6bdc26 100644
--- a/erpnext/manufacturing/doctype/work_order/work_order.json
+++ b/erpnext/manufacturing/doctype/work_order/work_order.json
@@ -40,6 +40,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -71,6 +72,7 @@
    "reqd": 1, 
    "search_index": 0, 
    "set_only_once": 1, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -105,6 +107,7 @@
    "reqd": 1, 
    "search_index": 1, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -137,6 +140,7 @@
    "reqd": 1, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -171,6 +175,7 @@
    "reqd": 1, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -202,6 +207,38 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "allow_alternative_item", 
+   "fieldtype": "Check", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Allow Alternative Item", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -231,6 +268,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0, 
    "width": "50%"
   }, 
@@ -264,6 +302,7 @@
    "reqd": 1, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -297,6 +336,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -331,6 +371,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -362,6 +403,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -394,6 +436,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -425,6 +468,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -455,6 +499,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -485,6 +530,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -517,6 +563,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -545,6 +592,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -576,6 +624,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -606,6 +655,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -637,6 +687,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -668,6 +719,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -699,6 +751,7 @@
    "reqd": 1, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -729,6 +782,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -758,6 +812,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -788,6 +843,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -818,6 +874,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -848,6 +905,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -880,6 +938,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -912,6 +971,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -944,6 +1004,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -975,6 +1036,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1006,6 +1068,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1037,6 +1100,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1066,6 +1130,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1097,6 +1162,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1127,6 +1193,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1156,6 +1223,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1189,6 +1257,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1221,6 +1290,7 @@
    "reqd": 1, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1249,6 +1319,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0, 
    "width": "50%"
   }, 
@@ -1282,6 +1353,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1312,6 +1384,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1342,6 +1415,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1373,6 +1447,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1403,6 +1478,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1435,6 +1511,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }
  ], 
@@ -1449,7 +1526,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2018-02-13 02:58:11.328693", 
+ "modified": "2018-03-05 12:43:10.442928", 
  "modified_by": "Administrator", 
  "module": "Manufacturing", 
  "name": "Work Order", 
diff --git a/erpnext/manufacturing/doctype/work_order/work_order.py b/erpnext/manufacturing/doctype/work_order/work_order.py
index 6c632a5..bb773ab 100644
--- a/erpnext/manufacturing/doctype/work_order/work_order.py
+++ b/erpnext/manufacturing/doctype/work_order/work_order.py
@@ -489,6 +489,7 @@
 						'item_code': item.item_code,
 						'item_name': item.item_name,
 						'description': item.description,
+						'allow_alternative_item': item.allow_alternative_item,
 						'required_qty': item.qty,
 						'source_warehouse': item.source_warehouse or item.default_warehouse
 					})
@@ -503,15 +504,17 @@
 			transferred_qty = frappe.db.sql('''select sum(qty)
 				from `tabStock Entry` entry, `tabStock Entry Detail` detail
 				where
-					entry.work_order = %s
+					entry.work_order = %(name)s
 					and entry.purpose = "Material Transfer for Manufacture"
 					and entry.docstatus = 1
 					and detail.parent = entry.name
-					and detail.item_code = %s''', (self.name, d.item_code))[0][0]
+					and (detail.item_code = %(item)s or detail.original_item = %(item)s)''', {
+						'name': self.name,
+						'item': d.item_code
+					})[0][0]
 
 			d.db_set('transferred_qty', flt(transferred_qty), update_modified = False)
 
-
 @frappe.whitelist()
 def get_item_details(item, project = None):
 	res = frappe.db.sql("""
@@ -548,6 +551,7 @@
 			frappe.throw(_("Default BOM for {0} not found").format(item))
 
 	res['project'] = project or frappe.db.get_value('BOM', res['bom_no'], 'project')
+	res['allow_alternative_item'] = frappe.db.get_value('BOM', res['bom_no'], 'allow_alternative_item')
 	res.update(check_if_scrap_warehouse_mandatory(res["bom_no"]))
 
 	return res
diff --git a/erpnext/manufacturing/doctype/work_order_item/work_order_item.json b/erpnext/manufacturing/doctype/work_order_item/work_order_item.json
index 1ac6b66..2f89a3d 100644
--- a/erpnext/manufacturing/doctype/work_order_item/work_order_item.json
+++ b/erpnext/manufacturing/doctype/work_order_item/work_order_item.json
@@ -41,6 +41,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -72,6 +73,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -101,6 +103,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -131,6 +134,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -161,6 +165,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -191,6 +196,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -221,6 +227,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -252,6 +259,38 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "allow_alternative_item", 
+   "fieldtype": "Check", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Allow Alternative Item", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -281,6 +320,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -311,6 +351,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -341,6 +382,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }
  ], 
@@ -354,7 +396,7 @@
  "issingle": 0, 
  "istable": 1, 
  "max_attachments": 0, 
- "modified": "2018-02-13 02:58:11.328693",
+ "modified": "2018-03-05 13:07:07.530725", 
  "modified_by": "Administrator", 
  "module": "Manufacturing", 
  "name": "Work Order Item",
diff --git a/erpnext/public/js/utils.js b/erpnext/public/js/utils.js
index 37291d7..0e60179 100644
--- a/erpnext/public/js/utils.js
+++ b/erpnext/public/js/utils.js
@@ -184,6 +184,142 @@
 	},
 });
 
+erpnext.utils.select_alternate_items = function(opts) {
+	const frm = opts.frm;
+	const warehouse_field = opts.warehouse_field || 'warehouse';
+	const item_field = opts.item_field || 'item_code';
+
+	this.data = [];
+	const dialog = new frappe.ui.Dialog({
+		title: __("Select Alternate Item"),
+		fields: [
+			{fieldtype:'Section Break', label: __('Items')},
+			{
+				fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true,
+				in_place_edit: true, data: this.data,
+				get_data: () => {
+					return this.data;
+				},
+				fields: [{
+					fieldtype:'Data',
+					fieldname:"docname",
+					hidden: 1
+				}, {
+					fieldtype:'Link',
+					fieldname:"item_code",
+					options: 'Item',
+					in_list_view: 1,
+					read_only: 1,
+					label: __('Item Code')
+				}, {
+					fieldtype:'Link',
+					fieldname:"alternate_item",
+					options: 'Item',
+					default: "",
+					in_list_view: 1,
+					label: __('Alternate Item'),
+					onchange: function() {
+						const item_code = this.get_value();
+						const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value();
+						if (item_code && warehouse) {
+							frappe.call({
+								method: "erpnext.stock.utils.get_latest_stock_qty",
+								args: {
+									item_code: item_code,
+									warehouse: warehouse
+								},
+								callback: (r) => {
+									this.grid_row.on_grid_fields_dict
+										.actual_qty.set_value(r.message || 0);
+								}
+							})
+						}
+					},
+					get_query: (e) => {
+						return {
+							query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items",
+							filters: {
+								item_code: e.item_code
+							}
+						};
+					}
+				}, {
+					fieldtype:'Link',
+					fieldname:"warehouse",
+					options: 'Warehouse',
+					default: "",
+					in_list_view: 1,
+					label: __('Warehouse'),
+					onchange: function() {
+						const warehouse = this.get_value();
+						const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value();
+						if (item_code && warehouse) {
+							frappe.call({
+								method: "erpnext.stock.utils.get_latest_stock_qty",
+								args: {
+									item_code: item_code,
+									warehouse: warehouse
+								},
+								callback: (r) => {
+									this.grid_row.on_grid_fields_dict
+										.actual_qty.set_value(r.message || 0);
+								}
+							})
+						}
+					},
+				}, {
+					fieldtype:'Float',
+					fieldname:"actual_qty",
+					default: 0,
+					read_only: 1,
+					in_list_view: 1,
+					label: __('Available Qty')
+				}]
+			},
+		],
+		primary_action: function() {
+			const args = this.get_values()["alternative_items"];
+			const alternative_items = args.filter(d => {
+				if (d.alternate_item && d.item_code != d.alternate_item) {
+					return true;
+				}
+			});
+
+			alternative_items.forEach(d => {
+				let row = frappe.get_doc(opts.child_doctype, d.docname);
+				let qty = row.qty;
+				row[item_field] = d.alternate_item;
+
+				frm.script_manager.trigger(item_field, row.doctype, row.name)
+					.then(() => {
+						frappe.model.set_value(row.doctype, row.name, 'qty', qty);
+						frappe.model.set_value(row.doctype, row.name,
+							opts.original_item_field, d.item_code);
+					});
+			});
+
+			refresh_field(opts.child_docname);
+			this.hide();
+		},
+		primary_action_label: __('Update')
+	});
+
+	frm.doc[opts.child_docname].forEach(d => {
+		if (!opts.condition || opts.condition(d)) {
+			dialog.fields_dict.alternative_items.df.data.push({
+				"docname": d.name,
+				"item_code": d[item_field],
+				"warehouse": d[warehouse_field],
+				"actual_qty": d.actual_qty
+			});
+		}
+	})
+
+	this.data = dialog.fields_dict.alternative_items.df.data;
+	dialog.fields_dict.alternative_items.grid.refresh();
+	dialog.show();
+}
+
 erpnext.utils.map_current_doc = function(opts) {
 	if(opts.get_query_filters) {
 		opts.get_query = function() {
diff --git a/erpnext/stock/doctype/bin/bin.py b/erpnext/stock/doctype/bin/bin.py
index acff76d..efa6c14 100644
--- a/erpnext/stock/doctype/bin/bin.py
+++ b/erpnext/stock/doctype/bin/bin.py
@@ -116,14 +116,14 @@
 				se.docstatus=1
 				and se.purpose='Subcontract'
 				and ifnull(se.purchase_order, '') !=''
-				and sed.item_code = %s
+				and (sed.item_code = %(item)s or sed.original_item = %(item)s)
 				and se.name = sed.parent
 				and se.purchase_order = po.name
 				and po.docstatus = 1
 				and po.is_subcontracted = 'Yes'
 				and po.status != 'Closed'
 				and po.per_received < 100
-		""", (self.item_code))[0][0]
+		""", {'item': self.item_code})[0][0]
 
 		if reserved_qty_for_sub_contract > materials_transferred:
 			reserved_qty_for_sub_contract = reserved_qty_for_sub_contract - materials_transferred
diff --git a/erpnext/stock/doctype/item/item.json b/erpnext/stock/doctype/item/item.json
index 1866d8f..a6137f9 100644
--- a/erpnext/stock/doctype/item/item.json
+++ b/erpnext/stock/doctype/item/item.json
@@ -367,6 +367,37 @@
    "unique": 0
   },
   {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "allow_alternative_item", 
+   "fieldtype": "Check", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Allow Alternative Item", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "translatable": 0, 
+   "unique": 0
+  },
+  {
    "allow_bulk_edit": 0,
    "allow_on_submit": 0,
    "bold": 1,
@@ -3686,7 +3717,7 @@
  "issingle": 0,
  "istable": 0,
  "max_attachments": 1,
- "modified": "2018-03-09 03:13:18.516087",
+ "modified": "2018-03-12 03:13:18.516087",
  "modified_by": "Administrator",
  "module": "Stock",
  "name": "Item",
diff --git a/erpnext/stock/doctype/item/item_dashboard.py b/erpnext/stock/doctype/item/item_dashboard.py
index 21608a6..c571355 100644
--- a/erpnext/stock/doctype/item/item_dashboard.py
+++ b/erpnext/stock/doctype/item/item_dashboard.py
@@ -15,7 +15,7 @@
 		'transactions': [
 			{
 				'label': _('Groups'),
-				'items': ['BOM', 'Product Bundle']
+				'items': ['BOM', 'Product Bundle', 'Item Alternative']
 			},
 			{
 				'label': _('Pricing'),
diff --git a/erpnext/stock/doctype/item_alternative/__init__.py b/erpnext/stock/doctype/item_alternative/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/stock/doctype/item_alternative/__init__.py
diff --git a/erpnext/stock/doctype/item_alternative/item_alternative.js b/erpnext/stock/doctype/item_alternative/item_alternative.js
new file mode 100644
index 0000000..ef0a88b
--- /dev/null
+++ b/erpnext/stock/doctype/item_alternative/item_alternative.js
@@ -0,0 +1,14 @@
+// Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and contributors
+// For license information, please see license.txt
+
+frappe.ui.form.on('Item Alternative', {
+	setup: function(frm) {
+		frm.fields_dict.item_code.get_query = () => {
+			return {
+				filters: {
+					'allow_alternative_item': 1
+				}
+			};
+		};
+	}
+});
diff --git a/erpnext/stock/doctype/item_alternative/item_alternative.json b/erpnext/stock/doctype/item_alternative/item_alternative.json
new file mode 100644
index 0000000..a13e307
--- /dev/null
+++ b/erpnext/stock/doctype/item_alternative/item_alternative.json
@@ -0,0 +1,292 @@
+{
+ "allow_copy": 0, 
+ "allow_guest_to_view": 0, 
+ "allow_import": 0, 
+ "allow_rename": 0, 
+ "beta": 0, 
+ "creation": "2018-02-26 17:39:11.249778", 
+ "custom": 0, 
+ "docstatus": 0, 
+ "doctype": "DocType", 
+ "document_type": "", 
+ "editable_grid": 1, 
+ "engine": "InnoDB", 
+ "fields": [
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "item_code", 
+   "fieldtype": "Link", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 1, 
+   "in_standard_filter": 0, 
+   "label": "Item Code", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "Item", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "translatable": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "alternative_item_code", 
+   "fieldtype": "Link", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 1, 
+   "in_standard_filter": 0, 
+   "label": "Alternative Item Code", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "Item", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "translatable": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "two_way", 
+   "fieldtype": "Check", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Two-way", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "translatable": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "column_break_4", 
+   "fieldtype": "Column Break", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "translatable": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "item_name", 
+   "fieldtype": "Read Only", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Item Name", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "item_code.item_name", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "translatable": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "alternative_item_name", 
+   "fieldtype": "Read Only", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Alternative Item Name", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "alternative_item_code.item_name", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "translatable": 0, 
+   "unique": 0
+  }
+ ], 
+ "has_web_view": 0, 
+ "hide_heading": 0, 
+ "hide_toolbar": 0, 
+ "idx": 0, 
+ "image_view": 0, 
+ "in_create": 0, 
+ "is_submittable": 0, 
+ "issingle": 0, 
+ "istable": 0, 
+ "max_attachments": 0, 
+ "modified": "2018-03-07 16:08:08.097107", 
+ "modified_by": "Administrator", 
+ "module": "Stock", 
+ "name": "Item Alternative", 
+ "name_case": "", 
+ "owner": "Administrator", 
+ "permissions": [
+  {
+   "amend": 0, 
+   "apply_user_permissions": 0, 
+   "cancel": 0, 
+   "create": 1, 
+   "delete": 1, 
+   "email": 1, 
+   "export": 1, 
+   "if_owner": 0, 
+   "import": 0, 
+   "permlevel": 0, 
+   "print": 1, 
+   "read": 1, 
+   "report": 1, 
+   "role": "Stock User", 
+   "set_user_permissions": 0, 
+   "share": 1, 
+   "submit": 0, 
+   "write": 1
+  }, 
+  {
+   "amend": 0, 
+   "apply_user_permissions": 0, 
+   "cancel": 0, 
+   "create": 1, 
+   "delete": 1, 
+   "email": 1, 
+   "export": 1, 
+   "if_owner": 0, 
+   "import": 0, 
+   "permlevel": 0, 
+   "print": 1, 
+   "read": 1, 
+   "report": 1, 
+   "role": "Stock Manager", 
+   "set_user_permissions": 0, 
+   "share": 1, 
+   "submit": 0, 
+   "write": 1
+  }, 
+  {
+   "amend": 0, 
+   "apply_user_permissions": 0, 
+   "cancel": 0, 
+   "create": 1, 
+   "delete": 1, 
+   "email": 1, 
+   "export": 1, 
+   "if_owner": 0, 
+   "import": 0, 
+   "permlevel": 0, 
+   "print": 1, 
+   "read": 1, 
+   "report": 1, 
+   "role": "Item Manager", 
+   "set_user_permissions": 0, 
+   "share": 1, 
+   "submit": 0, 
+   "write": 1
+  }
+ ], 
+ "quick_entry": 0, 
+ "read_only": 0, 
+ "read_only_onload": 0, 
+ "show_name_in_global_search": 0, 
+ "sort_field": "modified", 
+ "sort_order": "DESC", 
+ "title_field": "item_code", 
+ "track_changes": 1, 
+ "track_seen": 0
+}
\ No newline at end of file
diff --git a/erpnext/stock/doctype/item_alternative/item_alternative.py b/erpnext/stock/doctype/item_alternative/item_alternative.py
new file mode 100644
index 0000000..6f9c5de
--- /dev/null
+++ b/erpnext/stock/doctype/item_alternative/item_alternative.py
@@ -0,0 +1,40 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2018, Frappe Technologies Pvt. Ltd. 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
+
+class ItemAlternative(Document):
+	def validate(self):
+		self.has_alternative_item()
+		self.validate_alternative_item()
+		self.validate_duplicate()
+
+	def has_alternative_item(self):
+		if (self.item_code and
+			not frappe.db.get_value('Item', self.item_code, 'allow_alternative_item')):
+			frappe.throw(_("Not allow to set alternative item for the item {0}").format(self.item_code))
+
+	def validate_alternative_item(self):
+		if self.item_code == self.alternative_item_code:
+			frappe.throw(_("Alternative item must not be same as item code"))
+
+	def validate_duplicate(self):
+		if frappe.db.get_value("Item Alternative", {'item_code': self.item_code,
+			'alternative_item_code': self.alternative_item_code, 'name': ('!=', self.name)}):
+			frappe.throw(_("Already record exists for the item {0}".format(self.item_code)))
+
+def get_alternative_items(doctype, txt, searchfield, start, page_len, filters):
+	return frappe.db.sql(""" (select alternative_item_code from `tabItem Alternative`
+			where item_code = %(item_code)s and alternative_item_code like %(txt)s)
+		union
+			(select item_code from `tabItem Alternative`
+			where alternative_item_code = %(item_code)s and item_code like %(txt)s
+			and two_way = 1) limit {0}, {1}
+		""".format(start, page_len), {
+			"item_code": frappe.db.escape(filters.get('item_code')),
+			"txt": "%%%s%%" % frappe.db.escape(txt)
+		})
\ No newline at end of file
diff --git a/erpnext/stock/doctype/item_alternative/test_item_alternative.js b/erpnext/stock/doctype/item_alternative/test_item_alternative.js
new file mode 100644
index 0000000..8731849
--- /dev/null
+++ b/erpnext/stock/doctype/item_alternative/test_item_alternative.js
@@ -0,0 +1,23 @@
+/* eslint-disable */
+// rename this file from _test_[name] to test_[name] to activate
+// and remove above this line
+
+QUnit.test("test: Item Alternative", function (assert) {
+	let done = assert.async();
+
+	// number of asserts
+	assert.expect(1);
+
+	frappe.run_serially([
+		// insert a new Item Alternative
+		() => frappe.tests.make('Item Alternative', [
+			// values to be set
+			{key: 'value'}
+		]),
+		() => {
+			assert.equal(cur_frm.doc.key, 'value');
+		},
+		() => done()
+	]);
+
+});
diff --git a/erpnext/stock/doctype/item_alternative/test_item_alternative.py b/erpnext/stock/doctype/item_alternative/test_item_alternative.py
new file mode 100644
index 0000000..d5700fe
--- /dev/null
+++ b/erpnext/stock/doctype/item_alternative/test_item_alternative.py
@@ -0,0 +1,133 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and Contributors
+# See license.txt
+from __future__ import unicode_literals
+import frappe, json
+from frappe.utils import flt
+from erpnext.stock.doctype.item.test_item import create_item
+from erpnext.manufacturing.doctype.production_plan.test_production_plan import make_bom
+from erpnext.buying.doctype.purchase_order.test_purchase_order import create_purchase_order
+from erpnext.manufacturing.doctype.work_order.work_order import make_stock_entry
+from erpnext.stock.doctype.stock_reconciliation.test_stock_reconciliation import create_stock_reconciliation
+from erpnext.manufacturing.doctype.work_order.test_work_order import make_wo_order_test_record
+from erpnext.buying.doctype.purchase_order.purchase_order import make_purchase_receipt, make_rm_stock_entry
+import unittest
+
+class TestItemAlternative(unittest.TestCase):
+	def setUp(self):
+		make_items()
+
+	def test_alternative_item_for_subcontract_rm(self):
+		create_stock_reconciliation(item_code='Alternate Item For A RW 1', warehouse='_Test Warehouse - _TC',
+			qty=5, rate=2000)
+		create_stock_reconciliation(item_code='Test FG A RW 2', warehouse='_Test Warehouse - _TC',
+			qty=5, rate=2000)
+
+		supplier_warehouse = "Test Supplier Warehouse - _TC"
+		po = create_purchase_order(item = "Test Finished Goods - A",
+			is_subcontracted='Yes', qty=5, rate=3000, supplier_warehouse=supplier_warehouse)
+
+		rm_item = [{"item_code": "Test Finished Goods - A", "rm_item_code": "Test FG A RW 1", "item_name":"Test FG A RW 1",
+					"qty":5, "warehouse":"_Test Warehouse - _TC", "rate":2000, "amount":10000, "stock_uom":"Nos"},
+					{"item_code": "Test Finished Goods - A", "rm_item_code": "Test FG A RW 2", "item_name":"Test FG A RW 2",
+					"qty":5, "warehouse":"_Test Warehouse - _TC", "rate":2000, "amount":10000, "stock_uom":"Nos"}]
+
+		rm_item_string = json.dumps(rm_item)
+		reserved_qty_for_sub_contract = frappe.db.get_value('Bin',
+			{'item_code': 'Test FG A RW 1', 'warehouse': '_Test Warehouse - _TC'}, 'reserved_qty_for_sub_contract')
+
+		se = frappe.get_doc(make_rm_stock_entry(po.name, rm_item_string))
+		se.to_warehouse = supplier_warehouse
+		se.insert()
+
+		doc = frappe.get_doc('Stock Entry', se.name)
+		for item in doc.items:
+			if item.item_code == 'Test FG A RW 1':
+				item.item_code = 'Alternate Item For A RW 1'
+				item.item_name = 'Alternate Item For A RW 1'
+				item.description = 'Alternate Item For A RW 1'
+				item.original_item = 'Test FG A RW 1'
+
+		doc.save()
+		doc.submit()
+		after_transfer_reserved_qty_for_sub_contract = frappe.db.get_value('Bin',
+			{'item_code': 'Test FG A RW 1', 'warehouse': '_Test Warehouse - _TC'}, 'reserved_qty_for_sub_contract')
+
+		self.assertEqual(after_transfer_reserved_qty_for_sub_contract, flt(reserved_qty_for_sub_contract - 5))
+
+		pr = make_purchase_receipt(po.name)
+		pr.save()
+
+		pr = frappe.get_doc('Purchase Receipt', pr.name)
+		status = False
+		for d in pr.supplied_items:
+			if d.rm_item_code == 'Alternate Item For A RW 1':
+				status = True
+
+		self.assertEqual(status, True)
+
+	def test_alternative_item_for_production_rm(self):
+		create_stock_reconciliation(item_code='Alternate Item For A RW 1',
+			warehouse='_Test Warehouse - _TC',qty=5, rate=2000)
+		create_stock_reconciliation(item_code='Test FG A RW 2', warehouse='_Test Warehouse - _TC',
+			qty=5, rate=2000)
+		pro_order = make_wo_order_test_record(production_item='Test Finished Goods - A',
+			qty=5, source_warehouse='_Test Warehouse - _TC', wip_warehouse='Test Supplier Warehouse - _TC')
+
+		reserved_qty_for_production = frappe.db.get_value('Bin',
+			{'item_code': 'Test FG A RW 1', 'warehouse': '_Test Warehouse - _TC'}, 'reserved_qty_for_production')
+
+		ste = frappe.get_doc(make_stock_entry(pro_order.name, "Material Transfer for Manufacture", 5))
+		ste.insert()
+
+		for item in ste.items:
+			if item.item_code == 'Test FG A RW 1':
+				item.item_code = 'Alternate Item For A RW 1'
+				item.item_name = 'Alternate Item For A RW 1'
+				item.description = 'Alternate Item For A RW 1'
+				item.original_item = 'Test FG A RW 1'
+
+		ste.submit()
+		reserved_qty_for_production_after_transfer = frappe.db.get_value('Bin',
+			{'item_code': 'Test FG A RW 1', 'warehouse': '_Test Warehouse - _TC'}, 'reserved_qty_for_production')
+
+		self.assertEqual(reserved_qty_for_production_after_transfer, flt(reserved_qty_for_production - 5))
+		ste1 = frappe.get_doc(make_stock_entry(pro_order.name, "Manufacture", 5))
+
+		status = False
+		for d in ste1.items:
+			if d.item_code == 'Alternate Item For A RW 1':
+				status = True
+
+		self.assertEqual(status, True)
+		ste1.submit()
+
+def make_items():
+	items = ['Test Finished Goods - A', 'Test FG A RW 1', 'Test FG A RW 2', 'Alternate Item For A RW 1']
+	for item_code in items:
+		if not frappe.db.exists('Item', item_code):
+			create_item(item_code)
+
+	create_stock_reconciliation(item_code="Test FG A RW 1",
+		warehouse='_Test Warehouse - _TC', qty=10, rate=2000)
+
+	if frappe.db.exists('Item', 'Test FG A RW 1'):
+		doc = frappe.get_doc('Item', 'Test FG A RW 1')
+		doc.allow_alternative_item = 1
+		doc.save()
+
+	if frappe.db.exists('Item', 'Test Finished Goods - A'):
+		doc = frappe.get_doc('Item', 'Test Finished Goods - A')
+		doc.is_sub_contracted_item = 1
+		doc.save()
+
+	if not frappe.db.get_value('BOM',
+		{'item': 'Test Finished Goods - A', 'docstatus': 1}):
+		make_bom(item = 'Test Finished Goods - A', raw_materials = ['Test FG A RW 1', 'Test FG A RW 2'])
+
+	if not frappe.db.get_value('Warehouse', {'warehouse_name': 'Test Supplier Warehouse'}):
+		frappe.get_doc({
+			'doctype': 'Warehouse',
+			'warehouse_name': 'Test Supplier Warehouse',
+			'company': '_Test Company'
+		}).insert(ignore_permissions=True)
diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.js b/erpnext/stock/doctype/stock_entry/stock_entry.js
index a149481..0979fe7 100644
--- a/erpnext/stock/doctype/stock_entry/stock_entry.js
+++ b/erpnext/stock/doctype/stock_entry/stock_entry.js
@@ -120,6 +120,25 @@
 			});
 		}
 
+		if(frm.doc.items) {
+			const has_alternative = frm.doc.items.find(i => i.allow_alternative_item === 1);
+
+			if (frm.doc.docstatus == 0 && has_alternative) {
+				frm.add_custom_button(__('Alternate Item'), () => {
+					erpnext.utils.select_alternate_items({
+						frm: frm,
+						child_docname: "items",
+						warehouse_field: "s_warehouse",
+						child_doctype: "Stock Entry Detail",
+						original_item_field: "original_item",
+						condition: (d) => {
+							if (d.s_warehouse && d.allow_alternative_item) {return true;}
+						}
+					})
+				});
+			}
+		}
+
 		if (frm.doc.docstatus===0) {
 			frm.add_custom_button(__('Purchase Invoice'), function() {
 				erpnext.utils.map_current_doc({
diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py
index 4f9cefe..7ef8ab0 100644
--- a/erpnext/stock/doctype/stock_entry/stock_entry.py
+++ b/erpnext/stock/doctype/stock_entry/stock_entry.py
@@ -400,9 +400,10 @@
 		if self.purpose == "Subcontract" and self.purchase_order:
 			purchase_order = frappe.get_doc("Purchase Order", self.purchase_order)
 			for se_item in self.items:
+				item_code = se_item.original_item or se_item.item_code
 				precision = cint(frappe.db.get_default("float_precision")) or 3
 				total_allowed = sum([flt(d.required_qty) for d in purchase_order.supplied_items \
-					if d.rm_item_code == se_item.item_code])
+					if d.rm_item_code == item_code])
 				if not total_allowed:
 					frappe.throw(_("Item {0} not found in 'Raw Materials Supplied' table in Purchase Order {1}")
 						.format(se_item.item_code, self.purchase_order))
@@ -421,7 +422,8 @@
 	def validate_bom(self):
 		for d in self.get('items'):
 			if d.bom_no and (d.t_warehouse != getattr(self, "pro_doc", frappe._dict()).scrap_warehouse):
-				validate_bom_no(d.item_code, d.bom_no)
+				item_code = d.original_item or d.item_code
+				validate_bom_no(item_code, d.bom_no)
 
 	def validate_finished_goods(self):
 		"""validation: finished good quantity should be same as manufacturing quantity"""
@@ -608,7 +610,6 @@
 					item_dict = self.get_bom_raw_materials(self.fg_completed_qty)
 
 					#Get PO Supplied Items Details
-					print('Purchase Order', self.purchase_order, self.purpose)
 					if self.purchase_order and self.purpose == "Subcontract":
 						#Get PO Supplied Items Details
 						item_wh = frappe._dict(frappe.db.sql("""
@@ -695,9 +696,23 @@
 		item_dict = get_bom_items_as_dict(self.bom_no, self.company, qty=qty,
 			fetch_exploded = self.use_multi_level_bom)
 
+		used_alternative_items = get_used_alternative_items(work_order = self.work_order)
 		for item in item_dict.values():
 			# if source warehouse presents in BOM set from_warehouse as bom source_warehouse
+			if item["allow_alternative_item"]:
+				item["allow_alternative_item"] = frappe.db.get_value('Work Order',
+					self.work_order, "allow_alternative_item")
+
 			item.from_warehouse = self.from_warehouse or item.source_warehouse or item.default_warehouse
+			if item.item_code in used_alternative_items:
+				alternative_item_data = used_alternative_items.get(item.item_code)
+				item.item_code = alternative_item_data.item_code
+				item.item_name = alternative_item_data.item_name
+				item.stock_uom = alternative_item_data.stock_uom
+				item.uom = alternative_item_data.uom
+				item.conversion_factor = alternative_item_data.conversion_factor
+				item.description = alternative_item_data.description
+
 		return item_dict
 
 	def get_bom_scrap_material(self, qty):
@@ -805,16 +820,19 @@
 			wip_warehouse = pro_order.wip_warehouse
 		else:
 			wip_warehouse = None
-			
+
 		for d in pro_order.get("required_items"):
 			if flt(d.required_qty) > flt(d.transferred_qty):
 				item_row = d.as_dict()
 				if d.source_warehouse and not frappe.db.get_value("Warehouse", d.source_warehouse, "is_group"):
 					item_row["from_warehouse"] = d.source_warehouse
-				
+
 				item_row["to_warehouse"] = wip_warehouse
+				if item_row["allow_alternative_item"]:
+					item_row["allow_alternative_item"] = pro_order.allow_alternative_item
+
 				item_dict.setdefault(d.item_code, item_row)
-			
+
 		return item_dict
 
 	def add_to_stock_entry_detail(self, item_dict, bom_no=None):
@@ -827,7 +845,7 @@
 			se_child = self.append('items')
 			se_child.s_warehouse = item_dict[d].get("from_warehouse")
 			se_child.t_warehouse = item_dict[d].get("to_warehouse")
-			se_child.item_code = cstr(d)
+			se_child.item_code = item_dict[d].get('item_code') or cstr(d)
 			se_child.item_name = item_dict[d]["item_name"]
 			se_child.description = item_dict[d]["description"]
 			se_child.uom = stock_uom
@@ -835,6 +853,7 @@
 			se_child.qty = flt(item_dict[d]["qty"], se_child.precision("qty"))
 			se_child.expense_account = item_dict[d].get("expense_account") or expense_account
 			se_child.cost_center = item_dict[d].get("cost_center") or cost_center
+			se_child.allow_alternative_item = item_dict[d].get("allow_alternative_item", 0)
 
 			if item_dict[d].get("idx"):
 				se_child.idx = item_dict[d].get("idx")
@@ -882,8 +901,9 @@
 
 		#Update reserved sub contracted quantity in bin based on Supplied Item Details
 		for d in self.get("items"):
-			reserve_warehouse = item_wh.get(d.item_code)
-			stock_bin = get_bin(d.item_code, reserve_warehouse)
+			item_code = d.get('original_item') or d.get('item_code')
+			reserve_warehouse = item_wh.get(item_code)
+			stock_bin = get_bin(item_code, reserve_warehouse)
 			stock_bin.update_reserved_qty_for_sub_contracting()
 	
 @frappe.whitelist()
@@ -976,6 +996,30 @@
 
 	return operating_cost_per_unit
 
+def get_used_alternative_items(purchase_order=None, work_order=None):
+	cond = ""
+
+	if purchase_order:
+		cond = "and ste.purpose = 'Subcontract' and ste.purchase_order = '{0}'".format(purchase_order)
+	elif work_order:
+		cond = "and ste.purpose = 'Material Transfer for Manufacture' and ste.work_order = '{0}'".format(work_order)
+
+	if not cond: return {}
+
+	used_alternative_items = {}
+	data = frappe.db.sql(""" select sted.original_item, sted.uom, sted.conversion_factor,
+			sted.item_code, sted.item_name, sted.conversion_factor,sted.stock_uom, sted.description
+		from
+			`tabStock Entry` ste, `tabStock Entry Detail` sted
+		where
+			sted.parent = ste.name and ste.docstatus = 1 and sted.original_item !=  sted.item_code
+			{0} """.format(cond), as_dict=1)
+
+	for d in data:
+		used_alternative_items[d.original_item] = d
+
+	return used_alternative_items
+
 @frappe.whitelist()
 def get_uom_details(item_code, uom, qty):
 	"""Returns dict `{"conversion_factor": [value], "transfer_qty": qty * [value]}`
diff --git a/erpnext/stock/doctype/stock_entry_detail/stock_entry_detail.json b/erpnext/stock/doctype/stock_entry_detail/stock_entry_detail.json
index 0f8bd17..5fdab6f 100644
--- a/erpnext/stock/doctype/stock_entry_detail/stock_entry_detail.json
+++ b/erpnext/stock/doctype/stock_entry_detail/stock_entry_detail.json
@@ -41,6 +41,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -70,6 +71,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -102,6 +104,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -130,6 +133,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -162,6 +166,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -190,6 +195,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -222,6 +228,7 @@
    "reqd": 1, 
    "search_index": 1, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -250,6 +257,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -279,6 +287,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -309,6 +318,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -341,6 +351,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0, 
    "width": "300px"
   }, 
@@ -371,6 +382,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -401,6 +413,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -432,6 +445,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -461,6 +475,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -492,6 +507,7 @@
    "reqd": 1, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -524,6 +540,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -555,6 +572,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -586,6 +604,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -618,6 +637,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -649,6 +669,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -677,6 +698,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -709,6 +731,7 @@
    "reqd": 1, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -740,6 +763,7 @@
    "reqd": 1, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -772,6 +796,7 @@
    "reqd": 1, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -803,6 +828,7 @@
    "reqd": 1, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -834,6 +860,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -866,6 +893,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -895,6 +923,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -926,6 +955,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -954,6 +984,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -986,6 +1017,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1018,6 +1050,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1047,6 +1080,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1078,6 +1112,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1106,6 +1141,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1138,6 +1174,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1167,6 +1204,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1197,6 +1235,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1228,6 +1267,7 @@
    "reqd": 0, 
    "search_index": 1, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1259,6 +1299,39 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "depends_on": "s_warehouse", 
+   "fieldname": "allow_alternative_item", 
+   "fieldtype": "Check", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Allow Alternative Item", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 1, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 1, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1287,6 +1360,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1318,6 +1392,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -1348,6 +1423,39 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "original_item", 
+   "fieldtype": "Link", 
+   "hidden": 1, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Original Item", 
+   "length": 0, 
+   "no_copy": 1, 
+   "options": "Item", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 1, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 1, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }
  ], 
@@ -1361,7 +1469,7 @@
  "issingle": 0, 
  "istable": 1, 
  "max_attachments": 0, 
- "modified": "2018-02-16 20:19:57.471380", 
+ "modified": "2018-03-05 13:09:25.849700", 
  "modified_by": "Administrator", 
  "module": "Stock", 
  "name": "Stock Entry Detail",