blob: 2ca46d5542b2568c2bda5056b75a3f2dbe8a9b34 [file] [log] [blame]
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +05301# -*- coding: utf-8 -*-
2# Copyright (c) 2015, Frappe Technologies and contributors
3# For lice
4
5from __future__ import unicode_literals
6import frappe
7from frappe import _
8
9class OverlapError(frappe.ValidationError): pass
10
11def validate_overlap_for(doc, doctype, fieldname, value=None):
Manas Solanki54c42402017-04-12 19:24:12 +053012 """Checks overlap for specified field.
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +053013
Manas Solanki54c42402017-04-12 19:24:12 +053014 :param fieldname: Checks Overlap for this field
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +053015 """
16
17 existing = get_overlap_for(doc, doctype, fieldname, value)
18 if existing:
19 frappe.throw(_("This {0} conflicts with {1} for {2} {3}").format(doc.doctype, existing.name,
20 doc.meta.get_label(fieldname) if not value else fieldname , value or doc.get(fieldname)), OverlapError)
21
22def get_overlap_for(doc, doctype, fieldname, value=None):
Manas Solanki54c42402017-04-12 19:24:12 +053023 """Returns overlaping document for specified field.
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +053024
Manas Solanki54c42402017-04-12 19:24:12 +053025 :param fieldname: Checks Overlap for this field
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +053026 """
27
28 existing = frappe.db.sql("""select name, from_time, to_time from `tab{0}`
29 where `{1}`=%(val)s and schedule_date = %(schedule_date)s and
30 (
31 (from_time > %(from_time)s and from_time < %(to_time)s) or
32 (to_time > %(from_time)s and to_time < %(to_time)s) or
33 (%(from_time)s > from_time and %(from_time)s < to_time) or
34 (%(from_time)s = from_time and %(to_time)s = to_time))
35 and name!=%(name)s""".format(doctype, fieldname),
36 {
37 "schedule_date": doc.schedule_date,
38 "val": value or doc.get(fieldname),
39 "from_time": doc.from_time,
40 "to_time": doc.to_time,
41 "name": doc.name or "No Name"
42 }, as_dict=True)
43
44 return existing[0] if existing else None
Neil Trini Lasradof521a9c2016-07-22 01:28:41 +053045
46def validate_duplicate_student(students):
47 unique_students= []
48 for stud in students:
49 if stud.student in unique_students:
50 frappe.throw(_("Student {0} - {1} appears Multiple times in row {2} & {3}")
51 .format(stud.student, stud.student_name, unique_students.index(stud.student)+1, stud.idx))
52 else:
53 unique_students.append(stud.student)