allow to change new leave allocation after submit -- raise appropriate validations
This commit is contained in:
parent
b77ee2f3a7
commit
92e6ebdf71
@ -8,131 +8,129 @@
|
|||||||
#
|
#
|
||||||
# This program is distributed in the hope that it will be useful,
|
# This program is distributed in the hope that it will be useful,
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
# GNU General Public License for more details.
|
# GNU General Public License for more details.
|
||||||
#
|
#
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
# Please edit this list and import only required elements
|
# Please edit this list and import only required elements
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import webnotes
|
import webnotes
|
||||||
|
from webnotes.utils import cint, flt
|
||||||
from webnotes.utils import add_days, add_months, add_years, cint, cstr, date_diff, default_fields, flt, fmt_money, formatdate, getTraceback, get_defaults, get_first_day, get_last_day, getdate, has_common, month_name, now, nowdate, replace_newlines, sendmail, set_default, str_esc_quote, user_format, validate_email_add
|
from webnotes import msgprint
|
||||||
from webnotes.model import db_exists
|
|
||||||
from webnotes.model.doc import Document, addchild, getchildren, make_autoname
|
|
||||||
from webnotes.model.doclist import getlist, copy_doclist
|
|
||||||
from webnotes.model.code import get_obj, get_server_obj, run_server_obj, updatedb, check_syntax
|
|
||||||
from webnotes import session, form, msgprint, errprint
|
|
||||||
|
|
||||||
set = webnotes.conn.set
|
set = webnotes.conn.set
|
||||||
sql = webnotes.conn.sql
|
sql = webnotes.conn.sql
|
||||||
get_value = webnotes.conn.get_value
|
|
||||||
in_transaction = webnotes.conn.in_transaction
|
|
||||||
convert_to_lists = webnotes.conn.convert_to_lists
|
|
||||||
import datetime
|
|
||||||
|
|
||||||
# -----------------------------------------------------------------------------------------
|
|
||||||
class DocType:
|
class DocType:
|
||||||
def __init__(self, doc, doclist):
|
def __init__(self, doc, doclist):
|
||||||
self.doc = doc
|
self.doc, self.doclist = doc, doclist
|
||||||
self.doclist = doclist
|
|
||||||
|
|
||||||
|
def validate(self):
|
||||||
|
self.validate_new_leaves_allocated_value()
|
||||||
|
self.check_existing_leave_allocation()
|
||||||
|
self.validate_new_leaves_allocated()
|
||||||
|
|
||||||
# ************************************************ utilities *************************************************
|
def on_update_after_submit(self):
|
||||||
# --------------
|
self.validate_new_leaves_allocated_value()
|
||||||
# get leave bal
|
self.validate_new_leaves_allocated()
|
||||||
# --------------
|
|
||||||
def get_leave_bal(self, prev_fyear):
|
|
||||||
# leaves allocates
|
|
||||||
tot_leaves_all = sql("select SUM(total_leaves_allocated) from `tabLeave Allocation` where employee = '%s' and leave_type = '%s' and fiscal_year = '%s' and docstatus = 1 and name != '%s'" % (self.doc.employee, self.doc.leave_type, prev_fyear, self.doc.name))
|
|
||||||
tot_leaves_all = tot_leaves_all and flt(tot_leaves_all[0][0]) or 0
|
|
||||||
|
|
||||||
# leaves applied
|
def on_update(self):
|
||||||
tot_leaves_app = sql("select SUM(total_leave_days) from `tabLeave Application` where employee = '%s' and leave_type = '%s' and fiscal_year = '%s' and docstatus = 1" % (self.doc.employee, self.doc.leave_type, prev_fyear))
|
self.get_total_allocated_leaves()
|
||||||
tot_leaves_app = tot_leaves_app and flt(tot_leaves_app[0][0]) or 0
|
|
||||||
|
|
||||||
return tot_leaves_all - tot_leaves_app
|
def on_cancel(self):
|
||||||
|
self.check_for_leave_application()
|
||||||
|
|
||||||
|
def validate_new_leaves_allocated_value(self):
|
||||||
|
"""validate that leave allocation is in multiples of 0.5"""
|
||||||
|
if flt(self.doc.new_leaves_allocated) % 0.5:
|
||||||
|
guess = round(flt(self.doc.new_leaves_allocated) * 2.0) / 2.0
|
||||||
|
|
||||||
# ******************************************** client triggers ***********************************************
|
msgprint("""New Leaves Allocated should be a multiple of 0.5.
|
||||||
|
Perhaps you should enter %s or %s""" % (guess, guess + 0.5),
|
||||||
|
raise_exception=1)
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
def check_existing_leave_allocation(self):
|
||||||
# check whether carry forward is allowed or not for this leave type
|
"""check whether leave for same type is already allocated or not"""
|
||||||
# ------------------------------------------------------------------
|
leave_allocation = sql("""select name from `tabLeave Allocation`
|
||||||
def allow_carry_forward(self):
|
where employee=%s and leave_type=%s and fiscal_year=%s and docstatus=1""",
|
||||||
cf = sql("select is_carry_forward from `tabLeave Type` where name = %s" , self.doc.leave_type)
|
(self.doc.employee, self.doc.leave_type, self.doc.fiscal_year))
|
||||||
cf = cf and cint(cf[0][0]) or 0
|
if leave_allocation:
|
||||||
if not cf:
|
msgprint("""%s is already allocated to Employee: %s for Fiscal Year: %s.
|
||||||
set(self.doc,'carry_forward',0)
|
Please refere Leave Allocation: \
|
||||||
msgprint("Sorry ! You cannot carry forward %s" % (self.doc.leave_type))
|
<a href="#Form/Leave Allocation/%s">%s</a>""" % \
|
||||||
raise Exception
|
(self.doc.leave_type, self.doc.employee, self.doc.fiscal_year,
|
||||||
|
leave_allocation[0][0], leave_allocation[0][0]), raise_exception=1)
|
||||||
|
|
||||||
# ---------------------------
|
def validate_new_leaves_allocated(self):
|
||||||
# get carry forwarded leaves
|
"""check if Total Leaves Allocated >= Leave Applications"""
|
||||||
# ---------------------------
|
self.doc.total_leaves_allocated = flt(self.doc.carry_forwarded_leaves) + \
|
||||||
def get_carry_forwarded_leaves(self):
|
flt(self.doc.new_leaves_allocated)
|
||||||
if self.doc.carry_forward: self.allow_carry_forward()
|
leaves_applied = self.get_leaves_applied(self.doc.fiscal_year)
|
||||||
prev_fiscal_year = sql("select name from `tabFiscal Year` where name < '%s' order by name desc limit 1" % (self.doc.fiscal_year))
|
if leaves_applied > self.doc.total_leaves_allocated:
|
||||||
prev_fiscal_year = prev_fiscal_year and prev_fiscal_year[0][0] or ''
|
expected_new_leaves = flt(self.doc.new_leaves_allocated) + \
|
||||||
ret = {}
|
(leaves_applied - self.doc.total_leaves_allocated)
|
||||||
prev_bal = 0
|
msgprint("""Employee: %s has already applied for %s leaves.
|
||||||
if prev_fiscal_year and cint(self.doc.carry_forward) == 1:
|
Hence, New Leaves Allocated should be atleast %s""" % \
|
||||||
prev_bal = self.get_leave_bal(prev_fiscal_year)
|
(self.doc.employee, leaves_applied, expected_new_leaves),
|
||||||
ret = {
|
raise_exception=1)
|
||||||
'carry_forwarded_leaves' : prev_bal,
|
|
||||||
'total_leaves_allocated' : flt(prev_bal) + flt(self.doc.new_leaves_allocated)
|
|
||||||
}
|
|
||||||
return ret
|
|
||||||
|
|
||||||
|
def get_leave_bal(self, prev_fyear):
|
||||||
|
return self.get_leaves_allocated(prev_fyear) - self.get_leaves_applied(prev_fyear)
|
||||||
|
|
||||||
# ********************************************** validate *****************************************************
|
def get_leaves_applied(self, fiscal_year):
|
||||||
|
leaves_applied = sql("""select SUM(ifnull(total_leave_days, 0))
|
||||||
|
from `tabLeave Application` where employee=%s and leave_type=%s
|
||||||
|
and fiscal_year=%s and docstatus=1""",
|
||||||
|
(self.doc.employee, self.doc.leave_type, fiscal_year))
|
||||||
|
return leaves_applied and flt(leaves_applied[0][0]) or 0
|
||||||
|
|
||||||
# ---------------------------
|
def get_leaves_allocated(self, fiscal_year):
|
||||||
# get total allocated leaves
|
leaves_allocated = sql("""select SUM(ifnull(total_leaves_allocated, 0))
|
||||||
# ---------------------------
|
from `tabLeave Allocation` where employee=%s and leave_type=%s
|
||||||
def get_total_allocated_leaves(self):
|
and fiscal_year=%s and docstatus=1 and name!=%s""",
|
||||||
leave_det = self.get_carry_forwarded_leaves()
|
(self.doc.employee, self.doc.leave_type, fiscal_year, self.doc.name))
|
||||||
set(self.doc,'carry_forwarded_leaves',flt(leave_det['carry_forwarded_leaves']))
|
return leaves_allocated and flt(leaves_allocated[0][0]) or 0
|
||||||
set(self.doc,'total_leaves_allocated',flt(leave_det['total_leaves_allocated']))
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------------------------
|
def allow_carry_forward(self):
|
||||||
# validate leave (i.e. check whether leave for same type is already allocated or not)
|
"""check whether carry forward is allowed or not for this leave type"""
|
||||||
# ------------------------------------------------------------------------------------
|
cf = sql("""select is_carry_forward from `tabLeave Type` where name = %s""",
|
||||||
def validate_allocated_leave(self):
|
self.doc.leave_type)
|
||||||
l = sql("select name from `tabLeave Allocation` where employee = '%s' and leave_type = '%s' and fiscal_year = '%s' and docstatus = 1" % (self.doc.employee, self.doc.leave_type, self.doc.fiscal_year))
|
cf = cf and cint(cf[0][0]) or 0
|
||||||
l = l and l[0][0] or ''
|
if not cf:
|
||||||
if l:
|
set(self.doc,'carry_forward',0)
|
||||||
msgprint("%s is allocated to Employee: %s for Fiscal Year : %s. Please refer Leave Allocation : %s" % (self.doc.leave_type, self.doc.employee, self.doc.fiscal_year, l))
|
msgprint("Sorry! You cannot carry forward %s" % (self.doc.leave_type),
|
||||||
raise Exception
|
raise_exception=1)
|
||||||
|
|
||||||
# ---------
|
def get_carry_forwarded_leaves(self):
|
||||||
# validate
|
if self.doc.carry_forward:
|
||||||
# ---------
|
self.allow_carry_forward()
|
||||||
def validate(self):
|
prev_fiscal_year = sql("""select name from `tabFiscal Year`
|
||||||
self.validate_allocated_leave()
|
where name < %s order by name desc limit 1""", self.doc.fiscal_year)
|
||||||
|
prev_fiscal_year = prev_fiscal_year and prev_fiscal_year[0][0] or ''
|
||||||
|
prev_bal = 0
|
||||||
|
if prev_fiscal_year and cint(self.doc.carry_forward) == 1:
|
||||||
|
prev_bal = self.get_leave_bal(prev_fiscal_year)
|
||||||
|
ret = {
|
||||||
|
'carry_forwarded_leaves': prev_bal,
|
||||||
|
'total_leaves_allocated': flt(prev_bal) + flt(self.doc.new_leaves_allocated)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
|
||||||
# ----------
|
def get_total_allocated_leaves(self):
|
||||||
# on update
|
leave_det = self.get_carry_forwarded_leaves()
|
||||||
# ----------
|
set(self.doc,'carry_forwarded_leaves',flt(leave_det['carry_forwarded_leaves']))
|
||||||
def on_update(self):
|
set(self.doc,'total_leaves_allocated',flt(leave_det['total_leaves_allocated']))
|
||||||
self.get_total_allocated_leaves()
|
|
||||||
|
|
||||||
|
def check_for_leave_application(self):
|
||||||
|
exists = sql("""select name from `tabLeave Application`
|
||||||
|
where employee=%s and leave_type=%s and fiscal_year=%s and docstatus=1""",
|
||||||
|
(self.doc.employee, self.doc.leave_type, self.doc.fiscal_year))
|
||||||
|
if exists:
|
||||||
|
msgprint("""Cannot cancel this Leave Allocation as \
|
||||||
|
Employee : %s has already applied for %s.
|
||||||
|
Please check Leave Application: \
|
||||||
|
<a href="#Form/Leave Application/%s">%s</a>""" % \
|
||||||
|
(self.doc.employee, self.doc.leave_type, exists[0][0], exists[0][0]))
|
||||||
|
raise Exception
|
||||||
|
|
||||||
# ********************************************** cancel ********************************************************
|
|
||||||
|
|
||||||
# -------------------------
|
|
||||||
# check for applied leaves
|
|
||||||
# -------------------------
|
|
||||||
def check_for_leave_application(self):
|
|
||||||
chk = sql("select name from `tabLeave Application` where employee = '%s' and leave_type = '%s' and fiscal_year = '%s' and docstatus = 1" % (self.doc.employee, self.doc.leave_type, self.doc.fiscal_year))
|
|
||||||
chk = chk and chk[0][0] or ''
|
|
||||||
if chk:
|
|
||||||
msgprint("Cannot cancel this Leave Allocation as Employee : %s has already applied for %s. Please check Leave Application : %s" % (self.doc.employee, self.doc.leave_type, chk))
|
|
||||||
raise Exception
|
|
||||||
|
|
||||||
# -------
|
|
||||||
# cancel
|
|
||||||
# -------
|
|
||||||
def on_cancel(self):
|
|
||||||
self.check_for_leave_application()
|
|
||||||
|
@ -3,9 +3,9 @@
|
|||||||
|
|
||||||
# These values are common in all dictionaries
|
# These values are common in all dictionaries
|
||||||
{
|
{
|
||||||
u'creation': '2012-05-15 12:14:45',
|
u'creation': '2012-10-02 18:51:49',
|
||||||
u'docstatus': 0,
|
u'docstatus': 0,
|
||||||
u'modified': '2012-10-02 11:21:31',
|
u'modified': '2012-10-17 12:51:44',
|
||||||
u'modified_by': u'Administrator',
|
u'modified_by': u'Administrator',
|
||||||
u'owner': u'Administrator'
|
u'owner': u'Administrator'
|
||||||
},
|
},
|
||||||
@ -185,6 +185,7 @@
|
|||||||
|
|
||||||
# DocField
|
# DocField
|
||||||
{
|
{
|
||||||
|
'allow_on_submit': 1,
|
||||||
u'doctype': u'DocField',
|
u'doctype': u'DocField',
|
||||||
'fieldname': u'new_leaves_allocated',
|
'fieldname': u'new_leaves_allocated',
|
||||||
'fieldtype': u'Currency',
|
'fieldtype': u'Currency',
|
||||||
|
Loading…
Reference in New Issue
Block a user