Merge branch 'master' of github.com:webnotes/erpnext
diff --git a/controllers/stock_controller.py b/controllers/stock_controller.py
index d4c92a9..359dc9e 100644
--- a/controllers/stock_controller.py
+++ b/controllers/stock_controller.py
@@ -12,18 +12,17 @@
 
 class StockController(AccountsController):
 	def make_gl_entries(self):
-		if not cint(webnotes.defaults.get_global_default("auto_accounting_for_stock")):
-			return
-		
-		warehouse_account = self.get_warehouse_account()
-		
-		if self.doc.docstatus==1:
-			gl_entries = self.get_gl_entries_for_stock(warehouse_account)
-			make_gl_entries(gl_entries)
-		else:	
+		if self.doc.docstatus == 2:
 			delete_gl_entries(voucher_type=self.doc.doctype, voucher_no=self.doc.name)
+			
+		if cint(webnotes.defaults.get_global_default("auto_accounting_for_stock")):
+			warehouse_account = self.get_warehouse_account()
 		
-		self.update_gl_entries_after(warehouse_account)
+			if self.doc.docstatus==1:
+				gl_entries = self.get_gl_entries_for_stock(warehouse_account)
+				make_gl_entries(gl_entries)
+
+			self.update_gl_entries_after(warehouse_account)
 	
 	def get_gl_entries_for_stock(self, warehouse_account=None, default_expense_account=None,
 			default_cost_center=None):
@@ -91,15 +90,11 @@
 		return stock_ledger
 		
 	def get_warehouse_account(self):
-		for d in webnotes.conn.sql("select name from tabWarehouse"):
-			webnotes.bean("Warehouse", d[0]).save()
-
 		warehouse_account = dict(webnotes.conn.sql("""select master_name, name from tabAccount 
 			where account_type = 'Warehouse' and ifnull(master_name, '') != ''"""))
 		return warehouse_account
 		
 	def update_gl_entries_after(self, warehouse_account=None):
-		from accounts.utils import get_stock_and_account_difference
 		future_stock_vouchers = self.get_future_stock_vouchers()
 		gle = self.get_voucherwise_gl_entries(future_stock_vouchers)
 		if not warehouse_account:
diff --git a/patches/march_2013/p06_remove_sales_purchase_return_tool.py b/patches/march_2013/p06_remove_sales_purchase_return_tool.py
index ed8fbc8..ac11ecc 100644
--- a/patches/march_2013/p06_remove_sales_purchase_return_tool.py
+++ b/patches/march_2013/p06_remove_sales_purchase_return_tool.py
@@ -4,5 +4,7 @@
 import webnotes
 
 def execute():
-	webnotes.delete_doc("DocType", "Sales and Purchase Return Item")
-	webnotes.delete_doc("DocType", "Sales and Purchase Return Tool")
\ No newline at end of file
+	if webnotes.conn.exists("DocType", "Sales and Purchase Return Item"):
+		webnotes.delete_doc("DocType", "Sales and Purchase Return Item")
+	if webnotes.conn.exists("DocType", "Sales and Purchase Return Tool"):
+		webnotes.delete_doc("DocType", "Sales and Purchase Return Tool")
\ No newline at end of file
diff --git a/patches/october_2012/fix_cancelled_gl_entries.py b/patches/october_2012/fix_cancelled_gl_entries.py
index b610985..475ea1c 100644
--- a/patches/october_2012/fix_cancelled_gl_entries.py
+++ b/patches/october_2012/fix_cancelled_gl_entries.py
@@ -11,6 +11,7 @@
 				and docstatus=2""" % (entry['voucher_type'], "%s"), entry['voucher_no'])
 			is_cancelled = docstatus and 'Yes' or None
 			if is_cancelled:
+				print entry['voucher_type'], entry['voucher_no']
 				webnotes.conn.sql("""update `tabGL Entry` set is_cancelled = 'Yes'
 					where voucher_type = %s and voucher_no = %s""", 
 					(entry['voucher_type'], entry['voucher_no']))
diff --git a/patches/october_2013/p03_remove_sales_and_purchase_return_tool.py b/patches/october_2013/p03_remove_sales_and_purchase_return_tool.py
new file mode 100644
index 0000000..e0965ab
--- /dev/null
+++ b/patches/october_2013/p03_remove_sales_and_purchase_return_tool.py
@@ -0,0 +1,8 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
+# License: GNU General Public License v3. See license.txt
+
+from __future__ import unicode_literals
+
+def execute():
+	from patches.march_2013 import p06_remove_sales_purchase_return_tool
+	p06_remove_sales_purchase_return_tool.execute()
\ No newline at end of file
diff --git a/patches/october_2013/p04_update_report_permission.py b/patches/october_2013/p04_update_report_permission.py
new file mode 100644
index 0000000..1a9f99d
--- /dev/null
+++ b/patches/october_2013/p04_update_report_permission.py
@@ -0,0 +1,9 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
+# License: GNU General Public License v3. See license.txt
+
+import webnotes
+
+def execute():
+	webnotes.conn.sql("""update tabDocPerm set `create`=1 where
+		parent='Report'
+		and role in ('Administrator', 'Report Manager', 'System Manager')""")
\ No newline at end of file
diff --git a/patches/october_2013/p05_delete_gl_entries_for_cancelled_vouchers.py b/patches/october_2013/p05_delete_gl_entries_for_cancelled_vouchers.py
new file mode 100644
index 0000000..1d8657a
--- /dev/null
+++ b/patches/october_2013/p05_delete_gl_entries_for_cancelled_vouchers.py
@@ -0,0 +1,16 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
+# License: GNU General Public License v3. See license.txt
+
+def execute():
+	import webnotes
+	entries = webnotes.conn.sql("""select voucher_type, voucher_no 
+		from `tabGL Entry` group by voucher_type, voucher_no""", as_dict=1)
+	for entry in entries:
+		try:
+			cancelled_voucher = webnotes.conn.sql("""select name from `tab%s` where name = %s
+				and docstatus=2""" % (entry['voucher_type'], "%s"), entry['voucher_no'])
+			if cancelled_voucher:
+				webnotes.conn.sql("""delete from `tabGL Entry` where voucher_type = %s and 
+					voucher_no = %s""", (entry['voucher_type'], entry['voucher_no']))
+		except:
+			pass
\ No newline at end of file
diff --git a/patches/patch_list.py b/patches/patch_list.py
index 0b27a2e..565ff99 100644
--- a/patches/patch_list.py
+++ b/patches/patch_list.py
@@ -224,4 +224,7 @@
 	"patches.october_2013.repost_planned_qty",
 	"patches.october_2013.p02_update_price_list_and_item_details_in_item_price",
 	"execute:webnotes.delete_doc('Report', 'Item-wise Price List')",
+	"patches.october_2013.p03_remove_sales_and_purchase_return_tool",
+	"patches.october_2013.p04_update_report_permission",
+	"patches.october_2013.p05_delete_gl_entries_for_cancelled_vouchers",
 ]
\ No newline at end of file
diff --git a/selling/page/selling_home/selling_home.js b/selling/page/selling_home/selling_home.js
index a69e50b..4981ee7 100644
--- a/selling/page/selling_home/selling_home.js
+++ b/selling/page/selling_home/selling_home.js
@@ -168,6 +168,11 @@
 		icon: "icon-list",
 		items: [
 			{
+				"label":wn._("Lead Details"),
+				route: "query-report/Lead Details",
+				doctype: "Lead"
+			},
+			{
 				"label":wn._("Customer Addresses And Contacts"),
 				route: "query-report/Customer Addresses And Contacts",
 				doctype: "Contact"
diff --git a/selling/report/lead_details/__init__.py b/selling/report/lead_details/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/selling/report/lead_details/__init__.py
diff --git a/selling/report/lead_details/lead_details.txt b/selling/report/lead_details/lead_details.txt
new file mode 100644
index 0000000..6da9b79
--- /dev/null
+++ b/selling/report/lead_details/lead_details.txt
@@ -0,0 +1,22 @@
+[
+ {
+  "creation": "2013-10-22 11:58:16", 
+  "docstatus": 0, 
+  "modified": "2013-10-22 12:08:18", 
+  "modified_by": "Administrator", 
+  "owner": "Administrator"
+ }, 
+ {
+  "doctype": "Report", 
+  "is_standard": "Yes", 
+  "name": "__common__", 
+  "query": "SELECT\n    `tabLead`.name as \"Lead Id:Link/Lead:120\",\n    `tabLead`.lead_name as \"Lead Name::120\",\n\t`tabLead`.company_name as \"Company Name::120\",\n\t`tabLead`.status as \"Status::120\",\n\tconcat_ws(', ', \n\t\ttrim(',' from `tabAddress`.address_line1), \n\t\ttrim(',' from tabAddress.address_line2), \n\t\ttabAddress.state, tabAddress.pincode, tabAddress.country\n\t) as 'Address::180',\n\t`tabLead`.phone as \"Phone::100\",\n\t`tabLead`.mobile_no as \"Mobile No::100\",\n\t`tabLead`.email_id as \"Email Id::120\",\n\t`tabLead`.lead_owner as \"Lead Owner::120\",\n\t`tabLead`.source as \"Source::120\",\n\t`tabLead`.territory as \"Territory::120\"\nFROM\n\t`tabLead`\n\tleft join `tabAddress` on (\n\t\t`tabAddress`.lead=`tabLead`.name\n\t)\nWHERE\n\t`tabLead`.docstatus<2\nORDER BY\n\t`tabLead`.name asc", 
+  "ref_doctype": "Lead", 
+  "report_name": "Lead Details", 
+  "report_type": "Query Report"
+ }, 
+ {
+  "doctype": "Report", 
+  "name": "Lead Details"
+ }
+]
\ No newline at end of file
diff --git a/setup/doctype/global_defaults/global_defaults.txt b/setup/doctype/global_defaults/global_defaults.txt
index fbfa8ca..a8a80b1 100644
--- a/setup/doctype/global_defaults/global_defaults.txt
+++ b/setup/doctype/global_defaults/global_defaults.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-05-02 17:53:24", 
   "docstatus": 0, 
-  "modified": "2013-08-06 11:22:22", 
+  "modified": "2013-10-23 10:22:44", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -176,13 +176,6 @@
   "read_only": 0
  }, 
  {
-  "doctype": "DocField", 
-  "fieldname": "hr", 
-  "fieldtype": "Section Break", 
-  "label": "HR", 
-  "read_only": 0
- }, 
- {
   "doctype": "DocPerm"
  }
 ]
\ No newline at end of file
diff --git a/utilities/repost_stock.py b/utilities/repost_stock.py
index 7359304..536df81 100644
--- a/utilities/repost_stock.py
+++ b/utilities/repost_stock.py
@@ -13,8 +13,11 @@
 	"""
 	webnotes.conn.auto_commit_on_many_writes = 1
 	
-	for d in webnotes.conn.sql("select item_code, warehouse from tabBin"):
-		repost_stock(d[0], d[1])
+	for d in webnotes.conn.sql("""select distinct item_code, warehouse from 
+		(select item_code, warehouse from tabBin
+		union
+		select item_code, warehouse from `tabStock Ledger Entry`) a"""):
+			repost_stock(d[0], d[1])
 		
 	webnotes.conn.auto_commit_on_many_writes = 0
 
@@ -31,7 +34,10 @@
 
 def repost_actual_qty(item_code, warehouse):
 	from stock.stock_ledger import update_entries_after
-	update_entries_after({ "item_code": item_code, "warehouse": warehouse })
+	try:
+		update_entries_after({ "item_code": item_code, "warehouse": warehouse })
+	except:
+		pass
 
 def get_reserved_qty(item_code, warehouse):
 	reserved_qty = webnotes.conn.sql("""