Allocation of credit note amount and pdc in payment terms
diff --git a/erpnext/accounts/report/accounts_receivable/accounts_receivable.py b/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
index 20b19eb..5db186b 100755
--- a/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
+++ b/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
@@ -174,7 +174,7 @@
 		if not self.filters.get("company"):
 			self.filters["company"] = frappe.db.get_single_value('Global Defaults', 'default_company')
 
-		self.company_currency = frappe.get_cached_value('Company',  self.filters.get("company"),  "default_currency")
+		self.company_currency = frappe.get_cached_value('Company',  self.filters.get("company"), "default_currency")
 
 		return_entries = self.get_return_entries(args.get("party_type"))
 
@@ -192,29 +192,37 @@
 
 		for gle in gl_entries_data:
 			if self.is_receivable_or_payable(gle, self.dr_or_cr, future_vouchers):
-				if self.filters.based_on_payment_terms and self.payment_term_map.get(gle.voucher_no):
-					outstanding_amount, credit_note_amount, payment_amount = self.get_outstanding_amount(
-						gle,self.filters.report_date, self.dr_or_cr, return_entries, currency_precision)
-					if abs(outstanding_amount) > 0.1/10**currency_precision:
+				outstanding_amount, credit_note_amount, payment_amount = self.get_outstanding_amount(
+					gle,self.filters.report_date, self.dr_or_cr, return_entries, currency_precision)
+				if abs(outstanding_amount) > 0.1/10**currency_precision:
+					if self.filters.based_on_payment_terms and self.payment_term_map.get(gle.voucher_no):
+						pdc_amount = flt(self.pdc_details.get((gle.voucher_no, gle.party), {}).get("pdc_amount"))
 						for d in self.payment_term_map.get(gle.voucher_no):
-							if payment_amount >= d[1]:
-								payment_amount -= d[1]
+							if payment_amount + credit_note_amount >= d[1]:
+								temp = payment_amount
+								payment_amount = payment_amount - d[1] + credit_note_amount
+								credit_note_amount = credit_note_amount - d[1] + temp - payment_amount
 							else:
-								outstanding_amount = d[1] - payment_amount
-								row = self.prepare_row(party_naming_by, args, gle, outstanding_amount, 
-									credit_note_amount, d[0], payment_amount , d[1], d[2])
+								outstanding_amount = d[1] - payment_amount - credit_note_amount
+								if pdc_amount > outstanding_amount:
+									pdc = outstanding_amount
+									pdc_amount -= outstanding_amount
+								else:
+									pdc = pdc_amount
+									pdc_amount = 0
+
+								row = self.prepare_row(party_naming_by, args, gle, outstanding_amount,
+									credit_note_amount, d[0], payment_amount , d[1], d[2], pdc)
 								payment_amount = 0
+								credit_note_amount = 0
 								data.append(row)
-				else:
-					outstanding_amount, credit_note_amount, payment_amount = self.get_outstanding_amount(
-						gle,self.filters.report_date, self.dr_or_cr, return_entries, currency_precision)
-					if abs(outstanding_amount) > 0.1/10**currency_precision:
+					else:
 						row = self.prepare_row(party_naming_by, args, gle, outstanding_amount, credit_note_amount)
 						data.append(row)
 		return data
 
 	def prepare_row(self, party_naming_by, args, gle, outstanding_amount, credit_note_amount, 
-		due_date=None, paid_amt=None, payment_term_amount=None, payment_term=None):
+		due_date=None, paid_amt=None, payment_term_amount=None, payment_term=None, pdc_amount=None):
 		row = [gle.posting_date, gle.party]
 
 		# customer / supplier name
@@ -274,9 +282,14 @@
 
 		pdc = self.pdc_details.get((gle.voucher_no, gle.party), {})
 
-		remaining_balance = outstanding_amount - flt(pdc.get("pdc_amount"))
-		row += [pdc.get("pdc_date"), pdc.get("pdc_ref"),
-			flt(pdc.get("pdc_amount")), remaining_balance]
+		if pdc_amount == None:
+			pdc_amount = flt(pdc.get("pdc_amount"))
+
+		pdc_date = pdc.get("pdc_date") if pdc_amount else ''
+		pdc_ref = pdc.get("pdc_ref") if pdc_amount else ''
+
+		remaining_balance = outstanding_amount - pdc_amount
+		row += [pdc_date, pdc_ref, pdc_amount, remaining_balance]
 
 <<<<<<< HEAD
 					# customer territory / supplier group
@@ -555,7 +568,7 @@
 		on
 			(pref.parent = pent.name)
 		where
-			pent.docstatus < 2 and pent.posting_date > %s
+			pent.docstatus < 2 and pent.posting_date > %s 
 			and pent.party_type = %s
 			group by pent.party, pref.reference_name""", (report_date, party_type), as_dict=1):
 			pdc_details.setdefault((pdc.invoice_no, pdc.party), pdc)