/// <summary>
        /// 
        /// </summary>
        /// <param name="idReport"></param>
        /// <returns>The url to access the signature page</returns>
        public static string SignConfidentialityAgreementByAdviser(Guid idCustomer, string docToSignPathFileName, Upsilab.Data.Model.User userAdviser)
        {
            using (UpsilabEntities context = new UpsilabEntities())
            {
                Upsilab.Data.Model.CustomerProspect customer = (from cust in context.CustomerProspect.Include("ConfidentialityAgreement").Include("User").Include("User1")
                                                                where cust.idCustomer == idCustomer
                                                                select cust).FirstOrDefault();

                if (customer == null)
                {
                    //throw new Exception(SignatureExceptionCode.DataMissingInDB.ToString());
                    throw new Exception("Merci de sélectionner le client.");
                }

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

                if (confident == null)
                {
                    confident = new Upsilab.Data.Model.ConfidentialityAgreement()
                    {
                        idConfidentialityAgreement = Upsilab.Business.Utility.GuidHelper.GenerateGuid(),
                        Status = ReportBL.Status.InProgress.ToString(),
                        DateCreated = DateTime.Now,
                        SignedByCustomer = false,
                        SignedByAdviser = false,
                        Archived = false
                    };

                    context.ConfidentialityAgreement.Attach(confident);
                    context.ObjectStateManager.ChangeObjectState(confident, System.Data.EntityState.Added);

                    customer.idConfidentialityAgreement = confident.idConfidentialityAgreement;
                }

                //Check if already signed by adviser
                if (confident.SignedByAdviser)
                {
                    //throw new Exception(SignatureExceptionCode.AlreadySignedByAdviser.ToString());
                    throw new Exception("Vous avez déjà signé l'engagement de confidentialité.");
                }

                // Data needed
                string businessId = string.Format("{0}-{1}", CONFIDENTIALITY_AGREEMENT, idCustomer);
                string backUrl = string.Format("{0}/User/Signature_4_1/RetourSignatureEngagementParConseiller", Upsilab.Business.Utility.UrlHelper.GetHost());
                string IHM_Url = string.Empty;

                // Check if already signed by customer, if not, the adviser signs the doc first.
                if (!confident.SignedByCustomer)
                {
                    SignatureParameters signatureParameters = new SignatureParameters()
                    {
                        page = 8,
                        x_adviser = 28,
                        x_customer = 114,
                        y = 85
                    };

                    //Document to sign
                    DocumentToSign doc = new DocumentToSign()
                    {
                        Label = "Engagement de confidentialité",
                        Description = "Engagement de confidentialité",
                        PathFilename = docToSignPathFileName,
                        Type = DTPClient.DocumentTypes.CONTRACT.ToString(),
                        SignatureParameters = signatureParameters
                    };

                    List<DocumentToSign> documentsToSign = new List<DocumentToSign>() { doc };

                    //Sign document
                    string transactionId = SignatureDocumentBL_4_1.SignByFirstUser(documentsToSign, userAdviser, false, confident.idDictao, businessId, backUrl, out IHM_Url);

                    //Save transaction Id to table
                    confident.idDictao = transactionId;
                    context.SaveChanges();
                }
                else
                {
                    IHM_Url = SignatureDocumentBL_4_1.SignBySecondUser(userAdviser, false, confident.idDictao, backUrl);
                }

                return IHM_Url;

            }
        }
        public ActionResult SignerDocumentMissionParConseiller(string id)
        {
            string error = string.Empty;
            string urlIHM_Dictao = string.Empty;
            int pageCount = 0;
            MissionDocument missionDoc = new MissionDocument();
            List<DocumentToSign> documentsToSign = new List<DocumentToSign>();

            try
            {
                // Id mission doc = id in parameter                
                missionDoc = MissionDocumentBL.GetMissionDocumentById(new Guid(id));

                if (missionDoc == null)
                {
                    error = LanguageData.GetContent("selectionner_un_doc_a_signer");
                }
                else
                {
                    CustomerProspect customer = missionDoc.CustomerProspect;
                    SignatureParameters signatureParameters = null;

                    #region 1- Mission document
                    //A- Build pdf mission document to sign
                    string docType = (missionDoc.IsLetter) ? ElectronicSafeDocumentBL.DocumentType.LM.ToString() : ElectronicSafeDocumentBL.DocumentType.RM.ToString();

                    string destFullPath = ElectronicSafeDocumentBL.BuildClientUploadDocumentPath(docType, customer);
                    string filename = ElectronicSafeDocumentBL.BuildDocumentName(docType, missionDoc.idMissionDocument, false);

                    string destMissionDocFilePath = System.IO.Path.Combine(destFullPath, filename);

                    //If Upsideo model, generate pdf
                    //If Firm model, merge uploaded file with the first and last page
                    if (missionDoc.IsUpsideoModel)
                    {
                        //1 : Génération du PDF
                        if (missionDoc.IsLetter) //Lettre de Mission
                        {
                            bool isPP = (missionDoc.CustomerProspect.IsCorporation.HasValue) ? ((missionDoc.CustomerProspect.IsCorporation.Value) ? false : true) : false;
                            if (isPP)
                            {
                                MissionDocumentPdfGenerationBL.GeneratePP_EngagementLetter(missionDoc, destMissionDocFilePath, false);
                            }
                            else
                            {
                                MissionDocumentPdfGenerationBL.GeneratePM_EngagementLetter(missionDoc, destMissionDocFilePath, false);
                            }
                        }
                        else // Rapport de mission
                        {
                            MissionDocumentPdfGenerationBL.Generate_MissionReport(missionDoc, destMissionDocFilePath, false);
                        }

                    }
                    else
                    {
                        //Merge pdf
                        filename = missionDoc.FileName;
                        string uploadedDoc = System.IO.Path.Combine(destFullPath, filename);
                        if (missionDoc.IsLetter)
                        {
                            MissionDocumentPdfGenerationBL.GenerateEngagementLetterByMerge(missionDoc, uploadedDoc, destMissionDocFilePath, false, out pageCount);
                        }
                        else
                        {
                            MissionDocumentPdfGenerationBL.GenerateReportLetterByMerge(missionDoc, uploadedDoc, destMissionDocFilePath, false, out pageCount);
                        }
                    }

                    if (!System.IO.File.Exists(destMissionDocFilePath))
                    {
                        error = LanguageData.GetContent("doc_n_a_pas_ete_genere_correctement");
                    }
                    else
                    {
                        //B- Prepare object to sign
                        signatureParameters = new SignatureParameters()
                        {
                            page = (pageCount > 0) ? pageCount : (missionDoc.IsLetter) ? 13 : 9, //LM : 10pages; RM : 8pages
                            x_adviser = 33,
                            x_customer = 120,
                            y = 177
                        };

                        string typeDocLabel = (missionDoc.IsLetter) ? LanguageData.GetContent("lettre_de_mission") : LanguageData.GetContent("rapport_de_mission");

                        DocumentToSign docMD = new DocumentToSign()
                        {
                            Label = typeDocLabel,
                            Description = typeDocLabel,
                            PathFilename = destMissionDocFilePath,
                            Type = DTPClient.DocumentTypes.CONTRACT.ToString(),
                            SignatureParameters = signatureParameters
                        };

                        documentsToSign.Add(docMD);
                    }
                    #endregion

                    #region 2- Convention RTO Document
                    if (missionDoc.AttachConventionRTO.HasValue && missionDoc.AttachConventionRTO.Value)
                    {
                        //A- Generate CRTO pdf
                        pageCount = 0;
                        docType = (missionDoc.IsLetter) ? ElectronicSafeDocumentBL.DocumentType.LM.ToString() : ElectronicSafeDocumentBL.DocumentType.RM.ToString();
                        docType = docType + "_" + ElectronicSafeDocumentBL.DocumentType.CRTO.ToString();

                        destFullPath = ElectronicSafeDocumentBL.BuildClientUploadDocumentPath(docType, customer);
                        filename = ElectronicSafeDocumentBL.BuildDocumentName(docType, missionDoc.idMissionDocument, false);

                        string destCRTOFilePath = System.IO.Path.Combine(destFullPath, filename);

                        //If Upsideo model, generate pdf
                        //If Firm model, merge uploaded file with the first and last page
                        if (missionDoc.IsConventionRTOFromYourModel.HasValue && !missionDoc.IsConventionRTOFromYourModel.Value)
                        {
                            // MissionDocumentPdfGenerationBL.GenerateRTO(missionDoc, destCRTOFilePath, false);
                            pageCount = MissionDocumentPdfGenerationBL.GenerateRTO(missionDoc.idMissionDocument, missionDoc.User,
                                                                         missionDoc.CustomerProspect, destCRTOFilePath,
                                                                         false);
                        }
                        else if (!string.IsNullOrEmpty(missionDoc.ConventionRTOFirmModelFileName))
                        {
                            //Merge pdf
                            filename = missionDoc.ConventionRTOFirmModelFileName;
                            string uploadedDoc = System.IO.Path.Combine(destFullPath, filename);

                            MissionDocumentPdfGenerationBL.GenerateRTOByMerge(missionDoc, uploadedDoc, destCRTOFilePath, false, out pageCount);
                        }

                        if (!System.IO.File.Exists(destCRTOFilePath))
                        {
                            error = LanguageData.GetContent("pdf_rto_n_a_pas_ete_genere_correctement");
                        }
                        else
                        {
                            //B- Prepare object to send to signature
                            signatureParameters = new SignatureParameters()
                            {
                                page = (pageCount > 0) ? pageCount : 4, //CRTO : 4pages
                                x_adviser = 33,
                                x_customer = 120,
                                y = 177
                            };

                            DocumentToSign docRTO = new DocumentToSign()
                            {
                                Label = LanguageData.GetContent("convention_de_rto"),
                                Description = LanguageData.GetContent("convention_de_rto"),
                                PathFilename = destCRTOFilePath,
                                Type = DTPClient.DocumentTypes.APPENDIX3.ToString(),
                                SignatureParameters = signatureParameters
                            };

                            documentsToSign.Add(docRTO);
                        }
                    }
                    #endregion

                    #region 3- Presentation etablissement
                    if (missionDoc.AttachPresentation)
                    {
                        //A- PDF ?
                        pageCount = 0;
                        docType = ElectronicSafeDocumentBL.DocumentType.BS.ToString();
                        string firmDocumentPath = string.Format(@"{0}\FirmInstitution\FI_{1}", Server.MapPath("~/uploads"), missionDoc.CustomerProspect.FirmInstitution.idFirmInstitution);
                        destFullPath = ElectronicSafeDocumentBL.BuildClientUploadDocumentPath(docType, customer);
                        filename = ElectronicSafeDocumentBL.BuildDocumentName(docType, missionDoc.idMissionDocument, false);

                        string destBSFilePath = System.IO.Path.Combine(destFullPath, filename);

                        //From firmModel or from upsideomodel
                        if (missionDoc.IsPresentationFromModel)
                        {
                            //Merge pdf
                            filename = missionDoc.CustomerProspect.FirmInstitution.PresentationFile;
                            string uploadedDoc = System.IO.Path.Combine(firmDocumentPath, filename);

                            if (System.IO.File.Exists(uploadedDoc))
                            {
                                Upsilab.Business.Configuration.FirmInstitutionPdfGeneration.GeneratePresentationByMerge(missionDoc.CustomerProspect.FirmInstitution, uploadedDoc, destBSFilePath, false, out pageCount);
                            }
                            else
                            {
                                error = LanguageData.GetContent("pdf_presentation_etab_n_existe_pas_ou");
                            }

                        }
                        else
                        {
                            //From upsideo model => generate it
                            pageCount = Upsilab.Business.Configuration.FirmInstitutionPdfGeneration.GeneratePresentation(missionDoc.CustomerProspect.FirmInstitution, destBSFilePath, false);
                        }

                        if (!System.IO.File.Exists(destBSFilePath))
                        {
                            error = LanguageData.GetContent("pdf_presentation_etab_n_existe_pas_ou");
                        }
                        else
                        {
                            //B- Prepare object to send to signature
                            signatureParameters = new SignatureParameters()
                            {
                                //page = (pageCount > 0) ? pageCount : 6, //BS : 6pages
                                page = pageCount,
                                x_adviser = 33,
                                x_customer = 120,
                                y = 177
                            };

                            DocumentToSign docPE = new DocumentToSign()
                            {
                                Label = LanguageData.GetContent("presentation_de_l_etab"),
                                Description = LanguageData.GetContent("presentation_de_l_etab"),
                                PathFilename = destBSFilePath,
                                Type = DTPClient.DocumentTypes.APPENDIX1.ToString(),
                                SignatureParameters = signatureParameters
                            };

                            documentsToSign.Add(docPE);
                        }
                    }
                    #endregion

                    #region 4- Fiche d'information legale FIL => DER
                    if (string.IsNullOrEmpty(customer.DerStatus)
                        || (!string.IsNullOrEmpty(customer.DerStatus) && customer.DerStatus != DerModel.Status.Completed.ToString()))
                    {

                        if (missionDoc.AttachInformationCard)
                        {
                            //A- PDF ?
                            pageCount = 0;
                            docType = ElectronicSafeDocumentBL.DocumentType.FIL.ToString();
                            string firmDocumentPath = string.Format(@"{0}\FirmInstitution\FI_{1}", Server.MapPath("~/uploads"), missionDoc.CustomerProspect.FirmInstitution.idFirmInstitution);
                            destFullPath = ElectronicSafeDocumentBL.BuildClientUploadDocumentPath(docType, customer);
                            filename = ElectronicSafeDocumentBL.BuildDocumentName(docType, missionDoc.idMissionDocument, false);

                            string destFILFilePath = System.IO.Path.Combine(destFullPath, filename);

                            //From firmModel or from upsideomodel
                            if (missionDoc.IsInformationCardFromModel)
                            {
                                //Merge pdf
                                filename = missionDoc.CustomerProspect.FirmInstitution.LegalInformationFile;
                                string uploadedDoc = System.IO.Path.Combine(firmDocumentPath, filename);

                                if (System.IO.File.Exists(uploadedDoc))
                                {
                                    Upsilab.Business.Configuration.FirmInstitutionPdfGeneration.GenerateDERByMerge(missionDoc.CustomerProspect.FirmInstitution, uploadedDoc, destFILFilePath, false, out pageCount);
                                }
                                else
                                {
                                    error = LanguageData.GetContent("le_pdf_doc_entree_en_relation_n_existe_pas");
                                }

                            }
                            else
                            {
                                //From upsideo model => generate it
                                pageCount = Upsilab.Business.Configuration.FirmInstitutionPdfGeneration.GenerateDER(missionDoc.CustomerProspect.FirmInstitution, missionDoc.CustomerProspect.idCustomer, destFILFilePath, false);
                            }

                            if (!System.IO.File.Exists(destFILFilePath))
                            {
                                error = LanguageData.GetContent("le_pdf_doc_entree_en_relation_n_existe_pas");
                            }
                            else
                            {
                                //B- Prepare object to send to signature
                                signatureParameters = new SignatureParameters()
                                {
                                    page = (pageCount > 0) ? pageCount : 16, //DER : 14pages
                                    x_adviser = 33,
                                    x_customer = 120,
                                    y = 177
                                };

                                DocumentToSign docDER = new DocumentToSign()
                                {
                                    Label = LanguageData.GetContent("doc_entree_en_relation"),
                                    Description = LanguageData.GetContent("doc_entree_en_relation"),
                                    PathFilename = destFILFilePath,
                                    Type = DTPClient.DocumentTypes.APPENDIX2.ToString(),
                                    SignatureParameters = signatureParameters
                                };

                                documentsToSign.Add(docDER);
                            }
                        }
                    }
                    #endregion

                    //Signature
                    if (documentsToSign.Count == 0)
                    {
                        error = LanguageData.GetContent("aucun_doc_a_signer_veuillez");
                    }

                    if (string.IsNullOrEmpty(error))
                    {
                        // Sign the document mission
                        Upsilab.Data.Model.User userAdviser = (missionDoc.User != null) ? missionDoc.User : Upsilab.Business.Utility.SessionManager.GetUserSession();
                        urlIHM_Dictao = SignatureDocumentBL_4_1.SignMissionDocumentByAdviser(missionDoc.idMissionDocument, documentsToSign, userAdviser);
                    }
                }
            }
            catch (Exception ex)
            {
                Upsilab.Business.Log.Log.AppendException(ex);
                error = ex.Message;
            }

            if (!string.IsNullOrEmpty(error))
            {
                string missionDocLabel = (missionDoc.IsLetter) ? LanguageData.GetContent("lettre_de_mission") : LanguageData.GetContent("rapport_de_mission");

                SignatureModel signatureModel = new SignatureModel()
                {
                    H1Title = (missionDoc.IsLetter) ? LanguageData.GetContent("lettre_de_mission") : LanguageData.GetContent("rapport_de_mission"),
                    HeadTitle = string.Format("Signature {0}", missionDocLabel),
                    ErrorMessage = error
                };

                return View("AllerSignature", signatureModel);
            }
            else
            {
                return Redirect(urlIHM_Dictao);
            }

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="idReport"></param>
        /// <param name="docToSignPathFileName"></param>
        /// <returns>The url to access the signature page</returns>
        public static string SignReportByCustomer(Guid idReport, string docToSignPathFileName, int lastPage)
        {
            using (UpsilabEntities context = new UpsilabEntities())
            {
                Upsilab.Data.Model.Report report = (from rep in context.Report.Include("CustomerProspect")
                                                    where rep.idReport == idReport
                                                    select rep).FirstOrDefault();

                if (report == null)
                {
                    //throw new Exception(SignatureExceptionCode.DataMissingInDB.ToString());
                    throw new Exception("Merci de sélectionner le questionnaire à signer.");
                }

                // Check if already signed by customer
                if (report.SignedByCustomer)
                {
                    //throw new Exception(SignatureExceptionCode.AlreadySignedByCustomer.ToString());
                    throw new Exception("Le questionnaire a été déjà signé par le client.");
                }

                // Check if Firm has sufficient credit to send doc to coffre fort
                

                // Data needed
                string businessId = string.Format("{0}-{1}", REPORT, idReport);
                Upsilab.Data.Model.CustomerProspect customer = report.CustomerProspect;
                string backUrl = string.Format("{0}/Client/Signature_4_1/RetourSignatureRecueilParClient", Upsilab.Business.Utility.UrlHelper.GetHost());
                string IHM_Url = string.Empty;

                // Check if already signed by adviser, if not, the customer signs the doc first.
                if (!report.SignedByAdviser)
                {
                    bool isPP = (customer.IsCorporation.HasValue) ? ((customer.IsCorporation.Value) ? false : true) : false;

                    SignatureParameters signatureParameters = new SignatureParameters()
                    {
                        //page = (isPP) ? 30 : 19, //26 pages for PP / 19 pages for PM
                        page = lastPage,
                        x_adviser = 34,
                        x_customer = 120,
                        y = 178,
                        width = 64,
                        height = 36
                    };

                    //Document to sign
                    DocumentToSign doc = new DocumentToSign()
                    {
                        Label = "Questionnaire Client",
                        Description = "Questionnaire Client",
                        PathFilename = docToSignPathFileName,
                        Type = DTPClient.DocumentTypes.CONTRACT.ToString(),
                        SignatureParameters = signatureParameters
                    };

                    List<DocumentToSign> documentsToSign = new List<DocumentToSign>() { doc };

                    //Sign document
                    string transactionId = SignatureDocumentBL_4_1.SignByFirstUser(documentsToSign, customer.User, true, report.idDictao, businessId, backUrl, out IHM_Url);

                    //Save transaction Id to table
                    report.idDictao = transactionId;
                    context.SaveChanges();
                }
                else
                {
                    IHM_Url = SignatureDocumentBL_4_1.SignBySecondUser(customer.User, true, report.idDictao, backUrl);
                }

                return IHM_Url;
            }
        }
        public ActionResult SignerDERParConseiller(string id)
        {
            string error = string.Empty;
            string urlIHM_Dictao = string.Empty;
            int pageCount = 0;
            DER der = new DER();
            List<DocumentToSign> documentsToSign = new List<DocumentToSign>();

            try
            {
                // Id mission doc = id in parameter                
                der = DerBL.GetDERById(new Guid(id));

                if (der == null)
                {
                    error = LanguageData.GetContent("selectionner_un_doc_a_signer");
                }
                else
                {
                    CustomerProspect customer = der.CustomerProspect;
                    SignatureParameters signatureParameters = null;

                    #region 1- Fiche d'information legale FIL => DER
                    //A- PDF ?
                    pageCount = 0;
                    string docType = ElectronicSafeDocumentBL.DocumentType.FIL.ToString();
                    string firmDocumentPath = string.Format(@"{0}\FirmInstitution\FI_{1}", Server.MapPath("~/uploads"), der.CustomerProspect.FirmInstitution.idFirmInstitution);
                    string destFullPath = ElectronicSafeDocumentBL.BuildClientUploadDocumentPath(docType, customer);
                    string filename = ElectronicSafeDocumentBL.BuildDocumentName(docType, der.idDer, false);

                    string destFILFilePath = System.IO.Path.Combine(destFullPath, filename);

                    //From firmModel or from upsideomodel
                    if (der.IsDerFromModel)
                    {
                        //Merge pdf
                        filename = der.CustomerProspect.FirmInstitution.LegalInformationFile;
                        string uploadedDoc = System.IO.Path.Combine(firmDocumentPath, filename);

                        if (System.IO.File.Exists(uploadedDoc))
                        {
                            Upsilab.Business.Configuration.FirmInstitutionPdfGeneration.GenerateDERByMerge(der.CustomerProspect.FirmInstitution, uploadedDoc, destFILFilePath, false, out pageCount);
                        }
                        else
                        {
                            error = LanguageData.GetContent("le_pdf_doc_entree_en_relation_n_existe_pas");
                        }

                    }
                    else
                    {
                        //From upsideo model => generate it
                        pageCount = Upsilab.Business.Configuration.FirmInstitutionPdfGeneration.GenerateDER(der.CustomerProspect.FirmInstitution, der.CustomerProspect.idCustomer, destFILFilePath, false);
                    }

                    if (!System.IO.File.Exists(destFILFilePath))
                    {
                        error = LanguageData.GetContent("le_pdf_doc_entree_en_relation_n_existe_pas");
                    }
                    else
                    {
                        //B- Prepare object to send to signature
                        signatureParameters = new SignatureParameters()
                        {
                            page = (pageCount > 0) ? pageCount : 16, //DER : 14pages
                            x_adviser = 33,
                            x_customer = 120,
                            y = 177
                        };

                        DocumentToSign docDER = new DocumentToSign()
                        {
                            Label = LanguageData.GetContent("doc_entree_en_relation"),
                            Description = LanguageData.GetContent("doc_entree_en_relation"),
                            PathFilename = destFILFilePath,
                            Type = DTPClient.DocumentTypes.CONTRACT.ToString(),
                            SignatureParameters = signatureParameters
                        };

                        documentsToSign.Add(docDER);
                    }

                    #endregion

                    #region 2- Presentation etablissement
                    if (der.AttachPresentation)
                    {
                        //A- PDF ?
                        pageCount = 0;
                        docType = ElectronicSafeDocumentBL.DocumentType.BS.ToString();
                        firmDocumentPath = string.Format(@"{0}\FirmInstitution\FI_{1}", Server.MapPath("~/uploads"), der.CustomerProspect.FirmInstitution.idFirmInstitution);
                        destFullPath = ElectronicSafeDocumentBL.BuildClientUploadDocumentPath(docType, customer);
                        filename = ElectronicSafeDocumentBL.BuildDocumentName(docType, der.idDer, false);

                        string destBSFilePath = System.IO.Path.Combine(destFullPath, filename);

                        //From firmModel or from upsideomodel
                        if (der.IsPresentationFromModel)
                        {
                            //Merge pdf
                            filename = der.CustomerProspect.FirmInstitution.PresentationFile;
                            string uploadedDoc = System.IO.Path.Combine(firmDocumentPath, filename);

                            if (System.IO.File.Exists(uploadedDoc))
                            {
                                Upsilab.Business.Configuration.FirmInstitutionPdfGeneration.GeneratePresentationByMerge(der.CustomerProspect.FirmInstitution, uploadedDoc, destBSFilePath, false, out pageCount);
                            }
                            else
                            {
                                error = LanguageData.GetContent("pdf_presentation_etab_n_existe_pas_ou");
                            }

                        }
                        else
                        {
                            //From upsideo model => generate it
                            pageCount = Upsilab.Business.Configuration.FirmInstitutionPdfGeneration.GeneratePresentation(der.CustomerProspect.FirmInstitution, destBSFilePath, false);
                        }

                        if (!System.IO.File.Exists(destBSFilePath))
                        {
                            error = LanguageData.GetContent("pdf_presentation_etab_n_existe_pas_ou");
                        }
                        else
                        {
                            //B- Prepare object to send to signature
                            signatureParameters = new SignatureParameters()
                            {
                                // page = (pageCount > 0) ? pageCount : 6, //BS : 6pages
                                page = pageCount,
                                x_adviser = 33,
                                x_customer = 120,
                                y = 177
                            };

                            DocumentToSign docPE = new DocumentToSign()
                            {
                                Label = LanguageData.GetContent("presentation_de_l_etab"),
                                Description = LanguageData.GetContent("presentation_de_l_etab"),
                                PathFilename = destBSFilePath,
                                Type = DTPClient.DocumentTypes.APPENDIX1.ToString(),
                                SignatureParameters = signatureParameters
                            };

                            documentsToSign.Add(docPE);
                        }
                    }
                    #endregion

                    //Signature
                    if (documentsToSign.Count == 0)
                    {
                        error = LanguageData.GetContent("aucun_doc_a_signer_veuillez");
                    }

                    if (string.IsNullOrEmpty(error))
                    {
                        // Sign the document mission
                        Upsilab.Data.Model.User userAdviser = (der.User != null) ? der.User : Upsilab.Business.Utility.SessionManager.GetUserSession();
                        urlIHM_Dictao = SignatureDocumentBL_4_1.SignDERByAdviser(der.idDer, documentsToSign, userAdviser);
                    }
                }
            }
            catch (Exception ex)
            {
                Upsilab.Business.Log.Log.AppendException(ex);
                error = ex.Message;
            }

            if (!string.IsNullOrEmpty(error))
            {
                SignatureModel signatureModel = new SignatureModel()
                {
                    H1Title = Business.Language.LanguageContentBL.Translate("der_title"),
                    HeadTitle = string.Format("Signature {0}", Business.Language.LanguageContentBL.Translate("der_title")),
                    ErrorMessage = error
                };

                return View("AllerSignature", signatureModel);
            }
            else
            {
                return Redirect(urlIHM_Dictao);
            }

        }