From 65a5e47e688a336fb4445bfbd9b5c6d23b2baa44 Mon Sep 17 00:00:00 2001 From: Jamsheer Date: Tue, 22 May 2018 14:24:33 +0530 Subject: [PATCH] Payroll Period - validate dates and overlap --- .../doctype/payroll_period/payroll_period.py | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/erpnext/hr/doctype/payroll_period/payroll_period.py b/erpnext/hr/doctype/payroll_period/payroll_period.py index 0bf2b7958e..c7d87e6c70 100644 --- a/erpnext/hr/doctype/payroll_period/payroll_period.py +++ b/erpnext/hr/doctype/payroll_period/payroll_period.py @@ -4,11 +4,45 @@ from __future__ import unicode_literals import frappe -from frappe.utils import date_diff, getdate +from frappe import _ +from frappe.utils import date_diff, getdate, formatdate from frappe.model.document import Document class PayrollPeriod(Document): - pass + def validate(self): + self.validate_dates() + self.validate_overlap() + + def validate_dates(self): + if getdate(self.start_date) > getdate(self.end_date): + frappe.throw(_("End date can not be less than start date")) + + def validate_overlap(self): + query = """ + select name + from `tab{0}` + where name != %(name)s + and company = %(company)s and (start_date between %(start_date)s and %(end_date)s \ + or end_date between %(start_date)s and %(end_date)s \ + or (start_date < %(start_date)s and end_date > %(end_date)s)) + """ + if not self.name: + # hack! if name is null, it could cause problems with != + self.name = "New "+self.doctype + + overlap_doc = frappe.db.sql(query.format(self.doctype),{ + "start_date": self.start_date, + "end_date": self.end_date, + "name": self.name, + "company": self.company + }, as_dict = 1) + + if overlap_doc: + msg = _("A {0} exists between {1} and {2} (").format(self.doctype, + formatdate(self.start_date), formatdate(self.end_date)) \ + + """ {1}""".format(self.doctype, overlap_doc[0].name) \ + + _(") for {0}").format(self.company) + frappe.throw(msg) def get_payroll_period_days(start_date, end_date, company): payroll_period_dates = frappe.db.sql("""