fix: Show GL balance in Accounts Receivable and payable summary

(cherry picked from commit 1d270dca05501b5a15d19e3270188029b48eafde)

# Conflicts:
#	erpnext/accounts/report/accounts_receivable_summary/accounts_receivable_summary.py
diff --git a/erpnext/accounts/report/accounts_receivable_summary/accounts_receivable_summary.js b/erpnext/accounts/report/accounts_receivable_summary/accounts_receivable_summary.js
index 305cddb..715cd64 100644
--- a/erpnext/accounts/report/accounts_receivable_summary/accounts_receivable_summary.js
+++ b/erpnext/accounts/report/accounts_receivable_summary/accounts_receivable_summary.js
@@ -117,6 +117,11 @@
 			"label": __("Show Future Payments"),
 			"fieldtype": "Check",
 		},
+		{
+			"fieldname":"show_gl_balance",
+			"label": __("Show GL Balance"),
+			"fieldtype": "Check",
+		},
 	],
 
 	onload: function(report) {
diff --git a/erpnext/accounts/report/accounts_receivable_summary/accounts_receivable_summary.py b/erpnext/accounts/report/accounts_receivable_summary/accounts_receivable_summary.py
index 3c94629..7b69d3e 100644
--- a/erpnext/accounts/report/accounts_receivable_summary/accounts_receivable_summary.py
+++ b/erpnext/accounts/report/accounts_receivable_summary/accounts_receivable_summary.py
@@ -4,7 +4,12 @@
 
 import frappe
 from frappe import _, scrub
+<<<<<<< HEAD
 from frappe.utils import cint
+=======
+from frappe.utils import cint, flt
+from six import iteritems
+>>>>>>> 1d270dca05 (fix: Show GL balance in Accounts Receivable and payable summary)
 
 from erpnext.accounts.party import get_partywise_advanced_payment_amount
 from erpnext.accounts.report.accounts_receivable.accounts_receivable import ReceivablePayableReport
@@ -36,7 +41,14 @@
 		party_advance_amount = get_partywise_advanced_payment_amount(self.party_type,
 			self.filters.report_date, self.filters.show_future_payments, self.filters.company) or {}
 
+<<<<<<< HEAD
 		for party, party_dict in self.party_total.items():
+=======
+		if self.filters.show_gl_balance:
+			gl_balance_map = get_gl_balance(self.filters.report_date)
+
+		for party, party_dict in iteritems(self.party_total):
+>>>>>>> 1d270dca05 (fix: Show GL balance in Accounts Receivable and payable summary)
 			if party_dict.outstanding == 0:
 				continue
 
@@ -55,6 +67,10 @@
 			# but in summary report advance shown in separate column
 			row.paid -= row.advance
 
+			if self.filters.show_gl_balance:
+				row.gl_balance = gl_balance_map.get(party)
+				row.diff = flt(row.outstanding) - flt(row.gl_balance)
+
 			self.data.append(row)
 
 	def get_party_total(self, args):
@@ -114,6 +130,10 @@
 		self.add_column(_(credit_debit_label), fieldname='credit_note')
 		self.add_column(_('Outstanding Amount'), fieldname='outstanding')
 
+		if self.filters.show_gl_balance:
+			self.add_column(_('GL Balance'), fieldname='gl_balance')
+			self.add_column(_('Difference'), fieldname='diff')
+
 		self.setup_ageing_columns()
 
 		if self.party_type == "Customer":
@@ -140,3 +160,7 @@
 
 		# Add column for total due amount
 		self.add_column(label="Total Amount Due", fieldname='total_due')
+
+def get_gl_balance(report_date):
+	return frappe._dict(frappe.db.get_all("GL Entry", fields=['party', 'sum(debit -  credit)'],
+		filters={'posting_date': ("<=", report_date), 'is_cancelled': 0}, group_by='party', as_list=1))