blob: 61758e2d8438c89e4bfda1890263543b72440a5a [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
scmmishra4ae11f42018-10-12 15:18:26 +05305from __future__ import unicode_literals, division
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +05306import 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.
scmmishra685584b2018-10-17 12:41:50 +053013
14 :param fieldname: Checks Overlap for this field
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +053015 """
scmmishra685584b2018-10-17 12:41:50 +053016
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +053017 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)
scmmishra685584b2018-10-17 12:41:50 +053021
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +053022def get_overlap_for(doc, doctype, fieldname, value=None):
Manas Solanki54c42402017-04-12 19:24:12 +053023 """Returns overlaping document for specified field.
scmmishra685584b2018-10-17 12:41:50 +053024
25 :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))
Manas Solanki84e9d452017-11-28 18:04:08 +053035 and name!=%(name)s and docstatus!=2""".format(doctype, fieldname),
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +053036 {
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
scmmishra685584b2018-10-17 12:41:50 +053045
scmmishra7409fe62018-10-22 18:37:03 +053046
Neil Trini Lasradof521a9c2016-07-22 01:28:41 +053047def validate_duplicate_student(students):
48 unique_students= []
49 for stud in students:
50 if stud.student in unique_students:
51 frappe.throw(_("Student {0} - {1} appears Multiple times in row {2} & {3}")
52 .format(stud.student, stud.student_name, unique_students.index(stud.student)+1, stud.idx))
53 else:
54 unique_students.append(stud.student)
scmmishra4ae11f42018-10-12 15:18:26 +053055
scmmishrad133e822018-10-22 12:22:11 +053056 return None