Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Payroll): Option to use Email Template when send Salary Slip email #1350

Merged
merged 5 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions hrms/hr/doctype/interview/test_interview.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
update_job_applicant_status,
)
from hrms.hr.doctype.job_applicant.job_applicant import get_interview_details
from hrms.tests.test_utils import create_job_applicant
from hrms.tests.test_utils import create_job_applicant, get_email_by_subject


class TestInterview(FrappeTestCase):
Expand Down Expand Up @@ -317,7 +317,3 @@ def setup_reminder_settings():
hr_settings.interview_reminder_template = _("Interview Reminder")
hr_settings.feedback_reminder_notification_template = _("Interview Feedback Reminder")
hr_settings.save()


def get_email_by_subject(subject: str) -> bool:
return frappe.db.exists("Email Queue", {"message": ("like", f"%{subject}%")})
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)
context = self.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
34 changes: 31 additions & 3 deletions hrms/payroll/doctype/salary_slip/test_salary_slip.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@
)
from hrms.payroll.doctype.salary_slip.salary_slip_loan_utils import if_lending_app_installed
from hrms.payroll.doctype.salary_structure.salary_structure import make_salary_slip
from hrms.tests.test_utils import get_first_sunday
from hrms.tests.test_utils import get_email_by_subject, get_first_sunday


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 @@ -604,8 +605,21 @@ def test_email_salary_slip(self):
ss.save()
ss.submit()

email_queue = frappe.db.a_row_exists("Email Queue")
self.assertTrue(email_queue)
self.assertIsNotNone(get_email_by_subject("Salary Slip - from"))

@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()

self.assertIsNotNone(get_email_by_subject("Test Salary Slip Email Template"))

@if_lending_app_installed
def test_loan_repayment_salary_slip(self):
Expand Down Expand Up @@ -2391,6 +2405,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": "Email Template",
"name": "Salary Slip",
"response": "Test Salary Slip",
"subject": "Test Salary Slip Email Template",
"owner": frappe.session.user,
}
)
ss_template.insert()


def clear_cache():
for key in [
HOLIDAYS_BETWEEN_DATES,
Expand Down
4 changes: 4 additions & 0 deletions hrms/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,7 @@ def create_job_applicant(**args):
job_applicant.update(filters)
job_applicant.save()
return job_applicant


def get_email_by_subject(subject: str) -> str | None:
return frappe.db.exists("Email Queue", {"message": ("like", f"%{subject}%")})
Loading