error fixed in leave calculation

This commit is contained in:
Nabin Hait 2011-10-11 17:11:24 +05:30
parent fb37157163
commit db3728955b

View File

@ -17,108 +17,116 @@ import datetime
# ----------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------
class DocType: class DocType:
def __init__(self, doc, doclist): def __init__(self, doc, doclist):
self.doc = doc self.doc = doc
self.doclist = doclist self.doclist = doclist
# ******************************************** client triggers *********************************************** # ******************************************** client triggers ***********************************************
# ------------------ # ------------------
# get leave balance # get leave balance
# ------------------ # ------------------
def get_leave_balance(self): def get_leave_balance(self):
leave_all = sql("select total_leaves_allocated 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)) leave_all = sql("select total_leaves_allocated 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))
leave_all = leave_all and flt(leave_all[0][0]) or 0 leave_all = leave_all and flt(leave_all[0][0]) or 0
leave_app = sql("select 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, self.doc.fiscal_year)) leave_app = sql("select 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, self.doc.fiscal_year))
leave_app = leave_app and flt(leave_app[0][0]) or 0 leave_app = leave_app and flt(leave_app[0][0]) or 0
ret = {'leave_balance':leave_all - leave_app} ret = {'leave_balance':leave_all - leave_app}
return ret return ret
# ************************************************ utilities ************************************************* # ************************************************ utilities *************************************************
# ------------------- # -------------------
# get total holidays def get_holidays(self):
# ------------------- """
def get_holidays(self): get total holidays
tot_hol = sql("select count(*) from `tabHoliday List Detail` h1, `tabHoliday List` h2, `tabEmployee` e1 where e1.name = '%s' and h1.parent = h2.name and e1.holiday_list = h2.name and h1.holiday_date between '%s' and '%s'"% (self.doc.employee, self.doc.from_date, self.doc.to_date)) """
if not tot_hol: tot_hol = sql("select count(*) from `tabHoliday List Detail` h1, `tabHoliday List` h2, `tabEmployee` e1 where e1.name = '%s' and h1.parent = h2.name and e1.holiday_list = h2.name and h1.holiday_date between '%s' and '%s'"% (self.doc.employee, self.doc.from_date, self.doc.to_date))
tot_hol = sql("select count(*) from `tabHoliday List Detail` h1, `tabHoliday List` h2 where h1.parent = h2.name and h1.holiday_date between '%s' and '%s' and ifnull(h2.is_default,0) = 1 and h2.fiscal_year = %s"% (self.doc.from_date, self.doc.to_date, self.doc.fiscal_year)) if not tot_hol:
return tot_hol and flt(tot_hol[0][0]) or 0 tot_hol = sql("select count(*) from `tabHoliday List Detail` h1, `tabHoliday List` h2 where h1.parent = h2.name and h1.holiday_date between '%s' and '%s' and ifnull(h2.is_default,0) = 1 and h2.fiscal_year = %s"% (self.doc.from_date, self.doc.to_date, self.doc.fiscal_year))
return tot_hol and flt(tot_hol[0][0]) or 0
# --------------------- # ---------------------
# get total leave days # get total leave days
# --------------------- # ---------------------
def get_total_leave_days(self): def get_total_leave_days(self):
tot_days = date_diff(self.doc.to_date, self.doc.from_date) + 1 """
holidays = self.get_holidays() Calculates total leave days based on input and holidays
ret = {'total_leave_days':flt(tot_days)-flt(holidays)} """
return ret ret = {'total_leave_days' : 0.5}
if not self.doc.half_day:
tot_days = date_diff(self.doc.to_date, self.doc.from_date) + 1
holidays = self.get_holidays()
ret = {
'total_leave_days' : flt(tot_days)-flt(holidays)
}
return ret
# ************************************************ validate ************************************************* # ************************************************ validate *************************************************
# ----------------- # -----------------
# validate to date # validate to date
# ----------------- # -----------------
def validate_to_date(self): def validate_to_date(self):
if (getdate(self.doc.to_date) < getdate(self.doc.from_date)): if self.doc.from_date and self.doc.to_date and (getdate(self.doc.to_date) < getdate(self.doc.from_date)):
msgprint("To date cannot be before from date") msgprint("To date cannot be before from date")
raise Exception raise Exception
# -------------------------------- # --------------------------------
# check whether leave type is lwp # check whether leave type is lwp
# -------------------------------- # --------------------------------
def is_lwp(self): def is_lwp(self):
lwp = sql("select is_lwp from `tabLeave Type` where name = %s", self.doc.leave_type) lwp = sql("select is_lwp from `tabLeave Type` where name = %s", self.doc.leave_type)
return lwp and cint(lwp[0][0]) or 0 return lwp and cint(lwp[0][0]) or 0
# ------------------------ # ------------------------
# validate balance leaves # validate balance leaves
# ------------------------ # ------------------------
def validate_balance_leaves(self): def validate_balance_leaves(self):
if not self.is_lwp(): if self.doc.from_date and self.doc.to_date and not self.is_lwp():
bal = self.get_leave_balance() bal = self.get_leave_balance()
tot_leaves = self.get_total_leave_days() tot_leaves = self.get_total_leave_days()
bal, tot_leaves = bal, tot_leaves bal, tot_leaves = bal, tot_leaves
set(self.doc,'leave_balance',flt(bal['leave_balance'])) set(self.doc,'leave_balance',flt(bal['leave_balance']))
set(self.doc,'total_leave_days',flt(tot_leaves['total_leave_days'])) set(self.doc,'total_leave_days',flt(tot_leaves['total_leave_days']))
if flt(bal['leave_balance']) < flt(tot_leaves['total_leave_days']): if flt(bal['leave_balance']) < flt(tot_leaves['total_leave_days']):
msgprint("Employee : %s cannot apply for %s of more than %s days" % (self.doc.employee, self.doc.leave_type, flt(bal['leave_balance']))) msgprint("Employee : %s cannot apply for %s of more than %s days" % (self.doc.employee, self.doc.leave_type, flt(bal['leave_balance'])))
raise Exception raise Exception
# #
# validate overlapping leaves # validate overlapping leaves
# #
def validate_leave_overlap(self): def validate_leave_overlap(self):
for d in sql("""select name, leave_type, posting_date, from_date, to_date for d in sql("""select name, leave_type, posting_date, from_date, to_date
from `tabLeave Application` from `tabLeave Application`
where where
(from_date <= %(to_date)s and to_date >= %(from_date)s) (from_date <= %(to_date)s and to_date >= %(from_date)s)
and employee = %(employee)s and employee = %(employee)s
and docstatus = 1 and docstatus = 1
and name != %(name)s""", self.doc.fields, as_dict = 1): and name != %(name)s""", self.doc.fields, as_dict = 1):
msgprint("Employee : %s has already applied for %s between %s and %s on %s. Please refer Leave Application : %s" % (self.doc.employee, cstr(d['leave_type']), formatdate(d['from_date']), formatdate(d['to_date']), formatdate(d['posting_date']), d['name']), raise_exception = 1) msgprint("Employee : %s has already applied for %s between %s and %s on %s. Please refer Leave Application : %s" % (self.doc.employee, cstr(d['leave_type']), formatdate(d['from_date']), formatdate(d['to_date']), formatdate(d['posting_date']), d['name']), raise_exception = 1)
# --------------------------------------------------------------------- # ---------------------------------------------------------------------
# validate max days for which leave can be applied for particular type # validate max days for which leave can be applied for particular type
# --------------------------------------------------------------------- # ---------------------------------------------------------------------
def validate_max_days(self): def validate_max_days(self):
max_days = sql("select max_days_allowed from `tabLeave Type` where name = '%s'" %(self.doc.leave_type)) max_days = sql("select max_days_allowed from `tabLeave Type` where name = '%s'" %(self.doc.leave_type))
max_days = max_days and flt(max_days[0][0]) or 0 max_days = max_days and flt(max_days[0][0]) or 0
if max_days and self.doc.total_leave_days > max_days: if max_days and self.doc.total_leave_days > max_days:
msgprint("Sorry ! You cannot apply for %s for more than %s days" % (self.doc.leave_type, max_days)) msgprint("Sorry ! You cannot apply for %s for more than %s days" % (self.doc.leave_type, max_days))
raise Exception raise Exception
# --------- # ---------
# validate # validate
# --------- # ---------
def validate(self): def validate(self):
self.validate_to_date() self.validate_to_date()
self.validate_balance_leaves() self.validate_balance_leaves()
self.validate_leave_overlap() self.validate_leave_overlap()
self.validate_max_days() self.validate_max_days()