Script report for purchase analytics (#15612)

* Purchase Analytics Query Report

* Minor Changes

* Codacy Issue Fixes

* Codacy Issues Fixed

* Code cleaning and optimization

* Indentation Issue Fix

* Code cleaning and better function naming

* Added link for purchase analytics in buying.py

* Added link for production analytics in manufacturing.py

* Commonified purchase-analytics using Analytics class

* Codacy issue fix
diff --git a/erpnext/buying/report/purchase_analytics/__init__.py b/erpnext/buying/report/purchase_analytics/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/buying/report/purchase_analytics/__init__.py
diff --git a/erpnext/buying/report/purchase_analytics/purchase_analytics.js b/erpnext/buying/report/purchase_analytics/purchase_analytics.js
new file mode 100644
index 0000000..297ec51
--- /dev/null
+++ b/erpnext/buying/report/purchase_analytics/purchase_analytics.js
@@ -0,0 +1,128 @@
+// Copyright (c) 2016, Frappe Technologies Pvt. Ltd. and contributors
+// For license information, please see license.txt
+/* eslint-disable */
+
+frappe.query_reports["Purchase Analytics"] = {
+	"filters": [
+		{
+			fieldname: "tree_type",
+			label: __("Tree Type"),
+			fieldtype: "Select",
+			options: ["Supplier Group","Supplier","Item Group","Item"],
+			default: "Supplier",
+			reqd: 1
+		},
+		{
+			fieldname: "doc_type",
+			label: __("based_on"),
+			fieldtype: "Select",
+			options: ["Purchase Order","Purchase Receipt","Purchase Invoice"],
+			default: "Purchase Invoice",
+			reqd: 1
+		},
+		{
+			fieldname: "value_quantity",
+			label: __("Value Or Qty"),
+			fieldtype: "Select",
+			options: [
+				{ "value": "Value", "label": __("Value") },
+				{ "value": "Quantity", "label": __("Quantity") },
+			],
+			default: "Value",
+			reqd: 1
+		},
+		{
+			fieldname: "from_date",
+			label: __("From Date"),
+			fieldtype: "Date",
+			default: frappe.defaults.get_user_default("year_start_date"),
+			reqd: 1
+		},
+		{
+			fieldname:"to_date",
+			label: __("To Date"),
+			fieldtype: "Date",
+			default: frappe.defaults.get_user_default("year_end_date"),
+			reqd: 1
+		},
+		{
+			fieldname: "company",
+			label: __("Company"),
+			fieldtype: "Link",
+			options: "Company",
+			default: frappe.defaults.get_user_default("Company"),
+			reqd: 1
+		},
+		{
+			fieldname: "range",
+			label: __("Range"),
+			fieldtype: "Select",
+			options: [
+				{ "value": "Weekly", "label": __("Weekly") },
+				{ "value": "Monthly", "label": __("Monthly") },
+				{ "value": "Quarterly", "label": __("Quarterly") },
+				{ "value": "Yearly", "label": __("Yearly") }
+			],
+			default: "Monthly",
+			reqd: 1
+		}
+
+	],
+	"formatter": function(value, row, column, data) {
+		if(!value){
+			value = 0
+		}
+		return value;
+	},
+	get_datatable_options(options) {
+		return Object.assign(options, {
+			checkboxColumn: true,
+			events: {
+				onCheckRow: function(data) {
+					row_name = data[2].content;
+					row_values = data.slice(5).map(function (column) {
+						return column.content;
+					})
+
+					entry  = {
+						'name':row_name,
+						'values':row_values
+					}
+
+					let raw_data = frappe.query_report.chart.data;
+					let new_datasets = raw_data.datasets;
+
+					var found = false;
+
+					for(var i=0; i < new_datasets.length;i++){
+						if(new_datasets[i].name == row_name){
+							found = true;
+							new_datasets.splice(i,1);
+							break;
+						}
+					}
+
+					if(!found){
+						new_datasets.push(entry);
+					}
+
+					let new_data = {
+						labels: raw_data.labels,
+						datasets: new_datasets
+					}
+					
+					setTimeout(() => {
+						frappe.query_report.chart.update(new_data)
+					},200)
+					
+					
+					setTimeout(() => {
+						frappe.query_report.chart.draw(true);
+					}, 800)
+
+					frappe.query_report.raw_chart_data = new_data;
+				},
+			}
+		})
+	},
+}
diff --git a/erpnext/buying/report/purchase_analytics/purchase_analytics.json b/erpnext/buying/report/purchase_analytics/purchase_analytics.json
new file mode 100644
index 0000000..996e3ee
--- /dev/null
+++ b/erpnext/buying/report/purchase_analytics/purchase_analytics.json
@@ -0,0 +1,26 @@
+{
+ "add_total_row": 0, 
+ "creation": "2018-10-05 16:08:24.156448", 
+ "disabled": 0, 
+ "docstatus": 0, 
+ "doctype": "Report", 
+ "idx": 0, 
+ "is_standard": "Yes", 
+ "modified": "2018-10-05 16:08:33.272201", 
+ "modified_by": "Administrator", 
+ "module": "Buying", 
+ "name": "Purchase Analytics", 
+ "owner": "Administrator", 
+ "prepared_report": 0, 
+ "ref_doctype": "Purchase Order", 
+ "report_name": "Purchase Analytics", 
+ "report_type": "Script Report", 
+ "roles": [
+  {
+   "role": "Purchase Manager"
+  }, 
+  {
+   "role": "Purchase User"
+  }
+ ]
+}
\ No newline at end of file
diff --git a/erpnext/buying/report/purchase_analytics/purchase_analytics.py b/erpnext/buying/report/purchase_analytics/purchase_analytics.py
new file mode 100644
index 0000000..0f94947
--- /dev/null
+++ b/erpnext/buying/report/purchase_analytics/purchase_analytics.py
@@ -0,0 +1,8 @@
+# Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and contributors
+# For license information, please see license.txt
+
+from __future__ import unicode_literals
+from erpnext.selling.report.sales_analytics.sales_analytics import Analytics
+
+def execute(filters=None):
+	return Analytics(filters).run()
diff --git a/erpnext/config/buying.py b/erpnext/config/buying.py
index 270519e..e99b1d8 100644
--- a/erpnext/config/buying.py
+++ b/erpnext/config/buying.py
@@ -122,10 +122,10 @@
 			"icon": "fa fa-table",
 			"items": [
 				{
-					"type": "page",
-					"name": "purchase-analytics",
-					"label": _("Purchase Analytics"),
-					"icon": "fa fa-bar-chart",
+					"type": "report",
+					"is_query_report": True,
+					"name": "Purchase Analytics",
+					"doctype": "Purchase Order"
 				},
 				{
 					"type": "report",
diff --git a/erpnext/config/manufacturing.py b/erpnext/config/manufacturing.py
index 16ca914..621c2dc 100644
--- a/erpnext/config/manufacturing.py
+++ b/erpnext/config/manufacturing.py
@@ -112,11 +112,12 @@
 					"is_query_report": True,
 					"name": "Completed Work Orders",
 					"doctype": "Work Order"
-				},{ 
-					"type": "page",
-					"name": "production-analytics",
-					"label": _("Production Analytics"),  
-					"icon": "fa fa-bar-chart",
+				},
+				{
+					"type": "report",
+					"is_query_report": True,
+					"name": "Production Analytics",
+					"doctype": "Work Order"
 				},
 				{
 					"type": "report",