Modifications in last purchase rate button (#14948)

* Make last purchase rate button visible until submitted

* Move buttons

- Get last purchase rate and link to material request buttons moved to toolbar under Tools button

* Remove disable fetch last rate functionality

* Make last purchase rate field read_only
diff --git a/erpnext/buying/doctype/buying_settings/buying_settings.json b/erpnext/buying/doctype/buying_settings/buying_settings.json
index bb2b47d..d6657bb 100644
--- a/erpnext/buying/doctype/buying_settings/buying_settings.json
+++ b/erpnext/buying/doctype/buying_settings/buying_settings.json
@@ -1,231 +1,276 @@
 {
  "allow_copy": 0, 
+ "allow_guest_to_view": 0, 
  "allow_import": 0, 
  "allow_rename": 0, 
+ "beta": 0, 
  "creation": "2013-06-25 11:04:03", 
  "custom": 0, 
  "description": "Settings for Buying Module", 
  "docstatus": 0, 
  "doctype": "DocType", 
  "document_type": "Other", 
+ "editable_grid": 0, 
  "fields": [
   {
+   "allow_bulk_edit": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "default": "Supplier Name", 
    "fieldname": "supp_master_name", 
    "fieldtype": "Select", 
    "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": "Supplier Naming By", 
+   "length": 0, 
    "no_copy": 0, 
    "options": "Supplier Name\nNaming Series", 
    "permlevel": 0, 
    "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": "supplier_type", 
    "fieldtype": "Link", 
    "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": "Default Supplier Type", 
+   "length": 0, 
    "no_copy": 0, 
    "options": "Supplier Type", 
    "permlevel": 0, 
    "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": "buying_price_list", 
    "fieldtype": "Link", 
    "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": "Default Buying Price List", 
+   "length": 0, 
    "no_copy": 0, 
    "options": "Price List", 
    "permlevel": 0, 
    "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_3", 
    "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, 
    "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": "po_required", 
    "fieldtype": "Select", 
    "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": "Purchase Order Required", 
+   "length": 0, 
    "no_copy": 0, 
    "options": "No\nYes", 
    "permlevel": 0, 
    "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": "pr_required", 
    "fieldtype": "Select", 
    "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": "Purchase Receipt Required", 
+   "length": 0, 
    "no_copy": 0, 
    "options": "No\nYes", 
    "permlevel": 0, 
    "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": "maintain_same_rate", 
    "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": "Maintain same rate throughout purchase cycle", 
+   "length": 0, 
    "no_copy": 0, 
    "permlevel": 0, 
    "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": "allow_multiple_items", 
    "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 Item to be added multiple times in a transaction", 
+   "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, 
-   "unique": 0
-  },
-  {
-   "allow_bulk_edit": 0,
-   "allow_on_submit": 0,
-   "bold": 0,
-   "collapsible": 0,
-   "columns": 0,
-   "default": "0",
-   "description": "If enabled, last purchase details of items will not be fetched from previous purchase order or purchase receipt",
-   "fieldname": "disable_fetch_last_purchase_rate",
-   "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": "Disable Fetching Last Purchase Details in Purchase Order",
-   "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
   }
  ], 
+ "has_web_view": 0, 
  "hide_heading": 0, 
  "hide_toolbar": 0, 
  "icon": "fa fa-cog", 
  "idx": 1, 
+ "image_view": 0, 
  "in_create": 0, 
- "in_dialog": 0, 
  "is_submittable": 0, 
  "issingle": 1, 
  "istable": 0, 
- "modified": "2017-12-27 15:20:06.052342", 
+ "max_attachments": 0, 
+ "modified": "2018-07-18 07:52:38.062488", 
  "modified_by": "Administrator", 
  "module": "Buying", 
  "name": "Buying Settings", 
@@ -252,6 +297,10 @@
    "write": 1
   }
  ], 
+ "quick_entry": 0, 
  "read_only": 0, 
- "read_only_onload": 0
+ "read_only_onload": 0, 
+ "show_name_in_global_search": 0, 
+ "track_changes": 0, 
+ "track_seen": 0
 }
\ No newline at end of file
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.js b/erpnext/buying/doctype/purchase_order/purchase_order.js
index 27d7a2b..a668f86 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.js
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.js
@@ -32,12 +32,7 @@
 		erpnext.queries.setup_queries(frm, "Warehouse", function() {
 			return erpnext.queries.warehouse(frm.doc);
 		});
-
-		if (frm.doc.__onload) {
-			frm.toggle_display('get_last_purchase_rate',
-				frm.doc.__onload.disable_fetch_last_purchase_rate);
-		}
-	},
+	}
 });
 
 frappe.ui.form.on("Purchase Order Item", {
@@ -318,6 +313,73 @@
 				})
 			}, __("Add items from"));
 
+		this.frm.add_custom_button(__('Update rate as per last purchase'),
+			function() {
+				frappe.call({
+					"method": "get_last_purchase_rate",
+					"doc": me.frm.doc,
+					callback: function(r, rt) {
+						me.frm.dirty();
+						me.frm.cscript.calculate_taxes_and_totals();
+					}
+				})
+			}, __("Tools"));
+
+		this.frm.add_custom_button(__('Link to Material Request'),
+		function() {
+			var my_items = [];
+			for (var i in me.frm.doc.items) {
+				if(!me.frm.doc.items[i].material_request){
+					my_items.push(me.frm.doc.items[i].item_code);
+				}
+			}
+			frappe.call({
+				method: "erpnext.buying.utils.get_linked_material_requests",
+				args:{
+					items: my_items
+				},
+				callback: function(r) {
+					if(r.exc) return;
+	
+					var i = 0;
+					var item_length = me.frm.doc.items.length;
+					while (i < item_length) {
+						var qty = me.frm.doc.items[i].qty;
+						(r.message[0] || []).forEach(function(d) {
+							if (d.qty > 0 && qty > 0 && me.frm.doc.items[i].item_code == d.item_code && !me.frm.doc.items[i].material_request_item)
+							{
+								me.frm.doc.items[i].material_request = d.mr_name;
+								me.frm.doc.items[i].material_request_item = d.mr_item;
+								var my_qty = Math.min(qty, d.qty);
+								qty = qty - my_qty;
+								d.qty = d.qty  - my_qty;
+								me.frm.doc.items[i].stock_qty = my_qty * me.frm.doc.items[i].conversion_factor;
+								me.frm.doc.items[i].qty = my_qty;
+	
+								frappe.msgprint("Assigning " + d.mr_name + " to " + d.item_code + " (row " + me.frm.doc.items[i].idx + ")");
+								if (qty > 0) {
+									frappe.msgprint("Splitting " + qty + " units of " + d.item_code);
+									var new_row = frappe.model.add_child(me.frm.doc, me.frm.doc.items[i].doctype, "items");
+									item_length++;
+	
+									for (var key in me.frm.doc.items[i]) {
+										new_row[key] = me.frm.doc.items[i][key];
+									}
+	
+									new_row.idx = item_length;
+									new_row["stock_qty"] = new_row.conversion_factor * qty;
+									new_row["qty"] = qty;
+									new_row["material_request"] = "";
+									new_row["material_request_item"] = "";
+								}
+							}
+						});
+						i++;
+					}
+					refresh_field("items");
+				}
+			});
+		}, __("Tools"));
 	},
 
 	tc_name: function() {
@@ -346,17 +408,6 @@
 		cur_frm.cscript.update_status('Deliver', 'Delivered')
 	},
 
-	get_last_purchase_rate: function() {
-		frappe.call({
-			"method": "get_last_purchase_rate",
-			"doc": cur_frm.doc,
-			callback: function(r, rt) {
-				cur_frm.dirty();
-				cur_frm.cscript.calculate_taxes_and_totals();
-			}
-		})
-	},
-
 	items_on_form_rendered: function() {
 		set_schedule_date(this.frm);
 	},
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.json b/erpnext/buying/doctype/purchase_order/purchase_order.json
index baf3b23..1c44f3e 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.json
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.json
@@ -1244,68 +1244,6 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
-   "depends_on": "eval:doc.docstatus===0 && (doc.items && doc.items.length)", 
-   "fieldname": "get_last_purchase_rate", 
-   "fieldtype": "Button", 
-   "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": "Get last purchase rate", 
-   "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, 
-   "unique": 0
-  }, 
-  {
-   "allow_bulk_edit": 0, 
-   "allow_on_submit": 0, 
-   "bold": 0, 
-   "collapsible": 0, 
-   "columns": 0, 
-   "depends_on": "eval:doc.docstatus===0 && (doc.items && doc.items.length)", 
-   "fieldname": "link_to_mrs", 
-   "fieldtype": "Button", 
-   "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": "Link to material requests", 
-   "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, 
-   "unique": 0
-  }, 
-  {
-   "allow_bulk_edit": 0, 
-   "allow_on_submit": 0, 
-   "bold": 0, 
-   "collapsible": 0, 
-   "columns": 0, 
    "fieldname": "sb_last_purchase", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
@@ -3355,7 +3293,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2018-07-06 11:00:05.037716", 
+ "modified": "2018-07-18 07:49:53.131408", 
  "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 b9e7075..d4f1de1 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.py
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.py
@@ -33,12 +33,6 @@
 			'percent_join_field': 'material_request'
 		}]
 
-	def onload(self):
-		super(PurchaseOrder, self).onload()
-
-		self.set_onload('disable_fetch_last_purchase_rate',
-			cint(frappe.db.get_single_value("Buying Settings", "disable_fetch_last_purchase_rate")))
-
 	def validate(self):
 		super(PurchaseOrder, self).validate()
 
@@ -122,7 +116,6 @@
 
 	def get_last_purchase_rate(self):
 		"""get last purchase rates for all items"""
-		if cint(frappe.db.get_single_value("Buying Settings", "disable_fetch_last_purchase_rate")): return
 
 		conversion_rate = flt(self.get('conversion_rate')) or 1.0
 		for d in self.get("items"):
@@ -286,7 +279,6 @@
 
 def item_last_purchase_rate(name, conversion_rate, item_code, conversion_factor= 1.0):
 	"""get last purchase rate for an item"""
-	if cint(frappe.db.get_single_value("Buying Settings", "disable_fetch_last_purchase_rate")): return
 
 	conversion_rate = flt(conversion_rate) or 1.0
 
diff --git a/erpnext/buying/doctype/purchase_order/tests/test_purchase_order_with_last_purchase_rate.js b/erpnext/buying/doctype/purchase_order/tests/test_purchase_order_with_last_purchase_rate.js
deleted file mode 100644
index 8ccf1b6..0000000
--- a/erpnext/buying/doctype/purchase_order/tests/test_purchase_order_with_last_purchase_rate.js
+++ /dev/null
@@ -1,156 +0,0 @@
-QUnit.module('Buying');
-
-QUnit.test("test: purchase order with last purchase rate", function(assert) {
-	assert.expect(9);
-	let done = assert.async();
-
-	frappe.run_serially([
-		() => {
-			return frappe.tests.make('Purchase Order', [
-				{supplier: 'Test Supplier'},
-				{is_subcontracted: 'No'},
-				{currency: 'INR'},
-				{items: [
-					[
-						{"item_code": 'Test Product 4'},
-						{"schedule_date": frappe.datetime.add_days(frappe.datetime.now_date(), 1)},
-						{"expected_delivery_date": frappe.datetime.add_days(frappe.datetime.now_date(), 5)},
-						{"qty": 1},
-						{"rate": 800},
-						{"warehouse": 'Stores - '+frappe.get_abbr(frappe.defaults.get_default("Company"))}
-					],
-					[
-						{"item_code": 'Test Product 1'},
-						{"schedule_date": frappe.datetime.add_days(frappe.datetime.now_date(), 1)},
-						{"expected_delivery_date": frappe.datetime.add_days(frappe.datetime.now_date(), 5)},
-						{"qty": 1},
-						{"rate": 400},
-						{"warehouse": 'Stores - '+frappe.get_abbr(frappe.defaults.get_default("Company"))}
-					]
-				]}
-			]);
-		},
-
-		() => {
-			// Get item details
-			assert.ok(cur_frm.doc.items[0].item_name == 'Test Product 4', "Item 1 name correct");
-			assert.ok(cur_frm.doc.items[1].item_name == 'Test Product 1', "Item 2 name correct");
-		},
-
-		() => frappe.timeout(1),
-
-		() => frappe.tests.click_button('Submit'),
-		() => frappe.tests.click_button('Yes'),
-		() => frappe.timeout(3),
-
-		() => frappe.tests.click_button('Close'),
-		() => frappe.timeout(1),
-
-		() => {
-			return frappe.tests.make('Purchase Order', [
-				{supplier: 'Test Supplier'},
-				{is_subcontracted: 'No'},
-				{currency: 'INR'},
-				{items: [
-					[
-						{"item_code": 'Test Product 4'},
-						{"schedule_date": frappe.datetime.add_days(frappe.datetime.now_date(), 1)},
-						{"expected_delivery_date": frappe.datetime.add_days(frappe.datetime.now_date(), 5)},
-						{"qty": 1},
-						{"rate": 600},
-						{"warehouse": 'Stores - '+frappe.get_abbr(frappe.defaults.get_default("Company"))}
-					],
-					[
-						{"item_code": 'Test Product 1'},
-						{"schedule_date": frappe.datetime.add_days(frappe.datetime.now_date(), 1)},
-						{"expected_delivery_date": frappe.datetime.add_days(frappe.datetime.now_date(), 5)},
-						{"qty": 1},
-						{"rate": 200},
-						{"warehouse": 'Stores - '+frappe.get_abbr(frappe.defaults.get_default("Company"))}
-					]
-				]}
-			]);
-		},
-
-		() => frappe.timeout(2),
-
-		// Get the last purchase rate of items
-		() => {
-			assert.ok(cur_frm.doc.items[0].last_purchase_rate == 800, "Last purchase rate of item 1 correct");
-			assert.ok(cur_frm.doc.items[1].last_purchase_rate != 0);
-		},
-		() => {
-			assert.ok(cur_frm.doc.items[1].last_purchase_rate == 400, "Last purchase rate of item 2 correct");
-			assert.ok(cur_frm.doc.items[1].last_purchase_rate != 0);
-		},
-
-		() => frappe.tests.click_button('Submit'),
-		() => frappe.tests.click_button('Yes'),
-		() => frappe.timeout(3),
-
-		() => frappe.tests.click_button('Close'),
-
-		() => frappe.timeout(1),
-
-		() => {
-			assert.ok(cur_frm.doc.status == 'To Receive and Bill', "Submitted successfully");
-		},
-
-		// enable allow_last_purchase_rate
-		() => {
-			return frappe.tests.make('Buying Settings', [
-				// values to be set
-				{"disable_fetch_last_purchase_rate": 1}
-			]);
-		},
-
-		() => {
-			return frappe.tests.make('Purchase Order', [
-				{supplier: 'Test Supplier'},
-				{is_subcontracted: 'No'},
-				{currency: 'INR'},
-				{items: [
-					[
-						{"item_code": 'Test Product 4'},
-						{"schedule_date": frappe.datetime.add_days(frappe.datetime.now_date(), 1)},
-						{"expected_delivery_date": frappe.datetime.add_days(frappe.datetime.now_date(), 5)},
-						{"qty": 1},
-						{"rate": 800},
-						{"warehouse": 'Stores - '+frappe.get_abbr(frappe.defaults.get_default("Company"))}
-					],
-					[
-						{"item_code": 'Test Product 1'},
-						{"schedule_date": frappe.datetime.add_days(frappe.datetime.now_date(), 1)},
-						{"expected_delivery_date": frappe.datetime.add_days(frappe.datetime.now_date(), 5)},
-						{"qty": 1},
-						{"rate": 400},
-						{"warehouse": 'Stores - '+frappe.get_abbr(frappe.defaults.get_default("Company"))}
-					]
-				]}
-			]);
-		},
-
-		() => {
-			// Get item details
-			assert.ok(cur_frm.doc.items[0].last_purchase_rate == 0);
-			assert.ok(cur_frm.doc.items[1].last_purchase_rate == 0);
-		},
-
-		() => frappe.timeout(1),
-
-		() => frappe.tests.click_button('Submit'),
-		() => frappe.tests.click_button('Yes'),
-		() => frappe.timeout(3),
-
-		() => frappe.tests.click_button('Close'),
-		() => frappe.timeout(1),
-
-		// enable allow_last_purchase_rate
-		() => frappe.tests.make('Buying Settings', [
-			// values to be set
-			{"disable_fetch_last_purchase_rate": 0}
-		]),
-
-		() => done()
-	]);
-});
\ No newline at end of file
diff --git a/erpnext/buying/doctype/purchase_order_item/purchase_order_item.json b/erpnext/buying/doctype/purchase_order_item/purchase_order_item.json
index 1a2842a..eaa1360 100755
--- a/erpnext/buying/doctype/purchase_order_item/purchase_order_item.json
+++ b/erpnext/buying/doctype/purchase_order_item/purchase_order_item.json
@@ -680,7 +680,7 @@
    "precision": "", 
    "print_hide": 0, 
    "print_hide_if_no_value": 0, 
-   "read_only": 0, 
+   "read_only": 1, 
    "remember_last_selected_value": 0, 
    "report_hide": 0, 
    "reqd": 0, 
@@ -1897,7 +1897,7 @@
  "issingle": 0, 
  "istable": 1, 
  "max_attachments": 0, 
- "modified": "2017-12-14 09:36:40.837027", 
+ "modified": "2018-07-18 07:53:54.677844", 
  "modified_by": "Administrator", 
  "module": "Buying", 
  "name": "Purchase Order Item", 
diff --git a/erpnext/public/js/controllers/buying.js b/erpnext/public/js/controllers/buying.js
index a9e3ad4..9d92b9f 100644
--- a/erpnext/public/js/controllers/buying.js
+++ b/erpnext/public/js/controllers/buying.js
@@ -222,60 +222,6 @@
 
 	tc_name: function() {
 		this.get_terms();
-	},
-	link_to_mrs: function() {
-		var my_items = [];
-		for (var i in cur_frm.doc.items) {
-			if(!cur_frm.doc.items[i].material_request){
-				my_items.push(cur_frm.doc.items[i].item_code);
-			}
-		}
-		frappe.call({
-			method: "erpnext.buying.utils.get_linked_material_requests",
-			args:{
-				items: my_items
-			},
-			callback: function(r) {
-				if(r.exc) return;
-
-				var i = 0;
-				var item_length = cur_frm.doc.items.length;
-				while (i < item_length) {
-					var qty = cur_frm.doc.items[i].qty;
-					(r.message[0] || []).forEach(function(d) {
-						if (d.qty > 0 && qty > 0 && cur_frm.doc.items[i].item_code == d.item_code && !cur_frm.doc.items[i].material_request_item)
-						{
-							cur_frm.doc.items[i].material_request = d.mr_name;
-							cur_frm.doc.items[i].material_request_item = d.mr_item;
-							var my_qty = Math.min(qty, d.qty);
-							qty = qty - my_qty;
-							d.qty = d.qty  - my_qty;
-							cur_frm.doc.items[i].stock_qty = my_qty*cur_frm.doc.items[i].conversion_factor;
-							cur_frm.doc.items[i].qty = my_qty;
-
-							frappe.msgprint("Assigning " + d.mr_name + " to " + d.item_code + " (row " + cur_frm.doc.items[i].idx + ")");
-							if (qty > 0) {
-								frappe.msgprint("Splitting " + qty + " units of " + d.item_code);
-								var newrow = frappe.model.add_child(cur_frm.doc, cur_frm.doc.items[i].doctype, "items");
-								item_length++;
-
-								for (var key in cur_frm.doc.items[i]) {
-									newrow[key] = cur_frm.doc.items[i][key];
-								}
-
-								newrow.idx = item_length;
-								newrow["stock_qty"] = newrow.conversion_factor*qty;
-								newrow["qty"] = qty;
-								newrow["material_request"] = "";
-								newrow["material_request_item"] = "";
-							}
-						}
-					});
-					i++;
-				}
-				refresh_field("items");
-			}
-		});
 	}
 });