diff --git a/hrms/hr/doctype/employee_advance/employee_advance.js b/hrms/hr/doctype/employee_advance/employee_advance.js index 0a88833caf..b3db16b363 100644 --- a/hrms/hr/doctype/employee_advance/employee_advance.js +++ b/hrms/hr/doctype/employee_advance/employee_advance.js @@ -151,25 +151,7 @@ frappe.ui.form.on('Employee Advance', { }, employee: function(frm) { - if (frm.doc.employee) { - frappe.run_serially([ - () => frm.trigger('get_employee_currency'), - () => frm.trigger('get_pending_amount') - ]); - } - }, - - get_pending_amount: function(frm) { - frappe.call({ - method: "hrms.hr.doctype.employee_advance.employee_advance.get_pending_amount", - args: { - "employee": frm.doc.employee, - "posting_date": frm.doc.posting_date - }, - callback: function(r) { - frm.set_value("pending_amount", r.message); - } - }); + if (frm.doc.employee) frm.trigger('get_employee_currency') }, get_employee_currency: function(frm) { diff --git a/hrms/hr/doctype/employee_advance/employee_advance.json b/hrms/hr/doctype/employee_advance/employee_advance.json index f05bc26cef..4e2fae2cc1 100644 --- a/hrms/hr/doctype/employee_advance/employee_advance.json +++ b/hrms/hr/doctype/employee_advance/employee_advance.json @@ -89,6 +89,7 @@ "fieldtype": "Column Break" }, { + "description": "Amount of expense", "fieldname": "advance_amount", "fieldtype": "Currency", "in_list_view": 1, @@ -97,6 +98,7 @@ "reqd": 1 }, { + "description": "Amount that has been paid against this advance", "fieldname": "paid_amount", "fieldtype": "Currency", "label": "Paid Amount", @@ -105,6 +107,7 @@ "read_only": 1 }, { + "description": "Amount claimed via Expense Claim", "fieldname": "claimed_amount", "fieldtype": "Currency", "label": "Claimed Amount", @@ -160,6 +163,7 @@ }, { "allow_on_submit": 1, + "description": "Amount returned by the employee after the advance is paid", "fieldname": "return_amount", "fieldtype": "Currency", "label": "Returned Amount", @@ -175,6 +179,7 @@ }, { "depends_on": "eval:cur_frm.doc.employee", + "description": "Pending (unpaid) amount from previous advances", "fieldname": "pending_amount", "fieldtype": "Currency", "label": "Pending Amount", @@ -202,7 +207,7 @@ ], "is_submittable": 1, "links": [], - "modified": "2024-02-26 14:42:01.853329", + "modified": "2024-04-12 13:53:55.442187", "modified_by": "Administrator", "module": "HR", "name": "Employee Advance", @@ -271,4 +276,4 @@ ], "title_field": "employee_name", "track_changes": 1 -} +} \ No newline at end of file diff --git a/hrms/hr/doctype/employee_advance/employee_advance.py b/hrms/hr/doctype/employee_advance/employee_advance.py index 7d1d67f186..7efee209cf 100644 --- a/hrms/hr/doctype/employee_advance/employee_advance.py +++ b/hrms/hr/doctype/employee_advance/employee_advance.py @@ -27,6 +27,7 @@ def onload(self): def validate(self): validate_active_employee(self.employee) self.set_status() + self.set_pending_amount() def on_cancel(self): self.ignore_linked_doctypes = "GL Entry" @@ -143,15 +144,18 @@ def update_claimed_amount(self): self.reload() self.set_status(update=True) - -@frappe.whitelist() -def get_pending_amount(employee, posting_date): - employee_due_amount = frappe.get_all( - "Employee Advance", - filters={"employee": employee, "docstatus": 1, "posting_date": ("<=", posting_date)}, - fields=["advance_amount", "paid_amount"], - ) - return sum([(emp.advance_amount - emp.paid_amount) for emp in employee_due_amount]) + def set_pending_amount(self): + Advance = frappe.qb.DocType("Employee Advance") + self.pending_amount = ( + frappe.qb.from_(Advance) + .select(Sum(Advance.advance_amount - Advance.paid_amount)) + .where( + (Advance.employee == self.employee) + & (Advance.docstatus == 1) + & (Advance.posting_date <= self.posting_date) + & (Advance.status == "Unpaid") + ) + ).run()[0][0] or 0.0 @frappe.whitelist() diff --git a/hrms/hr/doctype/employee_advance/test_employee_advance.py b/hrms/hr/doctype/employee_advance/test_employee_advance.py index a042f43fd5..109722b3ac 100644 --- a/hrms/hr/doctype/employee_advance/test_employee_advance.py +++ b/hrms/hr/doctype/employee_advance/test_employee_advance.py @@ -258,6 +258,21 @@ def test_precision(self): self.assertEqual(advance.return_amount, 380.66) self.assertEqual(advance.status, "Partly Claimed and Returned") + def test_pending_amount(self): + employee_name = make_employee("_T@employee.advance") + + advance1 = make_employee_advance(employee_name) + make_payment_entry(advance1, 500) + + advance2 = make_employee_advance(employee_name) + # 1000 - 500 + self.assertEqual(advance2.pending_amount, 500) + make_payment_entry(advance2, 700) + + advance3 = make_employee_advance(employee_name) + # (1000 - 500) + (1000 - 700) + self.assertEqual(advance3.pending_amount, 800) + def make_journal_entry_for_advance(advance): journal_entry = frappe.get_doc(make_bank_entry("Employee Advance", advance.name))