public async Task <string> CreateUserAsync(string username, string firstname, string lastname, string position, string telephone, int?extension, string employmentDate, string registrationDate)
        {
            string message = string.Empty;

            try
            {
                ApplicationUser newUser = new ApplicationUser()
                {
                    UserName                    = username,
                    Email                       = username,
                    EmailConfirmed              = true,
                    Title                       = Enums.Titles.Mr,
                    FirstName                   = firstname,
                    LastName                    = lastname,
                    Position                    = position,
                    DirectDial                  = telephone,
                    Extension                   = extension,
                    EmploymentDate              = CommonBehaviour.ConvertStrToDateTime(employmentDate),
                    RegistrationDate            = CommonBehaviour.ConvertStrToDateTime(registrationDate),
                    LastLogInTime               = null,
                    LastLogoutTime              = null,
                    IsLoggedIn                  = false,
                    InvalidLoginAttemptCount    = 0,
                    LastInvalidLoginAttemptTime = null,
                    Locked                      = false
                };

                string         temporaryPassword = CommonBehaviour.GenerateTempPassword();
                IdentityResult result            = await userManager.CreateAsync(newUser, CommonBehaviour.GenerateTempPassword());      // user creation

                if (result != null && result.Succeeded == true)
                {
                    SendUserCreationEmail(username, temporaryPassword);
                    message = "Success - user creation successful";
                }
                else
                {
                    string errors = string.Empty;
                    foreach (string error in result.Errors)
                    {
                        errors += error + " ";
                    }
                    message = string.Format("Error : {0}", errors);
                }
            }
            catch (Exception)
            {
                message = "Error - user creation unsuccessful";
            }
            return(message);
        }
        public async Task <string> UpdateUserAsync(string username, string firstname, string lastname, string position, string telephone, int?extension, string employmentDate, string registrationDate, string locked)
        {
            string message = string.Empty;

            try
            {
                ApplicationUser userToUpdate = userManager.FindByEmail(username);
                userToUpdate.FirstName        = firstname;
                userToUpdate.LastName         = lastname;
                userToUpdate.Position         = position;
                userToUpdate.DirectDial       = telephone;
                userToUpdate.Extension        = extension;
                userToUpdate.EmploymentDate   = CommonBehaviour.ConvertStrToDateTime(employmentDate);
                userToUpdate.RegistrationDate = CommonBehaviour.ConvertStrToDateTime(registrationDate);
                userToUpdate.Locked           = locked == "Yes" ? true:false;
                IdentityResult result = await userManager.UpdateAsync(userToUpdate);

                if (result != null && result.Succeeded == true)
                {
                    message = "Success - user update successful";
                }
                else
                {
                    string errors = string.Empty;
                    foreach (string error in result.Errors)
                    {
                        errors += error + " ";
                    }
                    message = string.Format("Error : {0}", errors);
                }
            }
            catch (Exception)
            {
                message = "Error - user update unsuccessful - Contact IT support";
            }
            return(message);
        }
        private IList <ApplicationUser> SearchUsersHelper(IQueryable <ApplicationUser> users, IQueryable <ApplicationRole> roles, string userRolesCsv, string username, string firstname, string lastname, string position, string employmentDate, string registrationDate, string lastLoginDateTime, string lastInvalidLoginDateTime)
        {
            try
            {
                IList <ApplicationUser> searchedUsers = new List <ApplicationUser>();
                if (GeneralValidator.IsStringNotEmpty(username))
                {
                    users = users.Where(u => u.UserName.Contains(username));
                }
                if (GeneralValidator.IsStringNotEmpty(firstname))
                {
                    users = users.Where(u => u.FirstName.Contains(firstname));
                }
                if (GeneralValidator.IsStringNotEmpty(lastname))
                {
                    users = users.Where(u => u.LastName.Contains(lastname));
                }
                if (GeneralValidator.IsStringNotEmpty(position))
                {
                    users = users.Where(u => u.Position.Contains(position));
                }
                if (employmentDate != null)
                {
                    users = users.Where(u => u.EmploymentDate == CommonBehaviour.ConvertStrToDateTime(employmentDate));
                }
                if (registrationDate != null)
                {
                    users = users.Where(u => u.RegistrationDate == CommonBehaviour.ConvertStrToDateTime(registrationDate));
                }
                if (lastLoginDateTime != null)
                {
                    users = users.Where(u => u.LastLogInTime == CommonBehaviour.ConvertStrToDateTime(lastLoginDateTime));
                }
                if (lastInvalidLoginDateTime != null)
                {
                    users = users.Where(u => u.LastLogInTime == CommonBehaviour.ConvertStrToDateTime(lastInvalidLoginDateTime));
                }

                if (users.Count() > 0)
                {
                    // filter roles
                    string[] searchRoles = userRolesCsv.Split(',');
                    IList <ApplicationRole> searchAppRoles = new List <ApplicationRole>();
                    foreach (ApplicationRole role in roles)
                    {
                        foreach (string searchRole in searchRoles)
                        {
                            if (searchRole == role.Name)
                            {
                                searchAppRoles.Add(role);
                            }
                        }
                    }

                    // filter users based on searched roles
                    foreach (ApplicationUser user in users)
                    {
                        //UserRoleLoop:
                        foreach (ApplicationUserRole userRole in user.Roles)
                        {
                            foreach (ApplicationRole searchRole in searchAppRoles)
                            {
                                if (userRole.RoleId == searchRole.Id)
                                {
                                    searchedUsers.Add(user);
                                    //goto UserRoleLoop;
                                }
                            }
                        }
                    }
                }
                return(searchedUsers);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public IEnumerable <OrderViewModel> SearchOrders(int?companyId, string contactFulName, string orderId, string status,
                                                         string orderType, string creationDateFrom, string creationDateTo)
        {
            try
            {
                DateTime?fromDate = creationDateFrom != null?CommonBehaviour.ConvertStrToDateTime(creationDateFrom) : (DateTime?)null;

                DateTime?toDate = creationDateTo != null?CommonBehaviour.ConvertStrToDateTime(creationDateTo) : (DateTime?)null;

                status         = status != "0" ? CommonBehaviour.GetCommonStatusString(int.Parse(status)) : null;
                orderType      = orderType == "0" ? null : orderType;
                companyId      = companyId == -1 ? null : companyId;
                contactFulName = contactFulName == "-1" ? null : contactFulName;

                var result = orderRepository.SQLQuery <OrderViewModel>("SP_GetAllOrderViewModels", null);
                IList <OrderViewModel> orderVms = result.ToList <OrderViewModel>();

                // filter by company
                if (companyId != null)
                {
                    orderVms = orderVms.Where <OrderViewModel>(o => o.companyId == companyId).ToList <OrderViewModel>();
                }

                // filter by contact full name
                if (contactFulName != null)
                {
                    contactFulName = CommonBehaviour.CleanContactFulName(contactFulName);
                    orderVms       = orderVms.Where <OrderViewModel>(o => o.contactFulName == contactFulName).ToList <OrderViewModel>();
                }

                // filter by orderId
                if (orderId != null)
                {
                    orderVms = orderVms.Where <OrderViewModel>(o => o.id == int.Parse(orderId)).ToList <OrderViewModel>();
                }

                // filter by status
                if (status != null)
                {
                    orderVms = orderVms.Where <OrderViewModel>(o => o.status == status).ToList <OrderViewModel>();
                }

                // filter by order type
                if (orderType != null)
                {
                    orderVms = orderVms.Where <OrderViewModel>(o => o.type == orderType).ToList <OrderViewModel>();
                }

                // filter by from date
                if (fromDate != null)
                {
                    orderVms = orderVms.Where <OrderViewModel>(o => CommonBehaviour.ConvertStrToDateTime(o.orderCreationDate) >= fromDate).ToList <OrderViewModel>();
                }

                // filter by to date
                if (toDate != null)
                {
                    orderVms = orderVms.Where <OrderViewModel>(o => CommonBehaviour.ConvertStrToDateTime(o.orderCreationDate) <= toDate).ToList <OrderViewModel>();
                }

                return(orderVms);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }