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 |
Sambhaji Kolate | b14401c | 2014-09-11 16:09:05 +0530 | [diff] [blame] | 5 | |
Rushabh Mehta | 571a9d0 | 2016-03-03 15:03:23 +0530 | [diff] [blame] | 6 | from frappe.utils import cint, cstr, getdate, nowdate, \ |
| 7 | get_first_day, get_last_day, split_emails |
Sambhaji Kolate | b14401c | 2014-09-11 16:09:05 +0530 | [diff] [blame] | 8 | |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 9 | from frappe import _, msgprint, throw |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 10 | |
ankitjavalkarwork | e60822b | 2014-08-26 14:29:06 +0530 | [diff] [blame] | 11 | month_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6, 'Yearly': 12} |
Anand Doshi | 1394509 | 2014-09-21 19:45:49 +0530 | [diff] [blame] | 12 | date_field_map = { |
| 13 | "Sales Order": "transaction_date", |
| 14 | "Sales Invoice": "posting_date", |
| 15 | "Purchase Order": "transaction_date", |
| 16 | "Purchase Invoice": "posting_date" |
| 17 | } |
ankitjavalkarwork | e60822b | 2014-08-26 14:29:06 +0530 | [diff] [blame] | 18 | |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 19 | def create_recurring_documents(): |
| 20 | manage_recurring_documents("Sales Order") |
| 21 | manage_recurring_documents("Sales Invoice") |
Sambhaji Kolate | f37d433 | 2014-09-15 13:37:46 +0530 | [diff] [blame] | 22 | manage_recurring_documents("Purchase Order") |
| 23 | manage_recurring_documents("Purchase Invoice") |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 24 | |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 25 | def manage_recurring_documents(doctype, next_date=None, commit=True): |
| 26 | """ |
| 27 | Create recurring documents on specific date by copying the original one |
| 28 | and notify the concerned people |
| 29 | """ |
| 30 | next_date = next_date or nowdate() |
ankitjavalkarwork | e60822b | 2014-08-26 14:29:06 +0530 | [diff] [blame] | 31 | |
Anand Doshi | 1394509 | 2014-09-21 19:45:49 +0530 | [diff] [blame] | 32 | date_field = date_field_map[doctype] |
Anand Doshi | c5f919e | 2015-09-16 19:20:55 +0530 | [diff] [blame] | 33 | |
patilsangram | bf2b511 | 2016-02-22 16:24:23 +0530 | [diff] [blame] | 34 | condition = " and ifnull(status, '') != 'Closed'" if doctype in ("Sales Order", "Purchase Order") else "" |
ankitjavalkarwork | e60822b | 2014-08-26 14:29:06 +0530 | [diff] [blame] | 35 | |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 36 | recurring_documents = frappe.db.sql("""select name, recurring_id |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 37 | from `tab{0}` where is_recurring=1 |
ShashaQin | 8d1dea6 | 2016-02-27 14:00:58 +0800 | [diff] [blame] | 38 | and (docstatus=1 or docstatus=0) and next_date=%s |
Nabin Hait | 7580723 | 2015-07-02 14:41:27 +0530 | [diff] [blame] | 39 | and next_date <= ifnull(end_date, '2199-12-31') {1}""".format(doctype, condition), next_date) |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 40 | |
| 41 | exception_list = [] |
| 42 | for ref_document, recurring_id in recurring_documents: |
| 43 | if not frappe.db.sql("""select name from `tab%s` |
ShashaQin | 6071ddc | 2016-02-25 11:25:24 +0800 | [diff] [blame] | 44 | where %s=%s and recurring_id=%s and (docstatus=1 or docstatus=0)""" |
ankitjavalkarwork | e60822b | 2014-08-26 14:29:06 +0530 | [diff] [blame] | 45 | % (doctype, date_field, '%s', '%s'), (next_date, recurring_id)): |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 46 | try: |
Anand Doshi | 39e2c2b | 2016-01-25 17:03:50 +0530 | [diff] [blame] | 47 | reference_doc = frappe.get_doc(doctype, ref_document) |
| 48 | new_doc = make_new_document(reference_doc, date_field, next_date) |
ShashaQin | 6071ddc | 2016-02-25 11:25:24 +0800 | [diff] [blame] | 49 | if reference_doc.notify_by_email: |
| 50 | send_notification(new_doc) |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 51 | if commit: |
| 52 | frappe.db.commit() |
| 53 | except: |
| 54 | if commit: |
| 55 | frappe.db.rollback() |
| 56 | |
| 57 | frappe.db.begin() |
| 58 | frappe.db.sql("update `tab%s` \ |
Nabin Hait | 91fb661 | 2014-09-05 14:56:05 +0530 | [diff] [blame] | 59 | set is_recurring = 0 where name = %s" % (doctype, '%s'), |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 60 | (ref_document)) |
Anand Doshi | 39e2c2b | 2016-01-25 17:03:50 +0530 | [diff] [blame] | 61 | notify_errors(ref_document, doctype, reference_doc.get("customer") or reference_doc.get("supplier"), |
| 62 | reference_doc.owner) |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 63 | frappe.db.commit() |
| 64 | |
| 65 | exception_list.append(frappe.get_traceback()) |
| 66 | finally: |
| 67 | if commit: |
| 68 | frappe.db.begin() |
| 69 | |
| 70 | if exception_list: |
| 71 | exception_message = "\n\n".join([cstr(d) for d in exception_list]) |
| 72 | frappe.throw(exception_message) |
| 73 | |
Anand Doshi | 39e2c2b | 2016-01-25 17:03:50 +0530 | [diff] [blame] | 74 | def make_new_document(reference_doc, date_field, posting_date): |
Anand Doshi | 39e2c2b | 2016-01-25 17:03:50 +0530 | [diff] [blame] | 75 | new_document = frappe.copy_doc(reference_doc, ignore_no_copy=True) |
| 76 | mcount = month_map[reference_doc.recurring_type] |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 77 | |
Anand Doshi | 39e2c2b | 2016-01-25 17:03:50 +0530 | [diff] [blame] | 78 | from_date = get_next_date(reference_doc.from_date, mcount) |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 79 | |
| 80 | # get last day of the month to maintain period if the from date is first day of its own month |
| 81 | # and to date is the last day of its own month |
Anand Doshi | 39e2c2b | 2016-01-25 17:03:50 +0530 | [diff] [blame] | 82 | if (cstr(get_first_day(reference_doc.from_date)) == cstr(reference_doc.from_date)) and \ |
| 83 | (cstr(get_last_day(reference_doc.to_date)) == cstr(reference_doc.to_date)): |
| 84 | to_date = get_last_day(get_next_date(reference_doc.to_date, mcount)) |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 85 | else: |
Anand Doshi | 39e2c2b | 2016-01-25 17:03:50 +0530 | [diff] [blame] | 86 | to_date = get_next_date(reference_doc.to_date, mcount) |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 87 | |
| 88 | new_document.update({ |
ankitjavalkarwork | e60822b | 2014-08-26 14:29:06 +0530 | [diff] [blame] | 89 | date_field: posting_date, |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 90 | "from_date": from_date, |
| 91 | "to_date": to_date, |
Anand Doshi | c75c1d7 | 2016-03-11 14:56:19 +0530 | [diff] [blame] | 92 | "next_date": get_next_date(from_date, mcount,cint(reference_doc.repeat_on_day_of_month)) |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 93 | }) |
| 94 | |
Anand Doshi | 39e2c2b | 2016-01-25 17:03:50 +0530 | [diff] [blame] | 95 | # copy document fields |
| 96 | for fieldname in ("owner", "recurring_type", "repeat_on_day_of_month", |
| 97 | "recurring_id", "notification_email_address", "is_recurring", "end_date", |
| 98 | "title", "naming_series", "select_print_heading", "ignore_pricing_rule", |
Rushabh Mehta | 5839a8d | 2016-03-02 17:51:24 +0530 | [diff] [blame] | 99 | "posting_time", "remarks", 'submit_on_creation'): |
Anand Doshi | 39e2c2b | 2016-01-25 17:03:50 +0530 | [diff] [blame] | 100 | if new_document.meta.get_field(fieldname): |
| 101 | new_document.set(fieldname, reference_doc.get(fieldname)) |
| 102 | |
| 103 | # copy item fields |
| 104 | for i, item in enumerate(new_document.items): |
| 105 | for fieldname in ("page_break",): |
| 106 | item.set(fieldname, reference_doc.items[i].get(fieldname)) |
| 107 | |
| 108 | new_document.run_method("on_recurring", reference_doc=reference_doc) |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 109 | |
Rushabh Mehta | 5839a8d | 2016-03-02 17:51:24 +0530 | [diff] [blame] | 110 | if reference_doc.submit_on_creation: |
| 111 | new_document.submit() |
| 112 | else: |
ShashaQin | 6071ddc | 2016-02-25 11:25:24 +0800 | [diff] [blame] | 113 | new_document.docstatus=0 |
| 114 | new_document.insert() |
Nabin Hait | 91fb661 | 2014-09-05 14:56:05 +0530 | [diff] [blame] | 115 | |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 116 | return new_document |
| 117 | |
| 118 | def get_next_date(dt, mcount, day=None): |
| 119 | dt = getdate(dt) |
| 120 | |
| 121 | from dateutil.relativedelta import relativedelta |
| 122 | dt += relativedelta(months=mcount, day=day) |
| 123 | |
| 124 | return dt |
| 125 | |
| 126 | def send_notification(new_rv): |
| 127 | """Notify concerned persons about recurring document generation""" |
ankitjavalkarwork | 737d8e4 | 2014-09-01 16:18:44 +0530 | [diff] [blame] | 128 | |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 129 | frappe.sendmail(new_rv.notification_email_address, |
| 130 | subject= _("New {0}: #{1}").format(new_rv.doctype, new_rv.name), |
| 131 | message = _("Please find attached {0} #{1}").format(new_rv.doctype, new_rv.name), |
Neil Trini Lasrado | e2d8dd0 | 2015-06-22 19:06:15 +0530 | [diff] [blame] | 132 | attachments = [frappe.attach_print(new_rv.doctype, new_rv.name, file_name=new_rv.name, print_format=new_rv.recurring_print_format)]) |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 133 | |
Anand Doshi | 1394509 | 2014-09-21 19:45:49 +0530 | [diff] [blame] | 134 | def notify_errors(doc, doctype, party, owner): |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 135 | from frappe.utils.user import get_system_managers |
| 136 | recipients = get_system_managers(only_name=True) |
| 137 | |
| 138 | frappe.sendmail(recipients + [frappe.db.get_value("User", owner, "email")], |
| 139 | subject="[Urgent] Error while creating recurring %s for %s" % (doctype, doc), |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 140 | message = frappe.get_template("templates/emails/recurring_document_failed.html").render({ |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 141 | "type": doctype, |
| 142 | "name": doc, |
Anand Doshi | 1394509 | 2014-09-21 19:45:49 +0530 | [diff] [blame] | 143 | "party": party |
ankitjavalkarwork | ac085e0 | 2014-08-26 10:40:23 +0530 | [diff] [blame] | 144 | })) |
| 145 | |
| 146 | assign_task_to_owner(doc, doctype, "Recurring Invoice Failed", recipients) |
| 147 | |
| 148 | def assign_task_to_owner(doc, doctype, msg, users): |
| 149 | for d in users: |
Rushabh Mehta | c0bb453 | 2014-09-09 16:15:35 +0530 | [diff] [blame] | 150 | from frappe.desk.form import assign_to |
ankitjavalkarwork | ac085e0 | 2014-08-26 10:40:23 +0530 | [diff] [blame] | 151 | args = { |
| 152 | 'assign_to' : d, |
| 153 | 'doctype' : doctype, |
| 154 | 'name' : doc, |
| 155 | 'description' : msg, |
| 156 | 'priority' : 'High' |
| 157 | } |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 158 | assign_to.add(args) |
| 159 | |
| 160 | def validate_recurring_document(doc): |
| 161 | if doc.is_recurring: |
| 162 | validate_notification_email_id(doc) |
| 163 | |
Rushabh Mehta | 571a9d0 | 2016-03-03 15:03:23 +0530 | [diff] [blame] | 164 | if not doc.recurring_id and not doc.is_new(): |
Rushabh Mehta | 756e518 | 2016-03-03 13:00:17 +0530 | [diff] [blame] | 165 | doc.recurring_id = doc.name |
| 166 | |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 167 | if not doc.recurring_type: |
| 168 | msgprint(_("Please select {0}").format(doc.meta.get_label("recurring_type")), |
| 169 | raise_exception=1) |
| 170 | |
| 171 | elif not (doc.from_date and doc.to_date): |
Anand Doshi | aba8fdd | 2015-10-31 22:49:42 +0530 | [diff] [blame] | 172 | throw(_("Period From and Period To dates mandatory for recurring {0}").format(doc.doctype)) |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 173 | |
Sambhaji Kolate | b14401c | 2014-09-11 16:09:05 +0530 | [diff] [blame] | 174 | # |
| 175 | def convert_to_recurring(doc, posting_date): |
| 176 | if doc.is_recurring: |
Sambhaji Kolate | b14401c | 2014-09-11 16:09:05 +0530 | [diff] [blame] | 177 | set_next_date(doc, posting_date) |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 178 | |
Sambhaji Kolate | b14401c | 2014-09-11 16:09:05 +0530 | [diff] [blame] | 179 | elif doc.recurring_id: |
| 180 | frappe.db.sql("""update `tab%s` set is_recurring = 0 |
| 181 | where recurring_id = %s""" % (doc.doctype, '%s'), (doc.recurring_id)) |
| 182 | # |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 183 | |
| 184 | def validate_notification_email_id(doc): |
ShashaQin | 6071ddc | 2016-02-25 11:25:24 +0800 | [diff] [blame] | 185 | if doc.notify_by_email: |
| 186 | if doc.notification_email_address: |
| 187 | email_list = split_emails(doc.notification_email_address.replace("\n", "")) |
Rushabh Mehta | 5839a8d | 2016-03-02 17:51:24 +0530 | [diff] [blame] | 188 | |
ShashaQin | 6071ddc | 2016-02-25 11:25:24 +0800 | [diff] [blame] | 189 | from frappe.utils import validate_email_add |
| 190 | for email in email_list: |
| 191 | if not validate_email_add(email): |
| 192 | throw(_("{0} is an invalid email address in 'Notification \ |
| 193 | Email Address'").format(email)) |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 194 | |
ShashaQin | 6071ddc | 2016-02-25 11:25:24 +0800 | [diff] [blame] | 195 | else: |
| 196 | frappe.throw(_("'Notification Email Addresses' not specified for recurring %s") \ |
| 197 | % doc.doctype) |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 198 | |
| 199 | def set_next_date(doc, posting_date): |
| 200 | """ Set next date on which recurring document will be created""" |
Nabin Hait | 91fb661 | 2014-09-05 14:56:05 +0530 | [diff] [blame] | 201 | |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 202 | if not doc.repeat_on_day_of_month: |
| 203 | msgprint(_("Please enter 'Repeat on Day of Month' field value"), raise_exception=1) |
| 204 | |
ShashaQin | 6071ddc | 2016-02-25 11:25:24 +0800 | [diff] [blame] | 205 | next_date = doc.next_date or get_next_date(doc.from_date, month_map[doc.recurring_type], |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 206 | cint(doc.repeat_on_day_of_month)) |
| 207 | |
ankitjavalkarwork | 737d8e4 | 2014-09-01 16:18:44 +0530 | [diff] [blame] | 208 | frappe.db.set(doc, 'next_date', next_date) |
| 209 | |
| 210 | msgprint(_("Next Recurring {0} will be created on {1}").format(doc.doctype, next_date)) |