Merge pull request #20809 from ronelvcabrera/billing-contact

feat(Contacts): select billing contact for sales invoice
diff --git a/erpnext/buying/doctype/supplier_quotation/supplier_quotation.js b/erpnext/buying/doctype/supplier_quotation/supplier_quotation.js
index 39042b8..16061c6 100644
--- a/erpnext/buying/doctype/supplier_quotation/supplier_quotation.js
+++ b/erpnext/buying/doctype/supplier_quotation/supplier_quotation.js
@@ -4,15 +4,17 @@
 // attach required files
 {% include 'erpnext/public/js/controllers/buying.js' %};
 
-frappe.ui.form.on('Suppier Quotation', {
-	setup: function(frm) {
-		frm.custom_make_buttons = {
-			'Purchase Order': 'Purchase Order'
-		}
-	}
-});
-
 erpnext.buying.SupplierQuotationController = erpnext.buying.BuyingController.extend({
+	setup: function() {
+		this.frm.custom_make_buttons = {
+			'Purchase Order': 'Purchase Order',
+			'Quotation': 'Quotation',
+			'Subscription': 'Subscription'
+		}
+
+		this._super();
+	},
+
 	refresh: function() {
 		var me = this;
 		this._super();
diff --git a/erpnext/crm/doctype/lead/lead.py b/erpnext/crm/doctype/lead/lead.py
index 73ef79b..eb9f860 100644
--- a/erpnext/crm/doctype/lead/lead.py
+++ b/erpnext/crm/doctype/lead/lead.py
@@ -135,10 +135,17 @@
 
 		# do not create an address if no fields are available,
 		# skipping country since the system auto-sets it from system defaults
-		if not any([self.get(field) for field in address_fields if field != "country"]):
+		address = frappe.new_doc("Address")
+
+		mandatory_fields = [ df.fieldname for df in address.meta.fields if df.reqd ]
+
+		if not all([self.get(field) for field in mandatory_fields]):
+			frappe.msgprint(_('Missing mandatory fields in address. \
+				{0} to create address' ).format("<a href='desk#Form/Address/New Address 1' \
+				> Click here </a>"),
+				alert=True, indicator='yellow')
 			return
 
-		address = frappe.new_doc("Address")
 		address.update({addr_field: self.get(addr_field) for addr_field in address_fields})
 		address.update({info_field: self.get(info_field) for info_field in info_fields})
 		address.insert()
diff --git a/erpnext/manufacturing/doctype/production_plan/production_plan.py b/erpnext/manufacturing/doctype/production_plan/production_plan.py
index a79ea0e..358a542 100644
--- a/erpnext/manufacturing/doctype/production_plan/production_plan.py
+++ b/erpnext/manufacturing/doctype/production_plan/production_plan.py
@@ -144,7 +144,7 @@
 			item_condition = " and mr_item.item_code ={0}".format(frappe.db.escape(self.item_code))
 
 		items = frappe.db.sql("""select distinct parent, name, item_code, warehouse, description,
-			(qty - ordered_qty) as pending_qty
+			(qty - ordered_qty) * conversion_factor as pending_qty
 			from `tabMaterial Request Item` mr_item
 			where parent in (%s) and docstatus = 1 and qty > ordered_qty
 			and exists (select name from `tabBOM` bom where bom.item=mr_item.item_code
diff --git a/erpnext/selling/doctype/sales_order/sales_order.py b/erpnext/selling/doctype/sales_order/sales_order.py
index 13d2b15..ef2d19a 100755
--- a/erpnext/selling/doctype/sales_order/sales_order.py
+++ b/erpnext/selling/doctype/sales_order/sales_order.py
@@ -496,7 +496,7 @@
 
 def get_requested_item_qty(sales_order):
 	return frappe._dict(frappe.db.sql("""
-		select sales_order_item, sum(stock_qty)
+		select sales_order_item, sum(qty)
 		from `tabMaterial Request Item`
 		where docstatus = 1
 			and sales_order = %s
@@ -507,16 +507,12 @@
 def make_material_request(source_name, target_doc=None):
 	requested_item_qty = get_requested_item_qty(source_name)
 
-	def postprocess(source, doc):
-		doc.material_request_type = "Purchase"
-
 	def update_item(source, target, source_parent):
 		# qty is for packed items, because packed items don't have stock_qty field
-		qty = source.get("stock_qty") or source.get("qty")
+		qty = source.get("qty")
 		target.project = source_parent.project
 		target.qty = qty - requested_item_qty.get(source.name, 0)
-		target.conversion_factor = 1
-		target.stock_qty = qty - requested_item_qty.get(source.name, 0)
+		target.stock_qty = flt(target.qty) * flt(target.conversion_factor)
 
 	doc = get_mapped_doc("Sales Order", source_name, {
 		"Sales Order": {
@@ -537,14 +533,12 @@
 			"doctype": "Material Request Item",
 			"field_map": {
 				"name": "sales_order_item",
-				"parent": "sales_order",
-				"stock_uom": "uom",
-				"stock_qty": "qty"
+				"parent": "sales_order"
 			},
 			"condition": lambda doc: not frappe.db.exists('Product Bundle', doc.item_code) and doc.stock_qty > requested_item_qty.get(doc.name, 0),
 			"postprocess": update_item
 		}
-	}, target_doc, postprocess)
+	}, target_doc)
 
 	return doc
 
diff --git a/erpnext/selling/page/point_of_sale/point_of_sale.py b/erpnext/selling/page/point_of_sale/point_of_sale.py
index 3425f8f..17136e0 100644
--- a/erpnext/selling/page/point_of_sale/point_of_sale.py
+++ b/erpnext/selling/page/point_of_sale/point_of_sale.py
@@ -64,30 +64,40 @@
 		for d in item_prices_data:
 			item_prices[d.item_code] = d
 
-
+		# prepare filter for bin query
+		bin_filters = {'item_code': ['in', items]}
+		if warehouse:
+			bin_filters['warehouse'] = warehouse
 		if display_items_in_stock:
-			filters = {'actual_qty': [">", 0], 'item_code': ['in', items]}
+			bin_filters['actual_qty'] = [">", 0]
 
-			if warehouse:
-				filters['warehouse'] = warehouse
+		# query item bin
+		bin_data = frappe.get_all(
+			'Bin', fields=['item_code', 'sum(actual_qty) as actual_qty'],
+			filters=bin_filters, group_by='item_code'
+		)
 
-			bin_data = frappe._dict(
-				frappe.get_all("Bin", fields = ["item_code", "sum(actual_qty) as actual_qty"],
-				filters = filters, group_by = "item_code")
-			)
+		# convert list of dict into dict as {item_code: actual_qty}
+		bin_dict = {}
+		for b in bin_data:
+			bin_dict[b.get('item_code')] = b.get('actual_qty')
 
 		for item in items_data:
-			row = {}
+			item_code = item.item_code
+			item_price = item_prices.get(item_code) or {}
+			item_stock_qty = bin_dict.get(item_code)
 
-			row.update(item)
-			item_price = item_prices.get(item.item_code) or {}
-			row.update({
-				'price_list_rate': item_price.get('price_list_rate'),
-				'currency': item_price.get('currency'),
-				'actual_qty': bin_data.get('actual_qty')
-			})
-
-			result.append(row)
+			if display_items_in_stock and not item_stock_qty:
+				pass
+			else:
+				row = {}
+				row.update(item)
+				row.update({
+					'price_list_rate': item_price.get('price_list_rate'),
+					'currency': item_price.get('currency'),
+					'actual_qty': item_stock_qty,
+				})
+				result.append(row)
 
 	res = {
 		'items': result
diff --git a/erpnext/stock/doctype/material_request/material_request.py b/erpnext/stock/doctype/material_request/material_request.py
index 4542847..285643d 100644
--- a/erpnext/stock/doctype/material_request/material_request.py
+++ b/erpnext/stock/doctype/material_request/material_request.py
@@ -501,7 +501,7 @@
 				wo_order = frappe.new_doc("Work Order")
 				wo_order.update({
 					"production_item": d.item_code,
-					"qty": d.qty - d.ordered_qty,
+					"qty": d.stock_qty - d.ordered_qty,
 					"fg_warehouse": d.warehouse,
 					"wip_warehouse": default_wip_warehouse,
 					"description": d.description,