diff --git a/hrms/payroll/doctype/payroll_entry/payroll_entry.py b/hrms/payroll/doctype/payroll_entry/payroll_entry.py index f2488b858f..e31b9d59f9 100644 --- a/hrms/payroll/doctype/payroll_entry/payroll_entry.py +++ b/hrms/payroll/doctype/payroll_entry/payroll_entry.py @@ -923,7 +923,9 @@ def make_bank_entry(self, for_withheld_salaries=False): salary_slip_total -= salary_detail.amount - salary_slip_total -= flt(salary_detail.get("total_loan_repayment")) + unique_salary_slips = {slip["name"]: slip for slip in salary_slips}.values() + total_loan_repayment = sum(flt(slip.get("total_loan_repayment", 0)) for slip in unique_salary_slips) + salary_slip_total -= total_loan_repayment bank_entry = None if salary_slip_total > 0: diff --git a/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py b/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py index 2a5acdaee0..ef0e35d755 100644 --- a/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py +++ b/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py @@ -5,7 +5,7 @@ import frappe from frappe.tests.utils import FrappeTestCase, change_settings -from frappe.utils import add_days, add_months, cstr +from frappe.utils import add_days, add_months, cstr, flt import erpnext from erpnext.accounts.utils import get_fiscal_year, getdate, nowdate @@ -749,9 +749,34 @@ def test_loan_repayment_from_salary(self): total_loan_repayment=loan.monthly_repayment_amount, ) + salary_slip_name = frappe.db.get_value("Salary Slip", {"payroll_entry": payroll_entry.name}, "name") + salary_slip = frappe.get_doc("Salary Slip", salary_slip_name) + payroll_entry.reload() + + initial_gross_pay = flt(salary_slip.gross_pay) - flt(salary_slip.total_deduction) + loan_repayment_amount = flt(salary_slip.total_loan_repayment) + expected_bank_entry_amount = initial_gross_pay - loan_repayment_amount + payroll_entry.make_bank_entry() submit_bank_entry(payroll_entry.name) + bank_entry = frappe.db.sql( + """ + SELECT je.total_debit, je.total_credit + FROM `tabJournal Entry` je + INNER JOIN `tabJournal Entry Account` jea ON je.name = jea.parent + WHERE je.voucher_type = 'Bank Entry' AND jea.reference_type = 'Payroll Entry' AND jea.reference_name = %s + LIMIT 1 + """, + payroll_entry.name, + as_dict=True, + ) + + total_debit = bank_entry[0].get("total_debit", 0) + total_credit = bank_entry[0].get("total_credit", 0) + self.assertEqual(total_debit, expected_bank_entry_amount) + self.assertEqual(total_credit, expected_bank_entry_amount) + def get_payroll_entry(**args): args = frappe._dict(args)