Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
Rushabh Mehta | 3966f1d | 2012-02-23 12:35:32 +0530 | [diff] [blame] | 3 | |
Anand Doshi | 486f9df | 2012-07-19 13:40:31 +0530 | [diff] [blame] | 4 | from __future__ import unicode_literals |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 5 | import frappe |
Nabin Hait | 3cf67a4 | 2015-07-24 13:26:36 +0530 | [diff] [blame] | 6 | import frappe.share |
Nabin Hait | a1c96de | 2014-02-21 14:44:35 +0530 | [diff] [blame] | 7 | from frappe import _ |
Faris Ansari | 184491b | 2018-01-15 14:18:53 +0530 | [diff] [blame] | 8 | from frappe.utils import cstr, now_datetime, cint, flt, get_time |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 9 | from erpnext.controllers.status_updater import StatusUpdater |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 10 | |
Achilles Rasquinha | 1697a7a | 2018-02-15 11:39:45 +0530 | [diff] [blame] | 11 | from six import string_types |
| 12 | |
Nabin Hait | 3cf67a4 | 2015-07-24 13:26:36 +0530 | [diff] [blame] | 13 | class UOMMustBeIntegerError(frappe.ValidationError): pass |
Rushabh Mehta | b09d9da | 2014-01-02 11:47:23 +0530 | [diff] [blame] | 14 | |
Nabin Hait | b5be7ba | 2014-01-30 18:47:12 +0530 | [diff] [blame] | 15 | class TransactionBase(StatusUpdater): |
Rushabh Mehta | 35c017a | 2012-11-30 10:57:28 +0530 | [diff] [blame] | 16 | def load_notification_message(self): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 17 | dt = self.doctype.lower().replace(" ", "_") |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 18 | if int(frappe.db.get_value("Notification Control", None, dt) or 0): |
Anand Doshi | 5b552b5 | 2014-04-02 15:03:35 +0530 | [diff] [blame] | 19 | self.set("__notification_message", |
| 20 | frappe.db.get_value("Notification Control", None, dt + "_message")) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 21 | |
Anand Doshi | ee3d5cc | 2013-03-13 12:58:54 +0530 | [diff] [blame] | 22 | def validate_posting_time(self): |
Makarand Bauskar | b0df661 | 2017-05-16 07:59:58 +0530 | [diff] [blame] | 23 | # set Edit Posting Date and Time to 1 while data import |
Sagar Vora | 67fe101 | 2017-06-19 12:12:22 +0530 | [diff] [blame] | 24 | if frappe.flags.in_import and self.posting_date: |
Makarand Bauskar | b0df661 | 2017-05-16 07:59:58 +0530 | [diff] [blame] | 25 | self.set_posting_time = 1 |
| 26 | |
Rushabh Mehta | 131866a | 2017-03-14 21:06:15 +0530 | [diff] [blame] | 27 | if not getattr(self, 'set_posting_time', None): |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 28 | now = now_datetime() |
| 29 | self.posting_date = now.strftime('%Y-%m-%d') |
Nabin Hait | 945f502 | 2017-09-29 15:11:50 +0530 | [diff] [blame] | 30 | self.posting_time = now.strftime('%H:%M:%S.%f') |
Manas Solanki | c6d5611 | 2018-01-18 11:20:24 +0530 | [diff] [blame] | 31 | elif self.posting_time: |
Faris Ansari | 184491b | 2018-01-15 14:18:53 +0530 | [diff] [blame] | 32 | try: |
| 33 | get_time(self.posting_time) |
| 34 | except ValueError: |
| 35 | frappe.throw(_('Invalid Posting Time')) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 36 | |
Anand Doshi | 670199b | 2013-06-10 15:38:01 +0530 | [diff] [blame] | 37 | def add_calendar_event(self, opts, force=False): |
Nabin Hait | e06d01e | 2015-06-29 18:38:27 +0530 | [diff] [blame] | 38 | if cstr(self.contact_by) != cstr(self._prev.contact_by) or \ |
Zarrar | 0702f29 | 2018-03-12 18:49:54 +0530 | [diff] [blame] | 39 | cstr(self.contact_date) != cstr(self._prev.contact_date) or force or \ |
| 40 | (hasattr(self, "ends_on") and cstr(self.ends_on) != cstr(self._prev.ends_on)): |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 41 | |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 42 | self.delete_events() |
| 43 | self._add_calendar_event(opts) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 44 | |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 45 | def delete_events(self): |
Nabin Hait | e06d01e | 2015-06-29 18:38:27 +0530 | [diff] [blame] | 46 | events = frappe.db.sql_list("""select name from `tabEvent` |
| 47 | where ref_type=%s and ref_name=%s""", (self.doctype, self.name)) |
| 48 | if events: |
Zarrar | 6578bc1 | 2018-03-01 10:54:55 +0530 | [diff] [blame] | 49 | frappe.db.sql("delete from `tabEvent` where name in ({0})" |
Nabin Hait | e06d01e | 2015-06-29 18:38:27 +0530 | [diff] [blame] | 50 | .format(", ".join(['%s']*len(events))), tuple(events)) |
Anand Doshi | 414248b | 2015-08-26 17:56:40 +0530 | [diff] [blame] | 51 | |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 52 | def _add_calendar_event(self, opts): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 53 | opts = frappe._dict(opts) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 54 | |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 55 | if self.contact_date: |
Rushabh Mehta | 6efbc9d | 2015-02-13 10:16:41 +0530 | [diff] [blame] | 56 | event = frappe.get_doc({ |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 57 | "doctype": "Event", |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 58 | "owner": opts.owner or self.owner, |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 59 | "subject": opts.subject, |
| 60 | "description": opts.description, |
Anand Doshi | 414248b | 2015-08-26 17:56:40 +0530 | [diff] [blame] | 61 | "starts_on": self.contact_date, |
Zarrar | 0702f29 | 2018-03-12 18:49:54 +0530 | [diff] [blame] | 62 | "ends_on": opts.ends_on, |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 63 | "event_type": "Private", |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 64 | "ref_type": self.doctype, |
| 65 | "ref_name": self.name |
Rushabh Mehta | e88bc8b | 2014-03-27 17:51:41 +0530 | [diff] [blame] | 66 | }) |
Anand Doshi | f9fc04c | 2015-02-23 22:14:12 +0530 | [diff] [blame] | 67 | |
Pratik Vyas | 7f9489e | 2015-02-20 16:22:24 +0530 | [diff] [blame] | 68 | event.insert(ignore_permissions=True) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 69 | |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 70 | if frappe.db.exists("User", self.contact_by): |
Anand Doshi | 414248b | 2015-08-26 17:56:40 +0530 | [diff] [blame] | 71 | frappe.share.add("Event", event.name, self.contact_by, |
Nabin Hait | 3b0adc5 | 2015-05-21 16:51:15 +0530 | [diff] [blame] | 72 | flags={"ignore_share_permission": True}) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 73 | |
Rushabh Mehta | 4dca401 | 2013-07-25 17:45:59 +0530 | [diff] [blame] | 74 | def validate_uom_is_integer(self, uom_field, qty_fields): |
Rushabh Mehta | cfb6ccf | 2014-04-03 11:46:52 +0530 | [diff] [blame] | 75 | validate_uom_is_integer(self, uom_field, qty_fields) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 76 | |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 77 | def validate_with_previous_doc(self, ref): |
rohitwaghchaure | 0df95fa | 2018-03-01 11:31:33 +0530 | [diff] [blame] | 78 | self.exclude_fields = ["conversion_factor", "uom"] if self.get('is_return') else [] |
| 79 | |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 80 | for key, val in ref.items(): |
Nabin Hait | 6e68e0e | 2013-07-10 15:33:29 +0530 | [diff] [blame] | 81 | is_child = val.get("is_child_table") |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 82 | ref_doc = {} |
Nabin Hait | a9ec49e | 2013-07-15 16:33:24 +0530 | [diff] [blame] | 83 | item_ref_dn = [] |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 84 | for d in self.get_all_children(self.doctype + " Item"): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 85 | ref_dn = d.get(val["ref_dn_field"]) |
Nabin Hait | 6e68e0e | 2013-07-10 15:33:29 +0530 | [diff] [blame] | 86 | if ref_dn: |
| 87 | if is_child: |
| 88 | self.compare_values({key: [ref_dn]}, val["compare_fields"], d) |
Nabin Hait | a9ec49e | 2013-07-15 16:33:24 +0530 | [diff] [blame] | 89 | if ref_dn not in item_ref_dn: |
| 90 | item_ref_dn.append(ref_dn) |
Nabin Hait | 5e200e4 | 2013-07-29 15:29:51 +0530 | [diff] [blame] | 91 | elif not val.get("allow_duplicate_prev_row_id"): |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 92 | frappe.throw(_("Duplicate row {0} with same {1}").format(d.idx, key)) |
Nabin Hait | a9ec49e | 2013-07-15 16:33:24 +0530 | [diff] [blame] | 93 | elif ref_dn: |
Nabin Hait | 6e68e0e | 2013-07-10 15:33:29 +0530 | [diff] [blame] | 94 | ref_doc.setdefault(key, []) |
| 95 | if ref_dn not in ref_doc[key]: |
| 96 | ref_doc[key].append(ref_dn) |
| 97 | if ref_doc: |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 98 | self.compare_values(ref_doc, val["compare_fields"]) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 99 | |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 100 | def compare_values(self, ref_doc, fields, doc=None): |
Rushabh Mehta | 611b513 | 2015-03-19 17:15:45 +0530 | [diff] [blame] | 101 | for reference_doctype, ref_dn_list in ref_doc.items(): |
| 102 | for reference_name in ref_dn_list: |
| 103 | prevdoc_values = frappe.db.get_value(reference_doctype, reference_name, |
Nabin Hait | 6e68e0e | 2013-07-10 15:33:29 +0530 | [diff] [blame] | 104 | [d[0] for d in fields], as_dict=1) |
| 105 | |
Rushabh Mehta | afacc3d | 2015-12-01 15:53:07 +0530 | [diff] [blame] | 106 | if not prevdoc_values: |
| 107 | frappe.throw(_("Invalid reference {0} {1}").format(reference_doctype, reference_name)) |
| 108 | |
Nabin Hait | 6e68e0e | 2013-07-10 15:33:29 +0530 | [diff] [blame] | 109 | for field, condition in fields: |
rohitwaghchaure | 0df95fa | 2018-03-01 11:31:33 +0530 | [diff] [blame] | 110 | if prevdoc_values[field] is not None and field not in self.exclude_fields: |
Anand Doshi | 63d844b | 2013-07-10 20:55:15 +0530 | [diff] [blame] | 111 | self.validate_value(field, condition, prevdoc_values[field], doc) |
Anand Doshi | 414248b | 2015-08-26 17:56:40 +0530 | [diff] [blame] | 112 | |
| 113 | |
Nabin Hait | 28a9e35 | 2015-07-08 13:10:52 +0530 | [diff] [blame] | 114 | def validate_rate_with_reference_doc(self, ref_details): |
| 115 | for ref_dt, ref_dn_field, ref_link_field in ref_details: |
| 116 | for d in self.get("items"): |
| 117 | if d.get(ref_link_field): |
| 118 | ref_rate = frappe.db.get_value(ref_dt + " Item", d.get(ref_link_field), "rate") |
Anand Doshi | 414248b | 2015-08-26 17:56:40 +0530 | [diff] [blame] | 119 | |
Nabin Hait | 28a9e35 | 2015-07-08 13:10:52 +0530 | [diff] [blame] | 120 | if abs(flt(d.rate - ref_rate, d.precision("rate"))) >= .01: |
Nabin Hait | 5e3646a | 2015-07-08 14:39:03 +0530 | [diff] [blame] | 121 | frappe.throw(_("Row #{0}: Rate must be same as {1}: {2} ({3} / {4}) ") |
Nabin Hait | 28a9e35 | 2015-07-08 13:10:52 +0530 | [diff] [blame] | 122 | .format(d.idx, ref_dt, d.get(ref_dn_field), d.rate, ref_rate)) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 123 | |
Saurabh | 8f24ecb | 2016-06-16 18:01:58 +0530 | [diff] [blame] | 124 | def get_link_filters(self, for_doctype): |
Saurabh | b6d6b84 | 2016-06-27 14:48:26 +0530 | [diff] [blame] | 125 | if hasattr(self, "prev_link_mapper") and self.prev_link_mapper.get(for_doctype): |
Saurabh | 8f24ecb | 2016-06-16 18:01:58 +0530 | [diff] [blame] | 126 | fieldname = self.prev_link_mapper[for_doctype]["fieldname"] |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 127 | |
Saurabh | 8f24ecb | 2016-06-16 18:01:58 +0530 | [diff] [blame] | 128 | values = filter(None, tuple([item.as_dict()[fieldname] for item in self.items])) |
| 129 | |
| 130 | if values: |
| 131 | ret = { |
| 132 | for_doctype : { |
| 133 | "filters": [[for_doctype, "name", "in", values]] |
| 134 | } |
| 135 | } |
| 136 | else: |
| 137 | ret = None |
| 138 | else: |
| 139 | ret = None |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 140 | |
Saurabh | 8f24ecb | 2016-06-16 18:01:58 +0530 | [diff] [blame] | 141 | return ret |
Anand Doshi | f9fc04c | 2015-02-23 22:14:12 +0530 | [diff] [blame] | 142 | |
Anand Doshi | 11d3113 | 2013-06-17 12:51:36 +0530 | [diff] [blame] | 143 | def delete_events(ref_type, ref_name): |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 144 | frappe.delete_doc("Event", frappe.db.sql_list("""select name from `tabEvent` |
Anand Doshi | b5fd788 | 2013-06-17 12:55:05 +0530 | [diff] [blame] | 145 | where ref_type=%s and ref_name=%s""", (ref_type, ref_name)), for_reload=True) |
Rushabh Mehta | 4dca401 | 2013-07-25 17:45:59 +0530 | [diff] [blame] | 146 | |
Nabin Hait | c5fb88c | 2015-02-09 16:46:46 +0530 | [diff] [blame] | 147 | def validate_uom_is_integer(doc, uom_field, qty_fields, child_dt=None): |
Achilles Rasquinha | 1697a7a | 2018-02-15 11:39:45 +0530 | [diff] [blame] | 148 | if isinstance(qty_fields, string_types): |
Rushabh Mehta | 4dca401 | 2013-07-25 17:45:59 +0530 | [diff] [blame] | 149 | qty_fields = [qty_fields] |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 150 | |
Rushabh Mehta | f191f85 | 2014-04-02 18:09:34 +0530 | [diff] [blame] | 151 | distinct_uoms = list(set([d.get(uom_field) for d in doc.get_all_children()])) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 152 | integer_uoms = filter(lambda uom: frappe.db.get_value("UOM", uom, |
Rushabh Mehta | f191f85 | 2014-04-02 18:09:34 +0530 | [diff] [blame] | 153 | "must_be_whole_number") or None, distinct_uoms) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 154 | |
Rushabh Mehta | 4dca401 | 2013-07-25 17:45:59 +0530 | [diff] [blame] | 155 | if not integer_uoms: |
| 156 | return |
| 157 | |
Nabin Hait | c5fb88c | 2015-02-09 16:46:46 +0530 | [diff] [blame] | 158 | for d in doc.get_all_children(parenttype=child_dt): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 159 | if d.get(uom_field) in integer_uoms: |
Rushabh Mehta | 4dca401 | 2013-07-25 17:45:59 +0530 | [diff] [blame] | 160 | for f in qty_fields: |
Rushabh Mehta | e385b5b | 2017-04-20 15:21:01 +0530 | [diff] [blame] | 161 | qty = d.get(f) |
| 162 | if qty: |
Rushabh Mehta | b7b49f6 | 2017-06-19 09:58:48 +0530 | [diff] [blame] | 163 | if abs(cint(qty) - flt(qty)) > 0.0000001: |
Rushabh Mehta | e385b5b | 2017-04-20 15:21:01 +0530 | [diff] [blame] | 164 | frappe.throw(_("Quantity ({0}) cannot be a fraction in row {1}").format(qty, d.idx), UOMMustBeIntegerError) |