From ee53c82ba43cdd16033d57d23700dadb596a2a57 Mon Sep 17 00:00:00 2001 From: Aysha Date: Tue, 5 Nov 2024 19:10:49 +0530 Subject: [PATCH 1/7] fix: exclude advances scheduled for deduction when adjusting them against expense claims --- .../hr/doctype/expense_claim/expense_claim.py | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/hrms/hr/doctype/expense_claim/expense_claim.py b/hrms/hr/doctype/expense_claim/expense_claim.py index 04a02ba5d1..a254697467 100644 --- a/hrms/hr/doctype/expense_claim/expense_claim.py +++ b/hrms/hr/doctype/expense_claim/expense_claim.py @@ -490,6 +490,7 @@ def get_expense_claim_account(expense_claim_type, company): @frappe.whitelist() def get_advances(employee, advance_id=None): advance = frappe.qb.DocType("Employee Advance") + additional_salary = frappe.qb.DocType("Additional Salary") query = frappe.qb.from_(advance).select( advance.name, @@ -510,7 +511,24 @@ def get_advances(employee, advance_id=None): else: query = query.where(advance.name == advance_id) - return query.run(as_dict=True) + advances = query.run(as_dict=True) + for adv in advances: + # Query additional salary for deductions against this advance + deduction_query = ( + frappe.qb.from_(additional_salary) + .select( + additional_salary.ref_docname, + (additional_salary.amount).as_("deducted_amount"), + ) + .where( + (additional_salary.ref_docname == adv["name"]) + & (additional_salary.type == "Deduction") + & (additional_salary.docstatus == 1) + ) + ) + deductions_result = deduction_query.run(as_dict=True) + adv["claimed_amount"] += sum(item["deducted_amount"] for item in deductions_result) or 0.0 + return advances @frappe.whitelist() From e45718d2ea4d3c18c48a57da592df395db46c335 Mon Sep 17 00:00:00 2001 From: Aysha Date: Tue, 5 Nov 2024 20:33:29 +0530 Subject: [PATCH 2/7] fix: exclude claimed and returned advance amounts --- .../employee_advance/employee_advance.js | 1 + .../hr/doctype/expense_claim/expense_claim.js | 3 ++- .../hr/doctype/expense_claim/expense_claim.py | 25 +++---------------- 3 files changed, 7 insertions(+), 22 deletions(-) diff --git a/hrms/hr/doctype/employee_advance/employee_advance.js b/hrms/hr/doctype/employee_advance/employee_advance.js index 54f9a117ee..16ac8ea3a2 100644 --- a/hrms/hr/doctype/employee_advance/employee_advance.js +++ b/hrms/hr/doctype/employee_advance/employee_advance.js @@ -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, + returned_amount: frm.doc.return_amount, }, callback: function (r) { const doclist = frappe.model.sync(r.message); diff --git a/hrms/hr/doctype/expense_claim/expense_claim.js b/hrms/hr/doctype/expense_claim/expense_claim.js index 0f826807bd..68fcd47de6 100644 --- a/hrms/hr/doctype/expense_claim/expense_claim.js +++ b/hrms/hr/doctype/expense_claim/expense_claim.js @@ -308,7 +308,8 @@ frappe.ui.form.on("Expense Claim", { row.posting_date = d.posting_date; row.advance_account = d.advance_account; row.advance_paid = d.paid_amount; - row.unclaimed_amount = flt(d.paid_amount) - flt(d.claimed_amount); + row.unclaimed_amount = + flt(d.paid_amount) - flt(d.claimed_amount) - flt(d.return_amount); row.allocated_amount = 0; }); refresh_field("advances"); diff --git a/hrms/hr/doctype/expense_claim/expense_claim.py b/hrms/hr/doctype/expense_claim/expense_claim.py index a254697467..0501b212f8 100644 --- a/hrms/hr/doctype/expense_claim/expense_claim.py +++ b/hrms/hr/doctype/expense_claim/expense_claim.py @@ -490,7 +490,6 @@ def get_expense_claim_account(expense_claim_type, company): @frappe.whitelist() def get_advances(employee, advance_id=None): advance = frappe.qb.DocType("Employee Advance") - additional_salary = frappe.qb.DocType("Additional Salary") query = frappe.qb.from_(advance).select( advance.name, @@ -498,6 +497,7 @@ def get_advances(employee, advance_id=None): advance.posting_date, advance.paid_amount, advance.claimed_amount, + advance.return_amount, advance.advance_account, ) @@ -511,29 +511,12 @@ def get_advances(employee, advance_id=None): else: query = query.where(advance.name == advance_id) - advances = query.run(as_dict=True) - for adv in advances: - # Query additional salary for deductions against this advance - deduction_query = ( - frappe.qb.from_(additional_salary) - .select( - additional_salary.ref_docname, - (additional_salary.amount).as_("deducted_amount"), - ) - .where( - (additional_salary.ref_docname == adv["name"]) - & (additional_salary.type == "Deduction") - & (additional_salary.docstatus == 1) - ) - ) - deductions_result = deduction_query.run(as_dict=True) - adv["claimed_amount"] += sum(item["deducted_amount"] for item in deductions_result) or 0.0 - return advances + return query.run(as_dict=True) @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, returned_amount ): default_payable_account = frappe.get_cached_value( "Company", company, "default_expense_claim_payable_account" @@ -552,7 +535,7 @@ def get_expense_claim( "employee_advance": employee_advance_name, "posting_date": posting_date, "advance_paid": flt(paid_amount), - "unclaimed_amount": flt(paid_amount) - flt(claimed_amount), + "unclaimed_amount": flt(paid_amount) - flt(claimed_amount) - flt(returned_amount), "allocated_amount": flt(paid_amount) - flt(claimed_amount), }, ) From 61fb3b60880a02ef2b4de59d48e4a63827381e80 Mon Sep 17 00:00:00 2001 From: Aysha Date: Fri, 8 Nov 2024 21:42:54 +0530 Subject: [PATCH 3/7] fix: claim allocation issue --- .../doctype/employee_advance/employee_advance.js | 2 +- hrms/hr/doctype/expense_claim/expense_claim.js | 14 ++++++++------ hrms/hr/doctype/expense_claim/expense_claim.py | 9 +++++---- .../expense_claim_advance.json | 9 ++++++++- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/hrms/hr/doctype/employee_advance/employee_advance.js b/hrms/hr/doctype/employee_advance/employee_advance.js index 16ac8ea3a2..1764d2d922 100644 --- a/hrms/hr/doctype/employee_advance/employee_advance.js +++ b/hrms/hr/doctype/employee_advance/employee_advance.js @@ -138,7 +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, - returned_amount: frm.doc.return_amount, + return_amount: frm.doc.return_amount, }, callback: function (r) { const doclist = frappe.model.sync(r.message); diff --git a/hrms/hr/doctype/expense_claim/expense_claim.js b/hrms/hr/doctype/expense_claim/expense_claim.js index 68fcd47de6..266cdbaadc 100644 --- a/hrms/hr/doctype/expense_claim/expense_claim.js +++ b/hrms/hr/doctype/expense_claim/expense_claim.js @@ -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; @@ -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) { @@ -308,8 +308,8 @@ frappe.ui.form.on("Expense Claim", { row.posting_date = d.posting_date; row.advance_account = d.advance_account; row.advance_paid = d.paid_amount; - row.unclaimed_amount = - flt(d.paid_amount) - flt(d.claimed_amount) - flt(d.return_amount); + row.unclaimed_amount = flt(d.paid_amount) - flt(d.claimed_amount); + row.return_amount = flt(d.return_amount); row.allocated_amount = 0; }); refresh_field("advances"); @@ -386,8 +386,10 @@ 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.return_amount = flt(r.message[0].return_amount); child.allocated_amount = - flt(r.message[0].paid_amount) - flt(r.message[0].claimed_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"); } diff --git a/hrms/hr/doctype/expense_claim/expense_claim.py b/hrms/hr/doctype/expense_claim/expense_claim.py index 0501b212f8..1866f37bf7 100644 --- a/hrms/hr/doctype/expense_claim/expense_claim.py +++ b/hrms/hr/doctype/expense_claim/expense_claim.py @@ -325,7 +325,7 @@ 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 @@ -516,7 +516,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, returned_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" @@ -535,8 +535,9 @@ def get_expense_claim( "employee_advance": employee_advance_name, "posting_date": posting_date, "advance_paid": flt(paid_amount), - "unclaimed_amount": flt(paid_amount) - flt(claimed_amount) - flt(returned_amount), - "allocated_amount": flt(paid_amount) - flt(claimed_amount), + "unclaimed_amount": flt(paid_amount) - flt(claimed_amount), + "allocated_amount": flt(paid_amount) - (flt(claimed_amount) + flt(return_amount)), + "return_amount": flt(return_amount), }, ) diff --git a/hrms/hr/doctype/expense_claim_advance/expense_claim_advance.json b/hrms/hr/doctype/expense_claim_advance/expense_claim_advance.json index caba3afe53..dcaf0afecb 100644 --- a/hrms/hr/doctype/expense_claim_advance/expense_claim_advance.json +++ b/hrms/hr/doctype/expense_claim_advance/expense_claim_advance.json @@ -11,6 +11,7 @@ "advance_paid", "column_break_4", "unclaimed_amount", + "return_amount", "allocated_amount", "advance_account" ], @@ -84,11 +85,17 @@ { "fieldname": "column_break_4", "fieldtype": "Column Break" + }, + { + "fieldname": "return_amount", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Returned Amount" } ], "istable": 1, "links": [], - "modified": "2024-03-27 13:09:43.954706", + "modified": "2024-11-07 13:00:58.402981", "modified_by": "Administrator", "module": "HR", "name": "Expense Claim Advance", From 92ff1b70be6a8db58b0e65e2f3442b59917addb2 Mon Sep 17 00:00:00 2001 From: Aysha Date: Fri, 15 Nov 2024 15:33:49 +0530 Subject: [PATCH 4/7] chore: add test --- .../hr/doctype/expense_claim/expense_claim.js | 4 +- .../expense_claim/test_expense_claim.py | 48 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/hrms/hr/doctype/expense_claim/expense_claim.js b/hrms/hr/doctype/expense_claim/expense_claim.js index 266cdbaadc..153eda5dbc 100644 --- a/hrms/hr/doctype/expense_claim/expense_claim.js +++ b/hrms/hr/doctype/expense_claim/expense_claim.js @@ -310,7 +310,9 @@ frappe.ui.form.on("Expense Claim", { row.advance_paid = d.paid_amount; row.unclaimed_amount = flt(d.paid_amount) - flt(d.claimed_amount); row.return_amount = flt(d.return_amount); - row.allocated_amount = 0; + row.allocated_amount = + flt(d.paid_amount) - + (flt(d.claimed_amount) + flt(d.return_amount)); }); refresh_field("advances"); } diff --git a/hrms/hr/doctype/expense_claim/test_expense_claim.py b/hrms/hr/doctype/expense_claim/test_expense_claim.py index 4094932b91..ec05e40026 100644 --- a/hrms/hr/doctype/expense_claim/test_expense_claim.py +++ b/hrms/hr/doctype/expense_claim/test_expense_claim.py @@ -253,6 +253,54 @@ 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, + make_employee_advance, + make_journal_entry_for_advance, + ) + from hrms.hr.doctype.expense_claim.expense_claim import get_advances + 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 + + employee_name = make_employee("_T@employee.advance", "_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() + + args = {"type": "Deduction"} + create_salary_component("Advance Salary - Deduction", **args) + make_salary_structure( + "Test Additional Salary for Advance Return", + "Monthly", + employee=employee_name, + company="_Test Company", + ) + + advance.reload() + 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) + + payable_account = get_payable_account("_Test Company") + claim = make_expense_claim( + payable_account, 200, 200, "_Test Company", "Travel Expenses - _TC", do_not_submit=True + ) + advances = get_advances(claim.employee) + for entry in advances: + if entry.name == advance.name: + self.assertTrue(entry.return_amount, 400) + self.assertTrue( + entry.allocated_amount, + advance.paid_amount - (advance.claimed_amount + advance.return_amount), + ) + def test_expense_claim_gl_entry(self): payable_account = get_payable_account(company_name) taxes = generate_taxes() From bc369942dbfd3d043bca9bf5efb163bdd301c50e Mon Sep 17 00:00:00 2001 From: Aysha Date: Mon, 25 Nov 2024 19:42:11 +0530 Subject: [PATCH 5/7] chore: add get_allocation_amount function --- .../hr/doctype/expense_claim/expense_claim.js | 20 +++++++++++++------ .../hr/doctype/expense_claim/expense_claim.py | 9 ++++++++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/hrms/hr/doctype/expense_claim/expense_claim.js b/hrms/hr/doctype/expense_claim/expense_claim.js index 153eda5dbc..0b8f522d82 100644 --- a/hrms/hr/doctype/expense_claim/expense_claim.js +++ b/hrms/hr/doctype/expense_claim/expense_claim.js @@ -310,9 +310,11 @@ frappe.ui.form.on("Expense Claim", { row.advance_paid = d.paid_amount; row.unclaimed_amount = flt(d.paid_amount) - flt(d.claimed_amount); row.return_amount = flt(d.return_amount); - row.allocated_amount = - flt(d.paid_amount) - - (flt(d.claimed_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"); } @@ -389,9 +391,11 @@ frappe.ui.form.on("Expense Claim Advance", { child.unclaimed_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 = - flt(r.message[0].paid_amount) - - (flt(r.message[0].claimed_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"); } @@ -437,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); +} diff --git a/hrms/hr/doctype/expense_claim/expense_claim.py b/hrms/hr/doctype/expense_claim/expense_claim.py index 1866f37bf7..8f5009af6a 100644 --- a/hrms/hr/doctype/expense_claim/expense_claim.py +++ b/hrms/hr/doctype/expense_claim/expense_claim.py @@ -536,7 +536,9 @@ 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) + flt(return_amount)), + "allocated_amount": get_allocation_amount( + flt(paid_amount), flt(claimed_amount), flt(return_amount) + ), "return_amount": flt(return_amount), }, ) @@ -594,3 +596,8 @@ def make_expense_claim_for_delivery_trip(source_name, target_doc=None): ) return doc + + +@frappe.whitelist() +def get_allocation_amount(paid_amount, claimed_amount, return_amount): + return paid_amount - (claimed_amount + return_amount) From 0bc9307cd4a06523f9d92081793a46819d9c3ac0 Mon Sep 17 00:00:00 2001 From: Aysha Date: Mon, 25 Nov 2024 19:54:40 +0530 Subject: [PATCH 6/7] chore: return_amount field in advances child table --- .../expense_claim_advance/expense_claim_advance.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/hrms/hr/doctype/expense_claim_advance/expense_claim_advance.json b/hrms/hr/doctype/expense_claim_advance/expense_claim_advance.json index dcaf0afecb..06317de980 100644 --- a/hrms/hr/doctype/expense_claim_advance/expense_claim_advance.json +++ b/hrms/hr/doctype/expense_claim_advance/expense_claim_advance.json @@ -88,14 +88,14 @@ }, { "fieldname": "return_amount", - "fieldtype": "Data", - "in_list_view": 1, - "label": "Returned Amount" + "fieldtype": "Currency", + "label": "Returned Amount", + "options": "Company:company:default_currency" } ], "istable": 1, "links": [], - "modified": "2024-11-07 13:00:58.402981", + "modified": "2024-11-25 19:53:00.024597", "modified_by": "Administrator", "module": "HR", "name": "Expense Claim Advance", From a519fb3ccbaa4bac81324bfb046e9386cd14cc38 Mon Sep 17 00:00:00 2001 From: Aysha Date: Tue, 26 Nov 2024 15:19:09 +0530 Subject: [PATCH 7/7] fix: fix test --- .../employee_advance/test_employee_advance.py | 11 ++++-- .../hr/doctype/expense_claim/expense_claim.py | 21 ++++++++--- .../expense_claim/test_expense_claim.py | 36 ++++++++++++------- 3 files changed, 48 insertions(+), 20 deletions(-) diff --git a/hrms/hr/doctype/employee_advance/test_employee_advance.py b/hrms/hr/doctype/employee_advance/test_employee_advance.py index a29fc2ba01..9b50e83926 100644 --- a/hrms/hr/doctype/employee_advance/test_employee_advance.py +++ b/hrms/hr/doctype/employee_advance/test_employee_advance.py @@ -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, @@ -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", @@ -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, }, ) diff --git a/hrms/hr/doctype/expense_claim/expense_claim.py b/hrms/hr/doctype/expense_claim/expense_claim.py index 8f5009af6a..24f514e80c 100644 --- a/hrms/hr/doctype/expense_claim/expense_claim.py +++ b/hrms/hr/doctype/expense_claim/expense_claim.py @@ -333,7 +333,9 @@ def validate_advances(self): 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 @@ -537,7 +539,7 @@ def get_expense_claim( "advance_paid": flt(paid_amount), "unclaimed_amount": flt(paid_amount) - flt(claimed_amount), "allocated_amount": get_allocation_amount( - flt(paid_amount), flt(claimed_amount), flt(return_amount) + paid_amount=(paid_amount), claimed_amount=(claimed_amount), return_amount=(return_amount) ), "return_amount": flt(return_amount), }, @@ -598,6 +600,17 @@ 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, claimed_amount, return_amount): - return paid_amount - (claimed_amount + return_amount) +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.")) diff --git a/hrms/hr/doctype/expense_claim/test_expense_claim.py b/hrms/hr/doctype/expense_claim/test_expense_claim.py index ec05e40026..d37e2c7bde 100644 --- a/hrms/hr/doctype/expense_claim/test_expense_claim.py +++ b/hrms/hr/doctype/expense_claim/test_expense_claim.py @@ -256,20 +256,23 @@ def test_expense_claim_partially_paid_via_advance(self): 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_advances + 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("_T@employee.advance", "_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() - args = {"type": "Deduction"} - create_salary_component("Advance Salary - Deduction", **args) + # set up salary components and structure + create_salary_component("Advance Salary - Deduction", type="Deduction") make_salary_structure( "Test Additional Salary for Advance Return", "Monthly", @@ -277,29 +280,36 @@ def test_expense_claim_with_deducted_returned_advance(self): company="_Test Company", ) - advance.reload() + # 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 ) - advances = get_advances(claim.employee) - for entry in advances: - if entry.name == advance.name: - self.assertTrue(entry.return_amount, 400) - self.assertTrue( - entry.allocated_amount, - advance.paid_amount - (advance.claimed_amount + advance.return_amount), - ) + + # 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)