Option to disable fetching last purchase details in Purchase Order (#11573)

* add check for enable/disable last purchase details in Purchase Order

* fix tabs
diff --git a/erpnext/buying/doctype/buying_settings/buying_settings.json b/erpnext/buying/doctype/buying_settings/buying_settings.json
index ac97255..25e9c15 100644
--- a/erpnext/buying/doctype/buying_settings/buying_settings.json
+++ b/erpnext/buying/doctype/buying_settings/buying_settings.json
@@ -182,6 +182,38 @@
    "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,
+   "unique": 0
   }
  ], 
  "hide_heading": 0, 
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.py b/erpnext/buying/doctype/purchase_order/purchase_order.py
index 4150993..bbaa043 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.py
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.py
@@ -4,7 +4,7 @@
 from __future__ import unicode_literals
 import frappe
 import json
-from frappe.utils import cstr, flt
+from frappe.utils import cstr, flt, cint
 from frappe import msgprint, _
 from frappe.model.mapper import get_mapped_doc
 from erpnext.controllers.buying_controller import BuyingController
@@ -105,25 +105,26 @@
 	def get_last_purchase_rate(self):
 		"""get last purchase rates for all items"""
 
-		conversion_rate = flt(self.get('conversion_rate')) or 1.0
+		if not cint(frappe.db.get_single_value("Buying Settings", "disable_fetch_last_purchase_rate")):
+			conversion_rate = flt(self.get('conversion_rate')) or 1.0
 
-		for d in self.get("items"):
-			if d.item_code:
-				last_purchase_details = get_last_purchase_details(d.item_code, self.name)
+			for d in self.get("items"):
+				if d.item_code:
+					last_purchase_details = get_last_purchase_details(d.item_code, self.name)
 
-				if last_purchase_details:
-					d.base_price_list_rate = (last_purchase_details['base_price_list_rate'] *
-						(flt(d.conversion_factor) or 1.0))
-					d.discount_percentage = last_purchase_details['discount_percentage']
-					d.base_rate = last_purchase_details['base_rate'] * (flt(d.conversion_factor) or 1.0)
-					d.price_list_rate = d.base_price_list_rate / conversion_rate
-					d.last_purchase_rate = d.base_rate / conversion_rate
-				else:
+					if last_purchase_details:
+						d.base_price_list_rate = (last_purchase_details['base_price_list_rate'] *
+							(flt(d.conversion_factor) or 1.0))
+						d.discount_percentage = last_purchase_details['discount_percentage']
+						d.base_rate = last_purchase_details['base_rate'] * (flt(d.conversion_factor) or 1.0)
+						d.price_list_rate = d.base_price_list_rate / conversion_rate
+						d.last_purchase_rate = d.base_rate / conversion_rate
+					else:
 
-					item_last_purchase_rate = frappe.db.get_value("Item", d.item_code, "last_purchase_rate")
-					if item_last_purchase_rate:
-						d.base_price_list_rate = d.base_rate = d.price_list_rate \
-							= d.last_purchase_rate = item_last_purchase_rate
+						item_last_purchase_rate = frappe.db.get_value("Item", d.item_code, "last_purchase_rate")
+						if item_last_purchase_rate:
+							d.base_price_list_rate = d.base_rate = d.price_list_rate \
+								= d.last_purchase_rate = item_last_purchase_rate
 
 	# Check for Closed status
 	def check_for_closed_status(self):
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
index d19f017..bd5881b 100644
--- 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
@@ -1,7 +1,7 @@
 QUnit.module('Buying');
 
 QUnit.test("test: purchase order with last purchase rate", function(assert) {
-	assert.expect(5);
+	assert.expect(9);
 	let done = assert.async();
 
 	frappe.run_serially([
@@ -77,9 +77,11 @@
 		// 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'),
@@ -94,6 +96,59 @@
 			assert.ok(cur_frm.doc.status == 'To Receive and Bill', "Submitted successfully");
 		},
 
+		// enable allow_last_purchase_rate
+		() => 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