Exemplo n.º 1
0
        private void InitFields()
        {
            if (!ShopID.HasValue)
            {
                ShopID = HttpContext.Current.Request.QueryString["ShopID"].ToNullInt();
            }
            if (!ShopID.HasValue)
            {
                var index = HttpContext.Current.Request.RawUrl.IndexOf("?");
                if (index < 0)
                {
                    index = HttpContext.Current.Request.RawUrl.Length;
                }
                var parts = HttpContext.Current.Request.RawUrl.Substring(0, index).Split <string>("/").ToList();
                if (parts.Contains("ShopID"))
                {
                    ShopID = parts.ElementAt(parts.IndexOf("ShopID") + 1).ToNullInt();
                }
            }


            if (!StoreID.HasValue)
            {
                StoreID = HttpContext.Current.Request.QueryString["StoreID"].ToNullInt();
            }
            if (!StoreID.HasValue)
            {
                var index = HttpContext.Current.Request.RawUrl.IndexOf("?");
                if (index < 0)
                {
                    index = HttpContext.Current.Request.RawUrl.Length;
                }
                var parts = HttpContext.Current.Request.RawUrl.Substring(0, index).Split <string>("/").ToList();
                if (parts.Contains("StoreID"))
                {
                    StoreID = parts.ElementAt(parts.IndexOf("StoreID") + 1).ToNullInt();
                }
            }


            if (CurrentUser.UserRoles.Contains("ShopOwner"))
            {
                ShopList =
                    DB.Shops.Where(x => x.Owner == CurrentUser.ID)
                    .Select(
                        x =>
                        new SelectListItem()
                {
                    Text     = x.Name,
                    Value    = x.ID.ToString(),
                    Selected = (ShopID ?? 0) == x.ID
                }).ToList();
            }
            else if (CurrentUser.UserRoles.Contains("Manager"))
            {
                ShopList =
                    DB.ShopManagers.Where(x => x.Manager.ManagerUserID == CurrentUser.ID).Select(x => x.Shop).OrderBy(x => x.Name)
                    .Select(
                        x =>
                        new SelectListItem()
                {
                    Text     = x.Name,
                    Value    = x.ID.ToString(),
                    Selected = (ShopID ?? 0) == x.ID
                }).ToList();
            }
            else if (CurrentUser.UserRoles.Contains("Operator"))
            {
                ShopList =
                    DB.OperatorShops.Where(x => x.UserID == CurrentUser.ID).Select(x => x.Shop).OrderBy(x => x.Name)
                    .Select(
                        x =>
                        new SelectListItem()
                {
                    Text     = x.Name,
                    Value    = x.ID.ToString(),
                    Selected = (ShopID ?? 0) == x.ID
                }).ToList();
            }

            ShopList.Insert(0,
                            new SelectListItem()
            {
                Selected = !ShopID.HasValue, Text = "Без привязки к магазину", Value = "0"
            });

            StoreList =
                CurrentUser.StoreList.Select(
                    x =>
                    new SelectListItem()
            {
                Text = x.Name, Value = x.ID.ToString(), Selected = (StoreID ?? -1) == x.ID
            })
                .ToList();

            StoreList.Insert(0,
                             new SelectListItem()
            {
                Selected = !StoreID.HasValue || StoreID.Value == 0,
                Text     = "Без привязки к складу",
                Value    = "0"
            });
        }