//
        // GET: /ProductList/

        public ActionResult ProductList()
        {
            ProductList prodlist = new Models.ProductList();

            ViewData["reportDsource"] = prodlist.GetData();
            return(View());
        }
        public ActionResult Brand(string link, int? page, int? pageSize)
        {
            if (link.IsNullOrTrimmedEmpty())
            {
                return Redirect("/");
            }
            link = link.Replace("_", " ");
            var brand = CatalogService.GetBrandByLink(link);
            if (brand == null)
            {
                return Redirect("/");
            }
            if (!page.HasValue || page < 0)
            {
                page = 0;
            }
            else
            {
                page = page.Value - 1;
            }
            if (page < 0)
            {
                page = 0;
            }
            int count = 0;
            pageSize = pageSize.GetValueOrDefault(ERPStoreApplication.WebSiteSettings.Catalog.PageSize);
            var parameters = CatalogService.RemoveNotFilteredParameters(Request.Url.Query.ToHtmlDecodedNameValueDictionary());
            var category = CatalogService.GetCategoryByCode(Request["category"]);

            var filter = CatalogService.CreateProductListFilter(HttpContext);
            if (category != null)
            {
                filter.ProductCategoryId = category.Id;
            }
            filter.ExtendedParameters = parameters;
            filter.BrandId = brand.Id;

            // var productList = CatalogService.GetProductListByBrandId(brand.Id, parameters, page.Value, pageSize.Value , out count);
            var productList = CatalogService.GetProductListBySearch(filter, page.Value, pageSize.Value, out count);

            // Recherche des tarifs client si celui-ci est connecté
            CatalogService.ApplyBestPrice(productList, User.GetUserPrincipal().CurrentUser);

            var pageIndex = page.Value + 1;

            var result = new Models.ProductList(productList);
            result.Brand = brand;
            result.ItemCount = count;
            result.PageIndex = pageIndex;
            result.PageSize = pageSize.Value;
            result.ExtendedParameters = parameters;
            result.Category = category;
            result.ListType = ERPStore.Models.ProductListType.Brand;

            ViewData.Model = result;
            return View();
        }
        public ActionResult Search(string s, int? page, int? pageSize)
        {
            if (s.IsNullOrTrimmedEmpty())
            {
                return View("NoProductFound");
            }
            var pageId = GetPageId(page);

            s = s.Trim();
            s = Noises.Clean("fr", s);

            int count = 0;
            pageSize = pageSize.GetValueOrDefault(ERPStoreApplication.WebSiteSettings.Catalog.PageSize);
            var parameters = CatalogService.RemoveNotFilteredParameters(Request.Url.Query.ToHtmlDecodedNameValueDictionary());
            var category = CatalogService.GetCategoryByCode(Request["category"]);
            var brand = CatalogService.GetBrandByName(Request["brand"]);

            var filter = CatalogService.CreateProductListFilter(HttpContext);
            filter.ExtendedParameters = parameters;
            filter.Search = s;
            if (category != null)
            {
                filter.ProductCategoryId = category.Id;
            }
            if (brand != null)
            {
                filter.BrandId = brand.Id;
            }
            var productList = CatalogService.GetProductListBySearch(filter, pageId, pageSize.Value, out count);

            if (pageId == 0)
            {
                EventPublisherService.Publish(new Models.Events.ProductSearchEvent(User.GetUserPrincipal(), s, count, CatalogService.Name));
                ViewData["search"] = filter.Search;
                ViewData["resultCount"] = count;
            }

            if (productList == null || productList.Count == 0)
            {
                return View("NoProductFound");
            }

            // Afficher directement la fiche produit
            if (productList.Count == 1)
            {
                var p = productList.First();
                var urlHelper = new UrlHelper(this.ControllerContext.RequestContext);
                var url = urlHelper.Href(p);
                return Redirect(url);
            }

            // Recherche des tarifs client si celui-ci est connecté
            CatalogService.ApplyBestPrice(productList, User.GetUserPrincipal().CurrentUser);

            var result = new Models.ProductList(productList);
            result.Query = s;
            result.ItemCount = count;
            result.PageIndex = pageId + 1;
            result.PageSize = pageSize.Value;
            result.ExtendedParameters = parameters;
            result.Category = category;
            result.Brand = brand;
            result.ListType = ERPStore.Models.ProductListType.Search;

            ViewData.Model = result;
            return View("ProductSearchResult");
        }
        public ActionResult ProductListBySelection(string selectionName, int? page, int? pageSize)
        {
            if (selectionName != null)
            {
                selectionName = selectionName.Trim().TrimEnd('/');
            }
            else
            {
                // return RedirectToAction("CategoryList");
                return RedirectToERPStoreRoute(ERPStoreRoutes.HOME);
            }

            var selectionList = CatalogService.GetProductSelectionList();
            var selection = selectionList.FirstOrDefault(i => i.Name.Equals(selectionName, StringComparison.InvariantCultureIgnoreCase));

            if (selection == null)
            {
                return RedirectToERPStoreRoute(ERPStoreRoutes.HOME);
            }

            if (!page.HasValue || page < 0)
            {
                page = 0;
            }
            else
            {
                page = page.Value - 1;
            }
            if (page < 0)
            {
                page = 0;
            }
            int count = 0;
            pageSize = pageSize.GetValueOrDefault(ERPStoreApplication.WebSiteSettings.Catalog.PageSize);
            var parameters = CatalogService.RemoveNotFilteredParameters(Request.Url.Query.ToHtmlDecodedNameValueDictionary());
            var brand = CatalogService.GetBrandByName(Request["brand"]);
            var category = CatalogService.GetCategoryByLink(Request["category"]);

            var productSearch = CatalogService.CreateProductListFilter(this.HttpContext);
            if (brand != null)
            {
                productSearch.BrandId = brand.Id;
            }
            if (category != null)
            {
                productSearch.ProductCategoryId = category.Id;
            }
            productSearch.ExtendedParameters = parameters;
            productSearch.SelectionId = selection.Id;

            var productList = CatalogService.GetProductListBySearch(productSearch, page.Value, pageSize.Value, out count);

            // Recherche des tarifs client si celui-ci est connecté
            CatalogService.ApplyBestPrice(productList, User.GetUserPrincipal().CurrentUser);

            var pageIndex = page.Value + 1;

            var result = new Models.ProductList(productList);
            result.Category = category;
            result.ItemCount = count;
            result.PageIndex = pageIndex;
            result.PageSize = pageSize.Value;
            result.ExtendedParameters = parameters;
            result.ListType = ERPStore.Models.ProductListType.Category;
            result.Brand = brand;
            result.SelectionName = selection.Name;
            ViewData.Model = result;

            return View("Selection");
        }
        public ActionResult ProductList(Models.ProductListType type, int? page, int? pageSize)
        {
            var pageId = GetPageId(page);

            int count = 0;
            pageSize = pageSize.GetValueOrDefault(ERPStoreApplication.WebSiteSettings.Catalog.PageSize);
            var parameters = CatalogService.RemoveNotFilteredParameters(Request.Url.Query.ToHtmlDecodedNameValueDictionary());
            var category = CatalogService.GetCategoryByCode(Request["category"]);
            var brand = CatalogService.GetBrandByName(Request["brand"]);

            var filter = CatalogService.CreateProductListFilter(HttpContext);

            filter.ExtendedParameters = parameters;
            filter.Search = Request["s"];
            if (category != null)
            {
                filter.ProductCategoryId = category.Id;
            }
            if (brand != null)
            {
                filter.BrandId = brand.Id;
            }

            filter.ListType = type;

            string viewName = null;
            switch (type)
            {
                case ERPStore.Models.ProductListType.Promotional:
                    viewName = "promotions";
                    filter.SortList.Add(new ERPStore.Models.SortProductList()
                    {
                        PropertyName = "CreationDate",
                        Direction = System.ComponentModel.ListSortDirection.Descending,
                    });
                    break;
                case ERPStore.Models.ProductListType.New:
                    viewName = "newproducts";
                    filter.SortList.Add(new ERPStore.Models.SortProductList()
                    {
                        PropertyName = "CreationDate",
                        Direction = System.ComponentModel.ListSortDirection.Descending,
                    });
                    break;
                case ERPStore.Models.ProductListType.Destock:
                    viewName = "destock";
                    break;
                case ERPStore.Models.ProductListType.TopSell:
                    viewName = "topsells";
                    filter.SortList.Add(new ERPStore.Models.SortProductList()
                    {
                        PropertyName = "CreationDate",
                        Direction = System.ComponentModel.ListSortDirection.Descending,
                    });
                    break;
                case ERPStore.Models.ProductListType.FirstPrice:
                    viewName = "firstprice";
                    break;
            }

            var list = CatalogService.GetProductListBySearch(filter, pageId, pageSize.Value, out count);

            var result = new Models.ProductList(list);
            result.ItemCount = count;
            result.PageIndex = pageId + 1;
            result.PageSize = pageSize.Value;
            result.ListType = type;
            result.Brand = brand;
            result.Category = category;

            var user = User.GetUserPrincipal();
            CatalogService.ApplyBestPrice(list, user.CurrentUser);

            ViewData.Model = result;
            return View(viewName);
        }
        public ActionResult CustomerProduct(string s, int? page, int? pageSize)
        {
            var pageId = GetPageId(page);

            int count = 0;
            pageSize = pageSize.GetValueOrDefault(ERPStoreApplication.WebSiteSettings.Catalog.PageSize);
            var parameters = CatalogService.RemoveNotFilteredParameters(Request.Url.Query.ToHtmlDecodedNameValueDictionary());
            var category = CatalogService.GetCategoryByCode(Request["category"]);
            var brand = CatalogService.GetBrandByName(Request["brand"]);

            var filter = CatalogService.CreateProductListFilter(HttpContext);
            filter.ExtendedParameters = parameters;
            filter.Search = s;
            if (category != null)
            {
                filter.ProductCategoryId = category.Id;
            }
            if (brand != null)
            {
                filter.BrandId = brand.Id;
            }
            if (User.GetUserPrincipal().CurrentUser.Corporate != null)
            {
                filter.CorporateId = User.GetUserPrincipal().CurrentUser.Corporate.Id;
            }
            else
            {
                filter.UserId = User.GetUserPrincipal().CurrentUser.Id;
            }
            var productList = CatalogService.GetProductListByCustomer(filter, pageId, pageSize.Value, out count);

            if (productList == null || productList.Count == 0)
            {
                return View("NoProductFound");
            }

            // Recherche des tarifs client si celui-ci est connecté
            CatalogService.ApplyBestPrice(productList, User.GetUserPrincipal().CurrentUser);

            var result = new Models.ProductList(productList);
            result.ItemCount = count;
            result.PageIndex = pageId + 1;
            result.PageSize = pageSize.Value;
            result.ExtendedParameters = parameters;
            result.Category = category;
            result.Brand = brand;
            result.ListType = ERPStore.Models.ProductListType.Customer;

            ViewData.Model = result;
            return View("ProductCustomer");
        }
        public ActionResult Category(string link, int? page, int? pageSize)
        {
            if (link != null)
            {
                link = link.Trim().TrimEnd('/');
            }
            else
            {
                // return RedirectToAction("CategoryList");
                return RedirectToERPStoreRoute(ERPStoreRoutes.PRODUCT_CATEGORIES);
            }
            var category = CatalogService.GetCategoryByLink(link);
            if (category == null)
            {
                // Recherche dans la liste de compensation
                string fileName = Server.MapPath(@"/app_data/categories-compensation.txt");
                var categoryCompensationListFileInfo = new System.IO.FileInfo(fileName);
                if (!categoryCompensationListFileInfo.Exists)
                {
                    Logger.Notification("Missing category : {0} on {1}", link, fileName);
                    return new RedirectResult("/");
                }
                using (var content = categoryCompensationListFileInfo.OpenText())
                {
                    while(true)
                    {
                        var line = content.ReadLine();
                        if (line.IsNullOrTrimmedEmpty())
                        {
                            break;
                        }
                        string[] compensation = line.Split(':');
                        var badlink = compensation[0];
                        if (badlink.Equals(link, StringComparison.InvariantCultureIgnoreCase))
                        {
                            category = CatalogService.GetCategoryByLink(compensation[1]);
                            if (category != null)
                            {
                                break;
                            }
                        }
                    }
                }
                if (category == null)
                {
                    var adminUrl = string.Format("http://{0}/admin/ProductCategoryUrlMovedPermanently?badUrl={1}", ERPStoreApplication.WebSiteSettings.CurrentUrl, link);
                    Logger.Notification("Missing category : {0}\r\n {1}", link, adminUrl);
                    return new RedirectResult("/");
                }
                else
                {
                    var urlHelper = new UrlHelper(this.ControllerContext.RequestContext);
                    var redirect = urlHelper.Href(category);
                    MovedPermanently(redirect);
                }
            }
            if (!page.HasValue || page < 0)
            {
                page = 0;
            }
            else
            {
                page = page.Value - 1;
            }
            if (page < 0)
            {
                page = 0;
            }
            int count = 0;
            pageSize = pageSize.GetValueOrDefault(ERPStoreApplication.WebSiteSettings.Catalog.PageSize);
            var parameters = CatalogService.RemoveNotFilteredParameters(Request.Url.Query.ToHtmlDecodedNameValueDictionary());
            var brand = CatalogService.GetBrandByName(Request["brand"]);

            var productSearch = CatalogService.CreateProductListFilter(this.HttpContext);
            if (brand != null)
            {
                productSearch.BrandId = brand.Id;
            }
            productSearch.ProductCategoryId = category.Id;
            productSearch.ExtendedParameters = parameters;

            var productList = CatalogService.GetProductListBySearch(productSearch, page.Value, pageSize.Value, out count);

            // Recherche des tarifs client si celui-ci est connecté
            CatalogService.ApplyBestPrice(productList, User.GetUserPrincipal().CurrentUser);

            var pageIndex = page.Value + 1;

            var result = new Models.ProductList(productList);
            result.Category = category;
            result.ItemCount = count;
            result.PageIndex = pageIndex;
            result.PageSize = pageSize.Value;
            result.ExtendedParameters = parameters;
            result.ListType = ERPStore.Models.ProductListType.Category;
            result.Brand = brand;
            ViewData.Model = result;

            return View();
        }