From 41e542ce4d947325edf3381f4ad213f02a54bdf6 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Tue, 6 Dec 2011 10:41:54 +0530 Subject: [PATCH] Email Digest changes --- .../doctype/email_digest/email_digest.js | 9 ++ .../doctype/email_digest/email_digest.py | 96 +++++++++++++++++-- .../doctype/email_digest/email_digest.txt | 37 +++---- 3 files changed, 114 insertions(+), 28 deletions(-) diff --git a/erpnext/setup/doctype/email_digest/email_digest.js b/erpnext/setup/doctype/email_digest/email_digest.js index 7af68a4920..4d533ce10c 100644 --- a/erpnext/setup/doctype/email_digest/email_digest.js +++ b/erpnext/setup/doctype/email_digest/email_digest.js @@ -8,6 +8,15 @@ cur_frm.cscript.refresh = function(doc, dt, dn) { } }); }, 1); + cur_frm.add_custom_button('Send Now', function() { + $c_obj(make_doclist(dt, dn), 'send', '', function(r, rt) { + if(r.exc) { + msgprint(r.exc); + } else { + console.log(arguments); + } + }); + }, 1); } cur_frm.cscript['Add Recipients'] = function(doc, dt, dn) { diff --git a/erpnext/setup/doctype/email_digest/email_digest.py b/erpnext/setup/doctype/email_digest/email_digest.py index 6512f8c2b1..9c395c3a55 100644 --- a/erpnext/setup/doctype/email_digest/email_digest.py +++ b/erpnext/setup/doctype/email_digest/email_digest.py @@ -2,7 +2,8 @@ import webnotes class DocType: def __init__(self, doc, doclist=[]): - self.doc, self.doclist = doc, doclist + self.doc, self.doclist = doc, doclist + self.sending = False def get_profiles(self): @@ -110,7 +111,7 @@ class DocType: for query in query_dict.keys(): if self.doc.fields[query]: #webnotes.msgprint(query) - res = webnotes.conn.sql(query_dict[query], as_dict=1) + res = webnotes.conn.sql(query_dict[query], as_dict=1, debug=1) if query == 'income': for r in res: r['value'] = float(r['credit'] - r['debit']) @@ -238,8 +239,41 @@ class DocType: """ Returns start and end date depending on the frequency of email digest """ - start_date = '2011-11-01' - end_date = '2011-11-31' + from datetime import datetime, date, timedelta + today = datetime.now().date() + year, month, day = today.year, today.month, today.day + + if self.doc.frequency == 'Daily': + if self.sending: + start_date = end_date = today - timedelta(days=1) + else: + start_date = end_date = today + + elif self.doc.frequency == 'Weekly': + if self.sending: + start_date = today - timedelta(weeks=1) + end_date = today - timedelta(days=1) + else: + start_date = today - timedelta(days=today.weekday()) + end_date = start_date + timedelta(weeks=1) + + else: + import calendar + + if self.sending: + if month == 1: + year = year - 1 + prev_month = 12 + else: + prev_month = month - 1 + start_date = date(year, prev_month, 1) + last_day = calendar.monthrange(year, prev_month)[1] + end_date = date(year, prev_month, last_day) + else: + start_date = date(year, month, 1) + last_day = calendar.monthrange(year, month)[1] + end_date = date(year, month, last_day) + return start_date, end_date @@ -281,9 +315,10 @@ class DocType: * Execute Query * Prepare Email Body from Print Format """ - result = self.execute_queries() + result, email_body = self.execute_queries() webnotes.msgprint(result) - return result + webnotes.msgprint(email_body) + return result, email_body def execute_queries(self): @@ -294,15 +329,27 @@ class DocType: result = {} if self.doc.use_standard==1: result = self.get_standard_data() + email_body = self.get_standard_body(result) else: - result = self.execute_custom_code() + result, email_body = self.execute_custom_code(self.doc) #webnotes.msgprint(result) - return result + return result, email_body - def execute_custom_code(self): + def get_standard_body(self, result): + """ + Generate email body depending on the result + """ + return """ +
+ Invoiced Amount: %(invoiced_amount)s
+ Payables: %(payables)s
+
""" % result + + + def execute_custom_code(self, doc): """ Execute custom python code """ @@ -314,4 +361,33 @@ class DocType: * Execute get method * Send email to recipients """ - pass + self.sending = True + result, email_body = self.get() + # TODO: before sending, check if user is disabled or not + from webnotes.utils.email_lib import sendmail + try: + sendmail( + recipients=self.doc.recipient_list.split("\n"), + sender='anand@erpnext.com', + reply_to='support@erpnext.com', + subject='Digest', + msg=email_body, + from_defs=1 + ) + except Exception, e: + webnotes.msgprint('There was a problem in sending your email. Please contact support@erpnext.com') + #webnotes.errprint(webnotes.getTraceback()) + + + def schedule(self): + """ + + """ + # Get TimeZone + # Get System TimeZone + import time + from pytz import timezone + server_tz = timezone(time.tzname[0]) + cp = webnotes.model.doc.Document('Control Panel','Control Panel') + app_tz = timezone(cp.time_zone) + diff --git a/erpnext/setup/doctype/email_digest/email_digest.txt b/erpnext/setup/doctype/email_digest/email_digest.txt index d57a04543e..34822ed74b 100644 --- a/erpnext/setup/doctype/email_digest/email_digest.txt +++ b/erpnext/setup/doctype/email_digest/email_digest.txt @@ -5,14 +5,14 @@ { 'creation': '2011-11-28 13:11:56', 'docstatus': 0, - 'modified': '2011-12-05 11:55:31', + 'modified': '2011-12-05 18:54:27', 'modified_by': 'Administrator', 'owner': 'Administrator' }, # These values are common for all DocType { - '_last_update': '1323066269', + '_last_update': '1323083188', 'autoname': 'Prompt', 'colour': 'White:FFF', 'doctype': 'DocType', @@ -21,7 +21,7 @@ 'name': '__common__', 'section_style': 'Simple', 'show_in_menu': 0, - 'version': 51 + 'version': 61 }, # These values are common for all DocField @@ -114,12 +114,10 @@ # DocField { 'doctype': 'DocField', - 'fieldname': 'send_time', - 'fieldtype': 'Select', - 'label': 'At what time?', - 'options': '\n1 AM\n2 AM\n3 AM\n4 AM\n5 AM\n6 AM\n7 AM\n8 AM\n9 AM\n10 AM\n11 AM\nNoon\n1 PM\n2 PM\n3 PM\n4 PM\n5 PM\n6 PM\n7 PM\n8 PM\n9 PM\n10 PM\n11 PM\nMidnight', - 'permlevel': 0, - 'reqd': 1 + 'fieldname': 'next_send_datetime', + 'fieldtype': 'Text', + 'label': 'Next email will be sent on:', + 'permlevel': 1 }, # DocField @@ -128,6 +126,7 @@ 'doctype': 'DocField', 'fieldname': 'use_standard', 'fieldtype': 'Check', + 'hidden': 1, 'label': 'Use standard?', 'permlevel': 0 }, @@ -309,21 +308,13 @@ # DocField { + 'depends_on': 'eval:!doc.use_standard', 'doctype': 'DocField', 'fieldtype': 'Section Break', 'label': 'Enter Custom Code', 'permlevel': 0 }, - # DocField - { - 'doctype': 'DocField', - 'fieldname': 'email_template', - 'fieldtype': 'Code', - 'label': 'Email Template', - 'permlevel': 0 - }, - # DocField { 'default': '\n', @@ -333,5 +324,15 @@ 'fieldtype': 'Code', 'label': 'Custom Python Code', 'permlevel': 0 + }, + + # DocField + { + 'depends_on': 'eval:!doc.use_standard', + 'doctype': 'DocField', + 'fieldname': 'email_template', + 'fieldtype': 'Code', + 'label': 'Email Template', + 'permlevel': 0 } ] \ No newline at end of file