Merge pull request #18852 from mbhavesh95863/patch-1

fix(selling): remove redundant listing
diff --git a/erpnext/config/buying.py b/erpnext/config/buying.py
index f5d8da7..6f5ab32 100644
--- a/erpnext/config/buying.py
+++ b/erpnext/config/buying.py
@@ -16,6 +16,12 @@
 				},
 				{
 					"type": "doctype",
+					"name": "Purchase Invoice",
+					"onboard": 1,
+					"dependencies": ["Item", "Supplier"]
+				},
+				{
+					"type": "doctype",
 					"name": "Material Request",
 					"onboard": 1,
 					"dependencies": ["Item"],
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index dc6c6da..07b8ee6 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -625,5 +625,10 @@
 erpnext.patches.v12_0.update_ewaybill_field_position
 erpnext.patches.v12_0.create_accounting_dimensions_in_missing_doctypes
 erpnext.patches.v11_1.set_status_for_material_request_type_manufacture
+execute:frappe.reload_doc('desk', 'doctype','dashboard_chart_link')
+execute:frappe.reload_doc('desk', 'doctype','dashboard')
+execute:frappe.reload_doc('desk', 'doctype','dashboard_chart_source')
+execute:frappe.reload_doc('desk', 'doctype','dashboard_chart')
+erpnext.patches.v12_0.add_default_dashboards
 erpnext.patches.v12_0.remove_bank_remittance_custom_fields
 erpnext.patches.v12_0.generate_leave_ledger_entries
diff --git a/erpnext/patches/v12_0/add_default_dashboards.py b/erpnext/patches/v12_0/add_default_dashboards.py
new file mode 100644
index 0000000..ab92fba
--- /dev/null
+++ b/erpnext/patches/v12_0/add_default_dashboards.py
@@ -0,0 +1,8 @@
+# Copyright (c) 2019, Frappe and Contributors
+# License: GNU General Public License v3. See license.txt
+
+import frappe
+from erpnext.setup.setup_wizard.operations.install_fixtures import add_dashboards
+
+def execute():
+	add_dashboards()
diff --git a/erpnext/setup/setup_wizard/data/dashboard_charts.py b/erpnext/setup/setup_wizard/data/dashboard_charts.py
new file mode 100644
index 0000000..41cfcb5
--- /dev/null
+++ b/erpnext/setup/setup_wizard/data/dashboard_charts.py
@@ -0,0 +1,107 @@
+from __future__ import unicode_literals
+from frappe import _
+import frappe
+import json
+
+def get_default_dashboards():
+	company = frappe.get_doc("Company", frappe.defaults.get_defaults().company)
+	income_account = company.default_income_account or get_account("Income Account", company.name)
+	expense_account = company.default_expense_account or get_account("Expense Account", company.name)
+	bank_account = company.default_bank_account or get_account("Bank", company.name)
+
+	return {
+		"Dashboards": [
+			{
+				"doctype": "Dashboard",
+				"dashboard_name": "Accounts",
+				"charts": [
+					{ "chart": "Outgoing Bills (Sales Invoice)" },
+					{ "chart": "Incoming Bills (Purchase Invoice)" },
+					{ "chart": "Bank Balance" },
+					{ "chart": "Income" },
+					{ "chart": "Expenses" }
+				]
+			}
+		],
+		"Charts": [
+			{
+				"doctype": "Dashboard Chart",
+				"time_interval": "Quarterly",
+				"chart_name": "Income",
+				"timespan": "Last Year",
+				"color": None,
+				"filters_json": json.dumps({"company": company.name, "account": income_account}),
+				"source": "Account Balance Timeline",
+				"chart_type": "Custom",
+				"timeseries": 1,
+				"owner": "Administrator",
+				"type": "Line",
+				"width": "Half"
+			},
+			{
+				"doctype": "Dashboard Chart",
+				"time_interval": "Quarterly",
+				"chart_name": "Expenses",
+				"timespan": "Last Year",
+				"color": None,
+				"filters_json": json.dumps({"company": company.name, "account": expense_account}),
+				"source": "Account Balance Timeline",
+				"chart_type": "Custom",
+				"timeseries": 1,
+				"owner": "Administrator",
+				"type": "Line",
+				"width": "Half"
+			},
+			{
+				"doctype": "Dashboard Chart",
+				"time_interval": "Quarterly",
+				"chart_name": "Bank Balance",
+				"timespan": "Last Year",
+				"color": "#ffb868",
+				"filters_json": json.dumps({"company": company.name, "account": bank_account}),
+				"source": "Account Balance Timeline",
+				"chart_type": "Custom",
+				"timeseries": 1,
+				"owner": "Administrator",
+				"type": "Line",
+				"width": "Half"
+			},
+			{
+				"doctype": "Dashboard Chart",
+				"time_interval": "Monthly",
+				"chart_name": "Incoming Bills (Purchase Invoice)",
+				"timespan": "Last Year",
+				"color": "#a83333",
+				"value_based_on": "base_grand_total",
+				"filters_json": json.dumps({}),
+				"chart_type": "Sum",
+				"timeseries": 1,
+				"based_on": "posting_date",
+				"owner": "Administrator",
+				"document_type": "Purchase Invoice",
+				"type": "Bar",
+				"width": "Half"
+			},
+			{
+				"doctype": "Dashboard Chart",
+				"time_interval": "Monthly",
+				"chart_name": "Outgoing Bills (Sales Invoice)",
+				"timespan": "Last Year",
+				"color": "#7b933d",
+				"value_based_on": "base_grand_total",
+				"filters_json": json.dumps({}),
+				"chart_type": "Sum",
+				"timeseries": 1,
+				"based_on": "posting_date",
+				"owner": "Administrator",
+				"document_type": "Sales Invoice",
+				"type": "Bar",
+				"width": "Half"
+			}
+		]
+	}
+
+def get_account(account_type, company):
+	accounts = frappe.get_list("Account", filters={"account_type": account_type, "company": company})
+	if accounts:
+		return accounts[0].name
\ No newline at end of file
diff --git a/erpnext/setup/setup_wizard/operations/install_fixtures.py b/erpnext/setup/setup_wizard/operations/install_fixtures.py
index 4b734df..7123021 100644
--- a/erpnext/setup/setup_wizard/operations/install_fixtures.py
+++ b/erpnext/setup/setup_wizard/operations/install_fixtures.py
@@ -475,13 +475,14 @@
 
 				frappe.db.set_value("Company", args.company_name, "default_bank_account", bank_account.name, update_modified=False)
 
-				return doc
 			except RootNotEditable:
 				frappe.throw(_("Bank account cannot be named as {0}").format(args.bank_account))
 			except frappe.DuplicateEntryError:
 				# bank account same as a CoA entry
 				pass
 
+	add_dashboards()
+
 	# Now, with fixtures out of the way, onto concrete stuff
 	records = [
 
@@ -499,6 +500,13 @@
 
 	make_records(records)
 
+def add_dashboards():
+	from erpnext.setup.setup_wizard.data.dashboard_charts import get_default_dashboards
+	dashboard_data = get_default_dashboards()
+
+	make_records(dashboard_data["Charts"])
+	make_records(dashboard_data["Dashboards"])
+
 
 def get_fy_details(fy_start_date, fy_end_date):
 	start_year = getdate(fy_start_date).year
diff --git a/erpnext/stock/stock_ledger.py b/erpnext/stock/stock_ledger.py
index 920fc27..f7deac3 100644
--- a/erpnext/stock/stock_ledger.py
+++ b/erpnext/stock/stock_ledger.py
@@ -227,9 +227,9 @@
 		elif actual_qty < 0:
 			# In case of delivery/stock issue, get average purchase rate
 			# of serial nos of current entry
-			stock_value_change = -1 * flt(frappe.db.sql("""select sum(purchase_rate)
-				from `tabSerial No` where name in (%s)""" % (", ".join(["%s"]*len(serial_no))),
-				tuple(serial_no))[0][0])
+			stock_value_change = -1 * flt(frappe.get_all("Serial No",
+				fields=["sum(purchase_rate)"],
+				filters = {'name': ('in', serial_no)}, as_list=1)[0][0])
 
 		new_stock_qty = self.qty_after_transaction + actual_qty