Ejemplo n.º 1
0
        public ActionResult PesquisaAvancada(PesquisaEmpregadoViewModel entidade)
        {
            try
            {
                if (string.IsNullOrEmpty(entidade.Nome) &&
                    string.IsNullOrEmpty(entidade.CPF) &&
                    entidade.Status == null &&
                    string.IsNullOrEmpty(entidade.Empresa) &&
                    string.IsNullOrEmpty(entidade.Contrato) &&
                    string.IsNullOrEmpty(entidade.DataAdmissao) &&
                    string.IsNullOrEmpty(entidade.Cargo) &&
                    string.IsNullOrEmpty(entidade.Funcao) &&
                    string.IsNullOrEmpty(entidade.Atividade))
                {
                    throw new Exception("Informe pelo menos um filtro para prosseguir na pesquisa de empregado.");
                }


                string sWhere = string.Empty;
                string sFrom  = string.Empty;

                if (!string.IsNullOrEmpty(entidade.Nome))
                {
                    sWhere += " and upper(e.Nome) like '%" + entidade.Nome.ToUpper() + "%'";
                }

                if (!string.IsNullOrEmpty(entidade.CPF))
                {
                    sWhere += " and e.CPF = '" + entidade.CPF + "'";
                }

                if (entidade.Status != null)
                {
                    sWhere += " and e.Status = '" + entidade.Status.GetDisplayName() + "'";
                }


                if (!string.IsNullOrEmpty(entidade.DataAdmissao) ||
                    !string.IsNullOrEmpty(entidade.Empresa) ||
                    !string.IsNullOrEmpty(entidade.Contrato) ||
                    !string.IsNullOrEmpty(entidade.Cargo) ||
                    !string.IsNullOrEmpty(entidade.Funcao))
                {
                    sFrom  += ", tbAdmissao a ";
                    sWhere += " and e.UniqueKey = a.UKEmpregado and a.DataExclusao = CONVERT(datetime,'9999-12-31 23:59:59.997',101) ";

                    if (!string.IsNullOrEmpty(entidade.DataAdmissao))
                    {
                        string data1 = entidade.DataAdmissao.Substring(0, entidade.DataAdmissao.IndexOf(" - "));
                        string data2 = entidade.DataAdmissao.Substring(entidade.DataAdmissao.IndexOf(" - ") + 3);

                        sWhere += @" and a.DataAdmissao between '" + data1 + "' and '" + data2 + "'";
                    }

                    if (!string.IsNullOrEmpty(entidade.Empresa))
                    {
                        sWhere += " and a.UKEmpresa = '" + entidade.Empresa + "' ";
                    }

                    if (!string.IsNullOrEmpty(entidade.Contrato) ||
                        !string.IsNullOrEmpty(entidade.Cargo) ||
                        !string.IsNullOrEmpty(entidade.Funcao))
                    {
                        sFrom  += ", tbAlocacao al ";
                        sWhere += " and a.UniqueKey = al.UKAdmissao and al.DataExclusao = CONVERT(datetime,'9999-12-31 23:59:59.997',101) ";

                        if (!string.IsNullOrEmpty(entidade.Contrato))
                        {
                            sWhere += @" and al.UKContrato = '" + entidade.Contrato + "'";
                        }

                        if (!string.IsNullOrEmpty(entidade.Cargo))
                        {
                            sWhere += @" and al.UKCargo = '" + entidade.Cargo + "'";
                        }

                        if (!string.IsNullOrEmpty(entidade.Funcao))
                        {
                            sWhere += @" and al.UKFuncao = '" + entidade.Funcao + "'";
                        }
                    }
                }



                string sql = @"select top 100 e.UniqueKey, e.Nome, e.CPF, e.DataNascimento, e.Email, e.Status
                           from tbEmpregado e " + sFrom + @"
                           where e.DataExclusao = CONVERT(datetime,'9999-12-31 23:59:59.997',101) " + sWhere + @"
                           order by e.Nome";

                List <Empregado> lista  = new List <Empregado>();
                DataTable        result = ContratoBusiness.GetDataTable(sql);
                if (result.Rows.Count > 0)
                {
                    foreach (DataRow row in result.Rows)
                    {
                        lista.Add(new Empregado()
                        {
                            UniqueKey      = Guid.Parse(row["UniqueKey"].ToString()),
                            Nome           = row["Nome"].ToString(),
                            CPF            = row["CPF"].ToString(),
                            DataNascimento = row["DataNascimento"].ToString(),
                            Email          = row["Email"].ToString(),
                            Status         = row["Status"].ToString()
                        });
                    }
                }

                return(PartialView("_PesquisaAvancada", lista));
            }
            catch (Exception ex)
            {
                return(Json(new { resultado = new RetornoJSON()
                                  {
                                      Erro = ex.Message
                                  } }));
            }
        }
Ejemplo n.º 2
0
        public ActionResult Pesquisar(VMPesquisaContrato entidade)
        {
            try
            {
                string sFrom  = string.Empty;
                string sWhere = string.Empty;

                if (string.IsNullOrEmpty(entidade.DataFim) &&
                    string.IsNullOrEmpty(entidade.DataInicio) &&
                    string.IsNullOrEmpty(entidade.Descricao) &&
                    string.IsNullOrEmpty(entidade.UKDepartamento) &&
                    string.IsNullOrEmpty(entidade.Numero) &&
                    string.IsNullOrEmpty(entidade.UKFornecedor) &&
                    string.IsNullOrEmpty(entidade.UKSubContratada))
                {
                    throw new Exception("Informe pelo menos um filtro para prosseguir na pesquisa.");
                }

                if (!string.IsNullOrEmpty(entidade.Numero))
                {
                    sWhere += " and o.Numero like '" + entidade.Numero.Replace("*", "%") + "'";
                }

                if (!string.IsNullOrEmpty(entidade.DataInicio))
                {
                    sWhere += " and o.DataInicio = '" + entidade.DataInicio + "'";
                }

                if (!string.IsNullOrEmpty(entidade.DataFim))
                {
                    sWhere += " and o.DataFim = '" + entidade.DataFim + "'";
                }

                if (!string.IsNullOrEmpty(entidade.UKFornecedor))
                {
                    sWhere += " AND r1.UKFornecedor = '" + entidade.UKFornecedor + "'";
                }

                if (!string.IsNullOrEmpty(entidade.UKDepartamento))
                {
                    sFrom   = ", REL_DepartamentoContrato r2 ";
                    sWhere += " and o.UniqueKey = r2.UKContrato and r2.DataExclusao = '9999-12-31 23:59:59.997' and r2.UKDepartamento = '" + entidade.UKDepartamento + "'";
                }

                if (!string.IsNullOrEmpty(entidade.UKSubContratada))
                {
                    sFrom  += ", REL_ContratoFornecedor r3 ";
                    sWhere += " and o.UniqueKey = r3.UKContrato and r3.TipoContratoFornecedor = 1 and r3.DataExclusao = '9999-12-31 23:59:59.997' and r3.UKFornecedor = '" + entidade.UKSubContratada + "'";
                }

                string sql = @"select top 100 o.UniqueKey, o.Numero, o.DataInicio, o.DataFim,
	                                  (     select STRING_AGG(d.Sigla, ',') WITHIN GROUP (ORDER BY d.Sigla) 
		                                    from REL_DepartamentoContrato r1, tbDepartamento d 
		                                    where r1.DataExclusao = '9999-12-31 23:59:59.997' and d.DataExclusao = '9999-12-31 23:59:59.997' and 
			                                      r1.UKContrato = o.UniqueKey and r1.UKDepartamento = d.UniqueKey
                                      ) as Departamentos, 
		                              f.NomeFantasia,
		                              (     select STRING_AGG(f.NomeFantasia, ',') WITHIN GROUP (ORDER BY f.NomeFantasia)   
		                                    from REL_ContratoFornecedor r2, tbFornecedor f
		                                    WHERE r2.UKContrato = o.UniqueKey and r2.DataExclusao = '9999-12-31 23:59:59.997' and
			                                      r2.UKFornecedor = f.UniqueKey and f.DataExclusao = '9999-12-31 23:59:59.997' and
			                                      r2.TipoContratoFornecedor = 1
                                      ) as SubContratadas
                               from tbcontrato o, REL_ContratoFornecedor r1, tbEmpresa f " + sFrom + @"
                               where o.DataExclusao = '9999-12-31 23:59:59.997' and r1.DataExclusao = '9999-12-31 23:59:59.997' and
	                                 o.UniqueKey = r1.UKContrato and r1.TipoContratoFornecedor = 0 and
	                                 r1.UKFornecedor = f.UniqueKey and f.DataExclusao = '9999-12-31 23:59:59.997' "     + sWhere + @"
                               order by o.Numero";

                List <VMPesquisaContrato> lista = new List <VMPesquisaContrato>();
                DataTable result = ContratoBusiness.GetDataTable(sql);
                if (result.Rows.Count > 0)
                {
                    foreach (DataRow row in result.Rows)
                    {
                        lista.Add(new VMPesquisaContrato()
                        {
                            UniqueKey       = row["UniqueKey"].ToString(),
                            Numero          = row["Numero"].ToString(),
                            DataInicio      = row["DataInicio"].ToString(),
                            DataFim         = row["DataFim"].ToString(),
                            UKFornecedor    = row["NomeFantasia"].ToString(),
                            UKDepartamento  = row["Departamentos"].ToString(),
                            UKSubContratada = row["SubContratadas"].ToString()
                        });
                    }
                }

                return(PartialView("_Pesquisar", lista));
            }
            catch (Exception ex)
            {
                return(Json(new { resultado = new RetornoJSON()
                                  {
                                      Erro = ex.Message
                                  } }));
            }
        }