diff --git a/erpnext/payroll/doctype/gratuity/gratuity.py b/erpnext/payroll/doctype/gratuity/gratuity.py index 8cb804db6fa3..31a6af3a0776 100644 --- a/erpnext/payroll/doctype/gratuity/gratuity.py +++ b/erpnext/payroll/doctype/gratuity/gratuity.py @@ -242,7 +242,11 @@ def get_salary_structure(employee): order_by = "from_date desc")[0].salary_structure def get_last_salary_slip(employee): - return frappe.get_list("Salary Slip", filters = { + salary_slips = frappe.get_list("Salary Slip", filters = { "employee": employee, 'docstatus': 1 }, - order_by = "start_date desc")[0].name + order_by = "start_date desc" + ) + if not salary_slips: + return + return salary_slips[0].name diff --git a/erpnext/payroll/doctype/gratuity/test_gratuity.py b/erpnext/payroll/doctype/gratuity/test_gratuity.py index 7daea2da474e..6c3b6fbae58e 100644 --- a/erpnext/payroll/doctype/gratuity/test_gratuity.py +++ b/erpnext/payroll/doctype/gratuity/test_gratuity.py @@ -24,6 +24,11 @@ def setUp(self): frappe.db.sql("DELETE FROM `tabGratuity`") frappe.db.sql("DELETE FROM `tabAdditional Salary` WHERE ref_doctype = 'Gratuity'") + def test_get_last_salary_slip_should_return_none_for_new_employee(self): + new_employee = make_employee("new_employee@salary.com", company='_Test Company') + salary_slip = get_last_salary_slip(new_employee) + assert salary_slip is None + def test_check_gratuity_amount_based_on_current_slab_and_additional_salary_creation(self): employee, sal_slip = create_employee_and_get_last_salary_slip()