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