Rushabh Mehta | ad45e31 | 2013-11-20 12:59:58 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes 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 | a1c96de | 2014-02-21 14:44:35 +0530 | [diff] [blame] | 6 | from frappe import _ |
| 7 | from frappe.utils import cstr, now_datetime, cint |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 8 | |
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 | |
Rushabh Mehta | b09d9da | 2014-01-02 11:47:23 +0530 | [diff] [blame] | 11 | |
Nabin Hait | b5be7ba | 2014-01-30 18:47:12 +0530 | [diff] [blame] | 12 | class TransactionBase(StatusUpdater): |
Rushabh Mehta | 35c017a | 2012-11-30 10:57:28 +0530 | [diff] [blame] | 13 | def load_notification_message(self): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 14 | dt = self.doctype.lower().replace(" ", "_") |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 15 | if int(frappe.db.get_value("Notification Control", None, dt) or 0): |
Anand Doshi | 5b552b5 | 2014-04-02 15:03:35 +0530 | [diff] [blame] | 16 | self.set("__notification_message", |
| 17 | frappe.db.get_value("Notification Control", None, dt + "_message")) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 18 | |
Anand Doshi | ee3d5cc | 2013-03-13 12:58:54 +0530 | [diff] [blame] | 19 | def validate_posting_time(self): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 20 | if not self.posting_time: |
| 21 | self.posting_time = now_datetime().strftime('%H:%M:%S') |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 22 | |
Anand Doshi | 670199b | 2013-06-10 15:38:01 +0530 | [diff] [blame] | 23 | def add_calendar_event(self, opts, force=False): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 24 | if self.contact_by != cstr(self._prev.contact_by) or \ |
| 25 | self.contact_date != cstr(self._prev.contact_date) or force: |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 26 | |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 27 | self.delete_events() |
| 28 | self._add_calendar_event(opts) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 29 | |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 30 | def delete_events(self): |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 31 | frappe.delete_doc("Event", frappe.db.sql_list("""select name from `tabEvent` |
| 32 | where ref_type=%s and ref_name=%s""", (self.doctype, self.name)), |
Nabin Hait | ad6ce4e | 2013-09-19 11:02:24 +0530 | [diff] [blame] | 33 | ignore_permissions=True) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 34 | |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 35 | def _add_calendar_event(self, opts): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 36 | opts = frappe._dict(opts) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 37 | |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 38 | if self.contact_date: |
Rushabh Mehta | e88bc8b | 2014-03-27 17:51:41 +0530 | [diff] [blame] | 39 | event_doclist = frappe.get_doc({ |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 40 | "doctype": "Event", |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 41 | "owner": opts.owner or self.owner, |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 42 | "subject": opts.subject, |
| 43 | "description": opts.description, |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 44 | "starts_on": self.contact_date + " 10:00:00", |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 45 | "event_type": "Private", |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 46 | "ref_type": self.doctype, |
| 47 | "ref_name": self.name |
Rushabh Mehta | e88bc8b | 2014-03-27 17:51:41 +0530 | [diff] [blame] | 48 | }) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 49 | |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 50 | if frappe.db.exists("User", self.contact_by): |
Rushabh Mehta | e88bc8b | 2014-03-27 17:51:41 +0530 | [diff] [blame] | 51 | event_doclist.append("event_individuals", { |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 52 | "doctype": "Event User", |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 53 | "person": self.contact_by |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 54 | }) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 55 | |
Rushabh Mehta | e88bc8b | 2014-03-27 17:51:41 +0530 | [diff] [blame] | 56 | event_doclist.insert() |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 57 | |
Rushabh Mehta | 4dca401 | 2013-07-25 17:45:59 +0530 | [diff] [blame] | 58 | def validate_uom_is_integer(self, uom_field, qty_fields): |
Rushabh Mehta | cfb6ccf | 2014-04-03 11:46:52 +0530 | [diff] [blame] | 59 | validate_uom_is_integer(self, uom_field, qty_fields) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 60 | |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 61 | def validate_with_previous_doc(self, source_dt, ref): |
| 62 | for key, val in ref.items(): |
Nabin Hait | 6e68e0e | 2013-07-10 15:33:29 +0530 | [diff] [blame] | 63 | is_child = val.get("is_child_table") |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 64 | ref_doc = {} |
Nabin Hait | a9ec49e | 2013-07-15 16:33:24 +0530 | [diff] [blame] | 65 | item_ref_dn = [] |
Rushabh Mehta | f191f85 | 2014-04-02 18:09:34 +0530 | [diff] [blame] | 66 | for d in self.get_all_children(source_dt): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 67 | ref_dn = d.get(val["ref_dn_field"]) |
Nabin Hait | 6e68e0e | 2013-07-10 15:33:29 +0530 | [diff] [blame] | 68 | if ref_dn: |
| 69 | if is_child: |
| 70 | self.compare_values({key: [ref_dn]}, val["compare_fields"], d) |
Nabin Hait | a9ec49e | 2013-07-15 16:33:24 +0530 | [diff] [blame] | 71 | if ref_dn not in item_ref_dn: |
| 72 | item_ref_dn.append(ref_dn) |
Nabin Hait | 5e200e4 | 2013-07-29 15:29:51 +0530 | [diff] [blame] | 73 | elif not val.get("allow_duplicate_prev_row_id"): |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 74 | frappe.throw(_("Duplicate row {0} with same {1}").format(d.idx, key)) |
Nabin Hait | a9ec49e | 2013-07-15 16:33:24 +0530 | [diff] [blame] | 75 | elif ref_dn: |
Nabin Hait | 6e68e0e | 2013-07-10 15:33:29 +0530 | [diff] [blame] | 76 | ref_doc.setdefault(key, []) |
| 77 | if ref_dn not in ref_doc[key]: |
| 78 | ref_doc[key].append(ref_dn) |
| 79 | if ref_doc: |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 80 | self.compare_values(ref_doc, val["compare_fields"]) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 81 | |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 82 | def compare_values(self, ref_doc, fields, doc=None): |
Nabin Hait | 6e68e0e | 2013-07-10 15:33:29 +0530 | [diff] [blame] | 83 | for ref_doctype, ref_dn_list in ref_doc.items(): |
| 84 | for ref_docname in ref_dn_list: |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 85 | prevdoc_values = frappe.db.get_value(ref_doctype, ref_docname, |
Nabin Hait | 6e68e0e | 2013-07-10 15:33:29 +0530 | [diff] [blame] | 86 | [d[0] for d in fields], as_dict=1) |
| 87 | |
| 88 | for field, condition in fields: |
Anand Doshi | 63d844b | 2013-07-10 20:55:15 +0530 | [diff] [blame] | 89 | if prevdoc_values[field] is not None: |
| 90 | self.validate_value(field, condition, prevdoc_values[field], doc) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 91 | |
Anand Doshi | 11d3113 | 2013-06-17 12:51:36 +0530 | [diff] [blame] | 92 | def delete_events(ref_type, ref_name): |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 93 | frappe.delete_doc("Event", frappe.db.sql_list("""select name from `tabEvent` |
Anand Doshi | b5fd788 | 2013-06-17 12:55:05 +0530 | [diff] [blame] | 94 | 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] | 95 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 96 | class UOMMustBeIntegerError(frappe.ValidationError): pass |
Rushabh Mehta | c4f5e4f | 2013-07-26 11:21:45 +0530 | [diff] [blame] | 97 | |
Rushabh Mehta | f191f85 | 2014-04-02 18:09:34 +0530 | [diff] [blame] | 98 | def validate_uom_is_integer(doc, uom_field, qty_fields): |
Rushabh Mehta | 4dca401 | 2013-07-25 17:45:59 +0530 | [diff] [blame] | 99 | if isinstance(qty_fields, basestring): |
| 100 | qty_fields = [qty_fields] |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 101 | |
Rushabh Mehta | f191f85 | 2014-04-02 18:09:34 +0530 | [diff] [blame] | 102 | 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] | 103 | integer_uoms = filter(lambda uom: frappe.db.get_value("UOM", uom, |
Rushabh Mehta | f191f85 | 2014-04-02 18:09:34 +0530 | [diff] [blame] | 104 | "must_be_whole_number") or None, distinct_uoms) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 105 | |
Rushabh Mehta | 4dca401 | 2013-07-25 17:45:59 +0530 | [diff] [blame] | 106 | if not integer_uoms: |
| 107 | return |
| 108 | |
Rushabh Mehta | f191f85 | 2014-04-02 18:09:34 +0530 | [diff] [blame] | 109 | for d in doc.get_all_children(): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 110 | if d.get(uom_field) in integer_uoms: |
Rushabh Mehta | 4dca401 | 2013-07-25 17:45:59 +0530 | [diff] [blame] | 111 | for f in qty_fields: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 112 | if d.get(f): |
Rushabh Mehta | f2227d0 | 2014-03-31 23:37:40 +0530 | [diff] [blame] | 113 | if cint(d.get(f))!=d.get(f): |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 114 | frappe.throw(_("Quantity cannot be a fraction in row {0}").format(d.idx), UOMMustBeIntegerError) |