Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions hrms/payroll/report/salary_register/salary_register.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
],
};
141 changes: 106 additions & 35 deletions hrms/payroll/report/salary_register/salary_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

import erpnext

from hrms.payroll.utils import COMPONENT_PARENTFIELDS

salary_slip = frappe.qb.DocType("Salary Slip")
salary_detail = frappe.qb.DocType("Salary Detail")
salary_component = frappe.qb.DocType("Salary Component")


def execute(filters=None):
Expand All @@ -26,11 +27,15 @@ 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)
parentfields = get_active_parentfields(filters)
components = get_components_by_parentfield(salary_slips, parentfields)
fieldnames = get_component_fieldnames(components, parentfields)
columns = get_columns(components, fieldnames)

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 = {
parentfield: get_salary_slip_details(salary_slips, currency, company_currency, parentfield)
for parentfield in parentfields
}

doj_map = get_employee_doj_map()

Expand All @@ -56,11 +61,21 @@ 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 parentfield in parentfields:
amounts = component_maps[parentfield].get(ss.name, {})
for component, amount in amounts.items():
fieldname = fieldnames.get((parentfield, component))
if fieldname:
row[fieldname] = amount

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(
Expand All @@ -86,14 +101,49 @@ def execute(filters=None):
return columns, data


def get_earning_and_deduction_types(salary_slips):
salary_component_and_type = {_("Earning"): [], _("Deduction"): []}
def get_active_parentfields(filters):
if filters.get("show_employer_contributions"):
return COMPONENT_PARENTFIELDS

return ("earnings", "deductions")


def get_component_fieldnames(components, parentfields):
fieldnames = {}
used = set()

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

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 fieldnames

return sorted(salary_component_and_type[_("Earning")]), sorted(salary_component_and_type[_("Deduction")])

def get_components_by_parentfield(salary_slips, parentfields):
rows = get_salary_components(salary_slips)
components = {parentfield: set() for parentfield in COMPONENT_PARENTFIELDS}
pay_components = set()

for parentfield in parentfields:
for row in rows:
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)
pay_components.add(row.salary_component)

return {parentfield: sorted(names) for parentfield, names in components.items()}


def update_column_width(ss, columns):
Expand All @@ -107,7 +157,7 @@ def update_column_width(ss, columns):
columns[9].update({"width": 120})


def get_columns(earning_types, ded_types):
def get_columns(components, fieldnames):
columns = [
{
"label": _("Salary Slip ID"),
Expand Down Expand Up @@ -195,11 +245,11 @@ def get_columns(earning_types, ded_types):
},
]

for earning in earning_types:
for earning in components["earnings"]:
columns.append(
{
"label": earning,
"fieldname": frappe.scrub(earning),
"fieldname": fieldnames[("earnings", earning)],
"fieldtype": "Currency",
"options": "currency",
"width": 120,
Expand All @@ -216,11 +266,11 @@ def get_columns(earning_types, ded_types):
}
)

for deduction in ded_types:
for deduction in components["deductions"]:
columns.append(
{
"label": deduction,
"fieldname": frappe.scrub(deduction),
"fieldname": fieldnames[("deductions", deduction)],
"fieldtype": "Currency",
"options": "currency",
"width": 120,
Expand Down Expand Up @@ -254,29 +304,50 @@ 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": fieldnames[("employer_contributions", 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


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):
Expand Down Expand Up @@ -324,14 +395,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,
Expand Down
Loading
Loading