Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright (c) 2015, Frappe Technologies and contributors |
| 3 | # For lice |
| 4 | |
| 5 | from __future__ import unicode_literals |
| 6 | import frappe |
| 7 | from frappe import _ |
| 8 | |
| 9 | class OverlapError(frappe.ValidationError): pass |
| 10 | |
| 11 | def validate_overlap_for(doc, doctype, fieldname, value=None): |
Manas Solanki | 54c4240 | 2017-04-12 19:24:12 +0530 | [diff] [blame] | 12 | """Checks overlap for specified field. |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 13 | |
Manas Solanki | 54c4240 | 2017-04-12 19:24:12 +0530 | [diff] [blame] | 14 | :param fieldname: Checks Overlap for this field |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 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 | |
| 22 | def get_overlap_for(doc, doctype, fieldname, value=None): |
Manas Solanki | 54c4240 | 2017-04-12 19:24:12 +0530 | [diff] [blame] | 23 | """Returns overlaping document for specified field. |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 24 | |
Manas Solanki | 54c4240 | 2017-04-12 19:24:12 +0530 | [diff] [blame] | 25 | :param fieldname: Checks Overlap for this field |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 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)) |
Manas Solanki | 84e9d45 | 2017-11-28 18:04:08 +0530 | [diff] [blame] | 35 | and name!=%(name)s and docstatus!=2""".format(doctype, fieldname), |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 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 Lasrado | f521a9c | 2016-07-22 01:28:41 +0530 | [diff] [blame] | 45 | |
| 46 | def 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) |