From 0d72b656ece143511cb7ef4fad64aad722dc3bcc Mon Sep 17 00:00:00 2001 From: krantheman Date: Wed, 28 Feb 2024 15:07:52 +0530 Subject: [PATCH 1/7] refactor: update Pending Amount on validation (cherry picked from commit f7a2d7461e7f55088690671a0a2bb4956a9f89f5) --- .../employee_advance/employee_advance.js | 20 +------------------ .../employee_advance/employee_advance.py | 19 +++++++++--------- 2 files changed, 11 insertions(+), 28 deletions(-) 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.py b/hrms/hr/doctype/employee_advance/employee_advance.py index 7d1d67f186..7145199d62 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,15 @@ 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): + employee_due_amount = frappe.get_all( + "Employee Advance", + filters={"employee": self.employee, "docstatus": 1, "posting_date": ("<=", self.posting_date)}, + fields=["advance_amount", "paid_amount"], + ) + self.pending_amount = sum( + [(emp.advance_amount - emp.paid_amount) for emp in employee_due_amount] + ) @frappe.whitelist() From dd4642a41593b0fab2ea7c715e90e4a6cb2327dc Mon Sep 17 00:00:00 2001 From: krantheman Date: Wed, 28 Feb 2024 15:08:27 +0530 Subject: [PATCH 2/7] feat: add field descriptions (cherry picked from commit 1929b433dfd491af985b9db1151295dd37fb5356) --- hrms/hr/doctype/employee_advance/employee_advance.json | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/hrms/hr/doctype/employee_advance/employee_advance.json b/hrms/hr/doctype/employee_advance/employee_advance.json index f05bc26cef..ff0d3608ec 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 for 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 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-02-28 15:06:33.076761", "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 From 7fd70ba4f019be48ea9b35cd5cc13404614f105b Mon Sep 17 00:00:00 2001 From: krantheman Date: Wed, 28 Feb 2024 15:25:42 +0530 Subject: [PATCH 3/7] perf: better querying (cherry picked from commit 7adb69d7201bb65ecac0633e130a7e08b62c2a30) --- .../employee_advance/employee_advance.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/hrms/hr/doctype/employee_advance/employee_advance.py b/hrms/hr/doctype/employee_advance/employee_advance.py index 7145199d62..059f5627ae 100644 --- a/hrms/hr/doctype/employee_advance/employee_advance.py +++ b/hrms/hr/doctype/employee_advance/employee_advance.py @@ -145,14 +145,16 @@ def update_claimed_amount(self): self.set_status(update=True) def set_pending_amount(self): - employee_due_amount = frappe.get_all( - "Employee Advance", - filters={"employee": self.employee, "docstatus": 1, "posting_date": ("<=", self.posting_date)}, - fields=["advance_amount", "paid_amount"], - ) - self.pending_amount = sum( - [(emp.advance_amount - emp.paid_amount) for emp in employee_due_amount] - ) + 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) + ) + ).run()[0][0] or 0.0 @frappe.whitelist() From ca091e64e82d3a7047685ff46874609be8600efa Mon Sep 17 00:00:00 2001 From: krantheman Date: Mon, 8 Apr 2024 15:02:41 +0530 Subject: [PATCH 4/7] fix: fetch only unpaid advances (cherry picked from commit 75c3fb6662073393d3e25dc6cd23a25cb68e9d8c) # Conflicts: # hrms/hr/doctype/employee_advance/employee_advance.json --- hrms/hr/doctype/employee_advance/employee_advance.json | 4 ++++ hrms/hr/doctype/employee_advance/employee_advance.py | 1 + 2 files changed, 5 insertions(+) diff --git a/hrms/hr/doctype/employee_advance/employee_advance.json b/hrms/hr/doctype/employee_advance/employee_advance.json index ff0d3608ec..8b9f55c112 100644 --- a/hrms/hr/doctype/employee_advance/employee_advance.json +++ b/hrms/hr/doctype/employee_advance/employee_advance.json @@ -207,7 +207,11 @@ ], "is_submittable": 1, "links": [], +<<<<<<< HEAD "modified": "2024-02-28 15:06:33.076761", +======= + "modified": "2024-04-08 13:09:37.272548", +>>>>>>> 75c3fb666 (fix: fetch only unpaid advances) "modified_by": "Administrator", "module": "HR", "name": "Employee Advance", diff --git a/hrms/hr/doctype/employee_advance/employee_advance.py b/hrms/hr/doctype/employee_advance/employee_advance.py index 059f5627ae..7efee209cf 100644 --- a/hrms/hr/doctype/employee_advance/employee_advance.py +++ b/hrms/hr/doctype/employee_advance/employee_advance.py @@ -153,6 +153,7 @@ def set_pending_amount(self): (Advance.employee == self.employee) & (Advance.docstatus == 1) & (Advance.posting_date <= self.posting_date) + & (Advance.status == "Unpaid") ) ).run()[0][0] or 0.0 From 13b3808947db3a6cdbb3f6d24fcadd474f8bc393 Mon Sep 17 00:00:00 2001 From: krantheman Date: Mon, 8 Apr 2024 15:53:13 +0530 Subject: [PATCH 5/7] test: add test_pending_amount (cherry picked from commit 32f2a4c86fafc8539341476de922b388a9e421a9) --- .../employee_advance/test_employee_advance.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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)) From e606ead681b61f421e8262f2f187fe02ea362612 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Fri, 12 Apr 2024 12:54:59 +0530 Subject: [PATCH 6/7] chore: fix casing in doctype descriptions (cherry picked from commit abfe38696665232ba7040a29ce08acab43f0f1ce) # Conflicts: # hrms/hr/doctype/employee_advance/employee_advance.json --- hrms/hr/doctype/employee_advance/employee_advance.json | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/hrms/hr/doctype/employee_advance/employee_advance.json b/hrms/hr/doctype/employee_advance/employee_advance.json index 8b9f55c112..3c4151395d 100644 --- a/hrms/hr/doctype/employee_advance/employee_advance.json +++ b/hrms/hr/doctype/employee_advance/employee_advance.json @@ -98,7 +98,7 @@ "reqd": 1 }, { - "description": "Amount that has been paid for this Advance", + "description": "Amount that has been paid against this advance", "fieldname": "paid_amount", "fieldtype": "Currency", "label": "Paid Amount", @@ -163,7 +163,7 @@ }, { "allow_on_submit": 1, - "description": "Amount returned by the Employee after Advance is paid", + "description": "Amount returned by the employee after the advance is paid", "fieldname": "return_amount", "fieldtype": "Currency", "label": "Returned Amount", @@ -179,7 +179,7 @@ }, { "depends_on": "eval:cur_frm.doc.employee", - "description": "Pending (Unpaid) amount from previous Advances", + "description": "Pending (unpaid) amount from previous advances", "fieldname": "pending_amount", "fieldtype": "Currency", "label": "Pending Amount", @@ -207,11 +207,15 @@ ], "is_submittable": 1, "links": [], +<<<<<<< HEAD <<<<<<< HEAD "modified": "2024-02-28 15:06:33.076761", ======= "modified": "2024-04-08 13:09:37.272548", >>>>>>> 75c3fb666 (fix: fetch only unpaid advances) +======= + "modified": "2024-04-12 12:53:55.442187", +>>>>>>> abfe38696 (chore: fix casing in doctype descriptions) "modified_by": "Administrator", "module": "HR", "name": "Employee Advance", From 4f91aa911d8fddc2a70ca90013592037e4cc1f70 Mon Sep 17 00:00:00 2001 From: krantheman Date: Fri, 12 Apr 2024 15:48:51 +0530 Subject: [PATCH 7/7] fix: merge conflicts --- hrms/hr/doctype/employee_advance/employee_advance.json | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/hrms/hr/doctype/employee_advance/employee_advance.json b/hrms/hr/doctype/employee_advance/employee_advance.json index 3c4151395d..4e2fae2cc1 100644 --- a/hrms/hr/doctype/employee_advance/employee_advance.json +++ b/hrms/hr/doctype/employee_advance/employee_advance.json @@ -207,15 +207,7 @@ ], "is_submittable": 1, "links": [], -<<<<<<< HEAD -<<<<<<< HEAD - "modified": "2024-02-28 15:06:33.076761", -======= - "modified": "2024-04-08 13:09:37.272548", ->>>>>>> 75c3fb666 (fix: fetch only unpaid advances) -======= - "modified": "2024-04-12 12:53:55.442187", ->>>>>>> abfe38696 (chore: fix casing in doctype descriptions) + "modified": "2024-04-12 13:53:55.442187", "modified_by": "Administrator", "module": "HR", "name": "Employee Advance",