Merge branch 'master' into slow
diff --git a/accounts/report/accounts_payable/accounts_payable.py b/accounts/report/accounts_payable/accounts_payable.py
index 71aeb35..6bc98f6 100644
--- a/accounts/report/accounts_payable/accounts_payable.py
+++ b/accounts/report/accounts_payable/accounts_payable.py
@@ -24,7 +24,6 @@
 	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:
-			
 			if gle.voucher_type == "Purchase Invoice":
 				pi_info = pi_map.get(gle.voucher_no)
 				due_date = pi_info.get("due_date")
@@ -33,12 +32,12 @@
 			else:
 				due_date = bill_no = bill_date = ""
 		
-			invoiced_amount = gle.credit > 0 and gle.credit or 0		
-			paid_amount = get_paid_amount(gle, filters.get("report_date") or nowdate(), 
-				entries_after_report_date)
-			outstanding_amount = invoiced_amount - paid_amount
+			invoiced_amount = gle.credit > 0 and gle.credit or 0
+			outstanding_amount = get_outstanding_amount(gle, 
+				filters.get("report_date") or nowdate())
 
 			if abs(flt(outstanding_amount)) > 0.01:
+				paid_amount = invoiced_amount - outstanding_amount
 				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, 
 					bill_date, invoiced_amount, paid_amount, outstanding_amount]
@@ -116,17 +115,13 @@
 		
 	return pi_map
 
-def get_paid_amount(gle, report_date, entries_after_report_date):
-
-	paid_amount = 0
-	if flt(gle.debit) > 0 and (not gle.against_voucher or 
-		[gle.against_voucher_type, gle.against_voucher] in entries_after_report_date):
-			paid_amount = gle.debit
-	elif flt(gle.credit) > 0:
-		paid_amount = webnotes.conn.sql("""
-			select sum(ifnull(debit, 0)) - sum(ifnull(credit, 0)) from `tabGL Entry` 
-			where account = %s and posting_date <= %s and against_voucher_type = %s 
-			and against_voucher = %s and name != %s and ifnull(is_cancelled, 'No') = 'No'""", 
-			(gle.account, report_date, gle.voucher_type, gle.voucher_no, gle.name))[0][0]
-	
-	return flt(paid_amount)
\ No newline at end of file
+def get_outstanding_amount(gle, report_date):
+	payment_amount = webnotes.conn.sql("""
+		select sum(ifnull(debit, 0)) - sum(ifnull(credit, 0)) 
+		from `tabGL Entry` 
+		where account = %s and posting_date <= %s and against_voucher_type = %s 
+		and against_voucher = %s and name != %s and ifnull(is_cancelled, 'No') = 'No'""", 
+		(gle.account, report_date, gle.voucher_type, gle.voucher_no, gle.name))[0][0]
+		
+	outstanding_amount = flt(gle.credit) - flt(gle.debit) - flt(payment_amount)
+	return outstanding_amount
\ No newline at end of file
diff --git a/accounts/report/accounts_receivable/accounts_receivable.py b/accounts/report/accounts_receivable/accounts_receivable.py
index d791fad..2bd3be8 100644
--- a/accounts/report/accounts_receivable/accounts_receivable.py
+++ b/accounts/report/accounts_receivable/accounts_receivable.py
@@ -27,11 +27,11 @@
 				and si_due_date_map.get(gle.voucher_no) or ""
 		
 			invoiced_amount = gle.debit > 0 and gle.debit or 0		
-			payment_amount = get_payment_amount(gle, filters.get("report_date") or nowdate(), 
-				entries_after_report_date)
-			outstanding_amount = invoiced_amount - payment_amount
+			outstanding_amount = get_outstanding_amount(gle, 
+				filters.get("report_date") or nowdate())
 		
 			if abs(flt(outstanding_amount)) > 0.01:
+				payment_amount = invoiced_amount - outstanding_amount
 				row = [gle.posting_date, gle.account, gle.voucher_type, gle.voucher_no, 
 					gle.remarks, due_date, account_territory_map.get(gle.account), 
 					invoiced_amount, payment_amount, outstanding_amount]
@@ -104,6 +104,16 @@
 		
 	return si_due_date_map
 
+def get_outstanding_amount(gle, report_date):
+	payment_amount = webnotes.conn.sql("""
+		select sum(ifnull(credit, 0)) - sum(ifnull(debit, 0)) 
+		from `tabGL Entry` 
+		where account = %s and posting_date <= %s and against_voucher_type = %s 
+		and against_voucher = %s and name != %s and ifnull(is_cancelled, 'No') = 'No'""", 
+		(gle.account, report_date, gle.voucher_type, gle.voucher_no, gle.name))[0][0]
+		
+	return flt(gle.debit) - flt(gle.credit) - flt(payment_amount)
+
 def get_payment_amount(gle, report_date, entries_after_report_date):
 	payment_amount = 0
 	if flt(gle.credit) > 0 and (not gle.against_voucher or 
diff --git a/accounts/report/purchase_register/purchase_register.py b/accounts/report/purchase_register/purchase_register.py
index 705a654..0970541 100644
--- a/accounts/report/purchase_register/purchase_register.py
+++ b/accounts/report/purchase_register/purchase_register.py
@@ -23,7 +23,7 @@
 	if not filters: filters = {}
 
 	invoice_list = get_invoices(filters)
-	columns, expense_accounts, tax_accounts, renamed_columns = get_columns(invoice_list)
+	columns, expense_accounts, tax_accounts = get_columns(invoice_list)
 	
 	
 	if not invoice_list:
@@ -31,7 +31,7 @@
 		return columns, invoice_list
 	
 	invoice_expense_map = get_invoice_expense_map(invoice_list)
-	invoice_tax_map = get_invoice_tax_map(invoice_list, renamed_columns)
+	invoice_expense_map, invoice_tax_map = get_invoice_tax_map(invoice_list, invoice_expense_map)
 	invoice_po_pr_map = get_invoice_po_pr_map(invoice_list)
 	account_map = get_account_details(invoice_list)
 
@@ -47,20 +47,27 @@
 			inv.remarks, ", ".join(purchase_order), ", ".join(purchase_receipt)]
 		
 		# map expense values
+		net_total = 0
 		for expense_acc in expense_accounts:
-			row.append(invoice_expense_map.get(inv.name, {}).get(expense_acc))
+			expense_amount = flt(invoice_expense_map.get(inv.name, {}).get(expense_acc))
+			net_total += expense_amount
+			row.append(expense_amount)
 		
 		# net total
-		row.append(inv.net_total)
+		row.append(net_total)
 			
 		# tax account
+		total_tax = 0
 		for tax_acc in tax_accounts:
-			row.append(invoice_tax_map.get(inv.name, {}).get(tax_acc))
+			if tax_acc not in expense_accounts:
+				tax_amount = flt(invoice_tax_map.get(inv.name, {}).get(tax_acc))
+				total_tax += tax_amount
+				row.append(tax_amount)
 
 		# total tax, grand total, outstanding amount & rounded total
-		row += [inv.total_tax, inv.grand_total, flt(inv.grand_total, 2), \
-			inv.outstanding_amount]
+		row += [total_tax, inv.grand_total, flt(inv.grand_total, 2), inv.outstanding_amount]
 		data.append(row)
+		# raise Exception
 	
 	return columns, data
 	
@@ -74,7 +81,6 @@
 		"Purchase Order:Link/Purchase Order:100", "Purchase Receipt:Link/Purchase Receipt:100"
 	]
 	expense_accounts = tax_accounts = expense_columns = tax_columns = []
-	renamed_columns = {}
 	
 	if invoice_list:	
 		expense_accounts = webnotes.conn.sql_list("""select distinct expense_head 
@@ -91,11 +97,7 @@
 				
 	expense_columns = [(account + ":Currency:120") for account in expense_accounts]
 	for account in tax_accounts:
-		if account in expense_accounts:
-			new_account = account + " (Tax)"
-			renamed_columns[account] = new_account
-			tax_columns.append(new_account + ":Currency:120")
-		else:
+		if account not in expense_accounts:
 			tax_columns.append(account + ":Currency:120")
 	
 	columns = columns + expense_columns + \
@@ -103,7 +105,7 @@
 		["Total Tax:Currency:120"] + ["Grand Total:Currency:120"] + \
 		["Rounded Total:Currency:120"] + ["Outstanding Amount:Currency:120"]
 
-	return columns, expense_accounts, tax_accounts, renamed_columns
+	return columns, expense_accounts, tax_accounts
 
 def get_conditions(filters):
 	conditions = ""
@@ -119,7 +121,7 @@
 def get_invoices(filters):
 	conditions = get_conditions(filters)
 	return webnotes.conn.sql("""select name, posting_date, credit_to, supplier, supplier_name, 
-		bill_no, bill_date, remarks, net_total, total_tax, grand_total, outstanding_amount 
+		bill_no, bill_date, remarks, grand_total, outstanding_amount 
 		from `tabPurchase Invoice` where docstatus = 1 %s 
 		order by posting_date desc, name desc""" % conditions, filters, as_dict=1)
 	
@@ -136,18 +138,20 @@
 	
 	return invoice_expense_map
 	
-def get_invoice_tax_map(invoice_list, renamed_columns):
+def get_invoice_tax_map(invoice_list, invoice_expense_map):
 	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:
-		account = renamed_columns.get(d.account_head) or d.account_head
-		invoice_tax_map.setdefault(d.parent, webnotes._dict()).setdefault(account, [])
-		invoice_tax_map[d.parent][account] = flt(d.tax_amount)
+		if d.account_head in invoice_expense_map.get(d.parent):
+			invoice_expense_map[d.parent][d.account_head] += flt(d.tax_amount)
+		else:
+			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
+	return invoice_expense_map, invoice_tax_map
 	
 def get_invoice_po_pr_map(invoice_list):
 	pi_items = webnotes.conn.sql("""select parent, purchase_order, purchase_receipt, 
diff --git a/accounts/report/sales_register/sales_register.py b/accounts/report/sales_register/sales_register.py
index 2bc2850..91ad1c2 100644
--- a/accounts/report/sales_register/sales_register.py
+++ b/accounts/report/sales_register/sales_register.py
@@ -23,14 +23,14 @@
 	if not filters: filters = {}
 	
 	invoice_list = get_invoices(filters)
-	columns, income_accounts, tax_accounts, renamed_columns = get_columns(invoice_list)
+	columns, income_accounts, tax_accounts = get_columns(invoice_list)
 	
 	if not invoice_list:
-		msgprint(_("No record found"))		
+		msgprint(_("No record found"))
 		return columns, invoice_list
 	
 	invoice_income_map = get_invoice_income_map(invoice_list)
-	invoice_tax_map = get_invoice_tax_map(invoice_list, renamed_columns)
+	invoice_income_map, invoice_tax_map = get_invoice_tax_map(invoice_list, invoice_income_map)
 	
 	invoice_so_dn_map = get_invoice_so_dn_map(invoice_list)
 	customer_map = get_customer_deatils(invoice_list)
@@ -47,18 +47,25 @@
 			inv.remarks, ", ".join(sales_order), ", ".join(delivery_note)]
 		
 		# map income values
+		net_total = 0
 		for income_acc in income_accounts:
-			row.append(invoice_income_map.get(inv.name, {}).get(income_acc))
+			income_amount = flt(invoice_income_map.get(inv.name, {}).get(income_acc))
+			net_total += income_amount
+			row.append(income_amount)
 		
 		# net total
-		row.append(inv.net_total)
+		row.append(net_total)
 			
 		# tax account
+		total_tax = 0
 		for tax_acc in tax_accounts:
-			row.append(invoice_tax_map.get(inv.name, {}).get(tax_acc))
+			if tax_acc not in income_accounts:
+				tax_amount = flt(invoice_tax_map.get(inv.name, {}).get(tax_acc))
+				total_tax += tax_amount
+				row.append(tax_amount)
 
 		# total tax, grand total, outstanding amount & rounded total
-		row += [inv.other_charges_total, inv.grand_total, inv.rounded_total, inv.outstanding_amount]
+		row += [total_tax, inv.grand_total, inv.rounded_total, inv.outstanding_amount]
 
 		data.append(row)
 	
@@ -75,7 +82,6 @@
 	]
 	
 	income_accounts = tax_accounts = income_columns = tax_columns = []
-	renamed_columns = {}
 	
 	if invoice_list:
 		income_accounts = webnotes.conn.sql_list("""select distinct income_account 
@@ -85,24 +91,20 @@
 	
 		tax_accounts = 	webnotes.conn.sql_list("""select distinct account_head 
 			from `tabSales Taxes and Charges` where parenttype = 'Sales Invoice' 
-			and docstatus = 1 and ifnull(tax_amount, 0) > 0 
+			and docstatus = 1 and ifnull(tax_amount, 0) != 0 
 			and parent in (%s) order by account_head""" % 
 			', '.join(['%s']*len(invoice_list)), tuple([inv.name for inv in invoice_list]))
-			
+
 	income_columns = [(account + ":Currency:120") for account in income_accounts]
 	for account in tax_accounts:
-		if account in income_accounts:
-			new_account = account + " (Tax)"
-			renamed_columns[account] = new_account
-			tax_columns.append(new_account + ":Currency:120")
-		else:
+		if account not in income_accounts:
 			tax_columns.append(account + ":Currency:120")
 	
 	columns = columns + income_columns + ["Net Total:Currency:120"] + tax_columns + \
 		["Total Tax:Currency:120"] + ["Grand Total:Currency:120"] + \
 		["Rounded Total:Currency:120"] + ["Outstanding Amount:Currency:120"]
 
-	return columns, income_accounts, tax_accounts, renamed_columns
+	return columns, income_accounts, tax_accounts
 
 def get_conditions(filters):
 	conditions = ""
@@ -118,8 +120,8 @@
 def get_invoices(filters):
 	conditions = get_conditions(filters)
 	return webnotes.conn.sql("""select name, posting_date, debit_to, project_name, customer, 
-		customer_name, remarks, net_total, other_charges_total, grand_total, rounded_total, 
-		outstanding_amount from `tabSales Invoice` 
+		customer_name, remarks, grand_total, rounded_total, outstanding_amount 
+		from `tabSales Invoice` 
 		where docstatus = 1 %s order by posting_date desc, name desc""" % 
 		conditions, filters, as_dict=1)
 	
@@ -135,18 +137,20 @@
 	
 	return invoice_income_map
 	
-def get_invoice_tax_map(invoice_list, renamed_columns):
+def get_invoice_tax_map(invoice_list, invoice_income_map):
 	tax_details = webnotes.conn.sql("""select parent, account_head, sum(tax_amount) as tax_amount
 		from `tabSales 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:
-		account = renamed_columns.get(d.account_head) or d.account_head
-		invoice_tax_map.setdefault(d.parent, webnotes._dict()).setdefault(account, [])
-		invoice_tax_map[d.parent][account] = flt(d.tax_amount)
+		if d.account_head in invoice_income_map.get(d.parent):
+			invoice_income_map[d.parent][d.account_head] += flt(d.tax_amount)
+		else:
+			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
+	return invoice_income_map, invoice_tax_map
 	
 def get_invoice_so_dn_map(invoice_list):
 	si_items = webnotes.conn.sql("""select parent, sales_order, delivery_note
diff --git a/stock/report/purchase_order_items_to_be_received/purchase_order_items_to_be_received.txt b/stock/report/purchase_order_items_to_be_received/purchase_order_items_to_be_received.txt
index c588dde..98dcd22 100644
--- a/stock/report/purchase_order_items_to_be_received/purchase_order_items_to_be_received.txt
+++ b/stock/report/purchase_order_items_to_be_received/purchase_order_items_to_be_received.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-02-22 18:01:55", 
   "docstatus": 0, 
-  "modified": "2013-05-28 16:03:15", 
+  "modified": "2013-07-16 13:19:24", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -11,7 +11,7 @@
   "doctype": "Report", 
   "is_standard": "Yes", 
   "name": "__common__", 
-  "query": "select \n    `tabPurchase Order`.`name` as \"Purchase Order:Link/Purchase Order:120\",\n\t`tabPurchase Order`.`transaction_date` as \"Date:Date:100\",\n\t`tabPurchase Order`.`supplier` as \"Supplier:Link/Supplier:120\",\n\t`tabPurchase Order Item`.`project_name` as \"Project\",\n\t`tabPurchase Order Item`.item_code as \"Item Code:Link/Item:120\",\n\t`tabPurchase Order Item`.qty as \"Qty:Float:100\",\n\t`tabPurchase Order Item`.received_qty as \"Received Qty:Float:100\", \n\t(`tabPurchase Order Item`.qty - ifnull(`tabPurchase Order Item`.received_qty, 0)) as \"Qty to Receive:Float:100\",\n\t`tabPurchase Order Item`.item_name as \"Item Name::150\",\n\t`tabPurchase Order Item`.description as \"Description::200\"\nfrom\n\t`tabPurchase Order`, `tabPurchase Order Item`\nwhere\n\t`tabPurchase Order Item`.`parent` = `tabPurchase Order`.`name`\n\tand `tabPurchase Order`.docstatus = 1\n\tand `tabPurchase Order`.status != \"Stopped\"\n\tand ifnull(`tabPurchase Order Item`.received_qty, 0) < ifnull(`tabPurchase Order Item`.qty, 0)\norder by `tabPurchase Order`.transaction_date asc", 
+  "query": "select \n    `tabPurchase Order`.`name` as \"Purchase Order:Link/Purchase Order:120\",\n\t`tabPurchase Order`.`transaction_date` as \"Date:Date:100\",\n\t`tabPurchase Order`.`supplier` as \"Supplier:Link/Supplier:120\",\n\t`tabPurchase Order Item`.`project_name` as \"Project\",\n\t`tabPurchase Order Item`.item_code as \"Item Code:Link/Item:120\",\n\t`tabPurchase Order Item`.qty as \"Qty:Float:100\",\n\t`tabPurchase Order Item`.received_qty as \"Received Qty:Float:100\", \n\t(`tabPurchase Order Item`.qty - ifnull(`tabPurchase Order Item`.received_qty, 0)) as \"Qty to Receive:Float:100\",\n    `tabPurchase Order Item`.warehouse as \"Warehouse:Link/Warehouse:150\",\n\t`tabPurchase Order Item`.item_name as \"Item Name::150\",\n\t`tabPurchase Order Item`.description as \"Description::200\",\n    `tabPurchase Order Item`.brand as \"Brand::100\"\nfrom\n\t`tabPurchase Order`, `tabPurchase Order Item`\nwhere\n\t`tabPurchase Order Item`.`parent` = `tabPurchase Order`.`name`\n\tand `tabPurchase Order`.docstatus = 1\n\tand `tabPurchase Order`.status != \"Stopped\"\n\tand ifnull(`tabPurchase Order Item`.received_qty, 0) < ifnull(`tabPurchase Order Item`.qty, 0)\norder by `tabPurchase Order`.transaction_date asc", 
   "ref_doctype": "Purchase Receipt", 
   "report_name": "Purchase Order Items To Be Received", 
   "report_type": "Query Report"