/// <summary> /// Get TbRecebimentoVenda/TbRecebimentoVenda /// </summary> /// <param name="colecao"></param> /// <param name="campo"></param> /// <param name="orderby"></param> /// <param name="pageSize"></param> /// <param name="pageNumber"></param> /// <param name="queryString"></param> /// <returns></returns> private static IQueryable <tbRecebimentoVenda> getQuery(painel_taxservices_dbContext _db, int colecao, int campo, int orderby, int pageSize, int pageNumber, Dictionary <string, string> queryString) { // DEFINE A QUERY PRINCIPAL var entity = _db.tbRecebimentoVendas.AsQueryable <tbRecebimentoVenda>(); #region WHERE - ADICIONA OS FILTROS A QUERY // ADICIONA OS FILTROS A QUERY foreach (var item in queryString) { int key = Convert.ToInt16(item.Key); CAMPOS filtroEnum = (CAMPOS)key; switch (filtroEnum) { case CAMPOS.IDRECEBIMENTOVENDA: Int32 idRecebimentoVenda = Convert.ToInt32(item.Value); entity = entity.Where(e => e.idRecebimentoVenda.Equals(idRecebimentoVenda)).AsQueryable <tbRecebimentoVenda>(); break; case CAMPOS.NRCNPJ: string nrCNPJ = Convert.ToString(item.Value); entity = entity.Where(e => e.nrCNPJ.Equals(nrCNPJ)).AsQueryable <tbRecebimentoVenda>(); break; case CAMPOS.NRNSU: string nrNSU = Convert.ToString(item.Value); if (nrNSU.Contains("%")) // usa LIKE => ENDS WITH { string busca = nrNSU.Replace("%", "").ToString(); entity = entity.Where(e => e.nrNSU.EndsWith(busca)).AsQueryable <tbRecebimentoVenda>(); } else { entity = entity.Where(e => e.nrNSU.Equals(nrNSU)).AsQueryable <tbRecebimentoVenda>(); } break; case CAMPOS.DTVENDA: if (item.Value.Contains("|")) // BETWEEN { string[] busca = item.Value.Split('|'); DateTime dtaIni = DateTime.ParseExact(busca[0] + " 00:00:00.000", "yyyyMMdd HH:mm:ss.fff", CultureInfo.InvariantCulture); DateTime dtaFim = DateTime.ParseExact(busca[1] + " 23:59:00.000", "yyyyMMdd HH:mm:ss.fff", CultureInfo.InvariantCulture); entity = entity.Where(e => e.dtVenda >= dtaIni && e.dtVenda <= dtaFim).AsQueryable <tbRecebimentoVenda>(); } else // IGUAL { string busca = item.Value; DateTime dtaIni = DateTime.ParseExact(busca + " 00:00:00.000", "yyyyMMdd HH:mm:ss.fff", CultureInfo.InvariantCulture); entity = entity.Where(e => e.dtVenda.Year == dtaIni.Year && e.dtVenda.Month == dtaIni.Month && e.dtVenda.Day == dtaIni.Day).AsQueryable <tbRecebimentoVenda>(); } break; case CAMPOS.CDADQUIRENTE: Int32 cdAdquirente = Convert.ToInt32(item.Value); entity = entity.Where(e => e.cdAdquirente == cdAdquirente).AsQueryable <tbRecebimentoVenda>(); break; case CAMPOS.DSBANDEIRA: string dsBandeira = Convert.ToString(item.Value); entity = entity.Where(e => e.dsBandeira.Equals(dsBandeira)).AsQueryable <tbRecebimentoVenda>(); break; case CAMPOS.VLVENDA: decimal vlVenda = Convert.ToDecimal(item.Value); entity = entity.Where(e => e.vlVenda.Equals(vlVenda)).AsQueryable <tbRecebimentoVenda>(); break; case CAMPOS.QTPARCELAS: byte qtParcelas = Convert.ToByte(item.Value); entity = entity.Where(e => e.qtParcelas.Equals(qtParcelas)).AsQueryable <tbRecebimentoVenda>(); break; case CAMPOS.CDERP: string cdERP = Convert.ToString(item.Value); entity = entity.Where(e => e.cdERP.Equals(cdERP)).AsQueryable <tbRecebimentoVenda>(); break; case CAMPOS.CDSACADO: string cdSacado = Convert.ToString(item.Value); entity = entity.Where(e => e.cdSacado.Equals(cdSacado)).AsQueryable <tbRecebimentoVenda>(); break; case CAMPOS.DTAJUSTE: if (item.Value.Contains("|")) // BETWEEN { string[] busca = item.Value.Split('|'); DateTime dtaIni = DateTime.ParseExact(busca[0] + " 00:00:00.000", "yyyyMMdd HH:mm:ss.fff", CultureInfo.InvariantCulture); DateTime dtaFim = DateTime.ParseExact(busca[1] + " 23:59:00.000", "yyyyMMdd HH:mm:ss.fff", CultureInfo.InvariantCulture); entity = entity.Where(e => e.dtAjuste != null && e.dtAjuste >= dtaIni && e.dtAjuste <= dtaFim).AsQueryable <tbRecebimentoVenda>(); } else // IGUAL { string busca = item.Value; DateTime dtaIni = DateTime.ParseExact(busca + " 00:00:00.000", "yyyyMMdd HH:mm:ss.fff", CultureInfo.InvariantCulture); entity = entity.Where(e => e.dtAjuste != null && e.dtAjuste.Value.Year == dtaIni.Year && e.dtAjuste.Value.Month == dtaIni.Month && e.dtAjuste.Value.Day == dtaIni.Day).AsQueryable <tbRecebimentoVenda>(); } break; case CAMPOS.DSMENSAGEM: string dsMensagem = Convert.ToString(item.Value); entity = entity.Where(e => e.dsMensagem.Equals(dsMensagem)).AsQueryable <tbRecebimentoVenda>(); break; case CAMPOS.DSDETALHE: string dsDetalhe = Convert.ToString(item.Value); entity = entity.Where(e => e.dsDetalhe.Equals(dsDetalhe)).AsQueryable <tbRecebimentoVenda>(); break; // RELACIONAMENTOS case CAMPOS.ID_GRUPO: Int32 id_grupo = Convert.ToInt32(item.Value); entity = entity.Where(e => e.empresa.id_grupo == id_grupo).AsQueryable <tbRecebimentoVenda>(); break; } } #endregion string outValue = null; if (queryString.TryGetValue("" + (int)CAMPOS.TIPO, out outValue)) { string script = String.Empty; TIPO_FILTRO tipo = (TIPO_FILTRO)Convert.ToInt32(queryString["" + (int)CAMPOS.TIPO]); switch (tipo) { case TIPO_FILTRO.CONCILIADO: // entity = entity.Where(e => e.Recebimentos.Count > 0).AsQueryable<tbRecebimentoVenda>(); script = "SELECT DISTINCT V.idRecebimentoVenda" + " FROM card.tbRecebimentoVenda V (NOLOCK)" + " LEFT JOIN pos.Recebimento R (NOLOCK) ON R.idRecebimentoVenda = V.idRecebimentoVenda" + " WHERE R.idRecebimentoVenda IS NOT NULL" + " AND V.idRecebimentoVenda IN (" + string.Join(", ", entity.Select(e => e.idRecebimentoVenda)) + ")"; int[] vendasConciliadas = _db.Database.SqlQuery <int>(script).ToArray(); entity = entity.Where(e => vendasConciliadas.Contains(e.idRecebimentoVenda)).AsQueryable <tbRecebimentoVenda>(); break; case TIPO_FILTRO.NAO_CONCILIADO: // entity = entity.Where(e => e.Recebimentos.Count == 0).AsQueryable<tbRecebimentoVenda>(); script = "SELECT V.idRecebimentoVenda" + " FROM card.tbRecebimentoVenda V (NOLOCK)" + " LEFT JOIN pos.Recebimento R (NOLOCK) ON R.idRecebimentoVenda = V.idRecebimentoVenda" + " WHERE R.idRecebimentoVenda IS NULL" + " AND V.idRecebimentoVenda IN (" + string.Join(", ", entity.Select(e => e.idRecebimentoVenda)) + ")"; int[] vendasNaoConciliadas = _db.Database.SqlQuery <int>(script).ToArray(); entity = entity.Where(e => vendasNaoConciliadas.Contains(e.idRecebimentoVenda)).AsQueryable <tbRecebimentoVenda>(); break; case TIPO_FILTRO.CORRIGIDO: entity = entity.Where(e => e.dtAjuste != null).AsQueryable <tbRecebimentoVenda>(); break; case TIPO_FILTRO.CORRECAO_MANUAL: entity = entity.Where(e => e.dtAjuste != null && e.dsMensagem != null).AsQueryable <tbRecebimentoVenda>(); break; } } #region ORDER BY - ADICIONA A ORDENAÇÃO A QUERY // ADICIONA A ORDENAÇÃO A QUERY CAMPOS filtro = (CAMPOS)campo; switch (filtro) { case CAMPOS.IDRECEBIMENTOVENDA: if (orderby == 0) { entity = entity.OrderBy(e => e.idRecebimentoVenda).AsQueryable <tbRecebimentoVenda>(); } else { entity = entity.OrderByDescending(e => e.idRecebimentoVenda).AsQueryable <tbRecebimentoVenda>(); } break; case CAMPOS.NRCNPJ: if (orderby == 0) { entity = entity.OrderBy(e => e.nrCNPJ).AsQueryable <tbRecebimentoVenda>(); } else { entity = entity.OrderByDescending(e => e.nrCNPJ).AsQueryable <tbRecebimentoVenda>(); } break; case CAMPOS.NRNSU: if (orderby == 0) { entity = entity.OrderBy(e => e.nrNSU).AsQueryable <tbRecebimentoVenda>(); } else { entity = entity.OrderByDescending(e => e.nrNSU).AsQueryable <tbRecebimentoVenda>(); } break; case CAMPOS.DTVENDA: if (orderby == 0) { entity = entity.OrderBy(e => e.dtVenda).AsQueryable <tbRecebimentoVenda>(); } else { entity = entity.OrderByDescending(e => e.dtVenda).AsQueryable <tbRecebimentoVenda>(); } break; case CAMPOS.CDADQUIRENTE: if (orderby == 0) { entity = entity.OrderBy(e => e.cdAdquirente).AsQueryable <tbRecebimentoVenda>(); } else { entity = entity.OrderByDescending(e => e.cdAdquirente).AsQueryable <tbRecebimentoVenda>(); } break; case CAMPOS.DSBANDEIRA: if (orderby == 0) { entity = entity.OrderBy(e => e.dsBandeira).AsQueryable <tbRecebimentoVenda>(); } else { entity = entity.OrderByDescending(e => e.dsBandeira).AsQueryable <tbRecebimentoVenda>(); } break; case CAMPOS.VLVENDA: if (orderby == 0) { entity = entity.OrderBy(e => e.vlVenda).AsQueryable <tbRecebimentoVenda>(); } else { entity = entity.OrderByDescending(e => e.vlVenda).AsQueryable <tbRecebimentoVenda>(); } break; case CAMPOS.QTPARCELAS: if (orderby == 0) { entity = entity.OrderBy(e => e.qtParcelas).AsQueryable <tbRecebimentoVenda>(); } else { entity = entity.OrderByDescending(e => e.qtParcelas).AsQueryable <tbRecebimentoVenda>(); } break; case CAMPOS.CDERP: if (orderby == 0) { entity = entity.OrderBy(e => e.cdERP).AsQueryable <tbRecebimentoVenda>(); } else { entity = entity.OrderByDescending(e => e.cdERP).AsQueryable <tbRecebimentoVenda>(); } break; case CAMPOS.CDSACADO: if (orderby == 0) { entity = entity.OrderBy(e => e.cdSacado).AsQueryable <tbRecebimentoVenda>(); } else { entity = entity.OrderByDescending(e => e.cdSacado).AsQueryable <tbRecebimentoVenda>(); } break; case CAMPOS.DTAJUSTE: if (orderby == 0) { entity = entity.OrderBy(e => e.dtAjuste).AsQueryable <tbRecebimentoVenda>(); } else { entity = entity.OrderByDescending(e => e.dtAjuste).AsQueryable <tbRecebimentoVenda>(); } break; case CAMPOS.DSMENSAGEM: if (orderby == 0) { entity = entity.OrderBy(e => e.dsMensagem).AsQueryable <tbRecebimentoVenda>(); } else { entity = entity.OrderByDescending(e => e.dsMensagem).AsQueryable <tbRecebimentoVenda>(); } break; } #endregion return(entity); }
/// <summary> /// Get TbRecebimentoVenda/TbRecebimentoVenda /// </summary> /// <param name="colecao"></param> /// <param name="campo"></param> /// <param name="orderby"></param> /// <param name="pageSize"></param> /// <param name="pageNumber"></param> /// <param name="queryString"></param> /// <returns></returns> public static SimpleDataBaseQuery getQuery(int campo, int orderby, Dictionary <string, string> queryString) { Dictionary <string, string> join = new Dictionary <string, string>(); List <string> where = new List <string>(); List <string> order = new List <string>(); #region WHERE - ADICIONA OS FILTROS A QUERY // ADICIONA OS FILTROS A QUERY foreach (KeyValuePair <string, string> item in queryString) { int key = Convert.ToInt16(item.Key); CAMPOS filtroEnum = (CAMPOS)key; switch (filtroEnum) { case CAMPOS.IDRECEBIMENTOVENDA: Int32 idRecebimentoVenda = Convert.ToInt32(item.Value); where.Add(SIGLA_QUERY + ".idRecebimentoVenda = " + idRecebimentoVenda); break; case CAMPOS.NRCNPJ: string nrCNPJ = Convert.ToString(item.Value); where.Add(SIGLA_QUERY + ".nrCNPJ = '" + nrCNPJ + "'"); break; case CAMPOS.NRNSU: string nrNSU = Convert.ToString(item.Value); if (nrNSU.Contains("%")) // usa LIKE => ENDS WITH { string busca = nrNSU.Replace("%", "").ToString(); where.Add(SIGLA_QUERY + ".nrNSU like '%" + busca + "'"); } else { where.Add(SIGLA_QUERY + ".nrNSU = '" + nrNSU + "'"); } break; case CAMPOS.DTVENDA: if (item.Value.Contains("|")) // BETWEEN { string[] busca = item.Value.Split('|'); DateTime dtaIni = DateTime.ParseExact(busca[0] + " 00:00:00.000", "yyyyMMdd HH:mm:ss.fff", CultureInfo.InvariantCulture); DateTime dtaFim = DateTime.ParseExact(busca[1] + " 23:59:00.000", "yyyyMMdd HH:mm:ss.fff", CultureInfo.InvariantCulture); string dtInicio = DataBaseQueries.GetDate(dtaIni); string dtFim = DataBaseQueries.GetDate(dtaFim); where.Add(SIGLA_QUERY + ".dtVenda BETWEEN '" + dtInicio + "' AND '" + dtFim + " 23:59:00'"); } else // IGUAL { string busca = item.Value; DateTime data = DateTime.ParseExact(busca + " 00:00:00.000", "yyyyMMdd HH:mm:ss.fff", CultureInfo.InvariantCulture); string dt = DataBaseQueries.GetDate(data); where.Add(SIGLA_QUERY + ".dtVenda BETWEEN '" + dt + "' AND '" + dt + " 23:59:00'"); } break; case CAMPOS.CDADQUIRENTE: Int32 cdAdquirente = Convert.ToInt32(item.Value); where.Add(SIGLA_QUERY + ".cdAdquirente = " + cdAdquirente); break; case CAMPOS.DSBANDEIRA: string dsBandeira = Convert.ToString(item.Value); where.Add(SIGLA_QUERY + ".dsBandeira = '" + dsBandeira + "'"); break; case CAMPOS.VLVENDA: decimal vlVenda = Convert.ToDecimal(item.Value); where.Add(SIGLA_QUERY + ".vlVenda = " + vlVenda.ToString(CultureInfo.GetCultureInfo("en-GB"))); break; case CAMPOS.QTPARCELAS: byte qtParcelas = Convert.ToByte(item.Value); where.Add(SIGLA_QUERY + ".qtParcelas = " + qtParcelas); break; case CAMPOS.CDERP: string cdERP = Convert.ToString(item.Value); where.Add(SIGLA_QUERY + ".cdERP = '" + cdERP + "'"); break; case CAMPOS.CDSACADO: string cdSacado = Convert.ToString(item.Value); where.Add(SIGLA_QUERY + ".cdSacado = '" + cdSacado + "'"); break; case CAMPOS.DTAJUSTE: if (item.Value.Contains("|")) // BETWEEN { string[] busca = item.Value.Split('|'); DateTime dtaIni = DateTime.ParseExact(busca[0] + " 00:00:00.000", "yyyyMMdd HH:mm:ss.fff", CultureInfo.InvariantCulture); DateTime dtaFim = DateTime.ParseExact(busca[1] + " 23:59:00.000", "yyyyMMdd HH:mm:ss.fff", CultureInfo.InvariantCulture); string dtInicio = DataBaseQueries.GetDate(dtaIni); string dtFim = DataBaseQueries.GetDate(dtaFim); where.Add(SIGLA_QUERY + ".dtAjuste BETWEEN '" + dtInicio + "' AND '" + dtFim + " 23:59:00'"); } else // IGUAL { string busca = item.Value; DateTime data = DateTime.ParseExact(busca + " 00:00:00.000", "yyyyMMdd HH:mm:ss.fff", CultureInfo.InvariantCulture); string dt = DataBaseQueries.GetDate(data); where.Add(SIGLA_QUERY + ".dtAjuste BETWEEN '" + dt + "' AND '" + dt + " 23:59:00'"); } break; case CAMPOS.DSMENSAGEM: string dsMensagem = Convert.ToString(item.Value); where.Add(SIGLA_QUERY + ".dsMensagem = '" + dsMensagem + "'"); break; case CAMPOS.DSDETALHE: string dsDetalhe = Convert.ToString(item.Value); where.Add(SIGLA_QUERY + ".dsDetalhe = '" + dsDetalhe + "'"); break; // RELACIONAMENTOS case CAMPOS.ID_GRUPO: Int32 id_grupo = Convert.ToInt32(item.Value); // JOIN if (!join.ContainsKey("INNER JOIN cliente.empresa " + GatewayEmpresa.SIGLA_QUERY)) { join.Add("INNER JOIN cliente.empresa " + GatewayEmpresa.SIGLA_QUERY, " ON " + SIGLA_QUERY + ".nrCNPJ = " + GatewayEmpresa.SIGLA_QUERY + ".nu_cnpj"); } where.Add(GatewayEmpresa.SIGLA_QUERY + ".id_grupo = " + id_grupo); break; } } #endregion string outValue = null; if (queryString.TryGetValue("" + (int)CAMPOS.TIPO, out outValue)) { string script = String.Empty; TIPO_FILTRO tipo = (TIPO_FILTRO)Convert.ToInt32(queryString["" + (int)CAMPOS.TIPO]); switch (tipo) { case TIPO_FILTRO.CONCILIADO: // Adiciona o join if (!join.ContainsKey("LEFT JOIN pos.Recebimento " + GatewayRecebimento.SIGLA_QUERY)) { join.Add("LEFT JOIN pos.Recebimento " + GatewayRecebimento.SIGLA_QUERY, " ON " + GatewayRecebimento.SIGLA_QUERY + ".idRecebimentoVenda = " + SIGLA_QUERY + ".idRecebimentoVenda"); } where.Add(GatewayRecebimento.SIGLA_QUERY + ".idRecebimentoVenda IS NOT NULL"); break; case TIPO_FILTRO.NAO_CONCILIADO: // Adiciona o join if (!join.ContainsKey("LEFT JOIN pos.Recebimento " + GatewayRecebimento.SIGLA_QUERY)) { join.Add("LEFT JOIN pos.Recebimento " + GatewayRecebimento.SIGLA_QUERY, " ON " + GatewayRecebimento.SIGLA_QUERY + ".idRecebimentoVenda = " + SIGLA_QUERY + ".idRecebimentoVenda"); } where.Add(GatewayRecebimento.SIGLA_QUERY + ".idRecebimentoVenda IS NULL"); break; case TIPO_FILTRO.CORRIGIDO: where.Add(SIGLA_QUERY + ".dtAjuste IS NOT NULL"); break; case TIPO_FILTRO.CORRECAO_MANUAL: where.Add(SIGLA_QUERY + ".dtAjuste IS NOT NULL && " + SIGLA_QUERY + ".dsMensagem IS NOT NULL"); break; } } #region ORDER BY - ADICIONA A ORDENAÇÃO A QUERY // ADICIONA A ORDENAÇÃO A QUERY CAMPOS filtro = (CAMPOS)campo; switch (filtro) { case CAMPOS.IDRECEBIMENTOVENDA: if (orderby == 0) { order.Add(SIGLA_QUERY + ".idRecebimentoVenda ASC"); } else { order.Add(SIGLA_QUERY + ".idRecebimentoVenda DESC"); } break; case CAMPOS.NRCNPJ: if (orderby == 0) { order.Add(SIGLA_QUERY + ".nrCNPJ ASC"); } else { order.Add(SIGLA_QUERY + ".nrCNPJ DESC"); } break; case CAMPOS.NRNSU: if (orderby == 0) { order.Add(SIGLA_QUERY + ".nrNSU ASC"); } else { order.Add(SIGLA_QUERY + ".nrNSU DESC"); } break; case CAMPOS.DTVENDA: if (orderby == 0) { order.Add(SIGLA_QUERY + ".dtVenda ASC"); } else { order.Add(SIGLA_QUERY + ".dtVenda DESC"); } break; case CAMPOS.CDADQUIRENTE: if (orderby == 0) { order.Add(SIGLA_QUERY + ".cdAdquirente ASC"); } else { order.Add(SIGLA_QUERY + ".cdAdquirente DESC"); } break; case CAMPOS.DSBANDEIRA: if (orderby == 0) { order.Add(SIGLA_QUERY + ".dsBandeira ASC"); } else { order.Add(SIGLA_QUERY + ".dsBandeira DESC"); } break; case CAMPOS.VLVENDA: if (orderby == 0) { order.Add(SIGLA_QUERY + ".vlVenda ASC"); } else { order.Add(SIGLA_QUERY + ".vlVenda DESC"); } break; case CAMPOS.QTPARCELAS: if (orderby == 0) { order.Add(SIGLA_QUERY + ".qtParcelas ASC"); } else { order.Add(SIGLA_QUERY + ".qtParcelas DESC"); } break; case CAMPOS.CDERP: if (orderby == 0) { order.Add(SIGLA_QUERY + ".cdERP ASC"); } else { order.Add(SIGLA_QUERY + ".cdERP DESC"); } break; case CAMPOS.CDSACADO: if (orderby == 0) { order.Add(SIGLA_QUERY + ".cdSacado ASC"); } else { order.Add(SIGLA_QUERY + ".cdSacado DESC"); } break; case CAMPOS.DTAJUSTE: if (orderby == 0) { order.Add(SIGLA_QUERY + ".dtAjuste ASC"); } else { order.Add(SIGLA_QUERY + ".dtAjuste DESC"); } break; case CAMPOS.DSMENSAGEM: if (orderby == 0) { order.Add(SIGLA_QUERY + ".dsMensagem ASC"); } else { order.Add(SIGLA_QUERY + ".dsMensagem DESC"); } break; } #endregion return(new SimpleDataBaseQuery(null, "card.tbRecebimentoVenda " + SIGLA_QUERY, join, where.ToArray(), null, order.ToArray())); }