diff --git a/hrms/payroll/doctype/payroll_entry/payroll_entry.py b/hrms/payroll/doctype/payroll_entry/payroll_entry.py index 04b88830cfa..3149a40108c 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,122 @@ 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, employee_wise_accounting_enabled) + + 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 = 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 + item_amount = flt(item.amount, precision) + employee_cost_centers = list( + self.get_payroll_cost_centers_for_employee(item.employee, item.salary_structure).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) + 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) + item_amount + + if not any(expense_entries.values()): + return + 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, employee), 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, + party=employee, + 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, + employee_wise_accounting_enabled=employee_wise_accounting_enabled, + title=_("Employer Contribution"), + ) + + 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", + filters={ + "parenttype": "Salary Component", + "parent": ["in", salary_components], + "company": self.company, + }, + fields=["parent", "account", "liability_account"], + ) + 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 component_accounts + def make_journal_entry( self, accounts, @@ -688,6 +807,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 +824,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..661953013a0 100644 --- a/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py +++ b/hrms/payroll/doctype/payroll_entry/test_payroll_entry.py @@ -1022,6 +1022,207 @@ 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 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" + + employer_pf = create_salary_component("Test Employer PF", **{"type": "Employer Contribution"}) + employer_pf.accounts = [] + employer_pf.append( + "accounts", + { + "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", + 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", + department=department, + ) + + 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") + 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}) + 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) + + @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_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 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