fix - Benefit Application query (#14767)

This commit is contained in:
Ranjith Kurungadam 2018-07-01 16:44:37 +05:30 committed by Nabin Hait
parent 8662a4f677
commit 4dc5721ba5
2 changed files with 20 additions and 18 deletions

View File

@ -165,20 +165,23 @@ def calculate_lwp(employee, start_date, holidays, working_days):
return lwp return lwp
def get_benefit_component_amount(employee, start_date, end_date, struct_row, sal_struct, period_length, frequency): def get_benefit_component_amount(employee, start_date, end_date, struct_row, sal_struct, period_length, frequency):
# Considering there is only one application for an year payroll_period, period_factor, actual_payroll_days = get_payroll_period_days(start_date, end_date, employee)
if not payroll_period:
frappe.msgprint(_("Start and end dates not in a valid Payroll Period, cannot calculate {0}.")
.format(struct_row.salary_component))
return False
# Considering there is only one application for a year
benefit_application_name = frappe.db.sql(""" benefit_application_name = frappe.db.sql("""
select name from `tabEmployee Benefit Application` select name from `tabEmployee Benefit Application`
where employee=%(employee)s where payroll_period=%(payroll_period)s and employee=%(employee)s
and docstatus = 1 and docstatus = 1
and (date between %(start_date)s and %(end_date)s)
""", { """, {
'employee': employee, 'employee': employee,
'start_date': start_date, 'payroll_period': payroll_period
'end_date': end_date
}) })
period_factor, actual_payroll_days = get_payroll_period_days(start_date, end_date, employee)
if frappe.db.get_value("Salary Component", struct_row.salary_component, "depends_on_lwp") != 1: if frappe.db.get_value("Salary Component", struct_row.salary_component, "depends_on_lwp") != 1:
if frequency == "Monthly" and actual_payroll_days in range(360, 370): if frequency == "Monthly" and actual_payroll_days in range(360, 370):
period_length = 1 period_length = 1
@ -186,12 +189,12 @@ def get_benefit_component_amount(employee, start_date, end_date, struct_row, sal
if period_factor: if period_factor:
# If there is application for benefit then fetch the amount from the application. # If there is application for benefit then fetch the amount from the application.
# else Split the max benefits to the pro-rata components with the ratio of thier max_benefit_amount # else Split the max benefits to the pro-rata components with the ratio of their max_benefit_amount
if benefit_application_name: if benefit_application_name:
benefit_application = frappe.get_doc("Employee Benefit Application", benefit_application_name[0][0]) benefit_application = frappe.get_doc("Employee Benefit Application", benefit_application_name[0][0])
return get_benefit_amount(benefit_application, struct_row, period_factor, period_length) return get_benefit_amount(benefit_application, struct_row, period_factor, period_length)
# TODO: Check if there is benefit claim for employee then pro-rata devid the rest of amount (Late Benefit Application) # TODO: Check if there is benefit claim for employee then pro-rata divide the rest of amount (Late Benefit Application)
else: else:
component_max = frappe.db.get_value("Salary Component", struct_row.salary_component, "max_benefit_amount") component_max = frappe.db.get_value("Salary Component", struct_row.salary_component, "max_benefit_amount")
if component_max > 0: if component_max > 0:

View File

@ -47,24 +47,23 @@ class PayrollPeriod(Document):
def get_payroll_period_days(start_date, end_date, employee): def get_payroll_period_days(start_date, end_date, employee):
company = frappe.db.get_value("Employee", employee, "company") company = frappe.db.get_value("Employee", employee, "company")
payroll_period_dates = frappe.db.sql(""" payroll_period = frappe.db.sql("""
select start_date, end_date from `tabPayroll Period` select name, start_date, end_date from `tabPayroll Period`
where company=%(company)s where company=%(company)s
and ( and (
(%(start_date)s between start_date and end_date) (%(start_date)s between start_date and end_date)
or (%(end_date)s between start_date and end_date) and (%(end_date)s between start_date and end_date)
or (start_date between %(start_date)s and %(end_date)s)
)""", { )""", {
'company': company, 'company': company,
'start_date': start_date, 'start_date': start_date,
'end_date': end_date 'end_date': end_date
}) })
if len(payroll_period_dates) > 0: if len(payroll_period) > 0:
actual_no_of_days = date_diff(getdate(payroll_period_dates[0][1]), getdate(payroll_period_dates[0][0])) + 1 actual_no_of_days = date_diff(getdate(payroll_period[0][2]), getdate(payroll_period[0][1])) + 1
working_days = actual_no_of_days working_days = actual_no_of_days
if not cint(frappe.db.get_value("HR Settings", None, "include_holidays_in_total_working_days")): if not cint(frappe.db.get_value("HR Settings", None, "include_holidays_in_total_working_days")):
holidays = get_holidays_for_employee(employee, getdate(payroll_period_dates[0][0]), getdate(payroll_period_dates[0][1])) holidays = get_holidays_for_employee(employee, getdate(payroll_period[0][1]), getdate(payroll_period[0][2]))
working_days -= len(holidays) working_days -= len(holidays)
return working_days, actual_no_of_days return payroll_period[0][0], working_days, actual_no_of_days
return False, False return False, False, False