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 | |
Rushabh Mehta | 82c2589 | 2017-03-14 18:52:47 +0530 | [diff] [blame] | 96 | if new_document.meta.get_field('set_posting_time'): |
| 97 | new_document.set('set_posting_time', 1) |
| 98 | |
Anand Doshi | 39e2c2b | 2016-01-25 17:03:50 +0530 | [diff] [blame] | 99 | # copy document fields |
| 100 | for fieldname in ("owner", "recurring_type", "repeat_on_day_of_month", |
| 101 | "recurring_id", "notification_email_address", "is_recurring", "end_date", |
| 102 | "title", "naming_series", "select_print_heading", "ignore_pricing_rule", |
Rushabh Mehta | 5839a8d | 2016-03-02 17:51:24 +0530 | [diff] [blame] | 103 | "posting_time", "remarks", 'submit_on_creation'): |
Anand Doshi | 39e2c2b | 2016-01-25 17:03:50 +0530 | [diff] [blame] | 104 | if new_document.meta.get_field(fieldname): |
| 105 | new_document.set(fieldname, reference_doc.get(fieldname)) |
| 106 | |
| 107 | # copy item fields |
| 108 | for i, item in enumerate(new_document.items): |
| 109 | for fieldname in ("page_break",): |
| 110 | item.set(fieldname, reference_doc.items[i].get(fieldname)) |
| 111 | |
| 112 | new_document.run_method("on_recurring", reference_doc=reference_doc) |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 113 | |
Rushabh Mehta | 5839a8d | 2016-03-02 17:51:24 +0530 | [diff] [blame] | 114 | if reference_doc.submit_on_creation: |
Nabin Hait | 5996b2f | 2016-03-18 15:19:54 +0530 | [diff] [blame] | 115 | new_document.insert() |
Rushabh Mehta | 5839a8d | 2016-03-02 17:51:24 +0530 | [diff] [blame] | 116 | new_document.submit() |
| 117 | else: |
ShashaQin | 6071ddc | 2016-02-25 11:25:24 +0800 | [diff] [blame] | 118 | new_document.docstatus=0 |
| 119 | new_document.insert() |
Nabin Hait | 91fb661 | 2014-09-05 14:56:05 +0530 | [diff] [blame] | 120 | |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 121 | return new_document |
| 122 | |
| 123 | def get_next_date(dt, mcount, day=None): |
| 124 | dt = getdate(dt) |
| 125 | |
| 126 | from dateutil.relativedelta import relativedelta |
| 127 | dt += relativedelta(months=mcount, day=day) |
| 128 | |
| 129 | return dt |
| 130 | |
| 131 | def send_notification(new_rv): |
| 132 | """Notify concerned persons about recurring document generation""" |
ankitjavalkarwork | 737d8e4 | 2014-09-01 16:18:44 +0530 | [diff] [blame] | 133 | |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 134 | frappe.sendmail(new_rv.notification_email_address, |
| 135 | subject= _("New {0}: #{1}").format(new_rv.doctype, new_rv.name), |
| 136 | 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] | 137 | 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] | 138 | |
Anand Doshi | 1394509 | 2014-09-21 19:45:49 +0530 | [diff] [blame] | 139 | def notify_errors(doc, doctype, party, owner): |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 140 | from frappe.utils.user import get_system_managers |
| 141 | recipients = get_system_managers(only_name=True) |
| 142 | |
| 143 | frappe.sendmail(recipients + [frappe.db.get_value("User", owner, "email")], |
| 144 | subject="[Urgent] Error while creating recurring %s for %s" % (doctype, doc), |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 145 | message = frappe.get_template("templates/emails/recurring_document_failed.html").render({ |
ankitjavalkarwork | e8331d4 | 2014-08-25 18:00:12 +0530 | [diff] [blame] | 146 | "type": doctype, |
| 147 | "name": doc, |
Anand Doshi | 1394509 | 2014-09-21 19:45:49 +0530 | [diff] [blame] | 148 | "party": party |
ankitjavalkarwork | ac085e0 | 2014-08-26 10:40:23 +0530 | [diff] [blame] | 149 | })) |
| 150 | |
| 151 | assign_task_to_owner(doc, doctype, "Recurring Invoice Failed", recipients) |
| 152 | |
| 153 | def assign_task_to_owner(doc, doctype, msg, users): |
| 154 | for d in users: |
Rushabh Mehta | c0bb453 | 2014-09-09 16:15:35 +0530 | [diff] [blame] | 155 | from frappe.desk.form import assign_to |
ankitjavalkarwork | ac085e0 | 2014-08-26 10:40:23 +0530 | [diff] [blame] | 156 | args = { |
| 157 | 'assign_to' : d, |
| 158 | 'doctype' : doctype, |
| 159 | 'name' : doc, |
| 160 | 'description' : msg, |
| 161 | 'priority' : 'High' |
| 162 | } |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 163 | assign_to.add(args) |
| 164 | |
| 165 | def validate_recurring_document(doc): |
| 166 | if doc.is_recurring: |
| 167 | validate_notification_email_id(doc) |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 168 | if not doc.recurring_type: |
Nabin Hait | 5996b2f | 2016-03-18 15:19:54 +0530 | [diff] [blame] | 169 | frappe.throw(_("Please select {0}").format(doc.meta.get_label("recurring_type"))) |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 170 | |
| 171 | elif not (doc.from_date and doc.to_date): |
Nabin Hait | 5996b2f | 2016-03-18 15:19:54 +0530 | [diff] [blame] | 172 | 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] | 173 | |
Nabin Hait | 5996b2f | 2016-03-18 15:19:54 +0530 | [diff] [blame] | 174 | def validate_recurring_next_date(doc): |
| 175 | posting_date = doc.get("posting_date") or doc.get("transaction_date") |
| 176 | if getdate(posting_date) > getdate(doc.next_date): |
| 177 | frappe.throw(_("Next Date must be greater than Posting Date")) |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 178 | |
Rushabh Mehta | acf28af | 2016-03-31 15:02:11 +0530 | [diff] [blame] | 179 | next_date = getdate(doc.next_date) |
| 180 | if next_date.day != doc.repeat_on_day_of_month: |
| 181 | |
| 182 | # if the repeat day is the last day of the month (31) |
| 183 | # and the current month does not have as many days, |
| 184 | # then the last day of the current month is a valid date |
| 185 | lastday = calendar.monthrange(next_date.year, next_date.month)[1] |
| 186 | if doc.repeat_on_day_of_month < lastday: |
| 187 | |
| 188 | # the specified day of the month is not same as the day specified |
| 189 | # or the last day of the month |
| 190 | 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] | 191 | |
Sambhaji Kolate | b14401c | 2014-09-11 16:09:05 +0530 | [diff] [blame] | 192 | def convert_to_recurring(doc, posting_date): |
Nabin Hait | 5996b2f | 2016-03-18 15:19:54 +0530 | [diff] [blame] | 193 | if doc.is_recurring: |
| 194 | if not doc.recurring_id: |
| 195 | doc.db_set("recurring_id", doc.name) |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 196 | |
Nabin Hait | 5996b2f | 2016-03-18 15:19:54 +0530 | [diff] [blame] | 197 | set_next_date(doc, posting_date) |
Rushabh Mehta | acf28af | 2016-03-31 15:02:11 +0530 | [diff] [blame] | 198 | |
Nabin Hait | 7504ea7 | 2016-03-23 11:16:51 +0530 | [diff] [blame] | 199 | if doc.next_date: |
| 200 | validate_recurring_next_date(doc) |
Rushabh Mehta | acf28af | 2016-03-31 15:02:11 +0530 | [diff] [blame] | 201 | |
Nabin Hait | 5996b2f | 2016-03-18 15:19:54 +0530 | [diff] [blame] | 202 | elif doc.recurring_id: |
Nabin Hait | 7504ea7 | 2016-03-23 11:16:51 +0530 | [diff] [blame] | 203 | doc.db_set("recurring_id", None) |
Rushabh Mehta | acf28af | 2016-03-31 15:02:11 +0530 | [diff] [blame] | 204 | |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 205 | def validate_notification_email_id(doc): |
ShashaQin | 6071ddc | 2016-02-25 11:25:24 +0800 | [diff] [blame] | 206 | if doc.notify_by_email: |
| 207 | if doc.notification_email_address: |
| 208 | email_list = split_emails(doc.notification_email_address.replace("\n", "")) |
Rushabh Mehta | 5839a8d | 2016-03-02 17:51:24 +0530 | [diff] [blame] | 209 | |
ShashaQin | 6071ddc | 2016-02-25 11:25:24 +0800 | [diff] [blame] | 210 | from frappe.utils import validate_email_add |
| 211 | for email in email_list: |
| 212 | if not validate_email_add(email): |
| 213 | throw(_("{0} is an invalid email address in 'Notification \ |
| 214 | Email Address'").format(email)) |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 215 | |
ShashaQin | 6071ddc | 2016-02-25 11:25:24 +0800 | [diff] [blame] | 216 | else: |
| 217 | frappe.throw(_("'Notification Email Addresses' not specified for recurring %s") \ |
| 218 | % doc.doctype) |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 219 | |
| 220 | def set_next_date(doc, posting_date): |
| 221 | """ Set next date on which recurring document will be created""" |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 222 | if not doc.repeat_on_day_of_month: |
| 223 | msgprint(_("Please enter 'Repeat on Day of Month' field value"), raise_exception=1) |
| 224 | |
rohitwaghchaure | 90ff509 | 2016-03-17 17:30:53 +0530 | [diff] [blame] | 225 | next_date = get_next_date(posting_date, month_map[doc.recurring_type], |
ankitjavalkarwork | aaac7c1 | 2014-08-27 19:10:10 +0530 | [diff] [blame] | 226 | cint(doc.repeat_on_day_of_month)) |
| 227 | |
Nabin Hait | 5996b2f | 2016-03-18 15:19:54 +0530 | [diff] [blame] | 228 | doc.db_set('next_date', next_date) |
ankitjavalkarwork | 737d8e4 | 2014-09-01 16:18:44 +0530 | [diff] [blame] | 229 | |
| 230 | msgprint(_("Next Recurring {0} will be created on {1}").format(doc.doctype, next_date)) |