private EditViewModel Map(User user)
        {
            var editViewModel = new EditViewModel
                {
                    Address1 = user.Address1,
                    Cashback = user.Cashback.ToString(CultureInfo.InvariantCulture),
                    Cellphone = user.Cellphone,
                    CompaniesList = _companiesManagement.GetCompaniesList(SecurityContext),
                    Company = user.Company,
                    DistrictId = user.District.DistrictId,
                    Day = user.Dob != null ? user.Dob.Value.Day : 1,
                    Month = user.Dob != null ? user.Dob.Value.Month : 1,
                    Year = user.Dob != null ? user.Dob.Value.Year : 1970,
                    //Dob = (DateTime) user.Dob,

                    Email = user.Email,
                    Enabled = user.Enabled,
                    FirstName = user.FName,
                    GeneratedCode = user.GeneratedCode,
                    IdentificationNumber =
                        user.CedNumber.ToString(CultureInfo.InvariantCulture),
                    IdentificationType =
                        new IdentificationType {IdentificationTypeId = (int) IdentificationTypes.CedNumber},
                    IdentificationTypesList = GetIdentificationTypesList(),
                    LastName1 = user.LName1,
                    LastName2 = user.LName2,
                    Nationality = user.Nationality,
                    PhoneNumber = user.PhoneNumber,
                    RelationshipType = user.RelationshipType,
                    RelationshipTypeList = _relationshipTypesManagement.GetRelationshipTypesList(),
                    RolesList = _rolesManagement.GetRoles(SecurityContext),
                    UserId = user.UserId,
                    UserRol = user.UserRol,
                    ParentUser = user.UserReference
                    /*ParentIdentificationNumber =
                        user.UserReference != null
                            ? user.UserReference.CedNumber.ToString(CultureInfo.InvariantCulture)
                            : Constants.SolucionesArUser.ToString(),*/
                };

            return editViewModel;
        }
        private User Map(EditViewModel editViewModel)
        {
            var user = new User
                {
                    Address1 =
                        editViewModel.Address1 != null ? editViewModel.Address1.ToUpper() : string.Empty,
                    Cashback = editViewModel.Cashback != null ? Convert.ToDouble(editViewModel.Cashback) : 0,
                    CedNumber =
                        Convert.ToInt32(editViewModel.IdentificationNumber.Replace("-", string.Empty)),
                    Cellphone = editViewModel.Cellphone,
                    CompanyId = editViewModel.Company.CompanyId,
                    DistrictId = editViewModel.DistrictId,

                    Dob = new DateTime(editViewModel.Year, editViewModel.Month, editViewModel.Day),
                    Email = editViewModel.Email != null ? editViewModel.Email.ToUpper() : string.Empty,
                    Enabled = editViewModel.Enabled,
                    FName = editViewModel.FirstName.ToUpper(),
                    GeneratedCode =
                        GenerateUserCode(editViewModel.LastName1, editViewModel.LastName2,
                                         editViewModel.IdentificationNumber
                                                      .ToString(CultureInfo.InvariantCulture)).ToUpper(),
                    LName1 = editViewModel.LastName1.ToUpper(),
                    LName2 = editViewModel.LastName2.ToUpper(),
                    Nationality = editViewModel.Nationality.ToUpper(),
                    IdentificationTypeId = editViewModel.IdentificationType.IdentificationTypeId,
                    RolId = editViewModel.UserRol.RolId,
                    PhoneNumber = editViewModel.PhoneNumber,
                    RelationshipTypeId = editViewModel.RelationshipType.RelationshipTypeId,
                    UserId = editViewModel.UserId,
                    UserReferenceId = editViewModel.ParentUser.UserId
                    /*UserReferenceId = !string.IsNullOrEmpty(editViewModel.ParentIdentificationNumber)
                                          ? _usersManagement.GetUserByIdentificationNumber(
                                              Convert.ToInt32(
                                                  editViewModel.ParentIdentificationNumber.Replace("-", string.Empty)))
                                                            .UserId
                                          : (int) Constants.SolucionesArUser,*/
                };

            //update the user password with the SAR generated code
            user.Password = BCrypt.Net.BCrypt.HashPassword(
                user.GeneratedCode,
                BCrypt.Net.BCrypt.GenerateSalt((int) Constants.WorkFactor));

            return user;
        }
        private EditViewModel GenerateBasicEditViewModel()
        {
            var editViewModel = new EditViewModel
                {
                    UserId = 0,
                    CantonId = (int) Constants.DefaultCanton,
                    Company = new Company(),
                    CompaniesList = _companiesManagement.GetCompaniesList(SecurityContext),
                    Day = DateTime.Now.Day,
                    Month = DateTime.Now.Month,
                    Year = DateTime.Now.Year,
                    DistrictId = (int) Constants.DefaultDistrict,
                    Enabled = true,
                    IdentificationType =
                        new IdentificationType
                            {
                                IdentificationTypeId =
                                    (int) IdentificationTypes.CedNumber
                            },
                    IdentificationTypesList = GetIdentificationTypesList(),
                    ProvinceId = (int) Constants.DefaultProvince,
                    RelationshipType = new RelationshipType(),
                    RelationshipTypeList = _relationshipTypesManagement.GetRelationshipTypesList(),
                    RolesList = _rolesManagement.GetRoles(SecurityContext),
                    UserRol = new Rol(),

                    //ParentIdentificationNumber = Constants.SolucionesArUser.ToString(),
                };

            return editViewModel;
        }
 private bool IsValidUser(EditViewModel editViewModel)
 {
     //if it's a new user doesn't required the identification number validation
     if (editViewModel.UserId != 0 &&
         !_usersManagement.HasValidIdentificationNumber(editViewModel.UserId, editViewModel.IdentificationNumber))
     {
         ModelState.AddModelError("IdentificationNumber",
                                  "El número de idetificación ya está registrado en el sistema.");
         return false;
     }
     return true;
 }
        public ActionResult Save(EditViewModel editViewModel)
        {
            if (ModelState.IsValid && IsValidUser(editViewModel))
            {
                _usersManagement.Save(Map(editViewModel), SecurityContext.User.Id);
                UpdateAvailableUsersList();
                return RedirectToAction("Index");
            }

            editViewModel.CompaniesList = _companiesManagement.GetCompaniesList(SecurityContext);
            editViewModel.IdentificationTypesList = GetIdentificationTypesList();
            editViewModel.RelationshipTypeList = _relationshipTypesManagement.GetRelationshipTypesList();
            editViewModel.RolesList = _rolesManagement.GetRoles(SecurityContext);
            editViewModel.UsersToShowList = GenerateUsersToShowList(GetAvailableUsersList());
            ViewBag.ProvincesList = _provincesManagement.GetProvinces();
            ViewBag.CantonsList = _cantonsManagement.GetCantons(editViewModel.ProvinceId);
            ViewBag.DistrictsList = _districtsManagement.GetDistricts(editViewModel.CantonId);

            return View("Edit", editViewModel);
        }