mirror of
https://github.com/frappe/erpnext.git
synced 2026-06-01 19:29:10 +00:00
test: Employee Benefit Application
- make `get_no_of_days` a function for reusability
This commit is contained in:
@@ -3,6 +3,82 @@
|
|||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
import frappe
|
||||||
|
from frappe.tests.utils import FrappeTestCase
|
||||||
|
from frappe.utils import add_days, date_diff, get_year_ending, get_year_start, getdate
|
||||||
|
|
||||||
class TestEmployeeBenefitApplication(unittest.TestCase):
|
from erpnext.hr.doctype.employee.test_employee import make_employee
|
||||||
pass
|
from erpnext.hr.doctype.holiday_list.test_holiday_list import set_holiday_list
|
||||||
|
from erpnext.hr.doctype.leave_application.test_leave_application import get_first_sunday
|
||||||
|
from erpnext.hr.utils import get_holiday_dates_for_employee
|
||||||
|
from erpnext.payroll.doctype.employee_benefit_application.employee_benefit_application import (
|
||||||
|
calculate_lwp,
|
||||||
|
)
|
||||||
|
from erpnext.payroll.doctype.employee_tax_exemption_declaration.test_employee_tax_exemption_declaration import (
|
||||||
|
create_payroll_period,
|
||||||
|
)
|
||||||
|
from erpnext.payroll.doctype.salary_slip.test_salary_slip import (
|
||||||
|
make_holiday_list,
|
||||||
|
make_leave_application,
|
||||||
|
)
|
||||||
|
from erpnext.payroll.doctype.salary_structure.salary_structure import make_salary_slip
|
||||||
|
from erpnext.payroll.doctype.salary_structure.test_salary_structure import make_salary_structure
|
||||||
|
|
||||||
|
|
||||||
|
class TestEmployeeBenefitApplication(FrappeTestCase):
|
||||||
|
def setUp(self):
|
||||||
|
date = getdate()
|
||||||
|
make_holiday_list(from_date=get_year_start(date), to_date=get_year_ending(date))
|
||||||
|
|
||||||
|
@set_holiday_list("Salary Slip Test Holiday List", "_Test Company")
|
||||||
|
def test_employee_benefit_application(self):
|
||||||
|
payroll_period = create_payroll_period(name="_Test Payroll Period 1", company="_Test Company")
|
||||||
|
employee = make_employee("test_employee_benefits@salary.com", company="_Test Company")
|
||||||
|
first_sunday = get_first_sunday("Salary Slip Test Holiday List")
|
||||||
|
|
||||||
|
leave_application = make_leave_application(
|
||||||
|
employee,
|
||||||
|
add_days(first_sunday, 1),
|
||||||
|
add_days(first_sunday, 3),
|
||||||
|
"Leave Without Pay",
|
||||||
|
half_day=1,
|
||||||
|
half_day_date=add_days(first_sunday, 1),
|
||||||
|
submit=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
frappe.db.set_value("Leave Type", "Leave Without Pay", "include_holiday", 0)
|
||||||
|
salary_structure = make_salary_structure(
|
||||||
|
"Test Employee Benefits",
|
||||||
|
"Monthly",
|
||||||
|
other_details={"max_benefits": 100000},
|
||||||
|
include_flexi_benefits=True,
|
||||||
|
employee=employee,
|
||||||
|
payroll_period=payroll_period,
|
||||||
|
)
|
||||||
|
salary_slip = make_salary_slip(salary_structure.name, employee=employee, posting_date=getdate())
|
||||||
|
salary_slip.insert()
|
||||||
|
salary_slip.submit()
|
||||||
|
|
||||||
|
application = make_employee_benefit_application(
|
||||||
|
employee, payroll_period.name, date=leave_application.to_date
|
||||||
|
)
|
||||||
|
self.assertEqual(application.employee_benefits[0].max_benefit_amount, 15000)
|
||||||
|
|
||||||
|
holidays = get_holiday_dates_for_employee(employee, payroll_period.start_date, application.date)
|
||||||
|
working_days = date_diff(application.date, payroll_period.start_date) + 1
|
||||||
|
lwp = calculate_lwp(employee, payroll_period.start_date, holidays, working_days)
|
||||||
|
self.assertEqual(lwp, 2.5)
|
||||||
|
|
||||||
|
|
||||||
|
def make_employee_benefit_application(employee, payroll_period, date):
|
||||||
|
frappe.db.delete("Employee Benefit Application")
|
||||||
|
|
||||||
|
return frappe.get_doc(
|
||||||
|
{
|
||||||
|
"doctype": "Employee Benefit Application",
|
||||||
|
"employee": employee,
|
||||||
|
"date": date,
|
||||||
|
"payroll_period": payroll_period,
|
||||||
|
"employee_benefits": [{"earning_component": "Medical Allowance", "amount": 1500}],
|
||||||
|
}
|
||||||
|
).insert()
|
||||||
|
|||||||
@@ -29,7 +29,9 @@ class TestGratuity(FrappeTestCase):
|
|||||||
frappe.db.delete("Salary Slip")
|
frappe.db.delete("Salary Slip")
|
||||||
frappe.db.delete("Additional Salary", {"ref_doctype": "Gratuity"})
|
frappe.db.delete("Additional Salary", {"ref_doctype": "Gratuity"})
|
||||||
|
|
||||||
make_earning_salary_component(setup=True, test_tax=True, company_list=["_Test Company"])
|
make_earning_salary_component(
|
||||||
|
setup=True, test_tax=True, company_list=["_Test Company"], include_flexi_benefits=True
|
||||||
|
)
|
||||||
make_deduction_salary_component(setup=True, test_tax=True, company_list=["_Test Company"])
|
make_deduction_salary_component(setup=True, test_tax=True, company_list=["_Test Company"])
|
||||||
make_holiday_list()
|
make_holiday_list()
|
||||||
|
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ class TestSalarySlip(unittest.TestCase):
|
|||||||
"Payroll Settings", {"payroll_based_on": "Attendance", "daily_wages_fraction_for_half_day": 0.75}
|
"Payroll Settings", {"payroll_based_on": "Attendance", "daily_wages_fraction_for_half_day": 0.75}
|
||||||
)
|
)
|
||||||
def test_payment_days_based_on_attendance(self):
|
def test_payment_days_based_on_attendance(self):
|
||||||
no_of_days = self.get_no_of_days()
|
no_of_days = get_no_of_days()
|
||||||
|
|
||||||
emp_id = make_employee("test_payment_days_based_on_attendance@salary.com")
|
emp_id = make_employee("test_payment_days_based_on_attendance@salary.com")
|
||||||
frappe.db.set_value("Employee", emp_id, {"relieving_date": None, "status": "Active"})
|
frappe.db.set_value("Employee", emp_id, {"relieving_date": None, "status": "Active"})
|
||||||
@@ -128,7 +128,7 @@ class TestSalarySlip(unittest.TestCase):
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
def test_payment_days_for_mid_joinee_including_holidays(self):
|
def test_payment_days_for_mid_joinee_including_holidays(self):
|
||||||
no_of_days = self.get_no_of_days()
|
no_of_days = get_no_of_days()
|
||||||
month_start_date, month_end_date = get_first_day(nowdate()), get_last_day(nowdate())
|
month_start_date, month_end_date = get_first_day(nowdate()), get_last_day(nowdate())
|
||||||
|
|
||||||
new_emp_id = make_employee("test_payment_days_based_on_joining_date@salary.com")
|
new_emp_id = make_employee("test_payment_days_based_on_joining_date@salary.com")
|
||||||
@@ -196,7 +196,7 @@ class TestSalarySlip(unittest.TestCase):
|
|||||||
# tests mid month joining and relieving along with unmarked days
|
# tests mid month joining and relieving along with unmarked days
|
||||||
from erpnext.hr.doctype.holiday_list.holiday_list import is_holiday
|
from erpnext.hr.doctype.holiday_list.holiday_list import is_holiday
|
||||||
|
|
||||||
no_of_days = self.get_no_of_days()
|
no_of_days = get_no_of_days()
|
||||||
month_start_date, month_end_date = get_first_day(nowdate()), get_last_day(nowdate())
|
month_start_date, month_end_date = get_first_day(nowdate()), get_last_day(nowdate())
|
||||||
|
|
||||||
new_emp_id = make_employee("test_payment_days_based_on_joining_date@salary.com")
|
new_emp_id = make_employee("test_payment_days_based_on_joining_date@salary.com")
|
||||||
@@ -236,7 +236,7 @@ class TestSalarySlip(unittest.TestCase):
|
|||||||
def test_payment_days_for_mid_joinee_excluding_holidays(self):
|
def test_payment_days_for_mid_joinee_excluding_holidays(self):
|
||||||
from erpnext.hr.doctype.holiday_list.holiday_list import is_holiday
|
from erpnext.hr.doctype.holiday_list.holiday_list import is_holiday
|
||||||
|
|
||||||
no_of_days = self.get_no_of_days()
|
no_of_days = get_no_of_days()
|
||||||
month_start_date, month_end_date = get_first_day(nowdate()), get_last_day(nowdate())
|
month_start_date, month_end_date = get_first_day(nowdate()), get_last_day(nowdate())
|
||||||
|
|
||||||
new_emp_id = make_employee("test_payment_days_based_on_joining_date@salary.com")
|
new_emp_id = make_employee("test_payment_days_based_on_joining_date@salary.com")
|
||||||
@@ -267,7 +267,7 @@ class TestSalarySlip(unittest.TestCase):
|
|||||||
|
|
||||||
@change_settings("Payroll Settings", {"payroll_based_on": "Leave"})
|
@change_settings("Payroll Settings", {"payroll_based_on": "Leave"})
|
||||||
def test_payment_days_based_on_leave_application(self):
|
def test_payment_days_based_on_leave_application(self):
|
||||||
no_of_days = self.get_no_of_days()
|
no_of_days = get_no_of_days()
|
||||||
|
|
||||||
emp_id = make_employee("test_payment_days_based_on_leave_application@salary.com")
|
emp_id = make_employee("test_payment_days_based_on_leave_application@salary.com")
|
||||||
frappe.db.set_value("Employee", emp_id, {"relieving_date": None, "status": "Active"})
|
frappe.db.set_value("Employee", emp_id, {"relieving_date": None, "status": "Active"})
|
||||||
@@ -366,7 +366,7 @@ class TestSalarySlip(unittest.TestCase):
|
|||||||
salary_slip.submit()
|
salary_slip.submit()
|
||||||
salary_slip.reload()
|
salary_slip.reload()
|
||||||
|
|
||||||
no_of_days = self.get_no_of_days()
|
no_of_days = get_no_of_days()
|
||||||
days_in_month = no_of_days[0]
|
days_in_month = no_of_days[0]
|
||||||
no_of_holidays = no_of_days[1]
|
no_of_holidays = no_of_days[1]
|
||||||
|
|
||||||
@@ -387,7 +387,7 @@ class TestSalarySlip(unittest.TestCase):
|
|||||||
create_salary_structure_assignment,
|
create_salary_structure_assignment,
|
||||||
)
|
)
|
||||||
|
|
||||||
no_of_days = self.get_no_of_days()
|
no_of_days = get_no_of_days()
|
||||||
salary_structure = make_salary_structure_for_payment_days_based_component_dependency()
|
salary_structure = make_salary_structure_for_payment_days_based_component_dependency()
|
||||||
employee = make_employee("test_payment_days_based_component@salary.com", company="_Test Company")
|
employee = make_employee("test_payment_days_based_component@salary.com", company="_Test Company")
|
||||||
|
|
||||||
@@ -445,7 +445,7 @@ class TestSalarySlip(unittest.TestCase):
|
|||||||
|
|
||||||
@change_settings("Payroll Settings", {"include_holidays_in_total_working_days": 1})
|
@change_settings("Payroll Settings", {"include_holidays_in_total_working_days": 1})
|
||||||
def test_salary_slip_with_holidays_included(self):
|
def test_salary_slip_with_holidays_included(self):
|
||||||
no_of_days = self.get_no_of_days()
|
no_of_days = get_no_of_days()
|
||||||
make_employee("test_salary_slip_with_holidays_included@salary.com")
|
make_employee("test_salary_slip_with_holidays_included@salary.com")
|
||||||
frappe.db.set_value(
|
frappe.db.set_value(
|
||||||
"Employee",
|
"Employee",
|
||||||
@@ -477,7 +477,7 @@ class TestSalarySlip(unittest.TestCase):
|
|||||||
|
|
||||||
@change_settings("Payroll Settings", {"include_holidays_in_total_working_days": 0})
|
@change_settings("Payroll Settings", {"include_holidays_in_total_working_days": 0})
|
||||||
def test_salary_slip_with_holidays_excluded(self):
|
def test_salary_slip_with_holidays_excluded(self):
|
||||||
no_of_days = self.get_no_of_days()
|
no_of_days = get_no_of_days()
|
||||||
make_employee("test_salary_slip_with_holidays_excluded@salary.com")
|
make_employee("test_salary_slip_with_holidays_excluded@salary.com")
|
||||||
frappe.db.set_value(
|
frappe.db.set_value(
|
||||||
"Employee",
|
"Employee",
|
||||||
@@ -514,7 +514,7 @@ class TestSalarySlip(unittest.TestCase):
|
|||||||
create_salary_structure_assignment,
|
create_salary_structure_assignment,
|
||||||
)
|
)
|
||||||
|
|
||||||
no_of_days = self.get_no_of_days()
|
no_of_days = get_no_of_days()
|
||||||
|
|
||||||
# set joinng date in the same month
|
# set joinng date in the same month
|
||||||
employee = make_employee("test_payment_days@salary.com")
|
employee = make_employee("test_payment_days@salary.com")
|
||||||
@@ -842,6 +842,7 @@ class TestSalarySlip(unittest.TestCase):
|
|||||||
"Monthly",
|
"Monthly",
|
||||||
other_details={"max_benefits": 100000},
|
other_details={"max_benefits": 100000},
|
||||||
test_tax=True,
|
test_tax=True,
|
||||||
|
include_flexi_benefits=True,
|
||||||
employee=employee,
|
employee=employee,
|
||||||
payroll_period=payroll_period,
|
payroll_period=payroll_period,
|
||||||
)
|
)
|
||||||
@@ -945,6 +946,7 @@ class TestSalarySlip(unittest.TestCase):
|
|||||||
"Monthly",
|
"Monthly",
|
||||||
other_details={"max_benefits": 100000},
|
other_details={"max_benefits": 100000},
|
||||||
test_tax=True,
|
test_tax=True,
|
||||||
|
include_flexi_benefits=True,
|
||||||
employee=employee,
|
employee=employee,
|
||||||
payroll_period=payroll_period,
|
payroll_period=payroll_period,
|
||||||
)
|
)
|
||||||
@@ -986,17 +988,18 @@ class TestSalarySlip(unittest.TestCase):
|
|||||||
activity_type.wage_rate = 25
|
activity_type.wage_rate = 25
|
||||||
activity_type.save()
|
activity_type.save()
|
||||||
|
|
||||||
def get_no_of_days(self):
|
|
||||||
no_of_days_in_month = calendar.monthrange(getdate(nowdate()).year, getdate(nowdate()).month)
|
|
||||||
no_of_holidays_in_month = len(
|
|
||||||
[
|
|
||||||
1
|
|
||||||
for i in calendar.monthcalendar(getdate(nowdate()).year, getdate(nowdate()).month)
|
|
||||||
if i[6] != 0
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
return [no_of_days_in_month[1], no_of_holidays_in_month]
|
def get_no_of_days():
|
||||||
|
no_of_days_in_month = calendar.monthrange(getdate(nowdate()).year, getdate(nowdate()).month)
|
||||||
|
no_of_holidays_in_month = len(
|
||||||
|
[
|
||||||
|
1
|
||||||
|
for i in calendar.monthcalendar(getdate(nowdate()).year, getdate(nowdate()).month)
|
||||||
|
if i[6] != 0
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
return [no_of_days_in_month[1], no_of_holidays_in_month]
|
||||||
|
|
||||||
|
|
||||||
def make_employee_salary_slip(user, payroll_frequency, salary_structure=None, posting_date=None):
|
def make_employee_salary_slip(user, payroll_frequency, salary_structure=None, posting_date=None):
|
||||||
@@ -1096,7 +1099,9 @@ def create_account(account_name, company, parent_account, account_type=None):
|
|||||||
return account
|
return account
|
||||||
|
|
||||||
|
|
||||||
def make_earning_salary_component(setup=False, test_tax=False, company_list=None):
|
def make_earning_salary_component(
|
||||||
|
setup=False, test_tax=False, company_list=None, include_flexi_benefits=False
|
||||||
|
):
|
||||||
data = [
|
data = [
|
||||||
{
|
{
|
||||||
"salary_component": "Basic Salary",
|
"salary_component": "Basic Salary",
|
||||||
@@ -1117,7 +1122,7 @@ def make_earning_salary_component(setup=False, test_tax=False, company_list=None
|
|||||||
},
|
},
|
||||||
{"salary_component": "Leave Encashment", "abbr": "LE", "type": "Earning"},
|
{"salary_component": "Leave Encashment", "abbr": "LE", "type": "Earning"},
|
||||||
]
|
]
|
||||||
if test_tax:
|
if include_flexi_benefits:
|
||||||
data.extend(
|
data.extend(
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
@@ -1136,12 +1141,20 @@ def make_earning_salary_component(setup=False, test_tax=False, company_list=None
|
|||||||
"pay_against_benefit_claim": 0,
|
"pay_against_benefit_claim": 0,
|
||||||
"type": "Earning",
|
"type": "Earning",
|
||||||
"max_benefit_amount": 15000,
|
"max_benefit_amount": 15000,
|
||||||
|
"depends_on_payment_days": 1,
|
||||||
},
|
},
|
||||||
|
]
|
||||||
|
)
|
||||||
|
if test_tax:
|
||||||
|
data.extend(
|
||||||
|
[
|
||||||
{"salary_component": "Performance Bonus", "abbr": "B", "type": "Earning"},
|
{"salary_component": "Performance Bonus", "abbr": "B", "type": "Earning"},
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
if setup or test_tax:
|
if setup or test_tax:
|
||||||
make_salary_component(data, test_tax, company_list)
|
make_salary_component(data, test_tax, company_list)
|
||||||
|
|
||||||
data.append(
|
data.append(
|
||||||
{
|
{
|
||||||
"salary_component": "Basic Salary",
|
"salary_component": "Basic Salary",
|
||||||
@@ -1419,7 +1432,8 @@ def setup_test():
|
|||||||
|
|
||||||
|
|
||||||
def make_holiday_list(list_name=None, from_date=None, to_date=None):
|
def make_holiday_list(list_name=None, from_date=None, to_date=None):
|
||||||
fiscal_year = get_fiscal_year(nowdate(), company=erpnext.get_default_company())
|
if not (from_date and to_date):
|
||||||
|
fiscal_year = get_fiscal_year(nowdate(), company=erpnext.get_default_company())
|
||||||
name = list_name or "Salary Slip Test Holiday List"
|
name = list_name or "Salary Slip Test Holiday List"
|
||||||
|
|
||||||
frappe.delete_doc_if_exists("Holiday List", name, force=True)
|
frappe.delete_doc_if_exists("Holiday List", name, force=True)
|
||||||
|
|||||||
@@ -149,6 +149,7 @@ def make_salary_structure(
|
|||||||
company=None,
|
company=None,
|
||||||
currency=erpnext.get_default_currency(),
|
currency=erpnext.get_default_currency(),
|
||||||
payroll_period=None,
|
payroll_period=None,
|
||||||
|
include_flexi_benefits=False,
|
||||||
):
|
):
|
||||||
if test_tax:
|
if test_tax:
|
||||||
frappe.db.sql("""delete from `tabSalary Structure` where name=%s""", (salary_structure))
|
frappe.db.sql("""delete from `tabSalary Structure` where name=%s""", (salary_structure))
|
||||||
@@ -161,7 +162,10 @@ def make_salary_structure(
|
|||||||
"name": salary_structure,
|
"name": salary_structure,
|
||||||
"company": company or erpnext.get_default_company(),
|
"company": company or erpnext.get_default_company(),
|
||||||
"earnings": make_earning_salary_component(
|
"earnings": make_earning_salary_component(
|
||||||
setup=True, test_tax=test_tax, company_list=["_Test Company"]
|
setup=True,
|
||||||
|
test_tax=test_tax,
|
||||||
|
company_list=["_Test Company"],
|
||||||
|
include_flexi_benefits=include_flexi_benefits,
|
||||||
),
|
),
|
||||||
"deductions": make_deduction_salary_component(
|
"deductions": make_deduction_salary_component(
|
||||||
setup=True, test_tax=test_tax, company_list=["_Test Company"]
|
setup=True, test_tax=test_tax, company_list=["_Test Company"]
|
||||||
|
|||||||
Reference in New Issue
Block a user