attendance code cleanup

This commit is contained in:
Nabin Hait 2013-03-06 11:32:33 +05:30
parent 5cf1ef33d6
commit 7804401d03

View File

@ -17,87 +17,70 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import webnotes import webnotes
from webnotes.utils import add_days, getdate, now from webnotes.utils import getdate, nowdate
from webnotes.model import db_exists
from webnotes.model.doc import make_autoname from webnotes.model.doc import make_autoname
from webnotes.model.bean import copy_doclist from webnotes import msgprint, _
from webnotes import msgprint
sql = webnotes.conn.sql sql = webnotes.conn.sql
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
#autoname function
def autoname(self): def autoname(self):
self.doc.name = make_autoname(self.doc.naming_series+'.#####') self.doc.name = make_autoname(self.doc.naming_series+'.#####')
#get employee name based on employee id selected
def get_emp_name(self): def get_emp_name(self):
emp_nm = sql("select employee_name from `tabEmployee` where name=%s", self.doc.employee) return {
"employee_name": webnotes.conn.get_value("Employee",
self.doc.employee_name, "employee_name")
}
#this is done because sometimes user entered wrong employee name while uploading employee attendance
webnotes.conn.set(self.doc, 'employee_name', emp_nm and emp_nm[0][0] or '')
ret = { 'employee_name' : emp_nm and emp_nm[0][0] or ''}
return ret
#validation for duplicate record
def validate_duplicate_record(self): def validate_duplicate_record(self):
res = sql("select name from `tabAttendance` where employee = '%s' and att_date = '%s' and not name = '%s' and docstatus = 1"%(self.doc.employee,self.doc.att_date, self.doc.name)) res = sql("""select name from `tabAttendance` where employee = %s and att_date = %s
and name != %s and docstatus = 1""",
(self.doc.employee, self.doc.att_date, self.doc.name))
if res: if res:
msgprint("Employee's attendance already marked.") msgprint(_("Attendance for the employee: ") + self.doc.employee +
raise Exception _(" already marked"), raise_exception=1)
#check for already record present in leave transaction for same date
def check_leave_record(self): def check_leave_record(self):
if self.doc.status == 'Present': if self.doc.status == 'Present':
chk = sql("select name from `tabLeave Application` where employee=%s and (from_date <= %s and to_date >= %s) and docstatus!=2", (self.doc.employee, self.doc.att_date, self.doc.att_date)) leave = sql("""select name from `tabLeave Application`
if chk: where employee = %s and %s between from_date and to_date and status = 'Approved'
msgprint("Leave Application created for employee "+self.doc.employee+" whom you are trying to mark as 'Present' ") and docstatus = 1""", (self.doc.employee, self.doc.att_date))
raise Exception
if leave:
webnotes.msgprint(_("Employee: ") + self.doc.employee + _(" was on leave on ")
+ self.doc.att_date + _(". You can not mark his attendance as 'Present'"),
raise_exception=1)
def validate_fiscal_year(self): def validate_fiscal_year(self):
from accounts.utils import validate_fiscal_year from accounts.utils import validate_fiscal_year
validate_fiscal_year(self.doc.att_date, self.doc.fiscal_year) validate_fiscal_year(self.doc.att_date, self.doc.fiscal_year)
def validate_att_date(self): def validate_att_date(self):
import datetime if getdate(self.doc.att_date) > getdate(nowdate()):
if getdate(self.doc.att_date)>getdate(datetime.datetime.now().date().strftime('%Y-%m-%d')): msgprint(_("Attendance can not be marked for future dates"), raise_exception=1)
msgprint("Attendance can not be marked for future dates")
raise Exception
# Validate employee
#-------------------
def validate_employee(self): def validate_employee(self):
emp = sql("select name, status from `tabEmployee` where name = '%s'" % self.doc.employee) emp = sql("select name from `tabEmployee` where name = %s and status = 'Active'",
self.doc.employee)
if not emp: if not emp:
msgprint("Employee: %s does not exists in the system" % self.doc.employee, raise_exception=1) msgprint(_("Employee: ") + self.doc.employee +
elif emp[0][1] != 'Active': _(" not active or does not exists in the system"), raise_exception=1)
msgprint("Employee: %s is not Active" % self.doc.employee, raise_exception=1)
def validate(self): def validate(self):
import utilities import utilities
utilities.validate_status(self.doc.status, ["Present", "Absent", "Half Day"]) utilities.validate_status(self.doc.status, ["Present", "Absent", "Half Day"])
self.validate_fiscal_year() self.validate_fiscal_year()
self.validate_att_date() self.validate_att_date()
self.validate_duplicate_record() self.validate_duplicate_record()
self.check_leave_record() self.check_leave_record()
def on_update(self): def on_update(self):
#self.validate() # this is done because sometimes user entered wrong employee name
# while uploading employee attendance
#this is done because sometimes user entered wrong employee name while uploading employee attendance employee_name = webnotes.conn.get_value("Employee", self.doc.employee, "employee_name")
x=self.get_emp_name() webnotes.conn.set(self.doc, 'employee_name', employee_name)
def on_submit(self):
#this is done because while uploading attendance chnage docstatus to 1 i.e. submit
webnotes.conn.set(self.doc,'docstatus',1)
pass