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