Added test case for Accounts receivable report based on payment terms
diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
index 94037c7..68cc500 100644
--- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
@@ -375,7 +375,7 @@
 		si.insert()
 
 		self.assertEqual(si.net_total, 4600)
-		
+
 		self.assertEqual(si.get("taxes")[0].tax_amount, 874.0)
 		self.assertEqual(si.get("taxes")[0].total, 5474.0)
 
@@ -405,12 +405,12 @@
 
 		self.assertEqual(si.total, 975)
 		self.assertEqual(si.net_total, 900)
-		
+
 		self.assertEqual(si.get("taxes")[0].tax_amount, 216.0)
 		self.assertEqual(si.get("taxes")[0].total, 1116.0)
 
 		self.assertEqual(si.grand_total, 1116.0)
-		
+
 	def test_inclusive_rate_validations(self):
 		si = frappe.copy_doc(test_records[2])
 		for i, tax in enumerate(si.get("taxes")):
@@ -552,7 +552,7 @@
 		self.assertEqual(si.grand_total, 1215.90)
 		self.assertEqual(si.rounding_adjustment, 0.01)
 		self.assertEqual(si.base_rounding_adjustment, 0.50)
-		
+
 
 	def test_outstanding(self):
 		w = self.make()
@@ -923,7 +923,7 @@
 		self.assertRaises(SerialNoWarehouseError, si.submit)
 
 	def test_serial_numbers_against_delivery_note(self):
-		""" 
+		"""
 			check if the sales invoice item serial numbers and the delivery note items
 			serial numbers are same
 		"""
@@ -1238,7 +1238,7 @@
 
 	def test_item_wise_tax_breakup_india(self):
 		frappe.flags.country = "India"
-		
+
 		si = self.create_si_to_test_tax_breakup()
 		itemised_tax, itemised_taxable_amount = get_itemised_tax_breakup_data(si)
 
@@ -1256,12 +1256,12 @@
 
 		self.assertEqual(itemised_tax, expected_itemised_tax)
 		self.assertEqual(itemised_taxable_amount, expected_itemised_taxable_amount)
-		
+
 		frappe.flags.country = None
 
 	def test_item_wise_tax_breakup_outside_india(self):
 		frappe.flags.country = "United States"
-		
+
 		si = self.create_si_to_test_tax_breakup()
 
 		itemised_tax, itemised_taxable_amount = get_itemised_tax_breakup_data(si)
@@ -1287,7 +1287,7 @@
 
 		self.assertEqual(itemised_tax, expected_itemised_tax)
 		self.assertEqual(itemised_taxable_amount, expected_itemised_taxable_amount)
-		
+
 		frappe.flags.country = None
 
 	def create_si_to_test_tax_breakup(self):
@@ -1375,7 +1375,7 @@
 		shipping_rule = create_shipping_rule(shipping_rule_type = "Selling", shipping_rule_name = "Shipping Rule - Sales Invoice Test")
 
 		si = frappe.copy_doc(test_records[2])
-		
+
 		si.shipping_rule = shipping_rule.name
 		si.insert()
 
@@ -1392,14 +1392,14 @@
 			"cost_center": shipping_rule.cost_center,
 			"tax_amount": shipping_amount,
 			"description": shipping_rule.name
-		}	
+		}
 		si.append("taxes", shipping_charge)
 		si.save()
 
 		self.assertEqual(si.net_total, 1250)
 
 		self.assertEqual(si.total_taxes_and_charges, 577.05)
-		self.assertEqual(si.grand_total, 1827.05)		
+		self.assertEqual(si.grand_total, 1827.05)
 
 	def test_create_invoice_without_terms(self):
 		si = create_sales_invoice(do_not_save=1)
@@ -1496,7 +1496,7 @@
 
 		for gle in gl_entries:
 			self.assertEqual(expected_values[gle.account]["cost_center"], gle.cost_center)
-		
+
 		accounts_settings.allow_cost_center_in_entry_of_bs_account = 0
 		accounts_settings.save()
 
@@ -1524,9 +1524,9 @@
 		"warehouse": args.warehouse or "_Test Warehouse - _TC",
 		"qty": args.qty or 1,
 		"rate": args.rate or 100,
-		"income_account": "Sales - _TC",
-		"expense_account": "Cost of Goods Sold - _TC",
-		"cost_center": "_Test Cost Center - _TC",
+		"income_account": args.income_account or "Sales - _TC",
+		"expense_account": args.expense_account or "Cost of Goods Sold - _TC",
+		"cost_center": args.cost_center or "_Test Cost Center - _TC",
 		"serial_no": args.serial_no
 	})
 
diff --git a/erpnext/accounts/report/accounts_receivable/accounts_receivable.py b/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
index 3d20039..8b16ae3 100755
--- a/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
+++ b/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
@@ -221,6 +221,22 @@
 									d.credit_note_amount, d.due_date, d.payment_amount , d.payment_term_amount,
 									d.description, d.pdc_amount, d.pdc_details)
 								data.append(row)
+
+						if credit_note_amount:
+							outstanding_amount, credit_note_amount, payment_amount = self.get_outstanding_amount(
+							gle,self.filters.report_date, self.dr_or_cr, return_entries)
+
+							pdc_amount = 0
+							pdc_details = []
+							for d in pdc_list:
+								pdc_amount += flt(d.pdc_amount)
+								if pdc_amount and d.pdc_ref and d.pdc_date:
+									pdc_details.append(cstr(d.pdc_ref) + "/" + formatdate(d.pdc_date))
+
+							row = self.prepare_row(party_naming_by, args, gle, outstanding_amount,
+								credit_note_amount, pdc_amount=pdc_amount, pdc_details=pdc_details)
+							data.append(row)
+
 					else:
 						pdc_amount = 0
 						pdc_details = []
diff --git a/erpnext/accounts/report/accounts_receivable/test_accounts_receivable.py b/erpnext/accounts/report/accounts_receivable/test_accounts_receivable.py
new file mode 100644
index 0000000..34e6c83
--- /dev/null
+++ b/erpnext/accounts/report/accounts_receivable/test_accounts_receivable.py
@@ -0,0 +1,84 @@
+import frappe
+import frappe.defaults
+import unittest
+from frappe.utils import today, getdate, add_days
+from erpnext.accounts.report.accounts_receivable.accounts_receivable import execute
+from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_sales_invoice
+from erpnext.accounts.doctype.payment_entry.payment_entry import get_payment_entry
+
+class TestAccountsReceivable(unittest.TestCase):
+	def test_accounts_receivable(self):
+		frappe.db.sql("delete from `tabSales Invoice` where company='_Test Company 2'")
+		frappe.db.sql("delete from `tabGL Entry` where company='_Test Company 2'")
+
+		filters = {
+			'company': '_Test Company 2',
+			'based_on_payment_terms': 1
+		}
+
+		name = make_sales_invoice()
+		report = execute(filters)
+
+		expected_data = [[100,30], [100,50], [100,20]]
+
+		self.assertEqual(expected_data[0], report[1][0][6:8])
+		self.assertEqual(expected_data[1], report[1][1][6:8])
+		self.assertEqual(expected_data[2], report[1][2][6:8])
+
+		make_payment(name)
+		report = execute(filters)
+
+		expected_data_after_payment = [[100,50], [100,20]]
+
+		self.assertEqual(expected_data_after_payment[0], report[1][0][6:8])
+		self.assertEqual(expected_data_after_payment[1], report[1][1][6:8])
+
+		make_credit_note(name)
+		report = execute(filters)
+
+		expected_data_after_credit_note = [[100,100,30,100,-30]]
+
+		self.assertEqual(expected_data_after_credit_note[0], report[1][0][6:11])
+
+
+def make_sales_invoice():
+	frappe.set_user("Administrator")
+
+	si = create_sales_invoice(company="_Test Company 2",
+			customer = '_Test Customer 2',
+			currency = 'EUR',
+			warehouse = 'Finished Goods - _TC2',
+			debit_to = 'Debtors - _TC2',
+			income_account = 'Sales - _TC2',
+			expense_account = 'Cost of Goods Sold - _TC2',
+			cost_center = '_Test Company 2 - _TC2',
+			do_not_save=1)
+
+	si.append('payment_schedule', dict(due_date=getdate(add_days(today(), 30)), invoice_portion=30.00, payment_amount=30))
+	si.append('payment_schedule', dict(due_date=getdate(add_days(today(), 60)), invoice_portion=50.00, payment_amount=50))
+	si.append('payment_schedule', dict(due_date=getdate(add_days(today(), 90)), invoice_portion=20.00, payment_amount=20))
+
+	si.submit()
+
+	return si.name
+
+def make_payment(docname):
+	pe = get_payment_entry("Sales Invoice", docname, bank_account="Cash - _TC2", party_amount=30)
+	pe.paid_from = "Debtors - _TC2"
+	pe.insert()
+	pe.submit()
+
+
+def make_credit_note(docname):
+	create_sales_invoice(company="_Test Company 2",
+			customer = '_Test Customer 2',
+			currency = 'EUR',
+			qty = -1,
+			warehouse = 'Finished Goods - _TC2',
+			debit_to = 'Debtors - _TC2',
+			income_account = 'Sales - _TC2',
+			expense_account = 'Cost of Goods Sold - _TC2',
+			cost_center = '_Test Company 2 - _TC2',
+			is_return = 1,
+			return_against = docname)
+