From 0bf801af62c9085326f1bbdb5bbad88924354fc7 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Fri, 15 Feb 2013 18:27:05 +0530 Subject: [PATCH] email to leave approver and employee --- .../leave_application/leave_application.py | 86 ++++++++++++++++--- utilities/page/messages/messages.py | 28 +++--- 2 files changed, 90 insertions(+), 24 deletions(-) diff --git a/hr/doctype/leave_application/leave_application.py b/hr/doctype/leave_application/leave_application.py index bc685dfd55..71fa424a43 100755 --- a/hr/doctype/leave_application/leave_application.py +++ b/hr/doctype/leave_application/leave_application.py @@ -18,19 +18,19 @@ from __future__ import unicode_literals import webnotes from webnotes import _ -from webnotes.utils import cint, cstr, date_diff, flt, formatdate, getdate -from webnotes.model import db_exists -from webnotes.model.wrapper import copy_doclist -from webnotes import form, msgprint - -import datetime +from webnotes.utils import cint, cstr, date_diff, flt, formatdate, getdate, get_url_to_form, get_fullname +from webnotes import msgprint +from webnotes.utils.email_lib import sendmail class LeaveDayBlockedError(Exception): pass -class DocType: - def __init__(self, doc, doclist): - self.doc = doc - self.doclist = doclist +from webnotes.model.controller import DocListController +class DocType(DocListController): + def setup(self): + if webnotes.conn.exists(self.doc.doctype, self.doc.name): + self.previous_doc = webnotes.doc(self.doc.doctype, self.doc.name) + else: + self.previous_doc = None def validate(self): # if self.doc.leave_approver == self.doc.owner: @@ -39,12 +39,29 @@ class DocType: self.validate_leave_overlap() self.validate_max_days() self.validate_block_days() + + def on_update(self): + if (not self.previous_doc and self.doc.leave_approver) or (self.doc.status == "Open" \ + and self.previous_doc.leave_approver != self.doc.leave_approver): + # notify leave approver about creation + self.notify_leave_approver() + elif self.previous_doc and \ + self.previous_doc.status == "Open" and self.doc.status == "Rejected": + # notify employee about rejection + self.notify_employee(self.doc.status) def on_submit(self): if self.doc.status != "Approved": webnotes.msgprint("""Only Leave Applications with status 'Approved' can be Submitted.""", raise_exception=True) + # notify leave applier about approval + self.notify_employee(self.doc.status) + + def on_cancel(self): + # notify leave applier about cancellation + self.notify_employee("cancelled") + def validate_block_days(self): for block_list in self.get_applicable_block_lists(): self.check_block_dates(block_list) @@ -148,7 +165,54 @@ class DocType: if max_days and self.doc.total_leave_days > max_days: msgprint("Sorry ! You cannot apply for %s for more than %s days" % (self.doc.leave_type, max_days)) raise Exception - + + def notify_employee(self, status): + employee = webnotes.doc("Employee", self.doc.employee) + if not employee.user_id: + return + + def _get_message(url=False): + if url: + name = get_url_to_form(self.doc.doctype, self.doc.name) + else: + name = self.doc.name + + return (_("Leave Application") + ": %s - %s") % (name, _(status)) + + self.notify({ + # for post in messages + "message": _get_message(url=True), + "message_to": employee.user_id, + + "subject": _get_message(), + }) + + def notify_leave_approver(self): + employee = webnotes.doc("Employee", self.doc.employee) + + def _get_message(url=False): + name = self.doc.name + employee_name = get_fullname(employee.user_id) + if url: + name = get_url_to_form(self.doc.doctype, self.doc.name) + employee_name = get_url_to_form("Employee", self.doc.employee, label=employee_name) + + return (_("New Leave Application") + ": %s - " + _("Employee") + ": %s") % (name, employee_name) + + self.notify({ + # for post in messages + "message": _get_message(url=True), + "message_to": self.doc.leave_approver, + + # for email + "subject": _get_message() + }) + + def notify(self, args): + args = webnotes._dict(args) + from utilities.page.messages.messages import post + post({"txt": args.message, "contact": args.message_to, "subject": args.subject, + "notify": True}) @webnotes.whitelist() def get_leave_balance(employee, leave_type, fiscal_year): diff --git a/utilities/page/messages/messages.py b/utilities/page/messages/messages.py index 019cd071ea..90f5e9152f 100644 --- a/utilities/page/messages/messages.py +++ b/utilities/page/messages/messages.py @@ -66,12 +66,14 @@ def get_active_users(arg=None): def post(arg=None): import webnotes """post message""" - if arg: - import json - arg = json.loads(arg) - else: + if not arg: arg = {} arg.update(webnotes.form_dict) + + if isinstance(arg, basestring): + import json + arg = json.loads(arg) + from webnotes.model.doc import Document d = Document('Comment') d.parenttype = arg.get("parenttype") @@ -90,16 +92,13 @@ def delete(arg=None): webnotes.form_dict['name']); def notify(arg=None): - from webnotes.utils import cstr + from webnotes.utils import cstr, get_fullname from startup import get_url - fn = webnotes.conn.sql('select first_name, last_name from tabProfile where name=%s', webnotes.user.name)[0] - if fn[0] or f[1]: - fn = cstr(fn[0]) + (fn[0] and ' ' or '') + cstr(fn[1]) - else: - fn = webnotes.user.name - + fn = get_fullname(webnotes.user.name) or webnotes.user.name + url = get_url() + message = '''You have a message from %s: %s @@ -108,8 +107,11 @@ def notify(arg=None): %s ''' % (fn, arg['txt'], url, url) - sender = webnotes.user.name!='Administrator' and webnotes.user.name or 'support+admin_post@erpnext.com' + sender = webnotes.conn.get_value("Profile", webnotes.user.name, "email") \ + or webnotes.user.name + recipient = [webnotes.conn.get_value("Profile", arg["contact"], "email") \ + or arg["contact"]] from webnotes.utils.email_lib import sendmail - sendmail([arg['contact']], sender, message, "You have a message from %s" % (fn,)) + sendmail(recipient, sender, message, arg.get("subject") or "You have a message from %s" % (fn,)) \ No newline at end of file