[Agri] Item amount set on item set (#12091)

* [Agri] Item amount set on item set

- closes #12008
- fixes #12010

* used frappe.call instead of frm.call in crop

* made modifications for sql query for BOM Item

- done since Crop doctype in Agri module also uses 'BOM Item' child table

* hide BOM no from BOM Item childtable in Crop
diff --git a/erpnext/agriculture/doctype/crop/crop.js b/erpnext/agriculture/doctype/crop/crop.js
index 587727c..3f8a7fc 100644
--- a/erpnext/agriculture/doctype/crop/crop.js
+++ b/erpnext/agriculture/doctype/crop/crop.js
@@ -1,7 +1,55 @@
 // Copyright (c) 2017, Frappe Technologies Pvt. Ltd. and contributors
 // For license information, please see license.txt
 
+frappe.provide("erpnext.crop");
+
 frappe.ui.form.on('Crop', {
-	validate: (frm) => {
+	refresh: (frm) => {
+		frm.fields_dict.materials_required.grid.set_column_disp('bom_no', false);
 	}
-});
\ No newline at end of file
+});
+
+frappe.ui.form.on("BOM Item", {
+	item_code: (frm, cdt, cdn) => {
+		erpnext.crop.update_item_rate_uom(frm, cdt, cdn);
+	},
+	qty: (frm, cdt, cdn) => {
+		erpnext.crop.update_item_qty_amount(frm, cdt, cdn);
+	},
+	rate: (frm, cdt, cdn) => {
+		erpnext.crop.update_item_qty_amount(frm, cdt, cdn);
+	}
+});
+
+erpnext.crop.update_item_rate_uom = function(frm, cdt, cdn) {
+	let material_list = ['materials_required', 'produce', 'byproducts'];
+	material_list.forEach((material) => {
+		frm.doc[material].forEach((item, index) => {
+			if (item.name == cdn){
+				frappe.call({
+					method:'erpnext.agriculture.doctype.crop.crop.get_item_details',
+					args: {
+						item_code: item.item_code
+					},
+					callback: (r) => {
+						frappe.model.set_value('BOM Item', item.name, 'uom', r.message.uom);
+						frappe.model.set_value('BOM Item', item.name, 'rate', r.message.rate);
+					}
+				});
+			}
+		});
+	});
+};
+
+erpnext.crop.update_item_qty_amount = function(frm, cdt, cdn) {
+	let material_list = ['materials_required', 'produce', 'byproducts'];
+	material_list.forEach((material) => {
+		frm.doc[material].forEach((item, index) => {
+			if (item.name == cdn){
+				if (!frappe.model.get_value('BOM Item', item.name, 'qty'))
+					frappe.model.set_value('BOM Item', item.name, 'qty', 1);
+				frappe.model.set_value('BOM Item', item.name, 'amount', item.qty * item.rate);
+			}
+		});
+	});
+};
\ No newline at end of file
diff --git a/erpnext/agriculture/doctype/crop/crop.py b/erpnext/agriculture/doctype/crop/crop.py
index 29b9823..7eeb8af 100644
--- a/erpnext/agriculture/doctype/crop/crop.py
+++ b/erpnext/agriculture/doctype/crop/crop.py
@@ -15,4 +15,9 @@
 				frappe.throw("Start day is greater than end day in task '{0}'".format(task.subject))
 			# to calculate the period of the Crop Cycle
 			if task.end_day > max_period: max_period = task.end_day
-		if max_period > self.period: self.period = max_period
\ No newline at end of file
+		if max_period > self.period: self.period = max_period
+
+@frappe.whitelist()
+def get_item_details(item_code):
+	item = frappe.get_doc('Item', item_code)
+	return { "uom": item.stock_uom, "rate": item.valuation_rate }
\ No newline at end of file
diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py
index 848f46e..41670e0 100644
--- a/erpnext/manufacturing/doctype/bom/bom.py
+++ b/erpnext/manufacturing/doctype/bom/bom.py
@@ -192,7 +192,7 @@
 		# update parent BOMs
 		if self.total_cost != existing_bom_cost and update_parent:
 			parent_boms = frappe.db.sql_list("""select distinct parent from `tabBOM Item`
-				where bom_no = %s and docstatus=1""", self.name)
+				where bom_no = %s and docstatus=1 and parenttype='BOM'""", self.name)
 
 			for bom in parent_boms:
 				frappe.get_doc("BOM", bom).update_cost(from_child_bom=True)
@@ -330,7 +330,7 @@
 		for d in check_list:
 			bom_list, count = [self.name], 0
 			while (len(bom_list) > count ):
-				boms = frappe.db.sql(" select %s from `tabBOM Item` where %s = %s " %
+				boms = frappe.db.sql(" select %s from `tabBOM Item` where %s = %s and parenttype='BOM'" %
 					(d[0], d[1], '%s'), cstr(bom_list[count]))
 				count = count + 1
 				for b in boms:
@@ -350,7 +350,7 @@
 	def traverse_tree(self, bom_list=None):
 		def _get_children(bom_no):
 			return [cstr(d[0]) for d in frappe.db.sql("""select bom_no from `tabBOM Item`
-				where parent = %s and ifnull(bom_no, '') != ''""", bom_no)]
+				where parent = %s and ifnull(bom_no, '') != '' and parenttype='BOM'""", bom_no)]
 
 		count = 0
 		if not bom_list:
@@ -496,7 +496,7 @@
 	def validate_bom_links(self):
 		if not self.is_active:
 			act_pbom = frappe.db.sql("""select distinct bom_item.parent from `tabBOM Item` bom_item
-				where bom_item.bom_no = %s and bom_item.docstatus = 1
+				where bom_item.bom_no = %s and bom_item.docstatus = 1 and bom_item.parenttype='BOM'
 				and exists (select * from `tabBOM` where name = bom_item.parent
 					and docstatus = 1 and is_active = 1)""", self.name)
 
@@ -612,7 +612,7 @@
 def get_boms_in_bottom_up_order(bom_no=None):
 	def _get_parent(bom_no):
 		return frappe.db.sql_list("""select distinct parent from `tabBOM Item`
-			where bom_no = %s and docstatus=1""", bom_no)
+			where bom_no = %s and docstatus=1 and parenttype='BOM'""", bom_no)
 
 	count = 0
 	bom_list = []
diff --git a/erpnext/manufacturing/doctype/bom/test_bom.py b/erpnext/manufacturing/doctype/bom/test_bom.py
index 8bd98b2..f5b4eb8 100644
--- a/erpnext/manufacturing/doctype/bom/test_bom.py
+++ b/erpnext/manufacturing/doctype/bom/test_bom.py
@@ -55,7 +55,7 @@
 		# get current rate for '_Test Item 2'
 		rm_rate = frappe.db.sql("""select rate from `tabBOM Item`
 			where parent='BOM-_Test Item Home Desktop Manufactured-001'
-			and item_code='_Test Item 2' and docstatus=1""")
+			and item_code='_Test Item 2' and docstatus=1 and parenttype='BOM'""")
 		rm_rate = rm_rate[0][0] if rm_rate else 0
 
 		# update valuation rate of item '_Test Item 2'
@@ -74,7 +74,7 @@
 		
 		# check if new valuation rate updated in all BOMs
 		for d in frappe.db.sql("""select rate from `tabBOM Item`
-			where item_code='_Test Item 2' and docstatus=1""", as_dict=1):
+			where item_code='_Test Item 2' and docstatus=1 and parenttype='BOM'""", as_dict=1):
 				self.assertEqual(d.rate, rm_rate + 10)
 
 	def test_bom_cost(self):
diff --git a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py
index e3c61ed..3b6c3a1 100644
--- a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py
+++ b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py
@@ -35,12 +35,12 @@
 		new_bom_unitcost = flt(new_bom_unitcost[0][0]) if new_bom_unitcost else 0
 
 		frappe.db.sql("""update `tabBOM Item` set bom_no=%s,
-			rate=%s, amount=stock_qty*%s where bom_no = %s and docstatus < 2""",
+			rate=%s, amount=stock_qty*%s where bom_no = %s and docstatus < 2 and parenttype='BOM'""",
 			(self.new_bom, new_bom_unitcost, new_bom_unitcost, self.current_bom))
 
 	def get_parent_boms(self):
 		return [d[0] for d in frappe.db.sql("""select distinct parent
-			from `tabBOM Item` where ifnull(bom_no, '') = %s and docstatus < 2""",
+			from `tabBOM Item` where ifnull(bom_no, '') = %s and docstatus < 2 and parenttype='BOM'""",
 			self.new_bom)]
 
 @frappe.whitelist()
diff --git a/erpnext/manufacturing/report/bom_stock_report/bom_stock_report.py b/erpnext/manufacturing/report/bom_stock_report/bom_stock_report.py
index 343cfe9..ab9f83d 100644
--- a/erpnext/manufacturing/report/bom_stock_report/bom_stock_report.py
+++ b/erpnext/manufacturing/report/bom_stock_report/bom_stock_report.py
@@ -52,6 +52,6 @@
     		    ON bom_item.item_code = ledger.item_code
     		    %s
             WHERE
-    	        bom_item.parent = '%s'
+    	        bom_item.parent = '%s' and bom_item.parenttype='BOM'
 
             GROUP BY bom_item.item_code""" % (conditions, bom))