fix: allow creation of additional leave ledger entry

This commit is contained in:
Mangesh-Khairnar
2019-12-17 12:25:25 +05:30
parent ce6923ecb6
commit b4d9b80fed
4 changed files with 36 additions and 22 deletions

View File

@@ -69,10 +69,14 @@ class LeaveAllocation(Document):
def validate_allocation_overlap(self): def validate_allocation_overlap(self):
leave_allocation = frappe.db.sql(""" leave_allocation = frappe.db.sql("""
select name from `tabLeave Allocation` SELECT
where employee=%s and leave_type=%s and docstatus=1 name
and to_date >= %s and from_date <= %s""", FROM `tabLeave Allocation`
(self.employee, self.leave_type, self.from_date, self.to_date)) WHERE
employee=%s AND leave_type=%s
AND name <> %s AND docstatus=1
AND to_date >= %s AND from_date <= %s""",
(self.employee, self.leave_type, self.name, self.from_date, self.to_date))
if leave_allocation: if leave_allocation:
frappe.msgprint(_("{0} already allocated for Employee {1} for period {2} to {3}") frappe.msgprint(_("{0} already allocated for Employee {1} for period {2} to {3}")

View File

@@ -549,10 +549,10 @@ def get_leaves_for_period(employee, leave_type, from_date, to_date):
leave_days += leave_entry.leaves leave_days += leave_entry.leaves
elif inclusive_period and leave_entry.transaction_type == 'Leave Allocation' \ elif inclusive_period and leave_entry.transaction_type == 'Leave Allocation' \
and not skip_expiry_leaves(leave_entry, to_date): and leave_entry.is_expired and not skip_expiry_leaves(leave_entry, to_date):
leave_days += leave_entry.leaves leave_days += leave_entry.leaves
else: elif leave_entry.transaction_type == 'Leave Application':
if leave_entry.from_date < getdate(from_date): if leave_entry.from_date < getdate(from_date):
leave_entry.from_date = from_date leave_entry.from_date = from_date
if leave_entry.to_date > getdate(to_date): if leave_entry.to_date > getdate(to_date):
@@ -579,14 +579,15 @@ def skip_expiry_leaves(leave_entry, date):
def get_leave_entries(employee, leave_type, from_date, to_date): def get_leave_entries(employee, leave_type, from_date, to_date):
''' Returns leave entries between from_date and to_date ''' ''' Returns leave entries between from_date and to_date '''
return frappe.db.sql(""" return frappe.db.sql("""
select employee, leave_type, from_date, to_date, leaves, transaction_type, is_carry_forward, transaction_name SELECT
from `tabLeave Ledger Entry` employee, leave_type, from_date, to_date, leaves, transaction_name, transaction_type,
where employee=%(employee)s and leave_type=%(leave_type)s is_carry_forward, is_expired
and docstatus=1 FROM `tabLeave Ledger Entry`
and leaves<0 WHERE employee=%(employee)s AND leave_type=%(leave_type)s
and (from_date between %(from_date)s and %(to_date)s AND docstatus=1 AND leaves<0
or to_date between %(from_date)s and %(to_date)s AND (from_date between %(from_date)s AND %(to_date)s
or (from_date < %(from_date)s and to_date > %(to_date)s)) OR to_date between %(from_date)s AND %(to_date)s
OR (from_date < %(from_date)s AND to_date > %(to_date)s))
""", { """, {
"from_date": from_date, "from_date": from_date,
"to_date": to_date, "to_date": to_date,

View File

@@ -43,10 +43,18 @@ class TestLeavePeriod(unittest.TestCase):
leave_period.grant_leave_allocation(employee=employee_doc_name) leave_period.grant_leave_allocation(employee=employee_doc_name)
self.assertEqual(get_leave_balance_on(employee_doc_name, leave_type, today()), 20) self.assertEqual(get_leave_balance_on(employee_doc_name, leave_type, today()), 20)
def create_leave_period(from_date, to_date): def create_leave_period(from_date, to_date, company=None):
leave_period = frappe.db.get_value('Leave Period',
dict(company=company or erpnext.get_default_company(),
from_date=from_date,
to_date=to_date,
is_active=1), 'name')
if leave_period:
return frappe.get_doc("Leave Period", leave_period)
leave_period = frappe.get_doc({ leave_period = frappe.get_doc({
"doctype": "Leave Period", "doctype": "Leave Period",
"company": erpnext.get_default_company(), "company": company or erpnext.get_default_company(),
"from_date": from_date, "from_date": from_date,
"to_date": to_date, "to_date": to_date,
"is_active": 1 "is_active": 1

View File

@@ -321,11 +321,11 @@ def allocate_earned_leaves():
if new_allocation == allocation.total_leaves_allocated: if new_allocation == allocation.total_leaves_allocated:
continue continue
allocation.db_set("total_leaves_allocated", new_allocation, update_modified=False) allocation.db_set("total_leaves_allocated", new_allocation, update_modified=False)
create_earned_leave_ledger_entry(allocation, earned_leaves, today) create_additional_leave_ledger_entry(allocation, earned_leaves, today)
def create_earned_leave_ledger_entry(allocation, earned_leaves, date): def create_additional_leave_ledger_entry(allocation, leaves, date):
''' Create leave ledger entry based on the earned leave frequency ''' ''' Create leave ledger entry for leave types '''
allocation.new_leaves_allocated = earned_leaves allocation.new_leaves_allocated = leaves
allocation.from_date = date allocation.from_date = date
allocation.unused_leaves = 0 allocation.unused_leaves = 0
allocation.create_leave_ledger_entry() allocation.create_leave_ledger_entry()
@@ -389,6 +389,7 @@ def get_sal_slip_total_benefit_given(employee, payroll_period, component=False):
def get_holidays_for_employee(employee, start_date, end_date): def get_holidays_for_employee(employee, start_date, end_date):
holiday_list = get_holiday_list_for_employee(employee) holiday_list = get_holiday_list_for_employee(employee)
holidays = frappe.db.sql_list('''select holiday_date from `tabHoliday` holidays = frappe.db.sql_list('''select holiday_date from `tabHoliday`
where where
parent=%(holiday_list)s parent=%(holiday_list)s