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