def create_leave_ledger_entry(self, submit=True): if self.status != 'Approved' and submit: return expiry_date = get_allocation_expiry(self.employee, self.leave_type, self.to_date, self.from_date) lwp = frappe.db.get_value("Leave Type", self.leave_type, "is_lwp") if expiry_date: self.create_ledger_entry_for_intermediate_allocation_expiry( expiry_date, submit, lwp) else: raise_exception = True if frappe.flags.in_patch: raise_exception = False args = dict(leaves=self.total_leave_days * -1, from_date=self.from_date, to_date=self.to_date, is_lwp=lwp, holiday_list=get_holiday_list_for_employee( self.employee, raise_exception=raise_exception) or '') create_leave_ledger_entry(self, args, submit)
def create_ledger_entry_for_intermediate_allocation_expiry(self, expiry_date, submit, lwp): """Splits leave application into two ledger entries to consider expiry of allocation""" raise_exception = False if frappe.flags.in_patch else True leaves = get_number_of_leave_days( self.employee, self.leave_type, self.from_date, expiry_date, self.half_day, self.half_day_date ) if leaves: args = dict( from_date=self.from_date, to_date=expiry_date, leaves=leaves * -1, is_lwp=lwp, holiday_list=get_holiday_list_for_employee(self.employee, raise_exception=raise_exception) or "", ) create_leave_ledger_entry(self, args, submit) if getdate(expiry_date) != getdate(self.to_date): start_date = add_days(expiry_date, 1) leaves = get_number_of_leave_days( self.employee, self.leave_type, start_date, self.to_date, self.half_day, self.half_day_date ) if leaves: args.update(dict(from_date=start_date, to_date=self.to_date, leaves=leaves * -1)) create_leave_ledger_entry(self, args, submit)
def create_leave_ledger_entry(self, submit=True): if self.status != "Approved" and submit: return expiry_date = get_allocation_expiry_for_cf_leaves( self.employee, self.leave_type, self.to_date, self.from_date ) lwp = frappe.db.get_value("Leave Type", self.leave_type, "is_lwp") if expiry_date: self.create_ledger_entry_for_intermediate_allocation_expiry(expiry_date, submit, lwp) else: alloc_on_from_date, alloc_on_to_date = self.get_allocation_based_on_application_dates() if self.is_separate_ledger_entry_required(alloc_on_from_date, alloc_on_to_date): # required only if negative balance is allowed for leave type # else will be stopped in validation itself self.create_separate_ledger_entries(alloc_on_from_date, alloc_on_to_date, submit, lwp) else: raise_exception = False if frappe.flags.in_patch else True args = dict( leaves=self.total_leave_days * -1, from_date=self.from_date, to_date=self.to_date, is_lwp=lwp, holiday_list=get_holiday_list_for_employee(self.employee, raise_exception=raise_exception) or "", ) create_leave_ledger_entry(self, args, submit)
def create_separate_ledger_entries(self, alloc_on_from_date, alloc_on_to_date, submit, lwp): """Creates separate ledger entries for application period falling into separate allocations""" # for creating separate ledger entries existing allocation periods should be consecutive if (submit and alloc_on_from_date and alloc_on_to_date and add_days( alloc_on_from_date.to_date, 1) != alloc_on_to_date.from_date): frappe.throw( _("Leave Application period cannot be across two non-consecutive leave allocations {0} and {1}." ).format( get_link_to_form("Leave Allocation", alloc_on_from_date.name), get_link_to_form("Leave Allocation", alloc_on_to_date), )) raise_exception = False if frappe.flags.in_patch else True if alloc_on_from_date: first_alloc_end = alloc_on_from_date.to_date second_alloc_start = add_days(alloc_on_from_date.to_date, 1) else: first_alloc_end = add_days(alloc_on_to_date.from_date, -1) second_alloc_start = alloc_on_to_date.from_date leaves_in_first_alloc = get_number_of_leave_days( self.employee, self.leave_type, self.from_date, first_alloc_end, self.half_day, self.half_day_date, ) leaves_in_second_alloc = get_number_of_leave_days( self.employee, self.leave_type, second_alloc_start, self.to_date, self.half_day, self.half_day_date, ) args = dict( is_lwp=lwp, holiday_list=get_holiday_list_for_employee( self.employee, raise_exception=raise_exception) or "", ) if leaves_in_first_alloc: args.update( dict(from_date=self.from_date, to_date=first_alloc_end, leaves=leaves_in_first_alloc * -1)) create_leave_ledger_entry(self, args, submit) if leaves_in_second_alloc: args.update( dict(from_date=second_alloc_start, to_date=self.to_date, leaves=leaves_in_second_alloc * -1)) create_leave_ledger_entry(self, args, submit)
def on_update_after_submit(self): if self.has_value_changed("new_leaves_allocated"): self.validate_against_leave_applications() leaves_to_be_added = self.new_leaves_allocated - self.get_existing_leave_count( ) args = { "leaves": leaves_to_be_added, "from_date": self.from_date, "to_date": self.to_date, "is_carry_forward": 0 } create_leave_ledger_entry(self, args, True)
def create_leave_ledger_entry(self, submit=True): args = frappe._dict(leaves=self.encashable_days * -1, from_date=self.encashment_date, to_date=self.encashment_date, is_carry_forward=0) create_leave_ledger_entry(self, args, submit) # create reverse entry for expired leaves to_date = self.get_leave_allocation().get('to_date') if to_date < getdate(nowdate()): args = frappe._dict(leaves=self.encashable_days, from_date=to_date, to_date=to_date, is_carry_forward=0) create_leave_ledger_entry(self, args, submit)
def create_leave_ledger_entry(self, submit=True): expiry_date = get_allocation_expiry(self.employee, self.leave_type, self.to_date, self.from_date) lwp = frappe.db.get_value("Leave Type", self.leave_type, "is_lwp") if expiry_date: self.create_ledger_entry_for_intermediate_allocation_expiry( expiry_date, submit, lwp) else: args = dict(leaves=self.total_leave_days * -1, from_date=self.from_date, to_date=self.to_date, is_lwp=lwp) create_leave_ledger_entry(self, args, submit)
def create_ledger_entry_for_intermediate_allocation_expiry( self, expiry_date, submit, lwp): ''' splits leave application into two ledger entries to consider expiry of allocation ''' args = dict(from_date=self.from_date, to_date=expiry_date, leaves=(date_diff(expiry_date, self.from_date) + 1) * -1, is_lwp=lwp) create_leave_ledger_entry(self, args, submit) if getdate(expiry_date) != getdate(self.to_date): start_date = add_days(expiry_date, 1) args.update( dict(from_date=start_date, to_date=self.to_date, leaves=date_diff(self.to_date, expiry_date) * -1)) create_leave_ledger_entry(self, args, submit)
def create_leave_ledger_entry(self, submit=True): if self.unused_leaves: expiry_days = frappe.db.get_value( "Leave Type", self.leave_type, "expire_carry_forwarded_leaves_after_days") end_date = add_days(self.from_date, expiry_days - 1) if expiry_days else self.to_date args = dict(leaves=self.unused_leaves, from_date=self.from_date, to_date=min(getdate(end_date), getdate(self.to_date)), is_carry_forward=1) create_leave_ledger_entry(self, args, submit) args = dict(leaves=self.new_leaves_allocated, from_date=self.from_date, to_date=self.to_date, is_carry_forward=0) create_leave_ledger_entry(self, args, submit)
def on_update_after_submit(self): if self.has_value_changed("new_leaves_allocated"): self.validate_against_leave_applications() # recalculate total leaves allocated self.total_leaves_allocated = flt(self.unused_leaves) + flt( self.new_leaves_allocated) # run required validations again since total leaves are being updated self.validate_leave_days_and_dates() leaves_to_be_added = self.new_leaves_allocated - self.get_existing_leave_count( ) args = { "leaves": leaves_to_be_added, "from_date": self.from_date, "to_date": self.to_date, "is_carry_forward": 0, } create_leave_ledger_entry(self, args, True) self.db_update()
def create_leave_ledger(leave_allocation, employee, from_date, to_date, leave_type, new_leaves_allocated, leave_period, submit=True): lal = frappe.get_doc("Leave Allocation", leave_allocation) is_carry_forward = frappe.db.get_value("Leave Type", leave_type, "is_carry_forward") if is_carry_forward == 1: is_carry_forward = True args = dict( leaves=new_leaves_allocated, # transaction_type = "Leave Allocation", employee=employee, leave_type=leave_type, from_date=from_date, to_date=to_date, is_carry_forward=is_carry_forward) create_leave_ledger_entry(lal, args, submit)
def create_ledger_entry_for_intermediate_allocation_expiry( self, expiry_date, submit, lwp): ''' splits leave application into two ledger entries to consider expiry of allocation ''' raise_exception = True if frappe.flags.in_patch: raise_exception = False args = dict(from_date=self.from_date, to_date=expiry_date, leaves=(date_diff(expiry_date, self.from_date) + 1) * -1, is_lwp=lwp, holiday_list=get_holiday_list_for_employee( self.employee, raise_exception=raise_exception) or '') create_leave_ledger_entry(self, args, submit) if getdate(expiry_date) != getdate(self.to_date): start_date = add_days(expiry_date, 1) args.update( dict(from_date=start_date, to_date=self.to_date, leaves=date_diff(self.to_date, expiry_date) * -1)) create_leave_ledger_entry(self, args, submit)