Esempio n. 1
0
        public LeaveEntitlementModel(LeaveEntitlementUpdate leaveEntitlement)
        {
            Id = leaveEntitlement.Id;

            if (leaveEntitlement.Employee.User.Person != null)
            {
                Employee = leaveEntitlement.Employee.User.Person.Name;
            }
            if (leaveEntitlement.LeaveType != null)
            {
                LeaveType = leaveEntitlement.LeaveType.Title;
            }
            if (leaveEntitlement.LeaveTimePeriod != null)
            {
                LeaveTimePeriod = leaveEntitlement.LeaveTimePeriod.Title;
            }
            if (leaveEntitlement.CreatedByUser.Person != null)
            {
                AllocatedBy = leaveEntitlement.CreatedByUser.Person.Name;
            }
            Allocation      = leaveEntitlement.NewBalance;
            CreatedOn       = leaveEntitlement.CreatedOn;
            OperationType   = GetEnumDescription(leaveEntitlement.Operation);
            PreviousBalance = leaveEntitlement.PreviousBalance;
            NewBalance      = leaveEntitlement.NewBalance;
            LeaveCount      = leaveEntitlement.LeaveCount;
        }
        public LeaveEntitlementUpdateModel(LeaveEntitlementUpdate entitlementUpdate)
        {
            Id = entitlementUpdate.Id;
            if (entitlementUpdate.LeaveType != null)
            {
                LeaveType = entitlementUpdate.LeaveType.Title;
            }
            if (entitlementUpdate.LeaveTimePeriod != null)
            {
                LeaveTimePeriod = entitlementUpdate.LeaveTimePeriod.Title;
            }
            if (entitlementUpdate.CreatedByUser.Person != null)
            {
                AllocatedBy = entitlementUpdate.CreatedByUser.Person.Name;
            }

            OperationType   = GetEnumDescription(entitlementUpdate.Operation);
            LeaveCount      = entitlementUpdate.LeaveCount;
            PreviousBalance = entitlementUpdate.PreviousBalance;
            NewBalance      = entitlementUpdate.NewBalance;
            Comments        = entitlementUpdate.Comments;
        }
Esempio n. 3
0
        public bool Approve(int leaveId, int approverId, string approverComments, bool autoApproved)
        {
            var log = new StringBuilder();

            var leave = _leaveRepository.GetBy(l => l.Id == leaveId, "RequestedForUser.User.Person");

            if (leave != null)
            {
                // Calculate the number of days between these days
                float leaveCount = 0;
                var   counter    = leave.Start;

                while (counter <= leave.End)
                {
                    var result = ShouldDeduct(counter);
                    if (result.Key)
                    {
                        leaveCount = leaveCount + 1;
                        log.AppendLine($"{counter.ToShortDateString()} - {result.Key} - {result.Value}");
                    }
                    else
                    {
                        log.AppendLine($"{counter.ToShortDateString()} - {result.Key} - {result.Value}");
                    }

                    counter = counter.AddDays(1);
                }

                // Short Circuit - Deduct only half for half days
                if (leave.Duration == LeaveDuration.FirstHalf || leave.Duration == LeaveDuration.SecondHalf)
                {
                    leaveCount = 0.5f;
                }

                // Check for Leave Balance

                var leavePeriod      = _leaveTimePeriodRepository.GetBy(i => i.Start <= DateTime.UtcNow && i.End >= DateTime.UtcNow);
                var leaveEntitlement = _leaveEntitlementRepository.GetBy(l => l.LeaveTypeId == leave.LeaveTypeId && l.EmployeeId == leave.RequestedForUserId && l.LeaveTimePeriodId == leavePeriod.Id, "LeaveType");

                //var leaveEntitlement = _leaveEntitlementRepository.GetBy(l => l.LeaveTypeId == leave.LeaveTypeId && l.EmployeeId == leave.RequestedForUserId, "LeaveType");
                if (leaveEntitlement != null)
                {
                    log.AppendLine($"Going to deduct {leaveCount} from {leaveEntitlement.Allocation} leaves of Type {leaveEntitlement.LeaveType.Title}");
                    if (leaveCount <= leaveEntitlement.Allocation)
                    {
                        //Get current
                        var currentLeaveCount = leaveEntitlement.Allocation;

                        // Deduct only half for half days
                        leaveEntitlement.Allocation = leaveEntitlement.Allocation - leaveCount;

                        _leaveEntitlementRepository.Update(leaveEntitlement);
                        _unitOfWork.Commit();

                        // New Balance
                        log.AppendLine($"New Leave Balance is {leaveEntitlement.Allocation}");

                        // Approve the Leave
                        leave.Status           = LeaveStatus.Approved;
                        leave.Count            = leaveCount;
                        leave.CalculationLog   = log.ToString();
                        leave.ApproverId       = approverId;
                        leave.ApproverComments = approverComments;
                        leave.ActedOn          = DateTime.UtcNow;

                        _leaveRepository.Update(leave);
                        _unitOfWork.Commit();

                        var leaveApproverId = _employeeRepository.GetBy(i => i.Id == leave.ApproverId);
                        var getUser         = leaveApproverId.UserId;

                        var comments = "Deducting leave as leave got approved";

                        // If auto approved, mention that.
                        if (autoApproved)
                        {
                            comments = "Deducting leave as leave got auto approved";
                        }


                        // Create a log
                        var newLeaveEntitlementUpdate = new LeaveEntitlementUpdate
                        {
                            EmployeeId        = leave.RequestedForUserId,
                            LeaveTimePeriodId = leaveEntitlement.LeaveTimePeriodId,
                            LeaveTypeId       = leave.LeaveTypeId,
                            LeaveId           = leave.Id,
                            Operation         = LeaveOperation.Deduct,
                            LeaveCount        = leave.Count,
                            PreviousBalance   = currentLeaveCount,
                            NewBalance        = leaveEntitlement.Allocation,
                            Comments          = comments,
                            CreatedByUserId   = getUser
                        };

                        _leaveEntitlementUpdateRepository.Create(newLeaveEntitlementUpdate);
                        _unitOfWork.Commit();
                        return(true);
                    }
                }
            }

            return(false);
        }
        public JsonResult UpdateLeaveEntitlement(UpdateLeaveEntitlementViewModel vm)
        {
            var currentEntitlements = _leaveEntitlementRepository.GetBy(e => e.EmployeeId == vm.EmployeeId && e.LeaveTimePeriodId == vm.LeaveTimePeriodId && e.LeaveTypeId == vm.LeaveTypeId);

            if (vm.Operation == LeaveOperation.Deduct)
            {
                if (currentEntitlements != null)
                {
                    if (currentEntitlements.Allocation - vm.Allocation >= 0)
                    {
                        // Create a log
                        var newLeaveEntitlementUpdate = new LeaveEntitlementUpdate
                        {
                            EmployeeId        = vm.EmployeeId,
                            LeaveTimePeriodId = vm.LeaveTimePeriodId,
                            LeaveTypeId       = vm.LeaveTypeId,
                            Operation         = vm.Operation,
                            LeaveCount        = vm.Allocation,
                            PreviousBalance   = currentEntitlements.Allocation,
                            NewBalance        = currentEntitlements.Allocation - vm.Allocation,
                            Comments          = vm.Comments,
                            CreatedByUserId   = WebUser.Id
                        };

                        _leaveEntitlementUpdateRepository.Create(newLeaveEntitlementUpdate);
                        _unitOfWork.Commit();

                        // Update Entitlements
                        currentEntitlements.Allocation      = currentEntitlements.Allocation - vm.Allocation;
                        currentEntitlements.UpdatedByUserId = WebUser.Id;

                        _leaveEntitlementRepository.Update(currentEntitlements);
                        _unitOfWork.Commit();

                        return(Json(true));
                    }

                    return(Json(false));
                }

                return(Json(false));
            }
            else
            {
                if (currentEntitlements != null)
                {
                    // Create a log
                    var newLeaveEntitlementUpdate = new LeaveEntitlementUpdate
                    {
                        EmployeeId        = vm.EmployeeId,
                        LeaveTimePeriodId = vm.LeaveTimePeriodId,
                        LeaveTypeId       = vm.LeaveTypeId,
                        Operation         = vm.Operation,
                        LeaveCount        = vm.Allocation,
                        PreviousBalance   = currentEntitlements.Allocation,
                        NewBalance        = currentEntitlements.Allocation + vm.Allocation,
                        Comments          = vm.Comments,
                        CreatedByUserId   = WebUser.Id
                    };

                    _leaveEntitlementUpdateRepository.Create(newLeaveEntitlementUpdate);
                    _unitOfWork.Commit();

                    // Update Entitlements
                    currentEntitlements.Allocation      = currentEntitlements.Allocation + vm.Allocation;
                    currentEntitlements.UpdatedByUserId = WebUser.Id;


                    _leaveEntitlementRepository.Update(currentEntitlements);
                    _unitOfWork.Commit();

                    return(Json(true));
                }
                else
                {
                    // Create a log
                    var newLeaveEntitlementUpdate = new LeaveEntitlementUpdate
                    {
                        EmployeeId        = vm.EmployeeId,
                        LeaveTimePeriodId = vm.LeaveTimePeriodId,
                        LeaveTypeId       = vm.LeaveTypeId,
                        Operation         = vm.Operation,
                        LeaveCount        = vm.Allocation,
                        PreviousBalance   = 0,
                        NewBalance        = vm.Allocation,
                        Comments          = vm.Comments,
                        CreatedByUserId   = WebUser.Id
                    };

                    _leaveEntitlementUpdateRepository.Create(newLeaveEntitlementUpdate);
                    _unitOfWork.Commit();

                    // Create Entitlements
                    var newEntitlement = new LeaveEntitlement
                    {
                        EmployeeId        = vm.EmployeeId,
                        LeaveTimePeriodId = vm.LeaveTimePeriodId,
                        LeaveTypeId       = vm.LeaveTypeId,
                        Allocation        = vm.Allocation,
                        Comments          = vm.Comments,
                        CreatedByUserId   = WebUser.Id
                    };

                    _leaveEntitlementRepository.Create(newEntitlement);
                    _unitOfWork.Commit();

                    return(Json(true));
                }
            }
        }