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 |
| 6 | from frappe import msgprint, _ |
| 7 | from frappe.utils import cstr, flt, 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): |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 13 | def set_contact_fields(self): |
| 14 | party_type, party_name = self.get_party_type_and_name() |
| 15 | |
| 16 | if party_type == "Lead": |
| 17 | contact_dict = map_lead_contact_details(party_name) |
| 18 | else: |
| 19 | contact_dict = map_party_contact_details(self.doc.contact_person, party_type, party_name) |
| 20 | |
| 21 | for fieldname, value in contact_dict.items(): |
| 22 | if self.meta.get_field(fieldname): |
| 23 | self.doc.fields[fieldname] = value |
| 24 | |
| 25 | def get_party_type_and_name(self): |
| 26 | if not hasattr(self, "_party_type_and_name"): |
| 27 | for party_type in ("Lead", "Customer", "Supplier"): |
| 28 | party_field = party_type.lower() |
| 29 | if self.meta.get_field(party_field) and self.doc.fields.get(party_field): |
| 30 | self._party_type_and_name = (party_type, self.doc.fields.get(party_field)) |
| 31 | break |
| 32 | |
| 33 | return self._party_type_and_name |
Nabin Hait | 41cc327 | 2012-04-30 14:36:18 +0530 | [diff] [blame] | 34 | |
Rushabh Mehta | 35c017a | 2012-11-30 10:57:28 +0530 | [diff] [blame] | 35 | def load_notification_message(self): |
| 36 | dt = self.doc.doctype.lower().replace(" ", "_") |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 37 | if int(frappe.conn.get_value("Notification Control", None, dt) or 0): |
Rushabh Mehta | 35c017a | 2012-11-30 10:57:28 +0530 | [diff] [blame] | 38 | self.doc.fields["__notification_message"] = \ |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 39 | frappe.conn.get_value("Notification Control", None, dt + "_message") |
Rushabh Mehta | 96690eb | 2013-09-02 17:04:27 +0530 | [diff] [blame] | 40 | |
Anand Doshi | ee3d5cc | 2013-03-13 12:58:54 +0530 | [diff] [blame] | 41 | def validate_posting_time(self): |
| 42 | if not self.doc.posting_time: |
Anand Doshi | acec022 | 2013-03-26 12:33:43 +0530 | [diff] [blame] | 43 | self.doc.posting_time = now_datetime().strftime('%H:%M:%S') |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 44 | |
Anand Doshi | 670199b | 2013-06-10 15:38:01 +0530 | [diff] [blame] | 45 | def add_calendar_event(self, opts, force=False): |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 46 | if self.doc.contact_by != cstr(self._prev.contact_by) or \ |
Anand Doshi | 670199b | 2013-06-10 15:38:01 +0530 | [diff] [blame] | 47 | self.doc.contact_date != cstr(self._prev.contact_date) or force: |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 48 | |
| 49 | self.delete_events() |
| 50 | self._add_calendar_event(opts) |
| 51 | |
| 52 | def delete_events(self): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 53 | frappe.delete_doc("Event", frappe.conn.sql_list("""select name from `tabEvent` |
Nabin Hait | ad6ce4e | 2013-09-19 11:02:24 +0530 | [diff] [blame] | 54 | where ref_type=%s and ref_name=%s""", (self.doc.doctype, self.doc.name)), |
| 55 | ignore_permissions=True) |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 56 | |
| 57 | def _add_calendar_event(self, opts): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 58 | opts = frappe._dict(opts) |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 59 | |
| 60 | if self.doc.contact_date: |
| 61 | event_doclist = [{ |
| 62 | "doctype": "Event", |
| 63 | "owner": opts.owner or self.doc.owner, |
| 64 | "subject": opts.subject, |
| 65 | "description": opts.description, |
| 66 | "starts_on": self.doc.contact_date + " 10:00:00", |
| 67 | "event_type": "Private", |
| 68 | "ref_type": self.doc.doctype, |
| 69 | "ref_name": self.doc.name |
| 70 | }] |
| 71 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 72 | if frappe.conn.exists("Profile", self.doc.contact_by): |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 73 | event_doclist.append({ |
| 74 | "doctype": "Event User", |
| 75 | "parentfield": "event_individuals", |
| 76 | "person": self.doc.contact_by |
| 77 | }) |
| 78 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 79 | frappe.bean(event_doclist).insert() |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 80 | |
Rushabh Mehta | 4dca401 | 2013-07-25 17:45:59 +0530 | [diff] [blame] | 81 | def validate_uom_is_integer(self, uom_field, qty_fields): |
| 82 | validate_uom_is_integer(self.doclist, uom_field, qty_fields) |
| 83 | |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 84 | def validate_with_previous_doc(self, source_dt, ref): |
| 85 | for key, val in ref.items(): |
Nabin Hait | 6e68e0e | 2013-07-10 15:33:29 +0530 | [diff] [blame] | 86 | is_child = val.get("is_child_table") |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 87 | ref_doc = {} |
Nabin Hait | a9ec49e | 2013-07-15 16:33:24 +0530 | [diff] [blame] | 88 | item_ref_dn = [] |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 89 | for d in self.doclist.get({"doctype": source_dt}): |
Nabin Hait | 6e68e0e | 2013-07-10 15:33:29 +0530 | [diff] [blame] | 90 | ref_dn = d.fields.get(val["ref_dn_field"]) |
| 91 | if ref_dn: |
| 92 | if is_child: |
| 93 | self.compare_values({key: [ref_dn]}, val["compare_fields"], d) |
Nabin Hait | a9ec49e | 2013-07-15 16:33:24 +0530 | [diff] [blame] | 94 | if ref_dn not in item_ref_dn: |
| 95 | item_ref_dn.append(ref_dn) |
Nabin Hait | 5e200e4 | 2013-07-29 15:29:51 +0530 | [diff] [blame] | 96 | elif not val.get("allow_duplicate_prev_row_id"): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 97 | frappe.msgprint(_("Row ") + cstr(d.idx + 1) + |
Nabin Hait | a9ec49e | 2013-07-15 16:33:24 +0530 | [diff] [blame] | 98 | _(": Duplicate row from same ") + key, raise_exception=1) |
| 99 | elif ref_dn: |
Nabin Hait | 6e68e0e | 2013-07-10 15:33:29 +0530 | [diff] [blame] | 100 | ref_doc.setdefault(key, []) |
| 101 | if ref_dn not in ref_doc[key]: |
| 102 | ref_doc[key].append(ref_dn) |
| 103 | if ref_doc: |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 104 | self.compare_values(ref_doc, val["compare_fields"]) |
| 105 | |
| 106 | def compare_values(self, ref_doc, fields, doc=None): |
Nabin Hait | 6e68e0e | 2013-07-10 15:33:29 +0530 | [diff] [blame] | 107 | for ref_doctype, ref_dn_list in ref_doc.items(): |
| 108 | for ref_docname in ref_dn_list: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 109 | prevdoc_values = frappe.conn.get_value(ref_doctype, ref_docname, |
Nabin Hait | 6e68e0e | 2013-07-10 15:33:29 +0530 | [diff] [blame] | 110 | [d[0] for d in fields], as_dict=1) |
| 111 | |
| 112 | for field, condition in fields: |
Anand Doshi | 63d844b | 2013-07-10 20:55:15 +0530 | [diff] [blame] | 113 | if prevdoc_values[field] is not None: |
| 114 | self.validate_value(field, condition, prevdoc_values[field], doc) |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 115 | |
| 116 | def get_default_contact(party_field, party_name): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 117 | contact = frappe.conn.sql("""select name from `tabContact` where `%s`=%s |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 118 | order by is_primary_contact desc, name asc limit 1""" % (party_field, "%s"), |
| 119 | (party_name,)) |
| 120 | |
| 121 | return contact[0][0] if contact else None |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 122 | |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 123 | def map_lead_contact_details(party_name): |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 124 | out = {} |
| 125 | for fieldname in ["contact_display", "contact_email", "contact_mobile", "contact_phone"]: |
| 126 | out[fieldname] = None |
| 127 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 128 | lead = frappe.conn.sql("""select * from `tabLead` where name=%s""", party_name, as_dict=True) |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 129 | if lead: |
| 130 | lead = lead[0] |
| 131 | out.update({ |
| 132 | "contact_display": lead.get("lead_name"), |
| 133 | "contact_email": lead.get("email_id"), |
| 134 | "contact_mobile": lead.get("mobile_no"), |
| 135 | "contact_phone": lead.get("phone"), |
| 136 | }) |
| 137 | |
| 138 | return out |
| 139 | |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 140 | def map_party_contact_details(contact_name=None, party_field=None, party_name=None): |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 141 | out = {} |
| 142 | for fieldname in ["contact_person", "contact_display", "contact_email", |
| 143 | "contact_mobile", "contact_phone", "contact_designation", "contact_department"]: |
| 144 | out[fieldname] = None |
Nabin Hait | deff578 | 2013-07-24 11:30:35 +0530 | [diff] [blame] | 145 | |
Anand Doshi | ba8ea7d | 2013-07-24 12:08:42 +0530 | [diff] [blame] | 146 | if not contact_name and party_field: |
| 147 | contact_name = get_default_contact(party_field, party_name) |
| 148 | |
Nabin Hait | 02a3d7a | 2013-07-24 16:33:30 +0530 | [diff] [blame] | 149 | if contact_name: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 150 | contact = frappe.conn.sql("""select * from `tabContact` where name=%s""", |
Nabin Hait | 02a3d7a | 2013-07-24 16:33:30 +0530 | [diff] [blame] | 151 | contact_name, as_dict=True) |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 152 | |
Nabin Hait | 02a3d7a | 2013-07-24 16:33:30 +0530 | [diff] [blame] | 153 | if contact: |
| 154 | contact = contact[0] |
| 155 | out.update({ |
| 156 | "contact_person": contact.get("name"), |
| 157 | "contact_display": " ".join(filter(None, |
| 158 | [contact.get("first_name"), contact.get("last_name")])), |
| 159 | "contact_email": contact.get("email_id"), |
| 160 | "contact_mobile": contact.get("mobile_no"), |
| 161 | "contact_phone": contact.get("phone"), |
| 162 | "contact_designation": contact.get("designation"), |
| 163 | "contact_department": contact.get("department") |
| 164 | }) |
Anand Doshi | ba8ea7d | 2013-07-24 12:08:42 +0530 | [diff] [blame] | 165 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 166 | return out |
| 167 | |
| 168 | def get_address_territory(address_doc): |
| 169 | territory = None |
| 170 | for fieldname in ("city", "state", "country"): |
| 171 | value = address_doc.fields.get(fieldname) |
| 172 | if value: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 173 | territory = frappe.conn.get_value("Territory", value.strip()) |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 174 | if territory: |
| 175 | break |
| 176 | |
| 177 | return territory |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 178 | |
Anand Doshi | 11d3113 | 2013-06-17 12:51:36 +0530 | [diff] [blame] | 179 | def delete_events(ref_type, ref_name): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 180 | frappe.delete_doc("Event", frappe.conn.sql_list("""select name from `tabEvent` |
Anand Doshi | b5fd788 | 2013-06-17 12:55:05 +0530 | [diff] [blame] | 181 | 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] | 182 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 183 | class UOMMustBeIntegerError(frappe.ValidationError): pass |
Rushabh Mehta | c4f5e4f | 2013-07-26 11:21:45 +0530 | [diff] [blame] | 184 | |
Rushabh Mehta | 4dca401 | 2013-07-25 17:45:59 +0530 | [diff] [blame] | 185 | def validate_uom_is_integer(doclist, uom_field, qty_fields): |
| 186 | if isinstance(qty_fields, basestring): |
| 187 | qty_fields = [qty_fields] |
| 188 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 189 | integer_uoms = filter(lambda uom: frappe.conn.get_value("UOM", uom, |
Rushabh Mehta | 4dca401 | 2013-07-25 17:45:59 +0530 | [diff] [blame] | 190 | "must_be_whole_number") or None, doclist.get_distinct_values(uom_field)) |
| 191 | |
| 192 | if not integer_uoms: |
| 193 | return |
| 194 | |
| 195 | for d in doclist: |
| 196 | if d.fields.get(uom_field) in integer_uoms: |
| 197 | for f in qty_fields: |
| 198 | if d.fields.get(f): |
| 199 | if cint(d.fields[f])!=d.fields[f]: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 200 | frappe.msgprint(_("For UOM") + " '" + d.fields[uom_field] \ |
Rushabh Mehta | 4dca401 | 2013-07-25 17:45:59 +0530 | [diff] [blame] | 201 | + "': " + _("Quantity cannot be a fraction.") \ |
| 202 | + " " + _("In Row") + ": " + str(d.idx), |
Rushabh Mehta | c4f5e4f | 2013-07-26 11:21:45 +0530 | [diff] [blame] | 203 | raise_exception=UOMMustBeIntegerError) |