Skip to content

Commit

Permalink
feat(Payroll): Option to use Email Template when send Salary Slip email
Browse files Browse the repository at this point in the history
  • Loading branch information
kittiu committed Jan 24, 2024
1 parent 6e30318 commit 585d9ed
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
10 changes: 9 additions & 1 deletion hrms/payroll/doctype/payroll_settings/payroll_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"email_salary_slip_to_employee",
"sender",
"sender_email",
"email_template",
"column_break_iewr",
"encrypt_salary_slips_in_emails",
"password_policy",
Expand Down Expand Up @@ -172,13 +173,20 @@
"fieldtype": "Data",
"label": "Sender Email",
"read_only": 1
},
{
"depends_on": "eval:doc.email_salary_slip_to_employee",
"fieldname": "email_template",
"fieldtype": "Link",
"label": "Email Template",
"options": "Email Template"
}
],
"icon": "fa fa-cog",
"index_web_pages_for_search": 1,
"issingle": 1,
"links": [],
"modified": "2023-11-01 13:51:04.225492",
"modified": "2024-01-23 17:42:52.958013",
"modified_by": "Administrator",
"module": "Payroll",
"name": "Payroll Settings",
Expand Down
22 changes: 15 additions & 7 deletions hrms/payroll/doctype/salary_slip/salary_slip.py
Original file line number Diff line number Diff line change
Expand Up @@ -1794,21 +1794,29 @@ def get_component_totals(self, component_type, depends_on_payment_days=0):
def email_salary_slip(self):
receiver = frappe.db.get_value("Employee", self.employee, "prefered_email", cache=True)
payroll_settings = frappe.get_single("Payroll Settings")
message = "Please see attachment"
subject = "Salary Slip - from {0} to {1}".format(self.start_date, self.end_date)
message = _("Please see attachment")
if payroll_settings.email_template:
email_template = frappe.get_doc("Email Template", payroll_settings.email_template)
doc = frappe.get_doc("Salary Slip", self.name)
context = doc.as_dict()
subject = frappe.render_template(email_template.subject, context)
message = frappe.render_template(email_template.response_, context)
password = None
if payroll_settings.encrypt_salary_slips_in_emails:
password = generate_password_for_pdf(payroll_settings.password_policy, self.employee)
message += """<br>Note: Your salary slip is password protected,
the password to unlock the PDF is of the format {0}. """.format(
payroll_settings.password_policy
)
if not payroll_settings.email_template:
message += _("""<br>Note: Your salary slip is password protected,
the password to unlock the PDF is of the format {0}. """.format(
payroll_settings.password_policy)
)

if receiver:
email_args = {
"sender": payroll_settings.sender_email,
"recipients": [receiver],
"message": _(message),
"subject": "Salary Slip - from {0} to {1}".format(self.start_date, self.end_date),
"message": message,
"subject": subject,
"attachments": [
frappe.attach_print(self.doctype, self.name, file_name=self.name, password=password)
],
Expand Down
31 changes: 31 additions & 0 deletions hrms/payroll/doctype/salary_slip/test_salary_slip.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class TestSalarySlip(FrappeTestCase):
def setUp(self):
setup_test()
frappe.flags.pop("via_payroll_entry", None)
create_ss_email_template()
clear_cache()

def tearDown(self):
Expand Down Expand Up @@ -607,6 +608,22 @@ def test_email_salary_slip(self):
email_queue = frappe.db.a_row_exists("Email Queue")
self.assertTrue(email_queue)

@change_settings(
"Payroll Settings",
{"email_salary_slip_to_employee": 1, "email_template": "Salary Slip"}
)
def test_email_salary_slip_with_email_template(self):
frappe.db.delete("Email Queue")

emp_id = make_employee("[email protected]", company="_Test Company")
ss = make_employee_salary_slip(emp_id, "Monthly", "Test Salary Slip Email")
ss.company = "_Test Company"
ss.save()
ss.submit()

email_queue = frappe.db.a_row_exists("Email Queue")
self.assertTrue(email_queue)

@if_lending_app_installed
def test_loan_repayment_salary_slip(self):
from lending.loan_management.doctype.loan.test_loan import (
Expand Down Expand Up @@ -2391,6 +2408,20 @@ def mark_attendance(
attendance.submit()


def create_ss_email_template():
if not frappe.db.exists("Email Template", "Salary Slip"):
ss_template = frappe.get_doc(
{
"doctype": "Salary Slip",
"name": "Delivery Notification",
"response": "Test Salary Slip",
"subject": "Test Subject",
"owner": frappe.session.user,
}
)
ss_template.insert()


def clear_cache():
for key in [
HOLIDAYS_BETWEEN_DATES,
Expand Down

0 comments on commit 585d9ed

Please sign in to comment.