Merge branch 'master' of github.com:webnotes/erpnext
diff --git a/accounts/page/accounts_home/accounts_home.js b/accounts/page/accounts_home/accounts_home.js
index 2703d27..6a9f91a 100644
--- a/accounts/page/accounts_home/accounts_home.js
+++ b/accounts/page/accounts_home/accounts_home.js
@@ -154,6 +154,11 @@
 				route: "query-report/Sales Register",
 				doctype: "Sales Invoice"
 			},
+			{
+				"label":wn._("Purchase Register"),
+				route: "query-report/Purchase Register",
+				doctype: "Purchase Invoice"
+			},
 		]
 	},
 	{
diff --git a/accounts/report/accounts_payable/accounts_payable.py b/accounts/report/accounts_payable/accounts_payable.py
index 34c8ccd..a10dc7e 100644
--- a/accounts/report/accounts_payable/accounts_payable.py
+++ b/accounts/report/accounts_payable/accounts_payable.py
@@ -19,7 +19,6 @@
 		and nowdate() or filters.get("report_date")
 
 	data = []
-	total_invoiced_amount = total_paid = total_outstanding = 0
 	for gle in entries:
 		if cstr(gle.against_voucher) == gle.voucher_no or not gle.against_voucher \
 				or [gle.against_voucher_type, gle.against_voucher] in entries_after_report_date:
@@ -36,7 +35,7 @@
 			paid_amount = get_paid_amount(gle, filters.get("report_date") or nowdate(), 
 				entries_after_report_date)
 			outstanding_amount = invoiced_amount - paid_amount
-		
+
 			if abs(flt(outstanding_amount)) > 0.01:
 				row = [gle.posting_date, gle.account, gle.voucher_type, gle.voucher_no, 
 					gle.remarks, account_supplier_type_map.get(gle.account), due_date, bill_no, 
@@ -47,16 +46,9 @@
 					ageing_based_on_date = due_date
 				else:
 					ageing_based_on_date = gle.posting_date
+					
 				row += get_ageing_data(ageing_based_on_date, age_on, outstanding_amount)
-				
-				# Add to total
-				total_invoiced_amount += flt(invoiced_amount)
-				total_paid += flt(paid_amount)
-				total_outstanding += flt(outstanding_amount)
 				data.append(row)
-	if data:
-		data.append(["", "", "", "", "", "", "", "Total", "", total_invoiced_amount, total_paid, 
-			total_outstanding, "", "", "", ""])
 				
 	return columns, data
 	
diff --git a/accounts/report/accounts_payable/accounts_payable.txt b/accounts/report/accounts_payable/accounts_payable.txt
index 8920a0b..6de97f6 100644
--- a/accounts/report/accounts_payable/accounts_payable.txt
+++ b/accounts/report/accounts_payable/accounts_payable.txt
@@ -2,11 +2,12 @@
  {
   "creation": "2013-04-22 16:16:03", 
   "docstatus": 0, 
-  "modified": "2013-04-23 14:54:27", 
+  "modified": "2013-04-30 17:55:54", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
  {
+  "add_total_row": 1, 
   "doctype": "Report", 
   "is_standard": "Yes", 
   "name": "__common__", 
diff --git a/accounts/report/accounts_receivable/accounts_receivable.py b/accounts/report/accounts_receivable/accounts_receivable.py
index d385b36..a8c6d23 100644
--- a/accounts/report/accounts_receivable/accounts_receivable.py
+++ b/accounts/report/accounts_receivable/accounts_receivable.py
@@ -18,7 +18,6 @@
 		and nowdate() or filters.get("report_date")
 
 	data = []
-	total_invoiced_amount = total_payment = total_outstanding = 0
 	for gle in entries:
 		if cstr(gle.against_voucher) == gle.voucher_no or not gle.against_voucher \
 				or [gle.against_voucher_type, gle.against_voucher] in entries_after_report_date:
@@ -41,17 +40,9 @@
 				else:
 					ageing_based_on_date = gle.posting_date
 				row += get_ageing_data(ageing_based_on_date, age_on, outstanding_amount)
-				
-				# Add to total
-				total_invoiced_amount += flt(invoiced_amount)
-				total_payment += flt(payment_amount)
-				total_outstanding += flt(outstanding_amount)
-				
+								
 				data.append(row)
-	if data:
-		data.append(["", "", "", "", "", "", "Total", total_invoiced_amount, total_payment, 
-			total_outstanding, "", "", "", ""])
-				
+								
 	return columns, data
 	
 def get_columns():
diff --git a/accounts/report/accounts_receivable/accounts_receivable.txt b/accounts/report/accounts_receivable/accounts_receivable.txt
index 28f7e4f..7eb344f 100644
--- a/accounts/report/accounts_receivable/accounts_receivable.txt
+++ b/accounts/report/accounts_receivable/accounts_receivable.txt
@@ -2,11 +2,12 @@
  {
   "creation": "2013-04-16 11:31:13", 
   "docstatus": 0, 
-  "modified": "2013-04-16 11:31:13", 
+  "modified": "2013-04-30 17:54:47", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
  {
+  "add_total_row": 1, 
   "doctype": "Report", 
   "is_standard": "Yes", 
   "name": "__common__", 
diff --git a/accounts/report/purchase_register/__init__.py b/accounts/report/purchase_register/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/accounts/report/purchase_register/__init__.py
diff --git a/accounts/report/purchase_register/purchase_register.js b/accounts/report/purchase_register/purchase_register.js
new file mode 100644
index 0000000..21e0547
--- /dev/null
+++ b/accounts/report/purchase_register/purchase_register.js
@@ -0,0 +1,42 @@
+wn.query_reports["Purchase Register"] = {
+	"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": "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": "Credit",
+						"company": company,
+						"master_type": "Supplier"
+					}
+				}
+			}
+		},
+		{
+			"fieldname":"company",
+			"label": "Company",
+			"fieldtype": "Link",
+			"options": "Company",
+			"default": sys_defaults.company
+		}
+	]
+}
\ No newline at end of file
diff --git a/accounts/report/purchase_register/purchase_register.py b/accounts/report/purchase_register/purchase_register.py
new file mode 100644
index 0000000..01dc937
--- /dev/null
+++ b/accounts/report/purchase_register/purchase_register.py
@@ -0,0 +1,147 @@
+# 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 webnotes.utils import flt
+
+def execute(filters=None):
+	if not filters: filters = {}
+	columns, expense_accounts, tax_accounts = get_columns()
+	
+	invoice_list = get_invoices(filters)
+	invoice_expense_map = get_invoice_expense_map(invoice_list)
+	invoice_tax_map = get_invoice_tax_map(invoice_list)
+	invoice_po_pr_map = get_invoice_po_pr_map(invoice_list)
+	account_map = get_account_details(invoice_list)
+
+	data = []
+	for inv in invoice_list:
+		# invoice details
+		purchase_order = ", ".join(invoice_po_pr_map.get(inv.name, {}).get("purchase_order", []))
+		purchase_receipt = ", ".join(invoice_po_pr_map.get(inv.name, {}).get("purchase_receipt", []))
+		row = [inv.name, inv.posting_date, inv.supplier, inv.credit_to, 
+			account_map.get(inv.credit_to), inv.project_name, inv.bill_no, inv.bill_date, 
+			inv.remarks, purchase_order, purchase_receipt]
+		
+		# map expense values
+		for expense_acc in expense_accounts:
+			row.append(invoice_expense_map.get(inv.name, {}).get(expense_acc))
+		
+		# net total
+		row.append(inv.net_total)
+			
+		# tax account
+		for tax_acc in tax_accounts:
+			row.append(invoice_tax_map.get(inv.name, {}).get(tax_acc))
+
+		# total tax, grand total
+		row += [inv.total_tax, inv.grand_total]
+		data.append(row)
+	
+	return columns, data
+	
+	
+def get_columns():
+	"""return columns based on filters"""
+	columns = [
+		"Invoice:Link/Purchase Invoice:120", "Posting Date:Date:80", "Supplier:Link/Supplier:120", 
+		"Supplier Account:Link/Account:120", "Account Group:LInk/Account:120", 
+		"Project:Link/Project:80", "Bill No::120", "Bill Date:Date:80", "Remarks::150", 
+		"Purchase Order:Link/Purchase Order:100", "Purchase Receipt:Link/Purchase Receipt:100"
+	]
+	
+	expense_accounts = webnotes.conn.sql_list("""select distinct expense_head 
+		from `tabPurchase Invoice Item` where docstatus = 1 and ifnull(expense_head, '') != '' 
+		order by expense_head""")
+	tax_accounts = 	webnotes.conn.sql_list("""select distinct account_head 
+		from `tabPurchase Taxes and Charges` where parenttype = 'Purchase Invoice' 
+		and docstatus = 1 and ifnull(account_head, '') != '' order by account_head""")
+	
+	columns = columns + [(account + ":Currency:120") for account in expense_accounts] + \
+		["Net Total:Currency:120"] + [(account + ":Currency:120") for account in tax_accounts] + \
+		["Total Tax:Currency:120"] + ["Grand Total:Currency:120"]
+
+	return columns, expense_accounts, tax_accounts
+
+def get_conditions(filters):
+	conditions = ""
+	
+	if filters.get("company"): conditions += " and company=%(company)s"
+	if filters.get("account"): conditions += " and account = %(account)s"
+
+	if filters.get("from_date"): conditions += " and posting_date>=%(from_date)s"
+	if filters.get("to_date"): conditions += " and posting_date<=%(to_date)s"
+
+	return conditions
+	
+def get_invoices(filters):
+	conditions = get_conditions(filters)
+	return webnotes.conn.sql("""select name, posting_date, credit_to, project_name, supplier, 
+		bill_no, bill_date, remarks, net_total, total_tax, grand_total 
+		from `tabPurchase Invoice` where docstatus = 1 %s 
+		order by posting_date desc, name desc""" % conditions, filters, as_dict=1)
+	
+def get_invoice_expense_map(invoice_list):
+	expense_details = webnotes.conn.sql("""select parent, expense_head, sum(amount) as amount
+		from `tabPurchase Invoice Item` where parent in (%s) group by parent, expense_head""" % 
+		', '.join(['%s']*len(invoice_list)), tuple([inv.name for inv in invoice_list]), as_dict=1)
+	
+	invoice_expense_map = {}
+	for d in expense_details:
+		invoice_expense_map.setdefault(d.parent, webnotes._dict()).setdefault(d.expense_head, [])
+		invoice_expense_map[d.parent][d.expense_head] = flt(d.amount)
+	
+	return invoice_expense_map
+	
+def get_invoice_tax_map(invoice_list):
+	tax_details = webnotes.conn.sql("""select parent, account_head, sum(tax_amount) as tax_amount
+		from `tabPurchase Taxes and Charges` where parent in (%s) group by parent, account_head""" % 
+		', '.join(['%s']*len(invoice_list)), tuple([inv.name for inv in invoice_list]), as_dict=1)
+	
+	invoice_tax_map = {}
+	for d in tax_details:
+		invoice_tax_map.setdefault(d.parent, webnotes._dict()).setdefault(d.account_head, [])
+		invoice_tax_map[d.parent][d.account_head] = flt(d.tax_amount)
+	
+	return invoice_tax_map
+	
+def get_invoice_po_pr_map(invoice_list):
+	pi_items = webnotes.conn.sql("""select parent, purchase_order, purchase_receipt
+		from `tabPurchase Invoice Item` where parent in (%s) 
+		and (ifnull(purchase_order, '') != '' or ifnull(purchase_receipt, '') != '')""" % 
+		', '.join(['%s']*len(invoice_list)), tuple([inv.name for inv in invoice_list]), as_dict=1)
+	
+	invoice_po_pr_map = {}
+	for d in pi_items:
+		if d.purchase_order:
+			invoice_po_pr_map.setdefault(d.parent, webnotes._dict()).setdefault(
+				"purchase_order", []).append(d.purchase_order)
+		if d.purchase_receipt:
+			invoice_po_pr_map.setdefault(d.parent, webnotes._dict()).setdefault(
+				"purchase_receipt", []).append(d.purchase_receipt)
+				
+	return invoice_po_pr_map
+	
+def get_account_details(invoice_list):
+	account_map = {}
+	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/purchase_register/purchase_register.txt b/accounts/report/purchase_register/purchase_register.txt
new file mode 100644
index 0000000..847f5f3
--- /dev/null
+++ b/accounts/report/purchase_register/purchase_register.txt
@@ -0,0 +1,22 @@
+[
+ {
+  "creation": "2013-04-29 16:13:11", 
+  "docstatus": 0, 
+  "modified": "2013-04-30 17:51:19", 
+  "modified_by": "Administrator", 
+  "owner": "Administrator"
+ }, 
+ {
+  "add_total_row": 1, 
+  "doctype": "Report", 
+  "is_standard": "Yes", 
+  "name": "__common__", 
+  "ref_doctype": "Purchase Invoice", 
+  "report_name": "Purchase Register", 
+  "report_type": "Script Report"
+ }, 
+ {
+  "doctype": "Report", 
+  "name": "Purchase Register"
+ }
+]
\ 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 07772cb..a0020c4 100644
--- a/accounts/report/sales_register/sales_register.py
+++ b/accounts/report/sales_register/sales_register.py
@@ -94,8 +94,8 @@
 	
 def get_invoices(filters):
 	conditions = get_conditions(filters)
-	return webnotes.conn.sql("""select name, posting_date, territory, debit_to, territory, 
-		project_name, customer, remarks, net_total, other_charges_total, grand_total 
+	return webnotes.conn.sql("""select name, posting_date, debit_to, project_name, customer, 
+		remarks, net_total, other_charges_total, grand_total 
 		from `tabSales Invoice` where docstatus = 1 %s 
 		order by posting_date desc, name desc""" % conditions, filters, as_dict=1)
 	
diff --git a/accounts/report/sales_register/sales_register.txt b/accounts/report/sales_register/sales_register.txt
index 5aef814..dbf41af 100644
--- a/accounts/report/sales_register/sales_register.txt
+++ b/accounts/report/sales_register/sales_register.txt
@@ -2,11 +2,12 @@
  {
   "creation": "2013-04-23 18:15:29", 
   "docstatus": 0, 
-  "modified": "2013-04-23 18:15:29", 
+  "modified": "2013-04-30 17:53:10", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
  {
+  "add_total_row": 1, 
   "doctype": "Report", 
   "is_standard": "Yes", 
   "name": "__common__", 
diff --git a/accounts/search_criteria/bank_clearance_report/bank_clearance_report.txt b/accounts/search_criteria/bank_clearance_report/bank_clearance_report.txt
index 0038438..8aa94bf 100644
--- a/accounts/search_criteria/bank_clearance_report/bank_clearance_report.txt
+++ b/accounts/search_criteria/bank_clearance_report/bank_clearance_report.txt
@@ -1,29 +1,32 @@
 [
  {
-  "owner": "Administrator", 
+  "creation": "2012-05-14 18:05:41", 
   "docstatus": 0, 
-  "creation": "2012-04-03 12:49:50", 
+  "modified": "2013-04-30 14:49:06", 
   "modified_by": "Administrator", 
-  "modified": "2012-04-03 12:49:50"
+  "owner": "Administrator"
  }, 
  {
-  "description": "Bank Clearance report", 
-  "parent_doc_type": "Journal Voucher", 
-  "module": "Accounts", 
-  "standard": "Yes", 
-  "sort_order": "DESC", 
-  "filters": "{'Journal Voucher\u0001Submitted':1,'Journal Voucher\u0001Voucher Type':'','Journal Voucher\u0001Is Opening':'','Journal Voucher\u0001Fiscal Year':'','Journal Voucher\u0001Company':'','Journal Voucher\u0001TDS Applicable':'','Journal Voucher\u0001TDS Category':''}", 
-  "dis_filters": "fiscal_year", 
-  "doc_type": "Journal Voucher Detail", 
-  "name": "__common__", 
-  "doctype": "Search Criteria", 
-  "sort_by": "ID", 
-  "page_len": 50, 
+  "columns": "Journal Voucher\u0001ID,Journal Voucher Detail\u0001Account,Journal Voucher Detail\u0001Debit,Journal Voucher Detail\u0001Credit,Journal Voucher\u0001Clearance Date,Journal Voucher\u0001Cheque No,Journal Voucher\u0001Cheque Date,Journal Voucher\u0001Voucher Date,Journal Voucher\u0001Posting Date,Journal Voucher Detail\u0001Against Payable,Journal Voucher Detail\u0001Against Receivable", 
   "criteria_name": "Bank Clearance report", 
-  "columns": "Journal Voucher\u0001ID,Journal Voucher Detail\u0001Account,Journal Voucher Detail\u0001Debit,Journal Voucher Detail\u0001Credit,Journal Voucher\u0001Clearance Date,Journal Voucher\u0001Cheque No,Journal Voucher\u0001Cheque Date,Journal Voucher\u0001Voucher Date,Journal Voucher\u0001Posting Date,Journal Voucher Detail\u0001Against Payable,Journal Voucher Detail\u0001Against Receivable"
+  "custom_query": "", 
+  "description": "Bank Clearance report", 
+  "dis_filters": "fiscal_year", 
+  "disabled": 0, 
+  "doc_type": "Journal Voucher Detail", 
+  "doctype": "Search Criteria", 
+  "filters": "{'Journal Voucher\u0001Submitted':1,'Journal Voucher\u0001Voucher Type':'','Journal Voucher\u0001Is Opening':'','Journal Voucher\u0001Fiscal Year':'','Journal Voucher\u0001Company':'','Journal Voucher\u0001TDS Applicable':'','Journal Voucher\u0001TDS Category':''}", 
+  "module": "Accounts", 
+  "name": "__common__", 
+  "page_len": 50, 
+  "parent_doc_type": "Journal Voucher", 
+  "report_script": null, 
+  "sort_by": "ID", 
+  "sort_order": "DESC", 
+  "standard": "Yes"
  }, 
  {
-  "name": "bank_clearance_report", 
-  "doctype": "Search Criteria"
+  "doctype": "Search Criteria", 
+  "name": "bank_clearance_report"
  }
 ]
\ No newline at end of file