コード例 #1
0
ファイル: LeavesController.cs プロジェクト: xprasoulas/grid
        public JsonResult Approve(ApproveRejectLeaveViewModel vm)
        {
            ApiResult <ApproveRejectLeaveViewModel> result;

            var leave    = _leaveRepository.GetBy(r => r.Id == vm.Id, "RequestedForUser.User.Person");
            var employee = _employeeRepository.GetBy(r => r.UserId == WebUser.Id);
            var status   = _leaveService.Approve(vm.Id, employee.Id, vm.ApproverComments, false);

            if (status)
            {
#if !DEBUG
                _emailComposerService.LeaveApprovalEmail(vm.Id, false);
#endif

                result = new ApiResult <ApproveRejectLeaveViewModel>
                {
                    Status  = true,
                    Message = "Leave Approved",
                    Result  = vm
                };
            }
            else
            {
                result = new ApiResult <ApproveRejectLeaveViewModel>
                {
                    Status  = false,
                    Message = "Not Enough Balance to Approve the Leave",
                    Result  = null
                };
            }


            return(Json(result));
        }
コード例 #2
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);
        }