Merge pull request #4272 from nabinhait/report-fix1

[report] Added delay in payment column
diff --git a/erpnext/accounts/report/payment_period_based_on_invoice_date/payment_period_based_on_invoice_date.py b/erpnext/accounts/report/payment_period_based_on_invoice_date/payment_period_based_on_invoice_date.py
index 786a494..20bdae8 100644
--- a/erpnext/accounts/report/payment_period_based_on_invoice_date/payment_period_based_on_invoice_date.py
+++ b/erpnext/accounts/report/payment_period_based_on_invoice_date/payment_period_based_on_invoice_date.py
@@ -5,7 +5,7 @@
 import frappe
 from frappe import _
 from erpnext.accounts.report.accounts_receivable.accounts_receivable import get_ageing_data
-from frappe.utils import flt
+from frappe.utils import flt, getdate
 
 def execute(filters=None):
 	if not filters: filters = {}
@@ -13,25 +13,27 @@
 
 	columns = get_columns(filters)
 	entries = get_entries(filters)
-	invoice_posting_date_map = get_invoice_posting_date_map(filters)
+	invoice_details = get_invoice_posting_date_map(filters)
 	against_date = ""
 
 	data = []
 	for d in entries:
-		against_date = invoice_posting_date_map.get(d.reference_name) or ""
+		invoice = invoice_details.get(d.reference_name) or frappe._dict()
 		if d.reference_type=="Purchase Invoice":
 			payment_amount = flt(d.debit) or -1 * flt(d.credit)
 		else:
 			payment_amount = flt(d.credit) or -1 * flt(d.debit)
 
-		row = [d.name, d.party_type, d.party, d.posting_date, d.reference_name,
-			against_date, d.debit, d.credit, d.cheque_no, d.cheque_date, d.remark]
+		row = [d.name, d.party_type, d.party, d.posting_date, d.reference_name, invoice.posting_date, 
+			invoice.due_date, d.debit, d.credit, d.cheque_no, d.cheque_date, d.remark]
 
 		if d.reference_name:
 			row += get_ageing_data(30, 60, 90, d.posting_date, against_date, payment_amount)
 		else:
 			row += ["", "", "", "", ""]
-
+		if invoice.due_date:
+			row.append((getdate(d.posting_date) - getdate(invoice.due_date)).days or 0)
+		
 		data.append(row)
 
 	return columns, data
@@ -43,13 +45,25 @@
 				.format(filters.payment_type, filters.party_type))
 
 def get_columns(filters):
-	return [_("Journal Entry") + ":Link/Journal Entry:140",
-		_("Party Type") + "::100", _("Party") + ":Dynamic Link/Party Type:140",
+	return [
+		_("Journal Entry") + ":Link/Journal Entry:140",
+		_("Party Type") + "::100", 
+		_("Party") + ":Dynamic Link/Party Type:140",
 		_("Posting Date") + ":Date:100",
-		_("Against Invoice") + (":Link/Purchase Invoice:130" if filters.get("payment_type") == "Outgoing" else ":Link/Sales Invoice:130"),
-		_("Against Invoice Posting Date") + ":Date:130", _("Debit") + ":Currency:120", _("Credit") + ":Currency:120",
-		_("Reference No") + "::100", _("Reference Date") + ":Date:100", _("Remarks") + "::150", _("Age") +":Int:40",
-		"0-30:Currency:100", "30-60:Currency:100", "60-90:Currency:100", _("90-Above") + ":Currency:100"
+		_("Invoice") + (":Link/Purchase Invoice:130" if filters.get("payment_type") == "Outgoing" else ":Link/Sales Invoice:130"),
+		_("Invoice Posting Date") + ":Date:130", 
+		_("Payment Due Date") + ":Date:130", 
+		_("Debit") + ":Currency:120", 
+		_("Credit") + ":Currency:120",
+		_("Reference No") + "::100", 
+		_("Reference Date") + ":Date:100", 
+		_("Remarks") + "::150", 
+		_("Age") +":Int:40",
+		"0-30:Currency:100", 
+		"30-60:Currency:100", 
+		"60-90:Currency:100", 
+		_("90-Above") + ":Currency:100",
+		_("Delay in payment (Days)") + "::150"
 	]
 
 def get_conditions(filters):
@@ -66,7 +80,14 @@
 
 	if filters.get("party"):
 		conditions.append("jvd.party=%(party)s")
-
+		
+	if filters.get("party_type"):
+		conditions.append("jvd.reference_type=%(reference_type)s")
+		if filters.get("party_type") == "Customer":
+			filters["reference_type"] = "Sales Invoice"
+		else:
+			filters["reference_type"] = "Purchase Invoice"
+		
 	if filters.get("company"):
 		conditions.append("jv.company=%(company)s")
 
@@ -89,12 +110,9 @@
 	return entries
 
 def get_invoice_posting_date_map(filters):
-	invoice_posting_date_map = {}
-	if filters.get("payment_type") == "Incoming":
-		for t in frappe.db.sql("""select name, posting_date from `tabSales Invoice`"""):
-			invoice_posting_date_map[t[0]] = t[1]
-	else:
-		for t in frappe.db.sql("""select name, posting_date from `tabPurchase Invoice`"""):
-			invoice_posting_date_map[t[0]] = t[1]
+	invoice_details = {}
+	dt = "Sales Invoice" if filters.get("payment_type") == "Incoming" else "Purchase Invoice"
+	for t in frappe.db.sql("select name, posting_date, due_date from `tab{0}`".format(dt), as_dict=1):
+		invoice_details[t.name] = t
 
-	return invoice_posting_date_map
+	return invoice_details