private ConventionVM GetConventionViewModel(Guid idCGP, string type = null)
        {
            var currentUser = SessionManager.GetUserSession();
            Signatory signatory = null;
            //
            //Create View model
            //
            var transactionType = DocumentSignatureTransactionBL.SignatureTransactionType.CONVENTION;
            var transaction = DocumentSignatureTransactionBL.GetTransactionByCGP(idCGP, transactionType.ToString());

            if (transaction == null && currentUser.IsSdgAdmin()) //CGP coudn't create the transaction
            {
                transaction = DocumentSignatureTransactionBL.CreateTransaction(currentUser.idUser, idCGP, transactionType);
            }

            var firmCGP = transaction.FirmInstitution;

            SessionManager.SetFirmInstutionCGPSession(firmCGP);
            SessionManager.Set<Guid?>(CURRENT_SIGNATURE_TRANSACTION_ID, (transaction != null) ? transaction.idSignatureTransaction : (Guid?)null);

            //Get Current signatory if WaitingForClientSignature or WaitingForUserSignature
            if (transaction.Status == DocumentBL.Status.WaitingForClientSignature.ToString()
                || transaction.Status == DocumentBL.Status.WaitingForUserSignature.ToString())
            {
                signatory = SignatoryBL.GetSignatory(transaction.idSignatureTransaction, currentUser.idUser);
            }

            //StatusLog
            var isConvention = true;
            var transactionStepTitle = string.Empty;
            var statusLog = DocumentSignatureTransactionBL.GetTransactionLog(transaction, isConvention, out transactionStepTitle);

            //
            var idDocumentModel = DocumentModelBL.GetIdDocumentModelByType(firmCGP.FirmInstitutionParent.GetValueOrDefault(), type);
            var document = transaction.Document.FirstOrDefault(x => x.DocumentType == type);

            var convention = new ConventionVM()
            {
                FirmInstitutionName = firmCGP.FirmInstitutionName,
                FirmInstitutionCGP = firmCGP,
                DocumentType = type,
                IdFirmInstitutionParent = firmCGP.FirmInstitutionParent.GetValueOrDefault(),
                IdTransaction = transaction.idSignatureTransaction,
                Status = transaction.Status,
                Transaction = transaction,
                StepTitle = transactionStepTitle,
                StatusLog = statusLog,
                CurrentSignatory = signatory
            };

            //Get current document if documentType != "DOC"
            if (!string.IsNullOrEmpty(convention.DocumentType) && convention.DocumentType != ElectronicSafeDocumentBL.DocumentType.DOC.ToString())
            {
                convention.CurrentDocument = transaction.Document.FirstOrDefault(doc => doc.DocumentType.Trim().ToLower() == convention.DocumentType.ToLower());

                var documents = transaction.Document.Where(x => x.DocumentType == ElectronicSafeDocumentBL.DocumentType.CCOURT.ToString()
                    || x.DocumentType == ElectronicSafeDocumentBL.DocumentType.CINT.ToString()
                    || x.DocumentType == ElectronicSafeDocumentBL.DocumentType.CPART.ToString()
                    || x.DocumentType == ElectronicSafeDocumentBL.DocumentType.CCOMP.ToString());

                convention.PopulateFieldsList(!currentUser.IsCgpClient(), documents.Select(x => x.idDocument));

            }
            else //documentType == "DOC"
            {
                if (!transaction.DocumentsInitialized)
                {
                    //Génération documents par défaut.
                    var firmSDG = SessionManager.GetFirmInstitutionSession();
                    DocumentBL.GenerateDefaultDocuments(transaction.idSignatureTransaction, firmSDG.idFirmInstitution);
                }

                //convention.Documents = DocumentBL.GetDocuments(transaction.idSignatureTransaction, ElectronicSafeDocumentBL.DocumentType.DOC);
                var tmpdocs = DocumentBL.GetDocuments(transaction.idSignatureTransaction, ElectronicSafeDocumentBL.DocumentType.DOC);
                convention.Documents = (tmpdocs != null && tmpdocs.Count > 0) ?
                    SetDocumentsDeletableOrNot(tmpdocs.GroupBy(x => x.IdDocumentCategory).Select(y => y.OrderByDescending(y1 => y1.DateCreated).First()).ToList()) :
                    new List<Document>();
            }
            //
            
            return convention;
        }
        public ActionResult ConventionManuelleCoffreFort(ConventionVM conventionVM)
        {
            bool bOk = true;
            var error = string.Empty;

            try
            {
                var firmCGP = SessionManager.GetFirmInstutionCGPSession();
                var firmSDG = SessionManager.GetFirmInstitutionSession();

                if (firmCGP == null)
                {
                    return RedirectToPageIfCGPNotDefined();
                }

                //Signature date ?
                var signatureDate = DateTime.Now;
                DateTime.TryParse(conventionVM.StrDateSignature, out signatureDate);

                if (signatureDate == DateTime.MinValue)
                {
                    signatureDate = DateTime.Now;
                }

                //Get document to send to coffre fort
                var signedDocuments = DocumentBL.GetDocuments(conventionVM.IdTransaction, DocumentBL.Status.ToCoffreFort);

                //Send to coffre
                var clientD3S = new Upsilab.Infrastructure.DICTAO.D3S.D3SClient();

                foreach (var document in signedDocuments)
                {
                    try
                    {
                        //1- Copy to directory
                        var sourceFilePath = FileManager.GetFileFromUploadDirectory(document.Location, document.Name);

                        var relativePath = string.Empty;
                        var fullPath = ElectronicSafeDocumentBL.BuildSignatureDocumentPath(firmCGP.FirmInstitutionName, firmCGP.idFirmInstitution, document.DocumentType.ToDocumentTypeEnum(), false, out relativePath); //CF
                        var destFileName = ElectronicSafeDocumentBL.BuildDocumentName(document.DocumentType, document.idDocument);
                        var destFilePath = Path.Combine(fullPath, destFileName);

                        if (!System.IO.File.Exists(destFilePath))
                        {
                            System.IO.File.Copy(sourceFilePath, destFilePath);

                            //2- Send to dictao
                            var dictaoId = ElectronicSafeDocumentBL.SendToDictaoCoffreFort(sourceFilePath.Replace(document.Name, string.Empty),
                                document.Name, clientD3S);

                            if (!string.IsNullOrEmpty(dictaoId))
                            {
                                var documentLabel = Upsilab.Business.Language.LanguageContentBL.Translate(document.DocumentType.ToLower());
                                ElectronicSafeDocumentBL.SaveDocumentToDB(destFileName, document.DocumentType, documentLabel, relativePath, firmCGP.idFirmInstitution, dictaoId, clientD3S, false, document.IdDocumentCategory, null, conventionVM.IdTransaction);
                            }

                        }

                    }
                    catch (Exception ex)
                    {
                        Log.AppendException(ex);
                    }
                }

                //Soustraire 1 credit
                var transaction = DocumentSignatureTransactionBL.GetTransactionById(conventionVM.IdTransaction);
                CreditOperationBL.SubstractCredit(firmSDG.idFirmInstitution, CreditOperationBL.OperationType.MISE_AU_COFFRE_MANUELLE_SDG_CONVENTION.ToString(), transaction.idUserCreated);

                //Update status to "Completed" 
                DocumentSignatureTransactionBL.UpdateTransactionStatus(conventionVM.IdTransaction, DocumentBL.Status.Completed, signatureDate);
            }
            catch (Exception ex)
            {
                bOk = false;
                error = ex.Message;
            }

            //Show view info
            var languageData = Upsilab.Business.Utility.PageLanguageHelper.GetLanguageContent("User", "Convention");
            var languageDataSignature = Upsilab.Business.Utility.PageLanguageHelper.GetLanguageContent("User", "SignatureView");

            MessageViewModel messageVM = new MessageViewModel()
            {
                HeadTitle = languageDataSignature.GetContent("signature_signature"),
                H1Title = languageData.GetContent("convention_title"),
                H1Subtitle = languageDataSignature.GetContent("signature_signature"),
                IsError = !bOk,
                ErrorMessage = error
            };

            if (bOk)
            {
                messageVM.TableTitle = languageDataSignature.GetContent("signature_success_title");
                messageVM.Content = languageDataSignature.GetContent("signature_success_message");
            }
            else
            {
                messageVM.TableTitle = languageDataSignature.GetContent("signature_error_title");
                messageVM.Content = languageDataSignature.GetContent("signature_error_message");
            }

            return View("Message", messageVM);
        }