Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow salary slip preview for draft salary structure assignments #1827

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
57 changes: 33 additions & 24 deletions hrms/payroll/doctype/salary_slip/salary_slip.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,19 +725,28 @@ def add_earning_for_hourly_wages(self, doc, salary_component, amount):
}
doc.append("earnings", wages_row)

def set_salary_structure_assignment(self):
self._salary_structure_assignment = frappe.db.get_value(
"Salary Structure Assignment",
{
"employee": self.employee,
"salary_structure": self.salary_structure,
"from_date": ("<=", self.actual_start_date),
"docstatus": 1,
},
"*",
order_by="from_date desc",
as_dict=True,
)
def set_salary_structure_assignment(self, salary_structure_assignment=None, docstatus=1):
if not salary_structure_assignment:
self._salary_structure_assignment = frappe.db.get_value(
"Salary Structure Assignment",
{
"employee": self.employee,
"salary_structure": self.salary_structure,
"from_date": ("<=", self.actual_start_date),
"docstatus": docstatus,
},
"*",
order_by="from_date desc",
as_dict=True,
)
else:
self._salary_structure_assignment = frappe.db.get_value(
"Salary Structure Assignment",
salary_structure_assignment,
"*",
order_by="from_date desc",
as_dict=True,
)

if not self._salary_structure_assignment:
frappe.throw(
Expand All @@ -749,9 +758,9 @@ def set_salary_structure_assignment(self):
)
)

def calculate_net_pay(self):
def calculate_net_pay(self, salary_structure_assignment=None, docstatus=1):
if self.salary_structure:
self.calculate_component_amounts("earnings")
self.calculate_component_amounts("earnings", salary_structure_assignment, docstatus)

# get remaining numbers of sub-period (period for which one salary is processed)
if self.payroll_period:
Expand All @@ -771,7 +780,7 @@ def calculate_net_pay(self):
)

if self.salary_structure:
self.calculate_component_amounts("deductions")
self.calculate_component_amounts("deductions", salary_structure_assignment, docstatus)

set_loan_repayment(self)

Expand Down Expand Up @@ -1054,19 +1063,19 @@ def get_income_tax_deducted_till_date(self):
)
return tax_deducted

def calculate_component_amounts(self, component_type):
def calculate_component_amounts(self, component_type, salary_structure_assignment=None, docstatus=1):
if not getattr(self, "_salary_structure_doc", None):
self._salary_structure_doc = frappe.get_cached_doc("Salary Structure", self.salary_structure)

self.add_structure_components(component_type)
self.add_structure_components(component_type, salary_structure_assignment, docstatus)
self.add_additional_salary_components(component_type)
if component_type == "earnings":
self.add_employee_benefits()
else:
self.add_tax_components()

def add_structure_components(self, component_type):
self.data, self.default_data = self.get_data_for_eval()
def add_structure_components(self, component_type, salary_structure_assignment=None, docstatus=1):
self.data, self.default_data = self.get_data_for_eval(salary_structure_assignment, docstatus)
timesheet_component = self._salary_structure_doc.salary_component

for struct_row in self._salary_structure_doc.get(component_type):
Expand Down Expand Up @@ -1111,13 +1120,13 @@ def add_structure_components(self, component_type):
remove_if_zero_valued=remove_if_zero_valued,
)

def get_data_for_eval(self):
def get_data_for_eval(self, salary_structure_assignment=None, docstatus=1):
"""Returns data for evaluating formula"""
data = frappe._dict()
employee = frappe.get_cached_doc("Employee", self.employee).as_dict()

if not hasattr(self, "_salary_structure_assignment"):
self.set_salary_structure_assignment()
self.set_salary_structure_assignment(salary_structure_assignment, docstatus)

data.update(self._salary_structure_assignment)
data.update(self.as_dict())
Expand Down Expand Up @@ -1870,13 +1879,13 @@ def set_status(self, status=None):
status = self.get_status()
self.db_set("status", status)

def process_salary_structure(self, for_preview=0):
def process_salary_structure(self, for_preview=0, salary_structure_assignment=None, docstatus=1):
"""Calculate salary after salary structure details have been updated"""
if not self.salary_slip_based_on_timesheet:
self.get_date_details()
self.pull_emp_details()
self.get_working_days_details(for_preview=for_preview)
self.calculate_net_pay()
self.calculate_net_pay(salary_structure_assignment, docstatus)

def pull_emp_details(self):
account_details = frappe.get_cached_value(
Expand Down
9 changes: 8 additions & 1 deletion hrms/payroll/doctype/salary_structure/salary_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,14 +359,21 @@ def make_salary_slip(
print_format=None,
for_preview=0,
ignore_permissions=False,
salary_structure_assignment=None,
docstatus=1,
):
def postprocess(source, target):
if employee:
target.employee = employee
if posting_date:
target.posting_date = posting_date

target.run_method("process_salary_structure", for_preview=for_preview)
target.run_method(
"process_salary_structure",
for_preview=for_preview,
salary_structure_assignment=salary_structure_assignment,
docstatus=docstatus,
)

doc = get_mapped_doc(
"Salary Structure",
Expand Down
Loading