public async Task <IEnumerable <string> > GetCategoriesAsync(string shopSignId)
        {
            var categories = new List <string>();
            var articles   = await _articleRepository.ListAllAsync();

            var user = await _userRepository.GetByIdAsync(Thread.CurrentPrincipal.Identity.Name);

            IEnumerable <PriceList> prices = await _articlePriceRepository.ListAsync(new { shopSignId, user.Rate });

            var results = articles.Join(
                prices,
                a => a.Id,
                p => p.ArticleId,
                (a, p) => a.Category);

            return(results.Distinct());
        }
        // GET: Favourite
        public async Task <ActionResult> Index()
        {
            IEnumerable <Favourite> favourites = await _favouriteRepository.ListAsync(new { UserName = Thread.CurrentPrincipal.Identity.Name });

            List <FavouriteViewModel> model = new List <FavouriteViewModel>();

            var cache = await _articleRepository.ListAllAsync();

            //IEnumerable<PriceList> prices = await _articlePriceRepository.ListAsync(new { model.ShopSignId, model.User.Rate });

            foreach (Favourite fav in favourites)
            {
                model.Add(new FavouriteViewModel()
                {
                    Name       = fav.Name,
                    User       = fav.UserName,
                    LastUpdate = fav.LastUpdate
                });
            }
            return(View(model));
        }
        public async Task <HomeViewModel> GetCatalogItems(HomeViewModel model, bool isPost)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            //Set user
            model.User = await _userRepository.GetByIdAsync(Thread.CurrentPrincipal.Identity.Name);

            var shops = await _shopSignRepository.ListAllAsync();

            List <SelectListItem> shopItems = new List <SelectListItem>()
            {
                new SelectListItem()
                {
                    Value = string.Empty, Text = "--", Selected = true
                }
            };

            foreach (ShopSign shop in shops)
            {
                shopItems.Add(new SelectListItem()
                {
                    Text = shop.Name, Value = shop.Id
                });
            }
            model.ShopSigns = shopItems;

            var cache = await _articleRepository.ListAllAsync();

            IEnumerable <Article> articles = new List <Article>();

            if (isPost)
            {
                IEnumerable <PriceList> prices = await _articlePriceRepository.ListAsync(new { model.ShopSignId, model.User.Rate });

                var ap = cache.Join(
                    prices,
                    a => a.Id,
                    p => p.ArticleId,
                    (a, p) => new
                {
                    Article   = a,
                    PriceList = p
                })
                         .Where(f => (f.Article.IsKasanova == (model.ShopSignId == "K" ? true : false) ||
                                      f.Article.IsKasanovaPiu == (model.ShopSignId == "K+" ? true : false) ||
                                      (f.Article.IsCoimport == (model.ShopSignId == "COI" ? true : false)) ||
                                      (f.Article.IsOutlet == (model.ShopSignId == "ODK" ? true : false)) ||
                                      (f.Article.IsItalianFactory == (model.ShopSignId == "IT" ? true : false)) ||
                                      (f.Article.IsCasaSullAlbero == (model.ShopSignId == "CSA" ? true : false))) &&
                                (string.IsNullOrEmpty(model.CategoryId) || f.Article.Category == model.CategoryId) &&
                                (string.IsNullOrEmpty(model.FamilyId) || f.Article.Family == model.FamilyId) &&
                                (string.IsNullOrEmpty(model.SeriesId) || f.Article.Series == model.SeriesId) &&
                                (string.IsNullOrEmpty(model.Level1Id) || f.Article.Level1 == model.Level1Id) &&
                                (string.IsNullOrEmpty(model.Level2Id) || f.Article.Level1 == model.Level2Id) &&
                                (model.WebEnabledId == "10" || f.Article.IsMagentoEnabled == (model.WebEnabledId == "20" ? true : false)) &&
                                (model.VideoAvailableId == "10" || f.Article.HasVideo == (model.VideoAvailableId == "20" ? true : false)) &&
                                (model.MandatoryDeliveryId == "10" || f.PriceList.IsMandatoryPickup == (model.VideoAvailableId == "20" ? true : false))
                                //&& (model.WareHouseNameId == "10" || f.wa == (model.WareHouseNameId == "20" ? true : false))
                                && (model.DirectDeliveryId == "10" || f.PriceList.IsDirectDelivery == (model.DirectDeliveryId == "20" ? true : false)) &&
                                (model.ChalcoId == "10" || f.Article.HasChalcoData == (model.ChalcoId == "20" ? true : false)) &&
                                (model.PhotoId == "10" || f.Article.HasPhoto == (model.PhotoId == "20" ? true : false)) &&
                                (string.IsNullOrEmpty(model.StyleId) || model.StyleId.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList().Contains(f.Article.Style))
                                //&& (string.IsNullOrEmpty(model.StockGroupId) || f.StockGroups.Where(s => s.StockGroupId) )
                                )
                         .ToList();

                ap.ForEach(f => model.Articles.Add(
                               new ArticleLightViewModel()
                {
                    Book             = f.Article.Book,
                    HasPhotoInChalco = f.Article.HasChalcoData,
                    Description      = f.Article.Description,
                    Depth            = f.Article.Depth,
                    GrossRetailPrice = f.PriceList.GrossRetailPrice,
                    Height           = f.Article.Height,
                    Id = f.Article.Id,
                    IsDirectDelivery  = f.PriceList.IsDirectDelivery,
                    IsPrivateLabel    = f.PriceList.IsPrivateLabel,
                    IsMandatoryPickup = f.PriceList.IsMandatoryPickup,
                    Line                     = f.Article.Line,
                    Materials                = f.Article.Materials,
                    NameAlias                = f.Article.NameAlias,
                    NetRetailPrice           = f.PriceList.NetRetailPrice,
                    GrossSalesPrice          = f.PriceList.GrossSalesPrice,
                    RetailDiscountPercentage = f.PriceList.RetailDiscountPercentage,
                    Weight                   = f.Article.Weight,
                    Width                    = f.Article.Width,
                    Youtube                  = f.Article.Youtube,
                    Tag                 = f.PriceList.Tag,
                    StockQuantity       = f.Article.StockQuantity,
                    VendorStockQuantity = f.Article.VendorStockQuantity
                }));

                articles = ap.Select(f => f.Article).ToList();
            }
            else
            {
                IEnumerable <PriceList> prices = await _articlePriceRepository.ListAsync(new { model.ShopSignId, model.User.Rate });

                articles = cache.Join(
                    prices,
                    a => a.Id,
                    p => p.ArticleId,
                    (a, p) => a);
                articles = cache;
            }

            //Categoria
            var categories = cache.Select(f => f.Category).Distinct()
                             .OrderBy(f => f);            //TODO add condition for filter shop sign
            List <SelectListItem> items = new List <SelectListItem>()
            {
                new SelectListItem()
                {
                    Value = string.Empty, Text = "--", Selected = true
                }
            };

            foreach (string category in categories)
            {
                items.Add(new SelectListItem()
                {
                    Text = category, Value = category
                });
            }
            model.Categories = items;
            SetSelectedValue(items, model.CategoryId);

            PrepareListItems(model, articles);

            //Style filter (DevExpress List)
            var styles = articles.Select(f => f.Style)
                         .OrderBy(f => f)
                         .Distinct()
                         .ToList();

            model.StyleItems = styles;
            //SetSelectedValue(model.StyleItems, model.StyleId);

            //StockGroups filter (DevExpress List)
            var stockGroups = articles.SelectMany(f => f.StockGroups)
                              .OrderBy(f => f.StockGroupId)
                              .Select(f => f.StockGroupId)
                              .Distinct();

            model.StockGroupItems = stockGroups.ToList();
            //SetSelectedValue(model.StockGroupItems, model.StockGroupId);

            //SupplyStatus filter (DevExpress List)
            IEnumerable <string> supplyStatus = articles.SelectMany(f => f.StockGroups)
                                                .OrderBy(f => f.LastSupplyStatusId)
                                                .Select(s => s.LastSupplyStatusId)
                                                .Distinct();

            model.SupplyStatusItems = supplyStatus.ToList();
            //SetSelectedValue(model.SupplyStatusItems, model.SupplyStatusId);

            //Tag commerciale filter (DevExpress List)
            List <string> tags = model.Articles.Select(f => f.Tag)
                                 .OrderBy(f => f)
                                 .Distinct()
                                 .ToList();

            model.TagItems = tags;

            return(model);
        }