public ActionResult EnvoyerDocumentMissionAuClient(string id)
        {
            string error = string.Empty;
            MissionDocument missionDoc = null;
            string missionDocLabel = "Document";

            try
            {
                missionDoc = MissionDocumentBL.GetMissionDocumentById(new Guid(id));

                if (missionDoc == null)
                {
                    error = _languageData.GetContent("merci_de_selectionner_le_doc_a_envoyer");
                }
                else if (missionDoc.SignedByCustomer)
                {
                    error = string.Format(_languageData.GetContent("client_a_deja_signe_le_doc"), missionDoc.DateCustomerSignature.Value.ToShortDateString());
                }
                else
                {
                    SignatureDocumentBL.SendMissionDocumentMailToClient(missionDoc, true);
                }                
            }
            catch (Exception ex)
            {
                Upsilab.Business.Log.Log.AppendException(ex);
                error = ex.Message;
            }


            if (missionDoc != null)
            {
                missionDocLabel = (missionDoc.IsLetter) ? _languageData.GetContent("lettre_de_mission") : _languageData.GetContent("rapport_de_mission");
            }

            SignatureModel signatureModel = new SignatureModel()
            {
                H1Title = missionDocLabel,
                HeadTitle = string.Format("Signature {0}", missionDocLabel),
                ErrorMessage = error,
                ContentMessage =  _languageData.GetContent("email_invitation_à_signer_envoyé")
            };

            return View("AllerSignature", signatureModel);
        }
        public ActionResult SignerDocumentMissionParClient(string id)
        {
            string error = string.Empty;
            string urlIHM_Dictao = string.Empty;
            int pageCount = 0;
            MissionDocument missionDoc = new MissionDocument();

            try
            {
                // Id latest mission document = id in parameter                
                if (string.IsNullOrEmpty(id))
                {
                    error = _languageData.GetContent("selectionner_un_doc_a_signer");
                }
                else
                {
                    missionDoc = MissionDocumentBL.GetMissionDocumentById(new Guid(id));

                    if (missionDoc == null)
                    {
                        error = _languageData.GetContent("selectionner_un_doc_a_signer");
                    }
                    else
                    {
                        List<DocumentToSign> documentsToSign = this.GetDocumentsToSign(missionDoc); //TODO generate pdf ?
                        
                        // Sign the mission document (Customer signs secondly)
                        urlIHM_Dictao = SignatureDocumentBL.SignMissionDocumentByCustomer(missionDoc.idMissionDocument, documentsToSign, pageCount);
                    }
                }
            }
            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(_languageData.GetContent("signature_"), missionDocLabel),
                    ErrorMessage = error 
                };

                return View("AllerSignature", signatureModel);
            }
            else
            {
                return Redirect(urlIHM_Dictao);
            }
        }
        public ActionResult SignerDocumentMissionParConseiller(string id)
        {
            string error = string.Empty;
            string urlIHM_Dictao = string.Empty;
            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
                {
                    documentsToSign = this.GetDocumentsToSign(missionDoc); //TODO generate pdf ?

                    //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.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);
            }

        }
        public ActionResult SignerCRTOParConseiller(string id)
        {
            string error = string.Empty;
            string urlIHM_Dictao = string.Empty;

            try
            {
                // Id Customer = id in parameter                
                if (string.IsNullOrEmpty(id))
                {
                    error = _languageData.GetContent("client_identité_non_reconnue");
                }
                else
                {
                    //Adviser always signs the EC secondly
                    CustomerProspect customer =Business.CustomerProspect.CustomerProspectBL.GetCustomerProspectById(Guid.Parse(id));

                    string pdfPath = string.Empty;
                    // TODO ==> génération du pdf
                    if (customer != null)
                    {
                        pdfPath = GenerateRTODocument(customer);

                        var cust = Business.CustomerProspect.CustomerProspectBL.GetCustomerProspectById(customer.idCustomer);
                        if (cust != null) customer = cust;

                        // Sign the confidentiality agreement
                        User userAdviser = (customer.ConventionRTO != null && customer.ConventionRTO.User != null) ? customer.ConventionRTO.User : SessionManager.GetUserSession();
                        urlIHM_Dictao = SignatureDocumentBL.SignCRTOByAdviser(customer.idCustomer, pdfPath, 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("crto_title"),
                    HeadTitle = string.Format(_languageData.GetContent("signature_"), Business.Language.LanguageContentBL.Translate("crto_title")),
                    ErrorMessage = error
                };

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

        }
        public ActionResult SignerDocument(string id)
        {
            string error = string.Empty;
            string urlIHM_Dictao = string.Empty;        

            try
            {
                // Id Signatory = id in parameter                
                urlIHM_Dictao = SignatureDocumentBL.SignDocument(new Guid(id));
            }
            catch (Exception ex)
            {
                Upsilab.Business.Log.Log.AppendException(ex);
                error = ex.Message;
            }

            if (!string.IsNullOrEmpty(error))
            {
                SignatureModel signatureModel = new SignatureModel()
                {
                    H1Title = "Signature",
                    HeadTitle = "Signature",
                    ErrorMessage = error
                };

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

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

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

                if (der == null)
                {
                    error = _languageData.GetContent("selectionner_un_doc_a_signer");
                }
                else
                {
                    documentsToSign = this.GetDocumentsToSign(der); //TODO generate pdf ?

                    //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.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);
            }

        }
        public ActionResult SignerCRTOParClient(string id)
        {
            string error = string.Empty;
            string urlIHM_Dictao = string.Empty;

            try
            {
                // Id Customer = id in parameter                
                if (string.IsNullOrEmpty(id))
                {
                    error = _languageData.GetContent("client_identité_non_reconnue");
                }
                else
                {
                    Upsilab.Data.Model.CustomerProspect customer = Upsilab.Business.CustomerProspect.CustomerProspectBL.GetCustomerProspectById(Guid.Parse(id));

                    // Sign the confidentiality agreement                        
                    urlIHM_Dictao = SignatureDocumentBL.SignCRTOByCustomer(customer.idCustomer);
                }
            }
            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("crto_title"),
                    HeadTitle = string.Format(_languageData.GetContent("signature_"), Business.Language.LanguageContentBL.Translate("crto_title")),
                    ErrorMessage = error
                };

                return View("AllerSignature", signatureModel);
            }
            else
            {
                return Redirect(urlIHM_Dictao);
            }
        }
        public ActionResult GeneratePdf(string id)
        {
            MissionDocument missionDoc = null;
            string error = string.Empty;
            string missionDocLabel = "Document";

            try
            {
                //Get mission document
                missionDoc = MissionDocumentBL.GetMissionDocumentById(new Guid(id));
                if (missionDoc == null)
                {
                    error = LanguageData.GetContent("selectionnez_doc_a_generer");
                }
                else
                {
                    this.GenerateMissionDocument(missionDoc, true);
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }

            SignatureModel signatureModel = new SignatureModel()
            {
                HeadTitle = LanguageData.GetContent("generation_doc_mission"),
                H1Subtitle = LanguageData.GetContent("generation_pdf"),
                H1Title = (missionDoc != null) ? ((missionDoc.IsLetter) ? "Lettre de mission" : "Rapport de mission") : missionDocLabel
            };

            if (!string.IsNullOrEmpty(error))
            {
                signatureModel.ContentTitle = LanguageData.GetContent("erreur_generation_pdf");
                signatureModel.ContentMessage = error;
            }
            else
            {
                signatureModel.ContentTitle = LanguageData.GetContent("succes_generation");
                signatureModel.ContentMessage = LanguageData.GetContent("doc_generé_avec_succes");
            }

            return View("Message", signatureModel);
        }
        public ActionResult SignerDERParClient(string id)
        {
            string error = string.Empty;
            string urlIHM_Dictao = string.Empty;
            int pageCount = 0;
            DER der = new DER();

            try
            {
                // Id latest mission document = id in parameter                
                if (string.IsNullOrEmpty(id))
                {
                    error = _languageData.GetContent("selectionner_un_doc_a_signer");
                }
                else
                {
                    der = DerBL.GetDERById(new Guid(id));

                    if (der == null)
                    {
                        error = _languageData.GetContent("selectionner_un_doc_a_signer");
                    }
                    else
                    {
                        List<DocumentToSign> documentsToSign = this.GetDocumentsToSign(der); //TODO generate pdf ?

                        // Sign the mission document (Customer signs secondly)
                        urlIHM_Dictao = SignatureDocumentBL.SignDERByCustomer(der.idDer, documentsToSign, pageCount);
                    }
                }
            }
            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(_languageData.GetContent("signature_"), Business.Language.LanguageContentBL.Translate("der_title")),
                    ErrorMessage = error
                };

                return View("AllerSignature", signatureModel);
            }
            else
            {
                return Redirect(urlIHM_Dictao);
            }
        }
        public ActionResult Cancel(string id)
        {
            MissionDocument missionDoc = null;
            string error = string.Empty;
            string missionDocLabel = "Document";

            try
            {
                //Get mission document
                missionDoc = MissionDocumentBL.GetMissionDocumentById(new Guid(id));
                if (missionDoc == null)
                {
                    error = LanguageData.GetContent("selectionnez_doc_a_annuler");
                }
                else if (missionDoc.Status == RegulatoryDocumentModel.Status.Canceled.ToString()) // check if already canceled
                {
                    error = string.Format(LanguageData.GetContent("doc_dejà_annule"), missionDocLabel);
                }
                else
                {
                    missionDoc.Status = RegulatoryDocumentModel.Status.Canceled.ToString();
                    MissionDocumentBL.UpdateMissionDocument(missionDoc);

                    //update du customer status
                    CustomerProspect cust = CustomerProspectBL.GetCustomerProspectById(missionDoc.idCustomerProspect);
                    MissionDocument doc = MissionDocumentBL.GetLatestMissionDocumentByIdCustomer(cust.idCustomer, missionDoc.IsLetter);
                    if (missionDoc.IsLetter)
                    {
                        cust.LetterStatus = doc != null ? doc.Status : string.Empty;

                    }
                    else
                    {
                        cust.MissionStatus = doc != null ? doc.Status : string.Empty;
                    }
                    CustomerProspectBL.Update(cust);
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }

            SignatureModel signatureModel = new SignatureModel()
            {
                HeadTitle = LanguageData.GetContent("annulation_doc_mission"),
                H1Subtitle = LanguageData.GetContent("annulation"),
                H1Title = (missionDoc != null) ? ((missionDoc.IsLetter) ? LanguageData.GetContent("lettre_de_mission") : LanguageData.GetContent("rapport_de_mission")) : missionDocLabel
            };

            if (!string.IsNullOrEmpty(error))
            {
                signatureModel.ContentTitle = LanguageData.GetContent("erreur_annulation");
                signatureModel.ContentMessage = error;
            }
            else
            {
                signatureModel.ContentTitle = LanguageData.GetContent("annulation_ok");
                signatureModel.ContentMessage = LanguageData.GetContent("doc_bien_annulé");
            }

            return View("Message", signatureModel);
        }
        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);
            }

        }
        public ActionResult SignerDocumentMissionParClient(string id)
        {
            string error = string.Empty;
            string urlIHM_Dictao = string.Empty;
            int pageCount = 0;
            MissionDocument missionDoc = new MissionDocument();

            try
            {
                // Id latest mission document = id in parameter                
                if (string.IsNullOrEmpty(id))
                {
                    error = LanguageData.GetContent("selectionner_un_doc_a_signer");
                }
                else
                {
                    missionDoc = MissionDocumentBL.GetMissionDocumentById(new Guid(id));

                    if (missionDoc == null)
                    {
                        error = LanguageData.GetContent("selectionner_un_doc_a_signer");
                    }
                    else
                    {
                        // Sign the mission document (Customer signs secondly)
                        urlIHM_Dictao = SignatureDocumentBL_4_1.SignMissionDocumentByCustomer(missionDoc.idMissionDocument, string.Empty, pageCount);
                    }
                }
            }
            catch (Exception ex)
            {
                //Log
                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(LanguageData.GetContent("signature_"), missionDocLabel),
                    ErrorMessage = error
                };

                return View("AllerSignature", signatureModel);
            }
            else
            {
                return Redirect(urlIHM_Dictao);
            }
        }
        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);
            }

        }
Ejemplo n.º 14
0
        public ActionResult Cancel(string id)
        {
            DER der = null;
            string error = string.Empty;
            string derDocLabel = "Document";

            try
            {
                //Get mission document
                der = DerBL.GetDERById(new Guid(id));
                if (der == null)
                {
                    error = LanguageDataReg.GetContent("selectionnez_doc_a_annuler");
                }
                else if (der.Status == RegulatoryDocumentModel.Status.Canceled.ToString()) // check if already canceled
                {
                    error = string.Format(LanguageDataReg.GetContent("doc_dejà_annule"), derDocLabel);
                }
                else
                {
                    der.Status = RegulatoryDocumentModel.Status.Canceled.ToString();
                    DerBL.Update(der);

                    //update du customer status
                    CustomerProspect cust = CustomerProspectBL.GetCustomerProspectById(der.idCustomerProspect);
                    DER doc = DerBL.GetLatestDERByIdCustomer(cust.idCustomer);
                    cust.DerStatus = doc != null ? doc.Status : string.Empty;
                    CustomerProspectBL.Update(cust);
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }

            SignatureModel signatureModel = new SignatureModel()
            {
                HeadTitle = LanguageDataReg.GetContent("annulation_doc_mission"),
                H1Subtitle = LanguageDataReg.GetContent("annulation"),
                H1Title = (der != null) ? (LanguageData.GetContent("der")) : derDocLabel
            };

            if (!string.IsNullOrEmpty(error))
            {
                signatureModel.ContentTitle = LanguageDataReg.GetContent("erreur_annulation");
                signatureModel.ContentMessage = error;
            }
            else
            {
                signatureModel.ContentTitle = LanguageDataReg.GetContent("annulation_ok");
                signatureModel.ContentMessage = LanguageDataReg.GetContent("doc_bien_annulé");
            }

            return View("Message", signatureModel);
        }