blob: 180171406378222362a69b68867c441c7d0c88f5 [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302# License: GNU General Public License v3. See license.txt
Rushabh Mehta3966f1d2012-02-23 12:35:32 +05303
Anand Doshi486f9df2012-07-19 13:40:31 +05304from __future__ import unicode_literals
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05305import frappe
Nabin Haita1c96de2014-02-21 14:44:35 +05306from frappe import _
7from frappe.utils import cstr, now_datetime, cint
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +05308
Rushabh Mehta1f847992013-12-12 19:12:19 +05309from erpnext.controllers.status_updater import StatusUpdater
Anand Doshi756dca72013-01-15 18:39:21 +053010
Rushabh Mehtab09d9da2014-01-02 11:47:23 +053011
Nabin Haitb5be7ba2014-01-30 18:47:12 +053012class TransactionBase(StatusUpdater):
Rushabh Mehta35c017a2012-11-30 10:57:28 +053013 def load_notification_message(self):
14 dt = self.doc.doctype.lower().replace(" ", "_")
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053015 if int(frappe.conn.get_value("Notification Control", None, dt) or 0):
Rushabh Mehta35c017a2012-11-30 10:57:28 +053016 self.doc.fields["__notification_message"] = \
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053017 frappe.conn.get_value("Notification Control", None, dt + "_message")
Rushabh Mehta96690eb2013-09-02 17:04:27 +053018
Anand Doshiee3d5cc2013-03-13 12:58:54 +053019 def validate_posting_time(self):
20 if not self.doc.posting_time:
Anand Doshiacec0222013-03-26 12:33:43 +053021 self.doc.posting_time = now_datetime().strftime('%H:%M:%S')
Anand Doshie53a81d2013-06-10 15:15:40 +053022
Anand Doshi670199b2013-06-10 15:38:01 +053023 def add_calendar_event(self, opts, force=False):
Anand Doshie53a81d2013-06-10 15:15:40 +053024 if self.doc.contact_by != cstr(self._prev.contact_by) or \
Anand Doshi670199b2013-06-10 15:38:01 +053025 self.doc.contact_date != cstr(self._prev.contact_date) or force:
Anand Doshie53a81d2013-06-10 15:15:40 +053026
27 self.delete_events()
28 self._add_calendar_event(opts)
29
30 def delete_events(self):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053031 frappe.delete_doc("Event", frappe.conn.sql_list("""select name from `tabEvent`
Nabin Haitad6ce4e2013-09-19 11:02:24 +053032 where ref_type=%s and ref_name=%s""", (self.doc.doctype, self.doc.name)),
33 ignore_permissions=True)
Anand Doshie53a81d2013-06-10 15:15:40 +053034
35 def _add_calendar_event(self, opts):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053036 opts = frappe._dict(opts)
Anand Doshie53a81d2013-06-10 15:15:40 +053037
38 if self.doc.contact_date:
39 event_doclist = [{
40 "doctype": "Event",
41 "owner": opts.owner or self.doc.owner,
42 "subject": opts.subject,
43 "description": opts.description,
44 "starts_on": self.doc.contact_date + " 10:00:00",
45 "event_type": "Private",
46 "ref_type": self.doc.doctype,
47 "ref_name": self.doc.name
48 }]
49
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053050 if frappe.conn.exists("Profile", self.doc.contact_by):
Anand Doshie53a81d2013-06-10 15:15:40 +053051 event_doclist.append({
52 "doctype": "Event User",
53 "parentfield": "event_individuals",
54 "person": self.doc.contact_by
55 })
56
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053057 frappe.bean(event_doclist).insert()
Nabin Hait2bd37772013-07-08 19:00:29 +053058
Rushabh Mehta4dca4012013-07-25 17:45:59 +053059 def validate_uom_is_integer(self, uom_field, qty_fields):
60 validate_uom_is_integer(self.doclist, uom_field, qty_fields)
61
Nabin Hait2bd37772013-07-08 19:00:29 +053062 def validate_with_previous_doc(self, source_dt, ref):
63 for key, val in ref.items():
Nabin Hait6e68e0e2013-07-10 15:33:29 +053064 is_child = val.get("is_child_table")
Nabin Hait2bd37772013-07-08 19:00:29 +053065 ref_doc = {}
Nabin Haita9ec49e2013-07-15 16:33:24 +053066 item_ref_dn = []
Nabin Hait2bd37772013-07-08 19:00:29 +053067 for d in self.doclist.get({"doctype": source_dt}):
Nabin Hait6e68e0e2013-07-10 15:33:29 +053068 ref_dn = d.fields.get(val["ref_dn_field"])
69 if ref_dn:
70 if is_child:
71 self.compare_values({key: [ref_dn]}, val["compare_fields"], d)
Nabin Haita9ec49e2013-07-15 16:33:24 +053072 if ref_dn not in item_ref_dn:
73 item_ref_dn.append(ref_dn)
Nabin Hait5e200e42013-07-29 15:29:51 +053074 elif not val.get("allow_duplicate_prev_row_id"):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053075 frappe.msgprint(_("Row ") + cstr(d.idx + 1) +
Nabin Haita9ec49e2013-07-15 16:33:24 +053076 _(": Duplicate row from same ") + key, raise_exception=1)
77 elif ref_dn:
Nabin Hait6e68e0e2013-07-10 15:33:29 +053078 ref_doc.setdefault(key, [])
79 if ref_dn not in ref_doc[key]:
80 ref_doc[key].append(ref_dn)
81 if ref_doc:
Nabin Hait2bd37772013-07-08 19:00:29 +053082 self.compare_values(ref_doc, val["compare_fields"])
83
84 def compare_values(self, ref_doc, fields, doc=None):
Nabin Hait6e68e0e2013-07-10 15:33:29 +053085 for ref_doctype, ref_dn_list in ref_doc.items():
86 for ref_docname in ref_dn_list:
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053087 prevdoc_values = frappe.conn.get_value(ref_doctype, ref_docname,
Nabin Hait6e68e0e2013-07-10 15:33:29 +053088 [d[0] for d in fields], as_dict=1)
89
90 for field, condition in fields:
Anand Doshi63d844b2013-07-10 20:55:15 +053091 if prevdoc_values[field] is not None:
92 self.validate_value(field, condition, prevdoc_values[field], doc)
Anand Doshi1dde46a2013-05-15 21:15:57 +053093
Anand Doshi11d31132013-06-17 12:51:36 +053094def delete_events(ref_type, ref_name):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053095 frappe.delete_doc("Event", frappe.conn.sql_list("""select name from `tabEvent`
Anand Doshib5fd7882013-06-17 12:55:05 +053096 where ref_type=%s and ref_name=%s""", (ref_type, ref_name)), for_reload=True)
Rushabh Mehta4dca4012013-07-25 17:45:59 +053097
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053098class UOMMustBeIntegerError(frappe.ValidationError): pass
Rushabh Mehtac4f5e4f2013-07-26 11:21:45 +053099
Rushabh Mehta4dca4012013-07-25 17:45:59 +0530100def validate_uom_is_integer(doclist, uom_field, qty_fields):
101 if isinstance(qty_fields, basestring):
102 qty_fields = [qty_fields]
103
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530104 integer_uoms = filter(lambda uom: frappe.conn.get_value("UOM", uom,
Rushabh Mehta4dca4012013-07-25 17:45:59 +0530105 "must_be_whole_number") or None, doclist.get_distinct_values(uom_field))
106
107 if not integer_uoms:
108 return
109
110 for d in doclist:
111 if d.fields.get(uom_field) in integer_uoms:
112 for f in qty_fields:
113 if d.fields.get(f):
114 if cint(d.fields[f])!=d.fields[f]:
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530115 frappe.msgprint(_("For UOM") + " '" + d.fields[uom_field] \
Rushabh Mehta4dca4012013-07-25 17:45:59 +0530116 + "': " + _("Quantity cannot be a fraction.") \
117 + " " + _("In Row") + ": " + str(d.idx),
Rushabh Mehtac4f5e4f2013-07-26 11:21:45 +0530118 raise_exception=UOMMustBeIntegerError)