blob: 57a07a76c238edcff9c8e0e940797f64131b7984 [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):
Anand Doshif78d1ae2014-03-28 13:55:00 +053014 dt = self.doctype.lower().replace(" ", "_")
Anand Doshie9baaa62014-02-26 12:35:33 +053015 if int(frappe.db.get_value("Notification Control", None, dt) or 0):
Anand Doshif78d1ae2014-03-28 13:55:00 +053016 self.set("__notification_message", \)
Anand Doshie9baaa62014-02-26 12:35:33 +053017 frappe.db.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):
Anand Doshif78d1ae2014-03-28 13:55:00 +053020 if not self.posting_time:
21 self.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 Doshif78d1ae2014-03-28 13:55:00 +053024 if self.contact_by != cstr(self._prev.contact_by) or \
25 self.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):
Anand Doshie9baaa62014-02-26 12:35:33 +053031 frappe.delete_doc("Event", frappe.db.sql_list("""select name from `tabEvent`
Anand Doshif78d1ae2014-03-28 13:55:00 +053032 where ref_type=%s and ref_name=%s""", (self.doctype, self.name)),
Nabin Haitad6ce4e2013-09-19 11:02:24 +053033 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
Anand Doshif78d1ae2014-03-28 13:55:00 +053038 if self.contact_date:
Rushabh Mehtae88bc8b2014-03-27 17:51:41 +053039 event_doclist = frappe.get_doc({
Anand Doshie53a81d2013-06-10 15:15:40 +053040 "doctype": "Event",
Anand Doshif78d1ae2014-03-28 13:55:00 +053041 "owner": opts.owner or self.owner,
Anand Doshie53a81d2013-06-10 15:15:40 +053042 "subject": opts.subject,
43 "description": opts.description,
Anand Doshif78d1ae2014-03-28 13:55:00 +053044 "starts_on": self.contact_date + " 10:00:00",
Anand Doshie53a81d2013-06-10 15:15:40 +053045 "event_type": "Private",
Anand Doshif78d1ae2014-03-28 13:55:00 +053046 "ref_type": self.doctype,
47 "ref_name": self.name
Rushabh Mehtae88bc8b2014-03-27 17:51:41 +053048 })
Anand Doshie53a81d2013-06-10 15:15:40 +053049
Anand Doshif78d1ae2014-03-28 13:55:00 +053050 if frappe.db.exists("User", self.contact_by):
Rushabh Mehtae88bc8b2014-03-27 17:51:41 +053051 event_doclist.append("event_individuals", {
Anand Doshie53a81d2013-06-10 15:15:40 +053052 "doctype": "Event User",
Anand Doshif78d1ae2014-03-28 13:55:00 +053053 "person": self.contact_by
Anand Doshie53a81d2013-06-10 15:15:40 +053054 })
55
Rushabh Mehtae88bc8b2014-03-27 17:51:41 +053056 event_doclist.insert()
Nabin Hait2bd37772013-07-08 19:00:29 +053057
Rushabh Mehta4dca4012013-07-25 17:45:59 +053058 def validate_uom_is_integer(self, uom_field, qty_fields):
59 validate_uom_is_integer(self.doclist, uom_field, qty_fields)
60
Nabin Hait2bd37772013-07-08 19:00:29 +053061 def validate_with_previous_doc(self, source_dt, ref):
62 for key, val in ref.items():
Nabin Hait6e68e0e2013-07-10 15:33:29 +053063 is_child = val.get("is_child_table")
Nabin Hait2bd37772013-07-08 19:00:29 +053064 ref_doc = {}
Nabin Haita9ec49e2013-07-15 16:33:24 +053065 item_ref_dn = []
Nabin Hait2bd37772013-07-08 19:00:29 +053066 for d in self.doclist.get({"doctype": source_dt}):
Anand Doshif78d1ae2014-03-28 13:55:00 +053067 ref_dn = d.get(val["ref_dn_field"])
Nabin Hait6e68e0e2013-07-10 15:33:29 +053068 if ref_dn:
69 if is_child:
70 self.compare_values({key: [ref_dn]}, val["compare_fields"], d)
Nabin Haita9ec49e2013-07-15 16:33:24 +053071 if ref_dn not in item_ref_dn:
72 item_ref_dn.append(ref_dn)
Nabin Hait5e200e42013-07-29 15:29:51 +053073 elif not val.get("allow_duplicate_prev_row_id"):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053074 frappe.msgprint(_("Row ") + cstr(d.idx + 1) +
Nabin Haita9ec49e2013-07-15 16:33:24 +053075 _(": Duplicate row from same ") + key, raise_exception=1)
76 elif ref_dn:
Nabin Hait6e68e0e2013-07-10 15:33:29 +053077 ref_doc.setdefault(key, [])
78 if ref_dn not in ref_doc[key]:
79 ref_doc[key].append(ref_dn)
80 if ref_doc:
Nabin Hait2bd37772013-07-08 19:00:29 +053081 self.compare_values(ref_doc, val["compare_fields"])
82
83 def compare_values(self, ref_doc, fields, doc=None):
Nabin Hait6e68e0e2013-07-10 15:33:29 +053084 for ref_doctype, ref_dn_list in ref_doc.items():
85 for ref_docname in ref_dn_list:
Anand Doshie9baaa62014-02-26 12:35:33 +053086 prevdoc_values = frappe.db.get_value(ref_doctype, ref_docname,
Nabin Hait6e68e0e2013-07-10 15:33:29 +053087 [d[0] for d in fields], as_dict=1)
88
89 for field, condition in fields:
Anand Doshi63d844b2013-07-10 20:55:15 +053090 if prevdoc_values[field] is not None:
91 self.validate_value(field, condition, prevdoc_values[field], doc)
Anand Doshi1dde46a2013-05-15 21:15:57 +053092
Anand Doshi11d31132013-06-17 12:51:36 +053093def delete_events(ref_type, ref_name):
Anand Doshie9baaa62014-02-26 12:35:33 +053094 frappe.delete_doc("Event", frappe.db.sql_list("""select name from `tabEvent`
Anand Doshib5fd7882013-06-17 12:55:05 +053095 where ref_type=%s and ref_name=%s""", (ref_type, ref_name)), for_reload=True)
Rushabh Mehta4dca4012013-07-25 17:45:59 +053096
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053097class UOMMustBeIntegerError(frappe.ValidationError): pass
Rushabh Mehtac4f5e4f2013-07-26 11:21:45 +053098
Rushabh Mehta4dca4012013-07-25 17:45:59 +053099def validate_uom_is_integer(doclist, uom_field, qty_fields):
100 if isinstance(qty_fields, basestring):
101 qty_fields = [qty_fields]
102
Anand Doshie9baaa62014-02-26 12:35:33 +0530103 integer_uoms = filter(lambda uom: frappe.db.get_value("UOM", uom,
Rushabh Mehta4dca4012013-07-25 17:45:59 +0530104 "must_be_whole_number") or None, doclist.get_distinct_values(uom_field))
105
106 if not integer_uoms:
107 return
108
109 for d in doclist:
Anand Doshif78d1ae2014-03-28 13:55:00 +0530110 if d.get(uom_field) in integer_uoms:
Rushabh Mehta4dca4012013-07-25 17:45:59 +0530111 for f in qty_fields:
Anand Doshif78d1ae2014-03-28 13:55:00 +0530112 if d.get(f):
Rushabh Mehta4dca4012013-07-25 17:45:59 +0530113 if cint(d.fields[f])!=d.fields[f]:
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530114 frappe.msgprint(_("For UOM") + " '" + d.fields[uom_field] \
Rushabh Mehta4dca4012013-07-25 17:45:59 +0530115 + "': " + _("Quantity cannot be a fraction.") \
116 + " " + _("In Row") + ": " + str(d.idx),
Rushabh Mehtac4f5e4f2013-07-26 11:21:45 +0530117 raise_exception=UOMMustBeIntegerError)