private void AssignCatalogProduct(Model.Local.Article Article, Model.Prestashop.PsProduct Product, Model.Local.ArticleRepository ArticleRepository) { try { Model.Local.ArticleCatalogRepository ArticleCatalogRepository = new Model.Local.ArticleCatalogRepository(); Model.Local.CatalogRepository CatalogRepository = new Model.Local.CatalogRepository(); Model.Prestashop.PsCategoryProductRepository PsCategoryProductRepository = new Model.Prestashop.PsCategoryProductRepository(); Model.Prestashop.PsCategoryRepository PsCategoryRepository = new Model.Prestashop.PsCategoryRepository(); // filtrage des catalogues existants dans PC et PS List <Model.Local.CatalogLight> ListLocalCatalog = CatalogRepository.ListLight(); List <uint> ListPrestashopCategory = PsCategoryRepository.ListIdOrderByLevelDepth(Core.Global.CurrentShop.IDShop, PsCategoryRepository.ReadId(Core.Global.CurrentShop.IDCategory).LevelDepth); ListLocalCatalog = ListLocalCatalog.Where(lc => ListPrestashopCategory.Count(pc => pc == (uint)lc.Pre_Id) > 0).ToList(); // filtrage des associations PS par rapport aux catégories PS existantes en tant que catalogue PC List <Model.Prestashop.PsCategoryProduct> ListPsCategoryProduct = PsCategoryProductRepository.ListProduct(Product.IDProduct); ListPsCategoryProduct = ListPsCategoryProduct.Where(cp => ListLocalCatalog.Count(lc => lc.Pre_Id == cp.IDCategory) > 0).ToList(); // filtrage des associations PC par rapport aux catégories ayant un ID PS List <Model.Local.ArticleCatalog> ListLocal = ArticleCatalogRepository.ListArticle(Article.Art_Id); ListLocal = ListLocal.Where(ac => ac.Catalog.Pre_Id != null).ToList(); if (Core.Global.GetConfig().DeleteCatalogProductAssociation) { // suppressions des associations inexistantes dans PC while (ListPsCategoryProduct.Count(cp => ListLocal.Count(ac => ac.Catalog.Pre_Id == cp.IDCategory) == 0) > 0) { Model.Prestashop.PsCategoryProduct target = ListPsCategoryProduct.FirstOrDefault(cp => ListLocal.Count(ac => ac.Catalog.Pre_Id == cp.IDCategory) == 0); PsCategoryProductRepository.Delete(target); ListPsCategoryProduct.Remove(target); } ; } ListLocal = ListLocal.Where(ac => ListPsCategoryProduct.Count(cp => cp.IDCategory == ac.Catalog.Pre_Id) == 0).ToList(); foreach (Model.Local.ArticleCatalog ArticleCatalog in ListLocal) { uint IDCategory = (UInt32)ArticleCatalog.Catalog.Pre_Id; PsCategoryProductRepository.Add(new Model.Prestashop.PsCategoryProduct() { IDProduct = Product.IDProduct, IDCategory = IDCategory, Position = Model.Prestashop.PsCategoryProductRepository.NextPositionProductCatalog(IDCategory), }); } } catch (Exception ex) { Core.Error.SendMailError(ex.ToString()); } }
private void ExecDistantToLocal(Model.Prestashop.PsProduct Product, Model.Local.Article Article, Model.Local.ArticleRepository ArticleRepository) { try { #region Recovery Data From CategoryProduct Model.Prestashop.PsCategoryProductRepository PsCategoryProductRepository = new Model.Prestashop.PsCategoryProductRepository(); Model.Local.CatalogRepository CatalogRepository = new Model.Local.CatalogRepository(); Model.Local.ArticleCatalogRepository ArticleCatalogRepository = new Model.Local.ArticleCatalogRepository(); Model.Prestashop.PsCategoryRepository PsCategoryRepository = new Model.Prestashop.PsCategoryRepository(); // filtrage des catalogues existants dans PC et PS List <Model.Local.CatalogLight> ListLocalCatalog = CatalogRepository.ListLight(); List <uint> ListPrestashopCategory = PsCategoryRepository.ListIdOrderByLevelDepth(Core.Global.CurrentShop.IDShop, PsCategoryRepository.ReadId(Core.Global.CurrentShop.IDCategory).LevelDepth); ListLocalCatalog = ListLocalCatalog.Where(lc => ListPrestashopCategory.Count(pc => pc == (uint)lc.Pre_Id) > 0).ToList(); // filtrage des associations PS par rapport aux catégories PS existantes en tant que catalogue PC List <Model.Prestashop.PsCategoryProduct> ListPsCategoryProduct = PsCategoryProductRepository.ListProduct(Product.IDProduct); ListPsCategoryProduct = ListPsCategoryProduct.Where(cp => ListLocalCatalog.Count(lc => lc.Pre_Id == cp.IDCategory) > 0).ToList(); // filtrage des associations PC par rapport aux catégories ayant un ID PS List <Model.Local.ArticleCatalog> ListLocal = ArticleCatalogRepository.ListArticle(Article.Art_Id); ListLocal = ListLocal.Where(ac => ac.Catalog.Pre_Id != null).ToList(); // tant que les associations en local contiennent des associations non présentes dans Prestashop if (Core.Global.GetConfig().DeleteCatalogProductAssociation) { // suppression des occurences inexistantes dans PS while (ListLocal.Count(ac => ListPsCategoryProduct.Count(cp => cp.IDCategory == ac.Catalog.Pre_Id) == 0) > 0) { Model.Local.ArticleCatalog target = ListLocal.FirstOrDefault(ac => ListPsCategoryProduct.Count(cp => cp.IDCategory == ac.Catalog.Pre_Id) == 0); ArticleCatalogRepository.Delete(target); ListLocal.Remove(target); } ; } // récupération catégorie principale si catalogue existant dans Prestaconnect if (Product.IDCategoryDefault != null && Product.IDCategoryDefault != 0 && ListLocalCatalog.Count(lc => lc.Pre_Id == (int)Product.IDCategoryDefault) > 0) { ArticleRepository = new Model.Local.ArticleRepository(); Article = ArticleRepository.ReadArticle(Article.Art_Id); Article.Cat_Id = ListLocalCatalog.FirstOrDefault(lc => lc.Pre_Id == (int)Product.IDCategoryDefault).Cat_Id; ArticleRepository.Save(); if (ListLocal.Count(ac => ac.Art_Id == Article.Art_Id && ac.Cat_Id == Article.Cat_Id) == 0) { ArticleCatalogRepository.Add(new Model.Local.ArticleCatalog() { Art_Id = Article.Art_Id, Cat_Id = Article.Cat_Id }); } } // filtre des associations Prestashop par rapport à celles déjà présentes dans Prestaconnect puis ajout ListLocal = ArticleCatalogRepository.ListArticle(Article.Art_Id); ListLocal = ListLocal.Where(ac => ac.Catalog.Pre_Id != null).ToList(); ListPsCategoryProduct = ListPsCategoryProduct.Where(cp => ListLocal.Count(ac => ac.Catalog.Pre_Id == (Int32)cp.IDCategory) == 0).ToList(); foreach (Model.Prestashop.PsCategoryProduct PsCategoryProduct in ListPsCategoryProduct) { ArticleCatalogRepository.Add(new Model.Local.ArticleCatalog() { Art_Id = Article.Art_Id, Cat_Id = ListLocalCatalog.FirstOrDefault(lc => lc.Pre_Id == (Int32)PsCategoryProduct.IDCategory).Cat_Id }); } #endregion } catch (Exception ex) { Core.Error.SendMailError(ex.ToString()); } }