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
35 changes: 33 additions & 2 deletions hrms/hr/doctype/employee_advance/test_employee_advance.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,34 @@ def test_advance_return_on_payroll_submission(self):
advance.reload()
self.assertEqual(advance.status, "Returned")

def test_advance_return_posts_to_advance_account_when_component_account_differs(self):
company_doc = frappe.get_doc("Company", "_Test Company")
employee = make_employee("test_advance_component_account@payroll.com", company=company_doc.name)

advance = make_employee_advance(employee, {"repay_unclaimed_amount_from_salary": 1})
make_payment_entry(advance)
advance.reload()

create_payroll_for_advance_return(employee, company_doc, advance, component_account="Salary - _TC")

deduction_entry = frappe.get_all(
"Journal Entry Account",
fields=["account", "party", "credit"],
filters={
"reference_type": "Employee Advance",
"reference_name": advance.name,
"is_advance": "Yes",
},
)[0]
self.assertEqual(
deduction_entry,
{"account": advance.advance_account, "party": employee, "credit": advance.paid_amount},
)

advance.reload()
self.assertEqual(advance.return_amount, advance.paid_amount)
self.assertEqual(advance.status, "Returned")

def test_payment_entry_against_advance(self):
employee_name = make_employee("_T@employee.advance", "_Test Company")
advance = make_employee_advance(employee_name)
Expand Down Expand Up @@ -576,17 +604,20 @@ def create_advance_account(account_name, account_currency):
)


def create_payroll_for_advance_return(employee, company, advance, return_amount=None):
def create_payroll_for_advance_return(
employee, company, advance, return_amount=None, component_account="Employee Advances - _TC"
):
# Advance deduction component
component = create_salary_component(
"Advance Salary",
**{"type": "Deduction"},
)
component.set("accounts", [])
component.append(
"accounts",
{
"company": company.name,
"account": "Employee Advances - _TC",
"account": component_account,
},
)
component.save()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ def get_outstanding_statements(self):
self.add_withheld_salary_slips()
components = self.get_payable_component()
self.create_component_row(components, "payables")
if not self.receivables:
components = self.get_receivable_component()
self.create_component_row(components, "receivables")
self.add_outstanding_employee_advances()
self.create_component_row(self.get_receivable_component(), "receivables")
self.get_assets_statements()

def get_assets_statements(self):
Expand Down Expand Up @@ -170,8 +169,67 @@ def add_withheld_salary_slips(self):
},
)

def add_outstanding_employee_advances(self):
"""
Reconciles receivables with the employee's outstanding advances: placeholder rows and other
employees' advances are dropped, rows not marked Settled are refreshed to the current balance,
and missing advances are added.
"""
advances = frappe.get_all(
"Employee Advance",
filters={"employee": self.employee, "docstatus": 1},
fields=["name", "advance_account", "paid_amount", "claimed_amount", "return_amount"],
)
outstanding_advances = {}
for advance in advances:
advance.outstanding = (
flt(advance.paid_amount) - flt(advance.claimed_amount) - flt(advance.return_amount)
)
if advance.outstanding > 0:
outstanding_advances[advance.name] = advance

self.receivables = [
row
for row in self.receivables
if row.component != "Employee Advance"
or (row.reference_document in outstanding_advances)
or (not row.reference_document and not outstanding_advances)
]

referenced_advances = set()
for row in self.receivables:
advance = outstanding_advances.get(row.reference_document)
if not advance:
continue

referenced_advances.add(advance.name)
if row.status != "Settled":
row.account = advance.advance_account
row.amount = advance.outstanding

for advance in outstanding_advances.values():
if advance.name in referenced_advances:
continue

self.append(
"receivables",
{
"status": "Unsettled",
"component": "Employee Advance",
"reference_document_type": "Employee Advance",
"reference_document": advance.name,
"account": advance.advance_account,
"amount": advance.outstanding,
},
)

def create_component_row(self, components, component_type):
existing_components = {row.component for row in self.get(component_type)}

for component in components:
if component in existing_components:
continue

self.append(
component_type,
{
Expand Down Expand Up @@ -263,8 +321,15 @@ def create_journal_entry(self):
"user_remark": data.remark,
}
if data.reference_document_type == "Employee Advance":
account_dict["party_type"] = "Employee"
account_dict["party"] = self.employee
account_dict.update(
{
"party_type": "Employee",
"party": self.employee,
"reference_type": "Employee Advance",
"reference_name": data.reference_document,
"is_advance": "Yes",
}
)

jv.append("accounts", account_dict)

Expand Down Expand Up @@ -301,7 +366,10 @@ def update_reference_document_payment_status(self, payable):
def update_linked_payable_documents(self):
"""update payment status in linked payable documents"""
for payable in self.payables:
if payable.reference_document_type in ["Gratuity", "Leave Encashment"]:
if (
payable.reference_document_type in ["Gratuity", "Leave Encashment"]
and payable.reference_document
):
self.update_reference_document_payment_status(payable)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,131 @@ def test_journal_entry(self):
self.assertEqual(debit_entry.reference_type, "Full and Final Statement")
self.assertEqual(debit_entry.reference_name, self.fnf.name)

def test_employee_advance_settlement(self):
from hrms.hr.doctype.employee_advance.test_employee_advance import (
make_employee_advance,
make_payment_entry,
)

advance = make_employee_advance(self.employee)
make_payment_entry(advance)
advance.reload()

self.fnf.receivables = []
self.fnf.get_outstanding_statements()

advance_rows = [row for row in self.fnf.receivables if row.component == "Employee Advance"]
self.assertEqual(len(advance_rows), 1)
self.assertEqual(advance_rows[0].reference_document, advance.name)
self.assertEqual(advance_rows[0].account, advance.advance_account)
self.assertEqual(advance_rows[0].amount, advance.paid_amount)
self.fnf.save()

jv = self.fnf.create_journal_entry()
jv.accounts[-1].account = "_Test Bank - _TC"
jv.cheque_no = "FNF-ADV-001"
jv.cheque_date = today()
jv.insert()
jv.submit()

advance.reload()
self.assertEqual(advance.return_amount, advance.paid_amount)
self.assertEqual(advance.status, "Returned")

def test_employee_advance_added_after_bootstrap(self):
from hrms.hr.doctype.employee_advance.test_employee_advance import (
make_employee_advance,
make_payment_entry,
)

# placeholder rows already exist from bootstrap
self.assertTrue(any(row.component == "Employee Advance" for row in self.fnf.receivables))

advance = make_employee_advance(self.employee)
make_payment_entry(advance)
advance.reload()

self.fnf.get_outstanding_statements()
advance_rows = [row for row in self.fnf.receivables if row.component == "Employee Advance"]
self.assertEqual([row.reference_document for row in advance_rows], [advance.name])
self.assertEqual(advance_rows[0].amount, advance.paid_amount)

# calling again neither duplicates the advance nor re-adds the placeholder
self.fnf.get_outstanding_statements()
advance_rows = [row for row in self.fnf.receivables if row.component == "Employee Advance"]
self.assertEqual([row.reference_document for row in advance_rows], [advance.name])

def test_employee_advance_row_refreshed_to_current_balance(self):
from hrms.hr.doctype.employee_advance.employee_advance import make_return_entry
from hrms.hr.doctype.employee_advance.test_employee_advance import (
make_employee_advance,
make_payment_entry,
)

advance = make_employee_advance(self.employee)
make_payment_entry(advance)
advance.reload()
self.fnf.get_outstanding_statements()
advance_row = next(row for row in self.fnf.receivables if row.reference_document == advance.name)
self.assertEqual(advance_row.amount, advance.paid_amount)

# a partial return after the row was created reduces the outstanding balance
return_entry = frappe.get_doc(
make_return_entry(
employee=advance.employee,
company=advance.company,
employee_advance_name=advance.name,
return_amount=300,
advance_account=advance.advance_account,
mode_of_payment=advance.mode_of_payment,
currency=advance.currency,
)
)
return_entry.insert()
return_entry.submit()
advance.reload()
self.assertEqual(advance.return_amount, 300)

self.fnf.get_outstanding_statements()
advance_row = next(row for row in self.fnf.receivables if row.reference_document == advance.name)
self.assertEqual(advance_row.amount, advance.paid_amount - 300)

# a row with a blank status is refreshed like an unsettled one
advance_row.status = None
advance_row.amount = 100
self.fnf.get_outstanding_statements()
advance_row = next(row for row in self.fnf.receivables if row.reference_document == advance.name)
self.assertEqual(advance_row.amount, advance.paid_amount - 300)

# a row the user has already settled is left as it is
advance_row.status = "Settled"
advance_row.amount = 100
self.fnf.get_outstanding_statements()
advance_row = next(row for row in self.fnf.receivables if row.reference_document == advance.name)
self.assertEqual(advance_row.amount, 100)

def test_employee_advance_rows_follow_employee_change(self):
from hrms.hr.doctype.employee_advance.test_employee_advance import (
make_employee_advance,
make_payment_entry,
)

advance = make_employee_advance(self.employee)
make_payment_entry(advance)
self.fnf.get_outstanding_statements()
self.assertIn(advance.name, [row.reference_document for row in self.fnf.receivables])

other_employee = make_employee(
"test_fnf_other@example.com", company="_Test Company", relieving_date=add_days(today(), 30)
)
self.fnf.employee = other_employee
self.fnf.relieving_date = add_days(today(), 30)
self.fnf.get_outstanding_statements()

advance_rows = [row for row in self.fnf.receivables if row.component == "Employee Advance"]
self.assertEqual(len(advance_rows), 1)
self.assertFalse(advance_rows[0].reference_document)

def test_status_on_discard(self):
self.fnf.discard()
self.fnf.reload()
Expand Down
38 changes: 25 additions & 13 deletions hrms/payroll/doctype/payroll_entry/payroll_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,13 @@ def get_salary_component_total(
salary_components = self.get_salary_components(component_type)
if salary_components:
component_dict = {}
advance_deductions = self.get_advance_deductions(component_type, salary_components)

for item in salary_components:
employee_cost_centers = self.get_payroll_cost_centers_for_employee(
item.employee, item.salary_structure
)
employee_advance = self.get_advance_deduction(component_type, item)
employee_advance = advance_deductions.get(item.additional_salary)

for cost_center, percentage in employee_cost_centers.items():
amount_against_cost_center = flt(item.amount) * percentage / 100
Expand All @@ -477,33 +478,44 @@ def get_salary_component_total(

return account_details

def get_advance_deduction(self, component_type: str, item: dict) -> str | None:
if component_type == "deductions" and item.additional_salary:
ref_doctype, ref_docname = frappe.db.get_value(
"Additional Salary",
item.additional_salary,
["ref_doctype", "ref_docname"],
def get_advance_deductions(self, component_type: str, salary_components: list[dict]) -> dict[str, dict]:
"""Employee Advances being recovered, keyed by the Additional Salary that recovers them"""
additional_salaries = list({d.additional_salary for d in salary_components if d.additional_salary})
if component_type != "deductions" or not additional_salaries:
return {}

AdditionalSalary = frappe.qb.DocType("Additional Salary")
EmployeeAdvance = frappe.qb.DocType("Employee Advance")
advances = (
frappe.qb.from_(AdditionalSalary)
.join(EmployeeAdvance)
.on(AdditionalSalary.ref_docname == EmployeeAdvance.name)
.select(
AdditionalSalary.name.as_("additional_salary"),
EmployeeAdvance.name.as_("employee_advance"),
EmployeeAdvance.advance_account,
)
.where(AdditionalSalary.name.isin(additional_salaries))
.where(AdditionalSalary.ref_doctype == "Employee Advance")
).run(as_dict=True)

if ref_doctype == "Employee Advance":
return ref_docname
return
return {d.additional_salary: d for d in advances}

def add_advance_deduction_entry(
self,
item: dict,
amount: float,
cost_center: str,
employee_advance: str,
employee_advance: dict,
) -> None:
self._advance_deduction_entries.append(
{
"employee": item.employee,
"account": self.get_salary_component_account(item.salary_component),
"account": employee_advance.advance_account,
"amount": amount,
"cost_center": cost_center,
"reference_type": "Employee Advance",
"reference_name": employee_advance,
"reference_name": employee_advance.employee_advance,
}
)

Expand Down
2 changes: 1 addition & 1 deletion hrms/payroll/doctype/payroll_entry/test_payroll_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ def test_advance_deduction_in_accrual_journal_entry(self):
)[0]

expected_entry = {
"account": "Employee Advances - _TC",
"account": advance.advance_account,
"party": employee,
"debit": 0.0,
"credit": advance.paid_amount,
Expand Down
Loading