mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-23 15:09:20 +00:00
refactor: clean-up gratuity tests
(cherry picked from commit 6c66bbbbfe)
# Conflicts:
# erpnext/payroll/doctype/gratuity/test_gratuity.py
This commit is contained in:
@@ -5,10 +5,11 @@ import unittest
|
|||||||
|
|
||||||
import frappe
|
import frappe
|
||||||
from frappe.tests.utils import FrappeTestCase
|
from frappe.tests.utils import FrappeTestCase
|
||||||
from frappe.utils import add_days, add_months, flt, get_datetime, get_first_day, getdate
|
from frappe.utils import add_days, add_months, floor, flt, get_datetime, get_first_day, getdate
|
||||||
|
|
||||||
from erpnext.hr.doctype.employee.test_employee import make_employee
|
from erpnext.hr.doctype.employee.test_employee import make_employee
|
||||||
from erpnext.hr.doctype.expense_claim.test_expense_claim import get_payable_account
|
from erpnext.hr.doctype.expense_claim.test_expense_claim import get_payable_account
|
||||||
|
from erpnext.hr.doctype.holiday_list.test_holiday_list import set_holiday_list
|
||||||
from erpnext.payroll.doctype.gratuity.gratuity import get_last_salary_slip
|
from erpnext.payroll.doctype.gratuity.gratuity import get_last_salary_slip
|
||||||
from erpnext.payroll.doctype.salary_slip.test_salary_slip import (
|
from erpnext.payroll.doctype.salary_slip.test_salary_slip import (
|
||||||
make_deduction_salary_component,
|
make_deduction_salary_component,
|
||||||
@@ -39,37 +40,46 @@ class TestGratuity(FrappeTestCase):
|
|||||||
>>>>>>> b81d7519c1 (test: Gratuity status for payment via salary slip)
|
>>>>>>> b81d7519c1 (test: Gratuity status for payment via salary slip)
|
||||||
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"])
|
||||||
|
|
||||||
|
<<<<<<< HEAD
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
frappe.db.sql("DELETE FROM `tabGratuity`")
|
frappe.db.sql("DELETE FROM `tabGratuity`")
|
||||||
frappe.db.sql("DELETE FROM `tabAdditional Salary` WHERE ref_doctype = 'Gratuity'")
|
frappe.db.sql("DELETE FROM `tabAdditional Salary` WHERE ref_doctype = 'Gratuity'")
|
||||||
|
|
||||||
|
=======
|
||||||
|
@set_holiday_list("Salary Slip Test Holiday List", "_Test Company")
|
||||||
|
>>>>>>> 6c66bbbbfe (refactor: clean-up gratuity tests)
|
||||||
def test_get_last_salary_slip_should_return_none_for_new_employee(self):
|
def test_get_last_salary_slip_should_return_none_for_new_employee(self):
|
||||||
new_employee = make_employee("new_employee@salary.com", company="_Test Company")
|
new_employee = make_employee("new_employee@salary.com", company="_Test Company")
|
||||||
salary_slip = get_last_salary_slip(new_employee)
|
salary_slip = get_last_salary_slip(new_employee)
|
||||||
assert salary_slip is None
|
self.assertIsNone(salary_slip)
|
||||||
|
|
||||||
def test_check_gratuity_amount_based_on_current_slab_and_additional_salary_creation(self):
|
@set_holiday_list("Salary Slip Test Holiday List", "_Test Company")
|
||||||
employee, sal_slip = create_employee_and_get_last_salary_slip()
|
def test_gratuity_based_on_current_slab_via_additional_salary(self):
|
||||||
|
"""
|
||||||
|
Range | Fraction
|
||||||
|
5-0 | 1
|
||||||
|
"""
|
||||||
|
doj = add_days(getdate(), -(6 * 365))
|
||||||
|
relieving_date = getdate()
|
||||||
|
|
||||||
|
employee = make_employee(
|
||||||
|
"test_employee_gratuity@salary.com",
|
||||||
|
company="_Test Company",
|
||||||
|
date_of_joining=doj,
|
||||||
|
relieving_date=relieving_date,
|
||||||
|
)
|
||||||
|
sal_slip = create_salary_slip("test_employee_gratuity@salary.com")
|
||||||
|
|
||||||
rule = get_gratuity_rule("Rule Under Unlimited Contract on termination (UAE)")
|
rule = get_gratuity_rule("Rule Under Unlimited Contract on termination (UAE)")
|
||||||
|
|
||||||
gratuity = create_gratuity(pay_via_salary_slip=1, employee=employee, rule=rule.name)
|
gratuity = create_gratuity(pay_via_salary_slip=1, employee=employee, rule=rule.name)
|
||||||
|
|
||||||
# work experience calculation
|
# work experience calculation
|
||||||
date_of_joining, relieving_date = frappe.db.get_value(
|
employee_total_workings_days = (get_datetime(relieving_date) - get_datetime(doj)).days
|
||||||
"Employee", employee, ["date_of_joining", "relieving_date"]
|
experience = floor(employee_total_workings_days / rule.total_working_days_per_year)
|
||||||
)
|
self.assertEqual(gratuity.current_work_experience, experience)
|
||||||
employee_total_workings_days = (
|
|
||||||
get_datetime(relieving_date) - get_datetime(date_of_joining)
|
|
||||||
).days
|
|
||||||
|
|
||||||
experience = employee_total_workings_days / rule.total_working_days_per_year
|
# amount calculation
|
||||||
gratuity.reload()
|
|
||||||
from math import floor
|
|
||||||
|
|
||||||
self.assertEqual(floor(experience), gratuity.current_work_experience)
|
|
||||||
|
|
||||||
# amount Calculation
|
|
||||||
component_amount = frappe.get_all(
|
component_amount = frappe.get_all(
|
||||||
"Salary Detail",
|
"Salary Detail",
|
||||||
filters={
|
filters={
|
||||||
@@ -79,18 +89,15 @@ class TestGratuity(FrappeTestCase):
|
|||||||
"salary_component": "Basic Salary",
|
"salary_component": "Basic Salary",
|
||||||
},
|
},
|
||||||
fields=["amount"],
|
fields=["amount"],
|
||||||
|
limit=1,
|
||||||
)
|
)
|
||||||
|
|
||||||
""" 5 - 0 fraction is 1 """
|
|
||||||
|
|
||||||
gratuity_amount = component_amount[0].amount * experience
|
gratuity_amount = component_amount[0].amount * experience
|
||||||
gratuity.reload()
|
|
||||||
|
|
||||||
self.assertEqual(flt(gratuity_amount, 2), flt(gratuity.amount, 2))
|
self.assertEqual(flt(gratuity_amount, 2), flt(gratuity.amount, 2))
|
||||||
|
|
||||||
# additional salary creation (Pay via salary slip)
|
# additional salary creation (Pay via salary slip)
|
||||||
self.assertTrue(frappe.db.exists("Additional Salary", {"ref_docname": gratuity.name}))
|
self.assertTrue(frappe.db.exists("Additional Salary", {"ref_docname": gratuity.name}))
|
||||||
|
|
||||||
|
# gratuity should be marked "Paid" on the next salary slip submission
|
||||||
salary_slip = make_salary_slip("Test Gratuity", employee=employee)
|
salary_slip = make_salary_slip("Test Gratuity", employee=employee)
|
||||||
salary_slip.posting_date = getdate()
|
salary_slip.posting_date = getdate()
|
||||||
salary_slip.insert()
|
salary_slip.insert()
|
||||||
@@ -99,8 +106,27 @@ class TestGratuity(FrappeTestCase):
|
|||||||
gratuity.reload()
|
gratuity.reload()
|
||||||
self.assertEqual(gratuity.status, "Paid")
|
self.assertEqual(gratuity.status, "Paid")
|
||||||
|
|
||||||
def test_check_gratuity_amount_based_on_all_previous_slabs(self):
|
@set_holiday_list("Salary Slip Test Holiday List", "_Test Company")
|
||||||
employee, sal_slip = create_employee_and_get_last_salary_slip()
|
def test_gratuity_based_on_all_previous_slabs_via_payment_entry(self):
|
||||||
|
"""
|
||||||
|
Range | Fraction
|
||||||
|
0-1 | 0
|
||||||
|
1-5 | 0.7
|
||||||
|
5-0 | 1
|
||||||
|
"""
|
||||||
|
from erpnext.accounts.doctype.payment_entry.payment_entry import get_payment_entry
|
||||||
|
|
||||||
|
doj = add_days(getdate(), -(6 * 365))
|
||||||
|
relieving_date = getdate()
|
||||||
|
|
||||||
|
employee = make_employee(
|
||||||
|
"test_employee_gratuity@salary.com",
|
||||||
|
company="_Test Company",
|
||||||
|
date_of_joining=doj,
|
||||||
|
relieving_date=relieving_date,
|
||||||
|
)
|
||||||
|
|
||||||
|
sal_slip = create_salary_slip("test_employee_gratuity@salary.com")
|
||||||
rule = get_gratuity_rule("Rule Under Limited Contract (UAE)")
|
rule = get_gratuity_rule("Rule Under Limited Contract (UAE)")
|
||||||
set_mode_of_payment_account()
|
set_mode_of_payment_account()
|
||||||
|
|
||||||
@@ -109,22 +135,11 @@ class TestGratuity(FrappeTestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# work experience calculation
|
# work experience calculation
|
||||||
date_of_joining, relieving_date = frappe.db.get_value(
|
employee_total_workings_days = (get_datetime(relieving_date) - get_datetime(doj)).days
|
||||||
"Employee", employee, ["date_of_joining", "relieving_date"]
|
experience = floor(employee_total_workings_days / rule.total_working_days_per_year)
|
||||||
)
|
self.assertEqual(gratuity.current_work_experience, experience)
|
||||||
employee_total_workings_days = (
|
|
||||||
get_datetime(relieving_date) - get_datetime(date_of_joining)
|
|
||||||
).days
|
|
||||||
|
|
||||||
experience = employee_total_workings_days / rule.total_working_days_per_year
|
# amount calculation
|
||||||
|
|
||||||
gratuity.reload()
|
|
||||||
|
|
||||||
from math import floor
|
|
||||||
|
|
||||||
self.assertEqual(floor(experience), gratuity.current_work_experience)
|
|
||||||
|
|
||||||
# amount Calculation
|
|
||||||
component_amount = frappe.get_all(
|
component_amount = frappe.get_all(
|
||||||
"Salary Detail",
|
"Salary Detail",
|
||||||
filters={
|
filters={
|
||||||
@@ -134,36 +149,29 @@ class TestGratuity(FrappeTestCase):
|
|||||||
"salary_component": "Basic Salary",
|
"salary_component": "Basic Salary",
|
||||||
},
|
},
|
||||||
fields=["amount"],
|
fields=["amount"],
|
||||||
|
limit=1,
|
||||||
)
|
)
|
||||||
|
|
||||||
""" range | Fraction
|
|
||||||
0-1 | 0
|
|
||||||
1-5 | 0.7
|
|
||||||
5-0 | 1
|
|
||||||
"""
|
|
||||||
|
|
||||||
gratuity_amount = ((0 * 1) + (4 * 0.7) + (1 * 1)) * component_amount[0].amount
|
gratuity_amount = ((0 * 1) + (4 * 0.7) + (1 * 1)) * component_amount[0].amount
|
||||||
gratuity.reload()
|
|
||||||
|
|
||||||
self.assertEqual(flt(gratuity_amount, 2), flt(gratuity.amount, 2))
|
self.assertEqual(flt(gratuity_amount, 2), flt(gratuity.amount, 2))
|
||||||
self.assertEqual(gratuity.status, "Unpaid")
|
self.assertEqual(gratuity.status, "Unpaid")
|
||||||
|
|
||||||
from erpnext.accounts.doctype.payment_entry.payment_entry import get_payment_entry
|
pe = get_payment_entry("Gratuity", gratuity.name)
|
||||||
|
pe.reference_no = "123467"
|
||||||
|
pe.reference_date = getdate()
|
||||||
|
pe.submit()
|
||||||
|
|
||||||
pay_entry = get_payment_entry("Gratuity", gratuity.name)
|
|
||||||
pay_entry.reference_no = "123467"
|
|
||||||
pay_entry.reference_date = getdate()
|
|
||||||
pay_entry.save()
|
|
||||||
pay_entry.submit()
|
|
||||||
gratuity.reload()
|
gratuity.reload()
|
||||||
|
|
||||||
self.assertEqual(gratuity.status, "Paid")
|
self.assertEqual(gratuity.status, "Paid")
|
||||||
self.assertEqual(flt(gratuity.paid_amount, 2), flt(gratuity.amount, 2))
|
self.assertEqual(flt(gratuity.paid_amount, 2), flt(gratuity.amount, 2))
|
||||||
|
|
||||||
|
<<<<<<< HEAD
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
frappe.db.sql("DELETE FROM `tabGratuity`")
|
frappe.db.sql("DELETE FROM `tabGratuity`")
|
||||||
frappe.db.sql("DELETE FROM `tabAdditional Salary` WHERE ref_doctype = 'Gratuity'")
|
frappe.db.sql("DELETE FROM `tabAdditional Salary` WHERE ref_doctype = 'Gratuity'")
|
||||||
|
|
||||||
|
=======
|
||||||
|
>>>>>>> 6c66bbbbfe (refactor: clean-up gratuity tests)
|
||||||
|
|
||||||
def get_gratuity_rule(name):
|
def get_gratuity_rule(name):
|
||||||
rule = frappe.db.exists("Gratuity Rule", name)
|
rule = frappe.db.exists("Gratuity Rule", name)
|
||||||
@@ -173,7 +181,6 @@ def get_gratuity_rule(name):
|
|||||||
rule.applicable_earnings_component = []
|
rule.applicable_earnings_component = []
|
||||||
rule.append("applicable_earnings_component", {"salary_component": "Basic Salary"})
|
rule.append("applicable_earnings_component", {"salary_component": "Basic Salary"})
|
||||||
rule.save()
|
rule.save()
|
||||||
rule.reload()
|
|
||||||
|
|
||||||
return rule
|
return rule
|
||||||
|
|
||||||
@@ -228,29 +235,17 @@ def create_account():
|
|||||||
).insert(ignore_permissions=True)
|
).insert(ignore_permissions=True)
|
||||||
|
|
||||||
|
|
||||||
def create_employee_and_get_last_salary_slip():
|
def create_salary_slip(employee):
|
||||||
employee = make_employee("test_employee@salary.com", company="_Test Company")
|
|
||||||
frappe.db.set_value("Employee", employee, "relieving_date", getdate())
|
|
||||||
frappe.db.set_value("Employee", employee, "date_of_joining", add_days(getdate(), -(6 * 365)))
|
|
||||||
if not frappe.db.exists("Salary Slip", {"employee": employee}):
|
if not frappe.db.exists("Salary Slip", {"employee": employee}):
|
||||||
posting_date = get_first_day(add_months(getdate(), -1))
|
posting_date = get_first_day(add_months(getdate(), -1))
|
||||||
salary_slip = make_employee_salary_slip(
|
salary_slip = make_employee_salary_slip(
|
||||||
"test_employee@salary.com", "Monthly", "Test Gratuity", posting_date=posting_date
|
employee, "Monthly", "Test Gratuity", posting_date=posting_date
|
||||||
)
|
)
|
||||||
salary_slip.start_date = posting_date
|
salary_slip.start_date = posting_date
|
||||||
salary_slip.end_date = None
|
salary_slip.end_date = None
|
||||||
salary_slip.save()
|
|
||||||
salary_slip.submit()
|
salary_slip.submit()
|
||||||
salary_slip = salary_slip.name
|
salary_slip = salary_slip.name
|
||||||
else:
|
else:
|
||||||
salary_slip = get_last_salary_slip(employee)
|
salary_slip = get_last_salary_slip(employee)
|
||||||
|
|
||||||
if not frappe.db.get_value("Employee", "test_employee@salary.com", "holiday_list"):
|
return salary_slip
|
||||||
from erpnext.payroll.doctype.salary_slip.test_salary_slip import make_holiday_list
|
|
||||||
|
|
||||||
make_holiday_list()
|
|
||||||
frappe.db.set_value(
|
|
||||||
"Company", "_Test Company", "default_holiday_list", "Salary Slip Test Holiday List"
|
|
||||||
)
|
|
||||||
|
|
||||||
return employee, salary_slip
|
|
||||||
|
|||||||
Reference in New Issue
Block a user