blob: 0e3a4f952513543fa62c933a0d699389fdf0152e [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301# Copyright (c) 2015, Frappe 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 Hait3cf67a42015-07-24 13:26:36 +05306import frappe.share
Nabin Haita1c96de2014-02-21 14:44:35 +05307from frappe import _
Nabin Hait28a9e352015-07-08 13:10:52 +05308from frappe.utils import cstr, now_datetime, cint, flt
Rushabh Mehta1f847992013-12-12 19:12:19 +05309from erpnext.controllers.status_updater import StatusUpdater
Anand Doshi756dca72013-01-15 18:39:21 +053010
Nabin Hait3cf67a42015-07-24 13:26:36 +053011class UOMMustBeIntegerError(frappe.ValidationError): pass
Rushabh Mehtab09d9da2014-01-02 11:47:23 +053012
Nabin Haitb5be7ba2014-01-30 18:47:12 +053013class TransactionBase(StatusUpdater):
Rushabh Mehta35c017a2012-11-30 10:57:28 +053014 def load_notification_message(self):
Anand Doshif78d1ae2014-03-28 13:55:00 +053015 dt = self.doctype.lower().replace(" ", "_")
Anand Doshie9baaa62014-02-26 12:35:33 +053016 if int(frappe.db.get_value("Notification Control", None, dt) or 0):
Anand Doshi5b552b52014-04-02 15:03:35 +053017 self.set("__notification_message",
18 frappe.db.get_value("Notification Control", None, dt + "_message"))
Rushabh Mehta2c458992014-04-15 14:36:12 +053019
Anand Doshiee3d5cc2013-03-13 12:58:54 +053020 def validate_posting_time(self):
Makarand Bauskarb0df6612017-05-16 07:59:58 +053021 # set Edit Posting Date and Time to 1 while data import
Sagar Vora67fe1012017-06-19 12:12:22 +053022 if frappe.flags.in_import and self.posting_date:
Makarand Bauskarb0df6612017-05-16 07:59:58 +053023 self.set_posting_time = 1
24
Rushabh Mehta131866a2017-03-14 21:06:15 +053025 if not getattr(self, 'set_posting_time', None):
Rushabh Mehta6b537922017-03-14 16:59:11 +053026 now = now_datetime()
27 self.posting_date = now.strftime('%Y-%m-%d')
28 self.posting_time = now.strftime('%H:%M:%S')
Rushabh Mehta2c458992014-04-15 14:36:12 +053029
Anand Doshi670199b2013-06-10 15:38:01 +053030 def add_calendar_event(self, opts, force=False):
Nabin Haite06d01e2015-06-29 18:38:27 +053031 if cstr(self.contact_by) != cstr(self._prev.contact_by) or \
32 cstr(self.contact_date) != cstr(self._prev.contact_date) or force:
Rushabh Mehta2c458992014-04-15 14:36:12 +053033
Anand Doshie53a81d2013-06-10 15:15:40 +053034 self.delete_events()
35 self._add_calendar_event(opts)
Rushabh Mehta2c458992014-04-15 14:36:12 +053036
Anand Doshie53a81d2013-06-10 15:15:40 +053037 def delete_events(self):
Nabin Haite06d01e2015-06-29 18:38:27 +053038 events = frappe.db.sql_list("""select name from `tabEvent`
39 where ref_type=%s and ref_name=%s""", (self.doctype, self.name))
40 if events:
41 frappe.db.sql("delete from `tabEvent` where name in (%s)"
42 .format(", ".join(['%s']*len(events))), tuple(events))
Anand Doshi414248b2015-08-26 17:56:40 +053043
Anand Doshie53a81d2013-06-10 15:15:40 +053044 def _add_calendar_event(self, opts):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053045 opts = frappe._dict(opts)
Rushabh Mehta2c458992014-04-15 14:36:12 +053046
Anand Doshif78d1ae2014-03-28 13:55:00 +053047 if self.contact_date:
Rushabh Mehta6efbc9d2015-02-13 10:16:41 +053048 event = frappe.get_doc({
Anand Doshie53a81d2013-06-10 15:15:40 +053049 "doctype": "Event",
Anand Doshif78d1ae2014-03-28 13:55:00 +053050 "owner": opts.owner or self.owner,
Anand Doshie53a81d2013-06-10 15:15:40 +053051 "subject": opts.subject,
52 "description": opts.description,
Anand Doshi414248b2015-08-26 17:56:40 +053053 "starts_on": self.contact_date,
Anand Doshie53a81d2013-06-10 15:15:40 +053054 "event_type": "Private",
Anand Doshif78d1ae2014-03-28 13:55:00 +053055 "ref_type": self.doctype,
56 "ref_name": self.name
Rushabh Mehtae88bc8b2014-03-27 17:51:41 +053057 })
Anand Doshif9fc04c2015-02-23 22:14:12 +053058
Pratik Vyas7f9489e2015-02-20 16:22:24 +053059 event.insert(ignore_permissions=True)
Rushabh Mehta2c458992014-04-15 14:36:12 +053060
Anand Doshif78d1ae2014-03-28 13:55:00 +053061 if frappe.db.exists("User", self.contact_by):
Anand Doshi414248b2015-08-26 17:56:40 +053062 frappe.share.add("Event", event.name, self.contact_by,
Nabin Hait3b0adc52015-05-21 16:51:15 +053063 flags={"ignore_share_permission": True})
Rushabh Mehta2c458992014-04-15 14:36:12 +053064
Rushabh Mehta4dca4012013-07-25 17:45:59 +053065 def validate_uom_is_integer(self, uom_field, qty_fields):
Rushabh Mehtacfb6ccf2014-04-03 11:46:52 +053066 validate_uom_is_integer(self, uom_field, qty_fields)
Rushabh Mehta2c458992014-04-15 14:36:12 +053067
Nabin Haitdd38a262014-12-26 13:15:21 +053068 def validate_with_previous_doc(self, ref):
Nabin Hait2bd37772013-07-08 19:00:29 +053069 for key, val in ref.items():
Nabin Hait6e68e0e2013-07-10 15:33:29 +053070 is_child = val.get("is_child_table")
Nabin Hait2bd37772013-07-08 19:00:29 +053071 ref_doc = {}
Nabin Haita9ec49e2013-07-15 16:33:24 +053072 item_ref_dn = []
Nabin Haitdd38a262014-12-26 13:15:21 +053073 for d in self.get_all_children(self.doctype + " Item"):
Anand Doshif78d1ae2014-03-28 13:55:00 +053074 ref_dn = d.get(val["ref_dn_field"])
Nabin Hait6e68e0e2013-07-10 15:33:29 +053075 if ref_dn:
76 if is_child:
77 self.compare_values({key: [ref_dn]}, val["compare_fields"], d)
Nabin Haita9ec49e2013-07-15 16:33:24 +053078 if ref_dn not in item_ref_dn:
79 item_ref_dn.append(ref_dn)
Nabin Hait5e200e42013-07-29 15:29:51 +053080 elif not val.get("allow_duplicate_prev_row_id"):
Rushabh Mehta2c458992014-04-15 14:36:12 +053081 frappe.throw(_("Duplicate row {0} with same {1}").format(d.idx, key))
Nabin Haita9ec49e2013-07-15 16:33:24 +053082 elif ref_dn:
Nabin Hait6e68e0e2013-07-10 15:33:29 +053083 ref_doc.setdefault(key, [])
84 if ref_dn not in ref_doc[key]:
85 ref_doc[key].append(ref_dn)
86 if ref_doc:
Nabin Hait2bd37772013-07-08 19:00:29 +053087 self.compare_values(ref_doc, val["compare_fields"])
Rushabh Mehta2c458992014-04-15 14:36:12 +053088
Nabin Hait2bd37772013-07-08 19:00:29 +053089 def compare_values(self, ref_doc, fields, doc=None):
Rushabh Mehta611b5132015-03-19 17:15:45 +053090 for reference_doctype, ref_dn_list in ref_doc.items():
91 for reference_name in ref_dn_list:
92 prevdoc_values = frappe.db.get_value(reference_doctype, reference_name,
Nabin Hait6e68e0e2013-07-10 15:33:29 +053093 [d[0] for d in fields], as_dict=1)
94
Rushabh Mehtaafacc3d2015-12-01 15:53:07 +053095 if not prevdoc_values:
96 frappe.throw(_("Invalid reference {0} {1}").format(reference_doctype, reference_name))
97
Nabin Hait6e68e0e2013-07-10 15:33:29 +053098 for field, condition in fields:
Anand Doshi63d844b2013-07-10 20:55:15 +053099 if prevdoc_values[field] is not None:
100 self.validate_value(field, condition, prevdoc_values[field], doc)
Anand Doshi414248b2015-08-26 17:56:40 +0530101
102
Nabin Hait28a9e352015-07-08 13:10:52 +0530103 def validate_rate_with_reference_doc(self, ref_details):
104 for ref_dt, ref_dn_field, ref_link_field in ref_details:
105 for d in self.get("items"):
106 if d.get(ref_link_field):
107 ref_rate = frappe.db.get_value(ref_dt + " Item", d.get(ref_link_field), "rate")
Anand Doshi414248b2015-08-26 17:56:40 +0530108
Nabin Hait28a9e352015-07-08 13:10:52 +0530109 if abs(flt(d.rate - ref_rate, d.precision("rate"))) >= .01:
Nabin Hait5e3646a2015-07-08 14:39:03 +0530110 frappe.throw(_("Row #{0}: Rate must be same as {1}: {2} ({3} / {4}) ")
Nabin Hait28a9e352015-07-08 13:10:52 +0530111 .format(d.idx, ref_dt, d.get(ref_dn_field), d.rate, ref_rate))
Rushabh Mehta2c458992014-04-15 14:36:12 +0530112
Saurabh8f24ecb2016-06-16 18:01:58 +0530113 def get_link_filters(self, for_doctype):
Saurabhb6d6b842016-06-27 14:48:26 +0530114 if hasattr(self, "prev_link_mapper") and self.prev_link_mapper.get(for_doctype):
Saurabh8f24ecb2016-06-16 18:01:58 +0530115 fieldname = self.prev_link_mapper[for_doctype]["fieldname"]
Rushabh Mehta6b537922017-03-14 16:59:11 +0530116
Saurabh8f24ecb2016-06-16 18:01:58 +0530117 values = filter(None, tuple([item.as_dict()[fieldname] for item in self.items]))
118
119 if values:
120 ret = {
121 for_doctype : {
122 "filters": [[for_doctype, "name", "in", values]]
123 }
124 }
125 else:
126 ret = None
127 else:
128 ret = None
Rushabh Mehta6b537922017-03-14 16:59:11 +0530129
Saurabh8f24ecb2016-06-16 18:01:58 +0530130 return ret
Anand Doshif9fc04c2015-02-23 22:14:12 +0530131
Anand Doshi11d31132013-06-17 12:51:36 +0530132def delete_events(ref_type, ref_name):
Rushabh Mehta2c458992014-04-15 14:36:12 +0530133 frappe.delete_doc("Event", frappe.db.sql_list("""select name from `tabEvent`
Anand Doshib5fd7882013-06-17 12:55:05 +0530134 where ref_type=%s and ref_name=%s""", (ref_type, ref_name)), for_reload=True)
Rushabh Mehta4dca4012013-07-25 17:45:59 +0530135
Nabin Haitc5fb88c2015-02-09 16:46:46 +0530136def validate_uom_is_integer(doc, uom_field, qty_fields, child_dt=None):
Rushabh Mehta4dca4012013-07-25 17:45:59 +0530137 if isinstance(qty_fields, basestring):
138 qty_fields = [qty_fields]
Rushabh Mehta2c458992014-04-15 14:36:12 +0530139
Rushabh Mehtaf191f852014-04-02 18:09:34 +0530140 distinct_uoms = list(set([d.get(uom_field) for d in doc.get_all_children()]))
Rushabh Mehta2c458992014-04-15 14:36:12 +0530141 integer_uoms = filter(lambda uom: frappe.db.get_value("UOM", uom,
Rushabh Mehtaf191f852014-04-02 18:09:34 +0530142 "must_be_whole_number") or None, distinct_uoms)
Rushabh Mehta2c458992014-04-15 14:36:12 +0530143
Rushabh Mehta4dca4012013-07-25 17:45:59 +0530144 if not integer_uoms:
145 return
146
Nabin Haitc5fb88c2015-02-09 16:46:46 +0530147 for d in doc.get_all_children(parenttype=child_dt):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530148 if d.get(uom_field) in integer_uoms:
Rushabh Mehta4dca4012013-07-25 17:45:59 +0530149 for f in qty_fields:
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530150 qty = d.get(f)
151 if qty:
Rushabh Mehtab7b49f62017-06-19 09:58:48 +0530152 if abs(cint(qty) - flt(qty)) > 0.0000001:
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530153 frappe.throw(_("Quantity ({0}) cannot be a fraction in row {1}").format(qty, d.idx), UOMMustBeIntegerError)