[script report] collection report
diff --git a/accounts/report/accounts_payable/accounts_payable.py b/accounts/report/accounts_payable/accounts_payable.py
index a10dc7e..5e944a1 100644
--- a/accounts/report/accounts_payable/accounts_payable.py
+++ b/accounts/report/accounts_payable/accounts_payable.py
@@ -67,8 +67,7 @@
 	gl_entries = []
 	gl_entries = webnotes.conn.sql("""select * from `tabGL Entry` 
 		where ifnull(is_cancelled, 'No') = 'No' %s order by posting_date, account""" % 
-		(conditions) % (", ".join(['%s']*len(supplier_accounts))), 
-		tuple(supplier_accounts), as_dict=1)
+		(conditions), tuple(supplier_accounts), as_dict=1)
 	return gl_entries
 	
 def get_conditions(filters, before_report_date=True):
@@ -85,7 +84,7 @@
 			conditions, filters)
 	
 	if supplier_accounts:
-		conditions += " and account in (%s)"
+		conditions += " and account in (%s)" % (", ".join(['%s']*len(supplier_accounts)))
 		
 	if filters.get("report_date"):
 		if before_report_date:
diff --git a/accounts/report/accounts_receivable/accounts_receivable.py b/accounts/report/accounts_receivable/accounts_receivable.py
index a8c6d23..47908c3 100644
--- a/accounts/report/accounts_receivable/accounts_receivable.py
+++ b/accounts/report/accounts_receivable/accounts_receivable.py
@@ -58,8 +58,7 @@
 	conditions, customer_accounts = get_conditions(filters, upto_report_date)
 	return webnotes.conn.sql("""select * from `tabGL Entry` 
 		where ifnull(is_cancelled, 'No') = 'No' %s order by posting_date, account""" % 
-		(conditions) % (", ".join(['%s']*len(customer_accounts))), 
-		tuple(customer_accounts), as_dict=1)
+		(conditions), tuple(customer_accounts), as_dict=1)
 	
 def get_conditions(filters, upto_report_date=True):
 	conditions = ""
@@ -75,7 +74,7 @@
 			conditions, filters)
 	
 	if customer_accounts:
-		conditions += " and account in (%s)"
+		conditions += " and account in (%s)" % (", ".join(['%s']*len(customer_accounts)))
 		
 	if filters.get("report_date"):
 		if upto_report_date:
@@ -96,7 +95,7 @@
 def get_si_due_date_map():
 	""" get due_date from sales invoice """
 	si_due_date_map = {}
-	for t in webnotes.conn.sql("""select name, due_date from `tabSales Invoice` group by name"""):
+	for t in webnotes.conn.sql("""select name, due_date from `tabSales Invoice`"""):
 		si_due_date_map[t[0]] = t[1]
 		
 	return si_due_date_map
diff --git a/accounts/report/collection_report/__init__.py b/accounts/report/collection_report/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/accounts/report/collection_report/__init__.py
diff --git a/accounts/report/collection_report/collection_report.js b/accounts/report/collection_report/collection_report.js
new file mode 100644
index 0000000..697a779
--- /dev/null
+++ b/accounts/report/collection_report/collection_report.js
@@ -0,0 +1,42 @@
+wn.query_reports["Collection Report"] = {
+	"filters": [
+		{
+			"fieldname":"from_date",
+			"label": "From Date",
+			"fieldtype": "Date",
+			"default": wn.defaults.get_user_default("year_start_date"),
+			"width": "80"
+		},
+		{
+			"fieldname":"to_date",
+			"label": "To Date",
+			"fieldtype": "Date",
+			"default": get_today()
+		},
+		{
+			"fieldname":"account",
+			"label": "Customer Account",
+			"fieldtype": "Link",
+			"options": "Account",
+			"get_query": function() {
+				var company = wn.query_report.filters_by_name.company.get_value();
+				return {
+					"query": "accounts.utils.get_account_list", 
+					"filters": {
+						"is_pl_account": "No",
+						"debit_or_credit": "Debit",
+						"company": company,
+						"master_type": "Customer"
+					}
+				}
+			}
+		},
+		{
+			"fieldname":"company",
+			"label": "Company",
+			"fieldtype": "Link",
+			"options": "Company",
+			"default": sys_defaults.company
+		},
+	]
+}
\ No newline at end of file
diff --git a/accounts/report/collection_report/collection_report.py b/accounts/report/collection_report/collection_report.py
new file mode 100644
index 0000000..50f74e0
--- /dev/null
+++ b/accounts/report/collection_report/collection_report.py
@@ -0,0 +1,86 @@
+# ERPNext - web based ERP (http://erpnext.com)
+# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
+# 
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+from __future__ import unicode_literals
+import webnotes
+from accounts.report.accounts_receivable.accounts_receivable import get_ageing_data
+
+def execute(filters=None):
+	if not filters: filters = {}
+	
+	columns = get_columns()
+	entries = get_entries(filters)
+	si_posting_date_map = get_si_posting_date_map()
+	
+	data = []
+	for d in entries:
+		against_invoice_date = d.against_invoice and si_posting_date_map[d.against_invoice] or ""
+		
+		row = [d.name, d.account, d.posting_date, d.against_invoice, against_invoice_date, 
+			d.debit, d.credit, d.cheque_no, d.cheque_date, d.remark]
+			
+		if d.against_invoice:
+			row += get_ageing_data(against_invoice_date, d.posting_date, d.credit or -1*d.debit)
+		else:
+			row += ["", "", "", "", ""]
+			
+		data.append(row)
+	
+	return columns, data
+	
+def get_columns():
+	return ["Journal Voucher:Link/Journal Voucher:140", "Account:Link/Account:140", 
+		"Posting Date:Date:100", "Against Invoice: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"
+	]
+
+def get_conditions(filters):
+	conditions = ""
+	
+	customer_accounts = []
+	if filters.get("account"):
+		customer_accounts = [filters["account"]]
+	elif filters.get("company"):
+		customer_accounts = webnotes.conn.sql_list("""select name from `tabAccount` 
+			where ifnull(master_type, '') = 'Customer' and docstatus < 2 
+			and company = %s""", filters["company"])
+	
+	if customer_accounts:
+		conditions += " and jvd.account in (%s)" % (", ".join(['%s']*len(customer_accounts)))
+		
+	if filters.get("from_date"): conditions += " and jv.posting_date >= '%s'" % filters["from_date"]
+	if filters.get("to_date"): conditions += " and jv.posting_date <= '%s'" % filters["to_date"]
+	
+	return conditions, customer_accounts
+	
+def get_entries(filters):
+	conditions, customer_accounts = get_conditions(filters)
+	entries =  webnotes.conn.sql("""select jv.name, jvd.account, jv.posting_date, 
+		jvd.against_invoice, jvd.debit, jvd.credit, jv.cheque_no, jv.cheque_date, jv.remark
+		from `tabJournal Voucher Detail` jvd, `tabJournal Voucher` jv 
+		where jvd.parent = jv.name and jv.docstatus=1 %s order by jv.name DESC""" % 
+		(conditions), tuple(customer_accounts), as_dict=1)
+		
+	return entries
+	
+def get_si_posting_date_map():
+	si_posting_date_map = {}
+	for t in webnotes.conn.sql("""select name, posting_date from `tabSales Invoice`"""):
+		si_posting_date_map[t[0]] = t[1]
+		
+	return si_posting_date_map
\ No newline at end of file
diff --git a/accounts/report/collection_report/collection_report.txt b/accounts/report/collection_report/collection_report.txt
new file mode 100644
index 0000000..3933dee
--- /dev/null
+++ b/accounts/report/collection_report/collection_report.txt
@@ -0,0 +1,22 @@
+[
+ {
+  "creation": "2013-05-01 12:29:12", 
+  "docstatus": 0, 
+  "modified": "2013-05-01 12:29:12", 
+  "modified_by": "Administrator", 
+  "owner": "Administrator"
+ }, 
+ {
+  "add_total_row": 1, 
+  "doctype": "Report", 
+  "is_standard": "Yes", 
+  "name": "__common__", 
+  "ref_doctype": "Journal Voucher", 
+  "report_name": "Collection Report", 
+  "report_type": "Script Report"
+ }, 
+ {
+  "doctype": "Report", 
+  "name": "Collection Report"
+ }
+]
\ No newline at end of file
diff --git a/accounts/report/purchase_register/purchase_register.py b/accounts/report/purchase_register/purchase_register.py
index 01dc937..c131c17 100644
--- a/accounts/report/purchase_register/purchase_register.py
+++ b/accounts/report/purchase_register/purchase_register.py
@@ -141,7 +141,6 @@
 	accounts = list(set([inv.credit_to for inv in invoice_list]))
 	for acc in webnotes.conn.sql("""select name, parent_account from tabAccount 
 		where name in (%s)""" % ", ".join(["%s"]*len(accounts)), tuple(accounts), as_dict=1):
-			account_map.setdefault(acc.name, "")
 			account_map[acc.name] = acc.parent_account
 						
 	return account_map	
\ No newline at end of file
diff --git a/accounts/report/sales_register/sales_register.py b/accounts/report/sales_register/sales_register.py
index a0020c4..23d2227 100644
--- a/accounts/report/sales_register/sales_register.py
+++ b/accounts/report/sales_register/sales_register.py
@@ -145,7 +145,6 @@
 	customers = list(set([inv.customer for inv in invoice_list]))
 	for cust in webnotes.conn.sql("""select name, territory from `tabCustomer` 
 		where name in (%s)""" % ", ".join(["%s"]*len(customers)), tuple(customers), as_dict=1):
-			customer_map.setdefault(cust.name, "")
 			customer_map[cust.name] = cust.territory
 	
 	return customer_map
@@ -155,7 +154,6 @@
 	accounts = list(set([inv.debit_to for inv in invoice_list]))
 	for acc in webnotes.conn.sql("""select name, parent_account from tabAccount 
 		where name in (%s)""" % ", ".join(["%s"]*len(accounts)), tuple(accounts), as_dict=1):
-			account_map.setdefault(acc.name, "")
 			account_map[acc.name] = acc.parent_account
 						
 	return account_map
\ No newline at end of file