From 62a93533fc3be605138951f234803a2f5880d168 Mon Sep 17 00:00:00 2001 From: Jamsheer Date: Tue, 8 May 2018 12:24:39 +0530 Subject: [PATCH] Leave Policy - Validate Annual alloction for leave type --- .../hr/doctype/leave_policy/leave_policy.js | 25 ++++++++++++++++++- .../hr/doctype/leave_policy/leave_policy.py | 8 +++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/erpnext/hr/doctype/leave_policy/leave_policy.js b/erpnext/hr/doctype/leave_policy/leave_policy.js index 309215e778..fdf8e0cdbb 100644 --- a/erpnext/hr/doctype/leave_policy/leave_policy.js +++ b/erpnext/hr/doctype/leave_policy/leave_policy.js @@ -2,7 +2,30 @@ // For license information, please see license.txt frappe.ui.form.on('Leave Policy', { - refresh: function(frm) { +}); +frappe.ui.form.on('Leave Policy Detail',{ + leave_type: function(frm, cdt, cdn) { + var child = locals[cdt][cdn]; + if(child.leave_type){ + frappe.call({ + method: "frappe.client.get_value", + args: { + doctype: "Leave Type", + fieldname: "max_leaves_allowed", + filters: { name: child.leave_type } + }, + callback: function(r) { + if (r.message) { + child.annual_allocation = r.message.max_leaves_allowed; + refresh_field("leave_policy_details"); + } + } + }); + } + else{ + child.annual_allocation = ""; + refresh_field("leave_policy_details"); + } } }); diff --git a/erpnext/hr/doctype/leave_policy/leave_policy.py b/erpnext/hr/doctype/leave_policy/leave_policy.py index 1da84c2e43..964a5de83e 100644 --- a/erpnext/hr/doctype/leave_policy/leave_policy.py +++ b/erpnext/hr/doctype/leave_policy/leave_policy.py @@ -4,7 +4,13 @@ from __future__ import unicode_literals import frappe +from frappe import _ from frappe.model.document import Document class LeavePolicy(Document): - pass + def validate(self): + if self.leave_policy_details: + for lp_detail in self.leave_policy_details: + max_leaves_allowed = frappe.db.get_value("Leave Type", lp_detail.leave_type, "max_leaves_allowed") + if max_leaves_allowed > 0 and lp_detail.annual_allocation > max_leaves_allowed: + frappe.throw(_("Maximum leave allowed in the leave type {0} is {1}").format(lp_detail.leave_type, max_leaves_allowed))