public void GenerarRendimientoOrdenesApi(DateTime FechaDesde, DateTime FechaHasta) { using (ASIS_PRODEntities entities = new ASIS_PRODEntities()) { List <CONTROL_AVANCE_API> ListadoControlAvanceApi = new List <CONTROL_AVANCE_API>(); clsDApiOrdenFabricacion = new clsDApiOrdenFabricacion(); var ordendesFabricacion = (from p in entities.PROYECCION_PROGRAMACION join d in entities.PROYECCION_PROGRAMACION_DETALLE on p.IdProyeccionProgramacion equals d.IdProyeccionProgramacion where p.FechaProduccion >= FechaDesde && p.FechaProduccion <= FechaHasta && p.EstadoRegistro == clsAtributos.EstadoRegistroActivo select new { d.OrdenFabricacion }).Distinct().ToList(); //recorrer las ordenes de fabricacion para actualizar los datos o agregar. foreach (var orden in ordendesFabricacion) { int x = int.Parse(orden.OrdenFabricacion.ToString()); var ListaLotes = clsDApiOrdenFabricacion.ConsultaLotesPorOFCompleto(x); foreach (var detalle in ListaLotes) { var modelControlAvanceApi = entities.CONTROL_AVANCE_API.FirstOrDefault(y => y.OrdenFabricacion == x && y.Lote == detalle.Lote); if (modelControlAvanceApi == null) { if (!ListadoControlAvanceApi.Any(lista => lista.OrdenFabricacion == x && lista.Lote == detalle.Lote)) { ListadoControlAvanceApi.Add(new CONTROL_AVANCE_API { OrdenFabricacion = x, Limpieza = detalle.Limpieza, Lote = detalle.Lote, Peso = detalle.Peso != null ? int.Parse(double.Parse(detalle.Peso).ToString()) : 0, Piezas = detalle.Piezas != null ? int.Parse(double.Parse(detalle.Piezas).ToString()) : 0, Talla = detalle.Talla, Promedio = detalle.Promedio != null ? decimal.Parse(detalle.Promedio) : 0, Especie = detalle.Especie, Producto = detalle.Producto, LomoReal = detalle.Lomos, MigaReal = detalle.Migas }); } } else { modelControlAvanceApi.Promedio = detalle.Promedio != null?decimal.Parse(detalle.Promedio) : 0; modelControlAvanceApi.Talla = detalle.Talla; modelControlAvanceApi.LomoReal = detalle.Lomos; modelControlAvanceApi.MigaReal = detalle.Migas; modelControlAvanceApi.Especie = detalle.Especie; modelControlAvanceApi.Lote = detalle.Lote; modelControlAvanceApi.Limpieza = detalle.Limpieza; modelControlAvanceApi.Peso = detalle.Peso != null?int.Parse(double.Parse(detalle.Peso).ToString()) : 0; modelControlAvanceApi.Piezas = detalle.Piezas != null?int.Parse(double.Parse(detalle.Piezas).ToString()) : 0; } } } if (ListadoControlAvanceApi.Any()) { entities.CONTROL_AVANCE_API.AddRange(ListadoControlAvanceApi.Distinct()); } entities.SaveChanges(); } }
public void GenerarAvanceOrdenesApi2(DateTime FechaDesde, DateTime?FechaHasta) { using (ASIS_PRODEntities entities = new ASIS_PRODEntities()) { if (FechaHasta == null) { FechaHasta = FechaDesde; } List <CONTROL_AVANCE_API> ListadoControlAvanceApi = new List <CONTROL_AVANCE_API>(); clsDApiOrdenFabricacion = new clsDApiOrdenFabricacion(); var ordendesFabricacion = entities.CONTROL_HUESO.Where(x => x.Fecha >= FechaDesde && x.Fecha <= FechaHasta && x.EstadoRegistro == clsAtributos.EstadoRegistroActivo).Select(x => x.OrdenFabricacion).Distinct(); //Consulta de servicio var detalleOrden = clsDApiOrdenFabricacion.ConsultaDatosLotePorRangoFecha(FechaDesde, FechaHasta ?? FechaDesde); //recorrer las ordenes de fabricacion para actualizar los datos o agregar. foreach (int x in ordendesFabricacion) { var ListaLotes = detalleOrden.Where(o => o.OrdenFabricacion == x).ToList(); if (ListaLotes == null || ListaLotes.Count == 0) { ListaLotes = clsDApiOrdenFabricacion.ConsultaLotesPorOFCompleto(x); } foreach (var detalle in ListaLotes) { var modelControlAvanceApi = entities.CONTROL_AVANCE_API.FirstOrDefault(y => y.OrdenFabricacion == x && y.Lote == detalle.Lote); if (modelControlAvanceApi == null) { if (!ListadoControlAvanceApi.Any(lista => lista.OrdenFabricacion == x && lista.Lote == detalle.Lote)) { ListadoControlAvanceApi.Add(new CONTROL_AVANCE_API { OrdenFabricacion = x, Limpieza = detalle.Limpieza, Lote = detalle.Lote, Peso = detalle.Peso != null ? int.Parse(double.Parse(detalle.Peso).ToString()) : 0, Piezas = detalle.Piezas != null ? int.Parse(double.Parse(detalle.Piezas).ToString()) : 0, Talla = detalle.Talla, Promedio = detalle.Promedio != null ? decimal.Parse(detalle.Promedio) : 0, Especie = detalle.Especie, Producto = detalle.Producto, LomoReal = detalle.Lomos, MigaReal = detalle.Migas }); } } else { modelControlAvanceApi.Promedio = detalle.Promedio != null?decimal.Parse(detalle.Promedio) : 0; modelControlAvanceApi.Talla = detalle.Talla; modelControlAvanceApi.LomoReal = detalle.Lomos; modelControlAvanceApi.MigaReal = detalle.Migas; modelControlAvanceApi.Especie = detalle.Especie; modelControlAvanceApi.Lote = detalle.Lote; modelControlAvanceApi.Limpieza = detalle.Limpieza; modelControlAvanceApi.Peso = detalle.Peso != null?int.Parse(double.Parse(detalle.Peso).ToString()) : 0; modelControlAvanceApi.Piezas = detalle.Piezas != null?int.Parse(double.Parse(detalle.Piezas).ToString()) : 0; } } } if (ListadoControlAvanceApi.Any()) { entities.CONTROL_AVANCE_API.AddRange(ListadoControlAvanceApi.Distinct()); } entities.SaveChanges(); } }