Esempio n. 1
0
 /// <summary>
 /// Guarda un error en la bitacora
 /// </summary>
 /// <param name="errorInfo"></param>
 /// <returns></returns>
 internal int GuardarError(BitacoraErroresInfo errorInfo)
 {
     try
     {
         Logger.Info();
         Dictionary <string, object> parameters = AuxBitacoraErroresDAL.ObtenerParametrosGuardarError(errorInfo);
         int result = Create("BitacoraErrores_Guardar", parameters);
         return(result);
     }
     catch (SqlException ex)
     {
         Logger.Error(ex);
         throw new ExcepcionServicio(MethodBase.GetCurrentMethod(), ex);
     }
     catch (DataException ex)
     {
         Logger.Error(ex);
         throw new ExcepcionServicio(MethodBase.GetCurrentMethod(), ex);
     }
     catch (Exception ex)
     {
         Logger.Error(ex);
         throw new ExcepcionDesconocida(MethodBase.GetCurrentMethod(), ex);
     }
 }
 /// <summary>
 /// Obtiene los parametros necesarios para guardar en la bitacorra de errores
 /// </summary>
 /// <param name="errorInfo"></param>
 /// <returns></returns>
 internal static Dictionary <string, object> ObtenerParametrosGuardarError(BitacoraErroresInfo errorInfo)
 {
     try
     {
         Logger.Info();
         var parametros =
             new Dictionary <string, object>
         {
             { "@AccionesSiapID", (int)errorInfo.AccionesSiapID },
             { "@Mensaje", errorInfo.Mensaje },
             { "@UsuarioCreacionID", errorInfo.UsuarioCreacionID }
         };
         return(parametros);
     }
     catch (Exception ex)
     {
         Logger.Error(ex);
         throw new ExcepcionDesconocida(MethodBase.GetCurrentMethod(), ex);
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Metodo para generar la proyeccion de reimplante
        /// </summary>
        internal void GenerarProyeccionReimplante()
        {
            //ResultadoPolizaModel resultadoPolizaModel = null;
            try
            {
                Logger.Info();
                var organizacionBL   = new OrganizacionBL();
                var usuarioBL        = new UsuarioBL();
                var loteBL           = new LoteBL();
                var corralBl         = new CorralBL();
                var loteProyeccionBL = new LoteProyeccionBL();
                var loteReimplanteBL = new LoteReimplanteBL();
                //Se obtiene el usuario del proceso de alimentacion
                UsuarioInfo usuario = usuarioBL.ObtenerPorActiveDirectory(UsuarioProcesosEnum.ProyeccionReimplante.ToString());
                //Si se encontro el usuario
                if (usuario != null)
                {
                    //Consulta todas las organizaciones que esten activas
                    List <OrganizacionInfo> listaOrganizaciones =
                        organizacionBL.ObtenerTodos(EstatusEnum.Activo).ToList();
                    if (listaOrganizaciones != null && listaOrganizaciones.Any())
                    {
                        //Se obtienen las organizaciones que son de tipo ganadera
                        List <OrganizacionInfo> listaOrganizacionesFiltrada =
                            listaOrganizaciones.Where(
                                organizacion =>
                                organizacion.TipoOrganizacion.TipoOrganizacionID == (int)TipoOrganizacion.Ganadera)
                            .ToList();
                        if (listaOrganizacionesFiltrada.Count > 0)
                        {
                            foreach (OrganizacionInfo organizacion in listaOrganizacionesFiltrada)
                            {
                                try
                                {
                                    //using (var scope = new TransactionScope())
                                    //{

                                    //Obtener los corrales-Lotes que fueron reimplantados
                                    List <LoteCorralReimplanteInfo> listaCorrales = ObtenerCorralesReimplantados(organizacion);
                                    if (listaCorrales != null && listaCorrales.Any())
                                    {
                                        foreach (var loteCorral in listaCorrales)
                                        {
                                            //Obtener el tipo de ganado del lote
                                            TipoGanadoInfo tipoGanadoInfo = loteBL.ObtenerTipoGanadoLoteID(loteCorral.Lote);
                                            //Info del lote
                                            loteCorral.Lote = loteBL.ObtenerPorID(loteCorral.Lote.LoteID);

                                            //Obtener las proyecciones de los corrales origenes
                                            IList <LoteProyeccionInfo> listaProyeccionOrigen =
                                                loteProyeccionBL.ObtenerProyeccionDeLotesOrigen(loteCorral);

                                            if (listaProyeccionOrigen != null && listaProyeccionOrigen.Any())
                                            {
                                                var pesoReimplantePromedio = loteCorral.PesoReimplante /
                                                                             loteCorral.TotalCabezas;
                                                var pesoOrigenPromedio = loteCorral.PesoOrigen /
                                                                         loteCorral.TotalCabezas;

                                                //ObtenerDias engorda del corral
                                                var diasEngordaCorral =
                                                    corralBl.ObtenerDiasEngordaPorLote(loteCorral.Lote);
                                                //Calcular la Ganacia Diaria = (PesoReimplante-PesoOrigen)/DiasEngordaDelCorral
                                                decimal gananciaDiaria =
                                                    ((pesoReimplantePromedio - pesoOrigenPromedio) /
                                                     (decimal)diasEngordaCorral);
                                                gananciaDiaria = Math.Round(gananciaDiaria, 2);

                                                //Calculo diasEngorda: (PesoSalida-PesoPromedioReimplante)/GananciaDiaria
                                                var diasEngorda = (int)((tipoGanadoInfo.PesoSalida - pesoReimplantePromedio) /
                                                                        gananciaDiaria);

                                                var loteProyeccion =
                                                    loteProyeccionBL.ObtenerPorLote(loteCorral.Lote);

                                                LoteReimplanteInfo loteReimplante = null;
                                                var numeroReimplante = listaProyeccionOrigen.Max(p => p.NumeroReimplante);


                                                if (loteProyeccion == null)
                                                {
                                                    // Se crea el registro en lote proyeccion
                                                    loteProyeccion = new LoteProyeccionInfo
                                                    {
                                                        LoteID            = loteCorral.Lote.LoteID,      //OK
                                                        OrganizacionID    = organizacion.OrganizacionID, //OK
                                                        Frame             = 0,                           //OK
                                                        GananciaDiaria    = gananciaDiaria,              //OK
                                                        ConsumoBaseHumeda =
                                                            listaProyeccionOrigen.Select(p => p.ConsumoBaseHumeda).Sum() /
                                                            listaProyeccionOrigen.Count(),     // Promedio De los Corrales Origenes
                                                        Conversion =
                                                            listaProyeccionOrigen.Select(p => p.Conversion).Sum() /
                                                            listaProyeccionOrigen.Count(),     // Promedio De los Corrales Origenes
                                                        PesoMaduro =
                                                            listaProyeccionOrigen.Select(p => p.PesoMaduro).Sum() /
                                                            listaProyeccionOrigen.Count(),     // Promedio De los Corrales Origenes
                                                        PesoSacrificio =
                                                            listaProyeccionOrigen.Select(p => p.PesoSacrificio).Sum() /
                                                            listaProyeccionOrigen.Count(), // Promedio De los Corrales Origenes
                                                        DiasEngorda        = diasEngorda,  // OK
                                                        FechaEntradaZilmax =
                                                            ObtenerFechaZilmax(loteCorral.Lote, tipoGanadoInfo,
                                                                               diasEngorda),  //OK
                                                        UsuarioCreacionID = usuario.UsuarioID //OK
                                                    };

                                                    loteProyeccion.LoteProyeccionID =
                                                        loteProyeccionBL.Guardar(loteProyeccion);

                                                    // Fecha Sacrificio = fechaInicioLote + loteProyeccion.DiasEngorda
                                                    DateTime fechaProyectada =
                                                        loteCorral.Lote.FechaInicio.AddDays(loteProyeccion.DiasEngorda);

                                                    DateTime fechaHoy = DateTime.Now;
                                                    TimeSpan ts       = fechaProyectada - fechaHoy;
                                                    // GananciaDiaria*DiasQueFlatanParaReimplante) + PesoReimplante
                                                    int pesoProyectado =
                                                        (int)
                                                        ((loteProyeccion.GananciaDiaria * ts.Days) +
                                                         pesoReimplantePromedio);
                                                    //Se almacena el lote reimplante nuevo
                                                    GuardarNuevoReimplante(numeroReimplante, loteProyeccion, fechaProyectada, usuario, loteCorral, pesoProyectado, pesoOrigenPromedio);
                                                }
                                                else
                                                {
                                                    // Si ya existe el LoteProyeccion Obtener los lotesReimplantes
                                                    List <LoteReimplanteInfo> loteReimplanteInfo =
                                                        loteReimplanteBL.ObtenerListaPorLote(loteCorral.Lote);

                                                    DateTime fechaProyectada =
                                                        loteCorral.Lote.FechaInicio.AddDays(loteProyeccion.DiasEngorda);
                                                    DateTime fechaHoy = DateTime.Now;
                                                    TimeSpan ts       = fechaProyectada - fechaHoy;
                                                    // GananciaDiaria*DiasQueFlatanParaReimplante) + PesoReimplante
                                                    int pesoProyectado =
                                                        (int)
                                                        ((loteProyeccion.GananciaDiaria * ts.Days) +
                                                         pesoReimplantePromedio);

                                                    if (loteReimplanteInfo != null)
                                                    {
                                                        var numeroReimplanteMaximo = loteReimplanteInfo.Max(p => p.NumeroReimplante);
                                                        if (numeroReimplante == 2 && numeroReimplanteMaximo != 3)
                                                        {
                                                            // El peso Origen sea menor a 200 generar el lote reimplante
                                                            // y que los dias engorda sean mayores a 90 dias
                                                            if (pesoOrigenPromedio <= 200 && loteProyeccion.DiasEngorda >= 90)
                                                            {
                                                                // Se crea el registro en lote reimplante
                                                                loteReimplante = new LoteReimplanteInfo
                                                                {
                                                                    LoteProyeccionID  = loteProyeccion.LoteProyeccionID,
                                                                    NumeroReimplante  = 3,
                                                                    FechaProyectada   = fechaProyectada,
                                                                    PesoProyectado    = pesoProyectado,
                                                                    PesoReal          = 0,
                                                                    FechaReal         = new DateTime(1900, 01, 01),
                                                                    UsuarioCreacionID = usuario.UsuarioID
                                                                };
                                                                loteReimplanteBL.Guardar(loteReimplante);
                                                            }
                                                        }
                                                    }
                                                    else
                                                    {
                                                        //Se almacena el lote reimplante nuevo
                                                        GuardarNuevoReimplante(numeroReimplante, loteProyeccion, fechaProyectada, usuario, loteCorral, pesoProyectado, pesoOrigenPromedio);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    //    scope.Complete();
                                    //}
                                }
                                catch (Exception ex)
                                {
                                    var bitacoraBL = new BitacoraIncidenciasBL();
                                    var bitacora   = new BitacoraErroresInfo
                                    {
                                        AccionesSiapID    = AccionesSIAPEnum.SerProReim,
                                        Mensaje           = ex.Message,
                                        UsuarioCreacionID = usuario.UsuarioID
                                    };
                                    bitacoraBL.GuardarError(bitacora);
                                }
                            }
                        }
                    }
                }
            }
            catch (ExcepcionServicio ex)
            {
                Logger.Error(ex);
                throw;
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new ExcepcionDesconocida(MethodBase.GetCurrentMethod(), ex);
            }
        }