From bd999b09089c9f5cea8307cfe18a12d8b0f1952a Mon Sep 17 00:00:00 2001 From: Mangesh-Khairnar Date: Fri, 21 Jun 2019 00:49:48 +0530 Subject: [PATCH] fix: minor changes --- .../leave_allocation/leave_allocation.js | 15 +++-------- .../leave_allocation/leave_allocation.py | 15 ++++++++++- .../leave_application/leave_application.js | 3 ++- .../leave_application/leave_application.py | 23 +++++++++-------- .../test_leave_application.py | 2 +- .../leave_ledger_entry.json | 2 +- .../leave_ledger_entry/leave_ledger_entry.py | 25 ++++++++++++++++--- 7 files changed, 55 insertions(+), 30 deletions(-) diff --git a/erpnext/hr/doctype/leave_allocation/leave_allocation.js b/erpnext/hr/doctype/leave_allocation/leave_allocation.js index a620d9e081..8f734ac1bc 100755 --- a/erpnext/hr/doctype/leave_allocation/leave_allocation.js +++ b/erpnext/hr/doctype/leave_allocation/leave_allocation.js @@ -94,19 +94,10 @@ frappe.ui.form.on("Leave Allocation", { calculate_total_leaves_allocated: function(frm) { if (cint(frm.doc.carry_forward) == 1 && frm.doc.leave_type && frm.doc.employee) { return frappe.call({ - method: "erpnext.hr.doctype.leave_allocation.leave_allocation.get_carry_forwarded_leaves", - args: { - "employee": frm.doc.employee, - "date": frm.doc.from_date, - "leave_type": frm.doc.leave_type, - "carry_forward": frm.doc.carry_forward - }, + method: "set_total_leaves_allocated", + doc: frm.doc, callback: function(r) { - if (!r.exc && r.message) { - frm.set_value('carry_forwarded_leaves', r.message); - frm.set_value("total_leaves_allocated", - flt(r.message) + flt(frm.doc.new_leaves_allocated)); - } + frm.refresh_fields(); } }) } else if (cint(frm.doc.carry_forward) == 0) { diff --git a/erpnext/hr/doctype/leave_allocation/leave_allocation.py b/erpnext/hr/doctype/leave_allocation/leave_allocation.py index 72ea2733b0..df479e72e9 100755 --- a/erpnext/hr/doctype/leave_allocation/leave_allocation.py +++ b/erpnext/hr/doctype/leave_allocation/leave_allocation.py @@ -115,7 +115,7 @@ class LeaveAllocation(Document): args = dict( leaves=self.carry_forwarded_leaves, from_date=self.from_date, - to_date=add_days(self.from_date, expiry_days) if expiry_days else self.to_date, + to_date=add_days(self.from_date, expiry_days - 1) if expiry_days else self.to_date, is_carry_forward=1 ) create_leave_ledger_entry(self, args, submit) @@ -132,11 +132,13 @@ class LeaveAllocation(Document): ''' expires allocation ''' date = self.to_date if current else self.from_date leaves = get_unused_leaves(self.employee, self.leave_type, date) + ref_name = self.name if current else self.get_previous_allocation() if leaves: expiry_date = today() if current else 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, @@ -147,6 +149,17 @@ class LeaveAllocation(Document): if current: frappe.db.set_value("Leave Allocation", self.name, "status", "Expired") + def get_previous_allocation(self): + return frappe.db.get_value("Leave Allocation", + filters={ + 'to_date': ("<", self.from_date), + 'leave_type': self.leave_type, + 'employee': self.employee, + 'docstatus': 1 + }, + order_by='to_date DESC', + fieldname=['name']) + def get_leave_allocation_for_period(employee, leave_type, from_date, to_date): leave_allocated = 0 leave_allocations = frappe.db.sql(""" diff --git a/erpnext/hr/doctype/leave_application/leave_application.js b/erpnext/hr/doctype/leave_application/leave_application.js index b81e615b72..44a60b0259 100755 --- a/erpnext/hr/doctype/leave_application/leave_application.js +++ b/erpnext/hr/doctype/leave_application/leave_application.js @@ -49,7 +49,7 @@ frappe.ui.form.on("Leave Application", { async: false, args: { employee: frm.doc.employee, - date: frm.doc.posting_date + date: frm.doc.from_date? frm.doc.from_date:frm.doc.posting_date }, callback: function(r) { if (!r.exc && r.message['leave_allocation']) { @@ -124,6 +124,7 @@ frappe.ui.form.on("Leave Application", { }, from_date: function(frm) { + frm.trigger("make_dashboard"); frm.trigger("half_day_datepicker"); frm.trigger("calculate_total_days"); }, diff --git a/erpnext/hr/doctype/leave_application/leave_application.py b/erpnext/hr/doctype/leave_application/leave_application.py index d08c9edb1f..7aa3e95862 100755 --- a/erpnext/hr/doctype/leave_application/leave_application.py +++ b/erpnext/hr/doctype/leave_application/leave_application.py @@ -417,9 +417,9 @@ def get_leave_details(employee, date): allocation = allocation_records.get(d, frappe._dict()) remaining_leaves = get_leave_balance_on(employee, d, date, to_date = allocation.to_date, consider_all_leaves_in_the_allocation_period=True) - date = allocation.to_date - leaves_taken = get_leaves_for_period(employee, d, allocation.from_date, date) * -1 - leaves_pending = get_pending_leaves_for_period(employee, d, allocation.from_date, date) + end_date = allocation.to_date + leaves_taken = get_leaves_for_period(employee, d, allocation.from_date, end_date) * -1 + leaves_pending = get_pending_leaves_for_period(employee, d, allocation.from_date, end_date) leave_allocation[d] = { "total_leaves": allocation.total_leaves_allocated, @@ -435,14 +435,17 @@ def get_leave_details(employee, date): return ret @frappe.whitelist() -def get_leave_balance_on(employee, leave_type, date, to_date=nowdate(), allocation_records=None, - consider_all_leaves_in_the_allocation_period=False): - ''' Returns leave balance till date and fetches expiry date based on to_date - to calculate minimum remaining leave balance ''' - - if not allocation_records: - allocation_records = get_leave_allocation_records(employee, date, leave_type) +def get_leave_balance_on(employee, leave_type, date, to_date=nowdate(), consider_all_leaves_in_the_allocation_period=False): + ''' + Returns leave balance on date + :param employee: employee name + :param leave_type: leave type + :param date: date to check balance on + :param to_date: future date to check for allocation expiry + :param consider_all_leaves_in_the_allocation_period: consider all leaves taken till the allocation end date + ''' + allocation_records = get_leave_allocation_records(employee, date, leave_type) allocation = allocation_records.get(leave_type, frappe._dict()) end_date = allocation.to_date if consider_all_leaves_in_the_allocation_period else date diff --git a/erpnext/hr/doctype/leave_application/test_leave_application.py b/erpnext/hr/doctype/leave_application/test_leave_application.py index 54216ee3c7..ca6b99ca12 100644 --- a/erpnext/hr/doctype/leave_application/test_leave_application.py +++ b/erpnext/hr/doctype/leave_application/test_leave_application.py @@ -557,7 +557,7 @@ def create_carry_forwarded_allocation(employee, leave_type): leave_type="_Test_CF_leave_expiry", employee=employee.name, employee_name=employee.employee_name, - from_date=add_days(nowdate(), -85), + from_date=add_days(nowdate(), -84), to_date=add_days(nowdate(), 100), carry_forward=1) leave_allocation.submit() diff --git a/erpnext/hr/doctype/leave_ledger_entry/leave_ledger_entry.json b/erpnext/hr/doctype/leave_ledger_entry/leave_ledger_entry.json index 0166e43c0f..c11422211c 100644 --- a/erpnext/hr/doctype/leave_ledger_entry/leave_ledger_entry.json +++ b/erpnext/hr/doctype/leave_ledger_entry/leave_ledger_entry.json @@ -101,7 +101,7 @@ ], "in_create": 1, "is_submittable": 1, - "modified": "2019-06-06 20:33:37.531161", + "modified": "2019-06-21 00:37:07.782810", "modified_by": "Administrator", "module": "HR", "name": "Leave Ledger Entry", diff --git a/erpnext/hr/doctype/leave_ledger_entry/leave_ledger_entry.py b/erpnext/hr/doctype/leave_ledger_entry/leave_ledger_entry.py index 6142dcf648..a73f10adb8 100644 --- a/erpnext/hr/doctype/leave_ledger_entry/leave_ledger_entry.py +++ b/erpnext/hr/doctype/leave_ledger_entry/leave_ledger_entry.py @@ -6,7 +6,7 @@ from __future__ import unicode_literals import frappe from frappe.model.document import Document from frappe import _ -from frappe.utils import add_days, today, flt +from frappe.utils import add_days, today, flt, DATE_FORMAT class LeaveLedgerEntry(Document): def on_cancel(self): @@ -49,14 +49,31 @@ def create_leave_ledger_entry(ref_doc, args, submit=True): def delete_ledger_entry(ledger): ''' Delete ledger entry on cancel of leave application/allocation/encashment ''' - if ledger.transaction_type == "Leave Allocation": validate_leave_allocation_against_leave_application(ledger) + expired_entry = get_previous_expiry_ledger_entry(ledger) frappe.db.sql("""DELETE FROM `tabLeave Ledger Entry` WHERE - `transaction_name`=%s""", (ledger.transaction_name)) + `transaction_name`=%s + OR `name`=%s""", (ledger.transaction_name, expired_entry)) + +def get_previous_expiry_ledger_entry(ledger): + ''' Returns the expiry ledger entry having same creation date as the ledger entry to be cancelled ''' + creation_date = frappe.db.get_value("Leave Ledger Entry", filters={ + 'transaction_name': ledger.transaction_name, + 'is_expired': 0 + }, fieldname=['creation']).strftime(DATE_FORMAT) + + return frappe.db.get_value("Leave Ledger Entry", filters={ + 'creation': ('like', creation_date+"%"), + 'employee': ledger.employee, + 'leave_type': ledger.leave_type, + 'is_expired': 1, + 'docstatus': 1, + 'is_carry_forward': 0 + }, fieldname=['name']) def process_expired_allocation(): ''' Check if a carry forwarded allocation has expired and create a expiry ledger entry ''' @@ -70,7 +87,7 @@ def process_expired_allocation(): leave_type = [record[0] for record in leave_type_records] expired_allocation = frappe.get_all("Leave Ledger Entry", filters={ - 'to_date': today(), + 'to_date': add_days(today(), -1), 'transaction_type': 'Leave Allocation', 'is_carry_forward': 1, 'leave_type': ('in', leave_type)