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