Skip to content

Commit

Permalink
fix: correct bank entry calculation with deductions and prevent multi…
Browse files Browse the repository at this point in the history
…ple loan repayment deductions (backport #2347) (#2356)

Co-authored-by: Nihantra Patel <nihantra@frappe.io>
Co-authored-by: Rucha Mahabal <ruchamahabal2@gmail.com>
  • Loading branch information
3 people authored Oct 29, 2024
1 parent 0155017 commit 372e552
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
4 changes: 3 additions & 1 deletion hrms/payroll/doctype/payroll_entry/payroll_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
27 changes: 26 additions & 1 deletion hrms/payroll/doctype/payroll_entry/test_payroll_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 372e552

Please sign in to comment.