From 99b09c240fbb57e92e9966d4d946b7760e439a82 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Mon, 21 Sep 2026 17:26:52 +0530 Subject: [PATCH 1/4] fix: post employee advance recovery to the advance account and settle advances from F&F Payroll Entry credited the salary component's account when recovering an Employee Advance instead of the account the advance was paid from. The advance receivable never got cleared, and when the component's account was not a receivable ERPNext rejected the accrual entry with a misleading "already adjusted against some other voucher" error. The linked advances are now fetched in one query and the recovery is posted to each advance's own account. Full and Final Statement now prefills outstanding Employee Advances in receivables, references the advance on the settlement Journal Entry so its return amount and status update on submission, and no longer fails on placeholder Gratuity or Leave Encashment rows without a reference document. Closes #5137 Closes #3886 Closes #3550 Closes #3504 Closes #4156 --- .../employee_advance/test_employee_advance.py | 35 +++++++++++++- .../full_and_final_statement.py | 46 +++++++++++++++++-- .../test_full_and_final_statement.py | 31 +++++++++++++ .../doctype/payroll_entry/payroll_entry.py | 38 +++++++++------ .../payroll_entry/test_payroll_entry.py | 2 +- 5 files changed, 133 insertions(+), 19 deletions(-) diff --git a/hrms/hr/doctype/employee_advance/test_employee_advance.py b/hrms/hr/doctype/employee_advance/test_employee_advance.py index fb2e172544b..945e92ea5b1 100644 --- a/hrms/hr/doctype/employee_advance/test_employee_advance.py +++ b/hrms/hr/doctype/employee_advance/test_employee_advance.py @@ -245,6 +245,34 @@ def test_advance_return_on_payroll_submission(self): advance.reload() self.assertEqual(advance.status, "Returned") + def test_advance_return_posts_to_advance_account_when_component_account_differs(self): + company_doc = frappe.get_doc("Company", "_Test Company") + employee = make_employee("test_advance_component_account@payroll.com", company=company_doc.name) + + advance = make_employee_advance(employee, {"repay_unclaimed_amount_from_salary": 1}) + make_payment_entry(advance) + advance.reload() + + create_payroll_for_advance_return(employee, company_doc, advance, component_account="Salary - _TC") + + deduction_entry = frappe.get_all( + "Journal Entry Account", + fields=["account", "party", "credit"], + filters={ + "reference_type": "Employee Advance", + "reference_name": advance.name, + "is_advance": "Yes", + }, + )[0] + self.assertEqual( + deduction_entry, + {"account": advance.advance_account, "party": employee, "credit": advance.paid_amount}, + ) + + advance.reload() + self.assertEqual(advance.return_amount, advance.paid_amount) + self.assertEqual(advance.status, "Returned") + def test_payment_entry_against_advance(self): employee_name = make_employee("_T@employee.advance", "_Test Company") advance = make_employee_advance(employee_name) @@ -576,17 +604,20 @@ def create_advance_account(account_name, account_currency): ) -def create_payroll_for_advance_return(employee, company, advance, return_amount=None): +def create_payroll_for_advance_return( + employee, company, advance, return_amount=None, component_account="Employee Advances - _TC" +): # Advance deduction component component = create_salary_component( "Advance Salary", **{"type": "Deduction"}, ) + component.set("accounts", []) component.append( "accounts", { "company": company.name, - "account": "Employee Advances - _TC", + "account": component_account, }, ) component.save() diff --git a/hrms/hr/doctype/full_and_final_statement/full_and_final_statement.py b/hrms/hr/doctype/full_and_final_statement/full_and_final_statement.py index b5a297e2bb9..ed402ac5431 100644 --- a/hrms/hr/doctype/full_and_final_statement/full_and_final_statement.py +++ b/hrms/hr/doctype/full_and_final_statement/full_and_final_statement.py @@ -115,6 +115,7 @@ def get_outstanding_statements(self): components = self.get_payable_component() self.create_component_row(components, "payables") if not self.receivables: + self.add_outstanding_employee_advances() components = self.get_receivable_component() self.create_component_row(components, "receivables") self.get_assets_statements() @@ -170,8 +171,37 @@ def add_withheld_salary_slips(self): }, ) + def add_outstanding_employee_advances(self): + advances = frappe.get_all( + "Employee Advance", + filters={"employee": self.employee, "docstatus": 1}, + fields=["name", "advance_account", "paid_amount", "claimed_amount", "return_amount"], + ) + + for advance in advances: + outstanding = flt(advance.paid_amount) - flt(advance.claimed_amount) - flt(advance.return_amount) + if outstanding <= 0: + continue + + self.append( + "receivables", + { + "status": "Unsettled", + "component": "Employee Advance", + "reference_document_type": "Employee Advance", + "reference_document": advance.name, + "account": advance.advance_account, + "amount": outstanding, + }, + ) + def create_component_row(self, components, component_type): + existing_components = {row.component for row in self.get(component_type)} + for component in components: + if component in existing_components: + continue + self.append( component_type, { @@ -263,8 +293,15 @@ def create_journal_entry(self): "user_remark": data.remark, } if data.reference_document_type == "Employee Advance": - account_dict["party_type"] = "Employee" - account_dict["party"] = self.employee + account_dict.update( + { + "party_type": "Employee", + "party": self.employee, + "reference_type": "Employee Advance", + "reference_name": data.reference_document, + "is_advance": "Yes", + } + ) jv.append("accounts", account_dict) @@ -301,7 +338,10 @@ def update_reference_document_payment_status(self, payable): def update_linked_payable_documents(self): """update payment status in linked payable documents""" for payable in self.payables: - if payable.reference_document_type in ["Gratuity", "Leave Encashment"]: + if ( + payable.reference_document_type in ["Gratuity", "Leave Encashment"] + and payable.reference_document + ): self.update_reference_document_payment_status(payable) diff --git a/hrms/hr/doctype/full_and_final_statement/test_full_and_final_statement.py b/hrms/hr/doctype/full_and_final_statement/test_full_and_final_statement.py index 6a2d94543ad..a3205f0f31e 100644 --- a/hrms/hr/doctype/full_and_final_statement/test_full_and_final_statement.py +++ b/hrms/hr/doctype/full_and_final_statement/test_full_and_final_statement.py @@ -66,6 +66,37 @@ def test_journal_entry(self): self.assertEqual(debit_entry.reference_type, "Full and Final Statement") self.assertEqual(debit_entry.reference_name, self.fnf.name) + def test_employee_advance_settlement(self): + from hrms.hr.doctype.employee_advance.test_employee_advance import ( + make_employee_advance, + make_payment_entry, + ) + + advance = make_employee_advance(self.employee) + make_payment_entry(advance) + advance.reload() + + self.fnf.receivables = [] + self.fnf.get_outstanding_statements() + + advance_rows = [row for row in self.fnf.receivables if row.component == "Employee Advance"] + self.assertEqual(len(advance_rows), 1) + self.assertEqual(advance_rows[0].reference_document, advance.name) + self.assertEqual(advance_rows[0].account, advance.advance_account) + self.assertEqual(advance_rows[0].amount, advance.paid_amount) + self.fnf.save() + + jv = self.fnf.create_journal_entry() + jv.accounts[-1].account = "_Test Bank - _TC" + jv.cheque_no = "FNF-ADV-001" + jv.cheque_date = today() + jv.insert() + jv.submit() + + advance.reload() + self.assertEqual(advance.return_amount, advance.paid_amount) + self.assertEqual(advance.status, "Returned") + def test_status_on_discard(self): self.fnf.discard() self.fnf.reload() diff --git a/hrms/payroll/doctype/payroll_entry/payroll_entry.py b/hrms/payroll/doctype/payroll_entry/payroll_entry.py index b6890291433..f5881cc367e 100644 --- a/hrms/payroll/doctype/payroll_entry/payroll_entry.py +++ b/hrms/payroll/doctype/payroll_entry/payroll_entry.py @@ -450,12 +450,13 @@ def get_salary_component_total( salary_components = self.get_salary_components(component_type) if salary_components: component_dict = {} + advance_deductions = self.get_advance_deductions(component_type, salary_components) for item in salary_components: employee_cost_centers = self.get_payroll_cost_centers_for_employee( item.employee, item.salary_structure ) - employee_advance = self.get_advance_deduction(component_type, item) + employee_advance = advance_deductions.get(item.additional_salary) for cost_center, percentage in employee_cost_centers.items(): amount_against_cost_center = flt(item.amount) * percentage / 100 @@ -477,33 +478,44 @@ def get_salary_component_total( return account_details - def get_advance_deduction(self, component_type: str, item: dict) -> str | None: - if component_type == "deductions" and item.additional_salary: - ref_doctype, ref_docname = frappe.db.get_value( - "Additional Salary", - item.additional_salary, - ["ref_doctype", "ref_docname"], + def get_advance_deductions(self, component_type: str, salary_components: list[dict]) -> dict[str, dict]: + """Employee Advances being recovered, keyed by the Additional Salary that recovers them""" + additional_salaries = list({d.additional_salary for d in salary_components if d.additional_salary}) + if component_type != "deductions" or not additional_salaries: + return {} + + AdditionalSalary = frappe.qb.DocType("Additional Salary") + EmployeeAdvance = frappe.qb.DocType("Employee Advance") + advances = ( + frappe.qb.from_(AdditionalSalary) + .join(EmployeeAdvance) + .on(AdditionalSalary.ref_docname == EmployeeAdvance.name) + .select( + AdditionalSalary.name.as_("additional_salary"), + EmployeeAdvance.name.as_("employee_advance"), + EmployeeAdvance.advance_account, ) + .where(AdditionalSalary.name.isin(additional_salaries)) + .where(AdditionalSalary.ref_doctype == "Employee Advance") + ).run(as_dict=True) - if ref_doctype == "Employee Advance": - return ref_docname - return + return {d.additional_salary: d for d in advances} def add_advance_deduction_entry( self, item: dict, amount: float, cost_center: str, - employee_advance: str, + employee_advance: dict, ) -> None: self._advance_deduction_entries.append( { "employee": item.employee, - "account": self.get_salary_component_account(item.salary_component), + "account": employee_advance.advance_account, "amount": amount, "cost_center": cost_center, "reference_type": "Employee Advance", - "reference_name": employee_advance, + "reference_name": employee_advance.employee_advance, } ) diff --git a/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py b/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py index 74c6dc5f88e..b6dad8ec0dc 100644 --- a/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py +++ b/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py @@ -669,7 +669,7 @@ def test_advance_deduction_in_accrual_journal_entry(self): )[0] expected_entry = { - "account": "Employee Advances - _TC", + "account": advance.advance_account, "party": employee, "debit": 0.0, "credit": advance.paid_amount, From cb4d2dc1b84aa84dd3e38cf6fab3691e3e6513ac Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Wed, 23 Sep 2026 13:16:53 +0530 Subject: [PATCH 2/4] fix: reconcile outstanding employee advances in F&F on every fetch Outstanding advances were only added when the receivables table was empty, so the bootstrapped placeholder rows or a change of employee left them out of the settlement. Advances are now reconciled against the current employee on every fetch: placeholder rows and other employees' advances are dropped, advances already listed are kept, and the blank placeholder is only added when nothing is outstanding. --- .../full_and_final_statement.py | 27 ++++++++--- .../test_full_and_final_statement.py | 45 +++++++++++++++++++ 2 files changed, 65 insertions(+), 7 deletions(-) diff --git a/hrms/hr/doctype/full_and_final_statement/full_and_final_statement.py b/hrms/hr/doctype/full_and_final_statement/full_and_final_statement.py index ed402ac5431..76a3c685d7a 100644 --- a/hrms/hr/doctype/full_and_final_statement/full_and_final_statement.py +++ b/hrms/hr/doctype/full_and_final_statement/full_and_final_statement.py @@ -114,10 +114,8 @@ def get_outstanding_statements(self): self.add_withheld_salary_slips() components = self.get_payable_component() self.create_component_row(components, "payables") - if not self.receivables: - self.add_outstanding_employee_advances() - components = self.get_receivable_component() - self.create_component_row(components, "receivables") + self.add_outstanding_employee_advances() + self.create_component_row(self.get_receivable_component(), "receivables") self.get_assets_statements() def get_assets_statements(self): @@ -172,17 +170,32 @@ def add_withheld_salary_slips(self): ) def add_outstanding_employee_advances(self): + """Adds the employee's outstanding advances, dropping placeholder rows and other employees' advances""" advances = frappe.get_all( "Employee Advance", filters={"employee": self.employee, "docstatus": 1}, fields=["name", "advance_account", "paid_amount", "claimed_amount", "return_amount"], ) + outstanding_advances = { + advance.name: advance + for advance in advances + if flt(advance.paid_amount) - flt(advance.claimed_amount) - flt(advance.return_amount) > 0 + } + + self.receivables = [ + row + for row in self.receivables + if row.component != "Employee Advance" + or (row.reference_document in outstanding_advances) + or (not row.reference_document and not outstanding_advances) + ] + referenced_advances = {row.reference_document for row in self.receivables} - for advance in advances: - outstanding = flt(advance.paid_amount) - flt(advance.claimed_amount) - flt(advance.return_amount) - if outstanding <= 0: + for advance in outstanding_advances.values(): + if advance.name in referenced_advances: continue + outstanding = flt(advance.paid_amount) - flt(advance.claimed_amount) - flt(advance.return_amount) self.append( "receivables", { diff --git a/hrms/hr/doctype/full_and_final_statement/test_full_and_final_statement.py b/hrms/hr/doctype/full_and_final_statement/test_full_and_final_statement.py index a3205f0f31e..c4c68a97317 100644 --- a/hrms/hr/doctype/full_and_final_statement/test_full_and_final_statement.py +++ b/hrms/hr/doctype/full_and_final_statement/test_full_and_final_statement.py @@ -97,6 +97,51 @@ def test_employee_advance_settlement(self): self.assertEqual(advance.return_amount, advance.paid_amount) self.assertEqual(advance.status, "Returned") + def test_employee_advance_added_after_bootstrap(self): + from hrms.hr.doctype.employee_advance.test_employee_advance import ( + make_employee_advance, + make_payment_entry, + ) + + # placeholder rows already exist from bootstrap + self.assertTrue(any(row.component == "Employee Advance" for row in self.fnf.receivables)) + + advance = make_employee_advance(self.employee) + make_payment_entry(advance) + advance.reload() + + self.fnf.get_outstanding_statements() + advance_rows = [row for row in self.fnf.receivables if row.component == "Employee Advance"] + self.assertEqual([row.reference_document for row in advance_rows], [advance.name]) + self.assertEqual(advance_rows[0].amount, advance.paid_amount) + + # calling again neither duplicates the advance nor re-adds the placeholder + self.fnf.get_outstanding_statements() + advance_rows = [row for row in self.fnf.receivables if row.component == "Employee Advance"] + self.assertEqual([row.reference_document for row in advance_rows], [advance.name]) + + def test_employee_advance_rows_follow_employee_change(self): + from hrms.hr.doctype.employee_advance.test_employee_advance import ( + make_employee_advance, + make_payment_entry, + ) + + advance = make_employee_advance(self.employee) + make_payment_entry(advance) + self.fnf.get_outstanding_statements() + self.assertIn(advance.name, [row.reference_document for row in self.fnf.receivables]) + + other_employee = make_employee( + "test_fnf_other@example.com", company="_Test Company", relieving_date=add_days(today(), 30) + ) + self.fnf.employee = other_employee + self.fnf.relieving_date = add_days(today(), 30) + self.fnf.get_outstanding_statements() + + advance_rows = [row for row in self.fnf.receivables if row.component == "Employee Advance"] + self.assertEqual(len(advance_rows), 1) + self.assertFalse(advance_rows[0].reference_document) + def test_status_on_discard(self): self.fnf.discard() self.fnf.reload() From 7f3049eb4bc1093322e449a09401713108618598 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Wed, 23 Sep 2026 14:05:06 +0530 Subject: [PATCH 3/4] fix: refresh unsettled employee advance rows in F&F to the current balance An advance row kept its original amount when the statement was fetched again, so a claim or return made after the row was created left the settlement Journal Entry recovering a stale amount. Unsettled rows now take the advance's current account and outstanding balance on every fetch; rows already marked Settled are left as they are. --- .../full_and_final_statement.py | 33 +++++++++++---- .../test_full_and_final_statement.py | 42 +++++++++++++++++++ 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/hrms/hr/doctype/full_and_final_statement/full_and_final_statement.py b/hrms/hr/doctype/full_and_final_statement/full_and_final_statement.py index 76a3c685d7a..ffc5451c1d3 100644 --- a/hrms/hr/doctype/full_and_final_statement/full_and_final_statement.py +++ b/hrms/hr/doctype/full_and_final_statement/full_and_final_statement.py @@ -170,17 +170,23 @@ def add_withheld_salary_slips(self): ) def add_outstanding_employee_advances(self): - """Adds the employee's outstanding advances, dropping placeholder rows and other employees' advances""" + """ + Reconciles receivables with the employee's outstanding advances: placeholder rows and other + employees' advances are dropped, unsettled rows are refreshed to the current balance, and + missing advances are added. + """ advances = frappe.get_all( "Employee Advance", filters={"employee": self.employee, "docstatus": 1}, fields=["name", "advance_account", "paid_amount", "claimed_amount", "return_amount"], ) - outstanding_advances = { - advance.name: advance - for advance in advances - if flt(advance.paid_amount) - flt(advance.claimed_amount) - flt(advance.return_amount) > 0 - } + outstanding_advances = {} + for advance in advances: + advance.outstanding = ( + flt(advance.paid_amount) - flt(advance.claimed_amount) - flt(advance.return_amount) + ) + if advance.outstanding > 0: + outstanding_advances[advance.name] = advance self.receivables = [ row @@ -189,13 +195,22 @@ def add_outstanding_employee_advances(self): or (row.reference_document in outstanding_advances) or (not row.reference_document and not outstanding_advances) ] - referenced_advances = {row.reference_document for row in self.receivables} + + referenced_advances = set() + for row in self.receivables: + advance = outstanding_advances.get(row.reference_document) + if not advance: + continue + + referenced_advances.add(advance.name) + if row.status == "Unsettled": + row.account = advance.advance_account + row.amount = advance.outstanding for advance in outstanding_advances.values(): if advance.name in referenced_advances: continue - outstanding = flt(advance.paid_amount) - flt(advance.claimed_amount) - flt(advance.return_amount) self.append( "receivables", { @@ -204,7 +219,7 @@ def add_outstanding_employee_advances(self): "reference_document_type": "Employee Advance", "reference_document": advance.name, "account": advance.advance_account, - "amount": outstanding, + "amount": advance.outstanding, }, ) diff --git a/hrms/hr/doctype/full_and_final_statement/test_full_and_final_statement.py b/hrms/hr/doctype/full_and_final_statement/test_full_and_final_statement.py index c4c68a97317..5dd6f474873 100644 --- a/hrms/hr/doctype/full_and_final_statement/test_full_and_final_statement.py +++ b/hrms/hr/doctype/full_and_final_statement/test_full_and_final_statement.py @@ -120,6 +120,48 @@ def test_employee_advance_added_after_bootstrap(self): advance_rows = [row for row in self.fnf.receivables if row.component == "Employee Advance"] self.assertEqual([row.reference_document for row in advance_rows], [advance.name]) + def test_employee_advance_row_refreshed_to_current_balance(self): + from hrms.hr.doctype.employee_advance.employee_advance import make_return_entry + from hrms.hr.doctype.employee_advance.test_employee_advance import ( + make_employee_advance, + make_payment_entry, + ) + + advance = make_employee_advance(self.employee) + make_payment_entry(advance) + advance.reload() + self.fnf.get_outstanding_statements() + advance_row = next(row for row in self.fnf.receivables if row.reference_document == advance.name) + self.assertEqual(advance_row.amount, advance.paid_amount) + + # a partial return after the row was created reduces the outstanding balance + return_entry = frappe.get_doc( + make_return_entry( + employee=advance.employee, + company=advance.company, + employee_advance_name=advance.name, + return_amount=300, + advance_account=advance.advance_account, + mode_of_payment=advance.mode_of_payment, + currency=advance.currency, + ) + ) + return_entry.insert() + return_entry.submit() + advance.reload() + self.assertEqual(advance.return_amount, 300) + + self.fnf.get_outstanding_statements() + advance_row = next(row for row in self.fnf.receivables if row.reference_document == advance.name) + self.assertEqual(advance_row.amount, advance.paid_amount - 300) + + # a row the user has already settled is left as it is + advance_row.status = "Settled" + advance_row.amount = 100 + self.fnf.get_outstanding_statements() + advance_row = next(row for row in self.fnf.receivables if row.reference_document == advance.name) + self.assertEqual(advance_row.amount, 100) + def test_employee_advance_rows_follow_employee_change(self): from hrms.hr.doctype.employee_advance.test_employee_advance import ( make_employee_advance, From a21b2f5855fa73d114773d15730888a8ca674514 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Wed, 23 Sep 2026 14:28:32 +0530 Subject: [PATCH 4/4] fix: treat a blank F&F row status as unsettled when refreshing advances The child row status is optional, so a blank status skipped the refresh while the Journal Entry still posted the stale amount. Only rows marked Settled are now left untouched. --- .../full_and_final_statement/full_and_final_statement.py | 6 +++--- .../test_full_and_final_statement.py | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/hrms/hr/doctype/full_and_final_statement/full_and_final_statement.py b/hrms/hr/doctype/full_and_final_statement/full_and_final_statement.py index ffc5451c1d3..7617ed4982d 100644 --- a/hrms/hr/doctype/full_and_final_statement/full_and_final_statement.py +++ b/hrms/hr/doctype/full_and_final_statement/full_and_final_statement.py @@ -172,8 +172,8 @@ def add_withheld_salary_slips(self): def add_outstanding_employee_advances(self): """ Reconciles receivables with the employee's outstanding advances: placeholder rows and other - employees' advances are dropped, unsettled rows are refreshed to the current balance, and - missing advances are added. + employees' advances are dropped, rows not marked Settled are refreshed to the current balance, + and missing advances are added. """ advances = frappe.get_all( "Employee Advance", @@ -203,7 +203,7 @@ def add_outstanding_employee_advances(self): continue referenced_advances.add(advance.name) - if row.status == "Unsettled": + if row.status != "Settled": row.account = advance.advance_account row.amount = advance.outstanding diff --git a/hrms/hr/doctype/full_and_final_statement/test_full_and_final_statement.py b/hrms/hr/doctype/full_and_final_statement/test_full_and_final_statement.py index 5dd6f474873..f5306f5775c 100644 --- a/hrms/hr/doctype/full_and_final_statement/test_full_and_final_statement.py +++ b/hrms/hr/doctype/full_and_final_statement/test_full_and_final_statement.py @@ -155,6 +155,13 @@ def test_employee_advance_row_refreshed_to_current_balance(self): advance_row = next(row for row in self.fnf.receivables if row.reference_document == advance.name) self.assertEqual(advance_row.amount, advance.paid_amount - 300) + # a row with a blank status is refreshed like an unsettled one + advance_row.status = None + advance_row.amount = 100 + self.fnf.get_outstanding_statements() + advance_row = next(row for row in self.fnf.receivables if row.reference_document == advance.name) + self.assertEqual(advance_row.amount, advance.paid_amount - 300) + # a row the user has already settled is left as it is advance_row.status = "Settled" advance_row.amount = 100