/// <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(LanguageContentBL.Translate("signature_select_customer"));
                }

                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(LanguageContentBL.Translate("signature_ec_signed"));
                }

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

                List<SignatureParameter> signatureParameters = new List<SignatureParameter>();

                SignatureParameter signatureParameter = new SignatureParameter()
                {
                    SignatureFieldLabel = DTPClient.USER_ADVISER,

                    SignaturePosition = new SignaturePosition()
                    {
                        Page = 8,
                        X = 28,
                        Y = 85,
                    }
                };

                signatureParameters.Add(signatureParameter);

                signatureParameter = new SignatureParameter()
                {
                    SignatureFieldLabel = DTPClient.USER_CUSTOMER,

                    SignaturePosition = new SignaturePosition()
                    {
                        Page = 8,
                        X = 114,
                        Y = 85,
                    }
                };

                signatureParameters.Add(signatureParameter);

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

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

                //DTP Data
                DTPData dtpData = new DTPData()
                {
                    TransactionId = confident.idDictao,
                    UserAccessId = confident.idDTPUserAccess,
                    BusinessId = businessId,
                    BackUrl = backUrl,
                    DocumentsToSign = documentsToSign,
                    SignatureFieldLabels = new List<string> { DTPClient.USER_ADVISER },
                    UserSpaceTitle = LanguageContentBL.Translate("signature_ec_sign_title"),
                    Language = SessionManager.GetCurrentLanguage().LanguageTypeName,
                };

                // Check if already signed by customer, if not, the adviser signs the doc first.
                if (!confident.SignedByCustomer)
                {
                    //Sign document
                    dtpData = SignatureDocumentBL.SignByFirstUser(userAdviser, dtpData);

                    //Save transaction Id to table
                    confident.idDictao = dtpData.TransactionId;
                    confident.idDTPUserAccess = dtpData.UserAccessId;
                }
                else
                {
                    dtpData = SignatureDocumentBL.SignBySecondUser(userAdviser, dtpData);

                    confident.idDTPUserAccess = dtpData.UserAccessId;
                }

                context.SaveChanges();

                return dtpData.IHM_Url;
            }
        }
        /// <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 ChooseAdvisor(string _idUser, string _customerid, string _action)
        {
            try
            {

                Guid _idUser2 = Guid.Empty;
                Guid.TryParse(_idUser, out _idUser2);

                Guid idcust = Guid.Empty;
                Guid.TryParse(_customerid, out idcust);

                Guid idReport = Guid.Empty;
                CustomerProspect cust = CustomerProspectBL.GetCustomerProspectById(idcust);

                if (cust != null)
                {
                    if (cust.ConfidentialityAgreement != null)
                    {
                        ConfidentialityAgreement confAg = ConfidentialityAgreementBL.GetById(cust.ConfidentialityAgreement.idConfidentialityAgreement);
                        confAg.idSignatoryUser = _idUser2;
                        ConfidentialityAgreementBL.Update(confAg);
                    }
                    else //Create new CA
                    {
                        var confident = new Upsilab.Data.Model.ConfidentialityAgreement()
                        {
                            idConfidentialityAgreement = Upsilab.Business.Utility.GuidHelper.GenerateGuid(),
                            idSignatoryUser = _idUser2,
                            Status = Upsilab.Business.Report.ReportBL.Status.InProgress.ToString(),
                            DateCreated = DateTime.Now,
                            SignedByCustomer = false,
                            SignedByAdviser = false,
                            Archived = false
                        };

                        ConfidentialityAgreementBL.Create(confident);

                        cust.idConfidentialityAgreement = confident.idConfidentialityAgreement;
                        CustomerProspectBL.Update(cust);

                    }
                }

                if (_action.CompareTo("Envoyer") == 0)
                {
                    return RedirectToAction("EnvoyerEngagementAuClient", "Signature", new { id = idcust });
                }
                else if (_action.CompareTo("Signer") == 0)
                {
                    return RedirectToAction("SignerEngagementParConseiller", "Signature", new { id = idcust });
                }
            }
            catch (Exception ex)
            {
                Log.AppendException(ex);
            }

            return RedirectToAction("Index");
        }