Esempio n. 1
0
        public bool EnvoyerSalesforce(GED.Tools.LDAPUser ldapUser)
        {
            bool retour = false;

            try
            {
                string loginSF = ConfigurationManager.AppSettings["loginSF"];
                string mdpSF   = ConfigurationManager.AppSettings["mdpSF"];
                string envSF   = ConfigurationManager.AppSettings["EnvironementSF"];
                string entite  = ConfigurationManager.AppSettings["Entite"];
                string sIdSF   = "";

                //Authentification
                if (envSF == "DEV")
                {
                    Tools.wsdl_enterprise_dev.SforceService bdAuth = new Tools.wsdl_enterprise_dev.SforceService();
                    Tools.wsdl_enterprise_dev.LoginResult   lr     = bdAuth.login(loginSF, mdpSF);
                    sIdSF = lr.sessionId;
                }
                else if (envSF == "QUALIF")
                {
                    Tools.wsdl_enterprise_qualif.SforceService bdAuth = new Tools.wsdl_enterprise_qualif.SforceService();
                    Tools.wsdl_enterprise_qualif.LoginResult   lr     = bdAuth.login(loginSF, mdpSF);
                    sIdSF = lr.sessionId;
                }
                else if (envSF == "DEMO")
                {
                    Tools.wsdl_enterprise_demo.SforceService bdAuth = new Tools.wsdl_enterprise_demo.SforceService();
                    Tools.wsdl_enterprise_demo.LoginResult   lr     = bdAuth.login(loginSF, mdpSF);
                    sIdSF = lr.sessionId;
                }
                else if (envSF == "PROD")
                {
                    Tools.wsdl_enterprise.SforceService bdAuth = new Tools.wsdl_enterprise.SforceService();
                    Tools.wsdl_enterprise.LoginResult   lr     = bdAuth.login(loginSF, mdpSF);
                    sIdSF = lr.sessionId;
                }



                //objet à envoyer
                if (envSF == "DEV")
                {
                    Tools.wsdl_docacquisiton_dev.DocAcquisitionInput docInput = new Tools.wsdl_docacquisiton_dev.DocAcquisitionInput();
                    //informations sur le pli
                    Tools.wsdl_docacquisiton_dev.Pli pliSF = new Tools.wsdl_docacquisiton_dev.Pli();
                    pliSF.PliReferenceId           = ID_Pli.ToString();
                    pliSF.NomPli                   = Nom;
                    pliSF.origin                   = NomSource;
                    pliSF.DateAcquisition          = DateAcquisition.ToUniversalTime();
                    pliSF.DateAcquisitionSpecified = true;
                    pliSF.userEmail                = ldapUser.Mail;
                    pliSF.dateSaisie               = DateDebutSaisie.ToUniversalTime();
                    pliSF.dateSaisieSpecified      = true;
                    if (Original)
                    {
                        pliSF.OriginalCopie = "Original";
                    }
                    else
                    {
                        pliSF.OriginalCopie = "Copie";
                    }
                    pliSF.Type      = NomType;
                    pliSF.entity    = entite;
                    docInput.PliGED = pliSF;

                    //information sur les docs
                    docInput.DocsGED = new Tools.wsdl_docacquisiton_dev.DocumentGED[Documents.Count];
                    int i = 0;
                    foreach (GED.Handlers.Document leDoc in Documents.Values)
                    {
                        if (leDoc.Type_EnvoyerSF)
                        {
                            Tools.wsdl_docacquisiton_dev.DocumentGED docSF = new Tools.wsdl_docacquisiton_dev.DocumentGED();
                            docSF.DocReferenceId = leDoc.ID_Document.ToString();
                            docSF.nom            = leDoc.Nom;
                            docSF.RefPliId       = leDoc.ID_Pli.ToString();
                            if (leDoc.Original)
                            {
                                docSF.OriginalCopie = "Original";
                            }
                            else
                            {
                                docSF.OriginalCopie = "Copie";
                            }
                            docSF.OriginalCopie = pliSF.OriginalCopie;
                            docSF.Type          = leDoc.NomType;

                            docSF.NbrePages          = leDoc.NbPage;
                            docSF.NbrePagesSpecified = true;

                            docInput.DocsGED[i] = docSF;
                            i++;
                        }
                    }

                    //consomation du web service SF
                    Tools.wsdl_docacquisiton_dev.WS01DocumentAcquisitionService bdGED = new Tools.wsdl_docacquisiton_dev.WS01DocumentAcquisitionService();
                    bdGED.SessionHeaderValue           = new Tools.wsdl_docacquisiton_dev.SessionHeader();
                    bdGED.SessionHeaderValue.sessionId = sIdSF;

                    Tools.wsdl_docacquisiton_dev.DocAcquisitionOutput docOutput = bdGED.DocAcquisition(docInput);

                    if (docOutput.ReturnCode != 0)//erreur
                    {
                        throw new Exception(docOutput.Error);
                    }

                    retour = true;
                }
                else if (envSF == "QUALIF")
                {
                    Tools.wsdl_docacquisiton_qualif.DocAcquisitionInput docInput = new Tools.wsdl_docacquisiton_qualif.DocAcquisitionInput();

                    //informations sur le pli
                    Tools.wsdl_docacquisiton_qualif.Pli pliSF = new Tools.wsdl_docacquisiton_qualif.Pli();
                    pliSF.PliReferenceId           = ID_Pli.ToString();
                    pliSF.NomPli                   = Nom;
                    pliSF.origin                   = NomSource;
                    pliSF.DateAcquisition          = DateAcquisition.ToUniversalTime();
                    pliSF.DateAcquisitionSpecified = true;
                    pliSF.userEmail                = ldapUser.Mail;
                    pliSF.dateSaisie               = DateDebutSaisie.ToUniversalTime();
                    pliSF.dateSaisieSpecified      = true;
                    if (Original)
                    {
                        pliSF.OriginalCopie = "Original";
                    }
                    else
                    {
                        pliSF.OriginalCopie = "Copie";
                    }
                    pliSF.Type      = NomType;
                    pliSF.entity    = entite;
                    docInput.PliGED = pliSF;

                    //information sur les docs
                    docInput.DocsGED = new Tools.wsdl_docacquisiton_qualif.DocumentGED[Documents.Count];
                    int i = 0;
                    foreach (GED.Handlers.Document leDoc in Documents.Values)
                    {
                        if (leDoc.Type_EnvoyerSF)
                        {
                            Tools.wsdl_docacquisiton_qualif.DocumentGED docSF = new Tools.wsdl_docacquisiton_qualif.DocumentGED();
                            docSF.DocReferenceId = leDoc.ID_Document.ToString();
                            docSF.nom            = leDoc.Nom;
                            docSF.RefPliId       = leDoc.ID_Pli.ToString();
                            if (leDoc.Original)
                            {
                                docSF.OriginalCopie = "Original";
                            }
                            else
                            {
                                docSF.OriginalCopie = "Copie";
                            }
                            docSF.Type = leDoc.NomType;

                            docSF.NbrePages          = leDoc.NbPage;
                            docSF.NbrePagesSpecified = true;

                            docInput.DocsGED[i] = docSF;
                            i++;
                        }
                    }

                    //consomation du web service SF
                    Tools.wsdl_docacquisiton_qualif.WS01DocumentAcquisitionService bdGED = new Tools.wsdl_docacquisiton_qualif.WS01DocumentAcquisitionService();
                    bdGED.SessionHeaderValue           = new Tools.wsdl_docacquisiton_qualif.SessionHeader();
                    bdGED.SessionHeaderValue.sessionId = sIdSF;

                    Tools.wsdl_docacquisiton_qualif.DocAcquisitionOutput docOutput = bdGED.DocAcquisition(docInput);

                    if (docOutput.ReturnCode != 0)//erreur
                    {
                        throw new Exception(docOutput.Error);
                    }

                    retour = true;
                }
                else if (envSF == "DEMO")
                {
                    Tools.wsdl_docacquisiton_demo.DocAcquisitionInput docInput = new Tools.wsdl_docacquisiton_demo.DocAcquisitionInput();

                    //informations sur le pli
                    Tools.wsdl_docacquisiton_demo.Pli pliSF = new Tools.wsdl_docacquisiton_demo.Pli();
                    pliSF.PliReferenceId           = ID_Pli.ToString();
                    pliSF.NomPli                   = Nom;
                    pliSF.origin                   = NomSource;
                    pliSF.DateAcquisition          = DateAcquisition.ToUniversalTime();
                    pliSF.DateAcquisitionSpecified = true;
                    pliSF.userEmail                = ldapUser.Mail;
                    pliSF.dateSaisie               = DateDebutSaisie.ToUniversalTime();
                    pliSF.dateSaisieSpecified      = true;
                    if (Original)
                    {
                        pliSF.OriginalCopie = "Original";
                    }
                    else
                    {
                        pliSF.OriginalCopie = "Copie";
                    }
                    pliSF.Type      = NomType;
                    pliSF.entity    = entite;
                    docInput.PliGED = pliSF;

                    //information sur les docs
                    docInput.DocsGED = new Tools.wsdl_docacquisiton_demo.DocumentGED[Documents.Count];
                    int i = 0;
                    foreach (GED.Handlers.Document leDoc in Documents.Values)
                    {
                        if (leDoc.Type_EnvoyerSF)
                        {
                            Tools.wsdl_docacquisiton_demo.DocumentGED docSF = new Tools.wsdl_docacquisiton_demo.DocumentGED();
                            docSF.DocReferenceId = leDoc.ID_Document.ToString();
                            docSF.nom            = leDoc.Nom;
                            docSF.RefPliId       = leDoc.ID_Pli.ToString();
                            if (leDoc.Original)
                            {
                                docSF.OriginalCopie = "Original";
                            }
                            else
                            {
                                docSF.OriginalCopie = "Copie";
                            }
                            docSF.Type = leDoc.NomType;

                            docSF.NbrePages          = leDoc.NbPage;
                            docSF.NbrePagesSpecified = true;

                            docInput.DocsGED[i] = docSF;
                            i++;
                        }
                    }

                    //consomation du web service SF
                    Tools.wsdl_docacquisiton_demo.WS01DocumentAcquisitionService bdGED = new Tools.wsdl_docacquisiton_demo.WS01DocumentAcquisitionService();
                    bdGED.SessionHeaderValue           = new Tools.wsdl_docacquisiton_demo.SessionHeader();
                    bdGED.SessionHeaderValue.sessionId = sIdSF;

                    Tools.wsdl_docacquisiton_demo.DocAcquisitionOutput docOutput = bdGED.DocAcquisition(docInput);

                    if (docOutput.ReturnCode != 0)//erreur
                    {
                        throw new Exception(docOutput.Error);
                    }

                    retour = true;
                }
                else if (envSF == "PROD")
                {
                    Tools.wsdl_docacquisiton.DocAcquisitionInput docInput = new Tools.wsdl_docacquisiton.DocAcquisitionInput();

                    //informations sur le pli
                    Tools.wsdl_docacquisiton.Pli pliSF = new Tools.wsdl_docacquisiton.Pli();
                    pliSF.PliReferenceId           = ID_Pli.ToString();
                    pliSF.NomPli                   = Nom;
                    pliSF.origin                   = NomSource;
                    pliSF.DateAcquisition          = DateAcquisition.ToUniversalTime();
                    pliSF.DateAcquisitionSpecified = true;
                    pliSF.userEmail                = ldapUser.Mail;
                    pliSF.dateSaisie               = DateDebutSaisie.ToUniversalTime();
                    pliSF.dateSaisieSpecified      = true;
                    if (Original)
                    {
                        pliSF.OriginalCopie = "Original";
                    }
                    else
                    {
                        pliSF.OriginalCopie = "Copie";
                    }
                    pliSF.Type      = NomType;
                    pliSF.entity    = entite;
                    docInput.PliGED = pliSF;

                    //information sur les docs
                    docInput.DocsGED = new Tools.wsdl_docacquisiton.DocumentGED[Documents.Count];
                    int i = 0;
                    foreach (GED.Handlers.Document leDoc in Documents.Values)
                    {
                        if (leDoc.Type_EnvoyerSF)
                        {
                            Tools.wsdl_docacquisiton.DocumentGED docSF = new Tools.wsdl_docacquisiton.DocumentGED();
                            docSF.DocReferenceId = leDoc.ID_Document.ToString();
                            docSF.nom            = leDoc.Nom;
                            docSF.RefPliId       = leDoc.ID_Pli.ToString();
                            if (leDoc.Original)
                            {
                                docSF.OriginalCopie = "Original";
                            }
                            else
                            {
                                docSF.OriginalCopie = "Copie";
                            }
                            docSF.Type = leDoc.NomType;

                            docSF.NbrePages          = leDoc.NbPage;
                            docSF.NbrePagesSpecified = true;

                            docInput.DocsGED[i] = docSF;
                            i++;
                        }
                    }

                    //consomation du web service SF
                    Tools.wsdl_docacquisiton.WS01DocumentAcquisitionService bdGED = new Tools.wsdl_docacquisiton.WS01DocumentAcquisitionService();
                    bdGED.SessionHeaderValue           = new Tools.wsdl_docacquisiton.SessionHeader();
                    bdGED.SessionHeaderValue.sessionId = sIdSF;

                    Tools.wsdl_docacquisiton.DocAcquisitionOutput docOutput = bdGED.DocAcquisition(docInput);

                    if (docOutput.ReturnCode != 0)//erreur
                    {
                        throw new Exception(docOutput.Error);
                    }

                    retour = true;
                }
                else
                {
                    retour = false;
                }
            }
            catch (Exception ex)
            {
                retour = false;
            }
            finally
            {
            }

            return(retour);
        }
Esempio n. 2
0
        public int Ajouter(string cheminFichierPDF, bool envoyerSF = false, string nomFichierNew = "", string entite = "NSAS")
        {
            SqlConnection con = null;
            //SqlTransaction trans=null;
            //string strSQLMedia = "";
            string strSQLAvenant = "";
            //SqlCommand cmdMedia = null;
            SqlCommand cmdAvenant = null;
            FileStream fs;

            Byte[] datas;

            ID_Document = 0;

            try
            {
                string typeAvenantDefaut = "A définir";//ConfigurationManager.AppSettings["TypeAvenantDefaut"];
                // lecture du ficheir pdf
                fs = File.Open(cheminFichierPDF, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
                if (fs != null)
                {
                    fs.Position = 0;
                    datas       = new BinaryReader(fs).ReadBytes(int.Parse(fs.Length.ToString()));
                    fs.Close();
                }
                else
                {
                    throw new Exception();
                }
                // nommer le fichier
                string nomFichier = "";
                if (nomFichierNew == "")
                {
                    nomFichier = Path.GetFileNameWithoutExtension(cheminFichierPDF);
                }
                else
                {
                    nomFichier = nomFichierNew;
                }
                // lire le nombre de pages
                PdfReader pdfReader = new PdfReader(cheminFichierPDF);
                NbPage = pdfReader.NumberOfPages;
                pdfReader.Close();
                // aller chercher le type de document ICI JE DOIS AGIR #########################
                int ID_Type_Document = TypeDocument.GetIDTypeDocumentCompagnie(CodeCompagnie, TypeAvenant, entite);
                if (ID_Type_Document > 0) //type existant
                {
                    TypeDocument tpDoc = new TypeDocument(ID_Type_Document, entite);

                    //trans = con.BeginTransaction("GEDAjoutAvenant");
                    //System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.RequiresNew);

                    //insertion CA_Media
                    Document docAvenant = new Document();
                    docAvenant.Datas            = datas;
                    docAvenant.Extension        = ".pdf";
                    docAvenant.Nom              = nomFichier;
                    docAvenant.NbPage           = NbPage;
                    docAvenant.ID_Pli           = 0;
                    docAvenant.ID_Type_Document = ID_Type_Document;
                    docAvenant.Original         = true;
                    docAvenant.VisibleNOL       = true;

                    ID_Document = docAvenant.Ajouter(0, entite);

                    if (ID_Document > 0)
                    {
                        DateTime dateAcq = DateTime.Now;

                        //insertion Avenant
                        if (entite == "NSAS")
                        {
                            con = new SqlConnection("data source=192.168.1.2\\SQL2008r2qualif;Database=GEDNortia_Invest;Uid=sa;password=NICKEL2000;");
                        }
                        else
                        {
                            con = new SqlConnection("data source=192.168.1.2\\SQL2008r2qualif;Database=GEDNortia_Invest;Uid=sa;password=NICKEL2000;");
                        }

                        con.Open();

                        strSQLAvenant = "INSERT INTO [Avenant] ([CodeCompagnie],[NumAvenant],[ID_Document],[TypeAvenant],[NumContrat],[CodeApporteur],[NomApporteur],[NomSouscripteur],[DateAvenant],[Visible],ReferenceInterne,[EnvoyerSF],[DateAcquisition],[NumLot])"
                                        + " VALUES(@CodeCompagnie,@NumAvenant,@ID_Document,@TypeAvenant,@NumContrat,@CodeApporteur,@NomApporteur,@NomSouscripteur,@DateAvenant,@Visible,@ReferenceInterne,@EnvoyerSF,@DateAcquisition,@NumLot);"
                                        + "SELECT CAST(SCOPE_IDENTITY() AS int)";

                        cmdAvenant = new SqlCommand(strSQLAvenant, con);
                        cmdAvenant.Parameters.AddWithValue("@CodeCompagnie", (object)CodeCompagnie ?? DBNull.Value);
                        cmdAvenant.Parameters.AddWithValue("@NumAvenant", (object)NumAvenant ?? DBNull.Value);
                        cmdAvenant.Parameters.AddWithValue("@ID_Document", (object)ID_Document ?? DBNull.Value);
                        cmdAvenant.Parameters.AddWithValue("@TypeAvenant", (object)TypeAvenant ?? DBNull.Value);
                        cmdAvenant.Parameters.AddWithValue("@NumContrat", (object)NumContrat ?? DBNull.Value);
                        cmdAvenant.Parameters.AddWithValue("@CodeApporteur", (object)CodeApporteur ?? DBNull.Value);
                        cmdAvenant.Parameters.AddWithValue("@NomApporteur", (object)NomApporteur ?? DBNull.Value);
                        cmdAvenant.Parameters.AddWithValue("@NomSouscripteur", (object)NomSouscripteur ?? DBNull.Value);
                        cmdAvenant.Parameters.AddWithValue("@DateAvenant", (object)DateAvenant ?? DBNull.Value);
                        cmdAvenant.Parameters.AddWithValue("@Visible", (object)Actif ?? DBNull.Value);
                        cmdAvenant.Parameters.AddWithValue("@ReferenceInterne", (object)ReferenceInterne ?? DBNull.Value);
                        cmdAvenant.Parameters.AddWithValue("@EnvoyerSF", (object)0 ?? DBNull.Value);
                        cmdAvenant.Parameters.AddWithValue("@DateAcquisition", (object)dateAcq ?? DBNull.Value);
                        cmdAvenant.Parameters.AddWithValue("@NumLot", (object)NumLot ?? DBNull.Value);

                        int ID_Avenant = (Int32)cmdAvenant.ExecuteScalar();

                        if (ID_Avenant > 0)
                        {
                            if (envoyerSF && tpDoc.EnvoyerSF)
                            {
                                if (entite == "NSAS")
                                {
                                    string envSF   = ConfigurationManager.AppSettings["EnvironementSF"];
                                    string loginSF = ConfigurationManager.AppSettings["loginSF"];
                                    string mdpSF   = ConfigurationManager.AppSettings["mdpSF"];

                                    string nomTypeActe = Avenant.GetNomTypeActe(tpDoc.ID_Type_Document);


                                    if (envSF == "DEV")
                                    {
                                        //identification
                                        Tools.wsdl_enterprise_dev.SforceService bdAuth = new Tools.wsdl_enterprise_dev.SforceService();
                                        Tools.wsdl_enterprise_dev.LoginResult   lr     = bdAuth.login(loginSF, mdpSF);
                                        string sIdSF = lr.sessionId;

                                        bdAuth.Url = lr.serverUrl;
                                        bdAuth.SessionHeaderValue           = new Tools.wsdl_enterprise_dev.SessionHeader();
                                        bdAuth.SessionHeaderValue.sessionId = sIdSF;

                                        //création de l'objet SF
                                        Tools.wsdl_enterprise_dev.sObject[] myObjectArr = new Tools.wsdl_enterprise_dev.sObject[1];

                                        Tools.wsdl_enterprise_dev.Avenant__c avSF = new Tools.wsdl_enterprise_dev.Avenant__c();
                                        avSF.Date_Acquisition__c          = dateAcq;
                                        avSF.Date_Acquisition__cSpecified = true;
                                        avSF.Id_GED__c = ID_Document.ToString();
                                        avSF.Name      = nomFichier;

                                        if (TypeAvenant == typeAvenantDefaut)
                                        {
                                            avSF.Type_Acte__c     = nomTypeActe;
                                            avSF.Type_Document__c = tpDoc.Nom;
                                        }

                                        myObjectArr[0] = avSF;

                                        //envoi de l'objet SF
                                        Tools.wsdl_enterprise_dev.SaveResult[] ret = bdAuth.create(myObjectArr);

                                        if (ret[0].success)
                                        {
                                            string strSQLEnvoiSF = "UPDATE dbo.Avenant SET EnvoyerSF=1 WHERE ID_Document=@ID_Document";

                                            SqlCommand cmdEnvoiSF = new SqlCommand(strSQLEnvoiSF, con);
                                            cmdEnvoiSF.Parameters.AddWithValue("@ID_Document", (object)ID_Document ?? DBNull.Value);

                                            int retour = cmdEnvoiSF.ExecuteNonQuery();
                                            if (retour == 1)
                                            {
                                                retour = 1;//trans.Commit();scope.Complete();
                                            }
                                            else
                                            {
                                                throw new Exception("Impossible de flaguer l'avenant comme envoyé à SF");
                                            }
                                        }
                                        else
                                        {
                                            throw new Exception("Impossible d'envoyer l'avenant à SF");
                                        }
                                    }
                                    else if (envSF == "QUALIF")
                                    {
                                        //identification
                                        Tools.wsdl_enterprise_qualif.SforceService bdAuth = new Tools.wsdl_enterprise_qualif.SforceService();
                                        Tools.wsdl_enterprise_qualif.LoginResult   lr     = bdAuth.login(loginSF, mdpSF);
                                        string sIdSF = lr.sessionId;

                                        bdAuth.Url = lr.serverUrl;
                                        bdAuth.SessionHeaderValue           = new Tools.wsdl_enterprise_qualif.SessionHeader();
                                        bdAuth.SessionHeaderValue.sessionId = sIdSF;


                                        //création de l'objet SF
                                        Tools.wsdl_enterprise_qualif.sObject[] myObjectArr = new Tools.wsdl_enterprise_qualif.sObject[1];

                                        Tools.wsdl_enterprise_qualif.Avenant__c avSF = new Tools.wsdl_enterprise_qualif.Avenant__c();
                                        avSF.Date_Acquisition__c          = dateAcq;
                                        avSF.Date_Acquisition__cSpecified = true;
                                        avSF.Id_GED__c = ID_Document.ToString();
                                        avSF.Name      = nomFichier;

                                        if (TypeAvenant == typeAvenantDefaut)
                                        {
                                            avSF.Type_Acte__c     = nomTypeActe;
                                            avSF.Type_Document__c = tpDoc.Nom;
                                        }

                                        myObjectArr[0] = avSF;

                                        //envoi de l'objet SF
                                        Tools.wsdl_enterprise_qualif.SaveResult[] ret = bdAuth.create(myObjectArr);

                                        if (ret[0].success)
                                        {
                                            string strSQLEnvoiSF = "UPDATE dbo.Avenant SET EnvoyerSF=1 WHERE ID_Document=@ID_Document";

                                            SqlCommand cmdEnvoiSF = new SqlCommand(strSQLEnvoiSF, con);
                                            cmdEnvoiSF.Parameters.AddWithValue("@ID_Document", (object)ID_Document ?? DBNull.Value);

                                            int retour = cmdEnvoiSF.ExecuteNonQuery();
                                            if (retour == 1)
                                            {
                                                retour = 1;//trans.Commit();scope.Complete();
                                            }
                                            else
                                            {
                                                throw new Exception("Impossible de flaguer l'avenant comme envoyé à SF");
                                            }
                                        }
                                        else
                                        {
                                            throw new Exception("Impossible d'envoyer l'avenant à SF");
                                        }
                                    }
                                    else if (envSF == "PROD")
                                    {
                                        //identification
                                        Tools.wsdl_enterprise.SforceService bdAuth = new Tools.wsdl_enterprise.SforceService();
                                        Tools.wsdl_enterprise.LoginResult   lr     = bdAuth.login(loginSF, mdpSF);
                                        string sIdSF = lr.sessionId;

                                        bdAuth.Url = lr.serverUrl;
                                        bdAuth.SessionHeaderValue           = new Tools.wsdl_enterprise.SessionHeader();
                                        bdAuth.SessionHeaderValue.sessionId = sIdSF;

                                        //création de l'objet SF
                                        Tools.wsdl_enterprise.sObject[] myObjectArr = new Tools.wsdl_enterprise.sObject[1];

                                        Tools.wsdl_enterprise.Avenant__c avSF = new Tools.wsdl_enterprise.Avenant__c();
                                        avSF.Date_Acquisition__c          = dateAcq;
                                        avSF.Date_Acquisition__cSpecified = true;
                                        avSF.Id_GED__c = ID_Document.ToString();
                                        avSF.Name      = nomFichier;

                                        if (TypeAvenant == typeAvenantDefaut)
                                        {
                                            avSF.Type_Acte__c     = nomTypeActe;
                                            avSF.Type_Document__c = tpDoc.Nom;
                                        }

                                        myObjectArr[0] = avSF;

                                        //envoi de l'objet SF
                                        Tools.wsdl_enterprise.SaveResult[] ret = bdAuth.create(myObjectArr);

                                        if (ret[0].success)
                                        {
                                            string strSQLEnvoiSF = "UPDATE dbo.Avenant SET EnvoyerSF=1 WHERE ID_Document=@ID_Document";

                                            SqlCommand cmdEnvoiSF = new SqlCommand(strSQLEnvoiSF, con);
                                            cmdEnvoiSF.Parameters.AddWithValue("@ID_Document", (object)ID_Document ?? DBNull.Value);

                                            int retour = cmdEnvoiSF.ExecuteNonQuery();
                                            if (retour == 1)
                                            {
                                                retour = 1;//trans.Commit();scope.Complete();
                                            }
                                            else
                                            {
                                                throw new Exception("Impossible de flaguer l'avenant comme envoyé à SF");
                                            }
                                        }
                                        else
                                        {
                                            throw new Exception("Impossible d'envoyer l'avenant à SF");
                                        }
                                    }
                                }
                                else
                                {
                                    string idContratSF = Contrat.FindIdContratSF(NumContrat, entite);
                                    if (!string.IsNullOrWhiteSpace(idContratSF))
                                    {
                                        Indexer("Portefeuille", idContratSF, "", entite);
                                    }
                                }
                            }

                            /*else
                             *  scope.Complete();//trans.Commit();*/
                        }
                        else
                        {
                            throw new Exception("Erreur lors de l'insertion des informations de l'avenant dans la table 'Avenant'");
                        }
                    }
                    else
                    {
                        throw new Exception("Erreur lors de l'insertion du document dans la table 'CA_MEDIA'");
                    }
                }
                else if (ID_Type_Document == 0)//type non transcodé
                {
                    ID_Document = -1;
                }
                else if (ID_Type_Document == -1)//type à effacer
                {
                    ID_Document = -2;
                }
                else if (ID_Type_Document == -2)//nouveau type
                {
                    string strSQL = "INSERT INTO [Type_Document_Compagnie] ([CodeCompagnie],[NomType_Comapagnie],[ID_Type_Document])"
                                    + " VALUES (@CodeCompagnie,@NomType_Comapagnie,@ID_Type_Document)";

                    if (entite == "NSAS")
                    {
                        con = new SqlConnection("data source=192.168.1.2\\SQL2008r2qualif;Database=GEDNortia_Invest;Uid=sa;password=NICKEL2000;");
                    }
                    else
                    {
                        con = new SqlConnection("data source=192.168.1.2\\SQL2008r2qualif;Database=GEDNortia_Invest;Uid=sa;password=NICKEL2000;");
                    }

                    con.Open();
                    SqlCommand cmd = new SqlCommand(strSQL, con);
                    cmd.Parameters.AddWithValue("@CodeCompagnie", (object)CodeCompagnie ?? DBNull.Value);
                    cmd.Parameters.AddWithValue("@NomType_Comapagnie", (object)TypeAvenant ?? DBNull.Value);
                    cmd.Parameters.AddWithValue("@ID_Type_Document", (object)0 ?? DBNull.Value);

                    cmd.ExecuteNonQuery();
                    ID_Document = -1;
                }
            }
            catch (Exception)
            {
                ID_Document = 0;

                /*try
                 * {
                 *  if(trans!=null)
                 *      trans.Rollback();
                 * }
                 * catch (Exception exRb)
                 * {
                 *
                 * }*/
            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }

            return(ID_Document);
        }