Skip to content

Commit

Permalink
test: Employee Benefit Application
Browse files Browse the repository at this point in the history
- make `get_no_of_days` a function for reusability
  • Loading branch information
ruchamahabal committed Jun 7, 2022
1 parent 10f0c93 commit ad1b419
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,82 @@

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):
pass
from erpnext.hr.doctype.employee.test_employee import make_employee
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()
4 changes: 3 additions & 1 deletion erpnext/payroll/doctype/gratuity/test_gratuity.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def setUp(self):
frappe.db.delete("Salary Slip")
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_holiday_list()

Expand Down
60 changes: 37 additions & 23 deletions erpnext/payroll/doctype/salary_slip/test_salary_slip.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def tearDown(self):
"Payroll Settings", {"payroll_based_on": "Attendance", "daily_wages_fraction_for_half_day": 0.75}
)
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")
frappe.db.set_value("Employee", emp_id, {"relieving_date": None, "status": "Active"})
Expand Down Expand Up @@ -128,7 +128,7 @@ def test_payment_days_based_on_attendance(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())

new_emp_id = make_employee("test_payment_days_based_on_joining_date@salary.com")
Expand Down Expand Up @@ -196,7 +196,7 @@ def test_payment_days_for_mid_joinee_including_holidays_and_unmarked_days(self):
# tests mid month joining and relieving along with unmarked days
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())

new_emp_id = make_employee("test_payment_days_based_on_joining_date@salary.com")
Expand Down Expand Up @@ -236,7 +236,7 @@ def test_payment_days_for_mid_joinee_including_holidays_and_unmarked_days(self):
def test_payment_days_for_mid_joinee_excluding_holidays(self):
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())

new_emp_id = make_employee("test_payment_days_based_on_joining_date@salary.com")
Expand Down Expand Up @@ -267,7 +267,7 @@ def test_payment_days_for_mid_joinee_excluding_holidays(self):

@change_settings("Payroll Settings", {"payroll_based_on": "Leave"})
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")
frappe.db.set_value("Employee", emp_id, {"relieving_date": None, "status": "Active"})
Expand Down Expand Up @@ -366,7 +366,7 @@ def test_payment_days_in_salary_slip_based_on_timesheet(self):
salary_slip.submit()
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]
no_of_holidays = no_of_days[1]

Expand All @@ -387,7 +387,7 @@ def test_component_amount_dependent_on_another_payment_days_based_component(self
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()
employee = make_employee("test_payment_days_based_component@salary.com", company="_Test Company")

Expand Down Expand Up @@ -445,7 +445,7 @@ def test_component_amount_dependent_on_another_payment_days_based_component(self

@change_settings("Payroll Settings", {"include_holidays_in_total_working_days": 1})
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")
frappe.db.set_value(
"Employee",
Expand Down Expand Up @@ -477,7 +477,7 @@ def test_salary_slip_with_holidays_included(self):

@change_settings("Payroll Settings", {"include_holidays_in_total_working_days": 0})
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")
frappe.db.set_value(
"Employee",
Expand Down Expand Up @@ -514,7 +514,7 @@ def test_payment_days(self):
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
employee = make_employee("test_payment_days@salary.com")
Expand Down Expand Up @@ -842,6 +842,7 @@ def test_tax_for_payroll_period(self):
"Monthly",
other_details={"max_benefits": 100000},
test_tax=True,
include_flexi_benefits=True,
employee=employee,
payroll_period=payroll_period,
)
Expand Down Expand Up @@ -945,6 +946,7 @@ def test_tax_for_recurring_additional_salary(self):
"Monthly",
other_details={"max_benefits": 100000},
test_tax=True,
include_flexi_benefits=True,
employee=employee,
payroll_period=payroll_period,
)
Expand Down Expand Up @@ -986,17 +988,18 @@ def make_activity_for_employee(self):
activity_type.wage_rate = 25
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):
Expand Down Expand Up @@ -1096,7 +1099,9 @@ def create_account(account_name, company, parent_account, account_type=None):
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 = [
{
"salary_component": "Basic Salary",
Expand All @@ -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"},
]
if test_tax:
if include_flexi_benefits:
data.extend(
[
{
Expand All @@ -1136,12 +1141,20 @@ def make_earning_salary_component(setup=False, test_tax=False, company_list=None
"pay_against_benefit_claim": 0,
"type": "Earning",
"max_benefit_amount": 15000,
"depends_on_payment_days": 1,
},
]
)
if test_tax:
data.extend(
[
{"salary_component": "Performance Bonus", "abbr": "B", "type": "Earning"},
]
)

if setup or test_tax:
make_salary_component(data, test_tax, company_list)

data.append(
{
"salary_component": "Basic Salary",
Expand Down Expand Up @@ -1419,7 +1432,8 @@ def setup_test():


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"

frappe.delete_doc_if_exists("Holiday List", name, force=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def make_salary_structure(
company=None,
currency=erpnext.get_default_currency(),
payroll_period=None,
include_flexi_benefits=False,
):
if test_tax:
frappe.db.sql("""delete from `tabSalary Structure` where name=%s""", (salary_structure))
Expand All @@ -161,7 +162,10 @@ def make_salary_structure(
"name": salary_structure,
"company": company or erpnext.get_default_company(),
"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(
setup=True, test_tax=test_tax, company_list=["_Test Company"]
Expand Down

0 comments on commit ad1b419

Please sign in to comment.