Merge branch 'master' of github.com:webnotes/erpnext
diff --git a/docs/user/stock/docs.user.stock.accounting_for_stock.md b/docs/user/stock/docs.user.stock.accounting_for_stock.md
index bf91fe5..8348c80 100644
--- a/docs/user/stock/docs.user.stock.accounting_for_stock.md
+++ b/docs/user/stock/docs.user.stock.accounting_for_stock.md
@@ -39,4 +39,4 @@
 
 This complete process is called Periodic Inventory.
 
-If you are an existing user using Periodic Inventory and want to use Perpetual Inventory, you have to follow some steps to migrate. For details, check [**Migration From Periodic Inventory**](docs.user.stock.perpetual_inventory.html)
+If you are an existing user using Periodic Inventory and want to use Perpetual Inventory, you have to follow some steps to migrate. For details, check [**Migration From Periodic Inventory**](docs.user.stock.periodic_to_perpetual.html)
diff --git a/selling/doctype/lead/lead.txt b/selling/doctype/lead/lead.txt
index a429c96..d96880b 100644
--- a/selling/doctype/lead/lead.txt
+++ b/selling/doctype/lead/lead.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-04-10 11:45:37", 
   "docstatus": 0, 
-  "modified": "2013-09-10 10:52:20", 
+  "modified": "2013-09-19 10:38:58", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -158,11 +158,78 @@
   "doctype": "DocField", 
   "fieldname": "communication_history", 
   "fieldtype": "Section Break", 
-  "label": "Communication History", 
+  "label": "Communication", 
   "options": "icon-comments", 
   "print_hide": 1
  }, 
  {
+  "default": "__user", 
+  "doctype": "DocField", 
+  "fieldname": "lead_owner", 
+  "fieldtype": "Link", 
+  "in_filter": 1, 
+  "label": "Lead Owner", 
+  "oldfieldname": "lead_owner", 
+  "oldfieldtype": "Link", 
+  "options": "Profile", 
+  "search_index": 1
+ }, 
+ {
+  "depends_on": "eval:!doc.__islocal", 
+  "description": "Date on which the lead was last contacted", 
+  "doctype": "DocField", 
+  "fieldname": "last_contact_date", 
+  "fieldtype": "Date", 
+  "label": "Last Contact Date", 
+  "no_copy": 1, 
+  "oldfieldname": "last_contact_date", 
+  "oldfieldtype": "Date", 
+  "print_hide": 1, 
+  "read_only": 1
+ }, 
+ {
+  "doctype": "DocField", 
+  "fieldname": "col_break123", 
+  "fieldtype": "Column Break", 
+  "width": "50%"
+ }, 
+ {
+  "allow_on_submit": 0, 
+  "description": "Your sales person who will contact the lead in future", 
+  "doctype": "DocField", 
+  "fieldname": "contact_by", 
+  "fieldtype": "Link", 
+  "hidden": 0, 
+  "in_filter": 1, 
+  "label": "Next Contact By", 
+  "oldfieldname": "contact_by", 
+  "oldfieldtype": "Link", 
+  "options": "Profile", 
+  "print_hide": 0, 
+  "reqd": 0, 
+  "width": "100px"
+ }, 
+ {
+  "allow_on_submit": 0, 
+  "description": "Your sales person will get a reminder on this date to contact the lead", 
+  "doctype": "DocField", 
+  "fieldname": "contact_date", 
+  "fieldtype": "Date", 
+  "in_filter": 1, 
+  "label": "Next Contact Date", 
+  "no_copy": 1, 
+  "oldfieldname": "contact_date", 
+  "oldfieldtype": "Date", 
+  "reqd": 0, 
+  "width": "100px"
+ }, 
+ {
+  "doctype": "DocField", 
+  "fieldname": "sec_break123", 
+  "fieldtype": "Section Break", 
+  "options": "Simple"
+ }, 
+ {
   "allow_on_submit": 0, 
   "doctype": "DocField", 
   "fieldname": "communication_html", 
@@ -275,18 +342,6 @@
   "options": "\nClient\nChannel Partner\nConsultant"
  }, 
  {
-  "default": "__user", 
-  "doctype": "DocField", 
-  "fieldname": "lead_owner", 
-  "fieldtype": "Link", 
-  "in_filter": 1, 
-  "label": "Lead Owner", 
-  "oldfieldname": "lead_owner", 
-  "oldfieldtype": "Link", 
-  "options": "Profile", 
-  "search_index": 1
- }, 
- {
   "doctype": "DocField", 
   "fieldname": "market_segment", 
   "fieldtype": "Select", 
@@ -348,49 +403,6 @@
   "options": "Quotation Lost Reason"
  }, 
  {
-  "allow_on_submit": 0, 
-  "description": "Your sales person who will contact the lead in future", 
-  "doctype": "DocField", 
-  "fieldname": "contact_by", 
-  "fieldtype": "Link", 
-  "hidden": 0, 
-  "in_filter": 1, 
-  "label": "Next Contact By", 
-  "oldfieldname": "contact_by", 
-  "oldfieldtype": "Link", 
-  "options": "Profile", 
-  "print_hide": 0, 
-  "reqd": 0, 
-  "width": "100px"
- }, 
- {
-  "allow_on_submit": 0, 
-  "description": "Your sales person will get a reminder on this date to contact the lead", 
-  "doctype": "DocField", 
-  "fieldname": "contact_date", 
-  "fieldtype": "Date", 
-  "in_filter": 1, 
-  "label": "Next Contact Date", 
-  "no_copy": 1, 
-  "oldfieldname": "contact_date", 
-  "oldfieldtype": "Date", 
-  "reqd": 0, 
-  "width": "100px"
- }, 
- {
-  "depends_on": "eval:!doc.__islocal", 
-  "description": "Date on which the lead was last contacted", 
-  "doctype": "DocField", 
-  "fieldname": "last_contact_date", 
-  "fieldtype": "Date", 
-  "label": "Last Contact Date", 
-  "no_copy": 1, 
-  "oldfieldname": "last_contact_date", 
-  "oldfieldtype": "Date", 
-  "print_hide": 1, 
-  "read_only": 1
- }, 
- {
   "doctype": "DocField", 
   "fieldname": "company", 
   "fieldtype": "Link", 
diff --git a/stock/doctype/item/item.py b/stock/doctype/item/item.py
index 2c702a6..9b69e9a 100644
--- a/stock/doctype/item/item.py
+++ b/stock/doctype/item/item.py
@@ -77,8 +77,8 @@
 				bin_list = webnotes.conn.sql("select * from tabBin where item_code=%s", 
 					self.doc.item_code, as_dict=1)
 				for bin in bin_list:
-					if bin.reserved_qty > 0 or bin.ordered_qty > 0 or bin.indented_qty > 0 \
-						or bin.planned_qty > 0 and cstr(bin.stock_uom) != cstr(self.doc.stock_uom):
+					if (bin.reserved_qty > 0 or bin.ordered_qty > 0 or bin.indented_qty > 0 \
+						or bin.planned_qty > 0) and cstr(bin.stock_uom) != cstr(self.doc.stock_uom):
 							matched = False
 							break
 						
diff --git a/stock/doctype/stock_entry/stock_entry.py b/stock/doctype/stock_entry/stock_entry.py
index 6a61461..1bb09db 100644
--- a/stock/doctype/stock_entry/stock_entry.py
+++ b/stock/doctype/stock_entry/stock_entry.py
@@ -886,7 +886,8 @@
 			"account": r.get("account"),
 			"against_invoice": r.get("against_invoice"),
 			"against_voucher": r.get("against_voucher"),
-			"balance": get_balance_on(r.get("account"), se.doc.posting_date)
+			"balance": get_balance_on(r.get("account"), se.doc.posting_date) \
+				if r.get("account") else 0
 		})
 		
 	return jv_list
diff --git a/utilities/transaction_base.py b/utilities/transaction_base.py
index ea807c2..2535db7 100644
--- a/utilities/transaction_base.py
+++ b/utilities/transaction_base.py
@@ -243,7 +243,8 @@
 			
 	def delete_events(self):
 		webnotes.delete_doc("Event", webnotes.conn.sql_list("""select name from `tabEvent` 
-			where ref_type=%s and ref_name=%s""", (self.doc.doctype, self.doc.name)))
+			where ref_type=%s and ref_name=%s""", (self.doc.doctype, self.doc.name)), 
+			ignore_permissions=True)
 			
 	def _add_calendar_event(self, opts):
 		opts = webnotes._dict(opts)