示例#1
0
        //
        // GET: /Roles/

        public void AddRightToDatabase(string label, string controllerName, string vAction)
        {
            
            bool found = false;
            string myController = ControllerContext.RouteData.Values["Controller"].ToString();
            string controllerActionName = controllerName + "." + vAction;
            if (controllerName != myController && !string.IsNullOrEmpty(label))
            {
                using (SemplestEntities dbContext = new SemplestEntities())
                {
                    foreach (Right r in dbContext.Rights)
                    {
                        if (label == r.Label && controllerActionName == r.Controller)
                        {
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        dbContext.Rights.Add(new Right { Controller = controllerActionName, Label = label });
                        dbContext.SaveChanges();
                    }
                }
            }
        }
        //
        // GET: /CreateNewCustomerAccount/

        public ActionResult Index(string usersearch, string accountnumbersearch, string emailsearch, FormCollection form)
        {

            //ViewBag.Message = "Welcome to SEMPLEST ADMIN!";
            SemplestEntities dbcontext = new SemplestEntities();



            var viewModel =
                from u in dbcontext.Users
                join c in dbcontext.Customers on u.CustomerFK equals c.CustomerPK
                //where ((c.Name.Contains(usersearch) || u.FirstName.Contains(usersearch) || u.LastName.Contains(usersearch)))
                select new HomeModel
                {
                    Customer = c.Name,
                    AccountNumber = c.CustomerPK,
                    FirstName = u.FirstName,
                    LastName = u.LastName,
                    Email = u.Email
                };

            var predicate = PredicateBuilder.True <HomeModel>();


            if (form["searchtype"] == "Customer" && usersearch != null && usersearch != "")
            {
                predicate = (p => p.Customer.ToLower().Contains(usersearch.ToLower()));
            }

            if (form["searchtype"] == "LastName" && usersearch != null && usersearch != "")
            {
                predicate = (p => p.LastName.ToLower().Contains(usersearch.ToLower()));
            }


            if ( accountnumbersearch != null && accountnumbersearch!="")
            {
                predicate = (p => p.AccountNumber.Equals(accountnumbersearch.ToLower()));
            }

            if (emailsearch != null && emailsearch!="")
            {
                predicate = (p => p.Email.ToLower().Contains(emailsearch.ToLower()));
            }


           
            viewModel = viewModel.AsExpandable().Where(predicate);
            
            //ordering by lastname, firstname
            viewModel = viewModel.OrderBy(p => p.LastName).ThenBy(p => p.FirstName); 

            return View(viewModel);
        }
        public ActionResult Edit(User model)
        {
            using (SemplestEntities dbContext = new SemplestEntities())
            {
                dbContext.Users.Add(model);
                dbContext.SaveChanges();
            }

            return View(model);

        }
        public ActionResult Finance(int id)
        {

            SemplestEntities dbcontext = new SemplestEntities();
            var viewModel =
               from u in dbcontext.Users
               join c in dbcontext.Customers on u.CustomerFK equals c.CustomerPK
               join caa in dbcontext.CustomerAddressAssociations on c.CustomerPK equals caa.CustomerFK
               join a in dbcontext.Addresses on caa.AddressFK equals a.AddressPK
               join sc in dbcontext.StateCodes on a.StateAbbrFK equals sc.StateAbbrPK
               join at in dbcontext.AddressTypes on caa.AddressTypeFK equals at.AddressTypePK
               join cpa in dbcontext.CustomerPhoneAssociations on c.CustomerPK equals cpa.CustomerFK
               join p in dbcontext.Phones on cpa.PhoneFK equals p.PhonePK
               join b in dbcontext.BillTypes on c.BillTypeFK equals b.BillTypePK
               where (c.CustomerPK == id)
               select new AccountServiceModel
               {
                   AccountNumber = c.CustomerPK,
                   Customer = c.Name,
                   FirstName = u.FirstName,
                   LastName = u.LastName,
                   Address1 = a.Address1,
                   Address2 = a.Address2,
                   City = a.City,
                   State = sc.StateAbbr,
                   Zip = a.ZipCode,
                   Phone = p.Phone1,
                   Email = u.Email,
                   BillType = b.BillType1
               };

            return View(viewModel.Single(c => c.AccountNumber == id));
        }
        //
        // GET: /ClientAccount/
      
        public ActionResult Index(int id)
        {
            SemplestEntities dbcontext = new SemplestEntities();

            


            var viewModel =
               from u in dbcontext.Users
               join c in dbcontext.Customers on u.CustomerFK equals c.CustomerPK
               join caa in dbcontext.CustomerAddressAssociations on c.CustomerPK equals caa.CustomerFK
               join a in dbcontext.Addresses on caa.AddressFK equals a.AddressPK
               join sc in dbcontext.StateCodes on a.StateAbbrFK equals sc.StateAbbrPK
               join at in dbcontext.AddressTypes on caa.AddressTypeFK equals at.AddressTypePK
               join cpa in dbcontext.CustomerPhoneAssociations on c.CustomerPK equals cpa.CustomerFK 
               join p in dbcontext.Phones on cpa.PhoneFK equals p.PhonePK 
               join b in dbcontext.BillTypes on c.BillTypeFK equals b.BillTypePK 
               
               where (c.CustomerPK==id)
               select new AccountServiceModel 
               {
                    AccountNumber = c.CustomerPK ,
                    Customer  = c.Name ,
                    FirstName  = u.FirstName  ,
                    LastName  = u.LastName ,
                    Address1  = a.Address1 ,
                    Address2  = a.Address2 ,
                    City  = a.City ,
                    State  = sc.StateAbbr ,
                    Zip = a.ZipCode ,
                    Phone  = p.Phone1 ,
                    Email  = u.Email ,
                    BillType  = b.BillType1 
               };


            var viewModel2 =
                from e in dbcontext.Employees 
                join eca in dbcontext.EmployeeCustomerAssociations on e.EmployeePK equals eca.EmployeeFK 
                join et in dbcontext.EmployeeTypes on e.EmployeeTypeFK equals et.EmployeeTypeID 
                join u in dbcontext.Users on e.UsersFK equals u.UserPK 
                where (eca.CustomerFK==id)
                select new EmployeeCustomerAssociaitionModel
            {
                AccountNumber = eca.CustomerFK,
                EmployeeType = et.EmployeeType1,
                employeePK = e.EmployeePK,
                FirstName = u.FirstName,
                LastName = u.LastName,
                EmployeeUserPK = u.UserPK
            };



            AccountServiceWithEmployeeModel x = new AccountServiceWithEmployeeModel();
            x.AccountServiceModel = viewModel.Single(c=>c.AccountNumber==id);
            x.EmployeeCustomerAssociaitionModel = viewModel2;
            

            //from u in dbcontext.Users
            //join c in dbcontext.Customers on u.CustomerFK equals c.CustomerPK
            //join eca in dbcontext.EmployeeCustomerAssociations on c.CustomerPK equals eca.CustomerFK
            //join e in dbcontext.Employees on u.UserPK equals e.UsersFK
            //join et in dbcontext.EmployeeTypes on e.EmployeeTypeFK equals et.EmployeeTypeID

            //where (c.CustomerPK == id)
            //select new EmployeeCustomerAssociaitionModel
            //{
            //    AccountNumber = c.CustomerPK,
            //    EmployeeType = et.EmployeeType1,
            //    employeePK = e.EmployeePK,
            //    FirstName = u.FirstName,
            //    LastName = u.LastName,
            //    EmployeeUserPK = u.UserPK
            //};

            //return View(viewModel.Single(c=>c.AccountNumber==id));
            return View(x);
        }
        public ActionResult Edit(int id)
        {

            SemplestEntities dbcontext = new SemplestEntities();

            var viewModel =
               from u in dbcontext.Users
               join c in dbcontext.Customers on u.CustomerFK equals c.CustomerPK
               join caa in dbcontext.CustomerAddressAssociations on c.CustomerPK equals caa.CustomerFK
               join a in dbcontext.Addresses on caa.AddressFK equals a.AddressPK
               join sc in dbcontext.StateCodes on a.StateAbbrFK equals sc.StateAbbrPK
               join at in dbcontext.AddressTypes on caa.AddressTypeFK equals at.AddressTypePK
               join cpa in dbcontext.CustomerPhoneAssociations on c.CustomerPK equals cpa.CustomerFK
               join p in dbcontext.Phones on cpa.PhoneFK equals p.PhonePK
               join b in dbcontext.BillTypes on c.BillTypeFK equals b.BillTypePK
               join n in dbcontext.CustomerNotes.DefaultIfEmpty() on c.CustomerPK equals n.CustomerFK 

               where (c.CustomerPK == id)
               select new CustomerAccount
               {
                   AccountNumber = c.CustomerPK,
                   Customer = c.Name,
                   FirstName = u.FirstName,
                   LastName = u.LastName,
                   MiddleInitial = u.MiddleInitial,
                   Address1 = a.Address1,
                   Address2 = a.Address2,
                   City = a.City,
                   State = sc.StateAbbr,
                   Zip = a.ZipCode,
                   Phone = p.Phone1,
                   Email = u.Email,
                   BillType = b.BillType1,
                   UserPK = u.UserPK,
                   StateID = sc.StateAbbrPK,
                   CustomerNote=(n.Note==null?null:n.Note),
                   isActive = u.IsActive 
               };

            //viewmodel2 might not be needed
            var viewModel2 =
                from e in dbcontext.Employees
                join eca in dbcontext.EmployeeCustomerAssociations on e.EmployeePK equals eca.EmployeeFK
                join et in dbcontext.EmployeeTypes on e.EmployeeTypeFK equals et.EmployeeTypeID
                join u in dbcontext.Users on e.UsersFK equals u.UserPK
                where (eca.CustomerFK == id && et.EmployeeType1.ToLower() == "rep")
                select new EmployeeCustomerAssociaitionModel
                {
                    AccountNumber = eca.CustomerFK,
                    EmployeeType = et.EmployeeType1,
                    employeePK = e.EmployeePK,
                    FirstName = u.FirstName,
                    MiddleInitial = u.MiddleInitial,
                    LastName = u.LastName,
                    EmployeeUserPK = u.UserPK
                };


            var selectedrep =
                from e in dbcontext.Employees
                join eca in dbcontext.EmployeeCustomerAssociations on e.EmployeePK equals eca.EmployeeFK
                join et in dbcontext.EmployeeTypes on e.EmployeeTypeFK equals et.EmployeeTypeID
                join u in dbcontext.Users on e.UsersFK equals u.UserPK
                where (eca.CustomerFK == id && et.EmployeeType1.ToLower()=="rep")
                select new EmployeeCustomerAssociaitionModel
                {
                    AccountNumber = eca.CustomerFK,
                    EmployeeType = et.EmployeeType1,
                    employeePK = e.EmployeePK,
                    FirstName = u.FirstName,
                    MiddleInitial = u.MiddleInitial,
                    LastName = u.LastName,
                    EmployeeUserPK = u.UserPK
                };


            var selectedsales =
                from e in dbcontext.Employees
                join eca in dbcontext.EmployeeCustomerAssociations on e.EmployeePK equals eca.EmployeeFK
                join et in dbcontext.EmployeeTypes on e.EmployeeTypeFK equals et.EmployeeTypeID
                join u in dbcontext.Users on e.UsersFK equals u.UserPK
                where (eca.CustomerFK == id && et.EmployeeType1.ToLower() == "sales")
                select new EmployeeCustomerAssociaitionModel
                {
                    AccountNumber = eca.CustomerFK,
                    EmployeeType = et.EmployeeType1,
                    employeePK = e.EmployeePK,
                    FirstName = u.FirstName,
                    MiddleInitial = u.MiddleInitial,
                    LastName = u.LastName,
                    EmployeeUserPK = u.UserPK
                };


            /////////////////////////////////////////////////////////////////////////////////
            //for reps dropdown
            /////////////////////////////////////////////////////////////////////////////////
            var allreps = from e in dbcontext.Employees
                          //join eca in dbcontext.EmployeeCustomerAssociations on e.EmployeePK equals eca.EmployeeFK
                          join et in dbcontext.EmployeeTypes on e.EmployeeTypeFK equals et.EmployeeTypeID
                          join u in dbcontext.Users on e.UsersFK equals u.UserPK
                          where (et.EmployeeType1 == "Rep")
                          select new EmployeeCustomerAssociaitionModel
                          {
                              //AccountNumber = eca.CustomerFK,
                              employeePK = e.EmployeePK,
                              EmployeeType = et.EmployeeType1,
                              EmployeeUserPK = u.UserPK,
                              FirstName = u.FirstName,
                              LastName = u.LastName,
                              MiddleInitial = u.MiddleInitial
                          };

            /////////////////////////////////////////////////////////////////////////////////
            //for sales dropdown
            /////////////////////////////////////////////////////////////////////////////////
            var allsalespersons = from e in dbcontext.Employees
                                  //join eca in dbcontext.EmployeeCustomerAssociations on e.EmployeePK equals eca.EmployeeFK
                                  join et in dbcontext.EmployeeTypes on e.EmployeeTypeFK equals et.EmployeeTypeID
                                  join u in dbcontext.Users on e.UsersFK equals u.UserPK
                                  where (et.EmployeeType1 == "Sales")
                                  select new EmployeeCustomerAssociaitionModel
                                  {
                                      //AccountNumber = eca.CustomerFK,
                                      employeePK = e.EmployeePK,
                                      EmployeeType = et.EmployeeType1,
                                      EmployeeUserPK = u.UserPK,
                                      FirstName = u.FirstName,
                                      LastName = u.LastName,
                                      MiddleInitial = u.MiddleInitial
                                  };


            CustomerAccountWithEmployeeModel x = new CustomerAccountWithEmployeeModel();
            x.CustomerAccount = viewModel.Single(c => c.AccountNumber == id);
            x.EmployeeCustomerAssociaitionModel = viewModel2;


            /////////////////////////////////////////////////////////////////////////////////
            //for state dropdown
            /////////////////////////////////////////////////////////////////////////////////
            var allstates = (from sc in dbcontext.StateCodes select sc).ToList();
            x.SelectedStateID = viewModel.Select(r => r.StateID).FirstOrDefault();
            x.States = allstates.Select(r => new SelectListItem
                        {
                            Value = r.StateAbbrPK.ToString(),
                            Text = r.StateAbbr.ToString()
                        });


            /////////////////////////////////////////////////////////////////////////////////
            //for reps dropdown
            /////////////////////////////////////////////////////////////////////////////////
            x.SelectedRepID = selectedrep.ToList().First().employeePK ;

            //x.Reps=allreps.Select(r=>new SelectListItem 
            //            {
            //                Value=r.employeePK.ToString() ,
            //                 Text=r.FirstName
            //            });

            //workaround below (same as for state dropdown but with lists, in order to get over the error i get above) ; need to refactor later!!
            List<EmployeeCustomerAssociaitionModel> ll1 = allreps.ToList();
            List<SelectListItem> sl1 = new List<SelectListItem>();
            foreach (EmployeeCustomerAssociaitionModel s in ll1)
            {
                SelectListItem mylistitem = new SelectListItem();
                mylistitem.Text = s.FirstName + " " + s.LastName;
                mylistitem.Value = s.employeePK.ToString();
                sl1.Add(mylistitem);
            }
            x.Reps = sl1;



            /////////////////////////////////////////////////////////////////////////////////
            //for salespersons drowdown
            /////////////////////////////////////////////////////////////////////////////////

            //x.SelectedSalesPersonID = viewModel2.Select(r => r.employeePK).FirstOrDefault();
            x.SelectedSalesPersonID = selectedsales.ToList().First().employeePK;
            //x.SalesPersons = allreps.Select(r => new SelectListItem
            //{
            //    //Value = r.EmployeeUserPK.ToString(),
            //    Value = r.employeePK.ToString(),
            //    Text = r.FirstName.ToString()
            //});
            //workaround below (same as for state dropdown but with lists, in order to get over the error) ; need to refactor later!!
            List<EmployeeCustomerAssociaitionModel> ll2 = allsalespersons.ToList();
            List<SelectListItem> sl2 = new List<SelectListItem>();
            foreach (EmployeeCustomerAssociaitionModel s in ll2)
            {
                SelectListItem mylistitem = new SelectListItem();
                mylistitem.Text = s.FirstName + " " + s.LastName;
                mylistitem.Value = s.employeePK.ToString();
                sl2.Add(mylistitem);
            }
            x.SalesPersons = sl2;


            return View(x);


        }
        public ActionResult Add(CustomerAccountWithEmployeeModel m)
        {

            try
            {
                SemplestEntities dbcontext = new SemplestEntities();

                BillType bt = dbcontext.BillTypes.First(p => p.BillType1 == "Flat Fee"); // --- feees --- !!!

                ProductGroupCycleType pgct = dbcontext.ProductGroupCycleTypes.First(p => p.ProductGroupCycleType1 == "Product Group Cycle 30");

                Customer c = dbcontext.Customers.Add(new Customer { Name = m.CustomerAccount.Customer, BillType = bt, ProductGroupCycleType = pgct });
                User u = dbcontext.Users.Add(new User
                {
                    Customer = c,
                    Email = m.CustomerAccount.Email,
                    FirstName = m.CustomerAccount.FirstName,
                    LastName = m.CustomerAccount.LastName,
                    MiddleInitial = m.CustomerAccount.MiddleInitial,
                    IsActive=m.CustomerAccount.isActive 
                });

                Credential cr = dbcontext.Credentials.Add(new Credential { User = u, UsersFK = u.UserPK, Username = m.CustomerAccount.Email, Password = "******" }); //-- default password --- !!

                PhoneType pt = dbcontext.PhoneTypes.First(p => p.PhoneType1 == "Business"); // --- phone types --- !!!!
                Phone ph = dbcontext.Phones.Add(new Phone { Phone1 = m.CustomerAccount.Phone, PhoneType = pt });
                CustomerPhoneAssociation cpa = dbcontext.CustomerPhoneAssociations.Add(new CustomerPhoneAssociation { Customer = c, Phone = ph });

                StateCode sc = dbcontext.StateCodes.First(p => p.StateAbbrPK == m.SelectedStateID);
                AddressType at = dbcontext.AddressTypes.First(p => p.AddressType1 == "H"); // --- address types --- !!!
                Address a = dbcontext.Addresses.Add(new Address { Address1 = m.CustomerAccount.Address1, Address2 = m.CustomerAccount.Address2, City = m.CustomerAccount.City, ZipCode = m.CustomerAccount.Zip, StateCode = sc });
                CustomerAddressAssociation caa = dbcontext.CustomerAddressAssociations.Add(new CustomerAddressAssociation { Address = a, Customer = c, AddressType = at });

                CustomerNote cn = dbcontext.CustomerNotes.Add(new CustomerNote { Customer=c, Note=m.CustomerAccount.CustomerNote });
                

                EmployeeCustomerAssociation addrep = dbcontext.EmployeeCustomerAssociations.Add(new EmployeeCustomerAssociation { Customer=c, EmployeeFK=m.SelectedRepID });
                EmployeeCustomerAssociation addsales = dbcontext.EmployeeCustomerAssociations.Add(new EmployeeCustomerAssociation { Customer = c, EmployeeFK = m.SelectedSalesPersonID });

                dbcontext.SaveChanges();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.TargetSite);
            }


            return RedirectToAction("Index");
        }
        public ActionResult Add()
        {

            SemplestEntities dbcontext = new SemplestEntities();

            /////////////////////////////////////////////////////////////////////////////////
            //for reps dropdown
            /////////////////////////////////////////////////////////////////////////////////
            var allreps = from e in dbcontext.Employees
                          //join eca in dbcontext.EmployeeCustomerAssociations on e.EmployeePK equals eca.EmployeeFK
                          join et in dbcontext.EmployeeTypes on e.EmployeeTypeFK equals et.EmployeeTypeID
                          join u in dbcontext.Users on e.UsersFK equals u.UserPK
                          where (et.EmployeeType1 == "Rep")
                          select new EmployeeCustomerAssociaitionModel
                          {
                              //AccountNumber = eca.CustomerFK,
                              employeePK = e.EmployeePK,
                              EmployeeType = et.EmployeeType1,
                              EmployeeUserPK = u.UserPK,
                              FirstName = u.FirstName,
                              MiddleInitial = u.MiddleInitial,
                              LastName = u.LastName
                          };






            /////////////////////////////////////////////////////////////////////////////////
            //for sales dropdown
            /////////////////////////////////////////////////////////////////////////////////
            var allsalespersons = from e in dbcontext.Employees
                                  //join eca in dbcontext.EmployeeCustomerAssociations on e.EmployeePK equals eca.EmployeeFK
                                  join et in dbcontext.EmployeeTypes on e.EmployeeTypeFK equals et.EmployeeTypeID
                                  join u in dbcontext.Users on e.UsersFK equals u.UserPK
                                  where (et.EmployeeType1 == "Sales")
                                  select new EmployeeCustomerAssociaitionModel
                                  {
                                      //AccountNumber = eca.CustomerFK,
                                      employeePK = e.EmployeePK,
                                      EmployeeType = et.EmployeeType1,
                                      EmployeeUserPK = u.UserPK,
                                      FirstName = u.FirstName,
                                      MiddleInitial = u.MiddleInitial,
                                      LastName = u.LastName
                                  };


            CustomerAccountWithEmployeeModel x = new CustomerAccountWithEmployeeModel();
            //x.CustomerAccount = //viewModel.Single(c => c.AccountNumber == id);
            //x.EmployeeCustomerAssociaitionModel = viewModel2;


            /////////////////////////////////////////////////////////////////////////////////
            //for state dropdown
            /////////////////////////////////////////////////////////////////////////////////
            var allstates = (from sc in dbcontext.StateCodes select sc).ToList();
            x.SelectedStateID = -1;//viewModel.Select(r => r.StateID).FirstOrDefault();
            x.States = allstates.Select(r => new SelectListItem
            {
                Value = r.StateAbbrPK.ToString(),
                Text = r.StateAbbr.ToString()
            });


            /////////////////////////////////////////////////////////////////////////////////
            //for reps dropdown
            /////////////////////////////////////////////////////////////////////////////////
            x.SelectedRepID = -1; //viewModel2.Select(r => r.employeePK).FirstOrDefault();

            //x.Reps=allreps.Select(r=>new SelectListItem 
            //            {
            //                Value=r.employeePK.ToString() ,
            //                 Text=r.FirstName
            //            });

            //workaround below (same as for state dropdown but with lists, in order to get over the error i get above) ; need to refactor later!!
            List<EmployeeCustomerAssociaitionModel> ll1 = allreps.ToList();
            List<SelectListItem> sl1 = new List<SelectListItem>();
            foreach (EmployeeCustomerAssociaitionModel s in ll1)
            {
                SelectListItem mylistitem = new SelectListItem();
                mylistitem.Text = s.FirstName + " " + s.LastName;
                mylistitem.Value = s.EmployeeUserPK.ToString();
                sl1.Add(mylistitem);
            }
            x.Reps = sl1;



            /////////////////////////////////////////////////////////////////////////////////
            //for salespersons drowdown
            /////////////////////////////////////////////////////////////////////////////////

            x.SelectedSalesPersonID = -1; //viewModel2.Select(r => r.employeePK).FirstOrDefault();
            //x.SalesPersons = allreps.Select(r => new SelectListItem
            //{
            //    //Value = r.EmployeeUserPK.ToString(),
            //    Value = r.employeePK.ToString(),
            //    Text = r.FirstName.ToString()
            //});
            //workaround below (same as for state dropdown but with lists, in order to get over the error) ; need to refactor later!!
            List<EmployeeCustomerAssociaitionModel> ll2 = allsalespersons.ToList();
            List<SelectListItem> sl2 = new List<SelectListItem>();
            foreach (EmployeeCustomerAssociaitionModel s in ll2)
            {
                SelectListItem mylistitem = new SelectListItem();
                mylistitem.Text = s.FirstName + " " + s.LastName;
                mylistitem.Value = s.EmployeeUserPK.ToString();
                sl2.Add(mylistitem);
            }
            x.SalesPersons = sl2;


            return View(x);
        }
        public ActionResult Edit(CustomerAccountWithEmployeeModel m)
        {

            SemplestEntities dbcontext = new SemplestEntities();


            var user = dbcontext.Users.ToList().Find(p => p.UserPK == m.CustomerAccount.UserPK);
            user.FirstName = m.CustomerAccount.FirstName;
            user.LastName = m.CustomerAccount.LastName;
            user.MiddleInitial = m.CustomerAccount.MiddleInitial;
            user.Email = m.CustomerAccount.Email;
            user.EditedDate = DateTime.Now;
            user.IsActive = m.CustomerAccount.isActive;
            UpdateModel(user);

            
            var customer = dbcontext.Customers.ToList().Find(p => p.CustomerPK == m.CustomerAccount.AccountNumber);
            var customeraddressassociation = dbcontext.CustomerAddressAssociations.ToList().Find(p => p.CustomerFK == customer.CustomerPK);
            var address = dbcontext.Addresses.ToList().Find(p => p.AddressPK == customeraddressassociation.AddressFK);
            

            customer.Name = m.CustomerAccount.Customer;
            
            address.Address1 = m.CustomerAccount.Address1;
            address.Address2 = m.CustomerAccount.Address2;
            address.City = m.CustomerAccount.City;
            address.ZipCode = m.CustomerAccount.Zip;
            address.EditedDate = DateTime.Now;
            address.StateAbbrFK = m.SelectedStateID;



            var rep = from c in dbcontext.Customers
                      join eca in dbcontext.EmployeeCustomerAssociations on c.CustomerPK equals eca.CustomerFK
                      join e in dbcontext.Employees on eca.EmployeeFK equals e.EmployeePK
                      join et in dbcontext.EmployeeTypes on e.EmployeeTypeFK equals et.EmployeeTypeID
                      where (c.CustomerPK == m.CustomerAccount.AccountNumber && et.EmployeeType1.ToLower() == "rep")
                      select new ECAModel
                      {
                            CustomerPK=c.CustomerPK,
                             EmployeePK = e.EmployeePK
                      };

            var sales = from c in dbcontext.Customers
                      join eca in dbcontext.EmployeeCustomerAssociations on c.CustomerPK equals eca.CustomerFK
                      join e in dbcontext.Employees on eca.EmployeeFK equals e.EmployeePK
                      join et in dbcontext.EmployeeTypes on e.EmployeeTypeFK equals et.EmployeeTypeID
                      where (c.CustomerPK == m.CustomerAccount.AccountNumber && et.EmployeeType1.ToLower()  == "sales")
                      select new ECAModel
                      {
                          CustomerPK = c.CustomerPK,
                          EmployeePK = e.EmployeePK
                      };

            //rep.ToList().First().EmployeePK = m.SelectedRepID;
            //sales.ToList().First().EmployeePK = m.SelectedSalesPersonID;

            var employeesales= dbcontext.EmployeeCustomerAssociations.ToList().Find(p => p.CustomerFK==m.CustomerAccount.AccountNumber && p.EmployeeFK==sales.ToList().First().EmployeePK);
            employeesales.EmployeeFK = m.SelectedSalesPersonID;


            var employeerep = dbcontext.EmployeeCustomerAssociations.ToList().Find(p => p.CustomerFK == m.CustomerAccount.AccountNumber && p.EmployeeFK == rep.ToList().First().EmployeePK);
            employeerep.EmployeeFK = m.SelectedRepID;



            UpdateModel(employeerep);
            UpdateModel(employeesales);
            //var employeecustomerassociation = dbcontext.EmployeeCustomerAssociations.ToList().Find(p => p.CustomerFK  == customer.CustomerPK && p.EmployeeFK==1);
            
            
            //employeecustomerassociation.
            //employeecustomerassociation.EmployeeCustomerAssociationPK

            UpdateModel(address);
            var customernote=dbcontext.CustomerNotes.ToList().FirstOrDefault(p=>p.CustomerFK==m.CustomerAccount.AccountNumber);
            customernote.Note = m.CustomerAccount.CustomerNote;
            UpdateModel(customernote);
            
            

            dbcontext.SaveChanges();



            //Role ro = _dbContext.Roles.Add(new Role { RoleName = f["roleName"].ToString() });
            //_dbContext.SaveChanges();
            //foreach (sp_GetRigtsRolesInteraction_Result s in userRights)
            //{
            //    RolesRightsAssociation ra = new RolesRightsAssociation
            //    {
            //        IsReadonly = s.IsReadonly ==null ? false : bool.Parse(s.IsReadonly.ToString()), IsVisible = s.IsVisible == null ? false : bool.Parse(s.IsVisible.ToString()), RightsFK = s.RightsPK, RolesFK = ro.RolePK
            //    };
            //    _dbContext.RolesRightsAssociations.Add(ra);
            //}
            //_dbContext.SaveChanges();



            ////repopulate states ddl
            //var states = (from sc in dbcontext.StateCodes select sc).ToList();
            //m.States = states.Select(r => new SelectListItem
            // {
            //     Value = r.StateAbbrPK.ToString(),
            //     Text = r.StateAbbr.ToString()
            // });


            ////repopulate reps ddl
            //var allreps = from e in dbcontext.Employees
            //              join eca in dbcontext.EmployeeCustomerAssociations on e.EmployeePK equals eca.EmployeeFK
            //              join et in dbcontext.EmployeeTypes on e.EmployeeTypeFK equals et.EmployeeTypeID
            //              join u in dbcontext.Users on e.UsersFK equals u.UserPK
            //              where (et.EmployeeType1 == "Rep")
            //              select new EmployeeCustomerAssociaitionModel
            //              {
            //                  AccountNumber = eca.CustomerFK,
            //                  employeePK = e.EmployeePK,
            //                  EmployeeType = et.EmployeeType1,
            //                  EmployeeUserPK = u.UserPK,
            //                  FirstName = u.FirstName,
            //                  LastName = u.LastName
            //              };
            //List<EmployeeCustomerAssociaitionModel> ll1 = allreps.ToList();
            //List<SelectListItem> sl1 = new List<SelectListItem>();
            //foreach (EmployeeCustomerAssociaitionModel s in ll1)
            //{
            //    SelectListItem mylistitem = new SelectListItem();
            //    mylistitem.Text = s.FirstName + " " + s.LastName;
            //    mylistitem.Value = s.EmployeeUserPK.ToString();
            //    sl1.Add(mylistitem);
            //}
            //m.Reps = sl1;



            ////repopulate salepersons ddl
            ////for reps dropdown
            //var allsalespersons = from e in dbcontext.Employees
            //              join eca in dbcontext.EmployeeCustomerAssociations on e.EmployeePK equals eca.EmployeeFK
            //              join et in dbcontext.EmployeeTypes on e.EmployeeTypeFK equals et.EmployeeTypeID
            //              join u in dbcontext.Users on e.UsersFK equals u.UserPK
            //              where (et.EmployeeType1 == "Sales")
            //              select new EmployeeCustomerAssociaitionModel
            //              {
            //                  AccountNumber = eca.CustomerFK,
            //                  employeePK = e.EmployeePK,
            //                  EmployeeType = et.EmployeeType1,
            //                  EmployeeUserPK = u.UserPK,
            //                  FirstName = u.FirstName,
            //                  LastName = u.LastName
            //              };
            //List<EmployeeCustomerAssociaitionModel> ll2 = allsalespersons.ToList();
            //List<SelectListItem> sl2 = new List<SelectListItem>();
            //foreach (EmployeeCustomerAssociaitionModel s in ll2)
            //{
            //    SelectListItem mylistitem = new SelectListItem();
            //    mylistitem.Text = s.FirstName + " " + s.LastName;
            //    mylistitem.Value = s.EmployeeUserPK.ToString();
            //    sl2.Add(mylistitem);
            //}
            //m.SalesPersons = sl2;


            return RedirectToAction("Index");
            //return View("index");
            //return View(m);
        }
        public ActionResult Index(string search)
        {
            //ViewBag.Message = "Welcome to SEMPLEST ADMIN!";
            SemplestEntities dbcontext = new SemplestEntities();
            //FUTURE: add rearch by email and by account number || u.Email.Contains(emailsearch)
            //if (search == null) search = "";
            //var filter;
            

            var viewModel =
               from e in dbcontext.Employees
               join u in dbcontext.Users on e.UsersFK equals u.UserPK
               join et in dbcontext.EmployeeTypes on e.EmployeeTypeFK equals et.EmployeeTypeID
               join ura in dbcontext.UserRolesAssociations on e.UsersFK equals ura.UsersFK
               join r in dbcontext.Roles on ura.RolesFK equals r.RolePK
               //where (u.FirstName.ToLower().Contains(search.ToLower())||u.LastName.ToLower().Contains(search.ToLower())||u.Email.ToLower().Contains(search.ToLower()))
               //where Predicate
               select new EmployeeSetup
               {
                   EmployeePK = e.EmployeePK,
                   EmployeeTypeFK = e.EmployeeTypeFK,
                   EmployeeTypeID = et.EmployeeTypeID,
                   RolesFK = ura.RolesFK,
                   UserPK = u.UserPK,
                   EmployeeType = e.EmployeeType.EmployeeType1,
                   FirstName = u.FirstName,
                   MiddleInitial = u.MiddleInitial,
                   LastName = u.LastName,
                   RoleName = r.RoleName,
                   Email = u.Email,
                   ReportingTo = (e.ReportingTo == null ? -1 : e.ReportingTo.Value),
                   isActive = u.IsActive 
               };

            //ordering by lastname, firstname
            viewModel = viewModel.OrderBy(p => p.LastName).ThenBy(p => p.FirstName); 


            //filtering the search with linqkit (added linqkit package through nugit)
            var predicate = PredicateBuilder.True <EmployeeSetup>();
            if (search != null && search!="")
            {

                predicate = PredicateBuilder.False <EmployeeSetup>();
                predicate = predicate.Or(p => p.FirstName.ToLower().Contains(search.ToLower()));
                predicate = predicate.Or(p => p.LastName.ToLower().Contains(search.ToLower()));
                predicate = predicate.Or(p => p.Email.ToLower().Contains(search.ToLower()));
                viewModel = viewModel.AsExpandable().Where(predicate);

            }
            

            
            return View(viewModel);
        }
        public ActionResult Add(EmployeeSetupWithRolesModel m)
        {

            try
            {
                SemplestEntities dbcontext = new SemplestEntities();

                User u = dbcontext.Users.Add(new User
                {
                    Customer = null,
                    Email = m.EmployeeSetup.Email,
                    FirstName = m.EmployeeSetup.FirstName,
                    MiddleInitial = m.EmployeeSetup.MiddleInitial,
                    LastName = m.EmployeeSetup.LastName,
                    CustomerFK=null,
                    IsActive=m.EmployeeSetup.isActive 
                });

                Role r= dbcontext.Roles.First(p => p.RolePK   == m.SelectedRoleID );
                UserRolesAssociation ura = dbcontext.UserRolesAssociations.Add(new UserRolesAssociation { Role = r, User = u });

                EmployeeType et = dbcontext.EmployeeTypes.First(p => p.EmployeeTypeID == m.SelectedEmployeeTypeID);
                Employee e = dbcontext.Employees.Add(new Employee { EmployeeType = et, User = u });
                Credential c = dbcontext.Credentials.Add(new Credential { User = u, Username = m.EmployeeSetup.Email, Password = "******" });


                //BillType bt = dbcontext.BillTypes.First(p => p.BillType1 == "Flat Fee"); // --- feees --- !!!

                //ProductGroupCycleType pgct = dbcontext.ProductGroupCycleTypes.First(p => p.ProductGroupCycleType1 == "Product Group Cycle 30");

                //Customer c = dbcontext.Customers.Add(new Customer { Name = m.CustomerAccount.Customer, BillType = bt, ProductGroupCycleType = pgct });
                //User u = dbcontext.Users.Add(new User
                //{
                //    Customer = c,
                //    Email = m.CustomerAccount.Email,
                //    FirstName = m.CustomerAccount.FirstName,
                //    LastName = m.CustomerAccount.LastName
                //});

                //Credential cr = dbcontext.Credentials.Add(new Credential { User = u, UsersFK = u.UserPK, Username = m.CustomerAccount.Email, Password = "******" }); //-- default password --- !!

                //PhoneType pt = dbcontext.PhoneTypes.First(p => p.PhoneType1 == "Business"); // --- phone types --- !!!!
                //Phone ph = dbcontext.Phones.Add(new Phone { Phone1 = m.CustomerAccount.Phone, PhoneType = pt });
                //CustomerPhoneAssociation cpa = dbcontext.CustomerPhoneAssociations.Add(new CustomerPhoneAssociation { Customer = c, Phone = ph });

                //StateCode sc = dbcontext.StateCodes.First(p => p.StateAbbrPK == m.SelectedStateID);
                //AddressType at = dbcontext.AddressTypes.First(p => p.AddressType1 == "H"); // --- address types --- !!!
                //Address a = dbcontext.Addresses.Add(new Address { Address1 = m.CustomerAccount.Address1, Address2 = m.CustomerAccount.Address2, City = m.CustomerAccount.City, ZipCode = m.CustomerAccount.Zip, StateCode = sc });
                //CustomerAddressAssociation caa = dbcontext.CustomerAddressAssociations.Add(new CustomerAddressAssociation { Address = a, Customer = c, AddressType = at });


                dbcontext.SaveChanges();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.TargetSite);
            }


            return RedirectToAction("Index");
        }
        public ActionResult Edit(EmployeeSetupWithRolesModel m)
        {

            SemplestEntities dbcontext = new SemplestEntities();
            
            var user = dbcontext.Users.ToList().Find(p => p.UserPK == m.EmployeeSetup.UserPK);
            user.FirstName = m.EmployeeSetup.FirstName;
            user.MiddleInitial = m.EmployeeSetup.MiddleInitial;
            user.LastName = m.EmployeeSetup.LastName;
            user.Email = m.EmployeeSetup.Email;
            user.EditedDate = DateTime.Now;
            user.IsActive = m.EmployeeSetup.isActive;
            UpdateModel(user);
            //need to add effective date to db ---><><>

            var employee = dbcontext.Employees.ToList().Find(p => p.UsersFK == m.EmployeeSetup.UserPK);
            employee.EmployeeTypeFK = m.SelectedEmployeeTypeID;
            employee.ReportingTo = m.SelectedReportingToID == -1 ?  default(int?) : m.SelectedReportingToID;
            UpdateModel(employee);

            var userrolesassociation = dbcontext.UserRolesAssociations.ToList().Find(p => p.UsersFK == m.EmployeeSetup.UserPK);
            userrolesassociation.RolesFK = m.SelectedRoleID;
            UpdateModel(userrolesassociation);
            dbcontext.SaveChanges();


            ////repopulate states ddl
            //var states = (from sc in dbcontext.StateCodes select sc).ToList();
            //m.States = states.Select(r => new SelectListItem
            //{
            //    Value = r.StateAbbrPK.ToString(),
            //    Text = r.StateAbbr.ToString()
            //});


            ////repopulate reps ddl
            //var allreps = from e in dbcontext.Employees
            //              join eca in dbcontext.EmployeeCustomerAssociations on e.EmployeePK equals eca.EmployeeFK
            //              join et in dbcontext.EmployeeTypes on e.EmployeeTypeFK equals et.EmployeeTypeID
            //              join u in dbcontext.Users on e.UsersFK equals u.UserPK
            //              where (et.EmployeeType1 == "Rep")
            //              select new EmployeeCustomerAssociaitionModel
            //              {
            //                  AccountNumber = eca.CustomerFK,
            //                  employeePK = e.EmployeePK,
            //                  EmployeeType = et.EmployeeType1,
            //                  EmployeeUserPK = u.UserPK,
            //                  FirstName = u.FirstName,
            //                  LastName = u.LastName
            //              };
            //List<EmployeeCustomerAssociaitionModel> ll1 = allreps.ToList();
            //List<SelectListItem> sl1 = new List<SelectListItem>();
            //foreach (EmployeeCustomerAssociaitionModel s in ll1)
            //{
            //    SelectListItem mylistitem = new SelectListItem();
            //    mylistitem.Text = s.FirstName + " " + s.LastName;
            //    mylistitem.Value = s.EmployeeUserPK.ToString();
            //    sl1.Add(mylistitem);
            //}
            //m.Reps = sl1;

            ////repopulate salepersons ddl
            ////for reps dropdown
            //var allsalespersons = from e in dbcontext.Employees
            //                      join eca in dbcontext.EmployeeCustomerAssociations on e.EmployeePK equals eca.EmployeeFK
            //                      join et in dbcontext.EmployeeTypes on e.EmployeeTypeFK equals et.EmployeeTypeID
            //                      join u in dbcontext.Users on e.UsersFK equals u.UserPK
            //                      where (et.EmployeeType1 == "Sales")
            //                      select new EmployeeCustomerAssociaitionModel
            //                      {
            //                          AccountNumber = eca.CustomerFK,
            //                          employeePK = e.EmployeePK,
            //                          EmployeeType = et.EmployeeType1,
            //                          EmployeeUserPK = u.UserPK,
            //                          FirstName = u.FirstName,
            //                          LastName = u.LastName
            //                      };
            //List<EmployeeCustomerAssociaitionModel> ll2 = allsalespersons.ToList();
            //List<SelectListItem> sl2 = new List<SelectListItem>();
            //foreach (EmployeeCustomerAssociaitionModel s in ll2)
            //{
            //    SelectListItem mylistitem = new SelectListItem();
            //    mylistitem.Text = s.FirstName + " " + s.LastName;
            //    mylistitem.Value = s.EmployeeUserPK.ToString();
            //    sl2.Add(mylistitem);
            //}
            //m.SalesPersons = sl2;

            //return View("index");

            return RedirectToAction("Index"); 

        }
        public ActionResult Add()
        {
            SemplestEntities dbcontext = new SemplestEntities();
          

            EmployeeSetupWithRolesModel x = new EmployeeSetupWithRolesModel();
          

            /////////////////////////////////////////////////////////////////////////////////
            //for roles dropdown
            /////////////////////////////////////////////////////////////////////////////////
            var roles = (from r in dbcontext.Roles select r).ToList();
            x.SelectedRoleID = -1;
            x.Roles = roles.Select(r => new SelectListItem
            {
                Value = r.RolePK.ToString(),
                Text = r.RoleName.ToString()
            });


            /////////////////////////////////////////////////////////////////////////////////
            //for Reportingto dropdown
            /////////////////////////////////////////////////////////////////////////////////
            var reportingto = (
                from e in dbcontext.Employees
                join u in dbcontext.Users on e.UsersFK equals u.UserPK
                select new ReportingToModel
                {
                    EmployeePK = e.EmployeePK,
                    FirstName = u.FirstName,
                    LastName = u.LastName
                }
                ).ToList();
            x.SelectedReportingToID = -1;


            //to add exception to dropdownlist - it can be optional, in this case the employee reporting to may be optional
            List<SelectListItem> sli = new List<SelectListItem>();
            sli.Add(new SelectListItem { Value = (-1).ToString(), Text = "Nobody" });


            x.ReportingTo = reportingto.Select(r => new SelectListItem
            {
                Value = r.EmployeePK.ToString(),
                Text = r.FirstName.ToString() + " " + r.LastName.ToString()
            }).Union(sli);

            /////////////////////////////////////////////////////////////////////////////////
            // for employeetype dropdown
            /////////////////////////////////////////////////////////////////////////////////
            var employeetypes = (from r in dbcontext.EmployeeTypes select r).ToList();
            x.SelectedEmployeeTypeID = -1; //viewModel.Select(r => r.EmployeeTypeID).FirstOrDefault();
            x.EmployeeTypes = employeetypes.Select(r => new SelectListItem
            {
                Value = r.EmployeeTypeID.ToString(),
                Text = r.EmployeeType1.ToString()
            });

            //return View(x);
            return View(x);

        }
        public ActionResult Edit(int id)
        {

            SemplestEntities dbcontext = new SemplestEntities();
            /*
             select employeePK, EmployeeTypeFK, UserPK, FirstName, LastName, Email, EmployeeTypeID, RolesFK, RoleName, EmployeeType 
                        from Employee e join 
                        dbo.Users u on e.usersFK=u.UserPk
						join dbo.employeetype et on e.employeetypefk= et.employeetypeid
						join dbo.UserRolesAssociation ura on ura.usersFK=e.usersFK
						join dbo.roles r on r.rolePK = ura.rolesFK
             */
            var viewModel =
               from e in dbcontext.Employees
               join u in dbcontext.Users on e.UsersFK equals u.UserPK
               join et in dbcontext.EmployeeTypes on e.EmployeeTypeFK equals et.EmployeeTypeID
               join ura in dbcontext.UserRolesAssociations on e.UsersFK equals ura.UsersFK
               join r in dbcontext.Roles on ura.RolesFK equals r.RolePK
               where u.UserPK.Equals(id)
               select new EmployeeSetup
               {
                   EmployeePK = e.EmployeePK,
                   EmployeeTypeFK = e.EmployeeTypeFK,
                   EmployeeTypeID = et.EmployeeTypeID,
                   RolesFK = ura.RolesFK,
                   UserPK = u.UserPK,
                   EmployeeType = e.EmployeeType.EmployeeType1,
                   FirstName = u.FirstName,
                   LastName = u.LastName,
                   RoleName = r.RoleName,
                   Email = u.Email,
                    ReportingTo=(e.ReportingTo==null?-1:e.ReportingTo.Value),
                    isActive=u.IsActive 
               };

            EmployeeSetupWithRolesModel x = new EmployeeSetupWithRolesModel();
            x.EmployeeSetup = viewModel.FirstOrDefault(c => c.UserPK.Equals(id));
             

            /////////////////////////////////////////////////////////////////////////////////
            //for roles dropdown
            /////////////////////////////////////////////////////////////////////////////////
            var roles  = (from r in dbcontext.Roles  select r).ToList();
            x.SelectedRoleID = viewModel.Select(r => r.RolesFK).FirstOrDefault();
            x.Roles = roles.Select(r => new SelectListItem
            {
            Value = r.RolePK.ToString() ,
            Text = r.RoleName.ToString()
            });



            /////////////////////////////////////////////////////////////////////////////////
            //for Reportingto dropdown
            /////////////////////////////////////////////////////////////////////////////////
            var reportingto = (
                from e in dbcontext.Employees
                join u in dbcontext.Users on e.UsersFK equals u.UserPK
                select new ReportingToModel 
                {
                     EmployeePK =e.EmployeePK,
                     FirstName=u.FirstName,
                     LastName=u.LastName 
                }
                ).ToList();
            x.SelectedReportingToID= viewModel.Select(r => r.ReportingTo).FirstOrDefault();


            //to add exception to dropdownlist - it can be optional, in this case the employee reporting to may be optional
            List<SelectListItem> sli = new List<SelectListItem>();
            sli.Add(new SelectListItem { Value = (-1).ToString(), Text = "Nobody" });


            x.ReportingTo= reportingto.Select(r => new SelectListItem
            {
                Value = r.EmployeePK.ToString(),
                Text = r.FirstName.ToString() + " " + r.LastName.ToString()
            }).Union(sli);

            /////////////////////////////////////////////////////////////////////////////////
            // for employeetype dropdown
            /////////////////////////////////////////////////////////////////////////////////
            var employeetypes = (from r in dbcontext.EmployeeTypes  select r).ToList();
            x.SelectedEmployeeTypeID  = viewModel.Select(r => r.EmployeeTypeID).FirstOrDefault();
            x.EmployeeTypes = employeetypes.Select(r => new SelectListItem
            {
                Value = r.EmployeeTypeID.ToString(),
                Text = r.EmployeeType1.ToString()
            });

            //return View(x);
            return View(x);
        }