public void EditUser(User user)
        {
            var entry = _databaseModel.Entry(user);
            if (entry.State == EntityState.Detached)
            {
                var set = _databaseModel.Set<User>();
                var attachedEntity = set.Find(user.UserId);  // You need to have access to key

                if (attachedEntity != null)
                {
                    var attachedEntry = _databaseModel.Entry(attachedEntity);
                    attachedEntry.CurrentValues.SetValues(user);
                }
                else
                {
                    entry.State = EntityState.Modified; // This should attach entity
                }
            }

            _databaseModel.SaveChanges();
        }
        public List<Transaction> GetTransactions(User customer, Company company, DateTime beginningDate, DateTime endingDate)
        {
            var transactions = _databaseModel.Transactions
                .Where(t => (beginningDate <= t.CreatetedAt) && (t.CreatetedAt <= endingDate))
                .OrderByDescending(t => t.CreatetedAt);

            if (customer != null)
            {
                var filteredTransactions = transactions.Where(t => t.UserId.Equals(customer.UserId));
                return filteredTransactions.ToList();
            }
            if (company != null)
            {
               // var storesList = _databaseModel.Stores.Where(s => s.CompanyId.Equals(company.CompanyId));
               // var filteredTransactions = transactions.Join(storesList, t => t.StoreId, s => s.StoreId,
                                                            // (o, id) => o);
                var filteredTransactions = transactions.Where(t => t.CompanyId.Equals(company.CompanyId));
                return filteredTransactions.ToList();
            }
            return transactions.ToList();
        }
 public void AddUser(User user)
 {
     _databaseModel.Users.Add(user);
     _databaseModel.SaveChanges();
 }
 public bool UpdateUser(User user)
 {
     _databaseModel.Entry(user).State = EntityState.Modified;
     //_databaseModel.SaveChanges();
     return true;
 }
 public List<Transaction> GetTransactions(User customer, DateTime beginningDate, DateTime endingDate)
 {
     return _transactionsRepository.GetTransactions(customer, null, beginningDate, endingDate);
 }
 private void EditUser(User user)
 {
     user.UpdatedAt = DateTime.Now;
     _usersRepository.EditUser(user);
 }
 public void Save(User user, int updatedBy)
 {
     if (user.UserId == 0)
     {
         AddUser(user);
     }
     else
     {
         EditUser(user);
     }
 }
 private void AddUser(User user)
 {
     user.CreatetedAt = DateTime.Now;
     user.UpdatedAt = DateTime.Now;
     user.Password = BCrypt.Net.BCrypt.HashPassword(user.GeneratedCode,
                                                    BCrypt.Net.BCrypt.GenerateSalt((int) Constants.WorkFactor));
     _usersRepository.AddUser(user);
 }
 public JsonResult IsValidParentUser(User user)
 {
     //TODO: missing users table modification
     //check if the user...
     //1. exists
     if (user != null)
     {
         //2. is senior or master
         if (user.RelationshipType.RelationshipTypeId.Equals((int) RelationshipTypes.Senior) ||
             user.RelationshipType.RelationshipTypeId.Equals((int) RelationshipTypes.Master))
         {
             //3. has enoght space to add a new children
             return Json(true, JsonRequestBehavior.AllowGet);
         }
     }
     return Json(false, JsonRequestBehavior.AllowGet);
 }
        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 bool IsValidPayment(double cashback, User userInformation)
        {
            if (cashback <= 0)
            {
                ModelState.AddModelError("Cashback", "El cashback debe ser mayor a 0 colones.");
                return false;
            }

            if (userInformation.Cashback <= cashback)
            {
                ModelState.AddModelError("Cashback", string.Format("El cashback a debitar debe ser menor al cashback actual ({0} colones).", userInformation.Cashback));
                return false;
            }

            return true;
        }