ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
| 2 | import frappe |
| 3 | import frappe.utils |
| 4 | import frappe.defaults |
Nabin Hait | 91fb661 | 2014-09-05 14:56:05 +0530 | [diff] [blame] | 5 | from frappe.utils import cint, cstr, getdate, nowdate, get_first_day, get_last_day |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 6 | from frappe.model.naming import make_autoname |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 7 | from frappe import _, msgprint, throw |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 8 | |
ankitjavalkarwork | e60822b | 2014-08-26 14:29:06 +0530 | [diff] [blame] | 9 | month_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6, 'Yearly': 12} |
| 10 | |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 11 | def create_recurring_documents(): |
| 12 | manage_recurring_documents("Sales Order") |
| 13 | manage_recurring_documents("Sales Invoice") |
| 14 | |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 15 | def manage_recurring_documents(doctype, next_date=None, commit=True): |
| 16 | """ |
| 17 | Create recurring documents on specific date by copying the original one |
| 18 | and notify the concerned people |
| 19 | """ |
| 20 | next_date = next_date or nowdate() |
ankitjavalkarwork | e60822b | 2014-08-26 14:29:06 +0530 | [diff] [blame] | 21 | |
| 22 | if doctype == "Sales Order": |
| 23 | date_field = "transaction_date" |
| 24 | elif doctype == "Sales Invoice": |
| 25 | date_field = "posting_date" |
| 26 | |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 27 | recurring_documents = frappe.db.sql("""select name, recurring_id |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 28 | from `tab{}` where ifnull(is_recurring, 0)=1 |
| 29 | and docstatus=1 and next_date='{}' |
| 30 | and next_date <= ifnull(end_date, '2199-12-31')""".format(doctype, next_date)) |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 31 | |
| 32 | exception_list = [] |
| 33 | for ref_document, recurring_id in recurring_documents: |
| 34 | if not frappe.db.sql("""select name from `tab%s` |
ankitjavalkarwork | e60822b | 2014-08-26 14:29:06 +0530 | [diff] [blame] | 35 | where %s=%s and recurring_id=%s and docstatus=1""" |
| 36 | % (doctype, date_field, '%s', '%s'), (next_date, recurring_id)): |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 37 | try: |
| 38 | ref_wrapper = frappe.get_doc(doctype, ref_document) |
Anand Doshi | 01d1717 | 2014-09-15 12:50:37 +0530 | [diff] [blame] | 39 | if hasattr(ref_wrapper, "before_recurring"): |
| 40 | ref_wrapper.before_recurring() |
| 41 | |
ankitjavalkarwork | e60822b | 2014-08-26 14:29:06 +0530 | [diff] [blame] | 42 | new_document_wrapper = make_new_document(ref_wrapper, date_field, next_date) |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 43 | send_notification(new_document_wrapper) |
| 44 | if commit: |
| 45 | frappe.db.commit() |
| 46 | except: |
| 47 | if commit: |
| 48 | frappe.db.rollback() |
| 49 | |
| 50 | frappe.db.begin() |
| 51 | frappe.db.sql("update `tab%s` \ |
Nabin Hait | 91fb661 | 2014-09-05 14:56:05 +0530 | [diff] [blame] | 52 | set is_recurring = 0 where name = %s" % (doctype, '%s'), |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 53 | (ref_document)) |
| 54 | notify_errors(ref_document, doctype, ref_wrapper.customer, ref_wrapper.owner) |
| 55 | frappe.db.commit() |
| 56 | |
| 57 | exception_list.append(frappe.get_traceback()) |
| 58 | finally: |
| 59 | if commit: |
| 60 | frappe.db.begin() |
| 61 | |
| 62 | if exception_list: |
| 63 | exception_message = "\n\n".join([cstr(d) for d in exception_list]) |
| 64 | frappe.throw(exception_message) |
| 65 | |
ankitjavalkarwork | e60822b | 2014-08-26 14:29:06 +0530 | [diff] [blame] | 66 | def make_new_document(ref_wrapper, date_field, posting_date): |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 67 | from erpnext.accounts.utils import get_fiscal_year |
| 68 | new_document = frappe.copy_doc(ref_wrapper) |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 69 | mcount = month_map[ref_wrapper.recurring_type] |
| 70 | |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 71 | from_date = get_next_date(ref_wrapper.from_date, mcount) |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 72 | |
| 73 | # get last day of the month to maintain period if the from date is first day of its own month |
| 74 | # and to date is the last day of its own month |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 75 | if (cstr(get_first_day(ref_wrapper.from_date)) == \ |
| 76 | cstr(ref_wrapper.from_date)) and \ |
| 77 | (cstr(get_last_day(ref_wrapper.to_date)) == \ |
| 78 | cstr(ref_wrapper.to_date)): |
| 79 | to_date = get_last_day(get_next_date(ref_wrapper.to_date, |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 80 | mcount)) |
| 81 | else: |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 82 | to_date = get_next_date(ref_wrapper.to_date, mcount) |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 83 | |
| 84 | new_document.update({ |
ankitjavalkarwork | e60822b | 2014-08-26 14:29:06 +0530 | [diff] [blame] | 85 | date_field: posting_date, |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 86 | "from_date": from_date, |
| 87 | "to_date": to_date, |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 88 | "fiscal_year": get_fiscal_year(posting_date)[0], |
| 89 | "owner": ref_wrapper.owner, |
| 90 | }) |
| 91 | |
| 92 | if ref_wrapper.doctype == "Sales Order": |
| 93 | new_document.update({ |
Nabin Hait | 91fb661 | 2014-09-05 14:56:05 +0530 | [diff] [blame] | 94 | "delivery_date": get_next_date(ref_wrapper.delivery_date, mcount, |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 95 | cint(ref_wrapper.repeat_on_day_of_month)) |
| 96 | }) |
| 97 | |
| 98 | new_document.submit() |
Nabin Hait | 91fb661 | 2014-09-05 14:56:05 +0530 | [diff] [blame] | 99 | |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 100 | return new_document |
| 101 | |
| 102 | def get_next_date(dt, mcount, day=None): |
| 103 | dt = getdate(dt) |
| 104 | |
| 105 | from dateutil.relativedelta import relativedelta |
| 106 | dt += relativedelta(months=mcount, day=day) |
| 107 | |
| 108 | return dt |
| 109 | |
| 110 | def send_notification(new_rv): |
| 111 | """Notify concerned persons about recurring document generation""" |
ankitjavalkarwork | 737d8e4 | 2014-09-01 16:18:44 +0530 | [diff] [blame] | 112 | |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 113 | frappe.sendmail(new_rv.notification_email_address, |
| 114 | subject= _("New {0}: #{1}").format(new_rv.doctype, new_rv.name), |
| 115 | message = _("Please find attached {0} #{1}").format(new_rv.doctype, new_rv.name), |
| 116 | attachments = [{ |
| 117 | "fname": new_rv.name + ".pdf", |
ankitjavalkarwork | 737d8e4 | 2014-09-01 16:18:44 +0530 | [diff] [blame] | 118 | "fcontent": frappe.get_print_format(new_rv.doctype, new_rv.name, as_pdf=True) |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 119 | }]) |
| 120 | |
| 121 | def notify_errors(doc, doctype, customer, owner): |
| 122 | from frappe.utils.user import get_system_managers |
| 123 | recipients = get_system_managers(only_name=True) |
| 124 | |
| 125 | frappe.sendmail(recipients + [frappe.db.get_value("User", owner, "email")], |
| 126 | subject="[Urgent] Error while creating recurring %s for %s" % (doctype, doc), |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 127 | message = frappe.get_template("templates/emails/recurring_document_failed.html").render({ |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 128 | "type": doctype, |
| 129 | "name": doc, |
| 130 | "customer": customer |
ankitjavalkarwork | ac085e0 | 2014-08-26 10:40:23 +0530 | [diff] [blame] | 131 | })) |
| 132 | |
| 133 | assign_task_to_owner(doc, doctype, "Recurring Invoice Failed", recipients) |
| 134 | |
| 135 | def assign_task_to_owner(doc, doctype, msg, users): |
| 136 | for d in users: |
| 137 | from frappe.widgets.form import assign_to |
| 138 | args = { |
| 139 | 'assign_to' : d, |
| 140 | 'doctype' : doctype, |
| 141 | 'name' : doc, |
| 142 | 'description' : msg, |
| 143 | 'priority' : 'High' |
| 144 | } |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 145 | assign_to.add(args) |
| 146 | |
| 147 | def validate_recurring_document(doc): |
| 148 | if doc.is_recurring: |
| 149 | validate_notification_email_id(doc) |
| 150 | |
| 151 | if not doc.recurring_type: |
| 152 | msgprint(_("Please select {0}").format(doc.meta.get_label("recurring_type")), |
| 153 | raise_exception=1) |
| 154 | |
| 155 | elif not (doc.from_date and doc.to_date): |
| 156 | throw(_("Period From and Period To dates mandatory for recurring %s") % doc.doctype) |
| 157 | |
| 158 | def convert_to_recurring(doc, autoname, posting_date): |
| 159 | if doc.is_recurring: |
| 160 | if not doc.recurring_id: |
| 161 | frappe.db.set(doc, "recurring_id", |
| 162 | make_autoname(autoname)) |
| 163 | |
| 164 | set_next_date(doc, posting_date) |
| 165 | |
| 166 | elif doc.recurring_id: |
| 167 | frappe.db.sql("""update `tab%s` |
| 168 | set is_recurring = 0 |
| 169 | where recurring_id = %s""" % (doc.doctype, '%s'), (doc.recurring_id)) |
| 170 | |
| 171 | def validate_notification_email_id(doc): |
| 172 | if doc.notification_email_address: |
| 173 | email_list = filter(None, [cstr(email).strip() for email in |
| 174 | doc.notification_email_address.replace("\n", "").split(",")]) |
| 175 | |
| 176 | from frappe.utils import validate_email_add |
| 177 | for email in email_list: |
| 178 | if not validate_email_add(email): |
| 179 | throw(_("{0} is an invalid email address in 'Notification \ |
| 180 | Email Address'").format(email)) |
| 181 | |
| 182 | else: |
| 183 | frappe.throw(_("'Notification Email Addresses' not specified for recurring %s") \ |
| 184 | % doc.doctype) |
| 185 | |
| 186 | def set_next_date(doc, posting_date): |
| 187 | """ Set next date on which recurring document will be created""" |
Nabin Hait | 91fb661 | 2014-09-05 14:56:05 +0530 | [diff] [blame] | 188 | |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 189 | if not doc.repeat_on_day_of_month: |
| 190 | msgprint(_("Please enter 'Repeat on Day of Month' field value"), raise_exception=1) |
| 191 | |
Nabin Hait | 91fb661 | 2014-09-05 14:56:05 +0530 | [diff] [blame] | 192 | next_date = get_next_date(posting_date, month_map[doc.recurring_type], |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 193 | cint(doc.repeat_on_day_of_month)) |
| 194 | |
ankitjavalkarwork | 737d8e4 | 2014-09-01 16:18:44 +0530 | [diff] [blame] | 195 | frappe.db.set(doc, 'next_date', next_date) |
| 196 | |
| 197 | msgprint(_("Next Recurring {0} will be created on {1}").format(doc.doctype, next_date)) |