private bool ValidateAllocationPercentage(UserReAllocationVM model)
        {
            bool    isHundredPer    = true;
            decimal totalPercentage = 0;

            if (model.SelectedDepartmentPercentage != null)
            {
                try
                {
                    for (int i = 0; i < model.SelectedDepartmentPercentage.Length; i++)
                    {
                        totalPercentage += Convert.ToDecimal(model.SelectedDepartmentPercentage[i]);
                    }

                    if (totalPercentage != 100)
                    {
                        return(false);
                    }
                }
                catch (Exception ex)
                {
                    return(false);
                }
            }
            return(isHundredPer);
        }
        private string ValidateUserDeparments(UserReAllocationVM model)
        {
            try
            {
                if (model.SelectedDepartment != null && model.SelectedDepartment.Count() > 0)
                {
                    for (int i = 0; i < model.SelectedDepartmentPercentage.Length; i++)
                    {
                        if (Convert.ToDecimal(model.SelectedDepartmentPercentage[i]) == 0)
                        {
                            return("Allocation percentage cannot be 0 value.");
                        }
                    }

                    var isDuplicateDeptExist = model.SelectedDepartment.GroupBy(n => n).Any(g => g.Count() > 1);
                    if (isDuplicateDeptExist)
                    {
                        return("Duplicate department exist in list.");
                    }
                }
                else
                {
                    return("Atlease one user allocation department must be selected.");
                }
                return(string.Empty);
            }
            catch (Exception ex)
            {
                return("Error occured try later.");
            }
        }
        public ActionResult View(string userId, long groupNumber)
        {
            ViewBag.Title     = "Add/Update User Allocation";
            ViewBag.IsAddEdit = false;

            if (!PermissionControl.CheckPermission(UserAppPermissions.UserAllocation_Add) && !PermissionControl.CheckPermission(UserAppPermissions.UserAllocation_Edit))
            {
                logger.Info("Don't have rights to add new Requisition");
                return(Json(new { Message = Resources.Messages.MSG_RESTRICTED_ACCESS, MessageClass = MessageClass.Error, Response = false }));
            }
            var AdminUser = RoleManager.Roles.Where(x => x.Name.Equals(UserRoles.Admin)).SingleOrDefault();
            var userList  = UserManager.Users.Where(x => !x.IsDeleted && !(x.Roles.Select(y => y.RoleId).Contains(AdminUser.Id))).ToList();

            UserReAllocationVM allocationVM = new UserReAllocationVM();

            allocationVM.UserList = userList;
            allocationVM.UserId   = userId;

            var userAllocatedDepartments = userAllocationManagement.GetUserAllocationByGroupNumber(groupNumber);

            if (userAllocatedDepartments != null && userAllocatedDepartments.Count() > 0)
            {
                var departments = departmentManagement.GetAllDepartments();
                foreach (var userDept in userAllocatedDepartments)
                {
                    userDept.Departments = departments;
                }
                allocationVM.UserAllocatedDepartments = userAllocatedDepartments;
            }

            return(PartialView("_AddUpdateAllocation", allocationVM));
        }
        public ActionResult AddEdit()
        {
            ViewBag.Title     = "Add/Update User Allocation";
            ViewBag.IsAddEdit = true;

            if (!PermissionControl.CheckPermission(UserAppPermissions.UserAllocation_Add) && !PermissionControl.CheckPermission(UserAppPermissions.UserAllocation_Edit))
            {
                logger.Info("Don't have rights to add new Requisition");
                return(Json(new { Message = Resources.Messages.MSG_RESTRICTED_ACCESS, MessageClass = MessageClass.Error, Response = false }));
            }
            var AdminUser = RoleManager.Roles.Where(x => x.Name.Equals(UserRoles.Admin)).SingleOrDefault();
            var userList  = UserManager.Users.Where(x => !x.IsDeleted && !(x.Roles.Select(y => y.RoleId).Contains(AdminUser.Id))).ToList();

            UserReAllocationVM allocationVM = new UserReAllocationVM();

            allocationVM.UserList = userList;

            return(PartialView("_AddUpdateAllocation", allocationVM));
        }
        public JsonResult AddEdit(UserReAllocationVM model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    string deparmentError = ValidateUserDeparments(model);
                    if (!string.IsNullOrEmpty(deparmentError))
                    {
                        return(Json(new { Message = deparmentError, MessageClass = MessageClass.Error, Response = false }));
                    }

                    bool isHundredPercent = ValidateAllocationPercentage(model);
                    if (!isHundredPercent)
                    {
                        return(Json(new { Message = "Total user department allocation must equal to 100%", MessageClass = MessageClass.Error, Response = false }));
                    }

                    if (!PermissionControl.CheckPermission(UserAppPermissions.UserAllocation_Add) && !PermissionControl.CheckPermission(UserAppPermissions.UserAllocation_Edit))
                    {
                        logger.Info("Don't have rights to add update user allocation");
                        return(Json(new { Message = Resources.Messages.MSG_RESTRICTED_ACCESS, MessageClass = MessageClass.Error, Response = false }));
                    }

                    var userPendingAllocation = userAllocationManagement.GetUserPendingAllocationsByUserId(model.UserId);
                    if (userPendingAllocation != null && userPendingAllocation.Count() > 0)
                    {
                        logger.Info("user have already applied for user allocation and its in pending.");
                        return(Json(new { Message = "you have already applied for this user allocation and its in pending.", MessageClass = MessageClass.Error, Response = false }));
                    }

                    var user           = UserManager.FindById(User.Identity.GetUserId());
                    var userDepartment = userdepartmentManagement.GetUserPrimaryDepartmentById(model.UserId);

                    if (userDepartment == null)
                    {
                        logger.InfoFormat("Selected User with ID [{0}] has no department assigned yet", model.UserId);
                        return(Json(new { Message = "Selected User have no department assigned. first add his department to perform user allocation opertion.", MessageClass = MessageClass.Error, Response = false }));
                    }

                    var requesteduserDepartment = userdepartmentManagement.GetUserPrimaryDepartmentById(User.Identity.GetUserId());

                    if (requesteduserDepartment == null)
                    {
                        logger.InfoFormat("User Allocation requested User with ID [{0}] has no department assigned him", model.UserId);
                        return(Json(new { Message = "User Allocation requested User has no department assigned. first add his department to perform user allocation opertion.", MessageClass = MessageClass.Error, Response = false }));
                    }

                    long groupNumber = DateTime.Now.Ticks;
                    for (int i = 0; i < model.SelectedDepartment.Length; i++)
                    {
                        UserAllocation userAllocate = new UserAllocation();
                        userAllocate.UserID                = model.UserId;
                        userAllocate.StartDate             = DateTime.Now;
                        userAllocate.DepartmentID          = model.SelectedDepartment[i];
                        userAllocate.Percentage            = model.SelectedDepartmentPercentage[i];
                        userAllocate.RequestedDepartmentID = requesteduserDepartment.DepartmentID;
                        userAllocate.Status                = RequestStatus.Pending;
                        userAllocate.CreatedBy             = new Guid(User.Identity.GetUserId());
                        userAllocate.IsActive              = false;
                        userAllocate.GroupNumber           = groupNumber;
                        userAllocate.UserDepartmentID      = userDepartment.DepartmentID;
                        userAllocationManagement.Add(userAllocate);
                    }

                    userAllocationManagement.SaveUserAllocation();

                    SendUserAllocationEmailsToLevel1Departments(groupNumber);
                    logger.Info("Successfully Saved User Allocation ");
                    return(Json(new { Message = string.Format(Resources.Messages.MSG_GENERIC_ADD_SUCCESS, "User Allocation"), MessageClass = MessageClass.Success, Response = true }));
                }
                else
                {
                    return(Json(new { Message = string.Format(Resources.Messages.MSG_GENERIC_ADD_FAILED, "User Allocation"), MessageClass = MessageClass.Error, Response = false }));
                }
            }
            catch (Exception ex)
            {
                logger.ErrorFormat("Exception Raised : Message[{0}] Stack Trace [{1}] ", ex.Message, ex.StackTrace);
                return(Json(new { Message = Resources.Messages.MSG_GENERIC_FAILED, MessageClass = MessageClass.Error, Response = false }));
            }
        }