public static void GetSignedDocumentFromDictao(string idDictao, string destFullPath, string destFileName, DTPClient.DocumentTypes dtpDocumentType)
        {
            //1- Instanciate DTPClient
            DTPClient dtpClient = new DTPClient(idDictao);

            //2- Save signed document to the server 
            //NB : only for CONTRACT type
            Upsilab.Infrastructure.DictaoDTPServiceReference.Document dtpDoc = dtpClient.GetDocumentByType(dtpDocumentType);
            dtpClient.SaveDocumentToFile(dtpDoc, destFullPath, destFileName);
        }
        private static bool ArchiveSignedDER(Upsilab.Data.Model.DER der)
        {
            bool bOK = true; //TODO

            //1- Seal and archive transaction
            DTPClient dtpClient = new DTPClient(der.idDictao);

            IList<string> documentTypesToSeal_ToArchive = new List<string>() { DTPClient.DocumentTypes.CONTRACT.ToString() };

            //Seal appendix 1 : presentation de l'etablissement ?
            if (der.AttachPresentation)
            {
                documentTypesToSeal_ToArchive.Add(DTPClient.DocumentTypes.APPENDIX1.ToString());
            }
                      

            try
            {
                dtpClient.SealTransaction(documentTypesToSeal_ToArchive);
                dtpClient.ArchiveTransaction(documentTypesToSeal_ToArchive);
                dtpClient.FinishTransaction(); //can't run "finish" if already "sealed" ?
            }
            catch { }

            //2- Update missionDocument table
            der.Status = ReportBL.Status.Completed.ToString(); //If signed by the 2 parts and saved to coffrefort
            der.Archived = true;
            der.IsOriginal = true;

            //3- Reduice firm credit after signature + mise au coffre
            //TODO : don't archive, sign doc if credit is not sufficient
            try
            {
                CreditOperationBL.SubstractCredit(der.CustomerProspect.idFirmInstitution, CreditOperationBL.OperationType.MISE_AU_COFFRE_AUTOMATIQUE_DER.ToString(), der.CustomerProspect.idUserCreated, der.CustomerProspect.idCustomer);
            }
            catch { }

            //4- Save signed document to the server 
            //a- CONTRACT
            ElectronicSafeDocumentBL.DocumentType docType = ElectronicSafeDocumentBL.DocumentType.FIL; //<=> DER
            string documentName = ElectronicSafeDocumentBL.BuildDocumentName(docType.ToString(), der.idCustomerProspect);
            string documentPath = ElectronicSafeDocumentBL.BuildClientDocumentPath(docType.ToString(), der.CustomerProspect);
            string fullPath = System.IO.Path.Combine(Upsilab.Business.Utility.ConfigurationManager.CoffreFortRootPath, documentPath);

            Upsilab.Infrastructure.DictaoDTPServiceReference.Document dtpDoc = dtpClient.GetDocumentByType(DTPClient.DocumentTypes.CONTRACT);
            dtpClient.SaveDocumentToFile(dtpDoc, fullPath, documentName);

            //Save document to ElectronicSafe table
            string docLabel = LanguageContentBL.Translate("der_title");
            string documentLabel = string.Format("{0} du {1}", docLabel, (der.DateUpdated.HasValue) ? der.DateUpdated.Value.ToString("dd/MM/yyyy") : der.DateCreated.ToString("dd/MM/yyyy"));

            ElectronicSafeDocumentBL.SaveDocumentToDB(documentName, docType.ToString(), documentLabel, documentPath, der.idCustomerProspect, der.idDictao, null);

            //b- APPENDIX 1 : BS
            if (der.AttachPresentation)
            {
                docType = ElectronicSafeDocumentBL.DocumentType.BS;
                documentName = ElectronicSafeDocumentBL.BuildDocumentName(docType.ToString(), der.idCustomerProspect);
                documentPath = ElectronicSafeDocumentBL.BuildClientDocumentPath(docType.ToString(), der.CustomerProspect);
                fullPath = System.IO.Path.Combine(Upsilab.Business.Utility.ConfigurationManager.CoffreFortRootPath, documentPath);

                dtpDoc = dtpClient.GetDocumentByType(DTPClient.DocumentTypes.APPENDIX1);

                if (dtpDoc != null)
                {
                    dtpClient.SaveDocumentToFile(dtpDoc, fullPath, documentName);

                    //Save document to ElectronicSafe table
                    documentLabel = string.Format("{0} du {1}", LanguageContentBL.Translate("der_bs_title"), (der.DateUpdated.HasValue) ? der.DateUpdated.Value.ToString("dd/MM/yyyy") : der.DateCreated.ToString("dd/MM/yyyy"));

                    ElectronicSafeDocumentBL.SaveDocumentToDB(documentName, docType.ToString(), documentLabel, documentPath, der.idCustomerProspect, der.idDictao, null);
                }

            }

            return bOK;
        }
        private static bool ArchiveSignedMissionDocument(Upsilab.Data.Model.MissionDocument missionDoc)
        {
            bool bOK = true; //TODO

            //1- Seal and archive transaction
            DTPClient dtpClient = new DTPClient(missionDoc.idDictao);

            IList<string> documentTypesToSeal_ToArchive = new List<string>() { DTPClient.DocumentTypes.CONTRACT.ToString() };

            //Seal appendix 1 : presentation de l'etablissement ?
            if (missionDoc.AttachPresentation)
            {
                documentTypesToSeal_ToArchive.Add(DTPClient.DocumentTypes.APPENDIX1.ToString());
            }
            //Seal appendix 2 : document d'entree en relation ?
            if (missionDoc.AttachInformationCard)
            {
                documentTypesToSeal_ToArchive.Add(DTPClient.DocumentTypes.APPENDIX2.ToString());
            }

            //Seal appendix 3 : convention de RTO ?
            if (missionDoc.AttachConventionRTO.HasValue && missionDoc.AttachConventionRTO.Value)
            {
                documentTypesToSeal_ToArchive.Add(DTPClient.DocumentTypes.APPENDIX3.ToString());
            }

            try
            {
                dtpClient.SealTransaction(documentTypesToSeal_ToArchive);
                dtpClient.ArchiveTransaction(documentTypesToSeal_ToArchive);
                dtpClient.FinishTransaction(); //can't run "finish" if already "sealed" ?
            }
            catch { }

            //2- Update missionDocument table
            missionDoc.Status = ReportBL.Status.Completed.ToString(); //If signed by the 2 parts and saved to coffrefort
            missionDoc.Archived = true;
            missionDoc.IsOriginal = true;

            //3- Reduice firm credit after signature + mise au coffre
            //TODO : don't archive, sign doc if credit is not sufficient
            try
            {
                CreditOperationBL.SubstractCredit(missionDoc.CustomerProspect.idFirmInstitution, CreditOperationBL.OperationType.MISE_AU_COFFRE_AUTOMATIQUE_DOCUMENT_MISSION.ToString(), missionDoc.CustomerProspect.idUserCreated, missionDoc.CustomerProspect.idCustomer);
            }
            catch { }

            //4- Save signed document to the server 
            //a- CONTRACT
            ElectronicSafeDocumentBL.DocumentType docType = (missionDoc.IsLetter) ? ElectronicSafeDocumentBL.DocumentType.LM : ElectronicSafeDocumentBL.DocumentType.RM;
            string documentName = ElectronicSafeDocumentBL.BuildDocumentName(docType.ToString(), missionDoc.idCustomerProspect);
            string documentPath = ElectronicSafeDocumentBL.BuildClientDocumentPath(docType.ToString(), missionDoc.CustomerProspect);
            string fullPath = System.IO.Path.Combine(Upsilab.Business.Utility.ConfigurationManager.CoffreFortRootPath, documentPath);

            Upsilab.Infrastructure.DictaoDTPServiceReference.Document dtpDoc = dtpClient.GetDocumentByType(DTPClient.DocumentTypes.CONTRACT);
            dtpClient.SaveDocumentToFile(dtpDoc, fullPath, documentName);

            //Save document to ElectronicSafe table
            string missionDocLabel = (missionDoc.IsLetter) ? "Lettre de mission" : "Rapport de mission";
            string documentLabel = string.Format("{0} du {1}", missionDocLabel, (missionDoc.DateUpdated.HasValue) ? missionDoc.DateUpdated.Value.ToString("dd/MM/yyyy") : missionDoc.DateCreated.ToString("dd/MM/yyyy"));

            ElectronicSafeDocumentBL.SaveDocumentToDB(documentName, docType.ToString(), documentLabel, documentPath, missionDoc.idCustomerProspect, missionDoc.idDictao, null);

            //b- APPENDIX 1 : BS
            if (missionDoc.AttachPresentation)
            {
                docType = ElectronicSafeDocumentBL.DocumentType.BS;
                documentName = ElectronicSafeDocumentBL.BuildDocumentName(docType.ToString(), missionDoc.idCustomerProspect);
                documentPath = ElectronicSafeDocumentBL.BuildClientDocumentPath(docType.ToString(), missionDoc.CustomerProspect);
                fullPath = System.IO.Path.Combine(Upsilab.Business.Utility.ConfigurationManager.CoffreFortRootPath, documentPath);

                dtpDoc = dtpClient.GetDocumentByType(DTPClient.DocumentTypes.APPENDIX1);

                if (dtpDoc != null)
                {
                    dtpClient.SaveDocumentToFile(dtpDoc, fullPath, documentName);

                    //Save document to ElectronicSafe table
                    documentLabel = string.Format("{0} du {1}", "Présentation de l'établissement", (missionDoc.DateUpdated.HasValue) ? missionDoc.DateUpdated.Value.ToString("dd/MM/yyyy") : missionDoc.DateCreated.ToString("dd/MM/yyyy"));

                    ElectronicSafeDocumentBL.SaveDocumentToDB(documentName, docType.ToString(), documentLabel, documentPath, missionDoc.idCustomerProspect, missionDoc.idDictao, null);
                }

            }

            //c- APPENDIX 2 : FIL => DER
            if (missionDoc.AttachInformationCard)
            {
                docType = ElectronicSafeDocumentBL.DocumentType.FIL;
                documentName = ElectronicSafeDocumentBL.BuildDocumentName(docType.ToString(), missionDoc.idCustomerProspect);
                documentPath = ElectronicSafeDocumentBL.BuildClientDocumentPath(docType.ToString(), missionDoc.CustomerProspect);
                fullPath = System.IO.Path.Combine(Upsilab.Business.Utility.ConfigurationManager.CoffreFortRootPath, documentPath);

                dtpDoc = dtpClient.GetDocumentByType(DTPClient.DocumentTypes.APPENDIX2);

                if (dtpDoc != null)
                {
                    dtpClient.SaveDocumentToFile(dtpDoc, fullPath, documentName);

                    //Save document to ElectronicSafe table
                    documentLabel = string.Format("{0} du {1}", "Document d'entrée en relation", (missionDoc.DateUpdated.HasValue) ? missionDoc.DateUpdated.Value.ToString("dd/MM/yyyy") : missionDoc.DateCreated.ToString("dd/MM/yyyy"));

                    ElectronicSafeDocumentBL.SaveDocumentToDB(documentName, docType.ToString(), documentLabel, documentPath, missionDoc.idCustomerProspect, missionDoc.idDictao, null);
                }

            }

            // d- APPENDIX 3 : CRTO
            if (missionDoc.AttachConventionRTO.HasValue && missionDoc.AttachConventionRTO.Value)
            {
                docType = ElectronicSafeDocumentBL.DocumentType.CRTO;
                documentName = ElectronicSafeDocumentBL.BuildDocumentName(docType.ToString(), missionDoc.idCustomerProspect);
                documentPath = ElectronicSafeDocumentBL.BuildClientDocumentPath(docType.ToString(), missionDoc.CustomerProspect);
                fullPath = System.IO.Path.Combine(Upsilab.Business.Utility.ConfigurationManager.CoffreFortRootPath, documentPath);

                dtpDoc = dtpClient.GetDocumentByType(DTPClient.DocumentTypes.APPENDIX3);

                if (dtpDoc != null)
                {
                    dtpClient.SaveDocumentToFile(dtpDoc, fullPath, documentName);

                    //Save document to ElectronicSafe table
                    documentLabel = string.Format("{0} du {1}", "Convention de RTO", (missionDoc.DateUpdated.HasValue) ? missionDoc.DateUpdated.Value.ToString("dd/MM/yyyy") : missionDoc.DateCreated.ToString("dd/MM/yyyy"));

                    ElectronicSafeDocumentBL.SaveDocumentToDB(documentName, docType.ToString(), documentLabel, documentPath, missionDoc.idCustomerProspect, missionDoc.idDictao, null);
                }

            }

            return bOK;
        }
        private static bool ArchiveConfidentialityAgreementSignedDocument(Upsilab.Data.Model.CustomerProspect customer)
        {
            bool bOK = true; //TODO

            Upsilab.Data.Model.ConfidentialityAgreement confident = customer.ConfidentialityAgreement;

            //1- Seal and archive transaction
            DTPClient dtpClient = new DTPClient(confident.idDictao);

            IList<string> documentTypesToSeal_ToArchive = new List<string>() { DTPClient.DocumentTypes.CONTRACT.ToString() };

            try
            {
                dtpClient.SealTransaction(documentTypesToSeal_ToArchive);
                dtpClient.ArchiveTransaction(documentTypesToSeal_ToArchive);
                dtpClient.FinishTransaction(); //can't run "finish" if already "sealed" ?
            }
            catch { }

            //2- Update report table
            confident.Status = ReportBL.Status.Completed.ToString(); //If signed by the 2 parts and saved to coffrefort
            confident.Archived = true;

            //3- Reduice firm credit after signature + mise au coffre
            //TODO : don't archive, sign doc if credit is not sufficient
            try
            {
                CreditOperationBL.SubstractCredit(customer.idFirmInstitution, CreditOperationBL.OperationType.MISE_AU_COFFRE_AUTOMATIQUE_ENGAGEMENT_CONFIDENTIALITE.ToString(), customer.idUserCreated, customer.idCustomer);
            }
            catch { }

            //4- Save signed document to the server
            string documentName = ElectronicSafeDocumentBL.BuildDocumentName(ElectronicSafeDocumentBL.DocumentType.EC.ToString(), customer.idCustomer);
            string documentPath = ElectronicSafeDocumentBL.BuildClientDocumentPath(ElectronicSafeDocumentBL.DocumentType.EC.ToString(), customer);
            string fullPath = System.IO.Path.Combine(Upsilab.Business.Utility.ConfigurationManager.CoffreFortRootPath, documentPath);

            Upsilab.Infrastructure.DictaoDTPServiceReference.Document dtpDoc = dtpClient.GetDocumentByType(DTPClient.DocumentTypes.CONTRACT);
            dtpClient.SaveDocumentToFile(dtpDoc, fullPath, documentName);

            //5- Save document to ElectronicSafe table
            string documentLabel = "Engagement de confidentialité";

            ElectronicSafeDocumentBL.SaveDocumentToDB(documentName, ElectronicSafeDocumentBL.DocumentType.EC.ToString(), documentLabel, documentPath, customer.idCustomer, confident.idDictao, null);

            return bOK;
        }
        private static bool ArchiveReportSignedDocument(Upsilab.Data.Model.Report report)
        {
            bool bOK = true; //TODO

            //1- Seal and archive transaction
            DTPClient dtpClient = new DTPClient(report.idDictao);

            IList<string> documentTypesToSeal_ToArchive = new List<string>() { DTPClient.DocumentTypes.CONTRACT.ToString() };

            try
            {
                dtpClient.SealTransaction(documentTypesToSeal_ToArchive);
                dtpClient.ArchiveTransaction(documentTypesToSeal_ToArchive);
                dtpClient.FinishTransaction(); //can't run "finish" if already "sealed" ?
            }
            catch { }

            //2- Update report table
            report.Status = ReportBL.Status.Completed.ToString(); //If signed by the 2 parts and saved to coffrefort
            report.Archived = true;
            report.IsOriginal = true;

            //3- Reduice firm credit after signature + mise au coffre
            //TODO : don't archive, sign doc if credit is not sufficient
            try
            {
                CreditOperationBL.SubstractCredit(report.CustomerProspect.idFirmInstitution, CreditOperationBL.OperationType.MISE_AU_COFFRE_RECUEIL.ToString(), report.CustomerProspect.idUserCreated, report.CustomerProspect.idCustomer);
            }
            catch { }

            //4- Save signed document to the server
            string documentName = ElectronicSafeDocumentBL.BuildDocumentName(ElectronicSafeDocumentBL.DocumentType.RC.ToString(), report.idCustomerProspect);
            string documentPath = ElectronicSafeDocumentBL.BuildClientDocumentPath(ElectronicSafeDocumentBL.DocumentType.RC.ToString(), report.CustomerProspect);
            string fullPath = System.IO.Path.Combine(Upsilab.Business.Utility.ConfigurationManager.CoffreFortRootPath, documentPath);

            Upsilab.Infrastructure.DictaoDTPServiceReference.Document dtpDoc = dtpClient.GetDocumentByType(DTPClient.DocumentTypes.CONTRACT);
            dtpClient.SaveDocumentToFile(dtpDoc, fullPath, documentName);

            //5- Save document to ElectronicSafe table
            string documentLabel = string.Format("Questionnaire Client du {0}", (report.DateUpdated.HasValue) ? report.DateUpdated.Value.ToString("dd/MM/yyyy") : report.DateCreated.ToString("dd/MM/yyyy"));

            ElectronicSafeDocumentBL.SaveDocumentToDB(documentName, ElectronicSafeDocumentBL.DocumentType.RC.ToString(), documentLabel, documentPath, report.idCustomerProspect, report.idDictao, null);

            return bOK;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="docToSignPathFileName"></param>
        /// <param name="user"></param>
        /// <param name="idDictao"></param>
        /// <param name="businessId"></param>
        /// <param name="backUrl"></param>
        /// <returns>IHM url</returns>
        private static string SignBySecondUser(Upsilab.Data.Model.User user, bool isCustomer, string idDictao, string backUrl)
        {
            if (string.IsNullOrEmpty(idDictao))
            {
                //throw new Exception(SignatureExceptionCode.TransactionIdMissing.ToString());
                throw new Exception("La transaction de cette signature est expirée.");
            }

            DTPClient dtpClient = new DTPClient(idDictao);

            //User1 : create access user for second user (Adviser ?)
            //string termAndConditionsFileName = "termsAndConditions_fr.pdf";
            string termAndConditionsFileName = "convention_preuve.pdf";
            string termAndConditionsUrl = string.Format("{0}/Client/Documents/{1}", Upsilab.Business.Utility.UrlHelper.GetHost(), termAndConditionsFileName);
            string accessId = dtpClient.CreateUserAccess(string.Empty, user.UserName, user.UserFirstName, user.UserMobilePhone, backUrl, true, termAndConditionsUrl, isCustomer);

            //Send OTP by sms to user
            dtpClient.SendOtp(accessId);

            string IHM_url = dtpClient.BuildUserInterfaceUrl(accessId);

            return IHM_url;
        }
        /// <summary>
        /// Sign by the first user
        /// </summary>
        /// <param name="docsToSignPathFileName"></param>
        /// <param name="user"></param>
        /// <param name="idDictao"></param>
        /// <param name="businessId"></param>
        /// <param name="backUrl"></param>
        /// <param name="IHM_Url"></param>
        /// <returns></returns>
        private static string SignByFirstUser(List<DocumentToSign> documentsToSign, Upsilab.Data.Model.User user, bool isCustomer, string idDictao, string businessId, string backUrl, out string IHM_Url)
        {
            DTPClient dtpClient = new DTPClient();

            //If idDictao has value, cancel this transaction and create a new one
            if (!string.IsNullOrEmpty(idDictao))
            {
                dtpClient.TransactionId = idDictao;
                try
                {
                    dtpClient.CancelTransaction();
                }
                catch { }
            }

            //#1 : Init transaction
            dtpClient.InitTransaction(businessId);

            //#2 : Provide document
            dtpClient.ProvideDocuments(documentsToSign);

            //#3 : create access user
            //string termAndConditionsFileName = "termsAndConditions_fr.pdf";
            string termAndConditionsFileName = "convention_preuve.pdf";
            string termAndConditionsUrl = string.Format("{0}/Client/Documents/{1}", Upsilab.Business.Utility.UrlHelper.GetHost(), termAndConditionsFileName);
            string accessId = dtpClient.CreateUserAccess(string.Empty, user.UserName, user.UserFirstName, user.UserMobilePhone, backUrl, true, termAndConditionsUrl, isCustomer);

            //#4 : send OTP by sms to user
            dtpClient.SendOtp(accessId);

            //#5 : build urlIhm
            IHM_Url = dtpClient.BuildUserInterfaceUrl(accessId);

            idDictao = dtpClient.TransactionId;

            return dtpClient.TransactionId;
        }