Ejemplo n.º 1
0
        public byte[] GerarHistorico(int idTurmaAluno)
        {
            var turmaAluno = turmaAlunoRepository.GetById(idTurmaAluno);
            var aluno      = alunoRepository.GetById(turmaAluno.AlunoId);

            turmaAluno = aluno.TurmasAluno.First(x => x.TurmaId == turmaAluno.TurmaId);
            Document doc = new Document(Path.Combine(ApplicationConstants.PastaDocumentos, turmaAluno.Turma.CursoId.ToString(), ApplicationConstants.ArquivoHistorico));

            try
            {
                CultureInfo        culture     = new CultureInfo("pt-BR");
                DateTimeFormatInfo dtfi        = culture.DateTimeFormat;
                string             mes         = culture.TextInfo.ToTitleCase(dtfi.GetMonthName(DateTime.Now.Month)).ToLower();
                string             dataGeracao = DateTime.Now.Day.ToString("00") + " de " + mes + " de " + DateTime.Now.Year;

                Dictionary <string, string> stringsToReplace = new Dictionary <string, string>()
                {
                    { ApplicationConstants.NomeReplace, aluno.Nome },
                    { ApplicationConstants.DataNascimentoReplace, aluno.DataNascimento.ToString(ApplicationConstants.DateFormat) },
                    { ApplicationConstants.RGReplace, aluno.RG },
                    { ApplicationConstants.OrgaoEmissorReplace, aluno.OrgaoEmissor },
                    { ApplicationConstants.CPFReplace, aluno.CPF },
                    { ApplicationConstants.DataInicioReplace, MaxDate(aluno.DataMatricula, turmaAluno.Turma.DataInicio).ToString(ApplicationConstants.DateFormat) },
                    { ApplicationConstants.DataTerminoReplace, turmaAluno.DataConclusao.HasValue ? turmaAluno.DataConclusao.Value.ToString(ApplicationConstants.DateFormat) : "" },
                    { ApplicationConstants.DataGeracaoReplace, dataGeracao }
                };
                var disciplinas = disciplinaRepository.ListarDisciplinasDeUmCurso(turmaAluno.Turma.CursoId);
                var notas       = notaAlunoRepository.ListarNotasDeUmAluno(aluno.Id);
                disciplinas.ToList().ForEach(d =>
                {
                    var notaReplace = ApplicationConstants.NotaReplace;
                    notaReplace     = notaReplace.Replace("%d", d.Id.ToString());
                    var nota        = notas.FirstOrDefault(x => x.DisciplinaId == d.Id);
                    var valor       = string.Empty;
                    if (nota != null)
                    {
                        valor = nota.ValorNota.ToString("0.0");
                    }
                    stringsToReplace.Add(notaReplace, valor);
                });
                ReplaceString(doc, stringsToReplace);
            }
            catch (Exception ex)
            {
                log.Error("Erro ao gerar histórico.", ex);
                throw new BusinessException("Erro ao gerar histórico.");
            }

            MemoryStream stream = new MemoryStream();

            doc.Save(stream, Aspose.Words.SaveFormat.Docx);
            stream.Seek(0, SeekOrigin.Begin);
            return(stream.ToArray());
        }