private void RecoveryDataProductAttribute(Model.Prestashop.PsProduct Product)
        {
            Model.Prestashop.PsProductAttributeRepository      PsProductAttributeRepository      = new Model.Prestashop.PsProductAttributeRepository();
            Model.Prestashop.PsProductAttributeShopRepository  PsProductAttributeShopRepository  = new Model.Prestashop.PsProductAttributeShopRepository();
            Model.Prestashop.PsProductAttributeImageRepository PsProductAttributeImageRepository = new Model.Prestashop.PsProductAttributeImageRepository();

            Model.Local.AttributeArticleRepository    AttributeArticleRepository    = new Model.Local.AttributeArticleRepository();
            Model.Local.ConditioningArticleRepository ConditioningArticleRepository = new Model.Local.ConditioningArticleRepository();
            Model.Local.CompositionArticleRepository  CompositionArticleRepository  = new Model.Local.CompositionArticleRepository();

            Model.Local.AttributeArticleImageRepository   AttributeArticleImageRepository   = new Model.Local.AttributeArticleImageRepository();
            Model.Local.CompositionArticleImageRepository CompositionArticleImageRepository = new Model.Local.CompositionArticleImageRepository();
            Model.Local.ArticleImageRepository            ArticleImageRepository            = new Model.Local.ArticleImageRepository();

            Model.Local.AttributeArticle    AttributeArticle;
            Model.Local.ConditioningArticle ConditioningArticle;
            Model.Local.CompositionArticle  CompositionArticle;
            Model.Local.ArticleImage        ArticleImage;

            List <Model.Prestashop.PsProductAttribute> ListPsProductAttribute = PsProductAttributeRepository.List(Product.IDProduct);

            //parcours déclinaisons PrestaShop
            foreach (Model.Prestashop.PsProductAttribute PsProductAttribute in ListPsProductAttribute)
            {
                // test si déclinaison en gamme dans PrestaConnect
                if (AttributeArticleRepository.ExistPrestashop((int)PsProductAttribute.IDProductAttribute))
                {
                    AttributeArticle = AttributeArticleRepository.ReadPrestashop((int)PsProductAttribute.IDProductAttribute);
                    Model.Prestashop.PsProductAttributeShop PsProductAttributeShop = (PsProductAttributeShopRepository.ExistPsProductAttributeShop(PsProductAttribute.IDProductAttribute, Core.Global.CurrentShop.IDShop))
                        ? PsProductAttributeShopRepository.ReadPsProductAttributeShop(PsProductAttribute.IDProductAttribute, Core.Global.CurrentShop.IDShop)
                        : null;
                    bool defaut = (PsProductAttributeShop != null) ? PsProductAttributeShop.DefaultOn == 1 : PsProductAttribute.DefaultOn == 1;
                    if (AttributeArticle.AttArt_Default != defaut)
                    {
                        AttributeArticle.AttArt_Default = defaut;
                        AttributeArticleRepository.Save();
                        if (defaut)
                        {
                            List <Model.Local.AttributeArticle> ListAttributeArticle = AttributeArticleRepository.ListArticle(AttributeArticle.Art_Id);
                            if (ListAttributeArticle.Count(i => i.AttArt_Default == true && i.AttArt_Id != AttributeArticle.AttArt_Id) > 0)
                            {
                                foreach (Model.Local.AttributeArticle AttributeArticleDefault in ListAttributeArticle.Where(i => i.AttArt_Default == true && i.AttArt_Id != AttributeArticle.Art_Id))
                                {
                                    AttributeArticleDefault.AttArt_Default = false;
                                    AttributeArticleRepository.Save();
                                }
                            }
                        }
                    }

                    // attribution images gammes
                    if (PsProductAttributeImageRepository.ExistProductAttribute(PsProductAttribute.IDProductAttribute))
                    {
                        List <Model.Prestashop.PsProductAttributeImage> ListPsProductAttributeImage = PsProductAttributeImageRepository.ListProductAttribute(PsProductAttribute.IDProductAttribute);
                        // étape 1 attachement dans PrestaConnect des images affectées a la déclinaison PrestaShop
                        foreach (Model.Prestashop.PsProductAttributeImage PsProductAttributeImage in ListPsProductAttributeImage)
                        {
                            // si l'image existe dans PrestaConnect
                            if (ArticleImageRepository.ExistPre_Id((int)PsProductAttributeImage.IDImage))
                            {
                                ArticleImage = ArticleImageRepository.ReadPrestaShop((int)PsProductAttributeImage.IDImage);
                                if (!AttributeArticleImageRepository.ExistAttributeArticleImage(AttributeArticle.AttArt_Id, ArticleImage.ImaArt_Id))
                                {
                                    AttributeArticleImageRepository.Add(new Model.Local.AttributeArticleImage()
                                    {
                                        AttArt_Id = AttributeArticle.AttArt_Id,
                                        ImaArt_Id = ArticleImage.ImaArt_Id,
                                    });
                                }
                            }
                        }
                        // étape 2 détachement
                        List <uint> list_prestashop = ListPsProductAttributeImage.Select(pai => pai.IDImage).ToList();
                        foreach (Model.Local.AttributeArticleImage AttributeArticleImage in AttributeArticleImageRepository.ListAttributeArticle(AttributeArticle.AttArt_Id))
                        {
                            if (AttributeArticleImage.ArticleImage.Pre_Id != null &&
                                !list_prestashop.Contains((uint)AttributeArticleImage.ArticleImage.Pre_Id.Value))
                            {
                                AttributeArticleImageRepository.Delete(AttributeArticleImage);
                            }
                        }
                    }
                    else if (AttributeArticleImageRepository.ExistAttributeArticle(AttributeArticle.AttArt_Id))
                    {
                        // absence de lien gamme déclinaison dans Prestashop donc suppression des liens dans PrestaConnect
                        AttributeArticleImageRepository.DeleteAll(AttributeArticleImageRepository.ListAttributeArticle(AttributeArticle.AttArt_Id));
                    }
                }
                // test si déclinaison en conditionnement dans PrestaConnect
                else if (ConditioningArticleRepository.ExistPrestashop((int)PsProductAttribute.IDProductAttribute))
                {
                    ConditioningArticle = ConditioningArticleRepository.ReadPrestashop((int)PsProductAttribute.IDProductAttribute);
                    Model.Prestashop.PsProductAttributeShop PsProductAttributeShop = (PsProductAttributeShopRepository.ExistPsProductAttributeShop(PsProductAttribute.IDProductAttribute, Core.Global.CurrentShop.IDShop))
                        ? PsProductAttributeShopRepository.ReadPsProductAttributeShop(PsProductAttribute.IDProductAttribute, Core.Global.CurrentShop.IDShop)
                        : null;
                    bool defaut = (PsProductAttributeShop != null) ? PsProductAttributeShop.DefaultOn == 1 : PsProductAttribute.DefaultOn == 1;
                    if (ConditioningArticle.ConArt_Default != defaut)
                    {
                        ConditioningArticle.ConArt_Default = defaut;
                        ConditioningArticleRepository.Save();
                        if (defaut)
                        {
                            List <Model.Local.ConditioningArticle> ListConditioningArticle = ConditioningArticleRepository.ListArticle(ConditioningArticle.Art_Id);
                            if (ListConditioningArticle.Count(i => i.ConArt_Default == true && i.ConArt_Id != ConditioningArticle.ConArt_Id) > 0)
                            {
                                foreach (Model.Local.ConditioningArticle ConditioningArticleDefault in ListConditioningArticle.Where(i => i.ConArt_Default == true && i.ConArt_Id != ConditioningArticle.Art_Id))
                                {
                                    ConditioningArticleDefault.ConArt_Default = false;
                                    ConditioningArticleRepository.Save();
                                }
                            }
                        }
                    }
                }
                // test si déclinaison composition dans PrestaConnect
                else if (CompositionArticleRepository.ExistPrestaShop((int)PsProductAttribute.IDProductAttribute))
                {
                    CompositionArticle = CompositionArticleRepository.ReadPrestaShop((int)PsProductAttribute.IDProductAttribute);
                    Model.Prestashop.PsProductAttributeShop PsProductAttributeShop = (PsProductAttributeShopRepository.ExistPsProductAttributeShop(PsProductAttribute.IDProductAttribute, Core.Global.CurrentShop.IDShop))
                        ? PsProductAttributeShopRepository.ReadPsProductAttributeShop(PsProductAttribute.IDProductAttribute, Core.Global.CurrentShop.IDShop)
                        : null;
                    bool defaut = (PsProductAttributeShop != null) ? PsProductAttributeShop.DefaultOn == 1 : PsProductAttribute.DefaultOn == 1;
                    if (CompositionArticle.ComArt_Default != defaut)
                    {
                        CompositionArticle.ComArt_Default = defaut;
                        CompositionArticleRepository.Save();
                        if (defaut)
                        {
                            List <Model.Local.CompositionArticle> ListCompositionArticle = CompositionArticleRepository.ListArticle(CompositionArticle.ComArt_ArtId);
                            if (ListCompositionArticle.Count(i => i.ComArt_Default == true && i.ComArt_Id != CompositionArticle.ComArt_Id) > 0)
                            {
                                foreach (Model.Local.CompositionArticle CompositionArticleDefault in ListCompositionArticle.Where(i => i.ComArt_Default == true && i.ComArt_Id != CompositionArticle.ComArt_ArtId))
                                {
                                    CompositionArticleDefault.ComArt_Default = false;
                                    ConditioningArticleRepository.Save();
                                }
                            }
                        }
                    }

                    // attribution images gammes
                    if (PsProductAttributeImageRepository.ExistProductAttribute(PsProductAttribute.IDProductAttribute))
                    {
                        List <Model.Prestashop.PsProductAttributeImage> ListPsProductAttributeImage = PsProductAttributeImageRepository.ListProductAttribute(PsProductAttribute.IDProductAttribute);
                        // étape 1 attachement dans PrestaConnect des images affectées a la déclinaison PrestaShop
                        foreach (Model.Prestashop.PsProductAttributeImage PsProductAttributeImage in ListPsProductAttributeImage)
                        {
                            // si l'image existe dans PrestaConnect
                            if (ArticleImageRepository.ExistPre_Id((int)PsProductAttributeImage.IDImage))
                            {
                                ArticleImage = ArticleImageRepository.ReadPrestaShop((int)PsProductAttributeImage.IDImage);
                                if (!CompositionArticleImageRepository.ExistCompositionArticleImage(CompositionArticle.ComArt_Id, ArticleImage.ImaArt_Id))
                                {
                                    CompositionArticleImageRepository.Add(new Model.Local.CompositionArticleImage()
                                    {
                                        ComArt_Id = CompositionArticle.ComArt_Id,
                                        ImaArt_Id = ArticleImage.ImaArt_Id,
                                    });
                                }
                            }
                        }
                        // étape 2 détachement
                        List <uint> list_prestashop = ListPsProductAttributeImage.Select(pai => pai.IDImage).ToList();
                        foreach (Model.Local.CompositionArticleImage CompositionArticleImage in CompositionArticleImageRepository.ListCompositionArticle(CompositionArticle.ComArt_Id))
                        {
                            if (CompositionArticleImage.ArticleImage.Pre_Id != null &&
                                !list_prestashop.Contains((uint)CompositionArticleImage.ArticleImage.Pre_Id.Value))
                            {
                                CompositionArticleImageRepository.Delete(CompositionArticleImage);
                            }
                        }
                    }
                    else if (CompositionArticleImageRepository.ExistCompositionArticle(CompositionArticle.ComArt_Id))
                    {
                        // absence de lien gamme déclinaison dans Prestashop donc suppression des liens dans PrestaConnect
                        CompositionArticleImageRepository.DeleteAll(CompositionArticleImageRepository.ListCompositionArticle(CompositionArticle.ComArt_Id));
                    }
                }
            }
        }
Esempio n. 2
0
        public void Exec(Model.Local.ArticleImage ArticleImage, Model.Local.Article Article, out List <string> log_out)
        {
            log_out = new List <string>();
            try
            {
                // Suppression de l'affectation de l'image à la gamme en local
                Model.Local.AttributeArticleImageRepository AttributeArticleImageRepository = new Model.Local.AttributeArticleImageRepository();
                AttributeArticleImageRepository.DeleteAll(AttributeArticleImageRepository.ListImageArticle(ArticleImage.ImaArt_Id));

                // 07/04/2016 ajout suppression liens compositions
                Model.Local.CompositionArticleImageRepository CompositionArticleImageRepository = new Model.Local.CompositionArticleImageRepository();
                CompositionArticleImageRepository.DeleteAll(CompositionArticleImageRepository.ListImageArticle(ArticleImage.ImaArt_Id));

                // Suppression des fichiers images en local
                if (File.Exists(ArticleImage.SmallFileName))
                {
                    try { File.Delete(ArticleImage.SmallFileName); }
                    catch (Exception ex) { Core.Error.SendMailError(ex.ToString()); }
                }
                if (File.Exists(ArticleImage.TempFileName))
                {
                    try { File.Delete(ArticleImage.TempFileName); }
                    catch (Exception ex) { Core.Error.SendMailError(ex.ToString()); }
                }

                string folder = (Core.Global.GetConfig().ConfigLocalStorageMode == Core.Parametres.LocalStorageMode.advanced_system)
                    ? ArticleImage.advanced_folder
                    : Core.Global.GetConfig().Folders.RootArticle;

                foreach (var fileName in Directory.GetFiles(folder, String.Format("{0}-*" + ArticleImage.GetExtension, ArticleImage.ImaArt_Id)))
                {
                    try { File.Delete(fileName); }
                    catch (Exception ex) { Core.Error.SendMailError(ex.ToString()); }
                }

                if (ArticleImage.Pre_Id != null && ArticleImage.Pre_Id > 0)
                {
                    Model.Prestashop.PsProductAttributeImageRepository PsProductAttributeImageRepository = new Model.Prestashop.PsProductAttributeImageRepository();
                    PsProductAttributeImageRepository.DeleteAll(PsProductAttributeImageRepository.ListImage((uint)ArticleImage.Pre_Id));

                    Model.Prestashop.PsImageLangRepository PsImageLangRepository = new Model.Prestashop.PsImageLangRepository();
                    PsImageLangRepository.DeleteAll(PsImageLangRepository.ListImage((uint)ArticleImage.Pre_Id));

                    // Suppression de l'occurence de l'image sur prestashop
                    Model.Prestashop.PsImageRepository PsImageRepository = new Model.Prestashop.PsImageRepository();
                    Model.Prestashop.PsImage           PsImage           = PsImageRepository.ReadImage((uint)ArticleImage.Pre_Id);

                    if (PsImage != null)
                    {
                        PsImageRepository.Delete(PsImage);

                        try
                        {
                            if (PsImage.Cover == 1 &&
                                !PsImageRepository.ExistProductCover(PsImage.IDProduct, 1) &&
                                PsImageRepository.ExistProduct(PsImage.IDProduct))
                            {
                                List <Model.Prestashop.PsImage> Others   = PsImageRepository.ListProduct(PsImage.IDProduct);
                                Model.Prestashop.PsImage        NewCover = Others.OrderBy(i => i.Position).FirstOrDefault();
                                if (NewCover != null)
                                {
                                    NewCover.Cover = 1;
                                    PsImageRepository.Save();

                                    Core.Transfert.TransfertArticleImage syncimage = new Core.Transfert.TransfertArticleImage();
                                    syncimage.ExecCover(PsImage.IDProduct, true, PsImageRepository, NewCover);

                                    Model.Prestashop.PsImageShopRepository PsImageShopRepository = new Model.Prestashop.PsImageShopRepository();
                                    foreach (Model.Prestashop.PsImageShop PsImageShop in PsImageShopRepository.List(NewCover.IDImage))
                                    {
                                        PsImageShop.Cover = 1;
                                    }
                                    PsImageShopRepository.Save();
                                }
                            }
                        }
                        catch
                        {
                            log_out.Add("II47- Erreur durant la réaffectation de l'image de couverture dans PrestaShop pour l'article " + Article.Art_Ref);
                        }
                        try
                        {
                            foreach (Model.Prestashop.PsImage Image in PsImageRepository.ListProduct(PsImage.IDProduct).Where(i => i.Position > PsImage.Position))
                            {
                                Image.Position--;
                            }
                            PsImageRepository.Save();
                        }
                        catch
                        {
                            log_out.Add("II48- Erreur durant le recalcul des positions dans PrestaShop pour les images de l'article " + Article.Art_Ref);
                        }

                        // Suppression des fichiers images sur prestashop
                        if (Core.Global.GetConfig().ConfigFTPActive)
                        {
                            String FTP      = Core.Global.GetConfig().ConfigFTPIP;
                            String User     = Core.Global.GetConfig().ConfigFTPUser;
                            String Password = Core.Global.GetConfig().ConfigFTPPassword;

                            // <JG> 10/04/2013 gestion système d'images
                            string ftpPath = "/img/p/";
                            switch (Core.Global.GetConfig().ConfigImageStorageMode)
                            {
                            case Core.Parametres.ImageStorageMode.old_system:
                                #region old_system
                                // no action on path
                                break;
                                #endregion

                            case Core.Parametres.ImageStorageMode.new_system:
                            default:
                                #region new_system

                                foreach (char directory in ArticleImage.Pre_Id.ToString())
                                {
                                    ftpPath += directory + "/";
                                }
                                break;
                                #endregion
                            }

                            // <JG> 21/05/2013 correct for 1.5 storage mode
                            string ftpfullpath = (Core.Global.GetConfig().ConfigImageStorageMode == Core.Parametres.ImageStorageMode.old_system)
                                ? FTP + ftpPath + Article.Pre_Id + "-" + ArticleImage.Pre_Id + ".jpg"
                                : FTP + ftpPath + ArticleImage.Pre_Id + ".jpg";

                            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
                            request.Credentials = new System.Net.NetworkCredential(User, Password);
                            request.Method      = WebRequestMethods.Ftp.DeleteFile;
                            request.UseBinary   = true;
                            request.UsePassive  = true;
                            request.KeepAlive   = false;

                            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                            response.Close();

                            Model.Prestashop.PsImageTypeRepository PsImageTypeRepository = new Model.Prestashop.PsImageTypeRepository();
                            List <Model.Prestashop.PsImageType>    ListPsImageType       = PsImageTypeRepository.ListProduct(1);

                            foreach (Model.Prestashop.PsImageType PsImageType in ListPsImageType)
                            {
                                try
                                {
                                    // <JG> 21/05/2013 correct for 1.5 storage mode
                                    ftpfullpath = (Core.Global.GetConfig().ConfigImageStorageMode == Core.Parametres.ImageStorageMode.old_system)
                                        ? FTP + ftpPath + Article.Pre_Id + "-" + ArticleImage.Pre_Id + "-" + PsImageType.Name + ".jpg"
                                        : FTP + ftpPath + ArticleImage.Pre_Id + "-" + PsImageType.Name + ".jpg";

                                    request             = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(ftpfullpath);
                                    request.Credentials = new System.Net.NetworkCredential(User, Password);
                                    request.Method      = WebRequestMethods.Ftp.DeleteFile;
                                    request.UseBinary   = true;
                                    request.UsePassive  = true;
                                    request.KeepAlive   = false;

                                    response = (FtpWebResponse)request.GetResponse();
                                    response.Close();
                                }
                                catch {}
                            }
                        }
                    }
                }

                // Suppression de l'occurence de l'image en local
                Model.Local.ArticleImageRepository ArticleImageRepository = new Model.Local.ArticleImageRepository();
                Model.Local.ArticleImage           articleImage           = ArticleImageRepository.ReadArticleImage(ArticleImage.ImaArt_Id);

                if (articleImage != null)
                {
                    ArticleImageRepository.Delete(articleImage);

                    try
                    {
                        if (articleImage.ImaArt_Default &&
                            !ArticleImageRepository.ExistArticleDefault(Article.Art_Id, true) &&
                            ArticleImageRepository.ExistArticle(Article.Art_Id))
                        {
                            Model.Local.ArticleImage NewCover = ArticleImageRepository.ListArticle(Article.Art_Id).OrderBy(i => i.ImaArt_Position).FirstOrDefault();
                            if (NewCover != null)
                            {
                                NewCover.ImaArt_Default = true;
                                ArticleImageRepository.Save();
                            }
                        }
                    }
                    catch
                    {
                        log_out.Add("II43- Erreur durant la réaffectation de l'image de couverture dans PrestaConnect pour l'article " + Article.Art_Ref);
                    }
                    try
                    {
                        foreach (Model.Local.ArticleImage Image in ArticleImageRepository.ListArticle(Article.Art_Id).Where(i => i.ImaArt_Position > articleImage.ImaArt_Position))
                        {
                            Image.ImaArt_Position--;
                        }
                        ArticleImageRepository.Save();
                    }
                    catch
                    {
                        log_out.Add("II44- Erreur durant le recalcul des positions dans PrestaConnect pour les images de l'article " + Article.Art_Ref);
                    }
                }

                log_out.Add("II40- Suppression de l'image " + ArticleImage.ImaArt_SourceFile + " en position " + ArticleImage.ImaArt_Position + " pour l'article " + Article.Art_Ref);
            }
            catch (Exception ex)
            {
                Core.Error.SendMailError(ex.ToString());
            }
        }