Skip to content

Commit

Permalink
fix: expire_allocation api endpoint by fetching allocation object (ba…
Browse files Browse the repository at this point in the history
…ckport #1576) (#2194)

* fix: expire_allocation api endpoint by fetching allocation object (#1576)

* fix: expire_allocation api endpoint by fetching allocation object

* fix: add a check to fetch allocation only if the allocation type is str in the expire_allocation endpoint

* feat: add a test case test_expire_allocation for leave ledger entry doctype

(cherry picked from commit 6ddfdac)

# Conflicts:
#	hrms/hr/doctype/leave_allocation/leave_allocation.js

* chore: fix conflicts

---------

Co-authored-by: zeel prajapati <[email protected]>
Co-authored-by: Rucha Mahabal <[email protected]>
  • Loading branch information
3 people committed Sep 16, 2024
1 parent 51cb889 commit 8e8380e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion hrms/hr/doctype/leave_allocation/leave_allocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ frappe.ui.form.on("Leave Allocation", {
},

refresh: function (frm) {
if (frm.doc.docstatus === 1 && frm.doc.expired) {
if (frm.doc.docstatus === 1 && !frm.doc.expired) {
var valid_expiry = moment(frappe.datetime.get_today()).isBetween(
frm.doc.from_date,
frm.doc.to_date,
Expand Down
7 changes: 6 additions & 1 deletion hrms/hr/doctype/leave_ledger_entry/leave_ledger_entry.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Copyright (c) 2019, Frappe Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt


import frappe
from frappe import _
from frappe.model.document import Document
Expand Down Expand Up @@ -185,6 +184,12 @@ def get_remaining_leaves(allocation):
@frappe.whitelist()
def expire_allocation(allocation, expiry_date=None):
"""expires non-carry forwarded allocation"""
import json

if isinstance(allocation, str):
allocation = json.loads(allocation)
allocation = frappe.get_doc("Leave Allocation", allocation["name"])

leaves = get_remaining_leaves(allocation)
expiry_date = expiry_date if expiry_date else allocation.to_date

Expand Down
39 changes: 37 additions & 2 deletions hrms/hr/doctype/leave_ledger_entry/test_leave_ledger_entry.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,44 @@
# Copyright (c) 2019, Frappe Technologies Pvt. Ltd. and Contributors
# See license.txt

# import frappe
import frappe
from frappe.tests.utils import FrappeTestCase
from frappe.utils.data import add_to_date, today

from erpnext.setup.doctype.employee.test_employee import make_employee

from hrms.hr.doctype.leave_ledger_entry.leave_ledger_entry import expire_allocation


class TestLeaveLedgerEntry(FrappeTestCase):
pass
def setUp(self):
emp_id = make_employee("[email protected]", company="_Test Company")
self.employee = frappe.get_doc("Employee", emp_id)

def test_expire_allocation(self):
import json

allocation = {
"doctype": "Leave Allocation",
"__islocal": 1,
"employee": self.employee.name,
"employee_name": self.employee.employee_name,
"leave_type": "_Test Leave Type",
"from_date": today(),
"to_date": add_to_date(today(), days=30),
"new_leaves_allocated": 5,
"docstatus": 1,
}

allocation = frappe.get_doc(allocation).save()

expire_allocation(json.dumps(allocation.as_dict()))
allocation.reload()

self.assertEqual(allocation.expired, 1)

def tearDown(self):
frappe.db.rollback()


test_dependencies = ["Employee", "Leave Type"]

0 comments on commit 8e8380e

Please sign in to comment.