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

refactor: leave encashment doc tests #2841

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ frappe.ui.form.on("Full and Final Statement", {

frm.set_query("reference_document", type, function (doc, cdt, cdn) {
let fnf_doc = frappe.get_doc(cdt, cdn);
3;

frappe.model.with_doctype(fnf_doc.reference_document_type, function () {
if (frappe.model.is_tree(fnf_doc.reference_document_type)) {
Expand All @@ -46,6 +47,11 @@ frappe.ui.form.on("Full and Final Statement", {
if (frappe.meta.has_field(fnf_doc.reference_document_type, "employee")) {
filters["employee"] = frm.doc.employee;
}

if (fnf_doc.reference_document_type === "Leave Encashment") {
filters["status"] = "Unpaid";
filters["pay_via_payment_entry"] = 1;
}
});

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,17 @@ def create_journal_entry(self):
)
return jv

def set_gratuity_status(self):
def update_reference_document_payment_status(self, payable):
doc = frappe.get_cached_doc(payable.reference_document_type, payable.reference_document)
amount = payable.amount if self.docstatus == 1 and self.status == "Paid" else 0
doc.db_set("paid_amount", amount)
doc.set_status(update=True)

def update_linked_payable_documents(self):
"""update payment status in linked payable documents"""
for payable in self.payables:
if payable.component != "Gratuity":
continue
gratuity = frappe.get_doc("Gratuity", payable.reference_document)
amount = payable.amount if self.docstatus == 1 and self.status == "Paid" else 0
gratuity.db_set("paid_amount", amount)
gratuity.set_status(update=True)
if payable.reference_document_type in ["Gratuity", "Leave Encashment"]:
self.update_reference_document_payment_status(payable)


@frappe.whitelist()
Expand Down Expand Up @@ -333,4 +336,4 @@ def update_full_and_final_statement_status(doc, method=None):
fnf = frappe.get_doc("Full and Final Statement", entry.reference_name)
fnf.db_set("status", status)
fnf.notify_update()
fnf.set_gratuity_status()
fnf.update_linked_payable_documents()
131 changes: 51 additions & 80 deletions hrms/hr/doctype/leave_encashment/test_leave_encashment.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,7 @@ def setUp(self):

@set_holiday_list("_Test Leave Encashment", "_Test Company")
def test_leave_balance_value_and_amount(self):
leave_encashment = frappe.get_doc(
dict(
doctype="Leave Encashment",
employee=self.employee,
leave_type="_Test Leave Type Encashment",
leave_period=self.leave_period.name,
encashment_date=self.leave_period.to_date,
currency="INR",
)
).insert()
leave_encashment = self.create_test_leave_encashment()

self.assertEqual(leave_encashment.leave_balance, 10)
self.assertTrue(leave_encashment.actual_encashable_days, 5)
Expand Down Expand Up @@ -119,16 +110,7 @@ def test_non_encashable_leaves_setting(self):
"_Test Leave Type Encashment",
)

leave_encashment = frappe.get_doc(
dict(
doctype="Leave Encashment",
employee=self.employee,
leave_type="_Test Leave Type Encashment",
leave_period=self.leave_period.name,
encashment_date=self.leave_period.to_date,
currency="INR",
)
).insert()
leave_encashment = self.create_test_leave_encashment()

self.assertEqual(leave_encashment.leave_balance, 7)
# non-encashable leaves = 5, total leaves are 7, so encashable days = 7-5 = 2
Expand Down Expand Up @@ -164,16 +146,7 @@ def test_max_encashable_leaves_setting(self):
"_Test Leave Type Encashment",
)

leave_encashment = frappe.get_doc(
dict(
doctype="Leave Encashment",
employee=self.employee,
leave_type="_Test Leave Type Encashment",
leave_period=self.leave_period.name,
encashment_date=self.leave_period.to_date,
currency="INR",
)
).insert()
leave_encashment = self.create_test_leave_encashment()

self.assertEqual(leave_encashment.leave_balance, 7)
# leave balance = 7, but encashment limit = 3 so encashable days = 3
Expand Down Expand Up @@ -208,16 +181,7 @@ def test_max_encashable_leaves_and_non_encashable_leaves_setting(self):
"_Test Leave Type Encashment",
)

leave_encashment = frappe.get_doc(
dict(
doctype="Leave Encashment",
employee=self.employee,
leave_type="_Test Leave Type Encashment",
leave_period=self.leave_period.name,
encashment_date=self.leave_period.to_date,
currency="INR",
)
).insert()
leave_encashment = self.create_test_leave_encashment()

self.assertEqual(leave_encashment.leave_balance, 7)
# 1. non-encashable leaves = 5, total leaves are 7, so encashable days = 7-5 = 2
Expand All @@ -235,17 +199,7 @@ def test_max_encashable_leaves_and_non_encashable_leaves_setting(self):

@set_holiday_list("_Test Leave Encashment", "_Test Company")
def test_creation_of_leave_ledger_entry_on_submit(self):
leave_encashment = frappe.get_doc(
dict(
doctype="Leave Encashment",
employee=self.employee,
leave_type="_Test Leave Type Encashment",
leave_period=self.leave_period.name,
encashment_date=self.leave_period.to_date,
currency="INR",
)
).insert()

leave_encashment = self.create_test_leave_encashment()
leave_encashment.submit()

leave_ledger_entry = frappe.get_all(
Expand Down Expand Up @@ -353,17 +307,9 @@ def get_encashment_created_after_leave_period(self, employee, is_carry_forward,
other_details={"leave_encashment_amount_per_day": 50},
)

leave_encashment = frappe.get_doc(
{
"doctype": "Leave Encashment",
"employee": employee,
"leave_type": self.leave_type,
"leave_period": self.leave_period.name,
"encashment_date": self.leave_period.to_date,
"encashment_days": encashment_days,
"currency": "INR",
}
).insert()
leave_encashment = self.create_test_leave_encashment(
employee=employee, encashment_days=encashment_days, leave_type=self.leave_type
)
leave_encashment.submit()
return leave_encashment

Expand All @@ -388,14 +334,7 @@ def test_status_of_leave_encashment_after_payment_via_salary_slip(self):
currency="INR",
)

leave_encashment = frappe.get_doc(
doctype="Leave Encashment",
employee=self.employee,
leave_type="_Test Leave Type Encashment",
leave_period=self.leave_period.name,
encashment_date=getdate(),
currency="INR",
).insert()
leave_encashment = self.create_test_leave_encashment(encashment_date=getdate())
leave_encashment.submit()

ss = make_employee_salary_slip(self.employee, "Monthly", salary_structure=salary_structure.name)
Expand All @@ -411,16 +350,7 @@ def test_status_of_leave_encashment_after_payment_via_salary_slip(self):
def test_status_of_leave_encashment_after_payment_via_payment_entry(self):
from hrms.overrides.employee_payment_entry import get_payment_entry_for_employee

leave_encashment = frappe.get_doc(
doctype="Leave Encashment",
employee=self.employee,
leave_type="_Test Leave Type Encashment",
leave_period=self.leave_period.name,
pay_via_payment_entry=1,
payable_account=frappe.get_cached_value("Company", "_Test Company", "default_payable_account"),
expense_account="Administrative Expenses - _TC",
cost_center="Main - _TC",
).save()
leave_encashment = self.create_test_leave_encashment(pay_via_payment_entry=1)
leave_encashment.submit()

pe = get_payment_entry_for_employee(leave_encashment.doctype, leave_encashment.name)
Expand All @@ -432,3 +362,44 @@ def test_status_of_leave_encashment_after_payment_via_payment_entry(self):
leave_encashment.reload()

self.assertEqual(leave_encashment.status, "Paid")

def create_test_leave_encashment(self, **kwargs):
"""Helper method to create leave encashment with default values"""
args = {
"employee": self.employee,
"leave_type": "_Test Leave Type Encashment",
"leave_period": self.leave_period.name,
"encashment_date": self.leave_period.to_date,
"currency": "INR",
}
args.update(kwargs)
return create_leave_encashment(**args)


def create_leave_encashment(**args):
if args:
args = frappe._dict(args)
leave_encashment = frappe.new_doc("Leave Encashment")
leave_encashment.company = args.company or "_Test Company"
leave_encashment.employee = args.employee
leave_encashment.posting_date = args.posting_date or getdate()
leave_encashment.leave_type = args.leave_type
leave_encashment.leave_period = args.leave_period
leave_encashment.encashment_date = args.encashment_date or getdate()
leave_encashment.currency = args.currency or frappe.get_cached_value(
"Company", "_Test Company", "default_payable_account"
)
leave_encashment.pay_via_payment_entry = args.pay_via_payment_entry or 0
if leave_encashment.pay_via_payment_entry:
leave_encashment.payable_account = args.payable_account or frappe.get_cached_value(
"Company", "_Test Company", "default_payable_account"
)
leave_encashment.expense_account = args.expense_account or "Administrative Expenses - _TC"
leave_encashment.cost_center = args.cost_center or "Main - _TC"

if args.encashment_days:
leave_encashment.encashment_days = args.encashment_days

leave_encashment.insert()

return leave_encashment
Loading