Exemplo n.º 1
0
        public async Task <FechamentoDto> ObterPorTipoCalendarioDreEUe(long tipoCalendarioId, Dre dre, Ue ue)
        {
            var usuarioLogado = await servicoUsuario.ObterUsuarioLogado();

            var dreIdFiltro = !(dre == null) || usuarioLogado.EhPerfilUE() ? dre?.Id : null;

            var fechamentoSME = repositorioPeriodoFechamento.ObterPorFiltros(tipoCalendarioId, null, null, null);

            var fechamentoSMEDre = repositorioPeriodoFechamento.ObterPorFiltros(tipoCalendarioId, dreIdFiltro, null, null);

            if (fechamentoSMEDre == null)
            {
                LimparCamposNaoUtilizadosRegistroPai(fechamentoSME);
                fechamentoSMEDre = fechamentoSME;

                if (fechamentoSMEDre == null)
                {
                    fechamentoSMEDre = new PeriodoFechamento(null, null);

                    var tipoCalendario = await repositorioTipoCalendario.ObterPorIdAsync(tipoCalendarioId);

                    if (tipoCalendario == null)
                    {
                        throw new NegocioException("Tipo de calendário não encontrado.");
                    }

                    var periodoEscolar = await repositorioPeriodoEscolar.ObterPorTipoCalendario(tipoCalendarioId);

                    if (periodoEscolar == null || !periodoEscolar.Any())
                    {
                        throw new NegocioException("Período escolar não encontrado.");
                    }

                    foreach (var periodo in periodoEscolar)
                    {
                        periodo.AdicionarTipoCalendario(tipoCalendario);
                        fechamentoSMEDre.AdicionarFechamentoBimestre(new PeriodoFechamentoBimestre(fechamentoSMEDre.Id, periodo, periodo.PeriodoInicio, periodo.PeriodoFim));
                    }
                }
            }

            var fechamentoDreUe = repositorioPeriodoFechamento.ObterPorFiltros(tipoCalendarioId, dre?.Id, ue?.Id, null);

            if (fechamentoDreUe == null)
            {
                LimparCamposNaoUtilizadosRegistroPai(fechamentoSMEDre);
                fechamentoDreUe     = fechamentoSMEDre;
                fechamentoDreUe.Dre = dre;
                fechamentoDreUe.Ue  = ue;
            }

            var fechamentoDto    = MapearParaDto(fechamentoDreUe);
            var fechamentoSMEDto = MapearParaDto(fechamentoSME);

            foreach (var bimestreSME in fechamentoSMEDre.FechamentosBimestre)
            {
                FechamentoBimestreDto bimestreFechamentoSME = null;

                if (fechamentoSMEDto != null)
                {
                    bimestreFechamentoSME = fechamentoSMEDto.FechamentosBimestres.FirstOrDefault(c => c.Bimestre == bimestreSME.PeriodoEscolar.Bimestre);
                }

                var bimestreDreUe = fechamentoDto.FechamentosBimestres.FirstOrDefault(c => c.Bimestre == bimestreSME.PeriodoEscolar.Bimestre);
                if (bimestreDreUe != null)
                {
                    bimestreDreUe.PeriodoEscolar = bimestreSME.PeriodoEscolar;
                    if (fechamentoSMEDre.Id > 0 && !(dre == null) || !(ue == null))
                    {
                        if (bimestreFechamentoSME != null)
                        {
                            bimestreDreUe.InicioMinimo =
                                bimestreFechamentoSME.InicioDoFechamento < bimestreSME.InicioDoFechamento ?
                                bimestreFechamentoSME.InicioDoFechamento.Value : bimestreSME.InicioDoFechamento;

                            bimestreDreUe.FinalMaximo =
                                bimestreFechamentoSME.FinalDoFechamento > bimestreSME.FinalDoFechamento ?
                                bimestreFechamentoSME.FinalDoFechamento.Value : bimestreSME.FinalDoFechamento;
                        }
                        else
                        {
                            bimestreDreUe.InicioMinimo = bimestreSME.InicioDoFechamento;
                            bimestreDreUe.FinalMaximo  = bimestreSME.FinalDoFechamento;
                        }
                    }
                    else
                    {
                        bimestreDreUe.InicioMinimo = new DateTime(bimestreSME.InicioDoFechamento.Year, 01, 01);
                        bimestreDreUe.FinalMaximo  = new DateTime(bimestreSME.InicioDoFechamento.Year, 12, 31);
                    }
                }
            }
            return(fechamentoDto);
        }
Exemplo n.º 2
0
        public async Task <FechamentoDto> ObterPorTipoCalendarioDreEUe(long tipoCalendarioId, string dreId, string ueId)
        {
            var tipoCalendario = repositorioTipoCalendario.ObterPorId(tipoCalendarioId);

            if (tipoCalendario == null)
            {
                throw new NegocioException("Tipo de calendário não encontrado.");
            }
            var periodoEscolar = repositorioPeriodoEscolar.ObterPorTipoCalendario(tipoCalendarioId);

            if (periodoEscolar == null || !periodoEscolar.Any())
            {
                throw new NegocioException("Período escolar não encontrado para o tipo de calendário informado.");
            }

            var usuarioLogado = await servicoUsuario.ObterUsuarioLogado();

            var(dre, ue) = ObterDreEUe(dreId, ueId);

            var dreIdFiltro = !string.IsNullOrWhiteSpace(ueId) || usuarioLogado.EhPerfilUE() ? dre?.Id : null;

            var fechamentoSMEDre    = repositorioFechamento.ObterPorTipoCalendarioDreEUE(tipoCalendarioId, dreIdFiltro, null);
            var ehRegistroExistente = (dreId == null && fechamentoSMEDre != null);

            if (fechamentoSMEDre == null)
            {
                fechamentoSMEDre    = repositorioFechamento.ObterPorTipoCalendarioDreEUE(tipoCalendarioId, null, null);
                ehRegistroExistente = fechamentoSMEDre != null;
                if (fechamentoSMEDre == null)
                {
                    if (!usuarioLogado.EhPerfilSME())
                    {
                        throw new NegocioException("Fechamento da SME/Dre não encontrado para este tipo de calendário.");
                    }
                    else
                    {
                        fechamentoSMEDre = new PeriodoFechamento(null, null);

                        foreach (var periodo in periodoEscolar)
                        {
                            periodo.AdicionarTipoCalendario(tipoCalendario);
                            fechamentoSMEDre.AdicionarFechamentoBimestre(new PeriodoFechamentoBimestre(fechamentoSMEDre.Id, periodo, null, null));
                        }
                    }
                }
            }

            var fechamentoDreUe = repositorioFechamento.ObterPorTipoCalendarioDreEUE(tipoCalendarioId, dre?.Id, ue?.Id);

            if (fechamentoDreUe == null)
            {
                ehRegistroExistente = false;
                fechamentoDreUe     = fechamentoSMEDre;
                fechamentoDreUe.Dre = dre;
                fechamentoDreUe.Ue  = ue;
            }
            else
            {
                ehRegistroExistente = true;
            }

            var fechamentoDto = MapearParaDto(fechamentoDreUe);

            fechamentoDto.EhRegistroExistente = ehRegistroExistente;

            foreach (var bimestreSME in fechamentoSMEDre.FechamentosBimestre)
            {
                var bimestreDreUe = fechamentoDto.FechamentosBimestres.FirstOrDefault(c => c.Bimestre == bimestreSME.PeriodoEscolar.Bimestre);
                if (bimestreDreUe != null)
                {
                    if (fechamentoSMEDre.Id > 0 && (!string.IsNullOrWhiteSpace(dreId) || !string.IsNullOrWhiteSpace(ueId)))
                    {
                        bimestreDreUe.InicioMinimo = bimestreSME.InicioDoFechamento;
                        bimestreDreUe.FinalMaximo  = bimestreSME.FinalDoFechamento;
                    }
                    else
                    {
                        bimestreDreUe.InicioMinimo = new DateTime(DateTime.Now.Year, 01, 01);
                        bimestreDreUe.FinalMaximo  = new DateTime(DateTime.Now.Year, 12, 31);
                    }
                }
            }
            return(fechamentoDto);
        }