Merge branch 'develop' into quoted-item-report-v2
diff --git a/erpnext/buying/report/quoted_item_comparison/quoted_item_comparison.js b/erpnext/buying/report/quoted_item_comparison/quoted_item_comparison.js
index a76ffee..ad390c4 100644
--- a/erpnext/buying/report/quoted_item_comparison/quoted_item_comparison.js
+++ b/erpnext/buying/report/quoted_item_comparison/quoted_item_comparison.js
@@ -12,7 +12,22 @@
 			"reqd": 1
 		},
 		{
-			reqd: 1,
+			"fieldname":"from_date",
+			"label": __("From Date"),
+			"fieldtype": "Date",
+			"width": "80",
+			"reqd": 1,
+			"default": frappe.datetime.add_months(frappe.datetime.get_today(), -1),
+		},
+		{
+			"fieldname":"to_date",
+			"label": __("To Date"),
+			"fieldtype": "Date",
+			"width": "80",
+			"reqd": 1,
+			"default": frappe.datetime.get_today()
+		},
+		{
 			default: "",
 			options: "Item",
 			label: __("Item"),
@@ -45,13 +60,12 @@
 			}
 		},
 		{
-			fieldtype: "Link",
+			fieldtype: "MultiSelectList",
 			label: __("Supplier Quotation"),
-			options: "Supplier Quotation",
 			fieldname: "supplier_quotation",
 			default: "",
-			get_query: () => {
-				return { filters: { "docstatus": ["<", 2] } }
+			get_data: function(txt) {
+				return frappe.db.get_link_options('Supplier Quotation', txt);
 			}
 		},
 		{
@@ -63,9 +77,30 @@
 			get_query: () => {
 				return { filters: { "docstatus": ["<", 2] } }
 			}
+		},
+		{
+			fieldtype: "Check",
+			label: __("Include Expired"),
+			fieldname: "include_expired",
+			default: 0
 		}
 	],
 
+	formatter: (value, row, column, data, default_formatter) => {
+		value = default_formatter(value, row, column, data);
+
+		if(column.fieldname === "valid_till" && data.valid_till){
+			if(frappe.datetime.get_diff(data.valid_till, frappe.datetime.nowdate()) <= 1){
+				value = `<div style="color:red">${value}</div>`;
+			}
+			else if (frappe.datetime.get_diff(data.valid_till, frappe.datetime.nowdate()) <= 7){
+				value = `<div style="color:darkorange">${value}</div>`;
+			}
+		}
+
+		return value;
+	},
+
 	onload: (report) => {
 		// Create a button for setting the default supplier
 		report.page.add_inner_button(__("Select Default Supplier"), () => {
@@ -75,6 +110,19 @@
 			reporter.make_default_supplier_dialog(report);
 		}, 'Tools');
 
+		const status_message = `
+		<span class="indicator">
+		Valid till : &nbsp;&nbsp;
+		</span>
+		<span class="indicator orange">
+		Expires in a week or less
+		</span>
+		&nbsp;&nbsp;&nbsp;&nbsp;
+		<span class="indicator red">
+		Expires today / Already Expired
+		</span>`;
+		report.$status.html(status_message).show();
+
 	},
 	make_default_supplier_dialog: (report) => {
 		// Get the name of the item to change
diff --git a/erpnext/buying/report/quoted_item_comparison/quoted_item_comparison.py b/erpnext/buying/report/quoted_item_comparison/quoted_item_comparison.py
index a33867a..ffa138f 100644
--- a/erpnext/buying/report/quoted_item_comparison/quoted_item_comparison.py
+++ b/erpnext/buying/report/quoted_item_comparison/quoted_item_comparison.py
@@ -16,44 +16,48 @@
 	supplier_quotation_data = get_data(filters, conditions)
 	columns = get_columns()
 
-	data, chart_data = prepare_data(supplier_quotation_data)
+	data, chart_data = prepare_data(supplier_quotation_data, filters)
 
 	return columns, data, None, chart_data
 
 def get_conditions(filters):
 	conditions = ""
+	if filters.get("item_code"):
+		conditions += " AND sqi.item_code = %(item_code)s"
+
 	if filters.get("supplier_quotation"):
-		conditions += " AND sqi.parent = %(supplier_quotation)s"
+		conditions += " AND sqi.parent in %(supplier_quotation)s"
 
 	if filters.get("request_for_quotation"):
 		conditions += " AND sqi.request_for_quotation = %(request_for_quotation)s"
 
 	if filters.get("supplier"):
 		conditions += " AND sq.supplier in %(supplier)s"
+
+	if not filters.get("include_expired"):
+		conditions += " AND sq.status != 'Expired'"
+
 	return conditions
 
 def get_data(filters, conditions):
-	if not filters.get("item_code"):
-		return []
-
 	supplier_quotation_data = frappe.db.sql("""SELECT
-		sqi.parent, sqi.qty, sqi.rate, sqi.uom, sqi.request_for_quotation,
-		sq.supplier
+		sqi.parent, sqi.item_code, sqi.qty, sqi.rate, sqi.uom, sqi.request_for_quotation,
+		sqi.lead_time_days, sq.supplier, sq.valid_till
 		FROM
 			`tabSupplier Quotation Item` sqi,
 			`tabSupplier Quotation` sq
 		WHERE
-			sqi.item_code = %(item_code)s
-			AND sqi.parent = sq.name
+			sqi.parent = sq.name
 			AND sqi.docstatus < 2
 			AND sq.company = %(company)s
-			AND sq.status != 'Expired'
-			{0}""".format(conditions), filters, as_dict=1)
+			AND sq.transaction_date between %(from_date)s and %(to_date)s
+			{0}
+			order by sq.transaction_date, sqi.item_code""".format(conditions), filters, as_dict=1)
 
 	return supplier_quotation_data
 
-def prepare_data(supplier_quotation_data):
-	out, suppliers, qty_list = [], [], []
+def prepare_data(supplier_quotation_data, filters):
+	out, suppliers, qty_list, chart_data = [], [], [], []
 	supplier_wise_map = defaultdict(list)
 	supplier_qty_price_map = {}
 
@@ -70,20 +74,24 @@
 			exchange_rate = 1
 
 		row = {
+			"item_code": data.get('item_code'),
 			"quotation": data.get("parent"),
 			"qty": data.get("qty"),
 			"price": flt(data.get("rate") * exchange_rate, float_precision),
 			"uom": data.get("uom"),
 			"request_for_quotation": data.get("request_for_quotation"),
+			"valid_till": data.get('valid_till'),
+			"lead_time_days": data.get('lead_time_days')
 		}
 
 		# map for report view of form {'supplier1':[{},{},...]}
 		supplier_wise_map[supplier].append(row)
 
 		# map for chart preparation of the form {'supplier1': {'qty': 'price'}}
-		if not supplier in supplier_qty_price_map:
-			supplier_qty_price_map[supplier] = {}
-		supplier_qty_price_map[supplier][row["qty"]] = row["price"]
+		if filters.get("item_code"):
+			if not supplier in supplier_qty_price_map:
+				supplier_qty_price_map[supplier] = {}
+			supplier_qty_price_map[supplier][row["qty"]] = row["price"]
 
 		suppliers.append(supplier)
 		qty_list.append(data.get("qty"))
@@ -97,7 +105,8 @@
 		for entry in supplier_wise_map[supplier]:
 			out.append(entry)
 
-	chart_data = prepare_chart_data(suppliers, qty_list, supplier_qty_price_map)
+	if filters.get("item_code"):
+		chart_data = prepare_chart_data(suppliers, qty_list, supplier_qty_price_map)
 
 	return out, chart_data
 
@@ -117,9 +126,10 @@
 				data_points_map[qty].append(None)
 
 	dataset = []
+	currency_symbol = frappe.db.get_value("Currency", frappe.db.get_default("currency"), "symbol")
 	for qty in qty_list:
 		datapoints = {
-			"name": _("Price for Qty ") + str(qty),
+			"name": currency_symbol + " (Qty " + str(qty) + " )",
 			"values": data_points_map[qty]
 		}
 		dataset.append(datapoints)
@@ -140,14 +150,21 @@
 		"label": _("Supplier"),
 		"fieldtype": "Link",
 		"options": "Supplier",
+		"width": 150
+	},
+	{
+		"fieldname": "item_code",
+		"label": _("Item"),
+		"fieldtype": "Link",
+		"options": "Item",
 		"width": 200
 	},
 	{
-		"fieldname": "quotation",
-		"label": _("Supplier Quotation"),
+		"fieldname": "uom",
+		"label": _("UOM"),
 		"fieldtype": "Link",
-		"options": "Supplier Quotation",
-		"width": 200
+		"options": "UOM",
+		"width": 90
 	},
 	{
 		"fieldname": "qty",
@@ -163,18 +180,30 @@
 		"width": 110
 	},
 	{
-		"fieldname": "uom",
-		"label": _("UOM"),
+		"fieldname": "quotation",
+		"label": _("Supplier Quotation"),
 		"fieldtype": "Link",
-		"options": "UOM",
-		"width": 90
+		"options": "Supplier Quotation",
+		"width": 200
+	},
+	{
+		"fieldname": "valid_till",
+		"label": _("Valid Till"),
+		"fieldtype": "Date",
+		"width": 100
+	},
+	{
+		"fieldname": "lead_time_days",
+		"label": _("Lead Time (Days)"),
+		"fieldtype": "Int",
+		"width": 100
 	},
 	{
 		"fieldname": "request_for_quotation",
 		"label": _("Request for Quotation"),
 		"fieldtype": "Link",
 		"options": "Request for Quotation",
-		"width": 200
+		"width": 150
 	}
 	]