Merge pull request #14058 from ESS-LLP/encashment

Leave Encashment
This commit is contained in:
rohitwaghchaure 2018-05-15 22:24:52 +05:30 committed by GitHub
commit 043aae7293
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 895 additions and 697 deletions

View File

@ -42,6 +42,7 @@
"reqd": 1,
"search_index": 0,
"set_only_once": 1,
"translatable": 0,
"unique": 0
},
{
@ -74,6 +75,7 @@
"reqd": 1,
"search_index": 1,
"set_only_once": 0,
"translatable": 0,
"unique": 0
},
{
@ -103,6 +105,7 @@
"reqd": 0,
"search_index": 1,
"set_only_once": 0,
"translatable": 0,
"unique": 0
},
{
@ -131,6 +134,7 @@
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"translatable": 0,
"unique": 0,
"width": "50%"
},
@ -163,6 +167,7 @@
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"translatable": 0,
"unique": 0,
"width": "300px"
},
@ -193,6 +198,7 @@
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"translatable": 0,
"unique": 0
},
{
@ -225,6 +231,7 @@
"reqd": 1,
"search_index": 1,
"set_only_once": 0,
"translatable": 0,
"unique": 0
},
{
@ -255,6 +262,7 @@
"reqd": 1,
"search_index": 0,
"set_only_once": 0,
"translatable": 0,
"unique": 0
},
{
@ -285,6 +293,7 @@
"reqd": 1,
"search_index": 0,
"set_only_once": 0,
"translatable": 0,
"unique": 0
},
{
@ -314,6 +323,7 @@
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"translatable": 0,
"unique": 0
},
{
@ -343,6 +353,7 @@
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"translatable": 0,
"unique": 0
},
{
@ -373,6 +384,7 @@
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"translatable": 0,
"unique": 0
},
{
@ -403,6 +415,7 @@
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"translatable": 0,
"unique": 0
},
{
@ -435,6 +448,38 @@
"translatable": 0,
"unique": 0
},
{
"allow_bulk_edit": 0,
"allow_on_submit": 0,
"bold": 0,
"collapsible": 0,
"columns": 0,
"depends_on": "eval:doc.total_leaves_encashed>0",
"fieldname": "total_leaves_encashed",
"fieldtype": "Float",
"hidden": 0,
"ignore_user_permissions": 0,
"ignore_xss_filter": 0,
"in_filter": 0,
"in_global_search": 0,
"in_list_view": 0,
"in_standard_filter": 0,
"label": "Total Leaves Encashed",
"length": 0,
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"print_hide_if_no_value": 0,
"read_only": 1,
"remember_last_selected_value": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"translatable": 0,
"unique": 0
},
{
"allow_bulk_edit": 0,
"allow_on_submit": 0,
@ -529,6 +574,7 @@
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"translatable": 0,
"unique": 0
}
],
@ -543,7 +589,7 @@
"issingle": 0,
"istable": 0,
"max_attachments": 0,
"modified": "2018-04-19 19:34:59.248428",
"modified": "2018-05-15 20:04:56.901123",
"modified_by": "Administrator",
"module": "HR",
"name": "Leave Allocation",
@ -551,7 +597,6 @@
"permissions": [
{
"amend": 1,
"apply_user_permissions": 0,
"cancel": 1,
"create": 1,
"delete": 1,
@ -571,7 +616,6 @@
},
{
"amend": 1,
"apply_user_permissions": 0,
"cancel": 1,
"create": 1,
"delete": 1,

View File

@ -378,7 +378,7 @@ def get_leave_details(employee, date):
@frappe.whitelist()
def get_leave_balance_on(employee, leave_type, date, allocation_records=None,
consider_all_leaves_in_the_allocation_period=False):
consider_all_leaves_in_the_allocation_period=False, consider_encshed_leaves=True):
if allocation_records == None:
allocation_records = get_leave_allocation_records(date, employee).get(employee, frappe._dict())
@ -388,7 +388,10 @@ def get_leave_balance_on(employee, leave_type, date, allocation_records=None,
date = allocation.to_date
leaves_taken = get_leaves_for_period(employee, leave_type, allocation.from_date, date, status="Approved")
return flt(allocation.total_leaves_allocated) - flt(leaves_taken)
if frappe.db.get_value("Leave Type", leave_type, 'allow_encashment') and consider_encshed_leaves:
leaves_encashed = flt(allocation.total_leaves_encashed)
return flt(allocation.total_leaves_allocated) - (flt(leaves_taken) + flt(leaves_encashed))
def get_leaves_for_period(employee, leave_type, from_date, to_date, status):
leave_applications = frappe.db.sql("""
@ -426,7 +429,7 @@ def get_leave_allocation_records(date, employee=None):
conditions = (" and employee='%s'" % employee) if employee else ""
leave_allocation_records = frappe.db.sql("""
select employee, leave_type, total_leaves_allocated, from_date, to_date
select employee, leave_type, total_leaves_allocated, total_leaves_encashed, from_date, to_date
from `tabLeave Allocation`
where %s between from_date and to_date and docstatus=1 {0}""".format(conditions), (date), as_dict=1)
@ -435,7 +438,8 @@ def get_leave_allocation_records(date, employee=None):
allocated_leaves.setdefault(d.employee, frappe._dict()).setdefault(d.leave_type, frappe._dict({
"from_date": d.from_date,
"to_date": d.to_date,
"total_leaves_allocated": d.total_leaves_allocated
"total_leaves_allocated": d.total_leaves_allocated,
"total_leaves_encashed":d.total_leaves_encashed
}))
return allocated_leaves
@ -597,4 +601,4 @@ def get_approved_leaves_for_period(employee, leave_type, from_date, to_date):
leave_app.from_date, leave_app.to_date)
return leave_days

View File

@ -2,7 +2,39 @@
// For license information, please see license.txt
frappe.ui.form.on('Leave Encashment', {
setup: function(frm) {
frm.set_query("leave_type", function() {
return {
filters: {
allow_encashment: 1
}
}
})
},
refresh: function(frm) {
cur_frm.set_intro("");
if(frm.doc.__islocal && !in_list(frappe.user_roles, "Employee")) {
frm.set_intro(__("Fill the form and save it"));
}
},
employee: function(frm) {
frm.trigger("get_leave_details_for_encashment");
},
leave_type: function(frm) {
frm.trigger("get_leave_details_for_encashment");
},
encashment_date: function(frm) {
frm.trigger("get_leave_details_for_encashment");
},
get_leave_details_for_encashment: function(frm) {
if(frm.doc.docstatus==0 && frm.doc.employee && frm.doc.leave_type) {
return frappe.call({
method: "get_leave_details_for_encashment",
doc: frm.doc,
callback: function(r) {
frm.refresh_fields();
}
});
}
}
});

View File

@ -171,6 +171,38 @@
"translatable": 0,
"unique": 0
},
{
"allow_bulk_edit": 0,
"allow_on_submit": 0,
"bold": 0,
"collapsible": 0,
"columns": 0,
"fieldname": "leave_allocation",
"fieldtype": "Link",
"hidden": 0,
"ignore_user_permissions": 0,
"ignore_xss_filter": 0,
"in_filter": 0,
"in_global_search": 0,
"in_list_view": 0,
"in_standard_filter": 0,
"label": "Leave Allocation",
"length": 0,
"no_copy": 1,
"options": "Leave Allocation",
"permlevel": 0,
"precision": "",
"print_hide": 0,
"print_hide_if_no_value": 0,
"read_only": 1,
"remember_last_selected_value": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"translatable": 0,
"unique": 0
},
{
"allow_bulk_edit": 0,
"allow_on_submit": 0,
@ -188,7 +220,7 @@
"in_standard_filter": 0,
"label": "Leave Balance",
"length": 0,
"no_copy": 0,
"no_copy": 1,
"permlevel": 0,
"precision": "",
"print_hide": 0,
@ -219,7 +251,7 @@
"in_standard_filter": 0,
"label": "Encashable days",
"length": 0,
"no_copy": 0,
"no_copy": 1,
"permlevel": 0,
"precision": "",
"print_hide": 0,
@ -308,11 +340,11 @@
"ignore_xss_filter": 0,
"in_filter": 0,
"in_global_search": 0,
"in_list_view": 0,
"in_list_view": 1,
"in_standard_filter": 0,
"label": "Encashment Amount",
"length": 0,
"no_copy": 0,
"no_copy": 1,
"permlevel": 0,
"precision": "",
"print_hide": 0,
@ -332,7 +364,8 @@
"bold": 0,
"collapsible": 0,
"columns": 0,
"fieldname": "payroll_date",
"default": "Today",
"fieldname": "encashment_date",
"fieldtype": "Date",
"hidden": 0,
"ignore_user_permissions": 0,
@ -341,7 +374,7 @@
"in_global_search": 0,
"in_list_view": 0,
"in_standard_filter": 0,
"label": "Payroll Date",
"label": "Encashment Date",
"length": 0,
"no_copy": 0,
"permlevel": 0,
@ -356,6 +389,38 @@
"set_only_once": 0,
"translatable": 0,
"unique": 0
},
{
"allow_bulk_edit": 0,
"allow_on_submit": 0,
"bold": 0,
"collapsible": 0,
"columns": 0,
"fieldname": "additional_component",
"fieldtype": "Link",
"hidden": 0,
"ignore_user_permissions": 0,
"ignore_xss_filter": 0,
"in_filter": 0,
"in_global_search": 0,
"in_list_view": 0,
"in_standard_filter": 0,
"label": "Additional Salary Component",
"length": 0,
"no_copy": 1,
"options": "Additional Salary Component",
"permlevel": 0,
"precision": "",
"print_hide": 0,
"print_hide_if_no_value": 0,
"read_only": 1,
"remember_last_selected_value": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"translatable": 0,
"unique": 0
}
],
"has_web_view": 0,
@ -368,7 +433,7 @@
"issingle": 0,
"istable": 0,
"max_attachments": 0,
"modified": "2018-04-14 15:35:51.428448",
"modified": "2018-05-15 17:54:47.167810",
"modified_by": "Administrator",
"module": "HR",
"name": "Leave Encashment",

View File

@ -4,7 +4,75 @@
from __future__ import unicode_literals
import frappe
from frappe import _
from frappe.model.document import Document
from frappe.utils import getdate, nowdate, flt
from erpnext.hr.utils import set_employee_name
from erpnext.hr.doctype.leave_application.leave_application import get_leave_balance_on
from erpnext.hr.doctype.salary_structure_assignment.salary_structure_assignment import get_assigned_salary_structure
class LeaveEncashment(Document):
pass
def validate(self):
set_employee_name(self)
self.get_leave_details_for_encashment()
if not self.encashment_date:
self.encashment_date = getdate(nowdate())
def before_submit(self):
if self.encashment_amount <= 0:
frappe.throw(_("You can only submit Leave Encashment for a valid encashment amount"))
def on_submit(self):
if not self.leave_allocation:
self.leave_allocation = self.get_leave_allocation()
additional_component = frappe.new_doc("Additional Salary Component")
additional_component.company = frappe.get_value("Employee", self.employee, "company")
additional_component.employee = self.employee
additional_component.salary_component = frappe.get_value("Leave Type", self.leave_type, "earning_component")
additional_component.from_date = self.encashment_date
additional_component.to_date = self.encashment_date
additional_component.amount = self.encashment_amount
additional_component.submit()
self.db_set("additional_component", additional_component.name)
# Set encashed leaves in Allocation
frappe.db.set_value("Leave Allocation", self.leave_allocation, "total_leaves_encashed",
frappe.db.get_value('Leave Allocation', self.leave_allocation, 'total_leaves_encashed') + self.encashable_days)
def on_cancel(self):
if self.additional_component:
frappe.get_doc("Additional Salary Component", self.additional_component).cancel()
self.db_set("additional_component", "")
if self.leave_allocation:
frappe.db.set_value("Leave Allocation", self.leave_allocation, "total_leaves_encashed",
frappe.db.get_value('Leave Allocation', self.leave_allocation, 'total_leaves_encashed') - self.encashable_days)
def get_leave_details_for_encashment(self):
salary_structure = get_assigned_salary_structure(self.employee, self.encashment_date or getdate(nowdate()))
if not salary_structure:
frappe.throw(_("No Salary Structure assigned for Employee {0} on given date {1}").format(self.employee, self.encashment_date))
if not frappe.db.get_value("Leave Type", self.leave_type, 'allow_encashment'):
frappe.throw(_("Leave Type {0} is not encashable").format(self.leave_type))
self.leave_balance = get_leave_balance_on(self.employee, self.leave_type,
self.encashment_date or getdate(nowdate()), consider_all_leaves_in_the_allocation_period=True)
encashable_days = self.leave_balance - frappe.db.get_value('Leave Type', self.leave_type, 'encashment_threshold_days')
self.encashable_days = encashable_days if encashable_days > 0 else 0
per_day_encashment = frappe.db.get_value('Salary Structure', salary_structure , 'leave_encashment_amount_per_day')
self.encashment_amount = self.encashable_days * per_day_encashment if per_day_encashment > 0 else 0
self.leave_allocation = self.get_leave_allocation()
return True
def get_leave_allocation(self):
leave_allocation = frappe.db.sql("""select name from `tabLeave Allocation` where '{0}'
between from_date and to_date and docstatus=1 and leave_type='{1}'
and employee= '{2}'""".format(self.encashment_date or getdate(nowdate()), self.leave_type, self.employee))
return leave_allocation[0][0] if leave_allocation else None

File diff suppressed because it is too large Load Diff

View File

@ -55,3 +55,20 @@ class SalaryStructureAssignment(Document):
if assignment:
frappe.throw(_("Active Salary Structure Assignment {0} found for employee {1} for the given dates").format(assignment[0][0], self.employee))
def get_assigned_salary_structure(employee, on_date):
if not employee or not on_date:
return None
salary_structure = frappe.db.sql("""
select salary_structure from `tabSalary Structure Assignment`
where employee=%(employee)s
and docstatus = 1
and (
(%(on_date)s between from_date and ifnull(to_date, '2199-12-31'))
)""", {
'employee': employee,
'on_date': on_date,
})
return salary_structure[0][0] if salary_structure else None

View File

@ -84,15 +84,15 @@ def install(country=None):
# leave type
{'doctype': 'Leave Type', 'leave_type_name': _('Casual Leave'), 'name': _('Casual Leave'),
'is_encash': 1, 'is_carry_forward': 1, 'max_days_allowed': '3', 'include_holiday': 1},
'allow_encashment': 1, 'is_carry_forward': 1, 'max_days_allowed': '3', 'include_holiday': 1},
{'doctype': 'Leave Type', 'leave_type_name': _('Compensatory Off'), 'name': _('Compensatory Off'),
'is_encash': 0, 'is_carry_forward': 0, 'include_holiday': 1},
'allow_encashment': 0, 'is_carry_forward': 0, 'include_holiday': 1},
{'doctype': 'Leave Type', 'leave_type_name': _('Sick Leave'), 'name': _('Sick Leave'),
'is_encash': 0, 'is_carry_forward': 0, 'include_holiday': 1},
'allow_encashment': 0, 'is_carry_forward': 0, 'include_holiday': 1},
{'doctype': 'Leave Type', 'leave_type_name': _('Privilege Leave'), 'name': _('Privilege Leave'),
'is_encash': 0, 'is_carry_forward': 0, 'include_holiday': 1},
'allow_encashment': 0, 'is_carry_forward': 0, 'include_holiday': 1},
{'doctype': 'Leave Type', 'leave_type_name': _('Leave Without Pay'), 'name': _('Leave Without Pay'),
'is_encash': 0, 'is_carry_forward': 0, 'is_lwp':1, 'include_holiday': 1},
'allow_encashment': 0, 'is_carry_forward': 0, 'is_lwp':1, 'include_holiday': 1},
# Employment Type
{'doctype': 'Employment Type', 'employee_type_name': _('Full-time')},