blob: 4ba162790c4b151ac15407a5342f289504ea1c21 [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 Doshi5b552b52014-04-02 15:03:35 +053016 self.set("__notification_message",
17 frappe.db.get_value("Notification Control", None, dt + "_message"))
Rushabh Mehta2c458992014-04-15 14:36:12 +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')
Rushabh Mehta2c458992014-04-15 14:36:12 +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:
Rushabh Mehta2c458992014-04-15 14:36:12 +053026
Anand Doshie53a81d2013-06-10 15:15:40 +053027 self.delete_events()
28 self._add_calendar_event(opts)
Rushabh Mehta2c458992014-04-15 14:36:12 +053029
Anand Doshie53a81d2013-06-10 15:15:40 +053030 def delete_events(self):
Rushabh Mehta2c458992014-04-15 14:36:12 +053031 frappe.delete_doc("Event", frappe.db.sql_list("""select name from `tabEvent`
32 where ref_type=%s and ref_name=%s""", (self.doctype, self.name)),
Nabin Haitad6ce4e2013-09-19 11:02:24 +053033 ignore_permissions=True)
Rushabh Mehta2c458992014-04-15 14:36:12 +053034
Anand Doshie53a81d2013-06-10 15:15:40 +053035 def _add_calendar_event(self, opts):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053036 opts = frappe._dict(opts)
Rushabh Mehta2c458992014-04-15 14:36:12 +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 })
Rushabh Mehta2c458992014-04-15 14:36:12 +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 })
Rushabh Mehta2c458992014-04-15 14:36:12 +053055
Rushabh Mehta92ce6932014-05-21 14:59:56 +053056 event_doclist.insert(ignore_permissions=True)
Rushabh Mehta2c458992014-04-15 14:36:12 +053057
Rushabh Mehta4dca4012013-07-25 17:45:59 +053058 def validate_uom_is_integer(self, uom_field, qty_fields):
Rushabh Mehtacfb6ccf2014-04-03 11:46:52 +053059 validate_uom_is_integer(self, uom_field, qty_fields)
Rushabh Mehta2c458992014-04-15 14:36:12 +053060
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 = []
Rushabh Mehtaf191f852014-04-02 18:09:34 +053066 for d in self.get_all_children(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 Mehta2c458992014-04-15 14:36:12 +053074 frappe.throw(_("Duplicate row {0} with same {1}").format(d.idx, key))
Nabin Haita9ec49e2013-07-15 16:33:24 +053075 elif ref_dn:
Nabin Hait6e68e0e2013-07-10 15:33:29 +053076 ref_doc.setdefault(key, [])
77 if ref_dn not in ref_doc[key]:
78 ref_doc[key].append(ref_dn)
79 if ref_doc:
Nabin Hait2bd37772013-07-08 19:00:29 +053080 self.compare_values(ref_doc, val["compare_fields"])
Rushabh Mehta2c458992014-04-15 14:36:12 +053081
Nabin Hait2bd37772013-07-08 19:00:29 +053082 def compare_values(self, ref_doc, fields, doc=None):
Nabin Hait6e68e0e2013-07-10 15:33:29 +053083 for ref_doctype, ref_dn_list in ref_doc.items():
84 for ref_docname in ref_dn_list:
Rushabh Mehta2c458992014-04-15 14:36:12 +053085 prevdoc_values = frappe.db.get_value(ref_doctype, ref_docname,
Nabin Hait6e68e0e2013-07-10 15:33:29 +053086 [d[0] for d in fields], as_dict=1)
87
88 for field, condition in fields:
Anand Doshi63d844b2013-07-10 20:55:15 +053089 if prevdoc_values[field] is not None:
90 self.validate_value(field, condition, prevdoc_values[field], doc)
Rushabh Mehta2c458992014-04-15 14:36:12 +053091
Anand Doshi11d31132013-06-17 12:51:36 +053092def delete_events(ref_type, ref_name):
Rushabh Mehta2c458992014-04-15 14:36:12 +053093 frappe.delete_doc("Event", frappe.db.sql_list("""select name from `tabEvent`
Anand Doshib5fd7882013-06-17 12:55:05 +053094 where ref_type=%s and ref_name=%s""", (ref_type, ref_name)), for_reload=True)
Rushabh Mehta4dca4012013-07-25 17:45:59 +053095
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053096class UOMMustBeIntegerError(frappe.ValidationError): pass
Rushabh Mehtac4f5e4f2013-07-26 11:21:45 +053097
Rushabh Mehtaf191f852014-04-02 18:09:34 +053098def validate_uom_is_integer(doc, uom_field, qty_fields):
Rushabh Mehta4dca4012013-07-25 17:45:59 +053099 if isinstance(qty_fields, basestring):
100 qty_fields = [qty_fields]
Rushabh Mehta2c458992014-04-15 14:36:12 +0530101
Rushabh Mehtaf191f852014-04-02 18:09:34 +0530102 distinct_uoms = list(set([d.get(uom_field) for d in doc.get_all_children()]))
Rushabh Mehta2c458992014-04-15 14:36:12 +0530103 integer_uoms = filter(lambda uom: frappe.db.get_value("UOM", uom,
Rushabh Mehtaf191f852014-04-02 18:09:34 +0530104 "must_be_whole_number") or None, distinct_uoms)
Rushabh Mehta2c458992014-04-15 14:36:12 +0530105
Rushabh Mehta4dca4012013-07-25 17:45:59 +0530106 if not integer_uoms:
107 return
108
Rushabh Mehtaf191f852014-04-02 18:09:34 +0530109 for d in doc.get_all_children():
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 Mehtaf2227d02014-03-31 23:37:40 +0530113 if cint(d.get(f))!=d.get(f):
Rushabh Mehta2c458992014-04-15 14:36:12 +0530114 frappe.throw(_("Quantity cannot be a fraction in row {0}").format(d.idx), UOMMustBeIntegerError)