blob: d3769a23bde033c732cdc97974eae78e392c6088 [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 _
Faris Ansari184491b2018-01-15 14:18:53 +05308from frappe.utils import cstr, now_datetime, cint, flt, get_time
Rushabh Mehta1f847992013-12-12 19:12:19 +05309from erpnext.controllers.status_updater import StatusUpdater
Anand Doshi756dca72013-01-15 18:39:21 +053010
Achilles Rasquinha1697a7a2018-02-15 11:39:45 +053011from six import string_types
12
Nabin Hait3cf67a42015-07-24 13:26:36 +053013class UOMMustBeIntegerError(frappe.ValidationError): pass
Rushabh Mehtab09d9da2014-01-02 11:47:23 +053014
Nabin Haitb5be7ba2014-01-30 18:47:12 +053015class TransactionBase(StatusUpdater):
Anand Doshiee3d5cc2013-03-13 12:58:54 +053016 def validate_posting_time(self):
Makarand Bauskarb0df6612017-05-16 07:59:58 +053017 # set Edit Posting Date and Time to 1 while data import
Sagar Vora67fe1012017-06-19 12:12:22 +053018 if frappe.flags.in_import and self.posting_date:
Makarand Bauskarb0df6612017-05-16 07:59:58 +053019 self.set_posting_time = 1
20
Rushabh Mehta131866a2017-03-14 21:06:15 +053021 if not getattr(self, 'set_posting_time', None):
Rushabh Mehta6b537922017-03-14 16:59:11 +053022 now = now_datetime()
23 self.posting_date = now.strftime('%Y-%m-%d')
Nabin Hait945f5022017-09-29 15:11:50 +053024 self.posting_time = now.strftime('%H:%M:%S.%f')
Manas Solankic6d56112018-01-18 11:20:24 +053025 elif self.posting_time:
Faris Ansari184491b2018-01-15 14:18:53 +053026 try:
27 get_time(self.posting_time)
28 except ValueError:
29 frappe.throw(_('Invalid Posting Time'))
Rushabh Mehta2c458992014-04-15 14:36:12 +053030
Anand Doshi670199b2013-06-10 15:38:01 +053031 def add_calendar_event(self, opts, force=False):
Nabin Haite06d01e2015-06-29 18:38:27 +053032 if cstr(self.contact_by) != cstr(self._prev.contact_by) or \
Zarrar0702f292018-03-12 18:49:54 +053033 cstr(self.contact_date) != cstr(self._prev.contact_date) or force or \
34 (hasattr(self, "ends_on") and cstr(self.ends_on) != cstr(self._prev.ends_on)):
Rushabh Mehta2c458992014-04-15 14:36:12 +053035
Anand Doshie53a81d2013-06-10 15:15:40 +053036 self.delete_events()
37 self._add_calendar_event(opts)
Rushabh Mehta2c458992014-04-15 14:36:12 +053038
Anand Doshie53a81d2013-06-10 15:15:40 +053039 def delete_events(self):
Nabin Haite06d01e2015-06-29 18:38:27 +053040 events = frappe.db.sql_list("""select name from `tabEvent`
41 where ref_type=%s and ref_name=%s""", (self.doctype, self.name))
42 if events:
Zarrar6578bc12018-03-01 10:54:55 +053043 frappe.db.sql("delete from `tabEvent` where name in ({0})"
Nabin Haite06d01e2015-06-29 18:38:27 +053044 .format(", ".join(['%s']*len(events))), tuple(events))
Anand Doshi414248b2015-08-26 17:56:40 +053045
Anand Doshie53a81d2013-06-10 15:15:40 +053046 def _add_calendar_event(self, opts):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053047 opts = frappe._dict(opts)
Rushabh Mehta2c458992014-04-15 14:36:12 +053048
Anand Doshif78d1ae2014-03-28 13:55:00 +053049 if self.contact_date:
Rushabh Mehta6efbc9d2015-02-13 10:16:41 +053050 event = frappe.get_doc({
Anand Doshie53a81d2013-06-10 15:15:40 +053051 "doctype": "Event",
Anand Doshif78d1ae2014-03-28 13:55:00 +053052 "owner": opts.owner or self.owner,
Anand Doshie53a81d2013-06-10 15:15:40 +053053 "subject": opts.subject,
54 "description": opts.description,
Anand Doshi414248b2015-08-26 17:56:40 +053055 "starts_on": self.contact_date,
Zarrar0702f292018-03-12 18:49:54 +053056 "ends_on": opts.ends_on,
Anand Doshie53a81d2013-06-10 15:15:40 +053057 "event_type": "Private",
Anand Doshif78d1ae2014-03-28 13:55:00 +053058 "ref_type": self.doctype,
59 "ref_name": self.name
Rushabh Mehtae88bc8b2014-03-27 17:51:41 +053060 })
Anand Doshif9fc04c2015-02-23 22:14:12 +053061
Pratik Vyas7f9489e2015-02-20 16:22:24 +053062 event.insert(ignore_permissions=True)
Rushabh Mehta2c458992014-04-15 14:36:12 +053063
Anand Doshif78d1ae2014-03-28 13:55:00 +053064 if frappe.db.exists("User", self.contact_by):
Anand Doshi414248b2015-08-26 17:56:40 +053065 frappe.share.add("Event", event.name, self.contact_by,
Nabin Hait3b0adc52015-05-21 16:51:15 +053066 flags={"ignore_share_permission": True})
Rushabh Mehta2c458992014-04-15 14:36:12 +053067
Rushabh Mehta4dca4012013-07-25 17:45:59 +053068 def validate_uom_is_integer(self, uom_field, qty_fields):
Rushabh Mehtacfb6ccf2014-04-03 11:46:52 +053069 validate_uom_is_integer(self, uom_field, qty_fields)
Rushabh Mehta2c458992014-04-15 14:36:12 +053070
Nabin Haitdd38a262014-12-26 13:15:21 +053071 def validate_with_previous_doc(self, ref):
rohitwaghchaure0df95fa2018-03-01 11:31:33 +053072 self.exclude_fields = ["conversion_factor", "uom"] if self.get('is_return') else []
73
Nabin Hait2bd37772013-07-08 19:00:29 +053074 for key, val in ref.items():
Nabin Hait6e68e0e2013-07-10 15:33:29 +053075 is_child = val.get("is_child_table")
Nabin Hait2bd37772013-07-08 19:00:29 +053076 ref_doc = {}
Nabin Haita9ec49e2013-07-15 16:33:24 +053077 item_ref_dn = []
Nabin Haitdd38a262014-12-26 13:15:21 +053078 for d in self.get_all_children(self.doctype + " Item"):
Anand Doshif78d1ae2014-03-28 13:55:00 +053079 ref_dn = d.get(val["ref_dn_field"])
Nabin Hait6e68e0e2013-07-10 15:33:29 +053080 if ref_dn:
81 if is_child:
82 self.compare_values({key: [ref_dn]}, val["compare_fields"], d)
Nabin Haita9ec49e2013-07-15 16:33:24 +053083 if ref_dn not in item_ref_dn:
84 item_ref_dn.append(ref_dn)
Nabin Hait5e200e42013-07-29 15:29:51 +053085 elif not val.get("allow_duplicate_prev_row_id"):
Rushabh Mehta2c458992014-04-15 14:36:12 +053086 frappe.throw(_("Duplicate row {0} with same {1}").format(d.idx, key))
Nabin Haita9ec49e2013-07-15 16:33:24 +053087 elif ref_dn:
Nabin Hait6e68e0e2013-07-10 15:33:29 +053088 ref_doc.setdefault(key, [])
89 if ref_dn not in ref_doc[key]:
90 ref_doc[key].append(ref_dn)
91 if ref_doc:
Nabin Hait2bd37772013-07-08 19:00:29 +053092 self.compare_values(ref_doc, val["compare_fields"])
Rushabh Mehta2c458992014-04-15 14:36:12 +053093
Nabin Hait2bd37772013-07-08 19:00:29 +053094 def compare_values(self, ref_doc, fields, doc=None):
Rushabh Mehta611b5132015-03-19 17:15:45 +053095 for reference_doctype, ref_dn_list in ref_doc.items():
96 for reference_name in ref_dn_list:
97 prevdoc_values = frappe.db.get_value(reference_doctype, reference_name,
Nabin Hait6e68e0e2013-07-10 15:33:29 +053098 [d[0] for d in fields], as_dict=1)
99
Rushabh Mehtaafacc3d2015-12-01 15:53:07 +0530100 if not prevdoc_values:
101 frappe.throw(_("Invalid reference {0} {1}").format(reference_doctype, reference_name))
102
Nabin Hait6e68e0e2013-07-10 15:33:29 +0530103 for field, condition in fields:
rohitwaghchaure0df95fa2018-03-01 11:31:33 +0530104 if prevdoc_values[field] is not None and field not in self.exclude_fields:
Anand Doshi63d844b2013-07-10 20:55:15 +0530105 self.validate_value(field, condition, prevdoc_values[field], doc)
Anand Doshi414248b2015-08-26 17:56:40 +0530106
107
Nabin Hait28a9e352015-07-08 13:10:52 +0530108 def validate_rate_with_reference_doc(self, ref_details):
109 for ref_dt, ref_dn_field, ref_link_field in ref_details:
110 for d in self.get("items"):
111 if d.get(ref_link_field):
112 ref_rate = frappe.db.get_value(ref_dt + " Item", d.get(ref_link_field), "rate")
Anand Doshi414248b2015-08-26 17:56:40 +0530113
Nabin Hait28a9e352015-07-08 13:10:52 +0530114 if abs(flt(d.rate - ref_rate, d.precision("rate"))) >= .01:
Nabin Hait5e3646a2015-07-08 14:39:03 +0530115 frappe.throw(_("Row #{0}: Rate must be same as {1}: {2} ({3} / {4}) ")
Nabin Hait28a9e352015-07-08 13:10:52 +0530116 .format(d.idx, ref_dt, d.get(ref_dn_field), d.rate, ref_rate))
Rushabh Mehta2c458992014-04-15 14:36:12 +0530117
Saurabh8f24ecb2016-06-16 18:01:58 +0530118 def get_link_filters(self, for_doctype):
Saurabhb6d6b842016-06-27 14:48:26 +0530119 if hasattr(self, "prev_link_mapper") and self.prev_link_mapper.get(for_doctype):
Saurabh8f24ecb2016-06-16 18:01:58 +0530120 fieldname = self.prev_link_mapper[for_doctype]["fieldname"]
Rushabh Mehta6b537922017-03-14 16:59:11 +0530121
Saurabh8f24ecb2016-06-16 18:01:58 +0530122 values = filter(None, tuple([item.as_dict()[fieldname] for item in self.items]))
123
124 if values:
125 ret = {
126 for_doctype : {
127 "filters": [[for_doctype, "name", "in", values]]
128 }
129 }
130 else:
131 ret = None
132 else:
133 ret = None
Rushabh Mehta6b537922017-03-14 16:59:11 +0530134
Saurabh8f24ecb2016-06-16 18:01:58 +0530135 return ret
Anand Doshif9fc04c2015-02-23 22:14:12 +0530136
Anand Doshi11d31132013-06-17 12:51:36 +0530137def delete_events(ref_type, ref_name):
Rushabh Mehta2c458992014-04-15 14:36:12 +0530138 frappe.delete_doc("Event", frappe.db.sql_list("""select name from `tabEvent`
Anand Doshib5fd7882013-06-17 12:55:05 +0530139 where ref_type=%s and ref_name=%s""", (ref_type, ref_name)), for_reload=True)
Rushabh Mehta4dca4012013-07-25 17:45:59 +0530140
Nabin Haitc5fb88c2015-02-09 16:46:46 +0530141def validate_uom_is_integer(doc, uom_field, qty_fields, child_dt=None):
Achilles Rasquinha1697a7a2018-02-15 11:39:45 +0530142 if isinstance(qty_fields, string_types):
Rushabh Mehta4dca4012013-07-25 17:45:59 +0530143 qty_fields = [qty_fields]
Rushabh Mehta2c458992014-04-15 14:36:12 +0530144
Rushabh Mehtaf191f852014-04-02 18:09:34 +0530145 distinct_uoms = list(set([d.get(uom_field) for d in doc.get_all_children()]))
Rushabh Mehta2c458992014-04-15 14:36:12 +0530146 integer_uoms = filter(lambda uom: frappe.db.get_value("UOM", uom,
Rushabh Mehtaf191f852014-04-02 18:09:34 +0530147 "must_be_whole_number") or None, distinct_uoms)
Rushabh Mehta2c458992014-04-15 14:36:12 +0530148
Rushabh Mehta4dca4012013-07-25 17:45:59 +0530149 if not integer_uoms:
150 return
151
Nabin Haitc5fb88c2015-02-09 16:46:46 +0530152 for d in doc.get_all_children(parenttype=child_dt):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530153 if d.get(uom_field) in integer_uoms:
Rushabh Mehta4dca4012013-07-25 17:45:59 +0530154 for f in qty_fields:
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530155 qty = d.get(f)
156 if qty:
Rushabh Mehtab7b49f62017-06-19 09:58:48 +0530157 if abs(cint(qty) - flt(qty)) > 0.0000001:
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530158 frappe.throw(_("Quantity ({0}) cannot be a fraction in row {1}").format(qty, d.idx), UOMMustBeIntegerError)