From ff8f2cf8f2184407d4c4476905bf535a40a66c97 Mon Sep 17 00:00:00 2001 From: Asmita Hase Date: Fri, 18 Sep 2026 13:50:42 +0530 Subject: [PATCH 1/5] fix: group salary register components by parentfield --- .../report/salary_register/salary_register.js | 6 ++ .../report/salary_register/salary_register.py | 102 ++++++++++++------ 2 files changed, 77 insertions(+), 31 deletions(-) diff --git a/hrms/payroll/report/salary_register/salary_register.js b/hrms/payroll/report/salary_register/salary_register.js index 4458c91026f..a0d5c1d9301 100644 --- a/hrms/payroll/report/salary_register/salary_register.js +++ b/hrms/payroll/report/salary_register/salary_register.js @@ -90,5 +90,11 @@ frappe.query_reports["Salary Register"] = { options: "Branch", width: "100px", }, + { + fieldname: "show_employer_contributions", + label: __("Show Employer Contributions"), + fieldtype: "Check", + default: 0, + }, ], }; diff --git a/hrms/payroll/report/salary_register/salary_register.py b/hrms/payroll/report/salary_register/salary_register.py index e8c41f26eb5..8d35a01430a 100644 --- a/hrms/payroll/report/salary_register/salary_register.py +++ b/hrms/payroll/report/salary_register/salary_register.py @@ -10,7 +10,8 @@ salary_slip = frappe.qb.DocType("Salary Slip") salary_detail = frappe.qb.DocType("Salary Detail") -salary_component = frappe.qb.DocType("Salary Component") + +COMPONENT_TYPES = ("earnings", "deductions", "employer_contributions") def execute(filters=None): @@ -26,11 +27,14 @@ def execute(filters=None): if not salary_slips: return [], [] - earning_types, ded_types = get_earning_and_deduction_types(salary_slips) - columns = get_columns(earning_types, ded_types) + component_types = get_active_component_types(filters) + components = get_components_by_type(salary_slips, component_types) + columns = get_columns(components) - ss_earning_map = get_salary_slip_details(salary_slips, currency, company_currency, "earnings") - ss_ded_map = get_salary_slip_details(salary_slips, currency, company_currency, "deductions") + component_maps = { + component_type: get_salary_slip_details(salary_slips, currency, company_currency, component_type) + for component_type in component_types + } doj_map = get_employee_doj_map() @@ -56,11 +60,19 @@ def execute(filters=None): update_column_width(ss, columns) - for e in earning_types: - row.update({frappe.scrub(e): ss_earning_map.get(ss.name, {}).get(e)}) + for component_type in component_types: + amounts = component_maps[component_type].get(ss.name, {}) + for component in components[component_type]: + row.update({frappe.scrub(component): amounts.get(component)}) - for d in ded_types: - row.update({frappe.scrub(d): ss_ded_map.get(ss.name, {}).get(d)}) + if components["employer_contributions"]: + row.update( + { + "total_employer_contribution": sum( + component_maps["employer_contributions"].get(ss.name, {}).values() + ) + } + ) if currency == company_currency: row.update( @@ -86,14 +98,21 @@ def execute(filters=None): return columns, data -def get_earning_and_deduction_types(salary_slips): - salary_component_and_type = {_("Earning"): [], _("Deduction"): []} +def get_active_component_types(filters): + if filters.get("show_employer_contributions"): + return COMPONENT_TYPES - for salary_component in get_salary_components(salary_slips): - component_type = get_salary_component_type(salary_component) - salary_component_and_type[_(component_type)].append(salary_component) + return ("earnings", "deductions") - return sorted(salary_component_and_type[_("Earning")]), sorted(salary_component_and_type[_("Deduction")]) + +def get_components_by_type(salary_slips, component_types): + components = {component_type: set() for component_type in COMPONENT_TYPES} + + for row in get_salary_components(salary_slips): + if row.parentfield in component_types: + components[row.parentfield].add(row.salary_component) + + return {component_type: sorted(names) for component_type, names in components.items()} def update_column_width(ss, columns): @@ -107,7 +126,7 @@ def update_column_width(ss, columns): columns[9].update({"width": 120}) -def get_columns(earning_types, ded_types): +def get_columns(components): columns = [ { "label": _("Salary Slip ID"), @@ -195,7 +214,7 @@ def get_columns(earning_types, ded_types): }, ] - for earning in earning_types: + for earning in components["earnings"]: columns.append( { "label": earning, @@ -216,7 +235,7 @@ def get_columns(earning_types, ded_types): } ) - for deduction in ded_types: + for deduction in components["deductions"]: columns.append( { "label": deduction, @@ -254,15 +273,40 @@ def get_columns(earning_types, ded_types): "options": "currency", "width": 120, }, - { - "label": _("Currency"), - "fieldtype": "Data", - "fieldname": "currency", - "options": "Currency", - "hidden": 1, - }, ] ) + + for contribution in components["employer_contributions"]: + columns.append( + { + "label": contribution, + "fieldname": frappe.scrub(contribution), + "fieldtype": "Currency", + "options": "currency", + "width": 120, + } + ) + + if components["employer_contributions"]: + columns.append( + { + "label": _("Total Employer Contribution"), + "fieldname": "total_employer_contribution", + "fieldtype": "Currency", + "options": "currency", + "width": 120, + } + ) + + columns.append( + { + "label": _("Currency"), + "fieldtype": "Data", + "fieldname": "currency", + "options": "Currency", + "hidden": 1, + } + ) return columns @@ -270,13 +314,9 @@ def get_salary_components(salary_slips): return ( frappe.qb.from_(salary_detail) .where((salary_detail.amount != 0) & (salary_detail.parent.isin([d.name for d in salary_slips]))) - .select(salary_detail.salary_component) + .select(salary_detail.parentfield, salary_detail.salary_component) .distinct() - ).run(pluck=True) - - -def get_salary_component_type(salary_component): - return frappe.db.get_value("Salary Component", salary_component, "type", cache=True) + ).run(as_dict=True) def get_salary_slips(filters, company_currency): From d05e2237072fdfb8c951406a0037cbd2dafc132b Mon Sep 17 00:00:00 2001 From: Asmita Hase Date: Fri, 18 Sep 2026 14:24:53 +0530 Subject: [PATCH 2/5] fix: reuse component parentfields and cover salary register with tests --- .../report/salary_register/salary_register.py | 44 +++--- .../salary_register/test_salary_register.py | 143 ++++++++++++++++++ 2 files changed, 167 insertions(+), 20 deletions(-) create mode 100644 hrms/payroll/report/salary_register/test_salary_register.py diff --git a/hrms/payroll/report/salary_register/salary_register.py b/hrms/payroll/report/salary_register/salary_register.py index 8d35a01430a..2022d3b4ba8 100644 --- a/hrms/payroll/report/salary_register/salary_register.py +++ b/hrms/payroll/report/salary_register/salary_register.py @@ -8,11 +8,11 @@ import erpnext +from hrms.payroll.utils import COMPONENT_PARENTFIELDS + salary_slip = frappe.qb.DocType("Salary Slip") salary_detail = frappe.qb.DocType("Salary Detail") -COMPONENT_TYPES = ("earnings", "deductions", "employer_contributions") - def execute(filters=None): if not filters: @@ -27,13 +27,13 @@ def execute(filters=None): if not salary_slips: return [], [] - component_types = get_active_component_types(filters) - components = get_components_by_type(salary_slips, component_types) + parentfields = get_active_parentfields(filters) + components = get_components_by_parentfield(salary_slips, parentfields) columns = get_columns(components) component_maps = { - component_type: get_salary_slip_details(salary_slips, currency, company_currency, component_type) - for component_type in component_types + parentfield: get_salary_slip_details(salary_slips, currency, company_currency, parentfield) + for parentfield in parentfields } doj_map = get_employee_doj_map() @@ -60,10 +60,10 @@ def execute(filters=None): update_column_width(ss, columns) - for component_type in component_types: - amounts = component_maps[component_type].get(ss.name, {}) - for component in components[component_type]: - row.update({frappe.scrub(component): amounts.get(component)}) + for parentfield in parentfields: + amounts = component_maps[parentfield].get(ss.name, {}) + for component, amount in amounts.items(): + row.setdefault(frappe.scrub(component), amount) if components["employer_contributions"]: row.update( @@ -98,21 +98,25 @@ def execute(filters=None): return columns, data -def get_active_component_types(filters): +def get_active_parentfields(filters): if filters.get("show_employer_contributions"): - return COMPONENT_TYPES + return COMPONENT_PARENTFIELDS return ("earnings", "deductions") -def get_components_by_type(salary_slips, component_types): - components = {component_type: set() for component_type in COMPONENT_TYPES} +def get_components_by_parentfield(salary_slips, parentfields): + rows = get_salary_components(salary_slips) + components = {parentfield: set() for parentfield in COMPONENT_PARENTFIELDS} + seen = set() - for row in get_salary_components(salary_slips): - if row.parentfield in component_types: - components[row.parentfield].add(row.salary_component) + for parentfield in parentfields: + for row in rows: + if row.parentfield == parentfield and row.salary_component not in seen: + components[parentfield].add(row.salary_component) + seen.add(row.salary_component) - return {component_type: sorted(names) for component_type, names in components.items()} + return {parentfield: sorted(names) for parentfield, names in components.items()} def update_column_width(ss, columns): @@ -364,14 +368,14 @@ def get_employee_doj_map(): return frappe._dict(result) -def get_salary_slip_details(salary_slips, currency, company_currency, component_type): +def get_salary_slip_details(salary_slips, currency, company_currency, parentfield): salary_slips = [ss.name for ss in salary_slips] result = ( frappe.qb.from_(salary_slip) .join(salary_detail) .on(salary_slip.name == salary_detail.parent) - .where((salary_detail.parent.isin(salary_slips)) & (salary_detail.parentfield == component_type)) + .where((salary_detail.parent.isin(salary_slips)) & (salary_detail.parentfield == parentfield)) .select( salary_detail.parent, salary_detail.salary_component, diff --git a/hrms/payroll/report/salary_register/test_salary_register.py b/hrms/payroll/report/salary_register/test_salary_register.py new file mode 100644 index 00000000000..7d535e053a0 --- /dev/null +++ b/hrms/payroll/report/salary_register/test_salary_register.py @@ -0,0 +1,143 @@ +# Copyright (c) 2026, Frappe Technologies Pvt. Ltd. and Contributors +# License: GNU General Public License v3. See license.txt + +import frappe +from frappe.utils import add_months, get_first_day, get_last_day, getdate + +from erpnext.setup.doctype.employee.test_employee import make_employee + +from hrms.payroll.doctype.salary_structure.salary_structure import make_salary_slip +from hrms.payroll.doctype.salary_structure.test_salary_structure import ( + create_salary_structure_assignment, + make_salary_structure, +) +from hrms.payroll.report.salary_register.salary_register import execute +from hrms.tests.utils import HRMSTestSuite + +EMPLOYER_PF = "_Test Register Employer PF" +SHARED_PF = "_Test Register Shared PF" + + +class TestSalaryRegister(HRMSTestSuite): + def setUp(self): + self.employee = make_employee("test_salary_register@example.com", company="_Test Company") + self.start_date = get_first_day(add_months(getdate(), -1)) + self.end_date = get_last_day(self.start_date) + + def create_component(self, component, abbr, component_type): + if frappe.db.exists("Salary Component", component): + frappe.delete_doc("Salary Component", component, force=True) + + frappe.get_doc( + { + "doctype": "Salary Component", + "salary_component": component, + "salary_component_abbr": abbr, + "type": component_type, + } + ).insert() + + def create_salary_slip(self, salary_structure, posting_date): + salary_slip = make_salary_slip(salary_structure, employee=self.employee, posting_date=posting_date) + salary_slip.insert() + salary_slip.submit() + + return salary_slip + + def get_report(self, **filters): + return execute( + frappe._dict( + { + "company": "_Test Company", + "employee": self.employee, + "docstatus": "Submitted", + **filters, + } + ) + ) + + def create_structure_with_employer_contribution(self): + self.create_component(EMPLOYER_PF, "TREPF", "Employer Contribution") + + return make_salary_structure( + "_Test Salary Register Structure", + "Monthly", + employee=self.employee, + company="_Test Company", + currency="INR", + from_date=self.start_date, + other_details={ + "employer_contributions": [{"salary_component": EMPLOYER_PF, "abbr": "TREPF", "amount": 1800}] + }, + ) + + def test_employer_contributions_hidden_by_default(self): + salary_structure = self.create_structure_with_employer_contribution() + self.create_salary_slip(salary_structure.name, self.end_date) + + columns, data = self.get_report() + + fieldnames = [column["fieldname"] for column in columns] + self.assertNotIn(frappe.scrub(EMPLOYER_PF), fieldnames) + self.assertNotIn("total_employer_contribution", fieldnames) + self.assertEqual(len(data), 1) + + def test_employer_contributions_shown_with_filter(self): + salary_structure = self.create_structure_with_employer_contribution() + salary_slip = self.create_salary_slip(salary_structure.name, self.end_date) + + columns, data = self.get_report(show_employer_contributions=1) + + fieldnames = [column["fieldname"] for column in columns] + self.assertIn(frappe.scrub(EMPLOYER_PF), fieldnames) + self.assertIn("total_employer_contribution", fieldnames) + + self.assertEqual(data[0][frappe.scrub(EMPLOYER_PF)], 1800) + self.assertEqual(data[0]["total_employer_contribution"], 1800) + self.assertEqual(data[0]["net_pay"], salary_slip.net_pay) + + def test_component_shared_across_parentfields(self): + self.create_component(SHARED_PF, "TRSPF", "Deduction") + + deduction_structure = make_salary_structure( + "_Test Salary Register Deduction Structure", + "Monthly", + employee=self.employee, + company="_Test Company", + currency="INR", + from_date=self.start_date, + deductions=[{"salary_component": SHARED_PF, "abbr": "TRSPF", "amount": 1800}], + ) + deduction_slip = self.create_salary_slip(deduction_structure.name, self.end_date) + + frappe.db.set_value("Salary Component", SHARED_PF, "type", "Employer Contribution") + + next_start_date = add_months(self.start_date, 1) + contribution_structure = make_salary_structure( + "_Test Salary Register Contribution Structure", + "Monthly", + company="_Test Company", + currency="INR", + other_details={ + "employer_contributions": [{"salary_component": SHARED_PF, "abbr": "TRSPF", "amount": 2400}] + }, + ) + create_salary_structure_assignment( + self.employee, + contribution_structure.name, + from_date=next_start_date, + currency="INR", + allow_duplicate=True, + ) + contribution_slip = self.create_salary_slip( + contribution_structure.name, get_last_day(next_start_date) + ) + + columns, data = self.get_report(show_employer_contributions=1) + + fieldname = frappe.scrub(SHARED_PF) + self.assertEqual([column["fieldname"] for column in columns].count(fieldname), 1) + + amounts = {row["salary_slip_id"]: row.get(fieldname) for row in data} + self.assertEqual(amounts[deduction_slip.name], 1800) + self.assertEqual(amounts[contribution_slip.name], 2400) From 45d991c713e2d7337dda96923bd54cf3bde37ecb Mon Sep 17 00:00:00 2001 From: Asmita Hase Date: Fri, 18 Sep 2026 14:47:12 +0530 Subject: [PATCH 3/5] fix: namespace employer contribution fieldnames instead of dropping them --- .../report/salary_register/salary_register.py | 38 +++++++++++++------ .../salary_register/test_salary_register.py | 25 +++++++----- 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/hrms/payroll/report/salary_register/salary_register.py b/hrms/payroll/report/salary_register/salary_register.py index 2022d3b4ba8..f388800f06b 100644 --- a/hrms/payroll/report/salary_register/salary_register.py +++ b/hrms/payroll/report/salary_register/salary_register.py @@ -13,6 +13,8 @@ salary_slip = frappe.qb.DocType("Salary Slip") salary_detail = frappe.qb.DocType("Salary Detail") +EMPLOYER_CONTRIBUTIONS = "employer_contributions" + def execute(filters=None): if not filters: @@ -63,13 +65,13 @@ def execute(filters=None): for parentfield in parentfields: amounts = component_maps[parentfield].get(ss.name, {}) for component, amount in amounts.items(): - row.setdefault(frappe.scrub(component), amount) + row.setdefault(get_component_fieldname(parentfield, component), amount) - if components["employer_contributions"]: + if components[EMPLOYER_CONTRIBUTIONS]: row.update( { "total_employer_contribution": sum( - component_maps["employer_contributions"].get(ss.name, {}).values() + component_maps[EMPLOYER_CONTRIBUTIONS].get(ss.name, {}).values() ) } ) @@ -102,19 +104,31 @@ def get_active_parentfields(filters): if filters.get("show_employer_contributions"): return COMPONENT_PARENTFIELDS - return ("earnings", "deductions") + return tuple(p for p in COMPONENT_PARENTFIELDS if p != EMPLOYER_CONTRIBUTIONS) + + +def get_component_fieldname(parentfield, component): + if parentfield == EMPLOYER_CONTRIBUTIONS: + return f"employer_contribution_{frappe.scrub(component)}" + + return frappe.scrub(component) def get_components_by_parentfield(salary_slips, parentfields): rows = get_salary_components(salary_slips) components = {parentfield: set() for parentfield in COMPONENT_PARENTFIELDS} - seen = set() + pay_components = set() for parentfield in parentfields: for row in rows: - if row.parentfield == parentfield and row.salary_component not in seen: + if row.parentfield != parentfield: + continue + + if parentfield == EMPLOYER_CONTRIBUTIONS: + components[parentfield].add(row.salary_component) + elif row.salary_component not in pay_components: components[parentfield].add(row.salary_component) - seen.add(row.salary_component) + pay_components.add(row.salary_component) return {parentfield: sorted(names) for parentfield, names in components.items()} @@ -222,7 +236,7 @@ def get_columns(components): columns.append( { "label": earning, - "fieldname": frappe.scrub(earning), + "fieldname": get_component_fieldname("earnings", earning), "fieldtype": "Currency", "options": "currency", "width": 120, @@ -243,7 +257,7 @@ def get_columns(components): columns.append( { "label": deduction, - "fieldname": frappe.scrub(deduction), + "fieldname": get_component_fieldname("deductions", deduction), "fieldtype": "Currency", "options": "currency", "width": 120, @@ -280,18 +294,18 @@ def get_columns(components): ] ) - for contribution in components["employer_contributions"]: + for contribution in components[EMPLOYER_CONTRIBUTIONS]: columns.append( { "label": contribution, - "fieldname": frappe.scrub(contribution), + "fieldname": get_component_fieldname(EMPLOYER_CONTRIBUTIONS, contribution), "fieldtype": "Currency", "options": "currency", "width": 120, } ) - if components["employer_contributions"]: + if components[EMPLOYER_CONTRIBUTIONS]: columns.append( { "label": _("Total Employer Contribution"), diff --git a/hrms/payroll/report/salary_register/test_salary_register.py b/hrms/payroll/report/salary_register/test_salary_register.py index 7d535e053a0..3d3f28bf79c 100644 --- a/hrms/payroll/report/salary_register/test_salary_register.py +++ b/hrms/payroll/report/salary_register/test_salary_register.py @@ -11,7 +11,7 @@ create_salary_structure_assignment, make_salary_structure, ) -from hrms.payroll.report.salary_register.salary_register import execute +from hrms.payroll.report.salary_register.salary_register import execute, get_component_fieldname from hrms.tests.utils import HRMSTestSuite EMPLOYER_PF = "_Test Register Employer PF" @@ -78,7 +78,7 @@ def test_employer_contributions_hidden_by_default(self): columns, data = self.get_report() fieldnames = [column["fieldname"] for column in columns] - self.assertNotIn(frappe.scrub(EMPLOYER_PF), fieldnames) + self.assertNotIn(get_component_fieldname("employer_contributions", EMPLOYER_PF), fieldnames) self.assertNotIn("total_employer_contribution", fieldnames) self.assertEqual(len(data), 1) @@ -88,11 +88,12 @@ def test_employer_contributions_shown_with_filter(self): columns, data = self.get_report(show_employer_contributions=1) + fieldname = get_component_fieldname("employer_contributions", EMPLOYER_PF) fieldnames = [column["fieldname"] for column in columns] - self.assertIn(frappe.scrub(EMPLOYER_PF), fieldnames) + self.assertIn(fieldname, fieldnames) self.assertIn("total_employer_contribution", fieldnames) - self.assertEqual(data[0][frappe.scrub(EMPLOYER_PF)], 1800) + self.assertEqual(data[0][fieldname], 1800) self.assertEqual(data[0]["total_employer_contribution"], 1800) self.assertEqual(data[0]["net_pay"], salary_slip.net_pay) @@ -135,9 +136,15 @@ def test_component_shared_across_parentfields(self): columns, data = self.get_report(show_employer_contributions=1) - fieldname = frappe.scrub(SHARED_PF) - self.assertEqual([column["fieldname"] for column in columns].count(fieldname), 1) + deduction_fieldname = get_component_fieldname("deductions", SHARED_PF) + contribution_fieldname = get_component_fieldname("employer_contributions", SHARED_PF) + fieldnames = [column["fieldname"] for column in columns] + + self.assertEqual(fieldnames.count(deduction_fieldname), 1) + self.assertEqual(fieldnames.count(contribution_fieldname), 1) + self.assertIn("total_employer_contribution", fieldnames) - amounts = {row["salary_slip_id"]: row.get(fieldname) for row in data} - self.assertEqual(amounts[deduction_slip.name], 1800) - self.assertEqual(amounts[contribution_slip.name], 2400) + rows = {row["salary_slip_id"]: row for row in data} + self.assertEqual(rows[deduction_slip.name][deduction_fieldname], 1800) + self.assertEqual(rows[contribution_slip.name][contribution_fieldname], 2400) + self.assertEqual(rows[contribution_slip.name]["total_employer_contribution"], 2400) From d8933304396daf2ba187a3bf6e4dbc7e9cff4380 Mon Sep 17 00:00:00 2001 From: Asmita Hase Date: Fri, 18 Sep 2026 14:54:37 +0530 Subject: [PATCH 4/5] fix: keep salary register component fieldnames unique --- .../report/salary_register/salary_register.py | 35 +++++++++++----- .../salary_register/test_salary_register.py | 40 ++++++++++++++++--- 2 files changed, 60 insertions(+), 15 deletions(-) diff --git a/hrms/payroll/report/salary_register/salary_register.py b/hrms/payroll/report/salary_register/salary_register.py index f388800f06b..a72315b6f01 100644 --- a/hrms/payroll/report/salary_register/salary_register.py +++ b/hrms/payroll/report/salary_register/salary_register.py @@ -31,7 +31,8 @@ def execute(filters=None): parentfields = get_active_parentfields(filters) components = get_components_by_parentfield(salary_slips, parentfields) - columns = get_columns(components) + fieldnames = get_component_fieldnames(components, parentfields) + columns = get_columns(components, fieldnames) component_maps = { parentfield: get_salary_slip_details(salary_slips, currency, company_currency, parentfield) @@ -65,7 +66,9 @@ def execute(filters=None): for parentfield in parentfields: amounts = component_maps[parentfield].get(ss.name, {}) for component, amount in amounts.items(): - row.setdefault(get_component_fieldname(parentfield, component), amount) + fieldname = fieldnames.get((parentfield, component)) + if fieldname: + row[fieldname] = amount if components[EMPLOYER_CONTRIBUTIONS]: row.update( @@ -107,11 +110,23 @@ def get_active_parentfields(filters): return tuple(p for p in COMPONENT_PARENTFIELDS if p != EMPLOYER_CONTRIBUTIONS) -def get_component_fieldname(parentfield, component): - if parentfield == EMPLOYER_CONTRIBUTIONS: - return f"employer_contribution_{frappe.scrub(component)}" +def get_component_fieldnames(components, parentfields): + fieldnames = {} + used = set() - return frappe.scrub(component) + for parentfield in parentfields: + for component in components[parentfield]: + fieldname = frappe.scrub(component) + if parentfield == EMPLOYER_CONTRIBUTIONS: + fieldname = f"employer_contribution_{fieldname}" + + if fieldname in used: + fieldname = f"{fieldname}_{len(used)}" + + used.add(fieldname) + fieldnames[(parentfield, component)] = fieldname + + return fieldnames def get_components_by_parentfield(salary_slips, parentfields): @@ -144,7 +159,7 @@ def update_column_width(ss, columns): columns[9].update({"width": 120}) -def get_columns(components): +def get_columns(components, fieldnames): columns = [ { "label": _("Salary Slip ID"), @@ -236,7 +251,7 @@ def get_columns(components): columns.append( { "label": earning, - "fieldname": get_component_fieldname("earnings", earning), + "fieldname": fieldnames[("earnings", earning)], "fieldtype": "Currency", "options": "currency", "width": 120, @@ -257,7 +272,7 @@ def get_columns(components): columns.append( { "label": deduction, - "fieldname": get_component_fieldname("deductions", deduction), + "fieldname": fieldnames[("deductions", deduction)], "fieldtype": "Currency", "options": "currency", "width": 120, @@ -298,7 +313,7 @@ def get_columns(components): columns.append( { "label": contribution, - "fieldname": get_component_fieldname(EMPLOYER_CONTRIBUTIONS, contribution), + "fieldname": fieldnames[(EMPLOYER_CONTRIBUTIONS, contribution)], "fieldtype": "Currency", "options": "currency", "width": 120, diff --git a/hrms/payroll/report/salary_register/test_salary_register.py b/hrms/payroll/report/salary_register/test_salary_register.py index 3d3f28bf79c..8faa26de4b2 100644 --- a/hrms/payroll/report/salary_register/test_salary_register.py +++ b/hrms/payroll/report/salary_register/test_salary_register.py @@ -11,11 +11,13 @@ create_salary_structure_assignment, make_salary_structure, ) -from hrms.payroll.report.salary_register.salary_register import execute, get_component_fieldname +from hrms.payroll.report.salary_register.salary_register import execute from hrms.tests.utils import HRMSTestSuite EMPLOYER_PF = "_Test Register Employer PF" SHARED_PF = "_Test Register Shared PF" +PREFIXED_PF = "_Test Register PF" +CLASHING_PF = "Employer Contribution _Test Register PF" class TestSalaryRegister(HRMSTestSuite): @@ -78,7 +80,7 @@ def test_employer_contributions_hidden_by_default(self): columns, data = self.get_report() fieldnames = [column["fieldname"] for column in columns] - self.assertNotIn(get_component_fieldname("employer_contributions", EMPLOYER_PF), fieldnames) + self.assertNotIn(f"employer_contribution_{frappe.scrub(EMPLOYER_PF)}", fieldnames) self.assertNotIn("total_employer_contribution", fieldnames) self.assertEqual(len(data), 1) @@ -88,7 +90,7 @@ def test_employer_contributions_shown_with_filter(self): columns, data = self.get_report(show_employer_contributions=1) - fieldname = get_component_fieldname("employer_contributions", EMPLOYER_PF) + fieldname = f"employer_contribution_{frappe.scrub(EMPLOYER_PF)}" fieldnames = [column["fieldname"] for column in columns] self.assertIn(fieldname, fieldnames) self.assertIn("total_employer_contribution", fieldnames) @@ -136,8 +138,8 @@ def test_component_shared_across_parentfields(self): columns, data = self.get_report(show_employer_contributions=1) - deduction_fieldname = get_component_fieldname("deductions", SHARED_PF) - contribution_fieldname = get_component_fieldname("employer_contributions", SHARED_PF) + deduction_fieldname = frappe.scrub(SHARED_PF) + contribution_fieldname = f"employer_contribution_{frappe.scrub(SHARED_PF)}" fieldnames = [column["fieldname"] for column in columns] self.assertEqual(fieldnames.count(deduction_fieldname), 1) @@ -148,3 +150,31 @@ def test_component_shared_across_parentfields(self): self.assertEqual(rows[deduction_slip.name][deduction_fieldname], 1800) self.assertEqual(rows[contribution_slip.name][contribution_fieldname], 2400) self.assertEqual(rows[contribution_slip.name]["total_employer_contribution"], 2400) + + def test_scrubbed_names_do_not_clash_across_parentfields(self): + self.create_component(PREFIXED_PF, "TRPF", "Employer Contribution") + self.create_component(CLASHING_PF, "ECTRPF", "Deduction") + + salary_structure = make_salary_structure( + "_Test Salary Register Clash Structure", + "Monthly", + employee=self.employee, + company="_Test Company", + currency="INR", + from_date=self.start_date, + deductions=[{"salary_component": CLASHING_PF, "abbr": "ECTRPF", "amount": 500}], + other_details={ + "employer_contributions": [{"salary_component": PREFIXED_PF, "abbr": "TRPF", "amount": 1800}] + }, + ) + self.create_salary_slip(salary_structure.name, self.end_date) + + columns, data = self.get_report(show_employer_contributions=1) + + deduction_column = next(column for column in columns if column["label"] == CLASHING_PF) + contribution_column = next(column for column in columns if column["label"] == PREFIXED_PF) + + self.assertNotEqual(deduction_column["fieldname"], contribution_column["fieldname"]) + self.assertEqual(data[0][deduction_column["fieldname"]], 500) + self.assertEqual(data[0][contribution_column["fieldname"]], 1800) + self.assertEqual(data[0]["total_employer_contribution"], 1800) From 99b514098ee5de6193cf8bf193ed8303e60d4fc0 Mon Sep 17 00:00:00 2001 From: Asmita Hase Date: Fri, 18 Sep 2026 15:29:22 +0530 Subject: [PATCH 5/5] refactor: use plain parentfield strings in salary register --- .../report/salary_register/salary_register.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/hrms/payroll/report/salary_register/salary_register.py b/hrms/payroll/report/salary_register/salary_register.py index a72315b6f01..a377819bbed 100644 --- a/hrms/payroll/report/salary_register/salary_register.py +++ b/hrms/payroll/report/salary_register/salary_register.py @@ -13,8 +13,6 @@ salary_slip = frappe.qb.DocType("Salary Slip") salary_detail = frappe.qb.DocType("Salary Detail") -EMPLOYER_CONTRIBUTIONS = "employer_contributions" - def execute(filters=None): if not filters: @@ -70,11 +68,11 @@ def execute(filters=None): if fieldname: row[fieldname] = amount - if components[EMPLOYER_CONTRIBUTIONS]: + if components["employer_contributions"]: row.update( { "total_employer_contribution": sum( - component_maps[EMPLOYER_CONTRIBUTIONS].get(ss.name, {}).values() + component_maps["employer_contributions"].get(ss.name, {}).values() ) } ) @@ -107,7 +105,7 @@ def get_active_parentfields(filters): if filters.get("show_employer_contributions"): return COMPONENT_PARENTFIELDS - return tuple(p for p in COMPONENT_PARENTFIELDS if p != EMPLOYER_CONTRIBUTIONS) + return ("earnings", "deductions") def get_component_fieldnames(components, parentfields): @@ -117,7 +115,7 @@ def get_component_fieldnames(components, parentfields): for parentfield in parentfields: for component in components[parentfield]: fieldname = frappe.scrub(component) - if parentfield == EMPLOYER_CONTRIBUTIONS: + if parentfield == "employer_contributions": fieldname = f"employer_contribution_{fieldname}" if fieldname in used: @@ -139,7 +137,7 @@ def get_components_by_parentfield(salary_slips, parentfields): if row.parentfield != parentfield: continue - if parentfield == EMPLOYER_CONTRIBUTIONS: + if parentfield == "employer_contributions": components[parentfield].add(row.salary_component) elif row.salary_component not in pay_components: components[parentfield].add(row.salary_component) @@ -309,18 +307,18 @@ def get_columns(components, fieldnames): ] ) - for contribution in components[EMPLOYER_CONTRIBUTIONS]: + for contribution in components["employer_contributions"]: columns.append( { "label": contribution, - "fieldname": fieldnames[(EMPLOYER_CONTRIBUTIONS, contribution)], + "fieldname": fieldnames[("employer_contributions", contribution)], "fieldtype": "Currency", "options": "currency", "width": 120, } ) - if components[EMPLOYER_CONTRIBUTIONS]: + if components["employer_contributions"]: columns.append( { "label": _("Total Employer Contribution"),