diff --git a/erpnext/setup/doctype/email_digest/email_digest.js b/erpnext/setup/doctype/email_digest/email_digest.js index 7af68a4920f..4d533ce10cc 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 6512f8c2b19..9c395c3a557 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 """ +