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

fix(Payroll Bank Entry): deduct loan repayments from employee-wise accounting entries #2403

Merged
merged 2 commits into from
Nov 8, 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
34 changes: 27 additions & 7 deletions hrms/payroll/doctype/payroll_entry/payroll_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
)
from erpnext.accounts.utils import get_fiscal_year

from hrms.payroll.doctype.salary_slip.salary_slip_loan_utils import if_lending_app_installed
from hrms.payroll.doctype.salary_withholding.salary_withholding import link_bank_entry_in_salary_withholdings


Expand Down Expand Up @@ -871,9 +872,9 @@ def make_bank_entry(self, for_withheld_salaries=False):
)

salary_slip_total = 0
salary_slips = self.get_salary_slip_details(for_withheld_salaries)
salary_details = self.get_salary_slip_details(for_withheld_salaries)

for salary_detail in salary_slips:
for salary_detail in salary_details:
if salary_detail.parentfield == "earnings":
(
is_flexible_benefit,
Expand Down Expand Up @@ -923,8 +924,7 @@ def make_bank_entry(self, for_withheld_salaries=False):

salary_slip_total -= salary_detail.amount

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)
total_loan_repayment = self.process_loan_repayments_for_bank_entry(salary_details) or 0
salary_slip_total -= total_loan_repayment

bank_entry = None
Expand All @@ -933,7 +933,7 @@ def make_bank_entry(self, for_withheld_salaries=False):
bank_entry = self.set_accounting_entries_for_bank_entry(salary_slip_total, remark)

if for_withheld_salaries:
link_bank_entry_in_salary_withholdings(salary_slips, bank_entry.name)
link_bank_entry_in_salary_withholdings(salary_details, bank_entry.name)

return bank_entry

Expand Down Expand Up @@ -971,6 +971,23 @@ def get_salary_slip_details(self, for_withheld_salaries=False):
query = query.where(SalarySlip.status != "Withheld")
return query.run(as_dict=True)

@if_lending_app_installed
def process_loan_repayments_for_bank_entry(self, salary_details: list[dict]) -> float:
unique_salary_slips = {row["employee"]: row for row in salary_details}.values()
total_loan_repayment = sum(flt(slip.get("total_loan_repayment", 0)) for slip in unique_salary_slips)

if self.employee_based_payroll_payable_entries:
for salary_slip in unique_salary_slips:
if salary_slip.get("total_loan_repayment"):
self.set_employee_based_payroll_payable_entries(
"total_loan_repayment",
salary_slip.employee,
salary_slip.total_loan_repayment,
salary_slip.salary_structure,
)

return total_loan_repayment

def set_accounting_entries_for_bank_entry(self, je_payment_amount, user_remark):
payroll_payable_account = self.payroll_payable_account
precision = frappe.get_precision("Journal Entry Account", "debit_in_account_currency")
Expand Down Expand Up @@ -998,9 +1015,12 @@ def set_accounting_entries_for_bank_entry(self, je_payment_amount, user_remark):

if self.employee_based_payroll_payable_entries:
for employee, employee_details in self.employee_based_payroll_payable_entries.items():
je_payment_amount = employee_details.get("earnings", 0) - (
employee_details.get("deductions", 0)
je_payment_amount = (
employee_details.get("earnings", 0)
- employee_details.get("deductions", 0)
- employee_details.get("total_loan_repayment", 0)
)

exchange_rate, amount = self.get_amount_and_exchange_rate_for_journal_entry(
self.payment_account, je_payment_amount, company_currency, currencies
)
Expand Down
13 changes: 11 additions & 2 deletions hrms/payroll/doctype/payroll_entry/test_payroll_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from dateutil.relativedelta import relativedelta

import frappe
from frappe.tests import IntegrationTestCase, change_settings
from frappe.tests.utils import FrappeTestCase, change_settings
from frappe.utils import add_days, add_months, cstr, flt

import erpnext
Expand Down Expand Up @@ -42,10 +42,11 @@
test_dependencies = ["Holiday List"]


class TestPayrollEntry(IntegrationTestCase):
class TestPayrollEntry(FrappeTestCase):
def setUp(self):
for dt in [
"Salary Slip",
"Salary Detail",
"Salary Component",
"Salary Component Account",
"Payroll Entry",
Expand Down Expand Up @@ -720,6 +721,14 @@ def test_validate_attendance(self):
@if_lending_app_installed
@change_settings("Payroll Settings", {"process_payroll_accounting_entry_based_on_employee": 0})
def test_loan_repayment_from_salary(self):
self.run_test_for_loan_repayment_from_salary()

@if_lending_app_installed
@change_settings("Payroll Settings", {"process_payroll_accounting_entry_based_on_employee": 1})
def test_loan_repayment_from_salary_with_employee_tagging(self):
self.run_test_for_loan_repayment_from_salary()

def run_test_for_loan_repayment_from_salary(self):
from lending.loan_management.doctype.loan.test_loan import make_loan_disbursement_entry
from lending.loan_management.doctype.process_loan_interest_accrual.process_loan_interest_accrual import (
process_loan_interest_accrual_for_term_loans,
Expand Down
Loading