Merge pull request #1280 from nabinhait/hotfix

Fixes in item-wise sales/purchase register
diff --git a/accounts/report/accounts_payable/accounts_payable.py b/accounts/report/accounts_payable/accounts_payable.py
index f9266dc..1bd8a9f 100644
--- a/accounts/report/accounts_payable/accounts_payable.py
+++ b/accounts/report/accounts_payable/accounts_payable.py
@@ -9,18 +9,20 @@
 
 def execute(filters=None):
 	if not filters: filters = {}
-	columns = get_columns()
+	supplier_naming_by = webnotes.conn.get_value("Buying Settings", None, "supp_master_name")
+	columns = get_columns(supplier_naming_by)
 	entries = get_gl_entries(filters)
-	account_supplier = dict(webnotes.conn.sql("""select account.name, supplier.supplier_name
-		from `tabAccount` account, `tabSupplier` supplier 
-		where account.master_type="Supplier" and supplier.name=account.master_name"""))
-	
+	account_map = dict(((r.name, r) for r in webnotes.conn.sql("""select acc.name, 
+		supp.supplier_name, supp.name as supplier 
+		from `tabAccount` acc, `tabSupplier` supp 
+		where acc.master_type="Supplier" and supp.name=acc.master_name""", as_dict=1)))
+
 	entries_after_report_date = [[gle.voucher_type, gle.voucher_no] 
 		for gle in get_gl_entries(filters, before_report_date=False)]
-	
+
 	account_supplier_type_map = get_account_supplier_type_map()
 	voucher_detail_map = get_voucher_details()
-	
+
 	# Age of the invoice on this date
 	age_on = getdate(filters.get("report_date")) > getdate(nowdate()) \
 		and nowdate() or filters.get("report_date")
@@ -37,9 +39,7 @@
 
 			if abs(flt(outstanding_amount)) > 0.01:
 				paid_amount = invoiced_amount - outstanding_amount
-				row = [gle.posting_date, gle.account, account_supplier.get(gle.account, ""),
-					gle.voucher_type, gle.voucher_no, gle.remarks, 
-					account_supplier_type_map.get(gle.account), 
+				row = [gle.posting_date, gle.account, gle.voucher_type, gle.voucher_no, 
 					voucher_details.get("due_date", ""), voucher_details.get("bill_no", ""), 
 					voucher_details.get("bill_date", ""), invoiced_amount, 
 					paid_amount, outstanding_amount]
@@ -50,21 +50,38 @@
 				else:
 					ageing_based_on_date = gle.posting_date
 					
-				row += get_ageing_data(age_on, ageing_based_on_date, outstanding_amount)
+				row += get_ageing_data(age_on, ageing_based_on_date, outstanding_amount) + \
+					[account_map.get(gle.account).get("supplier") or ""]
+
+				if supplier_naming_by == "Naming Series":
+					row += [account_map.get(gle.account).get("supplier_name") or ""]
+
+				row += [account_supplier_type_map.get(gle.account), gle.remarks]
 				data.append(row)
-				
+
+	for i in range(0, len(data)):
+		data[i].insert(4, """<a href="%s"><i class="icon icon-share" style="cursor: pointer;"></i></a>""" \
+			% ("/".join(["#Form", data[i][2], data[i][3]]),))
+
 	return columns, data
 	
-def get_columns():
-	return [
-		"Posting Date:Date:80", "Account:Link/Account:150", "Supplier::150", "Voucher Type::110", 
-		"Voucher No::120", "Remarks::150", "Supplier Type:Link/Supplier Type:120", 
-		"Due Date:Date:80", "Bill No::80", "Bill Date:Date:80", 
+def get_columns(supplier_naming_by):
+	columns = [
+		"Posting Date:Date:80", "Account:Link/Account:150", "Voucher Type::110", 
+		"Voucher No::120", "::30", "Due Date:Date:80", "Bill No::80", "Bill Date:Date:80", 
 		"Invoiced Amount:Currency:100", "Paid Amount:Currency:100", 
 		"Outstanding Amount:Currency:100", "Age:Int:50", "0-30:Currency:100", 
-		"30-60:Currency:100", "60-90:Currency:100", "90-Above:Currency:100"
+		"30-60:Currency:100", "60-90:Currency:100", "90-Above:Currency:100",
+		"Supplier:Link/Supplier:150"
 	]
-	
+
+	if supplier_naming_by == "Naming Series":
+		columns += ["Supplier Name::110"]
+
+	columns += ["Supplier Type:Link/Supplier Type:120", "Remarks::150"]
+
+	return columns
+
 def get_gl_entries(filters, before_report_date=True):
 	conditions, supplier_accounts = get_conditions(filters, before_report_date)
 	gl_entries = []
@@ -102,10 +119,10 @@
 	
 def get_account_supplier_type_map():
 	account_supplier_type_map = {}
-	for each in webnotes.conn.sql("""select t2.name, t1.supplier_type from `tabSupplier` t1, 
-			`tabAccount` t2 where t1.name = t2.master_name group by t2.name"""):
+	for each in webnotes.conn.sql("""select acc.name, supp.supplier_type from `tabSupplier` supp, 
+			`tabAccount` acc where supp.name = acc.master_name group by acc.name"""):
 		account_supplier_type_map[each[0]] = each[1]
-		
+
 	return account_supplier_type_map
 	
 def get_voucher_details():
diff --git a/accounts/report/accounts_receivable/accounts_receivable.py b/accounts/report/accounts_receivable/accounts_receivable.py
index 945bae4..781db2a 100644
--- a/accounts/report/accounts_receivable/accounts_receivable.py
+++ b/accounts/report/accounts_receivable/accounts_receivable.py
@@ -15,20 +15,28 @@
 			else self.filters.report_date
 			
 	def run(self):
-		return self.get_columns(), self.get_data()
+		customer_naming_by = webnotes.conn.get_value("Selling Settings", None, "cust_master_name")
+		return self.get_columns(customer_naming_by), self.get_data(customer_naming_by)
 		
-	def get_columns(self):
-		return [
+	def get_columns(self, customer_naming_by):
+		columns = [
 			"Posting Date:Date:80", "Account:Link/Account:150",
 			"Voucher Type::110", "Voucher No::120", "::30",
 			"Due Date:Date:80",  
 			"Invoiced Amount:Currency:100", "Payment Received:Currency:100", 
 			"Outstanding Amount:Currency:100", "Age:Int:50", "0-30:Currency:100",
 			"30-60:Currency:100", "60-90:Currency:100", "90-Above:Currency:100",
-			"Customer:Link/Customer:200", "Territory:Link/Territory:80", "Remarks::200"
+			"Customer:Link/Customer:200"
 		]
-		
-	def get_data(self):
+
+		if customer_naming_by == "Naming Series":
+			columns += ["Customer Name::110"]
+
+		columns += ["Territory:Link/Territory:80", "Remarks::200"]
+
+		return columns
+
+	def get_data(self, customer_naming_by):
 		data = []
 		future_vouchers = self.get_entries_after(self.filters.report_date)
 		for gle in self.get_entries_till(self.filters.report_date):
@@ -42,18 +50,23 @@
 						gle.voucher_type, gle.voucher_no, due_date,
 						invoiced_amount, payment_received,
 						outstanding_amount]
-					entry_date = due_date if self.filters.ageing_based_on=="Due Date" \
+					entry_date = due_date if self.filters.ageing_based_on == "Due Date" \
 						else gle.posting_date
-					row += get_ageing_data(self.age_as_on, entry_date, outstanding_amount)
-					row += [self.get_customer(gle.account), self.get_territory(gle.account), gle.remarks]
+					row += get_ageing_data(self.age_as_on, entry_date, outstanding_amount) + \
+						[self.get_customer(gle.account)]
+
+					if customer_naming_by == "Naming Series":
+						row += [self.get_customer_name(gle.account)]
+
+					row += [self.get_territory(gle.account), gle.remarks]
 					data.append(row)
 		
-		for i in range(0,len(data)):
+		for i in range(0, len(data)):
 			data[i].insert(4, """<a href="%s"><i class="icon icon-share" style="cursor: pointer;"></i></a>""" \
 				% ("/".join(["#Form", data[i][2], data[i][3]]),))
 		
 		return data
-				
+
 	def get_entries_after(self, report_date):
 		# returns a distinct list
 		return list(set([(e.voucher_type, e.voucher_no) for e in self.get_gl_entries()
@@ -77,18 +90,21 @@
 		return flt(gle.debit) - flt(gle.credit) - payment_received
 		
 	def get_customer(self, account):
+		return self.get_account_map().get(account).get("customer") or ""
+
+	def get_customer_name(self, account):
 		return self.get_account_map().get(account).get("customer_name") or ""
-		
+
 	def get_territory(self, account):
 		return self.get_account_map().get(account).get("territory") or ""
 		
 	def get_account_map(self):
 		if not hasattr(self, "account_map"):
 			self.account_map = dict(((r.name, r) for r in webnotes.conn.sql("""select 
-				account.name, customer.name as customer_name, customer.territory
-				from `tabAccount` account, `tabCustomer` customer 
-				where account.master_type="Customer" 
-				and customer.name=account.master_name""", as_dict=True)))
+				acc.name, cust.name as customer, cust.customer_name, cust.territory
+				from `tabAccount` acc, `tabCustomer` cust 
+				where acc.master_type="Customer" 
+				and cust.name=acc.master_name""", as_dict=True)))
 				
 		return self.account_map
 		
@@ -147,7 +163,7 @@
 
 def execute(filters=None):
 	return AccountsReceivableReport(filters).run()
-	
+
 def get_ageing_data(age_as_on, entry_date, outstanding_amount):
 	# [0-30, 30-60, 60-90, 90-above]
 	outstanding_range = [0.0, 0.0, 0.0, 0.0]
@@ -164,4 +180,4 @@
 	if index is None: index = 3
 	outstanding_range[index] = outstanding_amount
 	
-	return [age] + outstanding_range
+	return [age] + outstanding_range
\ No newline at end of file
diff --git a/config.json b/config.json
index c09c2af..26fc50d 100644
--- a/config.json
+++ b/config.json
@@ -1,6 +1,6 @@
 {
  "app_name": "ERPNext", 
- "app_version": "3.4.3", 
+ "app_version": "3.4.7", 
  "base_template": "app/portal/templates/base.html", 
  "modules": {
   "Accounts": {
@@ -74,5 +74,5 @@
    "type": "module"
   }
  }, 
- "requires_framework_version": "==3.4.2"
+ "requires_framework_version": "==3.4.3"
 }
\ No newline at end of file