[subcontract] refactor, make stock entry from PO
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json
index 71c2287..820d830 100755
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json
@@ -8,7 +8,7 @@
   {
    "fieldname": "supplier_section", 
    "fieldtype": "Section Break", 
-   "label": "Supplier", 
+   "label": "", 
    "options": "icon-user", 
    "permlevel": 0
   }, 
@@ -315,7 +315,7 @@
   {
    "fieldname": "taxes_section", 
    "fieldtype": "Section Break", 
-   "label": "Taxes and Charges", 
+   "label": "", 
    "oldfieldtype": "Section Break", 
    "options": "icon-money", 
    "permlevel": 0, 
@@ -655,7 +655,7 @@
   {
    "fieldname": "terms_section_break", 
    "fieldtype": "Section Break", 
-   "label": "Terms and Conditions", 
+   "label": "", 
    "options": "icon-legal", 
    "permlevel": 0
   }, 
@@ -710,7 +710,7 @@
   {
    "fieldname": "more_info", 
    "fieldtype": "Section Break", 
-   "label": "More Info", 
+   "label": "", 
    "oldfieldtype": "Section Break", 
    "options": "icon-file-text", 
    "permlevel": 0, 
@@ -963,7 +963,7 @@
  "icon": "icon-file-text", 
  "idx": 1, 
  "is_submittable": 1, 
- "modified": "2015-02-24 15:59:49.908324", 
+ "modified": "2015-03-03 02:46:30.118418", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Purchase Invoice", 
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.json b/erpnext/accounts/doctype/sales_invoice/sales_invoice.json
index b909cb4..10aac98 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.json
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.json
@@ -749,7 +749,7 @@
   {
    "fieldname": "terms_section_break", 
    "fieldtype": "Section Break", 
-   "label": "Terms and Conditions", 
+   "label": "", 
    "oldfieldtype": "Section Break", 
    "options": "icon-legal", 
    "permlevel": 0, 
@@ -780,7 +780,7 @@
    "fieldname": "contact_section", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
-   "label": "Contact Info", 
+   "label": "", 
    "options": "icon-bullhorn", 
    "permlevel": 0, 
    "read_only": 0
@@ -1244,7 +1244,7 @@
  "icon": "icon-file-text", 
  "idx": 1, 
  "is_submittable": 1, 
- "modified": "2015-02-24 15:23:28.554373", 
+ "modified": "2015-03-03 02:47:05.467179", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Sales Invoice", 
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.js b/erpnext/buying/doctype/purchase_order/purchase_order.js
index 5da7ac9..c4c28e4 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.js
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.js
@@ -7,6 +7,7 @@
 
 erpnext.buying.PurchaseOrderController = erpnext.buying.BuyingController.extend({
 	refresh: function(doc, cdt, cdn) {
+		var me = this;
 		this._super();
 		// this.frm.dashboard.reset();
 
@@ -16,9 +17,14 @@
 			// cur_frm.dashboard.add_progress(cint(doc.per_billed) + __("% Billed"),
 			// 	doc.per_billed);
 
-			if(flt(doc.per_received, 2) < 100)
+			if(flt(doc.per_received, 2) < 100) {
 				cur_frm.add_custom_button(__('Make Purchase Receipt'),
-					this.make_purchase_receipt, frappe.boot.doctype_icons["Purchase Receipt"]);
+					this.make_purchase_receipt);
+				if(doc.is_subcontracted==="Yes") {
+					cur_frm.add_custom_button(__('Transfer Material to Supplier'),
+						function() { me.make_stock_entry() });
+				}
+			}
 			if(flt(doc.per_billed, 2) < 100)
 				cur_frm.add_custom_button(__('Make Invoice'), this.make_purchase_invoice,
 					frappe.boot.doctype_icons["Purchase Invoice"]);
@@ -35,6 +41,33 @@
 				cur_frm.cscript['Unstop Purchase Order'], "icon-check");
 	},
 
+	make_stock_entry: function() {
+		var items = $.map(cur_frm.doc.items, function(d) { return d.bom ? d.item_code : false; }),
+			me = this;
+		if(items.length===1) {
+			me._make_stock_entry(items[0]);
+			return;
+		}
+		frappe.prompt({fieldname:"item", options: items, fieldtype:"Select",
+			label: __("Select Item for Transfer"), reqd: 1}, function(data) {
+			me._make_stock_entry(data.item);
+		}, __("Select Item"), __("Make"));
+	},
+
+	_make_stock_entry: function(item) {
+		frappe.call({
+			method:"erpnext.buying.doctype.purchase_order.purchase_order.make_stock_entry",
+			args: {
+				purchase_order: cur_frm.doc.name,
+				item_code: item
+			},
+			callback: function(r) {
+				var doclist = frappe.model.sync(r.message);
+				frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
+			}
+		});
+	},
+
 	make_purchase_receipt: function() {
 		frappe.model.open_mapped_doc({
 			method: "erpnext.buying.doctype.purchase_order.purchase_order.make_purchase_receipt",
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.json b/erpnext/buying/doctype/purchase_order/purchase_order.json
index 6188fbc..8df47c8 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.json
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.json
@@ -9,7 +9,7 @@
   {
    "fieldname": "supplier_section", 
    "fieldtype": "Section Break", 
-   "label": "Supplier", 
+   "label": "", 
    "options": "icon-user", 
    "permlevel": 0
   }, 
@@ -40,6 +40,15 @@
    "search_index": 1
   }, 
   {
+   "default": "No", 
+   "fieldname": "is_subcontracted", 
+   "fieldtype": "Select", 
+   "label": "Supply Raw Materials", 
+   "options": "No\nYes", 
+   "permlevel": 0, 
+   "print_hide": 1
+  }, 
+  {
    "fieldname": "supplier_name", 
    "fieldtype": "Data", 
    "hidden": 0, 
@@ -290,7 +299,7 @@
   {
    "fieldname": "taxes_section", 
    "fieldtype": "Section Break", 
-   "label": "Taxes and Charges", 
+   "label": "", 
    "oldfieldtype": "Section Break", 
    "options": "icon-money", 
    "permlevel": 0, 
@@ -540,7 +549,7 @@
   {
    "fieldname": "terms_section_break", 
    "fieldtype": "Section Break", 
-   "label": "Terms and Conditions", 
+   "label": "", 
    "oldfieldtype": "Section Break", 
    "options": "icon-legal", 
    "permlevel": 0
@@ -567,7 +576,7 @@
    "depends_on": "supplier", 
    "fieldname": "contact_section", 
    "fieldtype": "Section Break", 
-   "label": "Contact Info", 
+   "label": "", 
    "options": "icon-bullhorn", 
    "permlevel": 0
   }, 
@@ -597,7 +606,7 @@
   {
    "fieldname": "more_info", 
    "fieldtype": "Section Break", 
-   "label": "More Info", 
+   "label": "", 
    "oldfieldtype": "Section Break", 
    "permlevel": 0
   }, 
@@ -617,13 +626,18 @@
    "search_index": 1
   }, 
   {
-   "default": "No", 
-   "fieldname": "is_subcontracted", 
-   "fieldtype": "Select", 
-   "label": "Is Subcontracted", 
-   "options": "\nYes\nNo", 
+   "fieldname": "fiscal_year", 
+   "fieldtype": "Link", 
+   "in_filter": 1, 
+   "label": "Fiscal Year", 
+   "no_copy": 0, 
+   "oldfieldname": "fiscal_year", 
+   "oldfieldtype": "Select", 
+   "options": "Fiscal Year", 
    "permlevel": 0, 
-   "print_hide": 1
+   "print_hide": 1, 
+   "reqd": 1, 
+   "search_index": 1
   }, 
   {
    "fieldname": "ref_sq", 
@@ -638,6 +652,15 @@
    "read_only": 1
   }, 
   {
+   "fieldname": "column_break5", 
+   "fieldtype": "Column Break", 
+   "oldfieldtype": "Column Break", 
+   "permlevel": 0, 
+   "print_hide": 1, 
+   "print_width": "50%", 
+   "width": "50%"
+  }, 
+  {
    "allow_on_submit": 1, 
    "fieldname": "letter_head", 
    "fieldtype": "Link", 
@@ -649,20 +672,6 @@
    "print_hide": 1
   }, 
   {
-   "fieldname": "fiscal_year", 
-   "fieldtype": "Link", 
-   "in_filter": 1, 
-   "label": "Fiscal Year", 
-   "no_copy": 0, 
-   "oldfieldname": "fiscal_year", 
-   "oldfieldtype": "Select", 
-   "options": "Fiscal Year", 
-   "permlevel": 0, 
-   "print_hide": 1, 
-   "reqd": 1, 
-   "search_index": 1
-  }, 
-  {
    "allow_on_submit": 1, 
    "fieldname": "select_print_heading", 
    "fieldtype": "Link", 
@@ -676,13 +685,11 @@
    "report_hide": 1
   }, 
   {
-   "fieldname": "column_break5", 
-   "fieldtype": "Column Break", 
-   "oldfieldtype": "Column Break", 
+   "fieldname": "sub_contracting", 
+   "fieldtype": "Section Break", 
+   "label": "", 
    "permlevel": 0, 
-   "print_hide": 1, 
-   "print_width": "50%", 
-   "width": "50%"
+   "precision": ""
   }, 
   {
    "depends_on": "eval:!doc.__islocal", 
@@ -699,6 +706,12 @@
    "read_only": 1
   }, 
   {
+   "fieldname": "column_break_74", 
+   "fieldtype": "Column Break", 
+   "permlevel": 0, 
+   "precision": ""
+  }, 
+  {
    "depends_on": "eval:!doc.__islocal", 
    "description": "% of materials billed against this Purchase Order.", 
    "fieldname": "per_billed", 
@@ -738,7 +751,7 @@
   {
    "fieldname": "recurring_order", 
    "fieldtype": "Section Break", 
-   "label": "Recurring Order", 
+   "label": "Recurring", 
    "options": "icon-time", 
    "permlevel": 0
   }, 
@@ -857,7 +870,7 @@
  "icon": "icon-file-text", 
  "idx": 1, 
  "is_submittable": 1, 
- "modified": "2015-02-24 16:00:36.892356", 
+ "modified": "2015-03-03 01:58:41.152545", 
  "modified_by": "Administrator", 
  "module": "Buying", 
  "name": "Purchase Order", 
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.py b/erpnext/buying/doctype/purchase_order/purchase_order.py
index 758318d..9e2d34e 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.py
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.py
@@ -291,3 +291,21 @@
 	}, target_doc, postprocess)
 
 	return doc
+
+@frappe.whitelist()
+def make_stock_entry(purchase_order, item_code):
+	purchase_order = frappe.get_doc("Purchase Order", purchase_order)
+
+	stock_entry = frappe.new_doc("Stock Entry")
+	stock_entry.purpose = "Subcontract"
+	stock_entry.purchase_order = purchase_order.name
+	stock_entry.supplier = purchase_order.supplier
+	stock_entry.supplier_name = purchase_order.supplier_name
+	stock_entry.supplier_address = purchase_order.address_display
+	stock_entry.company = purchase_order.company
+	stock_entry.from_bom = 1
+	po_item = [d for d in purchase_order.items if d.item_code == item_code][0]
+	stock_entry.fg_completed_qty = po_item.qty
+	stock_entry.bom_no = po_item.bom
+	stock_entry.get_items()
+	return stock_entry.as_dict()
diff --git a/erpnext/buying/doctype/purchase_order_item_supplied/purchase_order_item_supplied.json b/erpnext/buying/doctype/purchase_order_item_supplied/purchase_order_item_supplied.json
index df947e6..1482b63 100644
--- a/erpnext/buying/doctype/purchase_order_item_supplied/purchase_order_item_supplied.json
+++ b/erpnext/buying/doctype/purchase_order_item_supplied/purchase_order_item_supplied.json
@@ -1,32 +1,9 @@
 {
- "creation": "2013-02-22 01:27:42.000000", 
+ "creation": "2013-02-22 01:27:42", 
  "docstatus": 0, 
  "doctype": "DocType", 
  "fields": [
   {
-   "fieldname": "reference_name", 
-   "fieldtype": "Data", 
-   "hidden": 0, 
-   "in_filter": 0, 
-   "in_list_view": 1, 
-   "label": "Reference Name", 
-   "oldfieldname": "reference_name", 
-   "oldfieldtype": "Data", 
-   "permlevel": 0, 
-   "read_only": 1, 
-   "search_index": 0
-  }, 
-  {
-   "fieldname": "bom_detail_no", 
-   "fieldtype": "Data", 
-   "in_list_view": 1, 
-   "label": "BOM Detail No", 
-   "oldfieldname": "bom_detail_no", 
-   "oldfieldtype": "Data", 
-   "permlevel": 0, 
-   "read_only": 1
-  }, 
-  {
    "fieldname": "main_item_code", 
    "fieldtype": "Data", 
    "in_list_view": 1, 
@@ -50,7 +27,7 @@
    "fieldname": "required_qty", 
    "fieldtype": "Float", 
    "in_list_view": 1, 
-   "label": "Required Qty", 
+   "label": "Supplied Qty", 
    "oldfieldname": "required_qty", 
    "oldfieldtype": "Currency", 
    "permlevel": 0, 
@@ -77,6 +54,29 @@
    "read_only": 1
   }, 
   {
+   "fieldname": "bom_detail_no", 
+   "fieldtype": "Data", 
+   "in_list_view": 1, 
+   "label": "BOM Detail No", 
+   "oldfieldname": "bom_detail_no", 
+   "oldfieldtype": "Data", 
+   "permlevel": 0, 
+   "read_only": 1
+  }, 
+  {
+   "fieldname": "reference_name", 
+   "fieldtype": "Data", 
+   "hidden": 0, 
+   "in_filter": 0, 
+   "in_list_view": 1, 
+   "label": "Reference Name", 
+   "oldfieldname": "reference_name", 
+   "oldfieldtype": "Data", 
+   "permlevel": 0, 
+   "read_only": 1, 
+   "search_index": 0
+  }, 
+  {
    "fieldname": "conversion_factor", 
    "fieldtype": "Float", 
    "hidden": 1, 
@@ -100,9 +100,10 @@
  "hide_toolbar": 1, 
  "idx": 1, 
  "istable": 1, 
- "modified": "2013-12-20 19:23:33.000000", 
+ "modified": "2015-03-03 03:40:45.982762", 
  "modified_by": "Administrator", 
  "module": "Buying", 
  "name": "Purchase Order Item Supplied", 
- "owner": "dhanalekshmi@webnotestech.com"
+ "owner": "dhanalekshmi@webnotestech.com", 
+ "permissions": []
 }
\ No newline at end of file
diff --git a/erpnext/controllers/buying_controller.py b/erpnext/controllers/buying_controller.py
index 95318be..9b1ab7a 100644
--- a/erpnext/controllers/buying_controller.py
+++ b/erpnext/controllers/buying_controller.py
@@ -149,12 +149,11 @@
 	def create_raw_materials_supplied(self, raw_material_table):
 		if self.is_subcontracted=="Yes":
 			parent_items = []
-			rm_supplied_idx = 0
 			for item in self.get("items"):
 				if self.doctype == "Purchase Receipt":
 					item.rm_supp_cost = 0.0
 				if item.item_code in self.sub_contracted_items:
-					self.update_raw_materials_supplied(item, raw_material_table, rm_supplied_idx)
+					self.update_raw_materials_supplied(item, raw_material_table)
 
 					if [item.item_code, item.name] not in parent_items:
 						parent_items.append([item.item_code, item.name])
@@ -165,7 +164,7 @@
 			for item in self.get("items"):
 				item.rm_supp_cost = 0.0
 
-	def update_raw_materials_supplied(self, item, raw_material_table, rm_supplied_idx):
+	def update_raw_materials_supplied(self, item, raw_material_table):
 		bom_items = self.get_items_from_bom(item.item_code, item.bom)
 		raw_materials_cost = 0
 
@@ -190,7 +189,6 @@
 			rm.required_qty = required_qty
 
 			rm.conversion_factor = item.conversion_factor
-			rm.idx = rm_supplied_idx
 
 			if self.doctype == "Purchase Receipt":
 				rm.consumed_qty = required_qty
@@ -198,8 +196,6 @@
 				if item.batch_no and not rm.batch_no:
 					rm.batch_no = item.batch_no
 
-			rm_supplied_idx += 1
-
 			# get raw materials rate
 			if self.doctype == "Purchase Receipt":
 				from erpnext.stock.utils import get_incoming_rate
diff --git a/erpnext/stock/doctype/item_price/item_price.json b/erpnext/stock/doctype/item_price/item_price.json
index 73c967a..b8194d9 100644
--- a/erpnext/stock/doctype/item_price/item_price.json
+++ b/erpnext/stock/doctype/item_price/item_price.json
@@ -18,6 +18,7 @@
    "fieldname": "price_list", 
    "fieldtype": "Link", 
    "in_filter": 1, 
+   "in_list_view": 1, 
    "label": "Price List", 
    "options": "Price List", 
    "permlevel": 0, 
@@ -26,7 +27,7 @@
   {
    "fieldname": "buying", 
    "fieldtype": "Check", 
-   "in_list_view": 1, 
+   "in_list_view": 0, 
    "label": "Buying", 
    "permlevel": 0, 
    "read_only": 1
@@ -34,7 +35,7 @@
   {
    "fieldname": "selling", 
    "fieldtype": "Check", 
-   "in_list_view": 1, 
+   "in_list_view": 0, 
    "label": "Selling", 
    "permlevel": 0, 
    "read_only": 1
@@ -50,7 +51,7 @@
    "fieldname": "item_code", 
    "fieldtype": "Link", 
    "in_filter": 1, 
-   "in_list_view": 1, 
+   "in_list_view": 0, 
    "label": "Item Code", 
    "oldfieldname": "price_list_name", 
    "oldfieldtype": "Select", 
@@ -105,7 +106,7 @@
  "idx": 1, 
  "in_create": 0, 
  "istable": 0, 
- "modified": "2015-02-20 05:04:11.609416", 
+ "modified": "2015-03-03 01:05:09.876025", 
  "modified_by": "Administrator", 
  "module": "Stock", 
  "name": "Item Price", 
@@ -138,5 +139,6 @@
    "write": 1
   }
  ], 
- "read_only": 0
+ "read_only": 0, 
+ "title_field": "item_code"
 }
\ No newline at end of file
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js
index 9acf38c..a2d5be5 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js
@@ -32,6 +32,8 @@
 					})
 				}, "icon-download", "btn-default");
 		}
+
+		this.frm.toggle_reqd("supplier_warehouse", this.frm.doc.is_subcontracted==="Yes");
 	},
 
 	received_qty: function(doc, cdt, cdn) {
@@ -178,4 +180,5 @@
 	if (frm.doc.is_subcontracted === "Yes") {
 		erpnext.buying.get_default_bom(frm);
 	}
+	frm.toggle_reqd("supplier_warehouse", frm.doc.is_subcontracted==="Yes");
 });
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.json b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.json
index fb279db..8aeb4ac 100755
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.json
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.json
@@ -8,7 +8,7 @@
   {
    "fieldname": "supplier_section", 
    "fieldtype": "Section Break", 
-   "label": "Supplier", 
+   "label": "", 
    "options": "icon-user", 
    "permlevel": 0
   }, 
@@ -292,7 +292,7 @@
    "description": "Add / Edit Taxes and Charges", 
    "fieldname": "taxes_section", 
    "fieldtype": "Section Break", 
-   "label": "Taxes and Charges", 
+   "label": "", 
    "oldfieldtype": "Section Break", 
    "options": "icon-money", 
    "permlevel": 0
@@ -464,7 +464,7 @@
    "read_only": 1
   }, 
   {
-   "description": "In Words will be visible once you save the Purchase Receipt.", 
+   "description": "", 
    "fieldname": "base_in_words", 
    "fieldtype": "Data", 
    "label": "In Words (Company Currency)", 
@@ -521,7 +521,7 @@
   {
    "fieldname": "terms_section_break", 
    "fieldtype": "Section Break", 
-   "label": "Terms and Conditions", 
+   "label": "", 
    "oldfieldtype": "Section Break", 
    "options": "icon-legal", 
    "permlevel": 0
@@ -548,7 +548,7 @@
    "depends_on": "supplier", 
    "fieldname": "contact_section", 
    "fieldtype": "Section Break", 
-   "label": "Contact Info", 
+   "label": "", 
    "options": "icon-bullhorn", 
    "permlevel": 0
   }, 
@@ -561,11 +561,6 @@
    "print_hide": 1
   }, 
   {
-   "fieldname": "column_break_57", 
-   "fieldtype": "Column Break", 
-   "permlevel": 0
-  }, 
-  {
    "fieldname": "contact_person", 
    "fieldtype": "Link", 
    "label": "Contact Person", 
@@ -574,9 +569,60 @@
    "print_hide": 1
   }, 
   {
+   "fieldname": "column_break_57", 
+   "fieldtype": "Column Break", 
+   "permlevel": 0
+  }, 
+  {
+   "default": "No", 
+   "description": "", 
+   "fieldname": "is_subcontracted", 
+   "fieldtype": "Select", 
+   "label": "Raw Materials Supplied", 
+   "oldfieldname": "is_subcontracted", 
+   "oldfieldtype": "Select", 
+   "options": "No\nYes", 
+   "permlevel": 0, 
+   "print_hide": 1
+  }, 
+  {
+   "description": "", 
+   "fieldname": "supplier_warehouse", 
+   "fieldtype": "Link", 
+   "label": "Supplier Warehouse", 
+   "no_copy": 1, 
+   "oldfieldname": "supplier_warehouse", 
+   "oldfieldtype": "Link", 
+   "options": "Warehouse", 
+   "permlevel": 0, 
+   "print_hide": 1, 
+   "print_width": "50px", 
+   "width": "50px"
+  }, 
+  {
+   "fieldname": "bill_no", 
+   "fieldtype": "Data", 
+   "hidden": 1, 
+   "label": "Bill No", 
+   "oldfieldname": "bill_no", 
+   "oldfieldtype": "Data", 
+   "permlevel": 0, 
+   "print_hide": 1
+  }, 
+  {
+   "fieldname": "bill_date", 
+   "fieldtype": "Date", 
+   "hidden": 1, 
+   "label": "Bill Date", 
+   "oldfieldname": "bill_date", 
+   "oldfieldtype": "Date", 
+   "permlevel": 0, 
+   "print_hide": 1
+  }, 
+  {
    "fieldname": "more_info", 
    "fieldtype": "Section Break", 
-   "label": "More Info", 
+   "label": "", 
    "oldfieldtype": "Section Break", 
    "options": "icon-file-text", 
    "permlevel": 0
@@ -599,16 +645,17 @@
    "width": "150px"
   }, 
   {
-   "default": "No", 
-   "description": "Select \"Yes\" for sub - contracting items", 
-   "fieldname": "is_subcontracted", 
-   "fieldtype": "Select", 
-   "label": "Is Subcontracted", 
-   "oldfieldname": "is_subcontracted", 
-   "oldfieldtype": "Select", 
-   "options": "\nYes\nNo", 
+   "description": "Warehouse where you are maintaining stock of rejected items", 
+   "fieldname": "rejected_warehouse", 
+   "fieldtype": "Link", 
+   "label": "Rejected Warehouse", 
+   "no_copy": 1, 
+   "oldfieldname": "rejected_warehouse", 
+   "oldfieldtype": "Link", 
+   "options": "Warehouse", 
    "permlevel": 0, 
-   "print_hide": 1
+   "print_hide": 1, 
+   "reqd": 0
   }, 
   {
    "fieldname": "amended_from", 
@@ -637,26 +684,6 @@
    "print_hide": 1
   }, 
   {
-   "fieldname": "bill_no", 
-   "fieldtype": "Data", 
-   "hidden": 1, 
-   "label": "Bill No", 
-   "oldfieldname": "bill_no", 
-   "oldfieldtype": "Data", 
-   "permlevel": 0, 
-   "print_hide": 1
-  }, 
-  {
-   "fieldname": "bill_date", 
-   "fieldtype": "Date", 
-   "hidden": 1, 
-   "label": "Bill Date", 
-   "oldfieldname": "bill_date", 
-   "oldfieldtype": "Date", 
-   "permlevel": 0, 
-   "print_hide": 1
-  }, 
-  {
    "allow_on_submit": 1, 
    "fieldname": "letter_head", 
    "fieldtype": "Link", 
@@ -734,33 +761,6 @@
    "width": "30%"
   }, 
   {
-   "description": "Warehouse where you are maintaining stock of rejected items", 
-   "fieldname": "rejected_warehouse", 
-   "fieldtype": "Link", 
-   "label": "Rejected Warehouse", 
-   "no_copy": 1, 
-   "oldfieldname": "rejected_warehouse", 
-   "oldfieldtype": "Link", 
-   "options": "Warehouse", 
-   "permlevel": 0, 
-   "print_hide": 1, 
-   "reqd": 0
-  }, 
-  {
-   "description": "Supplier warehouse where you have issued raw materials for sub - contracting", 
-   "fieldname": "supplier_warehouse", 
-   "fieldtype": "Link", 
-   "label": "Supplier Warehouse", 
-   "no_copy": 1, 
-   "oldfieldname": "supplier_warehouse", 
-   "oldfieldtype": "Link", 
-   "options": "Warehouse", 
-   "permlevel": 0, 
-   "print_hide": 1, 
-   "print_width": "50px", 
-   "width": "50px"
-  }, 
-  {
    "fieldname": "instructions", 
    "fieldtype": "Small Text", 
    "label": "Instructions", 
@@ -824,7 +824,7 @@
    "width": "50%"
   }, 
   {
-   "description": "Following table will show values if items are sub - contracted. These values will be fetched from the master of \"Bill of Materials\" of sub - contracted items.", 
+   "description": "Automatically updated from BOM table", 
    "fieldname": "raw_material_details", 
    "fieldtype": "Section Break", 
    "label": "Raw Materials Supplied", 
@@ -850,7 +850,7 @@
  "icon": "icon-truck", 
  "idx": 1, 
  "is_submittable": 1, 
- "modified": "2015-02-24 16:00:11.869752", 
+ "modified": "2015-03-03 05:12:46.199024", 
  "modified_by": "Administrator", 
  "module": "Stock", 
  "name": "Purchase Receipt", 
diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.js b/erpnext/stock/doctype/stock_entry/stock_entry.js
index aa4cedb..34f0ecb 100644
--- a/erpnext/stock/doctype/stock_entry/stock_entry.js
+++ b/erpnext/stock/doctype/stock_entry/stock_entry.js
@@ -245,23 +245,23 @@
 
 	from_warehouse: function(doc) {
 		var me = this;
-		this.set_warehouse_if_missing("s_warehouse", doc.from_warehouse, function(row) {
+		this.set_warehouse_if_different("s_warehouse", doc.from_warehouse, function(row) {
 			return me.source_mandatory.indexOf(me.frm.doc.purpose)!==-1;
 		});
 	},
 
 	to_warehouse: function(doc) {
 		var me = this;
-		this.set_warehouse_if_missing("t_warehouse", doc.to_warehouse, function(row) {
+		this.set_warehouse_if_different("t_warehouse", doc.to_warehouse, function(row) {
 			return me.target_mandatory.indexOf(me.frm.doc.purpose)!==-1;
 		});
 	},
 
-	set_warehouse_if_missing: function(fieldname, value, condition) {
+	set_warehouse_if_different: function(fieldname, value, condition) {
 		var changed = false;
 		for (var i=0, l=(this.frm.doc.items || []).length; i<l; i++) {
 			var row = this.frm.doc.items[i];
-			if (!row[fieldname]) {
+			if (row[fieldname] != value) {
 				if (condition && !condition(row)) {
 					continue;
 				}
@@ -353,20 +353,6 @@
 
 	cur_frm.cscript.toggle_enable_bom();
 
-	if(doc.purpose == 'Purchase Return') {
-		doc.customer = doc.customer_name = doc.customer_address =
-			doc.delivery_note_no = doc.sales_invoice_no = null;
-		doc.bom_no = doc.production_order = doc.fg_completed_qty = null;
-	} else if(doc.purpose == 'Sales Return') {
-		doc.supplier=doc.supplier_name = doc.supplier_address = doc.purchase_receipt_no=null;
-		doc.bom_no = doc.production_order = doc.fg_completed_qty = null;
-	} else {
-		doc.customer = doc.customer_name = doc.customer_address =
-			doc.delivery_note_no = doc.sales_invoice_no = doc.supplier =
-			doc.supplier_name = doc.supplier_address = doc.purchase_receipt_no = null;
-	}
-
-
 }
 
 cur_frm.fields_dict['production_order'].get_query = function(doc) {
diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.json b/erpnext/stock/doctype/stock_entry/stock_entry.json
index 16e7a89..e19a803 100644
--- a/erpnext/stock/doctype/stock_entry/stock_entry.json
+++ b/erpnext/stock/doctype/stock_entry/stock_entry.json
@@ -82,6 +82,15 @@
    "search_index": 1
   }, 
   {
+   "depends_on": "eval:doc.purpose==\"Subcontract\"", 
+   "fieldname": "purchase_order", 
+   "fieldtype": "Link", 
+   "label": "Purchase Order", 
+   "options": "Purchase Order", 
+   "permlevel": 0, 
+   "precision": ""
+  }, 
+  {
    "allow_on_submit": 0, 
    "depends_on": "eval:doc.purpose==\"Sales Return\"", 
    "fieldname": "delivery_note_no", 
@@ -224,7 +233,7 @@
    "fieldtype": "Float", 
    "hidden": 0, 
    "in_filter": 0, 
-   "label": "Manufacturing Quantity", 
+   "label": "For Quantity", 
    "no_copy": 0, 
    "oldfieldname": "fg_completed_qty", 
    "oldfieldtype": "Currency", 
@@ -400,16 +409,16 @@
    "permlevel": 0
   }, 
   {
-   "depends_on": "eval:(doc.purpose==\"Sales Return\" || doc.purpose==\"Purchase Return\")", 
+   "depends_on": "eval: in_list([\"Sales Return\", \"Purchase Return\", \"Subcontract\"], doc.purpose)", 
    "fieldname": "contact_section", 
    "fieldtype": "Section Break", 
-   "label": "Contact Info", 
+   "label": "", 
    "permlevel": 0, 
    "read_only": 0
   }, 
   {
    "allow_on_submit": 0, 
-   "depends_on": "eval:doc.purpose==\"Purchase Return\"", 
+   "depends_on": "eval:doc.purpose==\"Purchase Return\" || doc.purpose==\"Subcontract\"", 
    "fieldname": "supplier", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -428,7 +437,7 @@
   }, 
   {
    "allow_on_submit": 0, 
-   "depends_on": "eval:doc.purpose==\"Purchase Return\"", 
+   "depends_on": "eval:doc.purpose==\"Purchase Return\" || doc.purpose==\"Subcontract\"", 
    "fieldname": "supplier_name", 
    "fieldtype": "Data", 
    "hidden": 0, 
@@ -446,7 +455,7 @@
   }, 
   {
    "allow_on_submit": 0, 
-   "depends_on": "eval:doc.purpose==\"Purchase Return\"", 
+   "depends_on": "eval:doc.purpose==\"Purchase Return\" || doc.purpose==\"Subcontract\"", 
    "fieldname": "supplier_address", 
    "fieldtype": "Small Text", 
    "hidden": 0, 
@@ -463,6 +472,12 @@
    "search_index": 0
   }, 
   {
+   "fieldname": "column_break_39", 
+   "fieldtype": "Column Break", 
+   "permlevel": 0, 
+   "precision": ""
+  }, 
+  {
    "allow_on_submit": 0, 
    "depends_on": "eval:doc.purpose==\"Sales Return\"", 
    "fieldname": "customer", 
@@ -520,7 +535,7 @@
   {
    "fieldname": "more_info", 
    "fieldtype": "Section Break", 
-   "label": "More Info", 
+   "label": "", 
    "oldfieldtype": "Section Break", 
    "permlevel": 0, 
    "read_only": 0
@@ -654,7 +669,7 @@
  "is_submittable": 1, 
  "issingle": 0, 
  "max_attachments": 0, 
- "modified": "2015-02-25 06:13:11.899840", 
+ "modified": "2015-03-03 01:53:07.157141", 
  "modified_by": "Administrator", 
  "module": "Stock", 
  "name": "Stock Entry", 
diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py
index 3a016ca..a76e2b7 100644
--- a/erpnext/stock/doctype/stock_entry/stock_entry.py
+++ b/erpnext/stock/doctype/stock_entry/stock_entry.py
@@ -73,6 +73,7 @@
 		from erpnext.stock.doctype.serial_no.serial_no import update_serial_nos_after_submit
 		update_serial_nos_after_submit(self, "items")
 		self.update_production_order()
+		self.validate_purchase_order()
 		self.make_gl_entries()
 
 	def on_cancel(self):
@@ -286,20 +287,22 @@
 			if not d.t_warehouse:
 				raw_material_cost += flt(d.amount)
 
+
 		self.add_operation_cost(raw_material_cost, force)
 
 	def add_operation_cost(self, raw_material_cost, force):
 		"""Adds operating cost if Production Order is set"""
 		# set incoming rate for fg item
-		number_of_fg_items = len([t.t_warehouse for t in self.get("items") if t.t_warehouse])
-		for d in self.get("items"):
-			if (d.t_warehouse and number_of_fg_items == 1):
-				operation_cost_per_unit = 0.0
-				if self.production_order:
-					operation_cost_per_unit = self.get_operation_cost_per_unit(d.bom_no, d.qty)
-				d.incoming_rate = operation_cost_per_unit + (raw_material_cost / flt(d.transfer_qty))
-				d.amount = flt(flt(d.transfer_qty) * flt(d.incoming_rate), self.precision("transfer_qty", d))
-				break
+		if self.purpose in ("Manufacture", "Repack"):
+			number_of_fg_items = len([t.t_warehouse for t in self.get("items") if t.t_warehouse])
+			for d in self.get("items"):
+				if (d.t_warehouse and number_of_fg_items == 1):
+					operation_cost_per_unit = 0.0
+					if self.production_order:
+						operation_cost_per_unit = self.get_operation_cost_per_unit(d.bom_no, d.qty)
+					d.incoming_rate = operation_cost_per_unit + (raw_material_cost / flt(d.transfer_qty))
+					d.amount = flt(flt(d.transfer_qty) * flt(d.incoming_rate), self.precision("transfer_qty", d))
+					break
 
 	def get_operation_cost_per_unit(self, bom_no, qty):
 		"""Returns operating cost from Production Order for given `bom_no`"""
@@ -342,6 +345,26 @@
 
 		return incoming_rate
 
+	def validate_purchase_order(self):
+		"""Throw exception if more raw material is transferred against Purchase Order than in
+		the raw materials supplied table"""
+		if self.purpose == "Subcontract" and self.purchase_order:
+			purchase_order = frappe.get_doc("Purchase Order", self.purchase_order)
+			for se_item in self.items:
+				total_allowed = [d.required_qty for d in purchase_order.supplied_items \
+					if d.rm_item_code == se_item.item_code][0]
+				total_supplied = frappe.db.sql("""select sum(qty)
+					from `tabStock Entry Detail`, `tabStock Entry`
+					where `tabStock Entry`.purchase_order = %s
+						and `tabStock Entry`.docstatus = 1
+						and `tabStock Entry Detail`.item_code = %s
+						and `tabStock Entry Detail`.parent = `tabStock Entry`.name""",
+							(self.purchase_order, se_item.item_code))[0][0]
+
+				if total_supplied > total_allowed:
+					frappe.throw(_("Not allowed to tranfer more {0} than {1} against Purchase Order {2}").format(se_item.item_code,
+						total_allowed, self.purchase_order))
+
 	def validate_bom(self):
 		for d in self.get('items'):
 			if d.bom_no: