From fd1d4c2927d6d397965ad12b9792fb609064e6ea Mon Sep 17 00:00:00 2001 From: Mangesh-Khairnar Date: Mon, 22 Jul 2019 18:05:27 +0530 Subject: [PATCH] chore: split expire allocation --- .../leave_allocation/leave_allocation.js | 7 ++-- .../leave_allocation/leave_allocation.json | 22 ++++++------- .../leave_allocation/leave_allocation.py | 32 +++++++++++++++---- .../leave_allocation/leave_allocation_list.js | 2 +- .../v12_0/generate_leave_ledger_entries.py | 2 +- 5 files changed, 40 insertions(+), 25 deletions(-) diff --git a/erpnext/hr/doctype/leave_allocation/leave_allocation.js b/erpnext/hr/doctype/leave_allocation/leave_allocation.js index 2a26a85fa9..7c3e1e44fa 100755 --- a/erpnext/hr/doctype/leave_allocation/leave_allocation.js +++ b/erpnext/hr/doctype/leave_allocation/leave_allocation.js @@ -22,7 +22,7 @@ frappe.ui.form.on("Leave Allocation", { }, refresh: function(frm) { - if(frm.doc.docstatus === 1 && frm.doc.status === "Active") { + 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); if(valid_expiry) { // expire current allocation @@ -35,11 +35,8 @@ frappe.ui.form.on("Leave Allocation", { expire_allocation: function(frm) { frappe.call({ - method: 'expire_allocation', + method: 'expire_current_allocation', doc: frm.doc, - args: { - current: true - }, freeze: true, callback: function(r){ if(!r.exc){ diff --git a/erpnext/hr/doctype/leave_allocation/leave_allocation.json b/erpnext/hr/doctype/leave_allocation/leave_allocation.json index 41d864d912..1618c67d3d 100644 --- a/erpnext/hr/doctype/leave_allocation/leave_allocation.json +++ b/erpnext/hr/doctype/leave_allocation/leave_allocation.json @@ -24,7 +24,7 @@ "compensatory_request", "leave_period", "leave_policy", - "status", + "expired", "amended_from", "notes", "description" @@ -167,15 +167,6 @@ "options": "Leave Policy", "read_only": 1 }, - { - "fieldname": "status", - "fieldtype": "Select", - "hidden": 1, - "in_standard_filter": 1, - "label": "Status", - "options": "Active\nExpired", - "read_only": 1 - }, { "fieldname": "amended_from", "fieldtype": "Link", @@ -201,12 +192,21 @@ "oldfieldname": "reason", "oldfieldtype": "Small Text", "width": "300px" + }, + { + "default": "0", + "fieldname": "expired", + "fieldtype": "Check", + "hidden": 1, + "in_standard_filter": 1, + "label": "Expired", + "read_only": 1 } ], "icon": "fa fa-ok", "idx": 1, "is_submittable": 1, - "modified": "2019-06-14 15:39:02.898695", + "modified": "2019-07-22 17:50:39.591195", "modified_by": "Administrator", "module": "HR", "name": "Leave Allocation", diff --git a/erpnext/hr/doctype/leave_allocation/leave_allocation.py b/erpnext/hr/doctype/leave_allocation/leave_allocation.py index df479e72e9..494043ff14 100755 --- a/erpnext/hr/doctype/leave_allocation/leave_allocation.py +++ b/erpnext/hr/doctype/leave_allocation/leave_allocation.py @@ -42,7 +42,7 @@ class LeaveAllocation(Document): def on_submit(self): self.create_leave_ledger_entry() - self.expire_allocation() + self.expire_previous_allocation() def on_cancel(self): self.create_leave_ledger_entry(submit=False) @@ -128,14 +128,14 @@ class LeaveAllocation(Document): ) create_leave_ledger_entry(self, args, submit) - def expire_allocation(self, current=False): + def expire_current_allocation(self): ''' expires allocation ''' - date = self.to_date if current else self.from_date + date = self.to_date leaves = get_unused_leaves(self.employee, self.leave_type, date) - ref_name = self.name if current else self.get_previous_allocation() + ref_name = self.name if leaves: - expiry_date = today() if current else add_days(self.from_date, -1) + expiry_date = today() args = dict( leaves=flt(leaves) * -1, transaction_name=ref_name, @@ -146,8 +146,26 @@ class LeaveAllocation(Document): ) create_leave_ledger_entry(self, args) - if current: - frappe.db.set_value("Leave Allocation", self.name, "status", "Expired") + frappe.db.set_value("Leave Allocation", self.name, "expired", 1) + + def expire_previous_allocation(self): + date = self.from_date + leaves = get_unused_leaves(self.employee, self.leave_type, date) + ref_name = self.get_previous_allocation() + + if leaves: + expiry_date = add_days(self.from_date, -1) + args = dict( + leaves=flt(leaves) * -1, + transaction_name=ref_name, + from_date=expiry_date, + to_date=expiry_date, + is_carry_forward=0, + is_expired=1 + ) + create_leave_ledger_entry(self, args) + + frappe.db.set_value("Leave Allocation", ref_name, "expired", 1) def get_previous_allocation(self): return frappe.db.get_value("Leave Allocation", diff --git a/erpnext/hr/doctype/leave_allocation/leave_allocation_list.js b/erpnext/hr/doctype/leave_allocation/leave_allocation_list.js index 3ea0e2403f..93f7b8356b 100644 --- a/erpnext/hr/doctype/leave_allocation/leave_allocation_list.js +++ b/erpnext/hr/doctype/leave_allocation/leave_allocation_list.js @@ -5,7 +5,7 @@ frappe.listview_settings['Leave Allocation'] = { get_indicator: function(doc) { if(doc.status==="Expired") { - return [__("Expired"), "darkgrey", "status, =, Expired"]; + return [__("Expired"), "darkgrey", "expired, =, 1"]; } }, }; diff --git a/erpnext/patches/v12_0/generate_leave_ledger_entries.py b/erpnext/patches/v12_0/generate_leave_ledger_entries.py index 8a92bb3a3d..f2a798e1f8 100644 --- a/erpnext/patches/v12_0/generate_leave_ledger_entries.py +++ b/erpnext/patches/v12_0/generate_leave_ledger_entries.py @@ -54,7 +54,7 @@ def generate_expiry_allocation_ledger_entries(): if not frappe.db.exists("Leave Ledger Entry", {'transaction_type': 'Leave Allocation', 'transaction_name': allocation.name, 'is_expired': 1}): allocation.update(dict(doctype="Leave Allocation")) allocation_obj = frappe.get_doc(allocation) - allocation_obj.expire_allocation() + allocation_obj.expire_previous_allocation() def get_allocation_records(): return frappe.db.sql("""