public GenericResponse <CanalizacionesResponse> GetComboCanalizacion(string param)
        {
            try
            {
                IQueryable <Canalizaciones> query = null;

                Database context = new Database();

                if (param == null)
                {
                    param = "";
                }

                query = from b in context.Canalizaciones
                        where b.Descripcion.Contains(param)
                        select b;

                return(new GenericResponse <CanalizacionesResponse>()
                {
                    Code = 200, Result = new CanalizacionesResponse()
                    {
                        ListaCanalizaciones = CanalizacionesResponse.MapList(query.Take(100).ToList())
                    }
                });
            }
            catch (Exception ex)
            {
                var message      = ex.Message;
                var messageInner = ex.InnerException != null ? ex.InnerException.Message : "";

                DB.Database db2 = new DB.Database();
                db2.Log.Add(new DB.Log()
                {
                    Fecha = DateTime.Now, Ubicacion = Constants.LOG_UBICACION_DESTINATARIOS, Mensaje = message, Detalle = messageInner
                });

                db2.SaveChanges();

                return(new GenericResponse <CanalizacionesResponse>()
                {
                    Code = 200, Error = ex.Message
                });
            }
        }
        public GenericResponse <IngresosResponse> ObtenerIngresosFilterPaginado(int pageIndex, int pageSize, string nroSeguimiento, int tipoProducto, string sector, int proveedor, string destinatario, string fechaDesde, string fechaHasta, string canalizacion, string estado, string autorizado, string prefiltro)
        {
            GenericResponse <IngresosResponse> response = new GenericResponse <IngresosResponse>();

            response.Code = 200;

            try
            {
                Database db = new Database();
                response.Result = new IngresosResponse();


                if (string.IsNullOrEmpty(prefiltro) && String.IsNullOrEmpty(nroSeguimiento) && tipoProducto == 0 && String.IsNullOrEmpty(sector) && proveedor == 0 && String.IsNullOrEmpty(destinatario) && String.IsNullOrEmpty(fechaDesde) && String.IsNullOrEmpty(fechaHasta) && String.IsNullOrEmpty(estado) && String.IsNullOrEmpty(canalizacion) && String.IsNullOrEmpty(autorizado))
                {
                    var pedidosTodos = db.Pedidos.OrderByDescending(x => x.FechaImposicion).ToPagedList(pageIndex - 1, pageSize);
                    response.Result.ListaIngresos = IngresosResponse.MapList(pedidosTodos.ToList());
                    response.Result.totalCount    = pedidosTodos.TotalItemCount.ToString();
                }
                else
                {
                    if (nroSeguimiento == null)
                    {
                        nroSeguimiento = "";
                    }

                    var query = db.Pedidos.AsQueryable();

                    if (!string.IsNullOrEmpty(nroSeguimiento))
                    {
                        query = query.Where(w => w.NroSeguimiento.Contains(nroSeguimiento));
                    }

                    query = query.OrderByDescending(o => o.FechaImposicion);

                    //var query = from b in db.Pedidos
                    //            orderby b.FechaImposicion descending
                    //            where b.NroSeguimiento.Contains(nroSeguimiento) || string.IsNullOrEmpty(b.NroSeguimiento)
                    //            select b;

                    if (tipoProducto != 0)
                    {
                        query = from b in query where b.TipoDeProductos.Id == tipoProducto select b;
                    }

                    if (!String.IsNullOrEmpty(sector))
                    {
                        query = from b in query where b.Sectores.Id == sector select b;
                    }

                    if (proveedor != 0)
                    {
                        query = from b in query where b.Proveedores.Id == proveedor select b;
                    }

                    if (!String.IsNullOrEmpty(destinatario))
                    {
                        query = from b in query where b.Destinatarios.Id == destinatario select b;
                    }

                    if (!String.IsNullOrEmpty(canalizacion))
                    {
                        query = from b in query where b.IdCanalizacion == canalizacion select b;
                    }

                    if (!String.IsNullOrEmpty(autorizado))
                    {
                        query = from b in query where b.AutorizadoRetirar.Contains(autorizado) select b;
                    }

                    if (!String.IsNullOrEmpty(estado))
                    {
                        //int idEstado = int.Parse(estado);
                        List <int> idEstados = estado.Split(',').ToList().ConvertAll(int.Parse);
                        //var idEstados = new List<int>() { 1, 3 };
                        query = from b in query where idEstados.Contains(b.Estado) select b;
                    }

                    if (!string.IsNullOrWhiteSpace(fechaDesde))
                    {
                        CultureInfo esAR = new CultureInfo("es-AR");
                        DateTime    fechaSeleccioandaDesde = DateTime.ParseExact(fechaDesde + " 00:00", "dd/MM/yyyy HH:mm", esAR);
                        query = query.Where(w => w.FechaImposicion.Value > fechaSeleccioandaDesde);
                    }


                    if (!string.IsNullOrWhiteSpace(fechaHasta))
                    {
                        CultureInfo esAR = new CultureInfo("es-AR");
                        DateTime    fechaSeleccioandaHasta = DateTime.ParseExact(fechaHasta + " 23:59", "dd/MM/yyyy HH:mm", esAR);
                        query = query.Where(w => w.FechaImposicion.Value < fechaSeleccioandaHasta);
                    }


                    if (!string.IsNullOrEmpty(prefiltro))
                    {
                        switch (prefiltro)
                        {
                        case "ingresos":
                        {
                            var pedidosList = query.Where(x => x.Estado == Constants.Pedidos.ID_INGRESADO).OrderByDescending(x => x.FechaImposicion).ToPagedList(pageIndex - 1, pageSize);
                            response.Result.ListaIngresos = IngresosResponse.MapList(pedidosList.ToList());
                            response.Result.totalCount    = pedidosList.TotalItemCount.ToString();
                            response.Result.Prefiltros    = new Prefiltrado()
                            {
                                EstadoPrefiltradoIngresos = EstadosResponse.Map(db.Estados.Where(x => x.Id == 1).FirstOrDefault()), Seleccionado = "ingresos"
                            };
                            break;
                        }

                        case "egresos":
                        {
                            var pedidosList = query.Where(x => x.Estado == Constants.Pedidos.ID_RETIRADO).OrderByDescending(x => x.FechaImposicion).ToPagedList(pageIndex - 1, pageSize);
                            response.Result.ListaIngresos = IngresosResponse.MapList(pedidosList.ToList());
                            response.Result.totalCount    = pedidosList.TotalItemCount.ToString();
                            response.Result.Prefiltros    = new Prefiltrado()
                            {
                                EstadoPrefiltradoEgresos = EstadosResponse.Map(db.Estados.Where(x => x.Id == 4).FirstOrDefault()), Seleccionado = "egresos"
                            };
                            break;
                        }

                        case "correspondencia":
                        {
                            var pedidosList = query.Where(x => x.Canalizaciones.Id == "1").OrderByDescending(x => x.FechaImposicion).ToPagedList(pageIndex - 1, pageSize);
                            response.Result.ListaIngresos = IngresosResponse.MapList(pedidosList.ToList());
                            response.Result.totalCount    = pedidosList.TotalItemCount.ToString();
                            response.Result.Prefiltros    = new Prefiltrado()
                            {
                                CanalizacionesPrefiltradoCorrespondencia = CanalizacionesResponse.Map(db.Canalizaciones.Where(x => x.Id == "1").FirstOrDefault()), Seleccionado = "correspondencia"
                            };
                            break;
                        }

                        case "paquetes":
                        {
                            var pedidosList = query.Where(x => x.IdTipoDeProducto == 2).OrderByDescending(x => x.FechaImposicion).ToPagedList(pageIndex - 1, pageSize);
                            response.Result.ListaIngresos = IngresosResponse.MapList(pedidosList.ToList());
                            response.Result.totalCount    = pedidosList.TotalItemCount.ToString();
                            response.Result.Prefiltros    = new Prefiltrado()
                            {
                                TipoProductoPrefiltradoPaquetes = TiposProductoResponse.Map(db.TipoDeProductos.Where(x => x.Id == 2).FirstOrDefault()), Seleccionado = "paquetes"
                            };
                            break;
                        }
                        }
                        return(response);
                    }


                    var pedidos = query.ToPagedList(pageIndex - 1, pageSize);
                    response.Result.ListaIngresos = IngresosResponse.MapList(pedidos.ToList());
                    response.Result.totalCount    = pedidos.TotalItemCount.ToString();
                }

                return(response);
            }
            catch (Exception ex)
            {
                response.Code  = 500;
                response.Error = "Error al traer los pedidos";

                var message      = ex.Message;
                var messageInner = ex.InnerException != null ? ex.InnerException.Message : "";

                Database db2 = new Database();
                db2.Log.Add(new Log()
                {
                    Fecha = DateTime.Now, Ubicacion = Constants.LOG_UBICACION_RECEPCION, Mensaje = message, Detalle = messageInner
                });

                db2.SaveChanges();
            }

            return(response);
        }
        public GenericResponse <IngresosResponse> ObtenerIngresosPrefiltrado(string prefiltro, int pageIndex, int pageSize)
        {
            GenericResponse <IngresosResponse> response = new GenericResponse <IngresosResponse>();

            response.Code = 200;

            try
            {
                Database db = new Database();
                response.Result = new IngresosResponse();

                switch (prefiltro)
                {
                case "ingresos":
                {
                    var pedidos = db.Pedidos.Where(x => x.Estado == Constants.Pedidos.ID_INGRESADO).OrderByDescending(x => x.FechaImposicion).ToPagedList(pageIndex - 1, pageSize);
                    response.Result.ListaIngresos = IngresosResponse.MapList(pedidos.ToList());
                    response.Result.totalCount    = pedidos.TotalItemCount.ToString();
                    response.Result.Prefiltros    = new Prefiltrado()
                    {
                        EstadoPrefiltradoIngresos = EstadosResponse.Map(db.Estados.Where(x => x.Id == 1).FirstOrDefault()), Seleccionado = "ingresos"
                    };
                    break;
                }

                case "egresos":
                {
                    var pedidos = db.Pedidos.Where(x => x.Estado == Constants.Pedidos.ID_RETIRADO).OrderByDescending(x => x.FechaImposicion).ToPagedList(pageIndex - 1, pageSize);
                    response.Result.ListaIngresos = IngresosResponse.MapList(pedidos.ToList());
                    response.Result.totalCount    = pedidos.TotalItemCount.ToString();
                    response.Result.Prefiltros    = new Prefiltrado()
                    {
                        EstadoPrefiltradoEgresos = EstadosResponse.Map(db.Estados.Where(x => x.Id == 4).FirstOrDefault()), Seleccionado = "egresos"
                    };
                    break;
                }

                case "correspondencia":
                {
                    var pedidos = db.Pedidos.Where(x => x.Canalizaciones.Id == "1").OrderByDescending(x => x.FechaImposicion).ToPagedList(pageIndex - 1, pageSize);
                    response.Result.ListaIngresos = IngresosResponse.MapList(pedidos.ToList());
                    response.Result.totalCount    = pedidos.TotalItemCount.ToString();
                    response.Result.Prefiltros    = new Prefiltrado()
                    {
                        CanalizacionesPrefiltradoCorrespondencia = CanalizacionesResponse.Map(db.Canalizaciones.Where(x => x.Id == "1").FirstOrDefault()), Seleccionado = "correspondencia"
                    };
                    break;
                }

                case "paquetes":
                {
                    var pedidos = db.Pedidos.Where(x => x.IdTipoDeProducto == 2).OrderByDescending(x => x.FechaImposicion).ToPagedList(pageIndex - 1, pageSize);
                    response.Result.ListaIngresos = IngresosResponse.MapList(pedidos.ToList());
                    response.Result.totalCount    = pedidos.TotalItemCount.ToString();
                    response.Result.Prefiltros    = new Prefiltrado()
                    {
                        TipoProductoPrefiltradoPaquetes = TiposProductoResponse.Map(db.TipoDeProductos.Where(x => x.Id == 2).FirstOrDefault()), Seleccionado = "paquetes"
                    };
                    break;
                }
                }

                return(response);
            }
            catch (Exception ex)
            {
                response.Code  = 500;
                response.Error = "Error al traer los ingresos";

                var message      = ex.Message;
                var messageInner = ex.InnerException != null ? ex.InnerException.Message : "";

                Database db2 = new Database();
                db2.Log.Add(new Log()
                {
                    Fecha = DateTime.Now, Ubicacion = Constants.LOG_UBICACION_RECEPCION, Mensaje = message, Detalle = messageInner
                });

                db2.SaveChanges();
            }

            return(response);
        }