Merge branch 'staging-fixes' into supplier-customer-dashboard-fix
diff --git a/erpnext/accounts/party.py b/erpnext/accounts/party.py
index 8c7f3d8..e5cdad1 100644
--- a/erpnext/accounts/party.py
+++ b/erpnext/accounts/party.py
@@ -72,7 +72,7 @@
 
 	return out
 
-def set_address_details(out, party, party_type, doctype=None, company=None, party_address=None, shipping_address=None):  
+def set_address_details(out, party, party_type, doctype=None, company=None, party_address=None, shipping_address=None):
 	billing_address_field = "customer_address" if party_type == "Lead" \
 		else party_type.lower() + "_address"
 	out[billing_address_field] = party_address or get_default_address(party_type, party.name)
@@ -459,38 +459,65 @@
 
 def get_dashboard_info(party_type, party):
 	current_fiscal_year = get_fiscal_year(nowdate(), as_dict=True)
-	company = frappe.db.get_default("company") or frappe.get_all("Company")[0].name
-	party_account_currency = get_party_account_currency(party_type, party, company)
-	company_default_currency = get_default_currency() \
-		or frappe.get_cached_value('Company',  company,  'default_currency')
-
-	if party_account_currency==company_default_currency:
-		total_field = "base_grand_total"
-	else:
-		total_field = "grand_total"
 
 	doctype = "Sales Invoice" if party_type=="Customer" else "Purchase Invoice"
 
-	billing_this_year = frappe.db.sql("""
-		select sum({0})
-		from `tab{1}`
-		where {2}=%s and docstatus=1 and posting_date between %s and %s
-	""".format(total_field, doctype, party_type.lower()),
-	(party, current_fiscal_year.year_start_date, current_fiscal_year.year_end_date))
+	companies = frappe.get_all(doctype, filters={
+		'docstatus': 1,
+		party_type.lower(): party
+	}, distinct=1, fields=['company'])
 
-	total_unpaid = frappe.db.sql("""
-		select sum(debit_in_account_currency) - sum(credit_in_account_currency)
+	company_wise_info = []
+
+	company_wise_grand_total = frappe.get_all(doctype,
+		filters={
+			'docstatus': 1,
+			party_type.lower(): party,
+			'posting_date': ('between', [current_fiscal_year.year_start_date, current_fiscal_year.year_end_date])
+			},
+			group_by="company",
+			fields=["company", "sum(grand_total) as grand_total", "sum(base_grand_total) as base_grand_total"]
+		)
+
+	company_wise_billing_this_year = frappe._dict()
+
+	for d in company_wise_grand_total:
+		company_wise_billing_this_year.setdefault(
+			d.company,{
+				"grand_total": d.grand_total,
+				"base_grand_total": d.base_grand_total
+			})
+
+
+	company_wise_total_unpaid = frappe._dict(frappe.db.sql("""
+		select company, sum(debit_in_account_currency) - sum(credit_in_account_currency)
 		from `tabGL Entry`
-		where party_type = %s and party=%s""", (party_type, party))
+		where party_type = %s and party=%s
+		group by company""", (party_type, party)))
 
-	info = {}
-	info["billing_this_year"] = flt(billing_this_year[0][0]) if billing_this_year else 0
-	info["currency"] = party_account_currency
-	info["total_unpaid"] = flt(total_unpaid[0][0]) if total_unpaid else 0
-	if party_type == "Supplier":
-		info["total_unpaid"] = -1 * info["total_unpaid"]
+	for d in companies:
+		company_default_currency = frappe.db.get_value("Company", d.company, 'default_currency')
+		party_account_currency = get_party_account_currency(party_type, party, d.company)
 
-	return info
+		if party_account_currency==company_default_currency:
+			billing_this_year = flt(company_wise_billing_this_year.get(d.company,{}).get("base_grand_total"))
+		else:
+			billing_this_year = flt(company_wise_billing_this_year.get(d.company,{}).get("grand_total"))
+
+		total_unpaid = flt(company_wise_total_unpaid.get(d.company))
+
+		info = {}
+		info["billing_this_year"] = flt(billing_this_year) if billing_this_year else 0
+		info["currency"] = party_account_currency
+		info["total_unpaid"] = flt(total_unpaid) if total_unpaid else 0
+		info["company"] = d.company
+
+		if party_type == "Supplier":
+			info["total_unpaid"] = -1 * info["total_unpaid"]
+
+		company_wise_info.append(info)
+
+	return company_wise_info
 
 def get_party_shipping_address(doctype, name):
 	"""
diff --git a/erpnext/public/js/utils.js b/erpnext/public/js/utils.js
index baee68e..e293321 100644
--- a/erpnext/public/js/utils.js
+++ b/erpnext/public/js/utils.js
@@ -103,12 +103,32 @@
 $.extend(erpnext.utils, {
 	set_party_dashboard_indicators: function(frm) {
 		if(frm.doc.__onload && frm.doc.__onload.dashboard_info) {
-			var info = frm.doc.__onload.dashboard_info;
-			frm.dashboard.add_indicator(__('Annual Billing: {0}',
-				[format_currency(info.billing_this_year, info.currency)]), 'blue');
-			frm.dashboard.add_indicator(__('Total Unpaid: {0}',
-				[format_currency(info.total_unpaid, info.currency)]),
-				info.total_unpaid ? 'orange' : 'green');
+			var company_wise_info = frm.doc.__onload.dashboard_info;
+			if(company_wise_info.length > 1) {
+				frm.dashboard.stats_area.removeClass('hidden');
+				frm.dashboard.stats_area_row.addClass('flex');
+				frm.dashboard.stats_area_row.css('flex-wrap', 'wrap');
+				company_wise_info.forEach(function(info) {
+					frm.dashboard.stats_area_row.append(
+						'<div class="flex-column col-xs-6">'+
+							'<div style="margin-bottom:20px"><h6>'+info.company+'</h6></div>'+
+							'<div class="badge-link small" style="margin-bottom:10px">Annual Billing: '
+							+format_currency(info.billing_this_year, info.currency)+'</div>'+
+							'<div class="badge-link small" style="margin-bottom:20px">Total Unpaid: '
+							+format_currency(info.total_unpaid, info.currency)+'</div>'+
+						'</div>'
+					);
+				});
+			}
+			else {
+				frm.dashboard.stats_area.removeClass('hidden');
+				frm.dashboard.stats_area_row.append(
+					'</div><div class="col-xs-6 small" style="margin-bottom:10px">Annual Billing: <b>'
+					+format_currency(company_wise_info[0].billing_this_year, company_wise_info[0].currency)+'</b></div>' +
+					'<div class="col-xs-6 small" style="margin-bottom:10px">Total Unpaid: <b>'
+					+format_currency(company_wise_info[0].billing_this_year, company_wise_info[0].currency)+'</b></div>'
+				);
+			}
 		}
 	},