From 833b252db1f4edc631cf5756079399533e77b0ee Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Tue, 15 Sep 2026 16:21:45 +0530 Subject: [PATCH 1/4] feat: pass separate Journal Entry for employer contributions Salary Component's Accounts table gets a Liability Account field for Employer Contribution components: Account is debited as the expense (cost center wise) and Liability Account is credited, since employer contributions are payable to authorities and must not touch the Payroll Payable account. The JV is created after the salary accrual JV on payroll submission and carries a Payroll Entry reference on credit rows so cancelling the Payroll Entry cancels it as well. --- .../doctype/payroll_entry/payroll_entry.py | 96 ++++++++++++++++++- .../payroll_entry/test_payroll_entry.py | 71 ++++++++++++++ .../salary_component/salary_component.js | 10 ++ .../salary_component/salary_component.json | 4 +- .../salary_component_account.json | 16 +++- .../salary_component_account.py | 1 + 6 files changed, 192 insertions(+), 6 deletions(-) diff --git a/hrms/payroll/doctype/payroll_entry/payroll_entry.py b/hrms/payroll/doctype/payroll_entry/payroll_entry.py index 04b88830cfa..a6d676e94ea 100644 --- a/hrms/payroll/doctype/payroll_entry/payroll_entry.py +++ b/hrms/payroll/doctype/payroll_entry/payroll_entry.py @@ -623,6 +623,9 @@ def make_accrual_jv_entry(self, submitted_salary_slips): or {} ) + # fetched before the accrual JE gets linked to slips as get_sal_slip_list excludes linked slips + employer_contributions = self.get_salary_components("employer_contributions") or [] + precision = frappe.get_precision("Journal Entry Account", "debit_in_account_currency") if earnings or deductions: @@ -678,6 +681,96 @@ def make_accrual_jv_entry(self, submitted_salary_slips): employee_wise_accounting_enabled=employee_wise_accounting_enabled, ) + self.make_employer_contribution_jv_entry(employer_contributions) + + def make_employer_contribution_jv_entry(self, employer_contributions): + component_dict = {} + for item in employer_contributions: + employee_cost_centers = self.get_payroll_cost_centers_for_employee( + item.employee, item.salary_structure + ) + + for cost_center, percentage in employee_cost_centers.items(): + key = (item.salary_component, cost_center) + component_dict[key] = component_dict.get(key, 0) + flt(item.amount) * percentage / 100 + + if not component_dict: + return + + expense_entries = {} + liability_entries = {} + for (component, cost_center), amount in component_dict.items(): + expense_account, liability_account = self.get_employer_contribution_accounts(component) + + expense_key = (expense_account, cost_center) + expense_entries[expense_key] = expense_entries.get(expense_key, 0) + amount + liability_entries[liability_account] = liability_entries.get(liability_account, 0) + amount + + precision = frappe.get_precision("Journal Entry Account", "debit_in_account_currency") + accounting_dimensions = get_accounting_dimensions() or [] + company_currency = erpnext.get_company_currency(self.company) + accounts = [] + currencies = [] + + for (account, cost_center), amount in expense_entries.items(): + self.get_accounting_entries_and_payable_amount( + account, + cost_center or self.cost_center, + amount, + currencies, + company_currency, + 0, + accounting_dimensions, + precision, + entry_type="debit", + accounts=accounts, + ) + + for account, amount in liability_entries.items(): + self.get_accounting_entries_and_payable_amount( + account, + self.cost_center, + amount, + currencies, + company_currency, + 0, + accounting_dimensions, + precision, + entry_type="credit", + accounts=accounts, + reference_type=self.doctype, + reference_name=self.name, + ) + + self.make_journal_entry( + accounts, + currencies, + voucher_type="Journal Entry", + user_remark=_("Employer contribution accrual for salaries from {0} to {1}").format( + self.start_date, self.end_date + ), + submit_journal_entry=True, + title=_("Employer Contribution"), + ) + + def get_employer_contribution_accounts(self, salary_component): + accounts = frappe.db.get_value( + "Salary Component Account", + {"parent": salary_component, "company": self.company}, + ["account", "liability_account"], + as_dict=True, + cache=True, + ) + + if not accounts or not accounts.account or not accounts.liability_account: + frappe.throw( + _("Please set expense and liability accounts in Salary Component {0}").format( + get_link_to_form("Salary Component", salary_component) + ) + ) + + return accounts.account, accounts.liability_account + def make_journal_entry( self, accounts, @@ -688,6 +781,7 @@ def make_journal_entry( submitted_salary_slips: list | None = None, submit_journal_entry=False, employee_wise_accounting_enabled=False, + title=None, ) -> str: multi_currency = 0 if len(currencies) > 1: @@ -704,7 +798,7 @@ def make_journal_entry( journal_entry.multi_currency = multi_currency if voucher_type == "Journal Entry": - journal_entry.title = payroll_payable_account + journal_entry.title = title or payroll_payable_account journal_entry.save(ignore_permissions=True) diff --git a/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py b/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py index ad72cb7d778..929c228c41f 100644 --- a/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py +++ b/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py @@ -1022,6 +1022,77 @@ def test_component_exclusion_from_accounting_entries(self): self.assertIn(company.default_payroll_payable_account, accounts) self.assertNotIn("ESIC Payable - _TC", accounts, "ESIC component wrongly included in JE") + def test_employer_contribution_journal_entry(self): + company = frappe.get_doc("Company", "_Test Company") + employee = make_employee("employer_contribution_jv@payroll.com", company=company.name) + + create_account("Employer PF Contribution", company.name, "Indirect Expenses - _TC") + create_account("Employer PF Payable", company.name, "Current Liabilities - _TC") + expense_account = "Employer PF Contribution - _TC" + liability_account = "Employer PF Payable - _TC" + + employer_pf = create_salary_component("Test Employer PF", **{"type": "Employer Contribution"}) + employer_pf.accounts = [] + employer_pf.append( + "accounts", + {"company": company.name, "account": expense_account, "liability_account": liability_account}, + ) + employer_pf.save() + + make_salary_structure( + "Test Salary Structure Employer Contribution", + "Monthly", + employee, + company=company.name, + currency=company.default_currency, + other_details={ + "employer_contributions": [{"salary_component": employer_pf.name, "amount": 5000}] + }, + ) + + dates = get_start_end_dates("Monthly", nowdate()) + payroll_entry = make_payroll_entry( + start_date=dates.start_date, + end_date=dates.end_date, + payable_account=company.default_payroll_payable_account, + currency=company.default_currency, + company=company.name, + cost_center="Main - _TC", + ) + + employer_contribution_je = frappe.db.get_value( + "Journal Entry Account", + {"account": liability_account, "reference_name": payroll_entry.name, "docstatus": 1}, + "parent", + ) + self.assertTrue(employer_contribution_je, "Employer contribution Journal Entry not created") + + je_doc = frappe.get_doc("Journal Entry", employer_contribution_je) + self.assertEqual(je_doc.total_debit, 5000) + self.assertEqual(je_doc.total_credit, 5000) + + debit_row = next(d for d in je_doc.accounts if d.account == expense_account) + self.assertEqual(debit_row.debit, 5000) + + credit_row = next(d for d in je_doc.accounts if d.account == liability_account) + self.assertEqual(credit_row.credit, 5000) + self.assertEqual(credit_row.reference_type, "Payroll Entry") + + # employer contribution accounts should not leak into the salary accrual JV + salary_slip = frappe.get_doc("Salary Slip", {"payroll_entry": payroll_entry.name}) + self.assertNotEqual(salary_slip.journal_entry, employer_contribution_je) + + accrual_accounts = [ + d.account for d in frappe.get_doc("Journal Entry", salary_slip.journal_entry).accounts + ] + self.assertNotIn(expense_account, accrual_accounts) + self.assertNotIn(liability_account, accrual_accounts) + + # cancelling the payroll entry should cancel the employer contribution JV + payroll_entry.reload() + payroll_entry.cancel() + self.assertEqual(frappe.db.get_value("Journal Entry", employer_contribution_je, "docstatus"), 2) + def test_employee_benefits_accruals_in_salary_slip(self): """Test to verify - employee flexible benefits of accrual payout methods are fetched into salary slip diff --git a/hrms/payroll/doctype/salary_component/salary_component.js b/hrms/payroll/doctype/salary_component/salary_component.js index 9ee429a15f4..8c7a1babfa8 100644 --- a/hrms/payroll/doctype/salary_component/salary_component.js +++ b/hrms/payroll/doctype/salary_component/salary_component.js @@ -18,6 +18,16 @@ frappe.ui.form.on("Salary Component", { }, }; }); + frm.set_query("liability_account", "accounts", function (doc, cdt, cdn) { + var d = locals[cdt][cdn]; + return { + filters: { + is_group: 0, + company: d.company, + root_type: "Liability", + }, + }; + }); frm.set_query("earning_component_group", function () { return { filters: { diff --git a/hrms/payroll/doctype/salary_component/salary_component.json b/hrms/payroll/doctype/salary_component/salary_component.json index 3615952660f..6804933072a 100644 --- a/hrms/payroll/doctype/salary_component/salary_component.json +++ b/hrms/payroll/doctype/salary_component/salary_component.json @@ -154,7 +154,7 @@ "search_index": 1 }, { - "depends_on": "eval:doc.statistical_component != 1 && doc.type != \"Employer Contribution\"", + "depends_on": "eval:doc.statistical_component != 1", "fieldname": "section_break_5", "fieldtype": "Section Break", "label": "Accounts" @@ -297,7 +297,7 @@ "icon": "fa fa-flag", "index_web_pages_for_search": 1, "links": [], - "modified": "2026-08-06 13:01:43.187327", + "modified": "2026-09-15 12:00:00.000000", "modified_by": "Administrator", "module": "Payroll", "name": "Salary Component", diff --git a/hrms/payroll/doctype/salary_component_account/salary_component_account.json b/hrms/payroll/doctype/salary_component_account/salary_component_account.json index 605ea94c0cc..df270fa373a 100644 --- a/hrms/payroll/doctype/salary_component_account/salary_component_account.json +++ b/hrms/payroll/doctype/salary_component_account/salary_component_account.json @@ -6,7 +6,8 @@ "engine": "InnoDB", "field_order": [ "company", - "account" + "account", + "liability_account" ], "fields": [ { @@ -23,11 +24,20 @@ "in_list_view": 1, "label": "Account", "options": "Account" + }, + { + "depends_on": "eval:parent.type == \"Employer Contribution\"", + "description": "Liability account credited for employer contributions. Applicable only for Employer Contribution components, where Account is debited as the expense.", + "fieldname": "liability_account", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Liability Account", + "options": "Account" } ], "istable": 1, "links": [], - "modified": "2024-03-27 13:10:34.071210", + "modified": "2026-09-15 12:00:00.000000", "modified_by": "Administrator", "module": "Payroll", "name": "Salary Component Account", @@ -36,4 +46,4 @@ "sort_field": "creation", "sort_order": "DESC", "states": [] -} \ No newline at end of file +} diff --git a/hrms/payroll/doctype/salary_component_account/salary_component_account.py b/hrms/payroll/doctype/salary_component_account/salary_component_account.py index 101be328a8d..757700cf362 100644 --- a/hrms/payroll/doctype/salary_component_account/salary_component_account.py +++ b/hrms/payroll/doctype/salary_component_account/salary_component_account.py @@ -16,6 +16,7 @@ class SalaryComponentAccount(Document): account: DF.Link | None company: DF.Link | None + liability_account: DF.Link | None parent: DF.Data parentfield: DF.Data parenttype: DF.Data From ec2936ce4988878c2d3b0d61f92313bab7b541e8 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Tue, 15 Sep 2026 17:08:38 +0530 Subject: [PATCH 2/4] feat: honor employee-wise payroll accounting in employer contribution JV When 'Process Payroll Accounting Entry based on Employee' is enabled in Payroll Settings, the employer contribution JV credits the liability account employee-wise with the employee set as party, mirroring the payable rows of the salary accrual JV. Expense debits stay aggregated cost center wise. --- .../doctype/payroll_entry/payroll_entry.py | 39 ++++---- .../payroll_entry/test_payroll_entry.py | 91 +++++++++++++++++-- 2 files changed, 106 insertions(+), 24 deletions(-) diff --git a/hrms/payroll/doctype/payroll_entry/payroll_entry.py b/hrms/payroll/doctype/payroll_entry/payroll_entry.py index a6d676e94ea..7585a3ccc6f 100644 --- a/hrms/payroll/doctype/payroll_entry/payroll_entry.py +++ b/hrms/payroll/doctype/payroll_entry/payroll_entry.py @@ -681,30 +681,33 @@ def make_accrual_jv_entry(self, submitted_salary_slips): employee_wise_accounting_enabled=employee_wise_accounting_enabled, ) - self.make_employer_contribution_jv_entry(employer_contributions) + self.make_employer_contribution_jv_entry(employer_contributions, employee_wise_accounting_enabled) - def make_employer_contribution_jv_entry(self, employer_contributions): - component_dict = {} + def make_employer_contribution_jv_entry( + self, employer_contributions, employee_wise_accounting_enabled=False + ): + expense_entries = {} + liability_entries = {} for item in employer_contributions: + expense_account, liability_account = self.get_employer_contribution_accounts( + item.salary_component + ) + employee_cost_centers = self.get_payroll_cost_centers_for_employee( item.employee, item.salary_structure ) - for cost_center, percentage in employee_cost_centers.items(): - key = (item.salary_component, cost_center) - component_dict[key] = component_dict.get(key, 0) + flt(item.amount) * percentage / 100 - - if not component_dict: - return + expense_key = (expense_account, cost_center) + expense_entries[expense_key] = ( + expense_entries.get(expense_key, 0) + flt(item.amount) * percentage / 100 + ) - expense_entries = {} - liability_entries = {} - for (component, cost_center), amount in component_dict.items(): - expense_account, liability_account = self.get_employer_contribution_accounts(component) + # breaks up the liability employee-wise, mirroring the payable rows of the accrual JE + liability_key = (liability_account, item.employee if employee_wise_accounting_enabled else None) + liability_entries[liability_key] = liability_entries.get(liability_key, 0) + flt(item.amount) - expense_key = (expense_account, cost_center) - expense_entries[expense_key] = expense_entries.get(expense_key, 0) + amount - liability_entries[liability_account] = liability_entries.get(liability_account, 0) + amount + if not expense_entries: + return precision = frappe.get_precision("Journal Entry Account", "debit_in_account_currency") accounting_dimensions = get_accounting_dimensions() or [] @@ -726,7 +729,7 @@ def make_employer_contribution_jv_entry(self, employer_contributions): accounts=accounts, ) - for account, amount in liability_entries.items(): + for (account, employee), amount in liability_entries.items(): self.get_accounting_entries_and_payable_amount( account, self.cost_center, @@ -738,6 +741,7 @@ def make_employer_contribution_jv_entry(self, employer_contributions): precision, entry_type="credit", accounts=accounts, + party=employee, reference_type=self.doctype, reference_name=self.name, ) @@ -750,6 +754,7 @@ def make_employer_contribution_jv_entry(self, employer_contributions): self.start_date, self.end_date ), submit_journal_entry=True, + employee_wise_accounting_enabled=employee_wise_accounting_enabled, title=_("Employer Contribution"), ) diff --git a/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py b/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py index 929c228c41f..8bd6d061bdd 100644 --- a/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py +++ b/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py @@ -1022,12 +1022,9 @@ def test_component_exclusion_from_accounting_entries(self): self.assertIn(company.default_payroll_payable_account, accounts) self.assertNotIn("ESIC Payable - _TC", accounts, "ESIC component wrongly included in JE") - def test_employer_contribution_journal_entry(self): - company = frappe.get_doc("Company", "_Test Company") - employee = make_employee("employer_contribution_jv@payroll.com", company=company.name) - - create_account("Employer PF Contribution", company.name, "Indirect Expenses - _TC") - create_account("Employer PF Payable", company.name, "Current Liabilities - _TC") + def setup_employer_contribution_component(self): + create_account("Employer PF Contribution", "_Test Company", "Indirect Expenses - _TC") + create_account("Employer PF Payable", "_Test Company", "Current Liabilities - _TC") expense_account = "Employer PF Contribution - _TC" liability_account = "Employer PF Payable - _TC" @@ -1035,10 +1032,28 @@ def test_employer_contribution_journal_entry(self): employer_pf.accounts = [] employer_pf.append( "accounts", - {"company": company.name, "account": expense_account, "liability_account": liability_account}, + { + "company": "_Test Company", + "account": expense_account, + "liability_account": liability_account, + }, ) employer_pf.save() + return employer_pf, expense_account, liability_account + + @HRMSTestSuite.change_settings( + "Payroll Settings", {"process_payroll_accounting_entry_based_on_employee": 0} + ) + def test_employer_contribution_journal_entry(self): + company = frappe.get_doc("Company", "_Test Company") + department = create_department("EC JV Test") + employee = make_employee( + "employer_contribution_jv@payroll.com", company=company.name, department=department + ) + + employer_pf, expense_account, liability_account = self.setup_employer_contribution_component() + make_salary_structure( "Test Salary Structure Employer Contribution", "Monthly", @@ -1058,6 +1073,7 @@ def test_employer_contribution_journal_entry(self): currency=company.default_currency, company=company.name, cost_center="Main - _TC", + department=department, ) employer_contribution_je = frappe.db.get_value( @@ -1077,6 +1093,7 @@ def test_employer_contribution_journal_entry(self): credit_row = next(d for d in je_doc.accounts if d.account == liability_account) self.assertEqual(credit_row.credit, 5000) self.assertEqual(credit_row.reference_type, "Payroll Entry") + self.assertFalse(credit_row.party) # employer contribution accounts should not leak into the salary accrual JV salary_slip = frappe.get_doc("Salary Slip", {"payroll_entry": payroll_entry.name}) @@ -1093,6 +1110,66 @@ def test_employer_contribution_journal_entry(self): payroll_entry.cancel() self.assertEqual(frappe.db.get_value("Journal Entry", employer_contribution_je, "docstatus"), 2) + @HRMSTestSuite.change_settings( + "Payroll Settings", {"process_payroll_accounting_entry_based_on_employee": 1} + ) + def test_employer_contribution_journal_entry_with_employee_tagging(self): + company = frappe.get_doc("Company", "_Test Company") + department = create_department("EC Tagging Test") + emp1 = make_employee("ec_jv_tagging1@payroll.com", company=company.name, department=department) + emp2 = make_employee("ec_jv_tagging2@payroll.com", company=company.name, department=department) + + employer_pf, expense_account, liability_account = self.setup_employer_contribution_component() + + structure = make_salary_structure( + "Test Salary Structure EC Tagging", + "Monthly", + emp1, + company=company.name, + currency=company.default_currency, + other_details={ + "employer_contributions": [{"salary_component": employer_pf.name, "amount": 5000}] + }, + ) + create_salary_structure_assignment( + emp2, structure.name, company=company.name, currency=company.default_currency + ) + + dates = get_start_end_dates("Monthly", nowdate()) + payroll_entry = make_payroll_entry( + start_date=dates.start_date, + end_date=dates.end_date, + payable_account=company.default_payroll_payable_account, + currency=company.default_currency, + company=company.name, + cost_center="Main - _TC", + department=department, + ) + + credit_rows = frappe.get_all( + "Journal Entry Account", + filters={ + "account": liability_account, + "reference_name": payroll_entry.name, + "docstatus": 1, + }, + fields=["parent", "party_type", "party", "credit_in_account_currency"], + ) + + self.assertEqual(len(credit_rows), 2) + self.assertEqual({row.party for row in credit_rows}, {emp1, emp2}) + for row in credit_rows: + self.assertEqual(row.party_type, "Employee") + self.assertEqual(row.credit_in_account_currency, 5000) + + je_doc = frappe.get_doc("Journal Entry", credit_rows[0].parent) + self.assertEqual(je_doc.total_debit, 10000) + self.assertEqual(je_doc.total_credit, 10000) + + debit_row = next(d for d in je_doc.accounts if d.account == expense_account) + self.assertEqual(debit_row.debit, 10000) + self.assertFalse(debit_row.party) + def test_employee_benefits_accruals_in_salary_slip(self): """Test to verify - employee flexible benefits of accrual payout methods are fetched into salary slip From 5350ccbc524a45b138fd67ec6dc56d1cfa9f6f24 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Wed, 16 Sep 2026 12:02:06 +0530 Subject: [PATCH 3/4] fix: preserve contribution total across cost center splits in employer contribution JV Expense rows split by cost center percentage and the liability credit were rounded independently, so amounts like 0.01 split 50/50 produced unbalanced Journal Entries that failed submission. The last cost center now takes the rounding remainder so the expense splits always sum up to the amount credited against the liability. --- .../doctype/payroll_entry/payroll_entry.py | 29 ++++++---- .../payroll_entry/test_payroll_entry.py | 53 +++++++++++++++++++ 2 files changed, 72 insertions(+), 10 deletions(-) diff --git a/hrms/payroll/doctype/payroll_entry/payroll_entry.py b/hrms/payroll/doctype/payroll_entry/payroll_entry.py index 7585a3ccc6f..3b9e2d5eb19 100644 --- a/hrms/payroll/doctype/payroll_entry/payroll_entry.py +++ b/hrms/payroll/doctype/payroll_entry/payroll_entry.py @@ -686,6 +686,7 @@ def make_accrual_jv_entry(self, submitted_salary_slips): def make_employer_contribution_jv_entry( self, employer_contributions, employee_wise_accounting_enabled=False ): + precision = frappe.get_precision("Journal Entry Account", "debit_in_account_currency") expense_entries = {} liability_entries = {} for item in employer_contributions: @@ -693,23 +694,31 @@ def make_employer_contribution_jv_entry( item.salary_component ) - employee_cost_centers = self.get_payroll_cost_centers_for_employee( - item.employee, item.salary_structure + # the last cost center takes the rounding remainder so that the expense + # splits always sum up to the amount credited against the liability + item_amount = flt(item.amount, precision) + employee_cost_centers = list( + self.get_payroll_cost_centers_for_employee(item.employee, item.salary_structure).items() ) - for cost_center, percentage in employee_cost_centers.items(): + allocated = 0 + for cost_center, percentage in employee_cost_centers[:-1]: + split = flt(item_amount * percentage / 100, precision) + allocated += split expense_key = (expense_account, cost_center) - expense_entries[expense_key] = ( - expense_entries.get(expense_key, 0) + flt(item.amount) * percentage / 100 - ) + expense_entries[expense_key] = expense_entries.get(expense_key, 0) + split + + last_cost_center = employee_cost_centers[-1][0] + expense_key = (expense_account, last_cost_center) + expense_entries[expense_key] = expense_entries.get(expense_key, 0) + flt( + item_amount - allocated, precision + ) # breaks up the liability employee-wise, mirroring the payable rows of the accrual JE liability_key = (liability_account, item.employee if employee_wise_accounting_enabled else None) - liability_entries[liability_key] = liability_entries.get(liability_key, 0) + flt(item.amount) + liability_entries[liability_key] = liability_entries.get(liability_key, 0) + item_amount - if not expense_entries: + if not any(expense_entries.values()): return - - precision = frappe.get_precision("Journal Entry Account", "debit_in_account_currency") accounting_dimensions = get_accounting_dimensions() or [] company_currency = erpnext.get_company_currency(self.company) accounts = [] diff --git a/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py b/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py index 8bd6d061bdd..661953013a0 100644 --- a/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py +++ b/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py @@ -1170,6 +1170,59 @@ def test_employer_contribution_journal_entry_with_employee_tagging(self): self.assertEqual(debit_row.debit, 10000) self.assertFalse(debit_row.party) + def test_employer_contribution_jv_balances_with_cost_center_split_rounding(self): + company = frappe.get_doc("Company", "_Test Company") + department = create_department("EC Rounding Test") + employee = make_employee("ec_jv_rounding@payroll.com", company=company.name, department=department) + + employer_pf, expense_account, liability_account = self.setup_employer_contribution_component() + + structure = make_salary_structure( + "Test Salary Structure EC Rounding", + "Monthly", + employee, + company=company.name, + currency=company.default_currency, + other_details={ + "employer_contributions": [{"salary_component": employer_pf.name, "amount": 0.01}] + }, + ) + + ssa = frappe.db.get_value( + "Salary Structure Assignment", + {"employee": employee, "salary_structure": structure.name, "docstatus": 1}, + "name", + ) + ssa_doc = frappe.get_doc("Salary Structure Assignment", ssa) + ssa_doc.payroll_cost_centers = [] + ssa_doc.append("payroll_cost_centers", {"cost_center": "_Test Cost Center - _TC", "percentage": 50}) + ssa_doc.append("payroll_cost_centers", {"cost_center": "_Test Cost Center 2 - _TC", "percentage": 50}) + ssa_doc.save() + + dates = get_start_end_dates("Monthly", nowdate()) + payroll_entry = make_payroll_entry( + start_date=dates.start_date, + end_date=dates.end_date, + payable_account=company.default_payroll_payable_account, + currency=company.default_currency, + company=company.name, + cost_center="Main - _TC", + department=department, + ) + + # 0.005 + 0.005 rounded per row would not equal the 0.01 credit, + # making the JE unbalanced and failing submission + employer_contribution_je = frappe.db.get_value( + "Journal Entry Account", + {"account": liability_account, "reference_name": payroll_entry.name, "docstatus": 1}, + "parent", + ) + self.assertTrue(employer_contribution_je, "Employer contribution Journal Entry not created") + + je_doc = frappe.get_doc("Journal Entry", employer_contribution_je) + self.assertEqual(flt(je_doc.total_debit, 2), 0.01) + self.assertEqual(flt(je_doc.total_credit, 2), 0.01) + def test_employee_benefits_accruals_in_salary_slip(self): """Test to verify - employee flexible benefits of accrual payout methods are fetched into salary slip From 2b855ff54af0d8c6f352d1e2405b6c27bd6302e9 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Wed, 16 Sep 2026 12:06:24 +0530 Subject: [PATCH 4/4] refactor: fetch employer contribution accounts in a single query Account lookups ran once per contribution row inside the loop; fetch the mappings for the distinct components upfront instead so payroll submission does not scale queries with salary slip rows. --- .../doctype/payroll_entry/payroll_entry.py | 44 ++++++++++++------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/hrms/payroll/doctype/payroll_entry/payroll_entry.py b/hrms/payroll/doctype/payroll_entry/payroll_entry.py index 3b9e2d5eb19..3149a40108c 100644 --- a/hrms/payroll/doctype/payroll_entry/payroll_entry.py +++ b/hrms/payroll/doctype/payroll_entry/payroll_entry.py @@ -686,13 +686,18 @@ def make_accrual_jv_entry(self, submitted_salary_slips): def make_employer_contribution_jv_entry( self, employer_contributions, employee_wise_accounting_enabled=False ): + if not employer_contributions: + return + + component_accounts = self.get_employer_contribution_accounts( + {item.salary_component for item in employer_contributions} + ) + precision = frappe.get_precision("Journal Entry Account", "debit_in_account_currency") expense_entries = {} liability_entries = {} for item in employer_contributions: - expense_account, liability_account = self.get_employer_contribution_accounts( - item.salary_component - ) + expense_account, liability_account = component_accounts[item.salary_component] # the last cost center takes the rounding remainder so that the expense # splits always sum up to the amount credited against the liability @@ -767,23 +772,30 @@ def make_employer_contribution_jv_entry( title=_("Employer Contribution"), ) - def get_employer_contribution_accounts(self, salary_component): - accounts = frappe.db.get_value( + def get_employer_contribution_accounts(self, salary_components): + """Returns {salary_component: (expense_account, liability_account)} in a single query""" + salary_components = list(salary_components) + account_details = frappe.get_all( "Salary Component Account", - {"parent": salary_component, "company": self.company}, - ["account", "liability_account"], - as_dict=True, - cache=True, + filters={ + "parenttype": "Salary Component", + "parent": ["in", salary_components], + "company": self.company, + }, + fields=["parent", "account", "liability_account"], ) - - if not accounts or not accounts.account or not accounts.liability_account: - frappe.throw( - _("Please set expense and liability accounts in Salary Component {0}").format( - get_link_to_form("Salary Component", salary_component) + component_accounts = {d.parent: (d.account, d.liability_account) for d in account_details} + + for salary_component in salary_components: + accounts = component_accounts.get(salary_component) + if not accounts or not all(accounts): + frappe.throw( + _("Please set expense and liability accounts in Salary Component {0}").format( + get_link_to_form("Salary Component", salary_component) + ) ) - ) - return accounts.account, accounts.liability_account + return component_accounts def make_journal_entry( self,