Customer/Supplier dashboard fix for multi company setup
diff --git a/erpnext/accounts/party.py b/erpnext/accounts/party.py
index f19aaf8..7c12ece 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,61 @@
 
 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.db.sql("""
+		select distinct company from `tab{0}`
+		where docstatus =1 and {1} = %s
+		""".format(doctype, party_type.lower()), (party), as_dict=1)
 
-	total_unpaid = frappe.db.sql("""
-		select sum(debit_in_account_currency) - sum(credit_in_account_currency)
+	company_wise_info = []
+
+	company_wise_grand_total = frappe._dict(frappe.db.sql("""
+			select company, sum(grand_total)
+			from `tab{0}`
+			where {1}=%s and docstatus=1 and posting_date between %s and %s
+			group by company
+		""".format(doctype, party_type.lower()),
+		(party, current_fiscal_year.year_start_date, current_fiscal_year.year_end_date)))
+
+	company_wise_base_grand_total = frappe._dict(frappe.db.sql("""
+			select company, sum(base_grand_total)
+			from `tab{0}`
+			where {1}=%s and docstatus=1 and posting_date between %s and %s
+			group by company
+		""".format(doctype, party_type.lower()),
+		(party, current_fiscal_year.year_start_date, current_fiscal_year.year_end_date)))
+
+	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_base_grand_total.get(d.company))
+		else:
+			billing_this_year = flt(company_wise_grand_total.get(d.company))
+
+		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..4f5470d 100644
--- a/erpnext/public/js/utils.js
+++ b/erpnext/public/js/utils.js
@@ -103,12 +103,17 @@
 $.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;
+			frm.dashboard.add_indicator(__('Company'));
+			frm.dashboard.add_indicator(__('Annual Billing'));
+			frm.dashboard.add_indicator(__('Total Unpaid'));
+			company_wise_info.forEach(function(info){
+				frm.dashboard.add_indicator(__('{0}',[info.company]));
+				frm.dashboard.add_indicator(__('{0}',
+					[format_currency(info.billing_this_year, info.currency)]));
+				frm.dashboard.add_indicator(__('{0}',
+					[format_currency(info.total_unpaid, info.currency)]));
+			});
 		}
 	},