[feature] Ability to set UOM as Integer-only and related validations.
diff --git a/utilities/transaction_base.py b/utilities/transaction_base.py
index 2900bf6..e980236 100644
--- a/utilities/transaction_base.py
+++ b/utilities/transaction_base.py
@@ -17,7 +17,7 @@
from __future__ import unicode_literals
import webnotes
from webnotes import msgprint, _
-from webnotes.utils import load_json, cstr, flt, now_datetime
+from webnotes.utils import load_json, cstr, flt, now_datetime, cint
from webnotes.model.doc import addchild
from controllers.status_updater import StatusUpdater
@@ -276,6 +276,9 @@
webnotes.bean(event_doclist).insert()
+ def validate_uom_is_integer(self, uom_field, qty_fields):
+ validate_uom_is_integer(self.doclist, uom_field, qty_fields)
+
def validate_with_previous_doc(self, source_dt, ref):
for key, val in ref.items():
is_child = val.get("is_child_table")
@@ -482,3 +485,23 @@
def delete_events(ref_type, ref_name):
webnotes.delete_doc("Event", webnotes.conn.sql_list("""select name from `tabEvent`
where ref_type=%s and ref_name=%s""", (ref_type, ref_name)), for_reload=True)
+
+def validate_uom_is_integer(doclist, uom_field, qty_fields):
+ if isinstance(qty_fields, basestring):
+ qty_fields = [qty_fields]
+
+ integer_uoms = filter(lambda uom: webnotes.conn.get_value("UOM", uom,
+ "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]:
+ webnotes.msgprint(_("For UOM") + " '" + d.fields[uom_field] \
+ + "': " + _("Quantity cannot be a fraction.") \
+ + " " + _("In Row") + ": " + str(d.idx),
+ raise_exception=True)