Sales/purchase register and item-wise sales/purchase register fixes for party
Show Delivery Note and Purchase Receipt based on SO and PO if it not defined in SI/PI #2166
diff --git a/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.js b/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.js
index 13c0edc..8e322ab 100644
--- a/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.js
+++ b/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.js
@@ -23,21 +23,10 @@
 			"options": "Item",
 		},
 		{
-			"fieldname":"account",
-			"label": __("Account"),
+			"fieldname":"supplier",
+			"label": __("Supplier"),
 			"fieldtype": "Link",
-			"options": "Account",
-			"get_query": function() {
-				var company = frappe.query_report.filters_by_name.company.get_value();
-				return {
-					"query": "erpnext.controllers.queries.get_account_list", 
-					"filters": {
-						"report_type": "Balance Sheet",
-						"company": company,
-						"master_type": "Supplier"
-					}
-				}
-			}
+			"options": "Supplier"
 		},
 		{
 			"fieldname":"company",
@@ -47,4 +36,4 @@
 			"default": frappe.defaults.get_user_default("company")
 		}
 	]
-}
\ No newline at end of file
+}
diff --git a/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py b/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py
index 127e9cb..9e66771 100644
--- a/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py
+++ b/erpnext/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py
@@ -10,42 +10,50 @@
 	if not filters: filters = {}
 	columns = get_columns()
 	last_col = len(columns)
-	
+
 	item_list = get_items(filters)
 	aii_account_map = get_aii_accounts()
 	if item_list:
 		item_tax, tax_accounts = get_tax_accounts(item_list, columns)
-	
+
 	data = []
 	for d in item_list:
+		purchase_receipt = None
+		if d.purchase_receipt:
+			purchase_receipt = d.purchase_receipt
+		elif d.po_detail:
+			purchase_receipt = ", ".join(frappe.db.sql_list("""select distinct parent
+			from `tabPurchase Receipt Item` where docstatus=1 and prevdoc_detail_docname=%s""", d.po_detail))
+
 		expense_account = d.expense_account or aii_account_map.get(d.company)
-		row = [d.item_code, d.item_name, d.item_group, d.parent, d.posting_date, 
-			d.supplier_name, d.credit_to, d.project_name, d.company, d.purchase_order, 
-			d.purchase_receipt, expense_account, d.qty, d.base_rate, d.base_amount]
+		row = [d.item_code, d.item_name, d.item_group, d.parent, d.posting_date, d.supplier,
+			d.supplier_name, d.credit_to, d.project_name, d.company, d.purchase_order,
+			purchase_receipt, expense_account, d.qty, d.base_rate, d.base_amount]
 		for tax in tax_accounts:
 			row.append(item_tax.get(d.parent, {}).get(d.item_code, {}).get(tax, 0))
 
 		total_tax = sum(row[last_col:])
 		row += [total_tax, d.base_amount + total_tax]
-		
+
 		data.append(row)
-	
+
 	return columns, data
-	
-	
+
+
 def get_columns():
-	return [_("Item Code") + ":Link/Item:120", _("Item Name") + "::120", _("Item Group") + ":Link/Item Group:100", 
-		_("Invoice") + ":Link/Purchase Invoice:120", _("Posting Date") + ":Date:80", _("Supplier") + ":Link/Customer:120", 
-		_("Supplier Account") + ":Link/Account:120", _("Project") + ":Link/Project:80", _("Company") + ":Link/Company:100", 
-		_("Purchase Order") + ":Link/Purchase Order:100", _("Purchase Receipt") + ":Link/Purchase Receipt:100", 
-		_("Expense Account") + ":Link/Account:140", _("Qty") + ":Float:120", _("Rate") + ":Currency:120", 
-		_("Amount") + ":Currency:120"]
-	
+	return [_("Item Code") + ":Link/Item:120", _("Item Name") + "::120",
+		_("Item Group") + ":Link/Item Group:100", _("Invoice") + ":Link/Purchase Invoice:120",
+		_("Posting Date") + ":Date:80", _("Supplier") + ":Link/Supplier:120",
+		"Supplier Name::120", "Payable Account:Link/Account:120", _("Project") + ":Link/Project:80",
+		_("Company") + ":Link/Company:100", _("Purchase Order") + ":Link/Purchase Order:100",
+		_("Purchase Receipt") + ":Link/Purchase Receipt:100", _("Expense Account") + ":Link/Account:140",
+		_("Qty") + ":Float:120", _("Rate") + ":Currency:120", _("Amount") + ":Currency:120"]
+
 def get_conditions(filters):
 	conditions = ""
-	
+
 	for opts in (("company", " and company=%(company)s"),
-		("account", " and pi.credit_to = %(account)s"),
+		("supplier", " and pi.supplier = %(supplier)s"),
 		("item_code", " and pi_item.item_code = %(item_code)s"),
 		("from_date", " and pi.posting_date>=%(from_date)s"),
 		("to_date", " and pi.posting_date<=%(to_date)s")):
@@ -53,48 +61,48 @@
 				conditions += opts[1]
 
 	return conditions
-	
+
 def get_items(filters):
 	conditions = get_conditions(filters)
 	match_conditions = frappe.build_match_conditions("Purchase Invoice")
-	
-	return frappe.db.sql("""select pi_item.parent, pi.posting_date, pi.credit_to, pi.company, 
-		pi.supplier, pi.remarks, pi_item.item_code, pi_item.item_name, pi_item.item_group, 
-		pi_item.project_name, pi_item.purchase_order, pi_item.purchase_receipt, 
+
+	return frappe.db.sql("""select pi_item.parent, pi.posting_date, pi.credit_to, pi.company,
+		pi.supplier, pi.remarks, pi_item.item_code, pi_item.item_name, pi_item.item_group,
+		pi_item.project_name, pi_item.purchase_order, pi_item.purchase_receipt, pi_item.po_detail,
 		pi_item.expense_account, pi_item.qty, pi_item.base_rate, pi_item.base_amount, pi.supplier_name
-		from `tabPurchase Invoice` pi, `tabPurchase Invoice Item` pi_item 
+		from `tabPurchase Invoice` pi, `tabPurchase Invoice Item` pi_item
 		where pi.name = pi_item.parent and pi.docstatus = 1 %s %s
 		order by pi.posting_date desc, pi_item.item_code desc""" % (conditions, match_conditions), filters, as_dict=1)
-		
+
 def get_aii_accounts():
 	return dict(frappe.db.sql("select name, stock_received_but_not_billed from tabCompany"))
-	
+
 def get_tax_accounts(item_list, columns):
 	import json
 	item_tax = {}
 	tax_accounts = []
-	
+
 	tax_details = frappe.db.sql("""select parent, account_head, item_wise_tax_detail
-		from `tabPurchase Taxes and Charges` where parenttype = 'Purchase Invoice' 
-		and docstatus = 1 and ifnull(account_head, '') != '' and category in ('Total', 'Valuation and Total') 
+		from `tabPurchase Taxes and Charges` where parenttype = 'Purchase Invoice'
+		and docstatus = 1 and ifnull(account_head, '') != '' and category in ('Total', 'Valuation and Total')
 		and parent in (%s)""" % ', '.join(['%s']*len(item_list)), tuple([item.parent for item in item_list]))
-		
+
 	for parent, account_head, item_wise_tax_detail in tax_details:
 		if account_head not in tax_accounts:
 			tax_accounts.append(account_head)
-		
+
 		if item_wise_tax_detail:
 			try:
 				item_wise_tax_detail = json.loads(item_wise_tax_detail)
 				for item, tax_amount in item_wise_tax_detail.items():
 					item_tax.setdefault(parent, {}).setdefault(item, {})[account_head] = \
 						flt(tax_amount[1]) if isinstance(tax_amount, list) else flt(tax_amount)
-				
+
 			except ValueError:
 				continue
-	
+
 	tax_accounts.sort()
 	columns += [account_head + ":Currency:80" for account_head in tax_accounts]
 	columns += ["Total Tax:Currency:80", "Total:Currency:80"]
 
-	return item_tax, tax_accounts
\ No newline at end of file
+	return item_tax, tax_accounts
diff --git a/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.js b/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.js
index 79b1785..88a6cdd 100644
--- a/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.js
+++ b/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.js
@@ -17,21 +17,10 @@
 			"default": get_today()
 		},
 		{
-			"fieldname":"account",
-			"label": __("Account"),
+			"fieldname":"customer",
+			"label": __("Customer"),
 			"fieldtype": "Link",
-			"options": "Account",
-			"get_query": function() {
-				var company = frappe.query_report.filters_by_name.company.get_value();
-				return {
-					"query": "erpnext.controllers.queries.get_account_list", 
-					"filters": {
-						"report_type": "Balance Sheet",
-						"company": company,
-						"master_type": "Customer"
-					}
-				}
-			}
+			"options": "Customer"
 		},
 		{
 			"fieldname":"company",
@@ -41,4 +30,4 @@
 			"default": frappe.defaults.get_user_default("company")
 		}
 	]
-}
\ No newline at end of file
+}
diff --git a/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py b/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py
index 2840291..48cb8e1 100644
--- a/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py
+++ b/erpnext/accounts/report/item_wise_sales_register/item_wise_sales_register.py
@@ -14,38 +14,48 @@
 	item_list = get_items(filters)
 	if item_list:
 		item_tax, tax_accounts = get_tax_accounts(item_list, columns)
-	
+
 	data = []
 	for d in item_list:
-		row = [d.item_code, d.item_name, d.item_group, d.parent, d.posting_date, 
-			d.customer_name, d.debit_to, d.territory, d.project_name, d.company, d.sales_order, 
-			d.delivery_note, d.income_account, d.qty, d.base_rate, d.base_amount]
-			
+		delivery_note = None
+		if d.delivery_note:
+			delivery_note = d.delivery_note
+		elif d.so_detail:
+			delivery_note = ", ".join(frappe.db.sql_list("""select distinct parent
+			from `tabDelivery Note Item` where docstatus=1 and prevdoc_detail_docname=%s""", d.so_detail))
+
+		row = [d.item_code, d.item_name, d.item_group, d.parent, d.posting_date, d.customer, d.customer_name,
+			d.customer_group, d.debit_to, d.territory, d.project_name, d.company, d.sales_order,
+			delivery_note, d.income_account, d.qty, d.base_rate, d.base_amount]
+
 		for tax in tax_accounts:
 			row.append(item_tax.get(d.parent, {}).get(d.item_code, {}).get(tax, 0))
 
 		total_tax = sum(row[last_col:])
 		row += [total_tax, d.base_amount + total_tax]
-		
+
 		data.append(row)
-	
+
 	return columns, data
-	
+
 def get_columns():
 	return [
-		_("Item Code") + ":Link/Item:120", _("Item Name") + "::120", _("Item Group") + ":Link/Item Group:100", 
-		_("Invoice") + ":Link/Sales Invoice:120", _("Posting Date") + ":Date:80", _("Customer") + ":Link/Customer:120", 
-		_("Customer Account") + ":Link/Account:120", _("Territory") + ":Link/Territory:80",
-		_("Project") + ":Link/Project:80", _("Company") + ":Link/Company:100", _("Sales Order") + ":Link/Sales Order:100", 
-		_("Delivery Note") + ":Link/Delivery Note:100", _("Income Account") + ":Link/Account:140", 
-		_("Qty") + ":Float:120", _("Rate") + ":Currency:120", _("Amount") + ":Currency:120"
+		_("Item Code") + ":Link/Item:120", _("Item Name") + "::120",
+		_("Item Group") + ":Link/Item Group:100", _("Invoice") + ":Link/Sales Invoice:120",
+		_("Posting Date") + ":Date:80", _("Customer") + ":Link/Customer:120",
+		_("Customer Name") + "::120", _("Customer Group") + ":Link/Customer Group:120",
+		_("Receivable Account") + ":Link/Account:120", _("Territory") + ":Link/Territory:80",
+		_("Project") + ":Link/Project:80", _("Company") + ":Link/Company:100",
+		_("Sales Order") + ":Link/Sales Order:100", _("Delivery Note") + ":Link/Delivery Note:100",
+		_("Income Account") + ":Link/Account:140", _("Qty") + ":Float:120",
+		_("Rate") + ":Currency:120", _("Amount") + ":Currency:120"
 	]
-	
+
 def get_conditions(filters):
 	conditions = ""
-	
+
 	for opts in (("company", " and company=%(company)s"),
-		("account", " and si.debit_to = %(account)s"),
+		("customer", " and si.customer = %(customer)s"),
 		("item_code", " and si_item.item_code = %(item_code)s"),
 		("from_date", " and si.posting_date>=%(from_date)s"),
 		("to_date", " and si.posting_date<=%(to_date)s")):
@@ -53,32 +63,33 @@
 				conditions += opts[1]
 
 	return conditions
-		
+
 def get_items(filters):
 	conditions = get_conditions(filters)
-	return frappe.db.sql("""select si_item.parent, si.posting_date, si.debit_to, si.project_name, 
-		si.customer, si.remarks, si.territory, si.company, si_item.item_code, si_item.item_name, 
-		si_item.item_group, si_item.sales_order, si_item.delivery_note, si_item.income_account, 
-		si_item.qty, si_item.base_rate, si_item.base_amount, si.customer_name
-		from `tabSales Invoice` si, `tabSales Invoice Item` si_item 
-		where si.name = si_item.parent and si.docstatus = 1 %s 
+	return frappe.db.sql("""select si_item.parent, si.posting_date, si.debit_to, si.project_name,
+		si.customer, si.remarks, si.territory, si.company, si_item.item_code, si_item.item_name,
+		si_item.item_group, si_item.sales_order, si_item.delivery_note, si_item.income_account,
+		si_item.qty, si_item.base_rate, si_item.base_amount, si.customer_name,
+		si.customer_group, si_item.so_detail
+		from `tabSales Invoice` si, `tabSales Invoice Item` si_item
+		where si.name = si_item.parent and si.docstatus = 1 %s
 		order by si.posting_date desc, si_item.item_code desc""" % conditions, filters, as_dict=1)
-		
+
 def get_tax_accounts(item_list, columns):
 	import json
 	item_tax = {}
 	tax_accounts = []
-	
+
 	tax_details = frappe.db.sql("""select parent, account_head, item_wise_tax_detail
-		from `tabSales Taxes and Charges` where parenttype = 'Sales Invoice' 
+		from `tabSales Taxes and Charges` where parenttype = 'Sales Invoice'
 		and docstatus = 1 and ifnull(account_head, '') != ''
-		and parent in (%s)""" % ', '.join(['%s']*len(item_list)), 
+		and parent in (%s)""" % ', '.join(['%s']*len(item_list)),
 		tuple([item.parent for item in item_list]))
-		
+
 	for parent, account_head, item_wise_tax_detail in tax_details:
 		if account_head not in tax_accounts:
 			tax_accounts.append(account_head)
-				
+
 		if item_wise_tax_detail:
 			try:
 				item_wise_tax_detail = json.loads(item_wise_tax_detail)
@@ -87,9 +98,9 @@
 						 flt(tax_amount[1]) if isinstance(tax_amount, list) else flt(tax_amount)
 			except ValueError:
 				continue
-	
+
 	tax_accounts.sort()
 	columns += [account_head + ":Currency:80" for account_head in tax_accounts]
 	columns += ["Total Tax:Currency:80", "Total:Currency:80"]
 
-	return item_tax, tax_accounts
\ No newline at end of file
+	return item_tax, tax_accounts
diff --git a/erpnext/accounts/report/purchase_register/purchase_register.js b/erpnext/accounts/report/purchase_register/purchase_register.js
index a1a41a5..5497edb 100644
--- a/erpnext/accounts/report/purchase_register/purchase_register.js
+++ b/erpnext/accounts/report/purchase_register/purchase_register.js
@@ -17,21 +17,10 @@
 			"default": get_today()
 		},
 		{
-			"fieldname":"account",
-			"label": __("Account"),
+			"fieldname":"supplier",
+			"label": __("Supplier"),
 			"fieldtype": "Link",
-			"options": "Account",
-			"get_query": function() {
-				var company = frappe.query_report.filters_by_name.company.get_value();
-				return {
-					"query": "erpnext.controllers.queries.get_account_list", 
-					"filters": {
-						"report_type": "Balance Sheet",
-						"company": company,
-						"master_type": "Supplier"
-					}
-				}
-			}
+			"options": "Supplier"
 		},
 		{
 			"fieldname":"company",
@@ -41,4 +30,4 @@
 			"default": frappe.defaults.get_user_default("company")
 		}
 	]
-}
\ No newline at end of file
+}
diff --git a/erpnext/accounts/report/purchase_register/purchase_register.py b/erpnext/accounts/report/purchase_register/purchase_register.py
index 4000657..244a20c 100644
--- a/erpnext/accounts/report/purchase_register/purchase_register.py
+++ b/erpnext/accounts/report/purchase_register/purchase_register.py
@@ -11,17 +11,17 @@
 
 	invoice_list = get_invoices(filters)
 	columns, expense_accounts, tax_accounts = get_columns(invoice_list)
-	
-	
+
+
 	if not invoice_list:
-		msgprint(_("No record found"))		
+		msgprint(_("No record found"))
 		return columns, invoice_list
-	
+
 	invoice_expense_map = get_invoice_expense_map(invoice_list)
-	invoice_expense_map, invoice_tax_map = get_invoice_tax_map(invoice_list, 
+	invoice_expense_map, invoice_tax_map = get_invoice_tax_map(invoice_list,
 		invoice_expense_map, expense_accounts)
 	invoice_po_pr_map = get_invoice_po_pr_map(invoice_list)
-	account_map = get_account_details(invoice_list)
+	supplier_details = get_supplier_deatils(invoice_list)
 
 	data = []
 	for inv in invoice_list:
@@ -30,20 +30,20 @@
 		purchase_receipt = list(set(invoice_po_pr_map.get(inv.name, {}).get("purchase_receipt", [])))
 		project_name = list(set(invoice_po_pr_map.get(inv.name, {}).get("project_name", [])))
 
-		row = [inv.name, inv.posting_date, inv.supplier, inv.supplier_name, inv.credit_to, 
-			account_map.get(inv.credit_to), ", ".join(project_name), inv.bill_no, inv.bill_date, 
-			inv.remarks, ", ".join(purchase_order), ", ".join(purchase_receipt)]
-		
+		row = [inv.name, inv.posting_date, inv.supplier, inv.supplier_name, supplier_details.get(inv.supplier),
+			inv.credit_to, ", ".join(project_name), inv.bill_no, inv.bill_date, inv.remarks,
+			", ".join(purchase_order), ", ".join(purchase_receipt)]
+
 		# map expense values
 		net_total = 0
 		for expense_acc in expense_accounts:
 			expense_amount = flt(invoice_expense_map.get(inv.name, {}).get(expense_acc))
 			net_total += expense_amount
 			row.append(expense_amount)
-		
+
 		# net total
 		row.append(net_total or inv.net_total)
-			
+
 		# tax account
 		total_tax = 0
 		for tax_acc in tax_accounts:
@@ -56,82 +56,80 @@
 		row += [total_tax, inv.grand_total, flt(inv.grand_total, 2), inv.outstanding_amount]
 		data.append(row)
 		# raise Exception
-	
+
 	return columns, data
-	
-	
+
+
 def get_columns(invoice_list):
 	"""return columns based on filters"""
 	columns = [
-		_("Invoice") + ":Link/Purchase Invoice:120", _("Posting Date") + ":Date:80", _("Supplier Id") + "::120", 
-		_("Supplier Name") + "::120", _("Supplier Account") + ":Link/Account:120", 
-		_("Account Group") + ":Link/Account:120", _("Project") + ":Link/Project:80", _("Bill No") + "::120", 
-		_("Bill Date") + ":Date:80", _("Remarks") + "::150", 
+		_("Invoice") + ":Link/Purchase Invoice:120", _("Posting Date") + ":Date:80", _("Supplier Id") + "::120",
+		_("Supplier Name") + "::120", _("Supplier Type") + ":Link/Supplier Type:120", _("Payable Account") + ":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 = tax_accounts = expense_columns = tax_columns = []
-	
-	if invoice_list:	
-		expense_accounts = frappe.db.sql_list("""select distinct expense_account 
-			from `tabPurchase Invoice Item` where docstatus = 1 and ifnull(expense_account, '') != '' 
-			and parent in (%s) order by expense_account""" % 
+
+	if invoice_list:
+		expense_accounts = frappe.db.sql_list("""select distinct expense_account
+			from `tabPurchase Invoice Item` where docstatus = 1 and ifnull(expense_account, '') != ''
+			and parent in (%s) order by expense_account""" %
 			', '.join(['%s']*len(invoice_list)), tuple([inv.name for inv in invoice_list]))
-		
-		tax_accounts = 	frappe.db.sql_list("""select distinct account_head 
-			from `tabPurchase Taxes and Charges` where parenttype = 'Purchase Invoice' 
-			and docstatus = 1 and ifnull(account_head, '') != '' and category in ('Total', 'Valuation and Total') 
-			and parent in (%s) order by account_head""" % 
+
+		tax_accounts = 	frappe.db.sql_list("""select distinct account_head
+			from `tabPurchase Taxes and Charges` where parenttype = 'Purchase Invoice'
+			and docstatus = 1 and ifnull(account_head, '') != '' and category in ('Total', 'Valuation and Total')
+			and parent in (%s) order by account_head""" %
 			', '.join(['%s']*len(invoice_list)), tuple([inv.name for inv in invoice_list]))
-			
-				
+
+
 	expense_columns = [(account + ":Currency:120") for account in expense_accounts]
 	for account in tax_accounts:
 		if account not in expense_accounts:
 			tax_columns.append(account + ":Currency:120")
-	
-	columns = columns + expense_columns + \
-		["Net Total:Currency:120"] + tax_columns + \
-		["Total Tax:Currency:120"] + ["Grand Total:Currency:120"] + \
-		["Rounded Total:Currency:120"] + ["Outstanding Amount:Currency:120"]
+
+	columns = columns + expense_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, expense_accounts, tax_accounts
 
 def get_conditions(filters):
 	conditions = ""
-	
+
 	if filters.get("company"): conditions += " and company=%(company)s"
-	if filters.get("account"): conditions += " and credit_to = %(account)s"
+	if filters.get("supplier"): conditions += " and supplier = %(supplier)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 frappe.db.sql("""select name, posting_date, credit_to, supplier, supplier_name, 
-		bill_no, bill_date, remarks, net_total, grand_total, outstanding_amount 
-		from `tabPurchase Invoice` where docstatus = 1 %s 
+	return frappe.db.sql("""select name, posting_date, credit_to, supplier, supplier_name
+		bill_no, bill_date, remarks, net_total, grand_total, outstanding_amount
+		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 = frappe.db.sql("""select parent, expense_account, sum(base_amount) as amount
-		from `tabPurchase Invoice Item` where parent in (%s) group by parent, expense_account""" % 
+		from `tabPurchase Invoice Item` where parent in (%s) group by parent, expense_account""" %
 		', '.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, frappe._dict()).setdefault(d.expense_account, [])
 		invoice_expense_map[d.parent][d.expense_account] = flt(d.amount)
-	
+
 	return invoice_expense_map
-	
+
 def get_invoice_tax_map(invoice_list, invoice_expense_map, expense_accounts):
 	tax_details = frappe.db.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""" % 
+		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:
 		if d.account_head in expense_accounts:
@@ -142,34 +140,51 @@
 		else:
 			invoice_tax_map.setdefault(d.parent, frappe._dict()).setdefault(d.account_head, [])
 			invoice_tax_map[d.parent][d.account_head] = flt(d.tax_amount)
-	
+
 	return invoice_expense_map, invoice_tax_map
-	
+
 def get_invoice_po_pr_map(invoice_list):
-	pi_items = frappe.db.sql("""select parent, purchase_order, purchase_receipt, 
-		project_name from `tabPurchase Invoice Item` where parent in (%s) 
-		and (ifnull(purchase_order, '') != '' or ifnull(purchase_receipt, '') != '')""" % 
+	pi_items = frappe.db.sql("""select parent, purchase_order, purchase_receipt, po_detail
+		project_name 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, frappe._dict()).setdefault(
 				"purchase_order", []).append(d.purchase_order)
+
+		pr_list = None
 		if d.purchase_receipt:
-			invoice_po_pr_map.setdefault(d.parent, frappe._dict()).setdefault(
-				"purchase_receipt", []).append(d.purchase_receipt)
+			pr_list = [d.purchase_receipt]
+		elif d.po_detail:
+			pr_list = frappe.db.sql_list("""select distinct parent from `tabPurchase Receipt Item`
+				where docstatus=1 and prevdoc_detail_docname=%s""", d.pr_detail)
+
+		if pr_list:
+			invoice_po_pr_map.setdefault(d.parent, frappe._dict()).setdefault("purchase_receipt", pr_list)
+
 		if d.project_name:
 			invoice_po_pr_map.setdefault(d.parent, frappe._dict()).setdefault(
 				"project_name", []).append(d.project_name)
-				
+
 	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 frappe.db.sql("""select name, parent_account from tabAccount 
+	for acc in frappe.db.sql("""select name, parent_account from tabAccount
 		where name in (%s)""" % ", ".join(["%s"]*len(accounts)), tuple(accounts), as_dict=1):
 			account_map[acc.name] = acc.parent_account
-						
-	return account_map
\ No newline at end of file
+
+	return account_map
+
+def get_supplier_deatils(invoice_list):
+	supplier_details = {}
+	suppliers = list(set([inv.supplier for inv in invoice_list]))
+	for supp in frappe.db.sql("""select name, supplier_type from `tabSupplier`
+		where name in (%s)""" % ", ".join(["%s"]*len(suppliers)), tuple(suppliers), as_dict=1):
+			supplier_details.setdefault(supp.name, supp.supplier_type)
+
+	return supplier_details
diff --git a/erpnext/accounts/report/sales_register/sales_register.js b/erpnext/accounts/report/sales_register/sales_register.js
index ac30562..78a729f 100644
--- a/erpnext/accounts/report/sales_register/sales_register.js
+++ b/erpnext/accounts/report/sales_register/sales_register.js
@@ -17,21 +17,10 @@
 			"default": get_today()
 		},
 		{
-			"fieldname":"account",
-			"label": __("Account"),
+			"fieldname":"customer",
+			"label": __("Customer"),
 			"fieldtype": "Link",
-			"options": "Account",
-			"get_query": function() {
-				var company = frappe.query_report.filters_by_name.company.get_value();
-				return {
-					"query": "erpnext.controllers.queries.get_account_list", 
-					"filters": {
-						"report_type": "Balance Sheet",
-						"company": company,
-						"master_type": "Customer"
-					}
-				}
-			}
+			"options": "Customer"
 		},
 		{
 			"fieldname":"company",
@@ -41,4 +30,4 @@
 			"default": frappe.defaults.get_user_default("company")
 		}
 	]
-}
\ No newline at end of file
+}
diff --git a/erpnext/accounts/report/sales_register/sales_register.py b/erpnext/accounts/report/sales_register/sales_register.py
index 1bde110..01e7bad 100644
--- a/erpnext/accounts/report/sales_register/sales_register.py
+++ b/erpnext/accounts/report/sales_register/sales_register.py
@@ -8,21 +8,20 @@
 
 def execute(filters=None):
 	if not filters: filters = {}
-	
+
 	invoice_list = get_invoices(filters)
 	columns, income_accounts, tax_accounts = get_columns(invoice_list)
-	
+
 	if not invoice_list:
 		msgprint(_("No record found"))
 		return columns, invoice_list
-	
+
 	invoice_income_map = get_invoice_income_map(invoice_list)
-	invoice_income_map, invoice_tax_map = get_invoice_tax_map(invoice_list, 
+	invoice_income_map, invoice_tax_map = get_invoice_tax_map(invoice_list,
 		invoice_income_map, income_accounts)
-	
+
 	invoice_so_dn_map = get_invoice_so_dn_map(invoice_list)
 	customer_map = get_customer_deatils(invoice_list)
-	account_map = get_account_details(invoice_list)
 
 	data = []
 	for inv in invoice_list:
@@ -30,20 +29,20 @@
 		sales_order = list(set(invoice_so_dn_map.get(inv.name, {}).get("sales_order", [])))
 		delivery_note = list(set(invoice_so_dn_map.get(inv.name, {}).get("delivery_note", [])))
 
-		row = [inv.name, inv.posting_date, inv.customer, inv.customer_name, inv.debit_to, 
-			account_map.get(inv.debit_to), customer_map.get(inv.customer), inv.project_name, 
-			inv.remarks, ", ".join(sales_order), ", ".join(delivery_note)]
-		
+		row = [inv.name, inv.posting_date, inv.customer, inv.customer_name,
+		customer_map.get(inv.customer)["customer_group"], customer_map.get(inv.customer)["territory"],
+		inv.debit_to, inv.project_name, inv.remarks, ", ".join(sales_order), ", ".join(delivery_note)]
+
 		# map income values
 		net_total = 0
 		for income_acc in income_accounts:
 			income_amount = flt(invoice_income_map.get(inv.name, {}).get(income_acc))
 			net_total += income_amount
 			row.append(income_amount)
-		
+
 		# net total
 		row.append(net_total or inv.net_total)
-			
+
 		# tax account
 		total_tax = 0
 		for tax_acc in tax_accounts:
@@ -56,81 +55,81 @@
 		row += [total_tax, inv.grand_total, inv.rounded_total, inv.outstanding_amount]
 
 		data.append(row)
-	
+
 	return columns, data
-	
-	
+
+
 def get_columns(invoice_list):
 	"""return columns based on filters"""
 	columns = [
-		_("Invoice") + ":Link/Sales Invoice:120", _("Posting Date") + ":Date:80", _("Customer Id") + "::120", 
-		_("Customer Name") + "::120", _("Customer Account") + ":Link/Account:120", _("Account Group") + ":Link/Account:120",
-		_("Territory") + ":Link/Territory:80", _("Project") + ":Link/Project:80", _("Remarks") + "::150", 
+		_("Invoice") + ":Link/Sales Invoice:120", _("Posting Date") + ":Date:80", _("Customer Id") + "::120",
+		_("Customer Name") + "::120", _("Customer Group") + ":Link/Customer Group:120", _("Territory") + ":Link/Territory:80",
+		_("Receivable Account") + ":Link/Account:120", _("Project") +":Link/Project:80", _("Remarks") + "::150",
 		_("Sales Order") + ":Link/Sales Order:100", _("Delivery Note") + ":Link/Delivery Note:100"
 	]
-	
+
 	income_accounts = tax_accounts = income_columns = tax_columns = []
-	
+
 	if invoice_list:
-		income_accounts = frappe.db.sql_list("""select distinct income_account 
-			from `tabSales Invoice Item` where docstatus = 1 and parent in (%s) 
-			order by income_account""" % 
+		income_accounts = frappe.db.sql_list("""select distinct income_account
+			from `tabSales Invoice Item` where docstatus = 1 and parent in (%s)
+			order by income_account""" %
 			', '.join(['%s']*len(invoice_list)), tuple([inv.name for inv in invoice_list]))
-	
-		tax_accounts = 	frappe.db.sql_list("""select distinct account_head 
-			from `tabSales Taxes and Charges` where parenttype = 'Sales Invoice' 
-			and docstatus = 1 and ifnull(tax_amount_after_discount_amount, 0) != 0 
-			and parent in (%s) order by account_head""" % 
+
+		tax_accounts = 	frappe.db.sql_list("""select distinct account_head
+			from `tabSales Taxes and Charges` where parenttype = 'Sales Invoice'
+			and docstatus = 1 and ifnull(tax_amount_after_discount_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 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"]
+
+	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
 
 def get_conditions(filters):
 	conditions = ""
-	
+
 	if filters.get("company"): conditions += " and company=%(company)s"
-	if filters.get("account"): conditions += " and debit_to = %(account)s"
+	if filters.get("customer"): conditions += " and customer = %(customer)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 frappe.db.sql("""select name, posting_date, debit_to, project_name, customer, 
-		customer_name, remarks, net_total, grand_total, rounded_total, outstanding_amount 
-		from `tabSales Invoice` 
-		where docstatus = 1 %s order by posting_date desc, name desc""" % 
+	return frappe.db.sql("""select name, posting_date, debit_to, project_name, customer,
+		customer_name, remarks, net_total, 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)
-	
+
 def get_invoice_income_map(invoice_list):
 	income_details = frappe.db.sql("""select parent, income_account, sum(base_amount) as amount
-		from `tabSales Invoice Item` where parent in (%s) group by parent, income_account""" % 
+		from `tabSales Invoice Item` where parent in (%s) group by parent, income_account""" %
 		', '.join(['%s']*len(invoice_list)), tuple([inv.name for inv in invoice_list]), as_dict=1)
-	
+
 	invoice_income_map = {}
 	for d in income_details:
 		invoice_income_map.setdefault(d.parent, frappe._dict()).setdefault(d.income_account, [])
 		invoice_income_map[d.parent][d.income_account] = flt(d.amount)
-	
+
 	return invoice_income_map
-	
+
 def get_invoice_tax_map(invoice_list, invoice_income_map, income_accounts):
-	tax_details = frappe.db.sql("""select parent, account_head, 
-		sum(tax_amount_after_discount_amount) as tax_amount 
-		from `tabSales Taxes and Charges` where parent in (%s) group by parent, account_head""" % 
+	tax_details = frappe.db.sql("""select parent, account_head,
+		sum(tax_amount_after_discount_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:
 		if d.account_head in income_accounts:
@@ -141,40 +140,38 @@
 		else:
 			invoice_tax_map.setdefault(d.parent, frappe._dict()).setdefault(d.account_head, [])
 			invoice_tax_map[d.parent][d.account_head] = flt(d.tax_amount)
-	
+
 	return invoice_income_map, invoice_tax_map
-	
+
 def get_invoice_so_dn_map(invoice_list):
-	si_items = frappe.db.sql("""select parent, sales_order, delivery_note
-		from `tabSales Invoice Item` where parent in (%s) 
-		and (ifnull(sales_order, '') != '' or ifnull(delivery_note, '') != '')""" % 
+	si_items = frappe.db.sql("""select parent, sales_order, delivery_note, so_detail
+		from `tabSales Invoice Item` where parent in (%s)
+		and (ifnull(sales_order, '') != '' or ifnull(delivery_note, '') != '')""" %
 		', '.join(['%s']*len(invoice_list)), tuple([inv.name for inv in invoice_list]), as_dict=1)
-	
+
 	invoice_so_dn_map = {}
 	for d in si_items:
 		if d.sales_order:
 			invoice_so_dn_map.setdefault(d.parent, frappe._dict()).setdefault(
 				"sales_order", []).append(d.sales_order)
+
+		delivery_note_list = None
 		if d.delivery_note:
-			invoice_so_dn_map.setdefault(d.parent, frappe._dict()).setdefault(
-				"delivery_note", []).append(d.delivery_note)
-				
+			delivery_note_list = [d.delivery_note]
+		elif d.sales_order:
+			delivery_note_list = frappe.db.sql_list("""select distinct parent from `tabDelivery Note Item`
+				where docstatus=1 and prevdoc_detail_docname=%s""", d.so_detail)
+
+		if delivery_note_list:
+			invoice_so_dn_map.setdefault(d.parent, frappe._dict()).setdefault("delivery_note", delivery_note_list)
+
 	return invoice_so_dn_map
-	
+
 def get_customer_deatils(invoice_list):
 	customer_map = {}
 	customers = list(set([inv.customer for inv in invoice_list]))
-	for cust in frappe.db.sql("""select name, territory from `tabCustomer` 
+	for cust in frappe.db.sql("""select name, territory, customer_group from `tabCustomer`
 		where name in (%s)""" % ", ".join(["%s"]*len(customers)), tuple(customers), as_dict=1):
-			customer_map[cust.name] = cust.territory
-	
+			customer_map.setdefault(cust.name, cust)
+
 	return customer_map
-	
-def get_account_details(invoice_list):
-	account_map = {}
-	accounts = list(set([inv.debit_to for inv in invoice_list]))
-	for acc in frappe.db.sql("""select name, parent_account from tabAccount 
-		where name in (%s)""" % ", ".join(["%s"]*len(accounts)), tuple(accounts), as_dict=1):
-			account_map[acc.name] = acc.parent_account
-						
-	return account_map
\ No newline at end of file