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

fix: exclude advances scheduled for deduction when adjusting them aga… #2388

Closed
wants to merge 11 commits into from
1 change: 1 addition & 0 deletions hrms/hr/doctype/employee_advance/employee_advance.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ frappe.ui.form.on("Employee Advance", {
posting_date: frm.doc.posting_date,
paid_amount: frm.doc.paid_amount,
claimed_amount: frm.doc.claimed_amount,
return_amount: frm.doc.return_amount,
},
callback: function (r) {
const doclist = frappe.model.sync(r.message);
Expand Down
11 changes: 8 additions & 3 deletions hrms/hr/doctype/employee_advance/test_employee_advance.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
make_bank_entry,
make_return_entry,
)
from hrms.hr.doctype.expense_claim.expense_claim import get_advances
from hrms.hr.doctype.expense_claim.expense_claim import get_advances, get_allocation_amount
from hrms.hr.doctype.expense_claim.test_expense_claim import (
get_payable_account,
make_expense_claim,
Expand Down Expand Up @@ -353,7 +353,11 @@ def get_advances_for_claim(claim, advance_name, amount=None):
if amount:
allocated_amount = amount
else:
allocated_amount = flt(entry.paid_amount) - flt(entry.claimed_amount)
allocated_amount = get_allocation_amount(
paid_amount=entry.paid_amount,
claimed_amount=entry.claimed_amount,
return_amount=entry.return_amount,
)

claim.append(
"advances",
Expand All @@ -362,7 +366,8 @@ def get_advances_for_claim(claim, advance_name, amount=None):
"posting_date": entry.posting_date,
"advance_account": entry.advance_account,
"advance_paid": entry.paid_amount,
"unclaimed_amount": allocated_amount,
"return_amount": entry.return_amount,
"unclaimed_amount": entry.paid_amount - entry.claimed_amount,
"allocated_amount": allocated_amount,
},
)
Expand Down
25 changes: 19 additions & 6 deletions hrms/hr/doctype/expense_claim/expense_claim.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,9 @@ frappe.ui.form.on("Expense Claim", {
update_employee_advance_claimed_amount: function (frm) {
let amount_to_be_allocated = frm.doc.grand_total;
$.each(frm.doc.advances || [], function (i, advance) {
if (amount_to_be_allocated >= advance.unclaimed_amount) {
advance.allocated_amount = frm.doc.advances[i].unclaimed_amount;
if (amount_to_be_allocated >= advance.unclaimed_amount - advance.return_amount) {
advance.allocated_amount =
frm.doc.advances[i].unclaimed_amount - frm.doc.advances[i].return_amount;
amount_to_be_allocated -= advance.allocated_amount;
} else {
advance.allocated_amount = amount_to_be_allocated;
Expand All @@ -204,7 +205,6 @@ frappe.ui.form.on("Expense Claim", {
frm.refresh_field("advances");
});
},

make_payment_entry: function (frm) {
let method = "hrms.overrides.employee_payment_entry.get_payment_entry_for_employee";
if (frm.doc.__onload && frm.doc.__onload.make_payment_via_journal_entry) {
Expand Down Expand Up @@ -309,7 +309,12 @@ frappe.ui.form.on("Expense Claim", {
row.advance_account = d.advance_account;
row.advance_paid = d.paid_amount;
row.unclaimed_amount = flt(d.paid_amount) - flt(d.claimed_amount);
row.allocated_amount = 0;
row.return_amount = flt(d.return_amount);
row.allocated_amount = get_allocation_amount(
flt(d.paid_amount),
flt(d.claimed_amount),
flt(d.return_amount),
);
});
refresh_field("advances");
}
Expand Down Expand Up @@ -385,8 +390,12 @@ frappe.ui.form.on("Expense Claim Advance", {
child.advance_paid = r.message[0].paid_amount;
child.unclaimed_amount =
flt(r.message[0].paid_amount) - flt(r.message[0].claimed_amount);
child.allocated_amount =
flt(r.message[0].paid_amount) - flt(r.message[0].claimed_amount);
child.return_amount = flt(r.message[0].return_amount);
child.allocated_amount = get_allocation_amount(
flt(r.message[0].paid_amount),
flt(r.message[0].claimed_amount),
flt(r.message[0].return_amount),
);
frm.trigger("calculate_grand_total");
refresh_field("advances");
}
Expand Down Expand Up @@ -432,3 +441,7 @@ frappe.ui.form.on("Expense Taxes and Charges", {
frm.trigger("calculate_total_tax", cdt, cdn);
},
});

function get_allocation_amount(paid_amount, claimed_amount, return_amount) {
return paid_amount - (claimed_amount + return_amount);
}
30 changes: 26 additions & 4 deletions hrms/hr/doctype/expense_claim/expense_claim.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,15 +325,17 @@ def validate_advances(self):
ref_doc = frappe.db.get_value(
"Employee Advance",
d.employee_advance,
["posting_date", "paid_amount", "claimed_amount", "advance_account"],
["posting_date", "paid_amount", "claimed_amount", "return_amount", "advance_account"],
as_dict=1,
)
d.posting_date = ref_doc.posting_date
d.advance_account = ref_doc.advance_account
d.advance_paid = ref_doc.paid_amount
d.unclaimed_amount = flt(ref_doc.paid_amount) - flt(ref_doc.claimed_amount)

if d.allocated_amount and flt(d.allocated_amount) > flt(d.unclaimed_amount):
if d.allocated_amount and flt(d.allocated_amount) > (
flt(d.unclaimed_amount) - flt(d.return_amount)
):
frappe.throw(
_("Row {0}# Allocated amount {1} cannot be greater than unclaimed amount {2}").format(
d.idx, d.allocated_amount, d.unclaimed_amount
Expand Down Expand Up @@ -497,6 +499,7 @@ def get_advances(employee, advance_id=None):
advance.posting_date,
advance.paid_amount,
advance.claimed_amount,
advance.return_amount,
advance.advance_account,
)

Expand All @@ -515,7 +518,7 @@ def get_advances(employee, advance_id=None):

@frappe.whitelist()
def get_expense_claim(
employee_name, company, employee_advance_name, posting_date, paid_amount, claimed_amount
employee_name, company, employee_advance_name, posting_date, paid_amount, claimed_amount, return_amount
):
default_payable_account = frappe.get_cached_value(
"Company", company, "default_expense_claim_payable_account"
Expand All @@ -535,7 +538,10 @@ def get_expense_claim(
"posting_date": posting_date,
"advance_paid": flt(paid_amount),
"unclaimed_amount": flt(paid_amount) - flt(claimed_amount),
"allocated_amount": flt(paid_amount) - flt(claimed_amount),
"allocated_amount": get_allocation_amount(
paid_amount=(paid_amount), claimed_amount=(claimed_amount), return_amount=(return_amount)
),
"return_amount": flt(return_amount),
},
)

Expand Down Expand Up @@ -592,3 +598,19 @@ def make_expense_claim_for_delivery_trip(source_name, target_doc=None):
)

return doc


# // amke below fucntion reusable basef on wht is passed, if only unclaimed and return_amt is pased, return unclaimed - returne_amt else paid_amount - (claimed_amount + return_amount)
# @frappe.whitelist()
# def get_allocation_amount(paid_amount, claimed_amount, return_amount):
# return paid_amount - (claimed_amount + return_amount)


@frappe.whitelist()
def get_allocation_amount(paid_amount=None, claimed_amount=None, return_amount=None, unclaimed_amount=None):
if unclaimed_amount is not None and return_amount is not None:
return flt(unclaimed_amount) - flt(return_amount)
elif paid_amount is not None and claimed_amount is not None and return_amount is not None:
return flt(paid_amount) - (flt(claimed_amount) + flt(return_amount))
else:
frappe.throw(_("Invalid parameters provided. Please pass the required arguments."))
58 changes: 58 additions & 0 deletions hrms/hr/doctype/expense_claim/test_expense_claim.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,64 @@ def test_expense_claim_partially_paid_via_advance(self):
self.assertEqual(claim.total_amount_reimbursed, 500)
self.assertEqual(claim.status, "Paid")

def test_expense_claim_with_deducted_returned_advance(self):
from hrms.hr.doctype.employee_advance.test_employee_advance import (
create_return_through_additional_salary,
get_advances_for_claim,
make_employee_advance,
make_journal_entry_for_advance,
)
from hrms.hr.doctype.expense_claim.expense_claim import get_allocation_amount
from hrms.payroll.doctype.salary_component.test_salary_component import create_salary_component
from hrms.payroll.doctype.salary_structure.test_salary_structure import make_salary_structure

# create employee and employee advance
employee_name = make_employee("[email protected]", "_Test Company")
advance = make_employee_advance(employee_name, {"repay_unclaimed_amount_from_salary": 1})
journal_entry = make_journal_entry_for_advance(advance)
journal_entry.submit()
advance.reload()

# set up salary components and structure
create_salary_component("Advance Salary - Deduction", type="Deduction")
make_salary_structure(
"Test Additional Salary for Advance Return",
"Monthly",
employee=employee_name,
company="_Test Company",
)

# create additional salary for advance return
additional_salary = create_return_through_additional_salary(advance)
additional_salary.salary_component = "Advance Salary - Deduction"
additional_salary.payroll_date = nowdate()
additional_salary.amount = 400
additional_salary.insert()
additional_salary.submit()
advance.reload()

self.assertEqual(advance.return_amount, 400)

# create an expense claim
payable_account = get_payable_account("_Test Company")
claim = make_expense_claim(
payable_account, 200, 200, "_Test Company", "Travel Expenses - _TC", do_not_submit=True
)

# link advance to the claim
claim = get_advances_for_claim(claim, advance.name, amount=200)
claim.save()
claim.submit()

# verify the allocation amount
advance = claim.advances[0]
self.assertEqual(
get_allocation_amount(
unclaimed_amount=advance.unclaimed_amount, return_amount=advance.return_amount
),
600,
)

def test_expense_claim_gl_entry(self):
payable_account = get_payable_account(company_name)
taxes = generate_taxes()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"advance_paid",
"column_break_4",
"unclaimed_amount",
"return_amount",
"allocated_amount",
"advance_account"
],
Expand Down Expand Up @@ -84,11 +85,17 @@
{
"fieldname": "column_break_4",
"fieldtype": "Column Break"
},
{
"fieldname": "return_amount",
"fieldtype": "Currency",
"label": "Returned Amount",
"options": "Company:company:default_currency"
}
],
"istable": 1,
"links": [],
"modified": "2024-03-27 13:09:43.954706",
"modified": "2024-11-25 19:53:00.024597",
"modified_by": "Administrator",
"module": "HR",
"name": "Expense Claim Advance",
Expand Down
Loading