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