public ActionResult Index(int id, int?page, Transacao005 Proprietario, string placa, string chassi, string marcamodelo, string estadolotes, string numero_formulario_grv, bool ExportarExcel = false, bool LocalizadosFisicamente = false, string Ordenacao = "")
        {
            int TamanhoPagina = 10;
            int NumeroPagina  = page ?? 1;

            try
            {
                if (Proprietario.Id_lote > 0)
                {
                    RepositorioGlobal.Proprietario.Alterar(Proprietario);
                }

                List <Lote> filtro = new List <Lote>();
                filtro = RepositorioGlobal.Lote.SelecionarTudo(id).ToList();

                ViewBag.id_leilao = filtro.FirstOrDefault().id_leilao;

                if (filtro.Count > 0)
                {
                    ViewBag.NomeLeilao = filtro.FirstOrDefault().nome_leilao;
                }

                #region WHERE
                if (!string.IsNullOrEmpty(placa))
                {
                    filtro = filtro.Where(p => p.placa.Contains(placa.ToUpper())).ToList();
                }

                if (!string.IsNullOrEmpty(chassi))
                {
                    filtro = filtro.Where(p => p.chassi.Contains(chassi)).ToList();
                }

                if (!string.IsNullOrEmpty(marcamodelo))
                {
                    filtro = filtro.Where(p => p.marca_modelo.Contains(marcamodelo.ToUpper())).ToList();
                }

                if (LocalizadosFisicamente)
                {
                    filtro = filtro.Where(p => p.situacao_lote == "Sim").ToList();
                }

                if (!string.IsNullOrEmpty(estadolotes) && estadolotes != "X")
                {
                    if (estadolotes == "D")
                    {
                        filtro = filtro.Where(l => l.placa != (string.IsNullOrWhiteSpace(l._placa) ? l.placa : l._placa) ||
                                              l.chassi != (string.IsNullOrWhiteSpace(l._chassi) ? l.chassi : l._chassi) ||
                                              l.marca_modelo != (string.IsNullOrWhiteSpace(l._marca_modelo) ? l.marca_modelo : l._marca_modelo) ||
                                              l.tipo_veiculo != (string.IsNullOrWhiteSpace(l._tipo_veiculo) ? l.tipo_veiculo : l._tipo_veiculo) ||
                                              l.cor != (string.IsNullOrWhiteSpace(l._cor) ? l.cor : l._cor)).ToList();
                    }

                    if (estadolotes == "C")
                    {
                        filtro = filtro.Where(l => l.placa == (string.IsNullOrWhiteSpace(l._placa) ? l.placa : l._placa) &&
                                              l.chassi == (string.IsNullOrWhiteSpace(l._chassi) ? l.chassi : l._chassi) &&
                                              l.marca_modelo == (string.IsNullOrWhiteSpace(l._marca_modelo) ? l.marca_modelo : l._marca_modelo) &&
                                              l.tipo_veiculo == (string.IsNullOrWhiteSpace(l._tipo_veiculo) ? l.tipo_veiculo : l._tipo_veiculo) &&
                                              l.cor == (string.IsNullOrWhiteSpace(l._cor) ? l.cor : l._cor)).ToList();
                    }
                }

                if (!string.IsNullOrEmpty(numero_formulario_grv))
                {
                    filtro = filtro.Where(p => p.numero_formulario_grv.Contains(numero_formulario_grv.ToUpper())).ToList();
                }
                #endregion

                #region ORDER BY
                if (!string.IsNullOrEmpty(Ordenacao))
                {
                    List <Lote> itemList = new List <Lote>();

                    switch (Ordenacao)
                    {
                    case "NumeroLote":

                        itemList = (from t in filtro
                                    orderby t.numero_lote ascending
                                    select t).ToList();

                        filtro = itemList;

                        break;


                    case "Processo":

                        itemList = (from t in filtro
                                    orderby t.numero_formulario_grv ascending
                                    select t).ToList();

                        filtro = itemList;

                        break;

                    case "Placa":

                        itemList = (from t in filtro
                                    orderby t.placa ascending
                                    select t).ToList();

                        filtro = itemList;

                        break;

                    case "Chassi":

                        itemList = (from t in filtro
                                    orderby t.chassi ascending
                                    select t).ToList();

                        filtro = itemList;

                        break;

                    case "MarcaModelo":

                        itemList = (from t in filtro
                                    orderby t.marca_modelo ascending
                                    select t).ToList();

                        filtro = itemList;

                        break;

                    case "Cor":

                        itemList = (from t in filtro
                                    orderby t.cor ascending
                                    select t).ToList();

                        filtro = itemList;

                        break;

                    case "TipoVeiculo":

                        itemList = (from t in filtro
                                    orderby t.tipo_veiculo ascending
                                    select t).ToList();

                        filtro = itemList;

                        break;

                    default:
                        break;
                    }
                }
                #endregion

                ViewBag.placa                  = placa;
                ViewBag.chassi                 = chassi;
                ViewBag.marcamodelo            = marcamodelo;
                ViewBag.EstadoLotes            = estadolotes;
                ViewBag.LocalizadosFisicamente = LocalizadosFisicamente;
                ViewBag.numero_formulario_grv  = numero_formulario_grv;
                ViewBag.qtdregistros           = filtro.Count;

                if (filtro.Count <= 0)
                {
                    ViewBag.Erro = "LOTE NÃO ENCONTRADO OU NÃO PERTENCE AO LEILÃO.";
                }

                if (ExportarExcel == true)
                {
                    ExcelXLSX(filtro);
                }

                return(View(filtro.ToPagedList(NumeroPagina, TamanhoPagina)));
            }
            catch (Exception e)
            {
                throw;
            }
        }
        private List <ProprietarioSPE> RetornaProprietariosValidos(Transacao005 p)
        {
            List <ProprietarioSPE> listaRetorno = new List <ProprietarioSPE>();

            //if (!string.IsNullOrEmpty(p.nome_proprietario) &
            //    !string.IsNullOrEmpty(p.endereco_proprietario) &
            //    !string.IsNullOrEmpty(p.numero_endereco_proprietario) &
            //    !string.IsNullOrEmpty(p.complemento_endereco_proprietario) &
            //    !string.IsNullOrEmpty(p.descricao_municipio_endereco) &
            //    !string.IsNullOrEmpty(p.cep_endereco_proprietario) &
            //    !string.IsNullOrEmpty(p.bairro_proprietario) &
            //    !string.IsNullOrEmpty(p.uf_proprietario) & p.flag_notificar_proprietario == "S")
            //{
            //    //PROPRIETARIO
            //    listaRetorno.Add(new ProprietarioSPE()
            //    {
            //        BAIRRO = p.bairro_proprietario,
            //        CEP = p.cep_endereco_proprietario,
            //        CHASSI = p.chassi,
            //        COMPLEMENTO = p.complemento_endereco_proprietario,
            //        DATA_HORA_APREENSAO = p.Data_Hora_Apreensao,
            //        ENDERECO = p.endereco_proprietario,
            //        FORMULARIO_GRV = p.Processo,
            //        MARCA_MODELO = p.descricao_marca,
            //        MUNICIPIO = p.descricao_municipio_endereco,
            //        NOME = p.nome_proprietario,
            //        NUMERO = p.numero_endereco_proprietario,
            //        PLACA = p.placa,
            //        RENAVAM = p.renavam,
            //        UF = p.uf_proprietario
            //    });
            //}

            //if (!string.IsNullOrEmpty(p.nome_comunicado_venda) &
            //    !string.IsNullOrEmpty(p.endereco_comunicado_venda) &
            //    !string.IsNullOrEmpty(p.numero_comunicado_venda) &
            //    !string.IsNullOrEmpty(p.complemento_comunicado_venda) &
            //    !string.IsNullOrEmpty(p.municipio_comunicado_venda) &
            //    !string.IsNullOrEmpty(p.cep_comunicado_venda) &
            //    !string.IsNullOrEmpty(p.bairro_comunicado_venda) &
            //    !string.IsNullOrEmpty(p.uf_comunicado_venda) & p.flag_notificar_comunicado == "S")
            //{
            //    //COMUNICADO DE VENDA
            //    listaRetorno.Add(new ProprietarioSPE()
            //    {
            //        BAIRRO = p.bairro_comunicado_venda,
            //        CEP = p.cep_comunicado_venda,
            //        CHASSI = p.chassi,
            //        COMPLEMENTO = p.complemento_comunicado_venda,
            //        DATA_HORA_APREENSAO = p.Data_Hora_Apreensao,
            //        ENDERECO = p.endereco_comunicado_venda,
            //        FORMULARIO_GRV = p.Processo,
            //        MARCA_MODELO = p.descricao_marca,
            //        MUNICIPIO = p.municipio_comunicado_venda,
            //        NOME = p.nome_comunicado_venda,
            //        NUMERO = p.numero_comunicado_venda,
            //        PLACA = p.placa,
            //        RENAVAM = p.renavam,
            //        UF = p.uf_comunicado_venda
            //    });
            //}

            //if (!string.IsNullOrEmpty(p.nome_financiamento_efet) &
            //    !string.IsNullOrEmpty(p.endereco_financiamento_efet) &
            //    !string.IsNullOrEmpty(p.numero_financiamento_efet) &
            //    !string.IsNullOrEmpty(p.complemento_financiamento_efet) &
            //    !string.IsNullOrEmpty(p.municipio_financiamento_efet) &
            //    !string.IsNullOrEmpty(p.cep_financiamento_efet) &
            //    !string.IsNullOrEmpty(p.bairro_financiamento_efet) &
            //    !string.IsNullOrEmpty(p.uf_financiamento_efet) & p.flag_notificar_financeira == "S")
            //{
            //    //FINANCEIRA
            //    listaRetorno.Add(new ProprietarioSPE()
            //    {
            //        BAIRRO = p.bairro_financiamento_efet,
            //        CEP = p.cep_financiamento_efet,
            //        CHASSI = p.chassi,
            //        COMPLEMENTO = p.complemento_financiamento_efet,
            //        DATA_HORA_APREENSAO = p.Data_Hora_Apreensao,
            //        ENDERECO = p.endereco_financiamento_efet,
            //        FORMULARIO_GRV = p.Processo,
            //        MARCA_MODELO = p.descricao_marca,
            //        MUNICIPIO = p.municipio_financiamento_efet,
            //        NOME = p.nome_financiamento_efet,
            //        NUMERO = p.numero_financiamento_efet,
            //        PLACA = p.placa,
            //        RENAVAM = p.renavam,
            //        UF = p.uf_financiamento_efet
            //    });
            //}

            return(listaRetorno);
        }
        public ActionResult ConferenciaLeilao(string pPlaca, string pChassi, string pProcesso, bool?SalvarDados, Lote model, FormCollection FormCollection, Transacao005 Proprietario, int?id)
        {
            List <StatusLote> statusLote    = RepositorioGlobal.StatusLote.SelecionarTudo().ToList();
            SelectList        ddlStatusLote = new SelectList(statusLote, "Id", "Descricao");

            ViewBag.StatusLote = ddlStatusLote;

            ViewBag.pPlaca    = "";
            ViewBag.pChassi   = "";
            ViewBag.pProcesso = "";

            ViewBag.prop            = new Proprietario();
            ViewBag.Pericia         = new Pericia();
            ViewBag.Restricoes      = new List <Restricao>();
            ViewBag.ArquivosPericia = new List <ArquivoPericia>();

            if (SalvarDados != null && SalvarDados.Value)
            {
                //gravar form
                RepositorioGlobal.Lote.Alterar(model);

                Proprietario.Id_lote = model.id;
                Proprietario.Id      = 0;

                RepositorioGlobal.Proprietario.Alterar(Proprietario);

                ViewBag.Msg             = "Dados gravados com sucesso!";
                ViewBag.prop            = RepositorioGlobal.Proprietario.SelecionarProprietarioPorLote(model.id);
                ViewBag.Pericia         = RepositorioGlobal.Pericia.SelecionarPorId(model.id);
                ViewBag.Restricoes      = RepositorioGlobal.Restricao.SelecionarTudo(model.id);
                ViewBag.Arquivospericia = RepositorioGlobal.Pericia.SelecionarArquivos(model.id);

                return(View(model));
            }

            try
            {
                Lote filtro = new Lote();

                if (!string.IsNullOrEmpty(pPlaca))
                {
                    filtro = RepositorioGlobal.Lote.ConsultarLote(Placa: pPlaca);
                }

                if (!string.IsNullOrEmpty(pChassi))
                {
                    filtro = RepositorioGlobal.Lote.ConsultarLote(Chassi: pChassi);
                }

                if (!string.IsNullOrEmpty(pProcesso))
                {
                    filtro = RepositorioGlobal.Lote.ConsultarLote(Processo: pProcesso);
                }

                if (id.HasValue)
                {
                    filtro = RepositorioGlobal.Lote.ConsultarLote(IdLote: id.Value);
                }

                ViewBag.pPlaca    = pPlaca;
                ViewBag.pChassi   = pChassi;
                ViewBag.pProcesso = pProcesso;

                if (filtro == null)
                {
                    ViewBag.erro = "Lote não localizado ou não pertence ao pátio";
                    return(View(new Lote()));
                }
                else
                {
                    ViewBag.prop            = RepositorioGlobal.Proprietario.SelecionarProprietarioPorLote(filtro.id);
                    ViewBag.Pericia         = RepositorioGlobal.Pericia.SelecionarArquivosPorID(filtro.id);
                    ViewBag.Restricoes      = RepositorioGlobal.Restricao.SelecionarTudo(filtro.id);
                    ViewBag.Leilao          = RepositorioGlobal.Leilao.SelecionarPorId(filtro.id_leilao);
                    ViewBag.Arquivospericia = RepositorioGlobal.Pericia.SelecionarArquivos(filtro.id);

                    return(View(filtro));
                }
            }
            catch
            {
                throw;
            }
        }