Merge pull request #1181 from anandpdoshi/hotfix-allow-roles

[minor] [fix] allow roles
diff --git a/buying/doctype/supplier_quotation/supplier_quotation.txt b/buying/doctype/supplier_quotation/supplier_quotation.txt
index cf62a9f..e532aa6 100644
--- a/buying/doctype/supplier_quotation/supplier_quotation.txt
+++ b/buying/doctype/supplier_quotation/supplier_quotation.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-05-21 16:16:45", 
   "docstatus": 0, 
-  "modified": "2013-11-22 17:16:16", 
+  "modified": "2013-12-14 17:27:47", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -632,6 +632,7 @@
   "cancel": 0, 
   "create": 0, 
   "doctype": "DocPerm", 
+  "match": "supplier", 
   "role": "Supplier", 
   "submit": 0, 
   "write": 0
diff --git a/config.json b/config.json
index 77a00b8..bb88d76 100644
--- a/config.json
+++ b/config.json
@@ -1,6 +1,6 @@
 {
  "app_name": "ERPNext", 
- "app_version": "3.2.1", 
+ "app_version": "3.2.3", 
  "base_template": "app/portal/templates/base.html", 
  "modules": {
   "Accounts": {
diff --git a/patches/1311/p07_scheduler_errors_digest.py b/patches/1311/p07_scheduler_errors_digest.py
new file mode 100644
index 0000000..7cfa251
--- /dev/null
+++ b/patches/1311/p07_scheduler_errors_digest.py
@@ -0,0 +1,23 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
+# License: GNU General Public License v3. See license.txt
+
+from __future__ import unicode_literals
+import webnotes
+
+def execute():
+	from webnotes.profile import get_system_managers
+	system_managers = get_system_managers(only_name=True)
+	if not system_managers: 
+		return
+	
+	# scheduler errors digest
+	edigest = webnotes.new_bean("Email Digest")
+	edigest.doc.fields.update({
+		"name": "Scheduler Errors",
+		"company": webnotes.conn.get_default("company"),
+		"frequency": "Daily",
+		"enabled": 1,
+		"recipient_list": "\n".join(system_managers),
+		"scheduler_errors": 1
+	})
+	edigest.insert()
\ No newline at end of file
diff --git a/patches/1311/p08_email_digest_recipients.py b/patches/1311/p08_email_digest_recipients.py
new file mode 100644
index 0000000..fad5408
--- /dev/null
+++ b/patches/1311/p08_email_digest_recipients.py
@@ -0,0 +1,11 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
+# License: GNU General Public License v3. See license.txt
+
+from __future__ import unicode_literals
+import webnotes
+
+def execute():
+	from webnotes.utils import extract_email_id
+	for name, recipients in webnotes.conn.sql("""select name, recipient_list from `tabEmail Digest`"""):
+		recipients = "\n".join([extract_email_id(r) for r in recipients.split("\n")])
+		webnotes.conn.set_value("Email Digest", name, "recipient_list", recipients)
\ No newline at end of file
diff --git a/patches/patch_list.py b/patches/patch_list.py
index 9400e08..9df0207 100644
--- a/patches/patch_list.py
+++ b/patches/patch_list.py
@@ -258,4 +258,6 @@
 	"execute:webnotes.delete_doc('Report', 'Stock Ledger') #2013-11-29",
 	"execute:webnotes.delete_doc('Report', 'Payment Collection With Ageing')",
 	"execute:webnotes.delete_doc('Report', 'Payment Made With Ageing')",
+	"patches.1311.p07_scheduler_errors_digest",
+	"patches.1311.p08_email_digest_recipients",
 ]
\ No newline at end of file
diff --git a/selling/doctype/quotation/quotation.txt b/selling/doctype/quotation/quotation.txt
index dc34272..93346d3 100644
--- a/selling/doctype/quotation/quotation.txt
+++ b/selling/doctype/quotation/quotation.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-05-24 19:29:08", 
   "docstatus": 0, 
-  "modified": "2013-11-27 17:57:19", 
+  "modified": "2013-12-14 17:25:46", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -863,6 +863,7 @@
   "cancel": 0, 
   "create": 0, 
   "doctype": "DocPerm", 
+  "match": "customer", 
   "role": "Customer", 
   "submit": 0, 
   "write": 0
diff --git a/setup/doctype/email_digest/email_digest.js b/setup/doctype/email_digest/email_digest.js
index 3367a72..79cc681 100644
--- a/setup/doctype/email_digest/email_digest.js
+++ b/setup/doctype/email_digest/email_digest.js
@@ -70,8 +70,10 @@
 					check.checked = 1;
 					add_or_update = 'Update';
 				}
+				var fullname = wn.user.full_name(v.name);
+				if(fullname !== v.name) v.name = fullname + " <" + v.name + ">";
 				if(v.enabled==0) {
-					v.name = "<span style='color: red'>" + v.name + " (disabled user)</span>"
+					v.name = repl("<span style='color: red'> %(name)s (disabled user)</span>", {name: v.name});
 				}
 				var profile = $a($td(tab, i+1, 1), 'span', '', '', v.name);
 				//profile.onclick = function() { check.checked = !check.checked; }
diff --git a/setup/doctype/email_digest/email_digest.py b/setup/doctype/email_digest/email_digest.py
index 1078d03..082b92f 100644
--- a/setup/doctype/email_digest/email_digest.py
+++ b/setup/doctype/email_digest/email_digest.py
@@ -19,16 +19,16 @@
 	["Selling", ["new_leads", "new_enquiries", "new_quotations", "new_sales_orders"]], 
 	["Stock", ["new_delivery_notes",  "new_purchase_receipts", "new_stock_entries"]], 
 	["Support", ["new_communications", "new_support_tickets", "open_tickets"]], 
-	["Projects", ["new_projects"]]
+	["Projects", ["new_projects"]],
+	["System", ["scheduler_errors"]],
 ]
 
 user_specific_content = ["calendar_events", "todo_list"]
 
-digest_template = 	"""\
-<style>p.ed-indent { margin-right: 17px; }</style>
-<h2>%(digest)s</h2>
-<p style='color: grey'>%(date)s</p>
+digest_template = """<style>p.ed-indent { margin-right: 17px; }</style>
+<h2>%(name)s</h2>
 <h4>%(company)s</h4>
+<p style='color: grey'>%(date)s</p>
 <hr>
 %(with_value)s
 %(no_value)s
@@ -53,10 +53,10 @@
 
 	def get_profiles(self):
 		"""get list of profiles"""
-		import webnotes
 		profile_list = webnotes.conn.sql("""
 			select name, enabled from tabProfile
 			where docstatus=0 and name not in ('Administrator', 'Guest')
+			and user_type = "System User"
 			order by enabled desc, name asc""", as_dict=1)
 
 		if self.doc.recipient_list:
@@ -81,7 +81,9 @@
 				msg_for_this_receipient = self.get_msg_html(self.get_user_specific_content(user_id) + \
 					common_msg)
 				from webnotes.utils.email_lib import sendmail
-				sendmail(recipients=user_id, subject="[ERPNext] " + (self.doc.frequency + " Digest"),
+				sendmail(recipients=user_id, 
+					subject="[ERPNext] [{frequency} Digest] {name}".format(
+						frequency=self.doc.frequency, name=self.doc.name), 
 					msg=msg_for_this_receipient)
 			
 	def get_digest_msg(self):
@@ -123,7 +125,7 @@
 		if with_value:
 			with_value = "\n".join(with_value)
 		else:
-			with_value = "<p>There were no updates in the items selected for this digest.</p>"
+			with_value = "<p>There were no updates in the items selected for this digest.</p><hr>"
 		
 		# seperate out no value items
 		no_value = [o[1] for o in out if not o[0]]
@@ -138,7 +140,8 @@
 				"date": date,
 				"company": self.doc.company,
 				"with_value": with_value,
-				"no_value": no_value or ""
+				"no_value": no_value or "",
+				"name": self.doc.name
 			}
 		
 		return msg
@@ -241,7 +244,7 @@
 		return self.get_new_count("Lead", self.meta.get_label("new_leads"))
 		
 	def get_new_enquiries(self):
-		return self.get_new_count("Opportunity", self.meta.get_label("new_enquiries"))
+		return self.get_new_count("Opportunity", self.meta.get_label("new_enquiries"), docstatus=1)
 	
 	def get_new_quotations(self):
 		return self.get_new_sum("Quotation", self.meta.get_label("new_quotations"), "grand_total")
@@ -253,7 +256,8 @@
 		return self.get_new_sum("Delivery Note", self.meta.get_label("new_delivery_notes"), "grand_total")
 		
 	def get_new_purchase_requests(self):
-		return self.get_new_count("Material Request", self.meta.get_label("new_purchase_requests"))
+		return self.get_new_count("Material Request",
+			 self.meta.get_label("new_purchase_requests"), docstatus=1)
 		
 	def get_new_supplier_quotations(self):
 		return self.get_new_sum("Supplier Quotation", self.meta.get_label("new_supplier_quotations"),
@@ -271,13 +275,16 @@
 		return self.get_new_sum("Stock Entry", self.meta.get_label("new_stock_entries"), "total_amount")
 		
 	def get_new_support_tickets(self):
-		return self.get_new_count("Support Ticket", self.meta.get_label("new_support_tickets"), False)
+		return self.get_new_count("Support Ticket", self.meta.get_label("new_support_tickets"), 
+			filter_by_company=False)
 		
 	def get_new_communications(self):
-		return self.get_new_count("Communication", self.meta.get_label("new_communications"), False)
+		return self.get_new_count("Communication", self.meta.get_label("new_communications"), 
+			filter_by_company=False)
 		
 	def get_new_projects(self):
-		return self.get_new_count("Project", self.meta.get_label("new_projects"), False)
+		return self.get_new_count("Project", self.meta.get_label("new_projects"), 
+			filter_by_company=False)
 		
 	def get_calendar_events(self, user_id):
 		from core.doctype.event.event import get_events
@@ -321,22 +328,22 @@
 		else:
 			return 0, "<p>To Do</p>"
 	
-	def get_new_count(self, doctype, label, filter_by_company=True):
+	def get_new_count(self, doctype, label, docstatus=0, filter_by_company=True):
 		if filter_by_company:
 			company = """and company="%s" """ % self.doc.company
 		else:
 			company = ""
 		count = webnotes.conn.sql("""select count(*) from `tab%s`
-			where docstatus < 2 %s and
-			date(creation)>=%s and date(creation)<=%s""" % (doctype, company, "%s", "%s"),
-			(self.from_date, self.to_date))
+			where docstatus=%s %s and
+			date(creation)>=%s and date(creation)<=%s""" % 
+			(doctype, docstatus, company, "%s", "%s"), (self.from_date, self.to_date))
 		count = count and count[0][0] or 0
 		
 		return count, self.get_html(label, None, count)
 		
 	def get_new_sum(self, doctype, label, sum_field):
 		count_sum = webnotes.conn.sql("""select count(*), sum(ifnull(`%s`, 0))
-			from `tab%s` where docstatus < 2 and company = %s and
+			from `tab%s` where docstatus=1 and company = %s and
 			date(creation)>=%s and date(creation)<=%s""" % (sum_field, doctype, "%s",
 			"%s", "%s"), (self.doc.company, self.from_date, self.to_date))
 		count, total = count_sum and count_sum[0] or (0, 0)
@@ -448,6 +455,10 @@
 				t for t in open_tickets])
 		else:
 			return 0, "No Open Tickets!"
+			
+	def get_scheduler_errors(self):
+		import webnotes.utils.scheduler
+		return webnotes.utils.scheduler.get_error_report(self.from_date, self.to_date)
 	
 	def onload(self):
 		self.get_next_sending()
@@ -466,4 +477,4 @@
 			where enabled=1 and docstatus<2""", as_list=1):
 		ed_obj = get_obj('Email Digest', ed[0])
 		if (now_date == ed_obj.get_next_sending()):
-			ed_obj.send()
+			ed_obj.send()
\ No newline at end of file
diff --git a/setup/doctype/email_digest/email_digest.txt b/setup/doctype/email_digest/email_digest.txt
index f71d82a..b04885a 100644
--- a/setup/doctype/email_digest/email_digest.txt
+++ b/setup/doctype/email_digest/email_digest.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-02-21 14:15:31", 
   "docstatus": 0, 
-  "modified": "2013-07-05 14:36:13", 
+  "modified": "2013-12-16 12:37:43", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -100,11 +100,10 @@
   "label": "Add/Remove Recipients"
  }, 
  {
-  "description": "Check all the items below that you want to send in this digest.", 
   "doctype": "DocField", 
-  "fieldname": "select_digest_content", 
+  "fieldname": "accounts", 
   "fieldtype": "Section Break", 
-  "label": "Select Digest Content"
+  "label": "Accounts"
  }, 
  {
   "doctype": "DocField", 
@@ -178,7 +177,7 @@
   "doctype": "DocField", 
   "fieldname": "section_break_20", 
   "fieldtype": "Section Break", 
-  "options": "Simple"
+  "label": "Buying & Selling"
  }, 
  {
   "doctype": "DocField", 
@@ -236,6 +235,12 @@
  }, 
  {
   "doctype": "DocField", 
+  "fieldname": "section_break_34", 
+  "fieldtype": "Section Break", 
+  "label": "Inventory & Support"
+ }, 
+ {
+  "doctype": "DocField", 
   "fieldname": "stock_module", 
   "fieldtype": "Column Break", 
   "label": "Stock"
@@ -260,12 +265,6 @@
  }, 
  {
   "doctype": "DocField", 
-  "fieldname": "section_break_34", 
-  "fieldtype": "Section Break", 
-  "options": "Simple"
- }, 
- {
-  "doctype": "DocField", 
   "fieldname": "support_module", 
   "fieldtype": "Column Break", 
   "label": "Support"
@@ -290,6 +289,12 @@
  }, 
  {
   "doctype": "DocField", 
+  "fieldname": "section_break_40", 
+  "fieldtype": "Section Break", 
+  "label": "Projects & System"
+ }, 
+ {
+  "doctype": "DocField", 
   "fieldname": "projects_module", 
   "fieldtype": "Column Break", 
   "label": "Projects"
@@ -302,7 +307,25 @@
  }, 
  {
   "doctype": "DocField", 
-  "fieldname": "utilities_module", 
+  "fieldname": "core_module", 
+  "fieldtype": "Column Break", 
+  "label": "System"
+ }, 
+ {
+  "doctype": "DocField", 
+  "fieldname": "scheduler_errors", 
+  "fieldtype": "Check", 
+  "label": "Scheduler Failed Events"
+ }, 
+ {
+  "doctype": "DocField", 
+  "fieldname": "user_specific", 
+  "fieldtype": "Section Break", 
+  "label": "User Specific"
+ }, 
+ {
+  "doctype": "DocField", 
+  "fieldname": "general", 
   "fieldtype": "Column Break", 
   "label": "General"
  }, 
@@ -319,6 +342,12 @@
   "label": "To Do List"
  }, 
  {
+  "doctype": "DocField", 
+  "fieldname": "stub", 
+  "fieldtype": "Column Break", 
+  "label": "Stub"
+ }, 
+ {
   "cancel": 1, 
   "create": 1, 
   "doctype": "DocPerm", 
diff --git a/setup/doctype/naming_series/naming_series.py b/setup/doctype/naming_series/naming_series.py
index 234a689..3a14d41 100644
--- a/setup/doctype/naming_series/naming_series.py
+++ b/setup/doctype/naming_series/naming_series.py
@@ -22,7 +22,7 @@
 					where fieldname='naming_series'""")
 				)))),
 			"prefixes": "\n".join([''] + [i[0] for i in 
-				webnotes.conn.sql("""select name from tabSeries""")])
+				webnotes.conn.sql("""select name from tabSeries order by name""")])
 		}
 	
 	def scrub_options_list(self, ol):
@@ -38,7 +38,7 @@
 		self.set_series_for(self.doc.select_doc_for_series, series_list)
 		
 		# create series
-		map(self.insert_series, series_list)
+		map(self.insert_series, [d.split('.')[0] for d in series_list])
 		
 		msgprint('Series Updated')
 		
@@ -103,7 +103,8 @@
 			dt.validate_series(series, self.doc.select_doc_for_series)
 			for i in sr:
 				if i[0]:
-					if series in i[0].split("\n"):
+					existing_series = [d.split('.')[0] for d in i[0].split("\n")]
+					if series.split(".")[0] in existing_series:
 						msgprint("Oops! Series name %s is already in use in %s. \
 							Please select a new one" % (series, i[1]), raise_exception=1)
 			
@@ -120,17 +121,21 @@
 
 	def get_current(self, arg=None):
 		"""get series current"""
-		self.doc.current_value = webnotes.conn.get_value("Series", self.doc.prefix, "current")
+		self.doc.current_value = webnotes.conn.get_value("Series", 
+			self.doc.prefix.split('.')[0], "current")
 
 	def insert_series(self, series):
 		"""insert series if missing"""
 		if not webnotes.conn.exists('Series', series):
-			webnotes.conn.sql("insert into tabSeries (name, current) values (%s,0)", (series))			
+			webnotes.conn.sql("insert into tabSeries (name, current) values (%s, 0)", 
+				(series))			
 
 	def update_series_start(self):
 		if self.doc.prefix:
-			self.insert_series(self.doc.prefix)
-			webnotes.conn.sql("update `tabSeries` set current = '%s' where name = '%s'" % (self.doc.current_value,self.doc.prefix))
+			prefix = self.doc.prefix.split('.')[0]
+			self.insert_series(prefix)
+			webnotes.conn.sql("update `tabSeries` set current = %s where name = %s", 
+				(self.doc.current_value, prefix))
 			msgprint("Series Updated Successfully")
 		else:
 			msgprint("Please select prefix first")
diff --git a/setup/page/setup_wizard/setup_wizard.py b/setup/page/setup_wizard/setup_wizard.py
index bf14905..b5133ef 100644
--- a/setup/page/setup_wizard/setup_wizard.py
+++ b/setup/page/setup_wizard/setup_wizard.py
@@ -171,7 +171,7 @@
 
 def create_email_digest():
 	from webnotes.profile import get_system_managers
-	system_managers = get_system_managers()
+	system_managers = get_system_managers(only_name=True)
 	if not system_managers: 
 		return
 	
@@ -186,10 +186,23 @@
 			})
 
 			for fieldname in edigest.meta.get_fieldnames({"fieldtype": "Check"}):
-				edigest.doc.fields[fieldname] = 1
+				if fieldname != "scheduler_errors":
+					edigest.doc.fields[fieldname] = 1
 		
 			edigest.insert()
-		
+	
+	# scheduler errors digest
+	edigest = webnotes.new_bean("Email Digest")
+	edigest.doc.fields.update({
+		"name": "Scheduler Errors",
+		"company": webnotes.conn.get_default("company"),
+		"frequency": "Daily",
+		"recipient_list": "\n".join(system_managers),
+		"scheduler_errors": 1,
+		"enabled": 1
+	})
+	edigest.insert()
+	
 def get_fy_details(fy_start_date, fy_end_date):
 	start_year = getdate(fy_start_date).year
 	if start_year == getdate(fy_end_date).year:
diff --git a/startup/bean_handlers.py b/startup/bean_handlers.py
index 06d2734..678c8aa 100644
--- a/startup/bean_handlers.py
+++ b/startup/bean_handlers.py
@@ -13,4 +13,5 @@
 		clear_doctype_notifications(bean.controller, method)
 
 	if bean.doc.doctype=="Stock Entry" and method in ("on_submit", "on_cancel"):
-		update_completed_qty(bean.controller, method)
\ No newline at end of file
+		update_completed_qty(bean.controller, method)
+	
\ No newline at end of file
diff --git a/startup/schedule_handlers.py b/startup/schedule_handlers.py
index cdb0e5d..252a091 100644
--- a/startup/schedule_handlers.py
+++ b/startup/schedule_handlers.py
@@ -34,10 +34,6 @@
 	from core.doctype.notification_count.notification_count import delete_notification_count_for
 	delete_notification_count_for("Event")
 	
-	# email digest
-	from setup.doctype.email_digest.email_digest import send
-	run_fn(send)
-
 	# run recurring invoices
 	from accounts.doctype.sales_invoice.sales_invoice import manage_recurring_invoices
 	run_fn(manage_recurring_invoices)
@@ -53,10 +49,11 @@
 	# check reorder level
 	from stock.utils import reorder_item
 	run_fn(reorder_item)
+	
+	# email digest
+	from setup.doctype.email_digest.email_digest import send
+	run_fn(send)
 		
-	# scheduler error
-	scheduler.report_errors()
-
 def execute_weekly():
 	from setup.doctype.backup_manager.backup_manager import take_backups_weekly
 	run_fn(take_backups_weekly)
diff --git a/stock/doctype/delivery_note/delivery_note.txt b/stock/doctype/delivery_note/delivery_note.txt
index 1dc3cd3..480d45d 100644
--- a/stock/doctype/delivery_note/delivery_note.txt
+++ b/stock/doctype/delivery_note/delivery_note.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-05-24 19:29:09", 
   "docstatus": 0, 
-  "modified": "2013-12-09 16:24:08", 
+  "modified": "2013-12-14 17:26:12", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -1058,7 +1058,7 @@
  }, 
  {
   "doctype": "DocPerm", 
-  "match": "customer_name", 
+  "match": "customer", 
   "role": "Customer"
  }
 ]
\ No newline at end of file
diff --git a/support/doctype/support_ticket/support_ticket.txt b/support/doctype/support_ticket/support_ticket.txt
index 684c809..4fa4874 100644
--- a/support/doctype/support_ticket/support_ticket.txt
+++ b/support/doctype/support_ticket/support_ticket.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-02-01 10:36:25", 
   "docstatus": 0, 
-  "modified": "2013-11-02 14:06:26", 
+  "modified": "2013-12-14 17:27:02", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -278,6 +278,7 @@
  {
   "cancel": 0, 
   "doctype": "DocPerm", 
+  "match": "customer", 
   "role": "Customer"
  }, 
  {