From 3c55463b4b86e9715e194d8c8268ee6190a5d30c Mon Sep 17 00:00:00 2001 From: pateljannat Date: Thu, 7 Jan 2021 12:41:44 +0530 Subject: [PATCH 1/3] fix: validation for ldc against supplier and section code --- .../lower_deduction_certificate.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/erpnext/regional/doctype/lower_deduction_certificate/lower_deduction_certificate.py b/erpnext/regional/doctype/lower_deduction_certificate/lower_deduction_certificate.py index e8a8ed87505..d4e7c123624 100644 --- a/erpnext/regional/doctype/lower_deduction_certificate/lower_deduction_certificate.py +++ b/erpnext/regional/doctype/lower_deduction_certificate/lower_deduction_certificate.py @@ -5,12 +5,16 @@ from __future__ import unicode_literals import frappe from frappe import _ -from frappe.utils import getdate +from frappe.utils import getdate, get_link_to_form from frappe.model.document import Document from erpnext.accounts.utils import get_fiscal_year class LowerDeductionCertificate(Document): def validate(self): + self.validate_dates() + self.validate_supplier_against_section_code() + + def validate_dates(self): if getdate(self.valid_upto) < getdate(self.valid_from): frappe.throw(_("Valid Upto date cannot be before Valid From date")) @@ -24,3 +28,10 @@ class LowerDeductionCertificate(Document): <= fiscal_year.year_end_date): frappe.throw(_("Valid Upto date not in Fiscal Year {0}").format(frappe.bold(self.fiscal_year))) + def validate_supplier_against_section_code(self): + duplicate_certificate = frappe.db.get_value('Lower Deduction Certificate', {'supplier': self.supplier, 'section_code': self.section_code}, ['name']) + if duplicate_certificate: + certificate_link = get_link_to_form('Lower Deduction Certificate', duplicate_certificate) + frappe.throw(_("There is already a Lower Deduction Certificate {0} for Supplier {1} against Section Code {2}") + .format(certificate_link, frappe.bold(self.supplier), frappe.bold(self.section_code))) + From f1107598976d5584beaa3011654e78159b09b618 Mon Sep 17 00:00:00 2001 From: pateljannat Date: Wed, 20 Jan 2021 18:12:48 +0530 Subject: [PATCH 2/3] fix: date validation --- .../lower_deduction_certificate.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/erpnext/regional/doctype/lower_deduction_certificate/lower_deduction_certificate.py b/erpnext/regional/doctype/lower_deduction_certificate/lower_deduction_certificate.py index d4e7c123624..b0a656af306 100644 --- a/erpnext/regional/doctype/lower_deduction_certificate/lower_deduction_certificate.py +++ b/erpnext/regional/doctype/lower_deduction_certificate/lower_deduction_certificate.py @@ -29,9 +29,19 @@ class LowerDeductionCertificate(Document): frappe.throw(_("Valid Upto date not in Fiscal Year {0}").format(frappe.bold(self.fiscal_year))) def validate_supplier_against_section_code(self): - duplicate_certificate = frappe.db.get_value('Lower Deduction Certificate', {'supplier': self.supplier, 'section_code': self.section_code}, ['name']) - if duplicate_certificate: - certificate_link = get_link_to_form('Lower Deduction Certificate', duplicate_certificate) - frappe.throw(_("There is already a Lower Deduction Certificate {0} for Supplier {1} against Section Code {2}") + duplicate_certificate = frappe.db.get_value('Lower Deduction Certificate', {'supplier': self.supplier, 'section_code': self.section_code}, ['name', 'valid_from', 'valid_upto'], as_dict=True) + if duplicate_certificate and self.are_dates_overlapping(duplicate_certificate): + certificate_link = get_link_to_form('Lower Deduction Certificate', duplicate_certificate.name) + frappe.throw(_("There is already a valid Lower Deduction Certificate {0} for Supplier {1} against Section Code {2} for this time period.") .format(certificate_link, frappe.bold(self.supplier), frappe.bold(self.section_code))) + def are_dates_overlapping(self,duplicate_certificate): + valid_from = duplicate_certificate.valid_from + valid_upto = duplicate_certificate.valid_upto + if valid_from <= getdate(self.valid_from) and getdate(self.valid_from) <= valid_upto: + return True + elif valid_from <= getdate(self.valid_upto) and getdate(self.valid_upto) <= valid_upto: + return True + elif getdate(self.valid_from) <= valid_from and valid_upto <= getdate(self.valid_upto): + return True + return False \ No newline at end of file From 1ab95413beacf98f7f7627ee2f8b6a3a2b0930ce Mon Sep 17 00:00:00 2001 From: pateljannat Date: Tue, 9 Feb 2021 17:35:17 +0530 Subject: [PATCH 3/3] fix: conditions simplified --- .../lower_deduction_certificate.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/erpnext/regional/doctype/lower_deduction_certificate/lower_deduction_certificate.py b/erpnext/regional/doctype/lower_deduction_certificate/lower_deduction_certificate.py index b0a656af306..ad60db05595 100644 --- a/erpnext/regional/doctype/lower_deduction_certificate/lower_deduction_certificate.py +++ b/erpnext/regional/doctype/lower_deduction_certificate/lower_deduction_certificate.py @@ -38,9 +38,9 @@ class LowerDeductionCertificate(Document): def are_dates_overlapping(self,duplicate_certificate): valid_from = duplicate_certificate.valid_from valid_upto = duplicate_certificate.valid_upto - if valid_from <= getdate(self.valid_from) and getdate(self.valid_from) <= valid_upto: + if valid_from <= getdate(self.valid_from) <= valid_upto: return True - elif valid_from <= getdate(self.valid_upto) and getdate(self.valid_upto) <= valid_upto: + elif valid_from <= getdate(self.valid_upto) <= valid_upto: return True elif getdate(self.valid_from) <= valid_from and valid_upto <= getdate(self.valid_upto): return True