public virtual ActionResult LogOff()
        {
            var swapBranding = false;

            if (HttpContext.User.IsInRole("CompanyOwner"))
            {
                var userId = _currentUser == null ? Guid.Empty : (Guid)(_currentUser.ProviderUserKey ?? Guid.Empty);
                using (var ctx = new MembershipConnection())
                {
                    using (var db = new EightHundredEntities())
                    {
                        swapBranding = ctx.UserFranchise
                                       .Where(uf => uf.UserId == userId)
                                       .ToArray()
                                       .Select(uf => (from f in db.tbl_Franchise where f.FranchiseID == uf.FranchiseID select f).SingleOrDefault())
                                       .Any(f => f.FranchiseTypeID == 6);
                    }
                }
            }

            authenticationService.LogOff();
            SessionContainer.Remove();

            if (swapBranding)
            {
                Response.Redirect("http://www.1800plumber.com");
            }

            return(RedirectToAction("Index", "Home"));
        }
        public ActionResult Index()
        {
            var isOwner = HttpContext.User.IsInRole("CompanyOwner");

            ViewBag.Locked = isOwner;

            var swapBranding = false;

            if (HttpContext.User.IsInRole("CompanyOwner"))
            {
                var userId = _currentUser == null ? Guid.Empty : (Guid)(_currentUser.ProviderUserKey ?? Guid.Empty);
                using (var ctx = new MembershipConnection())
                {
                    using (var db = new EightHundredEntities())
                    {
                        swapBranding = ctx.UserFranchise
                                       .Where(uf => uf.UserId == userId)
                                       .ToArray()
                                       .Select(uf => (from f in db.tbl_Franchise where f.FranchiseID == uf.FranchiseID select f).SingleOrDefault())
                                       .Any(f => f.FranchiseTypeID == 6);
                    }
                }
            }

            ViewBag.SwapBranding = swapBranding;

            return(View());
        }
Esempio n. 3
0
        private void DisconnectItem_Click(object sender, RoutedEventArgs e)
        {
            MembershipConnection.Clear();
            var connectWindow = new ConnectWindow();

            connectWindow.Show();
            this.Close();
        }
        public static SessionContainer Create(MembershipUser user)
        {
            try
            {
                if (user == null)
                {
                    Remove();
                    return(null);
                }

                HttpContext.Current.Session[CONTAINER_KEY] = new SessionContainer {
                    User = user
                };
                var inst = GetInstance();

                inst.UserRoles   = Roles.GetRolesForUser(user.UserName);
                inst.UserKey     = (Guid)(user.ProviderUserKey ?? Guid.Empty);
                inst.Franchises  = new tbl_Franchise[] { };
                inst.IsOwner     = inst.UserRoles.Contains(OWNER_ROLE);
                inst.IsCorporate = inst.UserRoles.Contains(CORPORATE_ROLE);

                using (var db = new EightHundredEntities())
                {
                    IEnumerable <tbl_Franchise> franchises;
                    if (inst.IsCorporate)
                    {
                        franchises = db.tbl_Franchise;
                    }
                    else
                    {
                        using (var ctx = new MembershipConnection())
                        {
                            franchises = ctx.UserFranchise
                                         .Where(uf => uf.UserId == inst.UserKey)
                                         .ToArray()
                                         .Select(uf => db.tbl_Franchise.SingleOrDefault(f => f.FranchiseID == uf.FranchiseID))
                                         .Where(f => f != null);
                        }
                    }

                    inst.Franchises = franchises.OrderBy(f => f.FranchiseNUmber).Distinct().ToArray();
                }

                inst.SwapBranding     = !inst.IsCorporate && inst.Franchises.Any(f => f.FranchiseTypeID == 6);
                inst.DefaultFranchise = inst.Franchises.FirstOrDefault(f => f.FranchiseStatusID == 7 || inst.ShowInactiveFranchises);
                inst.CurrentFranchise = inst.DefaultFranchise;

                return(inst);
            }
            catch (Exception ex)
            {
                Logger.Log("Error loading the user's session container info.", ex, LogLevel.Error);
                Remove();
                return(null);
            }
        }
 private void cboConnections_KeyUp(object sender, KeyEventArgs e)
 {
     if (e.Key == Key.Delete)
     {
         if (_selectedConnection != null)
         {
             Connections.Remove(_selectedConnection);
             _selectedConnection = null;
             UpdateConnectionSettings();
         }
     }
 }
        public void GetFromString_ValidString()
        {
            string connectionString = "<add name=\"Name\" connectionString=\"Data Source=Server;Initial Catalog=Database;User ID=Username;Password=Password;\" applicationName=\"ApplicationName\" />";
            var    mc = MembershipConnection.GetFromString(connectionString);

            Assert.AreEqual("Name", mc.Name);
            Assert.AreEqual("Server", mc.Server);
            Assert.AreEqual("Database", mc.Database);
            Assert.AreEqual("Username", mc.Username);
            Assert.AreEqual("Password", mc.Password);
            Assert.AreEqual("ApplicationName", mc.ApplicationName);
        }
Esempio n. 7
0
        /// <summary>
        /// Return two lists:
        ///   1)  a list of Company Code not granted to the user
        ///   2)  a list of Company Code granted to the user
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public ActionResult CompanyCodeToUser(string username)
        {
            if (string.IsNullOrEmpty(username))
            {
                return(RedirectToAction("Index", "CompanyCode"));
            }

            MembershipUser user = membershipService.GetUser(username);
            var            AvailableCompanyCodeForUser = default(IList);
            var            AllCompanyCodeForUser       = default(IList);

            var isCorporate = User.IsInRole("Corporate");

            int[] assignedFranchises;


            using (var ctx = new MembershipConnection())
            {
                assignedFranchises = ctx.UserFranchise
                                     .Where(uf => uf.UserId == (Guid)user.ProviderUserKey)
                                     .Select(f => f.FranchiseID)
                                     .ToArray();
            }


            AvailableCompanyCodeForUser = (from a in memberShipContext.MembershipFranchise
                                           where !(from o in memberShipContext.UserFranchise
                                                   where o.UserId == (Guid)user.ProviderUserKey
                                                   select o.FranchiseID).Contains(a.FranchiseID)
                                           select a.FranchiseNumber).Distinct().ToList();


            var model = new GrantCompaniesToUser
            {
                UserName           = username,
                AvailibleCompanies =
                    (string.IsNullOrEmpty(username)
                         ? new SelectList(AllCompanyCodeForUser)
                         : new SelectList(AvailableCompanyCodeForUser)),
                GrantedCompanyCode =
                    memberShipContext.MembershipFranchise
                    .Where(f => assignedFranchises.Contains(f.FranchiseID))
                    .OrderBy(f => f.FranchiseNumber)
                    .Select(d => new SelectListItem
                {
                    Text  = d.FranchiseNumber,
                    Value = d.FranchiseNumber
                })
                    .ToList(),
            };

            return(View(model));
        }
Esempio n. 8
0
        public PartialViewResult PartialPayrollProcessing(string code)
        {
            try
            {
                int   frid        = 0;
                var   membership  = new MembershipService(Membership.Provider);
                var   user        = membership.GetUser(User.Identity.Name);
                var   userId      = user == null ? Guid.Empty : (Guid)(user.ProviderUserKey ?? Guid.Empty);
                var   isCorporate = User.IsInRole("Corporate");
                int[] assignedFranchises;

                if (isCorporate == true)
                {
                    frid = 56;
                }
                else
                {
                    using (var ctx = new MembershipConnection())
                    {
                        assignedFranchises = ctx.UserFranchise
                                             .Where(uf => uf.UserId == userId)
                                             .Select(f => f.FranchiseID)
                                             .ToArray();
                    }
                    if (assignedFranchises.Count() > 0)
                    {
                        frid = assignedFranchises[0];
                    }
                }

                tbl_Payroll objpayroll = (from payroll in db.tbl_Payroll where payroll.FranchiseID == frid select payroll).FirstOrDefault();
                if (objpayroll != null)
                {
                    DateTime dt = Convert.ToDateTime(objpayroll.PayrollDate);

                    ViewBag.Date      = dt.ToShortDateString();
                    ViewBag.PayrollID = objpayroll.PayrollID.ToString();
                }
                else
                {
                    ViewBag.PayrollID = "0";
                    ViewBag.Date      = System.DateTime.Now.ToShortDateString();
                }
                return(PartialView("PartialPayrollProcessing"));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 9
0
        public ActionResult Index(FormCollection formcollection)
        {
            List <Technician> reportTechnicians = new List <Technician>();

            if (User.Identity.Name != "")
            {
                var   membership  = new MembershipService(Membership.Provider);
                var   user        = membership.GetUser(User.Identity.Name);
                var   userId      = user == null ? Guid.Empty : (Guid)(user.ProviderUserKey ?? Guid.Empty);
                var   isCorporate = User.IsInRole("Corporate");
                int[] assignedFranchises;

                using (var ctx = new MembershipConnection())
                {
                    assignedFranchises = ctx.UserFranchise
                                         .Where(uf => uf.UserId == userId)
                                         .Select(f => f.FranchiseID)
                                         .ToArray();
                }

                var franchises = db.tbl_Franchise
                                 .Where(f => isCorporate || assignedFranchises.Contains(f.FranchiseID))
                                 .OrderBy(f => f.FranchiseNUmber)
                                 .Select(d => new { d.FranchiseID, Name = string.Concat(d.FranchiseNUmber, " - ", d.LegalName) })
                                 .ToArray();


                ViewBag.frenchise = franchises;
                var DefaultCompamyName = (from g in memberShipContext.UserFranchise
                                          where g.FranchiseID == 56 && g.UserId == userId
                                          select g.Franchise.FranchiseNumber).FirstOrDefault();
                if (DefaultCompamyName == null)
                {
                    DefaultCompamyName = (from g in memberShipContext.UserFranchise
                                          where g.UserId == userId
                                          select g.Franchise.FranchiseNumber).FirstOrDefault();
                }
                if (Session["code"] == null)
                {
                    ViewBag.code    = DefaultCompamyName;
                    Session["code"] = null;
                }
            }
            else
            {
                return(new RedirectResult("/SGAccount/LogOn"));
            }

            return(View(reportTechnicians));
        }
Esempio n. 10
0
        public override void Initialize(string name, NameValueCollection config)
        {
            var mc = MembershipConnection.GetCurrent();

            config.Set("applicationName", mc.ApplicationName);
            config.Set("connectionStringName", "DummyConnectionString");

            base.Initialize(name, config);

            var       connectionString      = String.Format("data source={0};initial catalog={1};user id={2};password={3};", mc.Server, mc.Database, mc.Username, mc.Password);
            FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);

            connectionStringField.SetValue(this, connectionString);
        }
Esempio n. 11
0
        public virtual ActionResult Index()
        {
            DashboardViewModel viewModel  = new DashboardViewModel();
            MembershipUser     user       = membershipService.GetUser(User.Identity.Name);
            int    TotalCompanyCode       = 0;
            int    TotalUserByCompanyCode = 0;
            int    companyCodeID          = 0;
            string username    = user.UserName;
            var    isCorporate = User.IsInRole("Corporate");

            if (RouteData.Values["id"] != null)
            {
                companyCodeID = int.Parse(Convert.ToString(RouteData.Values["id"]));
            }
            else
            {
                companyCodeID = 56;
            }

            TotalUserByCompanyCode = memberShipContext.UserFranchise
                                     .Where(f => f.FranchiseID == companyCodeID)
                                     .OrderBy(f => f.UserId)
                                     .Select(d => d.UserId).Count();

            viewModel.TotalUserCount        = TotalUserByCompanyCode.ToString();
            viewModel.TotalUsersOnlineCount = membershipService.GetNumberOfUsersOnline().ToString();


            int[] assignedFranchises;
            var   userId = user == null ? Guid.Empty : (Guid)(user.ProviderUserKey ?? Guid.Empty);

            using (var ctx = new MembershipConnection())
            {
                assignedFranchises = ctx.UserFranchise
                                     .Where(uf => uf.UserId == userId)
                                     .Select(f => f.FranchiseID)
                                     .ToArray();
            }
            TotalCompanyCode = memberShipContext.MembershipFranchise
                               .Where(f => assignedFranchises.Contains(f.FranchiseID))
                               .OrderBy(f => f.FranchiseNumber)
                               .Select(d => d.FranchiseNumber).Count();


            viewModel.TotalRolesCount  = roleService.GetAllRoles().Length.ToString();
            viewModel.TotalCompanyCode = Convert.ToString(TotalCompanyCode);
            return(View(viewModel));
        }
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            var mc = new MembershipConnection
            {
                Name            = txtApplicationName.Text,
                Server          = txtServer.Text,
                Database        = txtDatabase.Text,
                Username        = txtUsername.Text,
                Password        = txtPassword.Password,
                ApplicationName = txtApplicationName.Text
            };

            Connections.Add(mc);

            UpdateConnectionSettings();

            OnPropertyChanged("Connections");
        }
        public ConnectWindow()
        {
            InitializeComponent();

            Connections = new ObservableCollection <MembershipConnection>();
            Connections.Add(null);

            var connectionStrings = (StringCollection)Properties.Settings.Default["Connections"];

            if (connectionStrings != null)
            {
                foreach (string cs in connectionStrings)
                {
                    Connections.Add(MembershipConnection.GetFromString(cs));
                }
            }

            DataContext = this;
        }
        public ActionResult Training()
        {
            var isOwner = HttpContext.User.IsInRole("CompanyOwner");

            ViewBag.Locked = isOwner;

            var swapBranding = false;

            List <Training> trainings;

            if (isOwner)
            {
                var userId = _currentUser == null ? Guid.Empty : (Guid)(_currentUser.ProviderUserKey ?? Guid.Empty);
                using (var ctx = new MembershipConnection())
                {
                    swapBranding = ctx.UserFranchise
                                   .Where(uf => uf.UserId == userId)
                                   .ToArray()
                                   .Select(uf => (from f in db.tbl_Franchise where f.FranchiseID == uf.FranchiseID select f).SingleOrDefault())
                                   .Any(f => f.FranchiseTypeID == 6);
                }
            }

            if (swapBranding)
            {
                trainings = db.Trainings
                            .Where(t => t.IsActive == true)
                            .Where(t => t.SiteId == 2).ToList()
                            .OrderBy(t => t.Title).ToList();
            }
            else
            {
                trainings = db.Trainings
                            .Where(t => t.IsActive == true)
                            .Where(t => t.SiteId == 1).ToList()
                            .OrderBy(t => t.Title).ToList();
            }

            ViewBag.SwapBranding = swapBranding;
            return(View(trainings));
        }
Esempio n. 15
0
        public ActionResult HeaderData()
        {
            if (User.Identity.Name != "")
            {
                var   membership  = new MembershipService(Membership.Provider);
                var   user        = membership.GetUser(User.Identity.Name);
                var   userId      = user == null ? Guid.Empty : (Guid)(user.ProviderUserKey ?? Guid.Empty);
                var   isCorporate = User.IsInRole("Corporate");
                int[] assignedFranchises;
                using (var ctx = new MembershipConnection())
                {
                    assignedFranchises = ctx.UserFranchise
                                         .Where(uf => uf.UserId == userId)
                                         .Select(f => f.FranchiseID)
                                         .ToArray();
                }

                ViewBag.FunctionName = "doOnLoad()";
                var franchiseeList = DB.tbl_Franchise
                                     .Where(f => isCorporate || assignedFranchises.Contains(f.FranchiseID))
                                     .OrderBy(f => f.FranchiseNUmber)
                                     .Select(d => new { d.FranchiseID, FranchiseNumber = string.Concat(d.FranchiseNUmber, " - ", d.LegalName) })
                                     .ToList();

                ViewBag.frenchise = franchiseeList;
                var objFranchisee = (from e in DB.tbl_Franchise
                                     join o in DB.tbl_Franchise_Owner on e.OwnerID equals o.OwnerID
                                     where e.FranchiseID == 56
                                     select e).FirstOrDefault();
                ViewBag.FranchiseeId     = objFranchisee.FranchiseID;
                ViewBag.FranchiseeNumber = objFranchisee.FranchiseNUmber + "-" + objFranchisee.LegalName;
            }
            else
            {
                return(new RedirectResult("/SGAccount/LogOn"));
            }
            return(PartialView("Header"));
        }
Esempio n. 16
0
        //Running Code
        public ActionResult UserCompanyCode()
        {
            IMembershipService     membershipService;
            IAuthenticationService authenticationService;

            membershipService     = new MembershipService(Membership.Provider);
            authenticationService = new AuthenticationService(membershipService, new FormsAuthenticationService());

            MembershipUser user        = membershipService.GetUser(User.Identity.Name);
            var            userId      = user == null ? Guid.Empty : (Guid)(user.ProviderUserKey ?? Guid.Empty);
            var            isCorporate = User.IsInRole("Corporate");
            string         username    = user.UserName;

            int[] assignedFranchises;

            var DefaultCompamyName = default(String);
            var DefaultCompanyID   = default(int);

            DefaultCompamyName = (from g in memberShipContext.UserFranchise
                                  where g.FranchiseID == 56 && g.UserId == userId
                                  select g.Franchise.FranchiseNumber).FirstOrDefault();
            if (DefaultCompamyName == null)
            {
                DefaultCompamyName = (from g in memberShipContext.UserFranchise
                                      where g.UserId == userId
                                      select g.Franchise.FranchiseNumber).FirstOrDefault();
            }

            DefaultCompanyID = (from g in memberShipContext.UserFranchise
                                where g.FranchiseID == 56 && g.UserId == userId
                                select g.Franchise.FranchiseID).FirstOrDefault();
            if (DefaultCompanyID == 0)
            {
                DefaultCompanyID = (from g in memberShipContext.UserFranchise
                                    where g.UserId == userId
                                    select g.Franchise.FranchiseID).FirstOrDefault();
            }


            if (RouteData.Values["id"] != null)
            {
                int companyCodeID = int.Parse(Convert.ToString(RouteData.Values["id"]));
                DefaultCompamyName = (from g in memberShipContext.UserFranchise
                                      where g.FranchiseID == companyCodeID && g.UserId == userId
                                      select g.Franchise.FranchiseNumber).FirstOrDefault();
                DefaultCompanyID = (from g in memberShipContext.UserFranchise
                                    where g.FranchiseID == companyCodeID && g.UserId == userId
                                    select g.Franchise.FranchiseID).FirstOrDefault();
            }

            using (var ctx = new MembershipConnection())
            {
                assignedFranchises = ctx.UserFranchise
                                     .Where(uf => uf.UserId == userId)
                                     .Select(f => f.FranchiseID)
                                     .ToArray();
            }

            var model = new GrantCompaniesToUser
            {
                UserName           = username,
                GrantedCompanyCode =
                    memberShipContext.MembershipFranchise
                    .Where(f => assignedFranchises.Contains(f.FranchiseID))
                    .OrderBy(f => f.FranchiseNumber)
                    .Select(d => new SelectListItem
                {
                    Text  = d.FranchiseNumber,
                    Value = SqlFunctions.StringConvert((double)d.FranchiseID)
                })
                    .ToList(),
                defaultCompanyName = DefaultCompamyName,
                defaultCompanyID   = DefaultCompanyID
            };

            return(PartialView("CompanyCodeUser", model));
        }
Esempio n. 17
0
        public ActionResult PayrollProcessing()
        {
            try
            {
                DateTime selectedDate = DateTime.MinValue;
                if (Request.QueryString["dt"] != null)
                {
                    selectedDate = DateTime.Parse(Request.QueryString["dt"]);
                }
                int payrollID = -1;
                if (Request.QueryString["Id"] != null)
                {
                    payrollID = int.Parse(Request.QueryString["Id"]);
                }

                int   frid        = 0;
                var   membership  = new MembershipService(Membership.Provider);
                var   user        = membership.GetUser(User.Identity.Name);
                var   userId      = user == null ? Guid.Empty : (Guid)(user.ProviderUserKey ?? Guid.Empty);
                var   isCorporate = User.IsInRole("Corporate");
                int[] assignedFranchises;

                if (isCorporate == true)
                {
                    frid = 56;
                }
                else
                {
                    using (var ctx = new MembershipConnection())
                    {
                        assignedFranchises = ctx.UserFranchise
                                             .Where(uf => uf.UserId == userId)
                                             .Select(f => f.FranchiseID)
                                             .ToArray();
                    }
                    if (assignedFranchises.Count() > 0)
                    {
                        frid = assignedFranchises[0];
                    }
                }

                // Payroll Dates ALWAYS fall on a Saturday.
                // Give them list of the past 5 Saturdays and if they need farther back send them to Payroll History
                List <SelectListItem> payrollDates = new List <SelectListItem>();
                DateTime dateToAdd = DateTime.Now.Date;
                while (dateToAdd.DayOfWeek != DayOfWeek.Saturday)   // Get the next saturday (including today)
                {
                    dateToAdd = dateToAdd.AddDays(1.0);
                }
                for (int i = 0; i < 5; i++)                         // Add 5 Saturdays to the dropdownlist
                {
                    payrollDates.Add(new SelectListItem()
                    {
                        Text = dateToAdd.ToShortDateString(), Value = dateToAdd.ToShortDateString()
                    });
                    dateToAdd = dateToAdd.AddDays(-7.0);
                }
                if (selectedDate != DateTime.MinValue)
                {
                    SelectListItem itemSelectedDate = (from SelectListItem item in payrollDates
                                                       where DateTime.Parse(item.Value) == selectedDate
                                                       select item).FirstOrDefault();
                    if (itemSelectedDate == null)
                    {
                        do
                        {
                            SelectListItem itemToAdd = new SelectListItem()
                            {
                                Text = dateToAdd.ToShortDateString(), Value = dateToAdd.ToShortDateString()
                            };
                            if (dateToAdd == selectedDate)
                            {
                                itemToAdd.Selected = true;
                                selectedDate       = DateTime.MinValue;
                            }
                            payrollDates.Add(itemToAdd);
                            dateToAdd = dateToAdd.AddDays(-7.0);
                        } while (selectedDate != DateTime.MinValue);
                    }
                    else
                    {
                        itemSelectedDate.Selected = true;
                    }
                }

                // Finally add the "Other" option
                payrollDates.Add(new SelectListItem()
                {
                    Text = "Other", Value = "OTHER_DATE"
                });

                ViewData["DropDownList_PayrollDates"] = payrollDates;

                vRpt_Payroll objpayroll = (from payroll in rptDB.vRpt_Payroll where payroll.FranchiseID == frid && payroll.PayrollID == payrollID select payroll).FirstOrDefault();
                if (objpayroll != null)
                {
                    DateTime dt = Convert.ToDateTime(objpayroll.PayrollDate);

                    ViewBag.Date      = dt.ToShortDateString();
                    ViewBag.PayrollID = objpayroll.PayrollID.ToString();

                    selectedDate = dt;
                    if (selectedDate != DateTime.MinValue)
                    {
                        SelectListItem itemSelectedDate = (from SelectListItem item in payrollDates
                                                           where DateTime.Parse(item.Value) == selectedDate
                                                           select item).FirstOrDefault();
                        if (itemSelectedDate == null)
                        {
                            do
                            {
                                SelectListItem itemToAdd = new SelectListItem()
                                {
                                    Text = dateToAdd.ToShortDateString(), Value = dateToAdd.ToShortDateString()
                                };
                                if (dateToAdd == selectedDate)
                                {
                                    itemToAdd.Selected = true;
                                    selectedDate       = DateTime.MinValue;
                                }
                                payrollDates.Add(itemToAdd);
                                dateToAdd = dateToAdd.AddDays(-7.0);
                            } while (selectedDate != DateTime.MinValue);
                        }
                        else
                        {
                            itemSelectedDate.Selected = true;
                        }
                    }
                }
                else
                {
                    ViewBag.PayrollID = "0";
                    ViewBag.Date      = System.DateTime.Now.ToShortDateString();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(View());
        }
Esempio n. 18
0
        public ActionResult UserCompanyCode()
        {
            IMembershipService     membershipService;
            IAuthenticationService authenticationService;

            membershipService     = new MembershipService(Membership.Provider);
            authenticationService = new AuthenticationService(membershipService, new FormsAuthenticationService());

            MembershipUser user        = membershipService.GetUser(User.Identity.Name);
            var            userId      = user == null ? Guid.Empty : (Guid)(user.ProviderUserKey ?? Guid.Empty);
            var            isCorporate = User.IsInRole("Corporate");
            string         username    = user.UserName;

            int[] assignedFranchises;

            var DefaultCompamyName = default(String);
            var DefaultCompanyID   = default(int);

            if (Session["selectedFranchiseId"] != null)
            {
                DefaultCompanyID   = Convert.ToInt32(Session["selectedFranchiseId"]);
                DefaultCompamyName = (from g in db.tbl_Franchise
                                      where g.FranchiseID == DefaultCompanyID
                                      select string.Concat(g.LegalName, " - ", g.FranchiseNUmber)).FirstOrDefault();
                ViewBag.code = DefaultCompamyName;
            }
            else
            {
                DefaultCompamyName = (from g in memberShipContext.UserFranchise
                                      where g.FranchiseID == 56 && g.UserId == userId
                                      select g.Franchise.FranchiseNumber).FirstOrDefault();
                if (DefaultCompamyName != null)
                {
                    DefaultCompamyName = (from g in db.tbl_Franchise
                                          where g.FranchiseNUmber == DefaultCompamyName
                                          select string.Concat(g.LegalName, " - ", g.FranchiseNUmber)).FirstOrDefault();
                }
                if (DefaultCompamyName == null)
                {
                    DefaultCompamyName = (from g in memberShipContext.UserFranchise
                                          where g.UserId == userId
                                          select g.Franchise.FranchiseNumber).FirstOrDefault();

                    DefaultCompamyName = (from g in db.tbl_Franchise
                                          where g.FranchiseNUmber == DefaultCompamyName
                                          select string.Concat(g.LegalName, " - ", g.FranchiseNUmber)).FirstOrDefault();
                }

                DefaultCompanyID = (from g in memberShipContext.UserFranchise
                                    where g.FranchiseID == 56 && g.UserId == userId
                                    select g.Franchise.FranchiseID).FirstOrDefault();
                if (DefaultCompanyID == 0)
                {
                    DefaultCompanyID = (from g in memberShipContext.UserFranchise
                                        where g.UserId == userId
                                        select g.Franchise.FranchiseID).FirstOrDefault();
                }


                if (RouteData.Values["id"] != null)
                {
                    int companyCodeID = int.Parse(Convert.ToString(RouteData.Values["id"]));
                    DefaultCompamyName = (from g in memberShipContext.UserFranchise
                                          where g.FranchiseID == companyCodeID && g.UserId == userId
                                          select g.Franchise.FranchiseNumber).FirstOrDefault();
                    DefaultCompanyID = (from g in memberShipContext.UserFranchise
                                        where g.FranchiseID == companyCodeID && g.UserId == userId
                                        select g.Franchise.FranchiseID).FirstOrDefault();
                }
            }

            using (var ctx = new MembershipConnection())
            {
                assignedFranchises = ctx.UserFranchise
                                     .Where(uf => uf.UserId == userId)
                                     .Select(f => f.FranchiseID)
                                     .ToArray();
            }

            var model = new GrantCompaniesToUser
            {
                UserName           = username,
                GrantedCompanyCode =
                    db.tbl_Franchise
                    .Where(f => assignedFranchises.Contains(f.FranchiseID))
                    .OrderBy(f => f.FranchiseNUmber)
                    .Select(d => new SelectListItem
                {
                    Text  = string.Concat(d.LegalName, " - ", d.FranchiseNUmber),
                    Value = System.Data.Objects.SqlClient.SqlFunctions.StringConvert((double)d.FranchiseID)
                })
                    .ToList(),
                defaultCompanyName = DefaultCompamyName,
                defaultCompanyID   = DefaultCompanyID
            };

            var swapBranding = false;

            if (HttpContext.User.IsInRole("CompanyOwner"))
            {
                using (var ctx = new MembershipConnection())
                {
                    swapBranding = ctx.UserFranchise
                                   .Where(uf => uf.UserId == userId)
                                   .ToArray()
                                   .Select(uf => (from f in db.tbl_Franchise where f.FranchiseID == uf.FranchiseID select f).SingleOrDefault())
                                   .Any(f => f.FranchiseTypeID == 6);
                }
            }

            ViewBag.SwapBranding = swapBranding;

            return(PartialView("CompanyCodeUser", model));
        }