Leave allocation fix
This commit is contained in:
parent
73083dc960
commit
102842ba79
@ -7,62 +7,64 @@ import frappe
|
|||||||
from frappe.utils import cint, cstr, flt, nowdate
|
from frappe.utils import cint, cstr, flt, nowdate
|
||||||
from frappe.model.doc import Document
|
from frappe.model.doc import Document
|
||||||
from frappe.model.code import get_obj
|
from frappe.model.code import get_obj
|
||||||
from frappe import msgprint
|
from frappe import msgprint, _
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
# Get Employees
|
|
||||||
# **********************************************************************
|
|
||||||
def get_employees(self):
|
|
||||||
lst1 = [[self.doc.employee_type,"employment_type"],[self.doc.branch,"branch"],[self.doc.designation,"designation"],[self.doc.department, "department"],[self.doc.grade,"grade"]]
|
|
||||||
condition = "where "
|
|
||||||
flag = 0
|
|
||||||
for l in lst1:
|
|
||||||
if(l[0]):
|
|
||||||
if flag == 0:
|
|
||||||
condition += l[1] + "= '" + l[0] +"'"
|
|
||||||
else:
|
|
||||||
condition += " and " + l[1]+ "= '" +l[0] +"'"
|
|
||||||
flag = 1
|
|
||||||
emp_query = "select name from `tabEmployee` "
|
|
||||||
if flag == 1:
|
|
||||||
emp_query += condition
|
|
||||||
e = frappe.db.sql(emp_query)
|
|
||||||
return e
|
|
||||||
|
|
||||||
# ----------------
|
def get_employees(self):
|
||||||
# validate values
|
lst1 = [[self.doc.employee_type,"employment_type"],[self.doc.branch,"branch"],[self.doc.designation,"designation"],[self.doc.department, "department"],[self.doc.grade,"grade"]]
|
||||||
# ----------------
|
condition = "where "
|
||||||
def validate_values(self):
|
flag = 0
|
||||||
val_dict = {self.doc.fiscal_year:'Fiscal Year', self.doc.leave_type:'Leave Type', self.doc.no_of_days:'New Leaves Allocated'}
|
for l in lst1:
|
||||||
for d in val_dict:
|
if(l[0]):
|
||||||
if not d:
|
if flag == 0:
|
||||||
msgprint("Please enter : "+val_dict[d])
|
condition += l[1] + "= '" + l[0] +"'"
|
||||||
raise Exception
|
else:
|
||||||
|
condition += " and " + l[1]+ "= '" +l[0] +"'"
|
||||||
|
flag = 1
|
||||||
|
emp_query = "select name from `tabEmployee` "
|
||||||
|
if flag == 1:
|
||||||
|
emp_query += condition
|
||||||
|
e = frappe.db.sql(emp_query)
|
||||||
|
return e
|
||||||
|
|
||||||
|
def validate_values(self):
|
||||||
|
meta = frappe.get_doctype(self.doc.doctype)
|
||||||
|
for f in ["fiscal_year", "leave_type", "no_of_days"]:
|
||||||
|
if not self.doc.fields[f]:
|
||||||
|
frappe.throw(_(meta.get_label(f)) + _(" is mandatory"))
|
||||||
|
|
||||||
# Allocation
|
def allocate_leave(self):
|
||||||
# **********************************************************************
|
self.validate_values()
|
||||||
def allocate_leave(self):
|
leave_allocated_for = []
|
||||||
self.validate_values()
|
employees = self.get_employees()
|
||||||
for d in self.get_employees():
|
if not employees:
|
||||||
la = Document('Leave Allocation')
|
frappe.throw(_("No employee found"))
|
||||||
la.employee = cstr(d[0])
|
|
||||||
la.employee_name = frappe.db.get_value('Employee',cstr(d[0]),'employee_name')
|
for d in self.get_employees():
|
||||||
la.leave_type = self.doc.leave_type
|
try:
|
||||||
la.fiscal_year = self.doc.fiscal_year
|
la = Document('Leave Allocation')
|
||||||
la.posting_date = nowdate()
|
la.employee = cstr(d[0])
|
||||||
la.carry_forward = cint(self.doc.carry_forward)
|
la.employee_name = frappe.db.get_value('Employee',cstr(d[0]),'employee_name')
|
||||||
la.new_leaves_allocated = flt(self.doc.no_of_days)
|
la.leave_type = self.doc.leave_type
|
||||||
la_obj = get_obj(doc=la)
|
la.fiscal_year = self.doc.fiscal_year
|
||||||
la_obj.doc.docstatus = 1
|
la.posting_date = nowdate()
|
||||||
la_obj.validate()
|
la.carry_forward = cint(self.doc.carry_forward)
|
||||||
la_obj.on_update()
|
la.new_leaves_allocated = flt(self.doc.no_of_days)
|
||||||
la_obj.doc.save(1)
|
la_obj = get_obj(doc=la)
|
||||||
msgprint("Leaves Allocated Successfully")
|
la_obj.doc.docstatus = 1
|
||||||
|
la_obj.validate()
|
||||||
|
la_obj.on_update()
|
||||||
|
la_obj.doc.save(1)
|
||||||
|
leave_allocated_for.append(d[0])
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
if leave_allocated_for:
|
||||||
|
msgprint("Leaves Allocated Successfully for " + ", ".join(leave_allocated_for))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user