blob: be74aeb920d7929a1b4c7c08ad431a6e82e43d9e [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 Haita1c96de2014-02-21 14:44:35 +05306from frappe import _
7from frappe.utils import cstr, now_datetime, cint
Rushabh Mehta6efbc9d2015-02-13 10:16:41 +05308import frappe.share
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +05309
Rushabh Mehta1f847992013-12-12 19:12:19 +053010from erpnext.controllers.status_updater import StatusUpdater
Anand Doshi756dca72013-01-15 18:39:21 +053011
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):
Anand Doshif78d1ae2014-03-28 13:55:00 +053021 if not self.posting_time:
22 self.posting_time = now_datetime().strftime('%H:%M:%S')
Rushabh Mehta2c458992014-04-15 14:36:12 +053023
Anand Doshi670199b2013-06-10 15:38:01 +053024 def add_calendar_event(self, opts, force=False):
Nabin Haite06d01e2015-06-29 18:38:27 +053025 if cstr(self.contact_by) != cstr(self._prev.contact_by) or \
26 cstr(self.contact_date) != cstr(self._prev.contact_date) or force:
Rushabh Mehta2c458992014-04-15 14:36:12 +053027
Anand Doshie53a81d2013-06-10 15:15:40 +053028 self.delete_events()
29 self._add_calendar_event(opts)
Rushabh Mehta2c458992014-04-15 14:36:12 +053030
Anand Doshie53a81d2013-06-10 15:15:40 +053031 def delete_events(self):
Nabin Haite06d01e2015-06-29 18:38:27 +053032 events = frappe.db.sql_list("""select name from `tabEvent`
33 where ref_type=%s and ref_name=%s""", (self.doctype, self.name))
34 if events:
35 frappe.db.sql("delete from `tabEvent` where name in (%s)"
36 .format(", ".join(['%s']*len(events))), tuple(events))
37
38 frappe.db.sql("delete from `tabEvent Role` where parent in (%s)"
39 .format(", ".join(['%s']*len(events))), tuple(events))
Rushabh Mehta2c458992014-04-15 14:36:12 +053040
Anand Doshie53a81d2013-06-10 15:15:40 +053041 def _add_calendar_event(self, opts):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053042 opts = frappe._dict(opts)
Rushabh Mehta2c458992014-04-15 14:36:12 +053043
Anand Doshif78d1ae2014-03-28 13:55:00 +053044 if self.contact_date:
Rushabh Mehta6efbc9d2015-02-13 10:16:41 +053045 event = frappe.get_doc({
Anand Doshie53a81d2013-06-10 15:15:40 +053046 "doctype": "Event",
Anand Doshif78d1ae2014-03-28 13:55:00 +053047 "owner": opts.owner or self.owner,
Anand Doshie53a81d2013-06-10 15:15:40 +053048 "subject": opts.subject,
49 "description": opts.description,
Anand Doshif78d1ae2014-03-28 13:55:00 +053050 "starts_on": self.contact_date + " 10:00:00",
Anand Doshie53a81d2013-06-10 15:15:40 +053051 "event_type": "Private",
Anand Doshif78d1ae2014-03-28 13:55:00 +053052 "ref_type": self.doctype,
53 "ref_name": self.name
Rushabh Mehtae88bc8b2014-03-27 17:51:41 +053054 })
Anand Doshif9fc04c2015-02-23 22:14:12 +053055
Pratik Vyas7f9489e2015-02-20 16:22:24 +053056 event.insert(ignore_permissions=True)
Rushabh Mehta2c458992014-04-15 14:36:12 +053057
Anand Doshif78d1ae2014-03-28 13:55:00 +053058 if frappe.db.exists("User", self.contact_by):
Nabin Hait3b0adc52015-05-21 16:51:15 +053059 frappe.share.add("Event", event.name, self.contact_by,
60 flags={"ignore_share_permission": True})
Rushabh Mehta2c458992014-04-15 14:36:12 +053061
Rushabh Mehta4dca4012013-07-25 17:45:59 +053062 def validate_uom_is_integer(self, uom_field, qty_fields):
Rushabh Mehtacfb6ccf2014-04-03 11:46:52 +053063 validate_uom_is_integer(self, uom_field, qty_fields)
Rushabh Mehta2c458992014-04-15 14:36:12 +053064
Nabin Haitdd38a262014-12-26 13:15:21 +053065 def validate_with_previous_doc(self, ref):
Nabin Hait2bd37772013-07-08 19:00:29 +053066 for key, val in ref.items():
Nabin Hait6e68e0e2013-07-10 15:33:29 +053067 is_child = val.get("is_child_table")
Nabin Hait2bd37772013-07-08 19:00:29 +053068 ref_doc = {}
Nabin Haita9ec49e2013-07-15 16:33:24 +053069 item_ref_dn = []
Nabin Haitdd38a262014-12-26 13:15:21 +053070 for d in self.get_all_children(self.doctype + " Item"):
Anand Doshif78d1ae2014-03-28 13:55:00 +053071 ref_dn = d.get(val["ref_dn_field"])
Nabin Hait6e68e0e2013-07-10 15:33:29 +053072 if ref_dn:
73 if is_child:
74 self.compare_values({key: [ref_dn]}, val["compare_fields"], d)
Nabin Haita9ec49e2013-07-15 16:33:24 +053075 if ref_dn not in item_ref_dn:
76 item_ref_dn.append(ref_dn)
Nabin Hait5e200e42013-07-29 15:29:51 +053077 elif not val.get("allow_duplicate_prev_row_id"):
Rushabh Mehta2c458992014-04-15 14:36:12 +053078 frappe.throw(_("Duplicate row {0} with same {1}").format(d.idx, key))
Nabin Haita9ec49e2013-07-15 16:33:24 +053079 elif ref_dn:
Nabin Hait6e68e0e2013-07-10 15:33:29 +053080 ref_doc.setdefault(key, [])
81 if ref_dn not in ref_doc[key]:
82 ref_doc[key].append(ref_dn)
83 if ref_doc:
Nabin Hait2bd37772013-07-08 19:00:29 +053084 self.compare_values(ref_doc, val["compare_fields"])
Rushabh Mehta2c458992014-04-15 14:36:12 +053085
Nabin Hait2bd37772013-07-08 19:00:29 +053086 def compare_values(self, ref_doc, fields, doc=None):
Rushabh Mehta611b5132015-03-19 17:15:45 +053087 for reference_doctype, ref_dn_list in ref_doc.items():
88 for reference_name in ref_dn_list:
89 prevdoc_values = frappe.db.get_value(reference_doctype, reference_name,
Nabin Hait6e68e0e2013-07-10 15:33:29 +053090 [d[0] for d in fields], as_dict=1)
91
92 for field, condition in fields:
Anand Doshi63d844b2013-07-10 20:55:15 +053093 if prevdoc_values[field] is not None:
94 self.validate_value(field, condition, prevdoc_values[field], doc)
Rushabh Mehta2c458992014-04-15 14:36:12 +053095
Anand Doshif9fc04c2015-02-23 22:14:12 +053096
Anand Doshi11d31132013-06-17 12:51:36 +053097def delete_events(ref_type, ref_name):
Rushabh Mehta2c458992014-04-15 14:36:12 +053098 frappe.delete_doc("Event", frappe.db.sql_list("""select name from `tabEvent`
Anand Doshib5fd7882013-06-17 12:55:05 +053099 where ref_type=%s and ref_name=%s""", (ref_type, ref_name)), for_reload=True)
Rushabh Mehta4dca4012013-07-25 17:45:59 +0530100
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530101class UOMMustBeIntegerError(frappe.ValidationError): pass
Rushabh Mehtac4f5e4f2013-07-26 11:21:45 +0530102
Nabin Haitc5fb88c2015-02-09 16:46:46 +0530103def validate_uom_is_integer(doc, uom_field, qty_fields, child_dt=None):
Rushabh Mehta4dca4012013-07-25 17:45:59 +0530104 if isinstance(qty_fields, basestring):
105 qty_fields = [qty_fields]
Rushabh Mehta2c458992014-04-15 14:36:12 +0530106
Rushabh Mehtaf191f852014-04-02 18:09:34 +0530107 distinct_uoms = list(set([d.get(uom_field) for d in doc.get_all_children()]))
Rushabh Mehta2c458992014-04-15 14:36:12 +0530108 integer_uoms = filter(lambda uom: frappe.db.get_value("UOM", uom,
Rushabh Mehtaf191f852014-04-02 18:09:34 +0530109 "must_be_whole_number") or None, distinct_uoms)
Rushabh Mehta2c458992014-04-15 14:36:12 +0530110
Rushabh Mehta4dca4012013-07-25 17:45:59 +0530111 if not integer_uoms:
112 return
113
Nabin Haitc5fb88c2015-02-09 16:46:46 +0530114 for d in doc.get_all_children(parenttype=child_dt):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530115 if d.get(uom_field) in integer_uoms:
Rushabh Mehta4dca4012013-07-25 17:45:59 +0530116 for f in qty_fields:
Anand Doshif78d1ae2014-03-28 13:55:00 +0530117 if d.get(f):
Rushabh Mehtaf2227d02014-03-31 23:37:40 +0530118 if cint(d.get(f))!=d.get(f):
Rushabh Mehta2c458992014-04-15 14:36:12 +0530119 frappe.throw(_("Quantity cannot be a fraction in row {0}").format(d.idx), UOMMustBeIntegerError)