[fixes] patch, test cases and validations
This commit is contained in:
parent
d0b0a80be3
commit
a2c668cb77
@ -160,7 +160,7 @@
|
|||||||
"print_hide": 0,
|
"print_hide": 0,
|
||||||
"read_only": 0,
|
"read_only": 0,
|
||||||
"report_hide": 0,
|
"report_hide": 0,
|
||||||
"reqd": 0,
|
"reqd": 1,
|
||||||
"search_index": 0,
|
"search_index": 0,
|
||||||
"set_only_once": 0,
|
"set_only_once": 0,
|
||||||
"unique": 0
|
"unique": 0
|
||||||
@ -182,7 +182,7 @@
|
|||||||
"print_hide": 0,
|
"print_hide": 0,
|
||||||
"read_only": 0,
|
"read_only": 0,
|
||||||
"report_hide": 0,
|
"report_hide": 0,
|
||||||
"reqd": 0,
|
"reqd": 1,
|
||||||
"search_index": 0,
|
"search_index": 0,
|
||||||
"set_only_once": 0,
|
"set_only_once": 0,
|
||||||
"unique": 0
|
"unique": 0
|
||||||
@ -306,7 +306,7 @@
|
|||||||
"is_submittable": 1,
|
"is_submittable": 1,
|
||||||
"issingle": 0,
|
"issingle": 0,
|
||||||
"istable": 0,
|
"istable": 0,
|
||||||
"modified": "2015-10-28 14:52:26.724671",
|
"modified": "2015-10-28 18:18:29.137427",
|
||||||
"modified_by": "Administrator",
|
"modified_by": "Administrator",
|
||||||
"module": "HR",
|
"module": "HR",
|
||||||
"name": "Leave Allocation",
|
"name": "Leave Allocation",
|
||||||
|
@ -22,7 +22,6 @@ class LeaveAllocation(Document):
|
|||||||
self.validate_new_leaves_allocated_value()
|
self.validate_new_leaves_allocated_value()
|
||||||
|
|
||||||
def on_update(self):
|
def on_update(self):
|
||||||
pass
|
|
||||||
self.get_total_allocated_leaves()
|
self.get_total_allocated_leaves()
|
||||||
|
|
||||||
def validate_period(self):
|
def validate_period(self):
|
||||||
@ -87,6 +86,11 @@ class LeaveAllocation(Document):
|
|||||||
|
|
||||||
def get_total_allocated_leaves(self):
|
def get_total_allocated_leaves(self):
|
||||||
leave_det = self.get_carry_forwarded_leaves()
|
leave_det = self.get_carry_forwarded_leaves()
|
||||||
|
self.validate_total_leaves_allocated(leave_det)
|
||||||
frappe.db.set(self,'carry_forwarded_leaves',flt(leave_det['carry_forwarded_leaves']))
|
frappe.db.set(self,'carry_forwarded_leaves',flt(leave_det['carry_forwarded_leaves']))
|
||||||
frappe.db.set(self,'total_leaves_allocated',flt(leave_det['total_leaves_allocated']))
|
frappe.db.set(self,'total_leaves_allocated',flt(leave_det['total_leaves_allocated']))
|
||||||
|
|
||||||
|
def validate_total_leaves_allocated(self, leave_det):
|
||||||
|
if date_diff(self.to_date, self.from_date) <= leave_det['total_leaves_allocated']:
|
||||||
|
frappe.throw(_("Total allocated leaves are more than period"))
|
||||||
|
|
@ -1,4 +1,69 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import frappe
|
import frappe
|
||||||
|
import unittest
|
||||||
|
from frappe.utils import getdate
|
||||||
|
|
||||||
test_records = frappe.get_test_records('Leave Allocation')
|
class TestLeaveAllocation(unittest.TestCase):
|
||||||
|
def test_overlapping_allocation(self):
|
||||||
|
employee = frappe.get_doc("Employee", frappe.db.sql_list("select name from tabEmployee limit 1")[0])
|
||||||
|
leaves = [
|
||||||
|
{
|
||||||
|
"doctype": "Leave Allocation",
|
||||||
|
"__islocal": 1,
|
||||||
|
"employee": employee.name,
|
||||||
|
"employee_name": employee.employee_name,
|
||||||
|
"leave_type": "_Test Leave Type",
|
||||||
|
"from_date": getdate("2015-10-1"),
|
||||||
|
"to_date": getdate("2015-10-31"),
|
||||||
|
"new_leaves_allocated": 5,
|
||||||
|
"docstatus": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"doctype": "Leave Allocation",
|
||||||
|
"__islocal": 1,
|
||||||
|
"employee": employee.name,
|
||||||
|
"employee_name": employee.employee_name,
|
||||||
|
"leave_type": "_Test Leave Type",
|
||||||
|
"from_date": getdate("2015-09-1"),
|
||||||
|
"to_date": getdate("2015-11-30"),
|
||||||
|
"new_leaves_allocated": 5
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
frappe.get_doc(leaves[0]).save()
|
||||||
|
self.assertRaises(frappe.ValidationError, frappe.get_doc(leaves[1]).save)
|
||||||
|
|
||||||
|
def test_invalid_period(self):
|
||||||
|
employee = frappe.get_doc("Employee", frappe.db.sql_list("select name from tabEmployee limit 1")[0])
|
||||||
|
|
||||||
|
d = frappe.get_doc({
|
||||||
|
"doctype": "Leave Allocation",
|
||||||
|
"__islocal": 1,
|
||||||
|
"employee": employee.name,
|
||||||
|
"employee_name": employee.employee_name,
|
||||||
|
"leave_type": "_Test Leave Type",
|
||||||
|
"from_date": getdate("2015-09-30"),
|
||||||
|
"to_date": getdate("2015-09-1"),
|
||||||
|
"new_leaves_allocated": 5
|
||||||
|
})
|
||||||
|
|
||||||
|
#invalid period
|
||||||
|
self.assertRaises(frappe.ValidationError, d.save)
|
||||||
|
|
||||||
|
def test_allocated_leave_days_over_period(self):
|
||||||
|
employee = frappe.get_doc("Employee", frappe.db.sql_list("select name from tabEmployee limit 1")[0])
|
||||||
|
d = frappe.get_doc({
|
||||||
|
"doctype": "Leave Allocation",
|
||||||
|
"__islocal": 1,
|
||||||
|
"employee": employee.name,
|
||||||
|
"employee_name": employee.employee_name,
|
||||||
|
"leave_type": "_Test Leave Type",
|
||||||
|
"from_date": getdate("2015-09-1"),
|
||||||
|
"to_date": getdate("2015-09-30"),
|
||||||
|
"new_leaves_allocated": 35
|
||||||
|
})
|
||||||
|
|
||||||
|
#allocated leave more than period
|
||||||
|
self.assertRaises(frappe.ValidationError, d.save)
|
||||||
|
|
||||||
|
test_dependencies = ["Employee", "Leave Type"]
|
@ -1,18 +1 @@
|
|||||||
[
|
[]
|
||||||
{
|
|
||||||
"docstatus": 1,
|
|
||||||
"doctype": "Leave Allocation",
|
|
||||||
"employee": "_T-Employee-0001",
|
|
||||||
"fiscal_year": "_Test Fiscal Year 2013",
|
|
||||||
"leave_type": "_Test Leave Type",
|
|
||||||
"new_leaves_allocated": 15
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"docstatus": 1,
|
|
||||||
"doctype": "Leave Allocation",
|
|
||||||
"employee": "_T-Employee-0002",
|
|
||||||
"fiscal_year": "_Test Fiscal Year 2013",
|
|
||||||
"leave_type": "_Test Leave Type",
|
|
||||||
"new_leaves_allocated": 15
|
|
||||||
}
|
|
||||||
]
|
|
@ -4,11 +4,16 @@
|
|||||||
frappe.query_reports["Employee Leave Balance"] = {
|
frappe.query_reports["Employee Leave Balance"] = {
|
||||||
"filters": [
|
"filters": [
|
||||||
{
|
{
|
||||||
"fieldname":"fiscal_year",
|
"fieldname":"from_date",
|
||||||
"label": __("Fiscal Year"),
|
"label": __("From Date"),
|
||||||
"fieldtype": "Link",
|
"fieldtype": "Date",
|
||||||
"options": "Fiscal Year",
|
"default": frappe.datetime.year_start()
|
||||||
"default": frappe.defaults.get_user_default("fiscal_year")
|
},
|
||||||
|
{
|
||||||
|
"fieldname":"to_date",
|
||||||
|
"label": __("To Date"),
|
||||||
|
"fieldtype": "Date",
|
||||||
|
"default": frappe.datetime.year_end()
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"fieldname":"company",
|
"fieldname":"company",
|
||||||
|
@ -24,52 +24,47 @@ def execute(filters=None):
|
|||||||
|
|
||||||
leave_types = frappe.db.sql_list("select name from `tabLeave Type`")
|
leave_types = frappe.db.sql_list("select name from `tabLeave Type`")
|
||||||
|
|
||||||
if filters.get("fiscal_year"):
|
|
||||||
fiscal_years = [filters["fiscal_year"]]
|
|
||||||
else:
|
|
||||||
fiscal_years = frappe.db.sql_list("select name from `tabFiscal Year` order by name desc")
|
|
||||||
|
|
||||||
employee_names = [d.name for d in employees]
|
employee_names = [d.name for d in employees]
|
||||||
|
|
||||||
allocations = frappe.db.sql("""select employee, fiscal_year, leave_type, total_leaves_allocated
|
allocations = frappe.db.sql("""select employee, leave_type, sum(new_leaves_allocated) as leaves_allocated
|
||||||
from `tabLeave Allocation`
|
from `tabLeave Allocation`
|
||||||
where docstatus=1 and employee in (%s)""" %
|
where docstatus=1 and employee in (%s) and from_date >= '%s' and to_date <= '%s'""" %
|
||||||
','.join(['%s']*len(employee_names)), employee_names, as_dict=True)
|
(','.join(['%s']*len(employee_names)), filters.get("from_date"),
|
||||||
|
filters.get("to_date")), employee_names, as_dict=True)
|
||||||
applications = frappe.db.sql("""select employee, fiscal_year, leave_type,
|
|
||||||
|
applications = frappe.db.sql("""select employee, leave_type,
|
||||||
SUM(total_leave_days) as leaves
|
SUM(total_leave_days) as leaves
|
||||||
from `tabLeave Application`
|
from `tabLeave Application`
|
||||||
where status="Approved" and docstatus = 1 and employee in (%s)
|
where status="Approved" and docstatus = 1 and employee in (%s)
|
||||||
group by employee, fiscal_year, leave_type""" %
|
and from_date >= '%s' and to_date <= '%s'
|
||||||
','.join(['%s']*len(employee_names)), employee_names, as_dict=True)
|
group by employee, leave_type""" %
|
||||||
|
(','.join(['%s']*len(employee_names)), filters.get("from_date"),
|
||||||
|
filters.get("to_date")), employee_names, as_dict=True)
|
||||||
|
|
||||||
columns = [
|
columns = [
|
||||||
_("Fiscal Year"), _("Employee") + ":Link/Employee:150", _("Employee Name") + "::200", _("Department") +"::150"
|
_("Employee") + ":Link/Employee:150", _("Employee Name") + "::200", _("Department") +"::150"
|
||||||
]
|
]
|
||||||
|
|
||||||
for leave_type in leave_types:
|
for leave_type in leave_types:
|
||||||
columns.append(_(leave_type) + " " + _("Allocated") + ":Float")
|
columns.append(_(leave_type) + " " + _("Opening") + ":Float")
|
||||||
columns.append(_(leave_type) + " " + _("Taken") + ":Float")
|
columns.append(_(leave_type) + " " + _("Taken") + ":Float")
|
||||||
columns.append(_(leave_type) + " " + _("Balance") + ":Float")
|
columns.append(_(leave_type) + " " + _("Balance") + ":Float")
|
||||||
|
|
||||||
data = {}
|
data = {}
|
||||||
for d in allocations:
|
for d in allocations:
|
||||||
data.setdefault((d.fiscal_year, d.employee,
|
data.setdefault((d.employee,d.leave_type), frappe._dict()).allocation = d.leaves_allocated
|
||||||
d.leave_type), frappe._dict()).allocation = d.total_leaves_allocated
|
|
||||||
|
|
||||||
for d in applications:
|
for d in applications:
|
||||||
data.setdefault((d.fiscal_year, d.employee,
|
data.setdefault((d.employee, d.leave_type), frappe._dict()).leaves = d.leaves
|
||||||
d.leave_type), frappe._dict()).leaves = d.leaves
|
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
for fiscal_year in fiscal_years:
|
for employee in employees:
|
||||||
for employee in employees:
|
row = [employee.name, employee.employee_name, employee.department]
|
||||||
row = [fiscal_year, employee.name, employee.employee_name, employee.department]
|
result.append(row)
|
||||||
result.append(row)
|
for leave_type in leave_types:
|
||||||
for leave_type in leave_types:
|
tmp = data.get((employee.name, leave_type), frappe._dict())
|
||||||
tmp = data.get((fiscal_year, employee.name, leave_type), frappe._dict())
|
row.append(tmp.allocation or 0)
|
||||||
row.append(tmp.allocation or 0)
|
row.append(tmp.leaves or 0)
|
||||||
row.append(tmp.leaves or 0)
|
row.append((tmp.allocation or 0) - (tmp.leaves or 0))
|
||||||
row.append((tmp.allocation or 0) - (tmp.leaves or 0))
|
|
||||||
|
|
||||||
return columns, result
|
return columns, result
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
from __future__ import unicode_literals
|
||||||
|
import frappe
|
||||||
|
|
||||||
|
def execute():
|
||||||
|
for leave_allocation in frappe.db.sql("select name, fiscal_year from `tabLeave Allocation`", as_dict=True):
|
||||||
|
year_start_date, year_end_date = frappe.db.get_value("Fiscal Year", leave_allocation["fiscal_year"],
|
||||||
|
["year_start_date", "year_end_date"])
|
||||||
|
|
||||||
|
frappe.db.sql("""update `tabLeave Allocation`
|
||||||
|
set from_date=%s, to_date=%s where name=%s""",
|
||||||
|
(year_start_date, year_end_date, leave_allocation["name"]))
|
||||||
|
|
||||||
|
frappe.db.commit()
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user