public void AddCustomer(CustomerAccountWithEmployeeModel m)
        {
            try
            {
                SemplestModel.Semplest dbcontext = new SemplestModel.Semplest();
                //BillType bt = dbcontext.BillTypes.First(p => p.BillType1 == "Flat Fee"); // --- feees --- !!!
                //revisit 
                ProductGroupCycleType pgct = dbcontext.ProductGroupCycleTypes.First(p => p.ProductGroupCycleType1 == "Product Group Cycle 30");

                var c = new Customer
                            { 
                                Name = m.CustomerAccount.Customer,
                                BillTypeFK = m.SelectedBillTypeID,
                                ProductGroupCycleType = pgct,
                                 PercentOfMedia=m.CustomerAccount.PercentMedia,
                                 ServiceFee= m.CustomerAccount.ServiceFee,
                                  InternalCustomerId= m.CustomerAccount.internalID,
                                PromotionFeeAmount = m.CustomerAccount.PromotionFeeAmount,
                                CreditLimit=m.CustomerAccount.CreditLimit,
                                PromotionFeeOverride=m.CustomerAccount.PromotionFeeOverride,
                                CreatedDate = DateTime.Now,
                                AllowAutobid = m.CustomerAccount.AllowAutoBid
                            };

                dbcontext.Customers.Add(c);
                var ur = new UserRepository(dbcontext);
                var u = new User
                            {
                                Customer = c,
                                Email = m.CustomerAccount.Email,
                                FirstName = m.CustomerAccount.FirstName,
                                LastName = m.CustomerAccount.LastName,
                                MiddleInitial = m.CustomerAccount.MiddleInitial,
                                IsActive = m.CustomerAccount.isActive,
                                CreatedDate = DateTime.Now,
                                UserTypeFK = m.SelectedUserTypeID
                            };
                dbcontext.Users.Add(u);

                var r = dbcontext.Roles.First(p => p.RolePK == m.SelectedRoleID);
                var ura = new UserRolesAssociation { Role = r, User = u };
                dbcontext.UserRolesAssociations.Add(ura);
                AesEncyrption ae = AesEncyrption.getInstance();
                var encryptedPassword = ae.EncryptString(m.CustomerAccount.UserPassword);
                var cr = new Credential
                             { 
                                 User = u,
                                 UsersFK = u.UserPK,
                                 Username = m.CustomerAccount.UserID,
                                 Password = encryptedPassword
                             };
                dbcontext.Credentials.Add(cr);
                PhoneType pt = dbcontext.PhoneTypes.First(p => p.PhoneType1 == "Business"); // --- phone types --- !!!!
                var ph = new Phone {Phone1 = m.CustomerAccount.Phone, PhoneType = pt};
                dbcontext.Phones.Add(ph);
                var cpa = new CustomerPhoneAssociation {Customer = c, Phone = ph};
                dbcontext.CustomerPhoneAssociations.Add(cpa);
                var sc = dbcontext.StateCodes.First(p => p.StateAbbrPK == m.SelectedStateID);
                var at = dbcontext.AddressTypes.First(p => p.AddressType1 == "H"); // --- address types --- !!!
                var a = new Address
                            {
                                Address1 = m.CustomerAccount.Address1,
                                Address2 = m.CustomerAccount.Address2,
                                City = m.CustomerAccount.City,
                                ZipCode = m.CustomerAccount.Zip,
                                StateCode = sc
                            };
                dbcontext.Addresses.Add(a);
                var caa = new CustomerAddressAssociation {Address = a, Customer = c, AddressType = at};
                dbcontext.CustomerAddressAssociations.Add(caa);
                var cn = new CustomerNote {Customer = c, Note = m.CustomerAccount.CustomerNote};
                dbcontext.CustomerNotes.Add(cn);
                //don't add if not assigned
                if (m.SelectedRepID != -1)
                {
                    var addrep = new EmployeeCustomerAssociation { Customer = c, EmployeeFK = m.SelectedRepID };
                    dbcontext.EmployeeCustomerAssociations.Add(addrep);
                }
                //don't add if not assigned
                if (m.SelectedSalesPersonID != -1)
                {
                    var addsales = new EmployeeCustomerAssociation { Customer = c, EmployeeFK = m.SelectedSalesPersonID };
                    dbcontext.EmployeeCustomerAssociations.Add(addsales);
                }
                CustomerHierarchy ch = null;
                if (m.SelectedParentID == -1) //set parent
                {
                    ch = new CustomerHierarchy {CustomerFK = c.CustomerPK, CustomerParentFK = null};
                    dbcontext.CustomerHierarchies.Add(ch);
                }
                else if (m.SelectedParentID == 0) //set self -- single user
                {
                    ch = new CustomerHierarchy {CustomerFK = c.CustomerPK, CustomerParentFK = c.CustomerPK};
                    dbcontext.CustomerHierarchies.Add(ch);
                }
                else //assign a parent
                {
                    ch = new CustomerHierarchy {CustomerFK = c.CustomerPK, CustomerParentFK = m.SelectedParentID};
                    dbcontext.CustomerHierarchies.Add(ch);
                }
                dbcontext.SaveChanges();

                SendAccountActivationEmail(u.UserPK);

            }
            catch (Exception ex)
            {
                Semplest.SharedResources.Helpers.ExceptionHelper.LogException(ex);
            }
        }
        public ActionResult LogIn(Semplest.SharedResources.Models.ProfileModel pm, string ReturnUrl, string isAdmin, FormCollection f)
        {
            if (ModelState.IsValid)
            {
                ModelState.Clear();
                Dictionary<string, int> loginHash = (Dictionary<string, int>)Session[Semplest.SharedResources.SEMplestConstants.SESSION_LOGINATTEMPTS];
                if (loginHash == null)
                {
                    loginHash = new Dictionary<string, int>();
                    loginHash.Add(pm.UserName, 1);
                }
                else if (loginHash.ContainsKey(pm.UserName))
                    loginHash[pm.UserName] += 1;
                else
                    loginHash.Add(pm.UserName, 1);

                Session[Semplest.SharedResources.SEMplestConstants.SESSION_LOGINATTEMPTS] = loginHash;
                bool isAdminLogin = isAdmin != null;
                using (var dbContext = new SemplestModel.Semplest())
                {
                    Credential cred = null;
                    if (Session[Semplest.SharedResources.SEMplestConstants.SESSION_USERID] != null)
                    {
                        cred = (Credential)Session[Semplest.SharedResources.SEMplestConstants.SESSION_USERID];
                    }
                    else
                    {
                        IQueryable<Credential> creds = null;
                        if (pm.LoggedInSucceeded)
                            creds = dbContext.Credentials.Where(c => c.Username == pm.UserName);
                        else
                        {AesEncyrption a = AesEncyrption.getInstance();
                            var encryptedPassword = a.EncryptString(pm.Password1);
                            creds =
                                dbContext.Credentials.Where(c => c.Username == pm.UserName && c.Password == encryptedPassword);
                        }

                        if (creds.Count() == 1)
                        {
                            if ((isAdminLogin && creds.First().IsAdmin()) || (!isAdminLogin && !creds.First().IsAdmin()))
                                cred = creds.First();
                        }

                    }
                    if (cred == null)
                    {
                        pm.LoggedInSucceeded = false;
                        if (loginHash[pm.UserName] > 3)
                        {
                            var userCreds = dbContext.Credentials.Where(c => c.Username == pm.UserName);
                            if (userCreds.Count() > 0 && userCreds.First().User.IsActive)
                            {
                                userCreds.First().User.IsActive = false;
                                dbContext.SaveChanges();
                            }
                            pm.LoginFailedMessage = "Sorry, your account is currently locked. To enable your account, please email [email protected] for assistance. Thank you!";
                        }
                        else
                            pm.LoginFailedMessage = "The user name or password entered is incorrect. Please try again.";
                    }
                    else if (!cred.User.IsActive)
                    {
                        pm.LoggedInSucceeded = false;
                        pm.LoginFailedMessage = "Sorry, your account is currently locked. To enable your account, please email [email protected] for assistance. Thank you!";
                    }
                    else
                    {
                        Session[Semplest.SharedResources.SEMplestConstants.SESSION_USERID] = cred;
                        if (cred.User.IsRegistered)
                        {
                            //if the user doesn't have a parent in the customerparentfk column then they are a parent else they are a child
                            //if (cred.User.CustomerFK == null || string.IsNullOrEmpty(cred.User.Customer.CustomerHierarchies.First().CustomerParentFK.ToString()))
                            //    return RedirectToAction("Index", "Home");
                            //else if (cred.User.IsRegistered)
                            //user is a regular core user
                            var ur = new UserRepository(dbContext);
                            Session[SEMplestConstants.SessionDefaultProductGroupName] =
                                dbContext.Configurations.Select(q => q.DefaultProductGroupName).Single();
                            if (isAdminLogin)
                                return RedirectToAction("Index", "Home");
                            if (cred.User.UserTypeFK == ur.GetUserType("KeywordOnly").UserTypePK)
                            {
                                Session[SEMplestConstants.SESSION_ISKEYWORDBIDDING] = false;
                                return RedirectToAction("Index", "SmartWord");
                            }
                            Session[SEMplestConstants.SESSION_ISKEYWORDBIDDING] = true;
                            if (cred.User.CustomerFK == null)
                                return RedirectToAction("Index", "Home");
                            return RedirectToAction("Index2", "Home");
                        }
                        else if (pm.LoggedInSucceeded)
                        {
                            Credential saveCred = dbContext.Credentials.Where(x => x.Username == cred.Username && x.Password == cred.Password).First();
                            //authenticated properly and submitted secondary form SecurityAnswer/SecurityQuestion
                            saveCred.SecurityAnswer = pm.SecurityAnswer;
                            saveCred.SecurityQuestion = pm.SecurityQuestion;
                            AesEncyrption a = AesEncyrption.getInstance();
                            var encryptedPassword = a.EncryptString(pm.Password1);
                            saveCred.Password = encryptedPassword;
                            saveCred.User.IsRegistered = true;
                            int i = dbContext.SaveChanges();
                            return RedirectToAction("Index", "Home");
                        }
                        else
                        {
                            //authenticated properly and HAS NOT submitted secondary form SecurityAnswer/SecurityQuestion to complete registration
                            pm.IsRegistered = false;
                            pm.LoggedInSucceeded = true;
                        }
                    }
                }
            }
            return View(pm);
        }