private void GuardarProyeccion(CheckListCorralInfo checkListCorral, CheckListCorralDAL checkListCorralDAL) { var loteProyeccion = checkListCorralDAL.GenerarProyeccion(checkListCorral); var loteProyeccionBL = new LoteProyeccionPL(); var listaReimplantesGuardar = new List <LoteReimplanteInfo>(); if (loteProyeccion == null) { return; } loteProyeccion.UsuarioCreacionID = checkListCorral.UsuarioCreacionID; loteProyeccion.RequiereRevision = checkListCorral.RequiereRevision; var loteProyeccionID = loteProyeccionBL.Guardar(loteProyeccion); var primerReimplante = new LoteReimplanteInfo { LoteProyeccionID = loteProyeccionID, NumeroReimplante = loteProyeccion.Proyeccion.PrimerReimplante, FechaProyectada = loteProyeccion.Proyeccion.FechaProyectada1, PesoProyectado = loteProyeccion.Proyeccion.PesoProyectado1, UsuarioCreacionID = checkListCorral.UsuarioCreacionID }; if (primerReimplante.NumeroReimplante != 0 && primerReimplante.FechaProyectada != DateTime.MinValue && primerReimplante.PesoProyectado != 0) { listaReimplantesGuardar.Add(primerReimplante); } var segundoReimplante = new LoteReimplanteInfo { LoteProyeccionID = loteProyeccionID, NumeroReimplante = loteProyeccion.Proyeccion.SegundoReimplante, FechaProyectada = loteProyeccion.Proyeccion.FechaProyectada2, PesoProyectado = loteProyeccion.Proyeccion.PesoProyectado2, UsuarioCreacionID = checkListCorral.UsuarioCreacionID }; if (segundoReimplante.NumeroReimplante != 0 && segundoReimplante.FechaProyectada != DateTime.MinValue && segundoReimplante.PesoProyectado != 0) { listaReimplantesGuardar.Add(segundoReimplante); } var loteReimplanteBL = new LoteReimplanteBL(); if (listaReimplantesGuardar.Count > 0) { loteReimplanteBL.GuardarListaReimplantes(listaReimplantesGuardar); } checkListCorral.FechaSacrificio = loteProyeccion.FechaSacrificio; checkListCorral.Fecha1Reimplante = loteProyeccion.Proyeccion.FechaProyectada1; checkListCorral.Fecha2Reimplante = loteProyeccion.Proyeccion.FechaProyectada2; checkListCorral.DiasEngorda = loteProyeccion.DiasEngorda; }
/// <summary> /// Metodo para generar el nuevo LoteReimplante /// </summary> /// <param name="numeroReimplante"></param> /// <param name="loteProyeccion"></param> /// <param name="fechaProyectada"></param> /// <param name="usuario"></param> /// <param name="loteCorral"></param> /// <param name="pesoProyectado"></param> private void GuardarNuevoReimplante(int numeroReimplante, LoteProyeccionInfo loteProyeccion, DateTime fechaProyectada, UsuarioInfo usuario, LoteCorralReimplanteInfo loteCorral, int pesoProyectado, int pesoOrigenPromedio) { var loteReimplanteBL = new LoteReimplanteBL(); var loteReimplante = new LoteReimplanteInfo(); if (numeroReimplante == 1) { loteReimplante = new LoteReimplanteInfo { LoteProyeccionID = loteProyeccion.LoteProyeccionID, NumeroReimplante = 2, FechaProyectada = fechaProyectada, PesoProyectado = pesoProyectado, PesoReal = 0, FechaReal = new DateTime(1900, 01, 01), UsuarioCreacionID = usuario.UsuarioID }; loteReimplanteBL.Guardar(loteReimplante); } else if (numeroReimplante == 2) { // 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) { 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); } } }
/// <summary> /// Metodo para guardar la configuracion de reimplantes /// </summary> /// <param name="configuracionReimplante">Representa el Contexto de lo que se va a guardar</param> public int GuardarConfiguracionReimplante(ConfiguracionReimplanteModel configuracionReimplante) { try { Logger.Info(); var loteReimplanteBL = new LoteReimplanteBL(); var loteProyeccionDAL = new LoteProyeccionDAL(); var loteDAL = new LoteDAL(); int result = configuracionReimplante.LoteProyeccion.LoteProyeccionID; var opciones = new TransactionOptions(); var filtroDisponilidadInfo = new FiltroDisponilidadInfo { UsuarioId = configuracionReimplante.LoteProyeccion.UsuarioCreacionID, ListaLoteDisponibilidad = new List <DisponibilidadLoteInfo> { new DisponibilidadLoteInfo { LoteId = configuracionReimplante.Lote.LoteID, FechaDisponibilidad = configuracionReimplante.FechaDisponible, DisponibilidadManual = false } } }; opciones.IsolationLevel = IsolationLevel.ReadUncommitted; opciones.Timeout = new TimeSpan(2, 0, 0); using (var scope = new TransactionScope(TransactionScopeOption.Required, opciones)) { if (configuracionReimplante.LoteProyeccion.LoteProyeccionID == 0) { configuracionReimplante.LoteProyeccion.OrganizacionID = configuracionReimplante.Corral.Organizacion.OrganizacionID; configuracionReimplante.LoteProyeccion.LoteID = configuracionReimplante.Lote.LoteID; result = loteProyeccionDAL.Crear(configuracionReimplante.LoteProyeccion); configuracionReimplante.LoteProyeccion.LoteProyeccionID = result; loteProyeccionDAL.CrearBitacora(configuracionReimplante.LoteProyeccion); } else { if (configuracionReimplante.LoteProyeccion.AplicaBitacora) { loteProyeccionDAL.Actualizar(configuracionReimplante.LoteProyeccion); loteProyeccionDAL.CrearBitacora(configuracionReimplante.LoteProyeccion); } } if (configuracionReimplante.LoteProyeccion.ListaReimplantes != null && configuracionReimplante.LoteProyeccion.ListaReimplantes.Any()) { configuracionReimplante.LoteProyeccion.ListaReimplantes.ToList().ForEach( reimp => reimp.LoteProyeccionID = result); loteReimplanteBL.GuardarListaReimplantes( configuracionReimplante.LoteProyeccion.ListaReimplantes.ToList()); } loteDAL.ActualizarLoteDisponibilidad(filtroDisponilidadInfo); if (configuracionReimplante.Lote.AplicaCierreLote) { loteDAL.ActualizaFechaCierre(configuracionReimplante.Lote.LoteID, configuracionReimplante.UsuarioCreacionID); } scope.Complete(); } return(result); } catch (ExcepcionGenerica) { throw; } catch (Exception ex) { Logger.Error(ex); throw new ExcepcionDesconocida(MethodBase.GetCurrentMethod(), ex); } }
/// <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); } }