2013-11-20 07:29:58 +00:00
|
|
|
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
|
2013-08-05 09:29:54 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
2012-02-23 07:05:32 +00:00
|
|
|
|
2012-07-19 08:10:31 +00:00
|
|
|
from __future__ import unicode_literals
|
2014-02-14 10:17:51 +00:00
|
|
|
import frappe
|
2014-02-21 09:14:35 +00:00
|
|
|
from frappe import _
|
|
|
|
from frappe.utils import cstr, now_datetime, cint
|
2011-06-08 09:07:15 +00:00
|
|
|
|
2013-12-12 13:42:19 +00:00
|
|
|
from erpnext.controllers.status_updater import StatusUpdater
|
2013-01-15 13:09:21 +00:00
|
|
|
|
2014-01-02 06:17:23 +00:00
|
|
|
|
2014-01-30 13:17:12 +00:00
|
|
|
class TransactionBase(StatusUpdater):
|
2012-11-30 05:27:28 +00:00
|
|
|
def load_notification_message(self):
|
|
|
|
dt = self.doc.doctype.lower().replace(" ", "_")
|
2014-02-26 07:05:33 +00:00
|
|
|
if int(frappe.db.get_value("Notification Control", None, dt) or 0):
|
2012-11-30 05:27:28 +00:00
|
|
|
self.doc.fields["__notification_message"] = \
|
2014-02-26 07:05:33 +00:00
|
|
|
frappe.db.get_value("Notification Control", None, dt + "_message")
|
2013-09-02 11:34:27 +00:00
|
|
|
|
2013-03-13 07:28:54 +00:00
|
|
|
def validate_posting_time(self):
|
|
|
|
if not self.doc.posting_time:
|
2013-03-26 07:03:43 +00:00
|
|
|
self.doc.posting_time = now_datetime().strftime('%H:%M:%S')
|
2013-06-10 09:45:40 +00:00
|
|
|
|
2013-06-10 10:08:01 +00:00
|
|
|
def add_calendar_event(self, opts, force=False):
|
2013-06-10 09:45:40 +00:00
|
|
|
if self.doc.contact_by != cstr(self._prev.contact_by) or \
|
2013-06-10 10:08:01 +00:00
|
|
|
self.doc.contact_date != cstr(self._prev.contact_date) or force:
|
2013-06-10 09:45:40 +00:00
|
|
|
|
|
|
|
self.delete_events()
|
|
|
|
self._add_calendar_event(opts)
|
|
|
|
|
|
|
|
def delete_events(self):
|
2014-02-26 07:05:33 +00:00
|
|
|
frappe.delete_doc("Event", frappe.db.sql_list("""select name from `tabEvent`
|
2013-09-19 05:32:24 +00:00
|
|
|
where ref_type=%s and ref_name=%s""", (self.doc.doctype, self.doc.name)),
|
|
|
|
ignore_permissions=True)
|
2013-06-10 09:45:40 +00:00
|
|
|
|
|
|
|
def _add_calendar_event(self, opts):
|
2014-02-14 10:17:51 +00:00
|
|
|
opts = frappe._dict(opts)
|
2013-06-10 09:45:40 +00:00
|
|
|
|
|
|
|
if self.doc.contact_date:
|
|
|
|
event_doclist = [{
|
|
|
|
"doctype": "Event",
|
|
|
|
"owner": opts.owner or self.doc.owner,
|
|
|
|
"subject": opts.subject,
|
|
|
|
"description": opts.description,
|
|
|
|
"starts_on": self.doc.contact_date + " 10:00:00",
|
|
|
|
"event_type": "Private",
|
|
|
|
"ref_type": self.doc.doctype,
|
|
|
|
"ref_name": self.doc.name
|
|
|
|
}]
|
|
|
|
|
2014-03-11 10:45:05 +00:00
|
|
|
if frappe.db.exists("User", self.doc.contact_by):
|
2013-06-10 09:45:40 +00:00
|
|
|
event_doclist.append({
|
|
|
|
"doctype": "Event User",
|
|
|
|
"parentfield": "event_individuals",
|
|
|
|
"person": self.doc.contact_by
|
|
|
|
})
|
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.bean(event_doclist).insert()
|
2013-07-08 13:30:29 +00:00
|
|
|
|
2013-07-25 12:15:59 +00:00
|
|
|
def validate_uom_is_integer(self, uom_field, qty_fields):
|
|
|
|
validate_uom_is_integer(self.doclist, uom_field, qty_fields)
|
|
|
|
|
2013-07-08 13:30:29 +00:00
|
|
|
def validate_with_previous_doc(self, source_dt, ref):
|
|
|
|
for key, val in ref.items():
|
2013-07-10 10:03:29 +00:00
|
|
|
is_child = val.get("is_child_table")
|
2013-07-08 13:30:29 +00:00
|
|
|
ref_doc = {}
|
2013-07-15 11:03:24 +00:00
|
|
|
item_ref_dn = []
|
2013-07-08 13:30:29 +00:00
|
|
|
for d in self.doclist.get({"doctype": source_dt}):
|
2013-07-10 10:03:29 +00:00
|
|
|
ref_dn = d.fields.get(val["ref_dn_field"])
|
|
|
|
if ref_dn:
|
|
|
|
if is_child:
|
|
|
|
self.compare_values({key: [ref_dn]}, val["compare_fields"], d)
|
2013-07-15 11:03:24 +00:00
|
|
|
if ref_dn not in item_ref_dn:
|
|
|
|
item_ref_dn.append(ref_dn)
|
2013-07-29 09:59:51 +00:00
|
|
|
elif not val.get("allow_duplicate_prev_row_id"):
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.msgprint(_("Row ") + cstr(d.idx + 1) +
|
2013-07-15 11:03:24 +00:00
|
|
|
_(": Duplicate row from same ") + key, raise_exception=1)
|
|
|
|
elif ref_dn:
|
2013-07-10 10:03:29 +00:00
|
|
|
ref_doc.setdefault(key, [])
|
|
|
|
if ref_dn not in ref_doc[key]:
|
|
|
|
ref_doc[key].append(ref_dn)
|
|
|
|
if ref_doc:
|
2013-07-08 13:30:29 +00:00
|
|
|
self.compare_values(ref_doc, val["compare_fields"])
|
|
|
|
|
|
|
|
def compare_values(self, ref_doc, fields, doc=None):
|
2013-07-10 10:03:29 +00:00
|
|
|
for ref_doctype, ref_dn_list in ref_doc.items():
|
|
|
|
for ref_docname in ref_dn_list:
|
2014-02-26 07:05:33 +00:00
|
|
|
prevdoc_values = frappe.db.get_value(ref_doctype, ref_docname,
|
2013-07-10 10:03:29 +00:00
|
|
|
[d[0] for d in fields], as_dict=1)
|
|
|
|
|
|
|
|
for field, condition in fields:
|
2013-07-10 15:25:15 +00:00
|
|
|
if prevdoc_values[field] is not None:
|
|
|
|
self.validate_value(field, condition, prevdoc_values[field], doc)
|
2013-05-15 15:45:57 +00:00
|
|
|
|
2013-06-17 07:21:36 +00:00
|
|
|
def delete_events(ref_type, ref_name):
|
2014-02-26 07:05:33 +00:00
|
|
|
frappe.delete_doc("Event", frappe.db.sql_list("""select name from `tabEvent`
|
2013-06-17 07:25:05 +00:00
|
|
|
where ref_type=%s and ref_name=%s""", (ref_type, ref_name)), for_reload=True)
|
2013-07-25 12:15:59 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
class UOMMustBeIntegerError(frappe.ValidationError): pass
|
2013-07-26 05:51:45 +00:00
|
|
|
|
2013-07-25 12:15:59 +00:00
|
|
|
def validate_uom_is_integer(doclist, uom_field, qty_fields):
|
|
|
|
if isinstance(qty_fields, basestring):
|
|
|
|
qty_fields = [qty_fields]
|
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
integer_uoms = filter(lambda uom: frappe.db.get_value("UOM", uom,
|
2013-07-25 12:15:59 +00:00
|
|
|
"must_be_whole_number") or None, doclist.get_distinct_values(uom_field))
|
|
|
|
|
|
|
|
if not integer_uoms:
|
|
|
|
return
|
|
|
|
|
|
|
|
for d in doclist:
|
|
|
|
if d.fields.get(uom_field) in integer_uoms:
|
|
|
|
for f in qty_fields:
|
|
|
|
if d.fields.get(f):
|
|
|
|
if cint(d.fields[f])!=d.fields[f]:
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.msgprint(_("For UOM") + " '" + d.fields[uom_field] \
|
2013-07-25 12:15:59 +00:00
|
|
|
+ "': " + _("Quantity cannot be a fraction.") \
|
|
|
|
+ " " + _("In Row") + ": " + str(d.idx),
|
2013-07-26 05:51:45 +00:00
|
|
|
raise_exception=UOMMustBeIntegerError)
|