fix: minor changes

This commit is contained in:
Mangesh-Khairnar 2019-06-21 00:49:48 +05:30
parent 439313e524
commit bd999b0908
7 changed files with 55 additions and 30 deletions

View File

@ -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) {

View File

@ -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("""

View File

@ -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");
},

View File

@ -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

View File

@ -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()

View File

@ -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",

View File

@ -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)