blob: 3adbae4b5ef57bc120f4d76acea2af3ce7c977f2 [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):
12 """Checks overlap for specified feild.
13
14 :param fieldname: Checks Overlap for this feild
15 """
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):
23 """Returns overlaping document for specified feild.
24
25 :param fieldname: Checks Overlap for this feild
26 """
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)