using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using fatd_faturamento.DAL.Conexao;
using fatd_faturamento.ENTIDADE;

namespace fatd_faturamento.DAL.Persistencia
{
    public class dalNf
    {

        string vFILTER = null;
        string vINNERJOIN = null;
        string vAND = null;
        string vORDERBY = null;

        bool blnTabNf;
        bool lstrFilPref;
        string vTabela = null;

        #region Métodos

        public void Insert(entidadeNf objNf)
        {
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vAND = vAND + " AND  NFTRANSICAO.MESANO_REF = ?MESANO_REF ";

                    if (!string.IsNullOrEmpty(objNf.ROTA))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        //{ vAND = vAND + " AND MID(NFTRANSICAO.ROTA,1,4) IN (" + objNf.ROTA.ToString().ToUpper() + ") "; }
                        { vAND = vAND + " AND NFTRANSICAO.ROTA IN (" + objNf.ROTA.ToString().ToUpper() + ") "; }
                        else
                        //{ vAND = " AND MID(NFTRANSICAO.ROTA,1,4) IN (" + objNf.ROTA.ToString().ToUpper() + ") "; }
                        { vAND = " AND NFTRANSICAO.ROTA IN (" + objNf.ROTA.ToString().ToUpper() + ") "; }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }


                    if (objNf.ZONA.Trim().Length > 0 && objNf.LIVRO.Trim().Length > 0 && objNf.CONTA.Trim().Length > 0)
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        {
                            vAND = vAND + " AND NFTRANSICAO.ZONA = ?ZONA ";
                            vAND = vAND + " AND NFTRANSICAO.LIVRO = ?LIVRO ";
                            vAND = vAND + " AND NFTRANSICAO.CONTA = ?CONTA ";
                        }
                        else
                        {
                            vAND = vAND + " AND NFTRANSICAO.ZONA = ?ZONA ";
                            vAND = vAND + " AND NFTRANSICAO.LIVRO = ?LIVRO ";
                            vAND = vAND + " AND NFTRANSICAO.CONTA = ?CONTA ";
                        }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }


                    if (objNf.MICRO_GERACAO != 2)
                    {
                        if (objNf.MICRO_GERACAO == 1 && objNf.MICRO_GERACAO_DISTRIBUIDA == 1)
                        {
                            vAND = vAND + " AND (NFTRANSICAO.MICRO_GERACAO = 1 OR NFTRANSICAO.MICRO_GERACAO_DISTRIBUIDA = 1) ";
                        }
                        else
                        {
                            vAND = vAND + " AND NFTRANSICAO.MICRO_GERACAO = 0 ";
                            vAND = vAND + " AND NFTRANSICAO.MICRO_GERACAO_DISTRIBUIDA = 0 ";
                        }
                    }                    

                    vFILTER = vFILTER + "INSERT INTO NF (SELECT * FROM NFTRANSICAO WHERE NFTRANSICAO.MESANO_REF = ?MESANO_REF " + vAND + " ) ";

                    vORDERBY = " ";

                    vAND = vFILTER;

                    cmd = new MySqlCommand(this.QueryExecute);

                    cmd.Parameters.AddWithValue("?MESANO_REF", objNf.MESANO_REF);

                    if (!string.IsNullOrEmpty(objNf.ROTA))
                    {
                        cmd.Parameters.AddWithValue("?ROTA", objNf.ROTA.ToString().ToUpper());
                    }

                    if (objNf.ZONA.Trim().Length > 0 && objNf.LIVRO.Trim().Length > 0 && objNf.CONTA.Trim().Length > 0)
                    {

                        cmd.Parameters.AddWithValue("?ZONA", objNf.ZONA.ToString().ToUpper());

                        cmd.Parameters.AddWithValue("?LIVRO", objNf.LIVRO.ToString().ToUpper());

                        cmd.Parameters.AddWithValue("?CONTA", objNf.CONTA.ToString().ToUpper());

                    }

                    objGerenciadorTransacao.Execute(cmd);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public void Update(entidadeNf objNf)
        {
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vFILTER = vFILTER + "       FLGST      = ?FLGST , ";
                    vFILTER = vFILTER + "       DATAST     = ?DATAST , ";
                    vFILTER = vFILTER + "       MOTIVOCANC = ?MOTIVOCANC ";

                    vAND = vAND + " 	AND NUMNF  = ?NUMNF ";
                    vAND = vAND + "   AND MESANO_REF = ?MESANO_REF ";


                    cmd = new MySqlCommand(this.QueryUpdate);


                    cmd.Parameters.AddWithValue("?FLGST", objNf.FLGST.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?DATAST", objNf.DATAST);
                    cmd.Parameters.AddWithValue("?MOTIVOCANC", objNf.MOTIVOCANC.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?MESANO_REF", objNf.MESANO_REF);
                    cmd.Parameters.AddWithValue("?NUMNF", objNf.NUMNF);



                    objGerenciadorTransacao.Execute(cmd);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public void Update_CodBarras(entidadeNf objNf)
        {
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vFILTER = vFILTER + "       CODBARRA         = ?CODBARRA , ";
                    vFILTER = vFILTER + "       DVCODBARRA       = ?DVCODBARRA , ";
                    vFILTER = vFILTER + "       CODBARRA_IMAGEM  = ?CODBARRA_IMAGEM ";

                    vAND = vAND + "   AND NUMNF      = ?NUMNF ";
                    vAND = vAND + "   AND MESANO_REF = ?MESANO_REF ";


                    cmd = new MySqlCommand(this.QueryUpdate);


                    cmd.Parameters.AddWithValue("?CODBARRA", objNf.CODBARRA.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?DVCODBARRA", objNf.DVCODBARRA.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?CODBARRA_IMAGEM", objNf.CODBARRA_IMAGEM.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?MESANO_REF", objNf.MESANO_REF);
                    cmd.Parameters.AddWithValue("?NUMNF", objNf.NUMNF);



                    objGerenciadorTransacao.Execute(cmd);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public void Update_CodHash(entidadeNf objNf)
        {
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vFILTER = vFILTER + "       CODHASH     = ?CODHASH ";

                    vAND = vAND + " 	AND NUMNF  = ?NUMNF ";
                    vAND = vAND + "   AND MESANO_REF = ?MESANO_REF ";


                    cmd = new MySqlCommand(this.QueryUpdate);


                    cmd.Parameters.AddWithValue("?CODHASH", objNf.CODHASH.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?MESANO_REF", objNf.MESANO_REF);
                    cmd.Parameters.AddWithValue("?NUMNF", objNf.NUMNF);



                    objGerenciadorTransacao.Execute(cmd);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {
                    vFILTER = vFILTER + "NUMNF, ";
                    vFILTER = vFILTER + "CODCLAS, ";
                    vFILTER = vFILTER + "MESANO, ";
                    vFILTER = vFILTER + "MESANO_REF, ";
                    vFILTER = vFILTER + "DATAVENC, ";
                    vFILTER = vFILTER + "DATADIVIDA, ";
                    vFILTER = vFILTER + "DATALEIT, ";
                    vFILTER = vFILTER + "DATAAPR, ";
                    vFILTER = vFILTER + "DATAEM, ";
                    vFILTER = vFILTER + "DATALEITANT, ";
                    vFILTER = vFILTER + "DATAPAG, ";
                    vFILTER = vFILTER + "CODHASH, ";
                    vFILTER = vFILTER + "DATAST, ";
                    vFILTER = vFILTER + "FLGST, ";
                    vFILTER = vFILTER + "VALORTOT, ";
                    vFILTER = vFILTER + "ZONA, ";
                    vFILTER = vFILTER + "LIVRO, ";
                    vFILTER = vFILTER + "CONTA, ";
                    vFILTER = vFILTER + "ROTA, ";
                    vFILTER = vFILTER + "CONS_ID, ";
                    vFILTER = vFILTER + "UNID_ID, ";
                    vFILTER = vFILTER + "DATAPROXLEIT, ";
                    vFILTER = vFILTER + "BANARREC, ";
                    vFILTER = vFILTER + "BANIDENT, ";
                    vFILTER = vFILTER + "FLGMEDFAT, ";
                    vFILTER = vFILTER + "TIPOUC, ";
                    vFILTER = vFILTER + "CFOP, ";
                    vFILTER = vFILTER + "ITENSSERV, ";
                    vFILTER = vFILTER + "TA, ";
                    vFILTER = vFILTER + "GRUPO, ";
                    vFILTER = vFILTER + "FASE, ";
                    vFILTER = vFILTER + "TENSAO, ";
                    vFILTER = vFILTER + "NUM_MED, ";
                    vFILTER = vFILTER + "CONST_MED, ";
                    vFILTER = vFILTER + "MOTIVOCANC, ";
                    vFILTER = vFILTER + "BANCO_FATUR, ";
                    vFILTER = vFILTER + "SOCIAL, ";
                    vFILTER = vFILTER + "MULTA, ";
                    vFILTER = vFILTER + "DATAPRAZO, ";
                    vFILTER = vFILTER + "DIAS, ";
                    vFILTER = vFILTER + "VALORLIQ, ";
                    vFILTER = vFILTER + "NSU, ";
                    vFILTER = vFILTER + "CODBARRA, ";
                    vFILTER = vFILTER + "DVCODBARRA, ";
                    vFILTER = vFILTER + "AGRUPA, ";
                    vFILTER = vFILTER + "AGRUPA_ROTA, ";
                    vFILTER = vFILTER + "ITENSIMP, ";
                    vFILTER = vFILTER + "SGDENVIO, ";
                    vFILTER = vFILTER + "MOTIVOPGTO, ";
                    vFILTER = vFILTER + "MOTIVOSEGVIA, ";
                    vFILTER = vFILTER + "USUARIOSEGVIA, ";
                    vFILTER = vFILTER + "DATASEGVIA, ";
                    vFILTER = vFILTER + "CPF_CNPJ, ";
                    vFILTER = vFILTER + "PARCIALMENTE_CATIVO, ";
                    vFILTER = vFILTER + "LIVREESPECIAL, ";
                    vFILTER = vFILTER + "PERIODO_TESTE, ";
                    vFILTER = vFILTER + "MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO, ";
                    vFILTER = vFILTER + "IRRIGACAO_AQUICULTURA, ";
                    vFILTER = vFILTER + "AGRUPA_CADERNO, ";
                    vFILTER = vFILTER + "AGRUPA_SEQUENCIA, ";
                    vFILTER = vFILTER + "CADERNO, ";
                    vFILTER = vFILTER + "CADERNO_SEQUENCIA, ";
                    vFILTER = vFILTER + "ROTA_QUATRO, ";
                    vFILTER = vFILTER + "AGENCIA, ";
                    vFILTER = vFILTER + "CONTA_CORRENTE, ";
                    vFILTER = vFILTER + "LIVRE_VERDE, ";
                    vFILTER = vFILTER + "CANCELADA_MESANO, ";
                    vFILTER = vFILTER + "CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + "ENVIO_BCO, ";
                    vFILTER = vFILTER + "FATURA_FINAL, ";
                    vFILTER = vFILTER + "PAG_GOV, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_DISTRIBUIDA, ";
                    vFILTER = vFILTER + "USINA, ";
                    vFILTER = vFILTER + "SUPRIMENTO, ";
                    vFILTER = vFILTER + "BAIXA_NFV, ";
                    vFILTER = vFILTER + "SITE_ENVIO, ";
                    vFILTER = vFILTER + "TARIFA_BRANCA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_CONVENIO_SEFAZ, ";
                    vFILTER = vFILTER + "REGIAO, ";
                    vFILTER = vFILTER + "FATURA_COMPLEMENTAR  ";

                    vINNERJOIN = " ";

                    vORDERBY = " ";

                    if (objNf.MESANO_REF != 0)
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND MESANO_REF = ?MESANO_REF "; }
                        else
                        { vAND = "AND MESANO_REF = ?MESANO_REF "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }


                    if (objNf.CONS_ID != 0)
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND CONS_ID = ?CONS_ID "; }
                        else
                        { vAND = "AND CONS_ID = ?CONS_ID "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }


                    if (objNf.UNID_ID != 0)
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND UNID_ID = ?UNID_ID "; }
                        else
                        { vAND = "AND UNID_ID = ?UNID_ID "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }


                    if (!string.IsNullOrEmpty(objNf.ZONA))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND ZONA = ?ZONA "; }
                        else
                        { vAND = "AND ZONA = ?ZONA "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }

                    if (!string.IsNullOrEmpty(objNf.LIVRO))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND LIVRO = ?LIVRO "; }
                        else
                        { vAND = "AND LIVRO = ?LIVRO "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }


                    if (!string.IsNullOrEmpty(objNf.CONTA))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND CONTA = ?CONTA "; }
                        else
                        { vAND = "AND CONTA = ?CONTA "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }


                    if (!string.IsNullOrEmpty(objNf.GRUPO))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND GRUPO = ?GRUPO "; }
                        else
                        { vAND = "AND GRUPO = ?GRUPO "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }

                    cmd = new MySqlCommand(this.QuerySelect);

                    if (objNf.MESANO_REF != 0)
                    {
                        cmd.Parameters.AddWithValue("?MESANO_REF", objNf.MESANO_REF);
                    }

                    if (objNf.CONS_ID != 0)
                    {
                        cmd.Parameters.AddWithValue("?CONS_ID", objNf.CONS_ID);
                    }

                    if (objNf.UNID_ID != 0)
                    {
                        cmd.Parameters.AddWithValue("?UNID_ID", objNf.UNID_ID);
                    }

                    if (!string.IsNullOrEmpty(objNf.ZONA))
                    {
                        cmd.Parameters.AddWithValue("?ZONA", objNf.ZONA.ToString().ToUpper());
                    }

                    if (!string.IsNullOrEmpty(objNf.LIVRO))
                    {
                        cmd.Parameters.AddWithValue("?LIVRO", objNf.LIVRO.ToString().ToUpper());
                    }

                    if (!string.IsNullOrEmpty(objNf.CONTA))
                    {
                        cmd.Parameters.AddWithValue("?CONTA", objNf.CONTA.ToString().ToUpper());
                    }

                    if (!string.IsNullOrEmpty(objNf.GRUPO))
                    {
                        cmd.Parameters.AddWithValue("?GRUPO", objNf.GRUPO.ToString().ToUpper());
                    }


                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_1(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vFILTER = vFILTER + "NUMNF, ";
                    vFILTER = vFILTER + "CODCLAS, ";
                    vFILTER = vFILTER + "MESANO, ";
                    vFILTER = vFILTER + "MESANO_REF, ";
                    vFILTER = vFILTER + "DATAVENC, ";
                    vFILTER = vFILTER + "DATADIVIDA, ";
                    vFILTER = vFILTER + "DATALEIT, ";
                    vFILTER = vFILTER + "DATAAPR, ";
                    vFILTER = vFILTER + "DATAEM, ";
                    vFILTER = vFILTER + "DATALEITANT, ";
                    vFILTER = vFILTER + "DATAPAG, ";
                    vFILTER = vFILTER + "CODHASH, ";
                    vFILTER = vFILTER + "DATAST, ";
                    vFILTER = vFILTER + "FLGST, ";
                    vFILTER = vFILTER + "VALORTOT, ";
                    vFILTER = vFILTER + "ZONA, ";
                    vFILTER = vFILTER + "LIVRO, ";
                    vFILTER = vFILTER + "CONTA, ";
                    vFILTER = vFILTER + "ROTA, ";
                    vFILTER = vFILTER + "CONS_ID, ";
                    vFILTER = vFILTER + "UNID_ID, ";
                    vFILTER = vFILTER + "DATAPROXLEIT, ";
                    vFILTER = vFILTER + "BANARREC, ";
                    vFILTER = vFILTER + "BANIDENT, ";
                    vFILTER = vFILTER + "FLGMEDFAT, ";
                    vFILTER = vFILTER + "TIPOUC, ";
                    vFILTER = vFILTER + "CFOP, ";
                    vFILTER = vFILTER + "ITENSSERV, ";
                    vFILTER = vFILTER + "TA, ";
                    vFILTER = vFILTER + "GRUPO, ";
                    vFILTER = vFILTER + "FASE, ";
                    vFILTER = vFILTER + "TENSAO, ";
                    vFILTER = vFILTER + "NUM_MED, ";
                    vFILTER = vFILTER + "CONST_MED, ";
                    vFILTER = vFILTER + "MOTIVOCANC, ";
                    vFILTER = vFILTER + "BANCO_FATUR, ";
                    vFILTER = vFILTER + "SOCIAL, ";
                    vFILTER = vFILTER + "MULTA, ";
                    vFILTER = vFILTER + "DATAPRAZO, ";
                    vFILTER = vFILTER + "DIAS, ";
                    vFILTER = vFILTER + "VALORLIQ, ";
                    vFILTER = vFILTER + "NSU, ";
                    vFILTER = vFILTER + "CODBARRA, ";
                    vFILTER = vFILTER + "DVCODBARRA, ";
                    vFILTER = vFILTER + "AGRUPA, ";
                    vFILTER = vFILTER + "AGRUPA_ROTA, ";
                    vFILTER = vFILTER + "ITENSIMP, ";
                    vFILTER = vFILTER + "SGDENVIO, ";
                    vFILTER = vFILTER + "MOTIVOPGTO, ";
                    vFILTER = vFILTER + "MOTIVOSEGVIA, ";
                    vFILTER = vFILTER + "USUARIOSEGVIA, ";
                    vFILTER = vFILTER + "DATASEGVIA, ";
                    vFILTER = vFILTER + "CPF_CNPJ, ";
                    vFILTER = vFILTER + "PARCIALMENTE_CATIVO, ";
                    vFILTER = vFILTER + "LIVREESPECIAL, ";
                    vFILTER = vFILTER + "PERIODO_TESTE, ";
                    vFILTER = vFILTER + "MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO, ";
                    vFILTER = vFILTER + "IRRIGACAO_AQUICULTURA, ";
                    vFILTER = vFILTER + "AGRUPA_CADERNO, ";
                    vFILTER = vFILTER + "AGRUPA_SEQUENCIA, ";
                    vFILTER = vFILTER + "CADERNO, ";
                    vFILTER = vFILTER + "CADERNO_SEQUENCIA, ";
                    vFILTER = vFILTER + "ROTA_QUATRO, ";
                    vFILTER = vFILTER + "AGENCIA, ";
                    vFILTER = vFILTER + "CONTA_CORRENTE, ";
                    vFILTER = vFILTER + "LIVRE_VERDE, ";
                    vFILTER = vFILTER + "CANCELADA_MESANO, ";
                    vFILTER = vFILTER + "CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + "ENVIO_BCO, ";
                    vFILTER = vFILTER + "FATURA_FINAL, ";
                    vFILTER = vFILTER + "PAG_GOV, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_DISTRIBUIDA, ";
                    vFILTER = vFILTER + "USINA, ";
                    vFILTER = vFILTER + "SUPRIMENTO, ";
                    vFILTER = vFILTER + "BAIXA_NFV, ";
                    vFILTER = vFILTER + "SITE_ENVIO, ";
                    vFILTER = vFILTER + "TARIFA_BRANCA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_CONVENIO_SEFAZ, ";
                    vFILTER = vFILTER + "REGIAO, ";
                    vFILTER = vFILTER + "FATURA_COMPLEMENTAR  ";

                    vINNERJOIN = " ";

                    vORDERBY = " ";

                    if (!string.IsNullOrEmpty(vAND))
                    { vAND = vAND + "AND FLGST in (?FLGST) "; }
                    else
                    { vAND = "AND FLGST in (?FLGST) "; }

                    if (objNf.CONS_ID != 0)
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND CONS_ID = ?CONS_ID "; }
                        else
                        { vAND = "AND CONS_ID = ?CONS_ID "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }


                    if (objNf.UNID_ID != 0)
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND UNID_ID = ?UNID_ID "; }
                        else
                        { vAND = "AND UNID_ID = ?UNID_ID "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }


                    if (!string.IsNullOrEmpty(objNf.ZONA))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND ZONA = ?ZONA "; }
                        else
                        { vAND = "AND ZONA = ?ZONA "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }

                    if (!string.IsNullOrEmpty(objNf.LIVRO))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND LIVRO = ?LIVRO "; }
                        else
                        { vAND = "AND LIVRO = ?LIVRO "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }


                    if (!string.IsNullOrEmpty(objNf.CONTA))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND CONTA = ?CONTA "; }
                        else
                        { vAND = "AND CONTA = ?CONTA "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }


                    cmd = new MySqlCommand(this.QuerySelect);

                    cmd.Parameters.AddWithValue("?FLGST", objNf.FLGST.ToString().ToUpper());

                    if (objNf.CONS_ID != 0)
                    {
                        cmd.Parameters.AddWithValue("?CONS_ID", objNf.CONS_ID);
                    }

                    if (objNf.UNID_ID != 0)
                    {
                        cmd.Parameters.AddWithValue("?UNID_ID", objNf.UNID_ID);
                    }

                    if (!string.IsNullOrEmpty(objNf.ZONA))
                    {
                        cmd.Parameters.AddWithValue("?ZONA", objNf.ZONA.ToString().ToUpper());
                    }

                    if (!string.IsNullOrEmpty(objNf.LIVRO))
                    {
                        cmd.Parameters.AddWithValue("?LIVRO", objNf.LIVRO.ToString().ToUpper());
                    }

                    if (!string.IsNullOrEmpty(objNf.CONTA))
                    {
                        cmd.Parameters.AddWithValue("?CONTA", objNf.CONTA.ToString().ToUpper());
                    }


                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_2(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vFILTER = vFILTER + "NUMNF, ";
                    vFILTER = vFILTER + "CODCLAS, ";
                    vFILTER = vFILTER + "MESANO, ";
                    vFILTER = vFILTER + "MESANO_REF, ";
                    vFILTER = vFILTER + "DATAVENC, ";
                    vFILTER = vFILTER + "DATADIVIDA, ";
                    vFILTER = vFILTER + "DATALEIT, ";
                    vFILTER = vFILTER + "DATAAPR, ";
                    vFILTER = vFILTER + "DATAEM, ";
                    vFILTER = vFILTER + "DATALEITANT, ";
                    vFILTER = vFILTER + "DATAPAG, ";
                    vFILTER = vFILTER + "CODHASH, ";
                    vFILTER = vFILTER + "DATAST, ";
                    vFILTER = vFILTER + "FLGST, ";
                    vFILTER = vFILTER + "VALORTOT, ";
                    vFILTER = vFILTER + "ZONA, ";
                    vFILTER = vFILTER + "LIVRO, ";
                    vFILTER = vFILTER + "CONTA, ";
                    vFILTER = vFILTER + "ROTA, ";
                    vFILTER = vFILTER + "CONS_ID, ";
                    vFILTER = vFILTER + "UNID_ID, ";
                    vFILTER = vFILTER + "DATAPROXLEIT, ";
                    vFILTER = vFILTER + "BANARREC, ";
                    vFILTER = vFILTER + "BANIDENT, ";
                    vFILTER = vFILTER + "FLGMEDFAT, ";
                    vFILTER = vFILTER + "TIPOUC, ";
                    vFILTER = vFILTER + "CFOP, ";
                    vFILTER = vFILTER + "ITENSSERV, ";
                    vFILTER = vFILTER + "TA, ";
                    vFILTER = vFILTER + "GRUPO, ";
                    vFILTER = vFILTER + "FASE, ";
                    vFILTER = vFILTER + "TENSAO, ";
                    vFILTER = vFILTER + "NUM_MED, ";
                    vFILTER = vFILTER + "CONST_MED, ";
                    vFILTER = vFILTER + "MOTIVOCANC, ";
                    vFILTER = vFILTER + "BANCO_FATUR, ";
                    vFILTER = vFILTER + "SOCIAL, ";
                    vFILTER = vFILTER + "MULTA, ";
                    vFILTER = vFILTER + "DATAPRAZO, ";
                    vFILTER = vFILTER + "DIAS, ";
                    vFILTER = vFILTER + "VALORLIQ, ";
                    vFILTER = vFILTER + "NSU, ";
                    vFILTER = vFILTER + "CODBARRA, ";
                    vFILTER = vFILTER + "DVCODBARRA, ";
                    vFILTER = vFILTER + "AGRUPA, ";
                    vFILTER = vFILTER + "AGRUPA_ROTA, ";
                    vFILTER = vFILTER + "ITENSIMP, ";
                    vFILTER = vFILTER + "SGDENVIO, ";
                    vFILTER = vFILTER + "MOTIVOPGTO, ";
                    vFILTER = vFILTER + "MOTIVOSEGVIA, ";
                    vFILTER = vFILTER + "USUARIOSEGVIA, ";
                    vFILTER = vFILTER + "DATASEGVIA, ";
                    vFILTER = vFILTER + "CPF_CNPJ, ";
                    vFILTER = vFILTER + "PARCIALMENTE_CATIVO, ";
                    vFILTER = vFILTER + "LIVREESPECIAL, ";
                    vFILTER = vFILTER + "PERIODO_TESTE, ";
                    vFILTER = vFILTER + "MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO, ";
                    vFILTER = vFILTER + "IRRIGACAO_AQUICULTURA, ";
                    vFILTER = vFILTER + "AGRUPA_CADERNO, ";
                    vFILTER = vFILTER + "AGRUPA_SEQUENCIA, ";
                    vFILTER = vFILTER + "CADERNO, ";
                    vFILTER = vFILTER + "CADERNO_SEQUENCIA, ";
                    vFILTER = vFILTER + "ROTA_QUATRO, ";
                    vFILTER = vFILTER + "AGENCIA, ";
                    vFILTER = vFILTER + "CONTA_CORRENTE, ";
                    vFILTER = vFILTER + "LIVRE_VERDE, ";
                    vFILTER = vFILTER + "CANCELADA_MESANO, ";
                    vFILTER = vFILTER + "CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + "ENVIO_BCO, ";
                    vFILTER = vFILTER + "FATURA_FINAL, ";
                    vFILTER = vFILTER + "PAG_GOV, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_DISTRIBUIDA, ";
                    vFILTER = vFILTER + "USINA, ";
                    vFILTER = vFILTER + "SUPRIMENTO, ";
                    vFILTER = vFILTER + "BAIXA_NFV, ";
                    vFILTER = vFILTER + "SITE_ENVIO, ";
                    vFILTER = vFILTER + "TARIFA_BRANCA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_CONVENIO_SEFAZ, ";
                    vFILTER = vFILTER + "REGIAO, ";
                    vFILTER = vFILTER + "FATURA_COMPLEMENTAR  ";

                    vINNERJOIN = " ";

                    vORDERBY = " ";


                    vAND = vAND + "  AND MESANO_REF = ?MESANO_REF ";
                    vAND = vAND + "  AND GRUPO     = 'A' ";
                    vAND = vAND + "  AND FATURA_COMPLEMENTAR = 0 ";


                    cmd = new MySqlCommand(this.QuerySelect);

                    cmd.Parameters.AddWithValue("?MESANO_REF", objNf.MESANO_REF);


                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_3(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vFILTER = vFILTER + "NUMNF, ";
                    vFILTER = vFILTER + "CODCLAS, ";
                    vFILTER = vFILTER + "MESANO, ";
                    vFILTER = vFILTER + "MESANO_REF, ";
                    vFILTER = vFILTER + "DATAVENC, ";
                    vFILTER = vFILTER + "DATADIVIDA, ";
                    vFILTER = vFILTER + "DATALEIT, ";
                    vFILTER = vFILTER + "DATAAPR, ";
                    vFILTER = vFILTER + "DATAEM, ";
                    vFILTER = vFILTER + "DATALEITANT, ";
                    vFILTER = vFILTER + "DATAPAG, ";
                    vFILTER = vFILTER + "CODHASH, ";
                    vFILTER = vFILTER + "DATAST, ";
                    vFILTER = vFILTER + "FLGST, ";
                    vFILTER = vFILTER + "VALORTOT, ";
                    vFILTER = vFILTER + "ZONA, ";
                    vFILTER = vFILTER + "LIVRO, ";
                    vFILTER = vFILTER + "CONTA, ";
                    vFILTER = vFILTER + "ROTA, ";
                    vFILTER = vFILTER + "CONS_ID, ";
                    vFILTER = vFILTER + "UNID_ID, ";
                    vFILTER = vFILTER + "DATAPROXLEIT, ";
                    vFILTER = vFILTER + "BANARREC, ";
                    vFILTER = vFILTER + "BANIDENT, ";
                    vFILTER = vFILTER + "FLGMEDFAT, ";
                    vFILTER = vFILTER + "TIPOUC, ";
                    vFILTER = vFILTER + "CFOP, ";
                    vFILTER = vFILTER + "ITENSSERV, ";
                    vFILTER = vFILTER + "TA, ";
                    vFILTER = vFILTER + "GRUPO, ";
                    vFILTER = vFILTER + "FASE, ";
                    vFILTER = vFILTER + "TENSAO, ";
                    vFILTER = vFILTER + "NUM_MED, ";
                    vFILTER = vFILTER + "CONST_MED, ";
                    vFILTER = vFILTER + "MOTIVOCANC, ";
                    vFILTER = vFILTER + "BANCO_FATUR, ";
                    vFILTER = vFILTER + "SOCIAL, ";
                    vFILTER = vFILTER + "MULTA, ";
                    vFILTER = vFILTER + "DATAPRAZO, ";
                    vFILTER = vFILTER + "DIAS, ";
                    vFILTER = vFILTER + "VALORLIQ, ";
                    vFILTER = vFILTER + "NSU, ";
                    vFILTER = vFILTER + "CODBARRA, ";
                    vFILTER = vFILTER + "DVCODBARRA, ";
                    vFILTER = vFILTER + "AGRUPA, ";
                    vFILTER = vFILTER + "AGRUPA_ROTA, ";
                    vFILTER = vFILTER + "ITENSIMP, ";
                    vFILTER = vFILTER + "SGDENVIO, ";
                    vFILTER = vFILTER + "MOTIVOPGTO, ";
                    vFILTER = vFILTER + "MOTIVOSEGVIA, ";
                    vFILTER = vFILTER + "USUARIOSEGVIA, ";
                    vFILTER = vFILTER + "DATASEGVIA, ";
                    vFILTER = vFILTER + "CPF_CNPJ, ";
                    vFILTER = vFILTER + "PARCIALMENTE_CATIVO, ";
                    vFILTER = vFILTER + "LIVREESPECIAL, ";
                    vFILTER = vFILTER + "PERIODO_TESTE, ";
                    vFILTER = vFILTER + "MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO, ";
                    vFILTER = vFILTER + "IRRIGACAO_AQUICULTURA, ";
                    vFILTER = vFILTER + "AGRUPA_CADERNO, ";
                    vFILTER = vFILTER + "AGRUPA_SEQUENCIA, ";
                    vFILTER = vFILTER + "CADERNO, ";
                    vFILTER = vFILTER + "CADERNO_SEQUENCIA, ";
                    vFILTER = vFILTER + "ROTA_QUATRO, ";
                    vFILTER = vFILTER + "AGENCIA, ";
                    vFILTER = vFILTER + "CONTA_CORRENTE, ";
                    vFILTER = vFILTER + "LIVRE_VERDE, ";
                    vFILTER = vFILTER + "CANCELADA_MESANO, ";
                    vFILTER = vFILTER + "CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + "ENVIO_BCO, ";
                    vFILTER = vFILTER + "FATURA_FINAL, ";
                    vFILTER = vFILTER + "PAG_GOV, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_DISTRIBUIDA, ";
                    vFILTER = vFILTER + "USINA, ";
                    vFILTER = vFILTER + "SUPRIMENTO, ";
                    vFILTER = vFILTER + "BAIXA_NFV, ";
                    vFILTER = vFILTER + "SITE_ENVIO, ";
                    vFILTER = vFILTER + "TARIFA_BRANCA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_CONVENIO_SEFAZ, ";
                    vFILTER = vFILTER + "REGIAO, ";
                    vFILTER = vFILTER + "FATURA_COMPLEMENTAR  ";

                    vINNERJOIN = " ";

                    vORDERBY = " ";


                    vAND = vAND + "  AND MESANO_REF = ?MESANO_REF ";
                    vAND = vAND + "  AND GRUPO     = 'B' ";
                    vAND = vAND + "  AND (FLGMEDFAT <> 'S' OR FLGMEDFAT IS NULL)  AND FATURA_COMPLEMENTAR = 0 ";


                    cmd = new MySqlCommand(this.QuerySelect);

                    cmd.Parameters.AddWithValue("?MESANO_REF", objNf.MESANO_REF);


                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_4(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vFILTER = vFILTER + "       NF.MESANO_REF, ";
                    vFILTER = vFILTER + "       NF.MESANO, ";
                    vFILTER = vFILTER + "       NF.NUMNF, ";
                    vFILTER = vFILTER + "       NF.ZONA, ";
                    vFILTER = vFILTER + "       NF.LIVRO, ";
                    vFILTER = vFILTER + "       NF.CONTA, ";
                    vFILTER = vFILTER + "       NF.UNID_ID, ";
                    vFILTER = vFILTER + "       NF.CONS_ID, ";
                    vFILTER = vFILTER + "       NF.GRUPO, ";
                    vFILTER = vFILTER + "       NF.TA, ";
                    vFILTER = vFILTER + "       NF.DATAPAG, ";
                    vFILTER = vFILTER + "       NF.DATAEM, ";
                    vFILTER = vFILTER + "       NF.DATAVENC, ";
                    vFILTER = vFILTER + "       NF.FLGST, ";
                    vFILTER = vFILTER + "       NF.VALORLIQ, ";
                    vFILTER = vFILTER + "       NF.GRUPO, ";
                    vFILTER = vFILTER + "       IFNULL(IFNULL(UC_1.NOME,UC_2.NOME), CONSUM.NOME) AS NOME ";


                    vINNERJOIN = vINNERJOIN + " LEFT JOIN UC_FATURAMENTO UC_1 ON(UC_1.CONS_ID = NF.CONS_ID AND UC_1.UNID_ID = NF.UNID_ID AND NF.GRUPO = 'A')";
                    vINNERJOIN = vINNERJOIN + " LEFT JOIN UC_FATURAMENTO UC_2 ON(UC_2.CONS_ID = NF.CONS_ID AND UC_2.UNID_ID = NF.UNID_ID AND NF.GRUPO = 'B')";
                    vINNERJOIN = vINNERJOIN + " LEFT JOIN CONSUM ON(CONSUM.CONSREG = NF.CONS_ID)";


                    vORDERBY = vORDERBY + " LIMIT 0, 1";


                    vAND = vAND + "   AND NF.MESANO_REF = ?MESANO_REF ";
                    vAND = vAND + "   AND NF.NUMNF      = ?NUMNF ";


                    cmd = new MySqlCommand(this.QuerySelect);

                    cmd.Parameters.AddWithValue("?MESANO_REF", objNf.MESANO_REF);
                    cmd.Parameters.AddWithValue("?NUMNF", objNf.NUMNF);


                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_5(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vFILTER = vFILTER + "       NF.NUMNF, ";
                    vFILTER = vFILTER + "       NF.MESANO, ";
                    vFILTER = vFILTER + "       NF.MESANO_REF, ";
                    vFILTER = vFILTER + "       NF.CODCLAS, ";
                    vFILTER = vFILTER + "       NF.ITENSSERV, ";
                    vFILTER = vFILTER + "       NF.DATALEIT, ";
                    vFILTER = vFILTER + "       NF.DATALEITANT, ";
                    vFILTER = vFILTER + "       RESSARCIMENTO_NIVEL_TENSAO.KEI, ";
                    vFILTER = vFILTER + "       RESSARCIMENTO_NIVEL_TENSAO.LA_INICIAL, ";
                    vFILTER = vFILTER + "       RESSARCIMENTO_NIVEL_TENSAO.LA_FINAL, ";
                    vFILTER = vFILTER + "       RESSARCIMENTO_NIVEL_TENSAO.LA_PROCEDENTE, ";
                    vFILTER = vFILTER + "       RESSARCIMENTO_NIVEL_TENSAO.RE_EUSD, ";
                    vFILTER = vFILTER + "       RESSARCIMENTO_NIVEL_TENSAO.RE_DRP, ";
                    vFILTER = vFILTER + "       RESSARCIMENTO_NIVEL_TENSAO.RE_DRPM, ";
                    vFILTER = vFILTER + "       RESSARCIMENTO_NIVEL_TENSAO.RE_K1, ";
                    vFILTER = vFILTER + "       RESSARCIMENTO_NIVEL_TENSAO.RE_DRC, ";
                    vFILTER = vFILTER + "       RESSARCIMENTO_NIVEL_TENSAO.RE_DRCM, ";
                    vFILTER = vFILTER + "       RESSARCIMENTO_NIVEL_TENSAO.RE_K2, ";
                    vFILTER = vFILTER + "       RESSARCIMENTO_NIVEL_TENSAO.RE_PARCIAL, ";
                    vFILTER = vFILTER + "       RESSARCIMENTO_NIVEL_TENSAO.RE_CREDITO, ";
                    vFILTER = vFILTER + "       RESSARCIMENTO_NIVEL_TENSAO.GERADO, ";
                    vFILTER = vFILTER + "       RESSARCIMENTO_NIVEL_TENSAO.PROCESSADO ";

                    vINNERJOIN = vINNERJOIN + "  LEFT JOIN RESSARCIMENTO_NIVEL_TENSAO ON(RESSARCIMENTO_NIVEL_TENSAO.NF_NUMNF = NF.NUMNF AND RESSARCIMENTO_NIVEL_TENSAO.NF_MESANO = NF.MESANO) ";

                    vORDERBY = " ";

                    vAND = vAND + " AND NF.MESANO_REF >= ?MESANO_REF ";
                    vAND = vAND + "   AND NF.CONS_ID = ?CONS_ID ";
                    vAND = vAND + "   AND NF.UNID_ID =  ?UNID_ID ";
                    vAND = vAND + "   AND NF.GRUPO = ?GRUPO ";

                    if (objNf.AGRUPA != 0)
                    {
                        vAND = vAND + "  AND NF.MESANO_REF <= ?AGRUPA ";
                    }



                    cmd = new MySqlCommand(this.QuerySelect);

                    cmd.Parameters.AddWithValue("?MESANO_REF", objNf.MESANO_REF);
                    cmd.Parameters.AddWithValue("?CONS_ID", objNf.CONS_ID);
                    cmd.Parameters.AddWithValue("?UNID_ID", objNf.UNID_ID);
                    cmd.Parameters.AddWithValue("?GRUPO", objNf.GRUPO.ToString().ToUpper());

                    if (objNf.AGRUPA != 0)
                    {
                        cmd.Parameters.AddWithValue("?AGRUPA", objNf.AGRUPA);
                    }


                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_6(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vFILTER = vFILTER + "       ITENSSERV,  ";
                    vFILTER = vFILTER + "       MESANO, ";
                    vFILTER = vFILTER + "       MESANO_REF, ";
                    vFILTER = vFILTER + "       TA, ";
                    vFILTER = vFILTER + "       UNID_ID, ";
                    vFILTER = vFILTER + "       CONS_ID ";

                    vINNERJOIN = " ";

                    vORDERBY = vORDERBY + " ORDER BY MESANO_REF DESC ";
                    vORDERBY = vORDERBY + " LIMIT 0, 12 ";

                    vAND = vAND + " 	AND UNID_ID =  ?UNID_ID ";
                    vAND = vAND + "   AND CONS_ID = ?CONS_ID ";
                    vAND = vAND + "   AND MID(TA,3,1) = 'B' ";
                    vAND = vAND + "   AND FLGST <> 'C' ";



                    cmd = new MySqlCommand(this.QuerySelect);


                    cmd.Parameters.AddWithValue("?CONS_ID", objNf.CONS_ID);
                    cmd.Parameters.AddWithValue("?UNID_ID", objNf.UNID_ID);

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_7(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vFILTER = vFILTER + "NUMNF, ";
                    vFILTER = vFILTER + "CODCLAS, ";
                    vFILTER = vFILTER + "MESANO, ";
                    vFILTER = vFILTER + "MESANO_REF, ";
                    vFILTER = vFILTER + "DATAVENC, ";
                    vFILTER = vFILTER + "DATADIVIDA, ";
                    vFILTER = vFILTER + "DATALEIT, ";
                    vFILTER = vFILTER + "DATAAPR, ";
                    vFILTER = vFILTER + "DATAEM, ";
                    vFILTER = vFILTER + "DATALEITANT, ";
                    vFILTER = vFILTER + "DATAPAG, ";
                    vFILTER = vFILTER + "CODHASH, ";
                    vFILTER = vFILTER + "DATAST, ";
                    vFILTER = vFILTER + "FLGST, ";
                    vFILTER = vFILTER + "VALORTOT, ";
                    vFILTER = vFILTER + "ZONA, ";
                    vFILTER = vFILTER + "LIVRO, ";
                    vFILTER = vFILTER + "CONTA, ";
                    vFILTER = vFILTER + "ROTA, ";
                    vFILTER = vFILTER + "CONS_ID, ";
                    vFILTER = vFILTER + "UNID_ID, ";
                    vFILTER = vFILTER + "DATAPROXLEIT, ";
                    vFILTER = vFILTER + "BANARREC, ";
                    vFILTER = vFILTER + "BANIDENT, ";
                    vFILTER = vFILTER + "FLGMEDFAT, ";
                    vFILTER = vFILTER + "TIPOUC, ";
                    vFILTER = vFILTER + "CFOP, ";
                    vFILTER = vFILTER + "ITENSSERV, ";
                    vFILTER = vFILTER + "TA, ";
                    vFILTER = vFILTER + "GRUPO, ";
                    vFILTER = vFILTER + "FASE, ";
                    vFILTER = vFILTER + "TENSAO, ";
                    vFILTER = vFILTER + "NUM_MED, ";
                    vFILTER = vFILTER + "CONST_MED, ";
                    vFILTER = vFILTER + "MOTIVOCANC, ";
                    vFILTER = vFILTER + "BANCO_FATUR, ";
                    vFILTER = vFILTER + "SOCIAL, ";
                    vFILTER = vFILTER + "MULTA, ";
                    vFILTER = vFILTER + "DATAPRAZO, ";
                    vFILTER = vFILTER + "DIAS, ";
                    vFILTER = vFILTER + "VALORLIQ, ";
                    vFILTER = vFILTER + "NSU, ";
                    vFILTER = vFILTER + "CODBARRA, ";
                    vFILTER = vFILTER + "DVCODBARRA, ";
                    vFILTER = vFILTER + "AGRUPA, ";
                    vFILTER = vFILTER + "AGRUPA_ROTA, ";
                    vFILTER = vFILTER + "ITENSIMP, ";
                    vFILTER = vFILTER + "SGDENVIO, ";
                    vFILTER = vFILTER + "MOTIVOPGTO, ";
                    vFILTER = vFILTER + "MOTIVOSEGVIA, ";
                    vFILTER = vFILTER + "USUARIOSEGVIA, ";
                    vFILTER = vFILTER + "DATASEGVIA, ";
                    vFILTER = vFILTER + "CPF_CNPJ, ";
                    vFILTER = vFILTER + "PARCIALMENTE_CATIVO, ";
                    vFILTER = vFILTER + "LIVREESPECIAL, ";
                    vFILTER = vFILTER + "PERIODO_TESTE, ";
                    vFILTER = vFILTER + "MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO, ";
                    vFILTER = vFILTER + "IRRIGACAO_AQUICULTURA, ";
                    vFILTER = vFILTER + "AGRUPA_CADERNO, ";
                    vFILTER = vFILTER + "AGRUPA_SEQUENCIA, ";
                    vFILTER = vFILTER + "CADERNO, ";
                    vFILTER = vFILTER + "CADERNO_SEQUENCIA, ";
                    vFILTER = vFILTER + "ROTA_QUATRO, ";
                    vFILTER = vFILTER + "AGENCIA, ";
                    vFILTER = vFILTER + "CONTA_CORRENTE, ";
                    vFILTER = vFILTER + "LIVRE_VERDE, ";
                    vFILTER = vFILTER + "CANCELADA_MESANO, ";
                    vFILTER = vFILTER + "CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + "ENVIO_BCO, ";
                    vFILTER = vFILTER + "FATURA_FINAL, ";
                    vFILTER = vFILTER + "PAG_GOV, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_DISTRIBUIDA, ";
                    vFILTER = vFILTER + "USINA, ";
                    vFILTER = vFILTER + "SUPRIMENTO, ";
                    vFILTER = vFILTER + "BAIXA_NFV, ";
                    vFILTER = vFILTER + "SITE_ENVIO, ";
                    vFILTER = vFILTER + "TARIFA_BRANCA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_CONVENIO_SEFAZ, ";
                    vFILTER = vFILTER + "REGIAO, ";
                    vFILTER = vFILTER + "FATURA_COMPLEMENTAR  ";

                    vINNERJOIN = " ";

                    vORDERBY = "ORDER BY ANOMES_FAT DESC LIMIT 0, 12 ";

                    vAND = vAND + "     AND ANOMES_FAT  < ?ANOMES_FAT ";
                    vAND = vAND + "     AND ZONA  = ?ZONA ";
                    vAND = vAND + "     AND LIVRO = ?LIVRO ";
                    vAND = vAND + "     AND CONTA = ?CONTA ";
                    vAND = vAND + "     AND CONS_ID = ?CONS_ID ";
                    vAND = vAND + "     AND UNID_ID = ?UNID_ID ";
                    vAND = vAND + "     AND FLGST <> 'C' ";

                    cmd = new MySqlCommand(this.QuerySelect);

                    cmd.Parameters.AddWithValue("?CONS_ID", objNf.CONS_ID);
                    cmd.Parameters.AddWithValue("?UNID_ID", objNf.UNID_ID);
                    cmd.Parameters.AddWithValue("?ZONA", objNf.ZONA.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?LIVRO", objNf.LIVRO.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?CONTA", objNf.CONTA.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?ANOMES_FAT", objNf.ANOMES_FAT.ToString().ToUpper());


                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_8(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vFILTER = vFILTER + "NUMNF, ";
                    vFILTER = vFILTER + "CODCLAS, ";
                    vFILTER = vFILTER + "MESANO, ";
                    vFILTER = vFILTER + "MESANO_REF, ";
                    vFILTER = vFILTER + "DATAVENC, ";
                    vFILTER = vFILTER + "DATADIVIDA, ";
                    vFILTER = vFILTER + "DATALEIT, ";
                    vFILTER = vFILTER + "DATAAPR, ";
                    vFILTER = vFILTER + "DATAEM, ";
                    vFILTER = vFILTER + "DATALEITANT, ";
                    vFILTER = vFILTER + "DATAPAG, ";
                    vFILTER = vFILTER + "CODHASH, ";
                    vFILTER = vFILTER + "DATAST, ";
                    vFILTER = vFILTER + "FLGST, ";
                    vFILTER = vFILTER + "VALORTOT, ";
                    vFILTER = vFILTER + "ZONA, ";
                    vFILTER = vFILTER + "LIVRO, ";
                    vFILTER = vFILTER + "CONTA, ";
                    vFILTER = vFILTER + "ROTA, ";
                    vFILTER = vFILTER + "CONS_ID, ";
                    vFILTER = vFILTER + "UNID_ID, ";
                    vFILTER = vFILTER + "DATAPROXLEIT, ";
                    vFILTER = vFILTER + "BANARREC, ";
                    vFILTER = vFILTER + "BANIDENT, ";
                    vFILTER = vFILTER + "FLGMEDFAT, ";
                    vFILTER = vFILTER + "TIPOUC, ";
                    vFILTER = vFILTER + "CFOP, ";
                    vFILTER = vFILTER + "ITENSSERV, ";
                    vFILTER = vFILTER + "TA, ";
                    vFILTER = vFILTER + "GRUPO, ";
                    vFILTER = vFILTER + "FASE, ";
                    vFILTER = vFILTER + "TENSAO, ";
                    vFILTER = vFILTER + "NUM_MED, ";
                    vFILTER = vFILTER + "CONST_MED, ";
                    vFILTER = vFILTER + "MOTIVOCANC, ";
                    vFILTER = vFILTER + "BANCO_FATUR, ";
                    vFILTER = vFILTER + "SOCIAL, ";
                    vFILTER = vFILTER + "MULTA, ";
                    vFILTER = vFILTER + "DATAPRAZO, ";
                    vFILTER = vFILTER + "DIAS, ";
                    vFILTER = vFILTER + "VALORLIQ, ";
                    vFILTER = vFILTER + "NSU, ";
                    vFILTER = vFILTER + "CODBARRA, ";
                    vFILTER = vFILTER + "DVCODBARRA, ";
                    vFILTER = vFILTER + "AGRUPA, ";
                    vFILTER = vFILTER + "AGRUPA_ROTA, ";
                    vFILTER = vFILTER + "ITENSIMP, ";
                    vFILTER = vFILTER + "SGDENVIO, ";
                    vFILTER = vFILTER + "MOTIVOPGTO, ";
                    vFILTER = vFILTER + "MOTIVOSEGVIA, ";
                    vFILTER = vFILTER + "USUARIOSEGVIA, ";
                    vFILTER = vFILTER + "DATASEGVIA, ";
                    vFILTER = vFILTER + "CPF_CNPJ, ";
                    vFILTER = vFILTER + "PARCIALMENTE_CATIVO, ";
                    vFILTER = vFILTER + "LIVREESPECIAL, ";
                    vFILTER = vFILTER + "PERIODO_TESTE, ";
                    vFILTER = vFILTER + "MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO, ";
                    vFILTER = vFILTER + "IRRIGACAO_AQUICULTURA, ";
                    vFILTER = vFILTER + "AGRUPA_CADERNO, ";
                    vFILTER = vFILTER + "AGRUPA_SEQUENCIA, ";
                    vFILTER = vFILTER + "CADERNO, ";
                    vFILTER = vFILTER + "CADERNO_SEQUENCIA, ";
                    vFILTER = vFILTER + "ROTA_QUATRO, ";
                    vFILTER = vFILTER + "AGENCIA, ";
                    vFILTER = vFILTER + "CONTA_CORRENTE, ";
                    vFILTER = vFILTER + "LIVRE_VERDE, ";
                    vFILTER = vFILTER + "CANCELADA_MESANO, ";
                    vFILTER = vFILTER + "CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + "ENVIO_BCO, ";
                    vFILTER = vFILTER + "FATURA_FINAL, ";
                    vFILTER = vFILTER + "PAG_GOV, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_DISTRIBUIDA, ";
                    vFILTER = vFILTER + "USINA, ";
                    vFILTER = vFILTER + "SUPRIMENTO, ";
                    vFILTER = vFILTER + "BAIXA_NFV, ";
                    vFILTER = vFILTER + "SITE_ENVIO, ";
                    vFILTER = vFILTER + "TARIFA_BRANCA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_CONVENIO_SEFAZ, ";
                    vFILTER = vFILTER + "REGIAO, ";
                    vFILTER = vFILTER + "FATURA_COMPLEMENTAR  ";

                    vINNERJOIN = " ";

                    vORDERBY = "GROUP BY 1 ";


                    vAND = vAND + "   AND  MESANO = ?MESANO ";
                    vAND = vAND + "   AND GRUPO = ?GRUPO ";
                    vAND = vAND + "   AND FATURA_COMPLEMENTAR " + objNf.AGENCIA.ToString() + " ?FATURA_COMPLEMENTAR ";


                    cmd = new MySqlCommand(this.QuerySelect);


                    cmd.Parameters.AddWithValue("?MESANO", objNf.MESANO);
                    cmd.Parameters.AddWithValue("?GRUPO", objNf.GRUPO.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?FATURA_COMPLEMENTAR", objNf.FATURA_COMPLEMENTAR);

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_9(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vFILTER = vFILTER + "NUMNF, ";
                    vFILTER = vFILTER + "CODCLAS, ";
                    vFILTER = vFILTER + "MESANO, ";
                    vFILTER = vFILTER + "MESANO_REF, ";
                    vFILTER = vFILTER + "DATAVENC, ";
                    vFILTER = vFILTER + "DATADIVIDA, ";
                    vFILTER = vFILTER + "DATALEIT, ";
                    vFILTER = vFILTER + "DATAAPR, ";
                    vFILTER = vFILTER + "DATAEM, ";
                    vFILTER = vFILTER + "DATALEITANT, ";
                    vFILTER = vFILTER + "DATAPAG, ";
                    vFILTER = vFILTER + "CODHASH, ";
                    vFILTER = vFILTER + "DATAST, ";
                    vFILTER = vFILTER + "FLGST, ";
                    vFILTER = vFILTER + "VALORTOT, ";
                    vFILTER = vFILTER + "ZONA, ";
                    vFILTER = vFILTER + "LIVRO, ";
                    vFILTER = vFILTER + "CONTA, ";
                    vFILTER = vFILTER + "ROTA, ";
                    vFILTER = vFILTER + "CONS_ID, ";
                    vFILTER = vFILTER + "UNID_ID, ";
                    vFILTER = vFILTER + "DATAPROXLEIT, ";
                    vFILTER = vFILTER + "BANARREC, ";
                    vFILTER = vFILTER + "BANIDENT, ";
                    vFILTER = vFILTER + "FLGMEDFAT, ";
                    vFILTER = vFILTER + "TIPOUC, ";
                    vFILTER = vFILTER + "CFOP, ";
                    vFILTER = vFILTER + "ITENSSERV, ";
                    vFILTER = vFILTER + "TA, ";
                    vFILTER = vFILTER + "GRUPO, ";
                    vFILTER = vFILTER + "FASE, ";
                    vFILTER = vFILTER + "TENSAO, ";
                    vFILTER = vFILTER + "NUM_MED, ";
                    vFILTER = vFILTER + "CONST_MED, ";
                    vFILTER = vFILTER + "MOTIVOCANC, ";
                    vFILTER = vFILTER + "BANCO_FATUR, ";
                    vFILTER = vFILTER + "SOCIAL, ";
                    vFILTER = vFILTER + "MULTA, ";
                    vFILTER = vFILTER + "DATAPRAZO, ";
                    vFILTER = vFILTER + "DIAS, ";
                    vFILTER = vFILTER + "VALORLIQ, ";
                    vFILTER = vFILTER + "NSU, ";
                    vFILTER = vFILTER + "CODBARRA, ";
                    vFILTER = vFILTER + "DVCODBARRA, ";
                    vFILTER = vFILTER + "AGRUPA, ";
                    vFILTER = vFILTER + "AGRUPA_ROTA, ";
                    vFILTER = vFILTER + "ITENSIMP, ";
                    vFILTER = vFILTER + "SGDENVIO, ";
                    vFILTER = vFILTER + "MOTIVOPGTO, ";
                    vFILTER = vFILTER + "MOTIVOSEGVIA, ";
                    vFILTER = vFILTER + "USUARIOSEGVIA, ";
                    vFILTER = vFILTER + "DATASEGVIA, ";
                    vFILTER = vFILTER + "CPF_CNPJ, ";
                    vFILTER = vFILTER + "PARCIALMENTE_CATIVO, ";
                    vFILTER = vFILTER + "LIVREESPECIAL, ";
                    vFILTER = vFILTER + "PERIODO_TESTE, ";
                    vFILTER = vFILTER + "MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO, ";
                    vFILTER = vFILTER + "IRRIGACAO_AQUICULTURA, ";
                    vFILTER = vFILTER + "AGRUPA_CADERNO, ";
                    vFILTER = vFILTER + "AGRUPA_SEQUENCIA, ";
                    vFILTER = vFILTER + "CADERNO, ";
                    vFILTER = vFILTER + "CADERNO_SEQUENCIA, ";
                    vFILTER = vFILTER + "ROTA_QUATRO, ";
                    vFILTER = vFILTER + "AGENCIA, ";
                    vFILTER = vFILTER + "CONTA_CORRENTE, ";
                    vFILTER = vFILTER + "LIVRE_VERDE, ";
                    vFILTER = vFILTER + "CANCELADA_MESANO, ";
                    vFILTER = vFILTER + "CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + "ENVIO_BCO, ";
                    vFILTER = vFILTER + "FATURA_FINAL, ";
                    vFILTER = vFILTER + "PAG_GOV, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_DISTRIBUIDA, ";
                    vFILTER = vFILTER + "USINA, ";
                    vFILTER = vFILTER + "SUPRIMENTO, ";
                    vFILTER = vFILTER + "BAIXA_NFV, ";
                    vFILTER = vFILTER + "SITE_ENVIO, ";
                    vFILTER = vFILTER + "TARIFA_BRANCA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_CONVENIO_SEFAZ, ";
                    vFILTER = vFILTER + "REGIAO, ";
                    vFILTER = vFILTER + "FATURA_COMPLEMENTAR  ";                    

                    vINNERJOIN = " ";

                    vORDERBY = "ORDER BY MESANO_REF DESC LIMIT 0, " + objNf.MESANO_REF;


                    vAND = vAND + "   AND ZONA    = ?ZONA ";
                    vAND = vAND + "   AND LIVRO   = ?LIVRO ";
                    vAND = vAND + "   AND CONTA   = ?CONTA ";
                    vAND = vAND + "   AND UNID_ID = ?UNID_ID ";
                    vAND = vAND + "   AND CONS_ID = ?CONS_ID ";
                    vAND = vAND + "   AND FLGST <> 'C' ";


                    cmd = new MySqlCommand(this.QuerySelect);


                    cmd.Parameters.AddWithValue("?ZONA", objNf.ZONA.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?LIVRO", objNf.LIVRO.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?CONTA", objNf.CONTA.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?UNID_ID", objNf.UNID_ID);
                    cmd.Parameters.AddWithValue("?CONS_ID", objNf.CONS_ID);

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_10(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {


                    vFILTER = vFILTER + "MESANO_REF, ";
                    vFILTER = vFILTER + " SUM(BUSCAVALORITEM(ITENSSERV, ?ITENSSERV)) AS ITENSSERV ";

                    vINNERJOIN = " ";

                    vORDERBY = "GROUP BY MESANO_REF  ORDER BY MESANO_REF DESC LIMIT 0, " + objNf.MESANO_REF;


                    vAND = vAND + "   AND GRUPO = ?GRUPO ";
                    vAND = vAND + "   AND UNID_ID = ?UNID_ID ";
                    vAND = vAND + "   AND FLGST <> ?FLGST ";


                    cmd = new MySqlCommand(this.QuerySelect);


                    cmd.Parameters.AddWithValue("?ITENSSERV", objNf.ITENSSERV.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?GRUPO", objNf.GRUPO.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?FLGST", objNf.FLGST.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?UNID_ID", objNf.UNID_ID);

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_11(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {


                    vFILTER = vFILTER + "NUMNF, ";
                    vFILTER = vFILTER + "CODCLAS, ";
                    vFILTER = vFILTER + "MESANO, ";
                    vFILTER = vFILTER + "MESANO_REF, ";
                    vFILTER = vFILTER + "DATAVENC, ";
                    vFILTER = vFILTER + "DATADIVIDA, ";
                    vFILTER = vFILTER + "DATALEIT, ";
                    vFILTER = vFILTER + "DATAAPR, ";
                    vFILTER = vFILTER + "DATAEM, ";
                    vFILTER = vFILTER + "DATALEITANT, ";
                    vFILTER = vFILTER + "DATAPAG, ";
                    vFILTER = vFILTER + "CODHASH, ";
                    vFILTER = vFILTER + "DATAST, ";
                    vFILTER = vFILTER + "FLGST, ";
                    vFILTER = vFILTER + "VALORTOT, ";
                    vFILTER = vFILTER + "ZONA, ";
                    vFILTER = vFILTER + "LIVRO, ";
                    vFILTER = vFILTER + "CONTA, ";
                    vFILTER = vFILTER + "ROTA, ";
                    vFILTER = vFILTER + "CONS_ID, ";
                    vFILTER = vFILTER + "UNID_ID, ";
                    vFILTER = vFILTER + "DATAPROXLEIT, ";
                    vFILTER = vFILTER + "BANARREC, ";
                    vFILTER = vFILTER + "BANIDENT, ";
                    vFILTER = vFILTER + "FLGMEDFAT, ";
                    vFILTER = vFILTER + "TIPOUC, ";
                    vFILTER = vFILTER + "CFOP, ";
                    vFILTER = vFILTER + "ITENSSERV, ";
                    vFILTER = vFILTER + "TA, ";
                    vFILTER = vFILTER + "GRUPO, ";
                    vFILTER = vFILTER + "FASE, ";
                    vFILTER = vFILTER + "TENSAO, ";
                    vFILTER = vFILTER + "NUM_MED, ";
                    vFILTER = vFILTER + "CONST_MED, ";
                    vFILTER = vFILTER + "MOTIVOCANC, ";
                    vFILTER = vFILTER + "BANCO_FATUR, ";
                    vFILTER = vFILTER + "SOCIAL, ";
                    vFILTER = vFILTER + "MULTA, ";
                    vFILTER = vFILTER + "DATAPRAZO, ";
                    vFILTER = vFILTER + "DIAS, ";
                    vFILTER = vFILTER + "VALORLIQ, ";
                    vFILTER = vFILTER + "NSU, ";
                    vFILTER = vFILTER + "CODBARRA, ";
                    vFILTER = vFILTER + "DVCODBARRA, ";
                    vFILTER = vFILTER + "AGRUPA, ";
                    vFILTER = vFILTER + "AGRUPA_ROTA, ";
                    vFILTER = vFILTER + "ITENSIMP, ";
                    vFILTER = vFILTER + "SGDENVIO, ";
                    vFILTER = vFILTER + "MOTIVOPGTO, ";
                    vFILTER = vFILTER + "MOTIVOSEGVIA, ";
                    vFILTER = vFILTER + "USUARIOSEGVIA, ";
                    vFILTER = vFILTER + "DATASEGVIA, ";
                    vFILTER = vFILTER + "CPF_CNPJ, ";
                    vFILTER = vFILTER + "PARCIALMENTE_CATIVO, ";
                    vFILTER = vFILTER + "LIVREESPECIAL, ";
                    vFILTER = vFILTER + "PERIODO_TESTE, ";
                    vFILTER = vFILTER + "MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO, ";
                    vFILTER = vFILTER + "IRRIGACAO_AQUICULTURA, ";
                    vFILTER = vFILTER + "AGRUPA_CADERNO, ";
                    vFILTER = vFILTER + "AGRUPA_SEQUENCIA, ";
                    vFILTER = vFILTER + "CADERNO, ";
                    vFILTER = vFILTER + "CADERNO_SEQUENCIA, ";
                    vFILTER = vFILTER + "ROTA_QUATRO, ";
                    vFILTER = vFILTER + "AGENCIA, ";
                    vFILTER = vFILTER + "CONTA_CORRENTE, ";
                    vFILTER = vFILTER + "LIVRE_VERDE, ";
                    vFILTER = vFILTER + "CANCELADA_MESANO, ";
                    vFILTER = vFILTER + "CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + "ENVIO_BCO, ";
                    vFILTER = vFILTER + "FATURA_FINAL, ";
                    vFILTER = vFILTER + "PAG_GOV, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_DISTRIBUIDA, ";
                    vFILTER = vFILTER + "USINA, ";
                    vFILTER = vFILTER + "SUPRIMENTO, ";
                    vFILTER = vFILTER + "BAIXA_NFV, ";
                    vFILTER = vFILTER + "SITE_ENVIO, ";
                    vFILTER = vFILTER + "TARIFA_BRANCA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_CONVENIO_SEFAZ, ";
                    vFILTER = vFILTER + "REGIAO, ";
                    vFILTER = vFILTER + "FATURA_COMPLEMENTAR  ";

                    vINNERJOIN = " ";

                    vORDERBY = "ORDER BY MID(MESANO,4,4) DESC, MID(MESANO,1,2) DESC LIMIT 0,3 ";


                    vAND = vAND + "   AND ZONA    = ?ZONA ";
                    vAND = vAND + "   AND LIVRO   = ?LIVRO ";
                    vAND = vAND + "   AND CONTA   = ?CONTA ";
                    vAND = vAND + "   AND UNID_ID = ?UNID_ID ";
                    vAND = vAND + "   AND CONS_ID = ?CONS_ID ";
                    vAND = vAND + "   AND TRIM(FLGST) = ''  ";

                    cmd = new MySqlCommand(this.QuerySelect);


                    cmd.Parameters.AddWithValue("?ZONA", objNf.ZONA.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?LIVRO", objNf.LIVRO.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?CONTA", objNf.CONTA.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?UNID_ID", objNf.UNID_ID);
                    cmd.Parameters.AddWithValue("?CONS_ID", objNf.CONS_ID);

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_12(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {


                    vFILTER = vFILTER + "NUMNF, ";
                    vFILTER = vFILTER + "CODCLAS, ";
                    vFILTER = vFILTER + "MESANO, ";
                    vFILTER = vFILTER + "MESANO_REF, ";
                    vFILTER = vFILTER + "DATAVENC, ";
                    vFILTER = vFILTER + "DATADIVIDA, ";
                    vFILTER = vFILTER + "DATALEIT, ";
                    vFILTER = vFILTER + "DATAAPR, ";
                    vFILTER = vFILTER + "DATAEM, ";
                    vFILTER = vFILTER + "DATALEITANT, ";
                    vFILTER = vFILTER + "DATAPAG, ";
                    vFILTER = vFILTER + "CODHASH, ";
                    vFILTER = vFILTER + "DATAST, ";
                    vFILTER = vFILTER + "FLGST, ";
                    vFILTER = vFILTER + "VALORTOT, ";
                    vFILTER = vFILTER + "ZONA, ";
                    vFILTER = vFILTER + "LIVRO, ";
                    vFILTER = vFILTER + "CONTA, ";
                    vFILTER = vFILTER + "ROTA, ";
                    vFILTER = vFILTER + "CONS_ID, ";
                    vFILTER = vFILTER + "UNID_ID, ";
                    vFILTER = vFILTER + "DATAPROXLEIT, ";
                    vFILTER = vFILTER + "BANARREC, ";
                    vFILTER = vFILTER + "BANIDENT, ";
                    vFILTER = vFILTER + "FLGMEDFAT, ";
                    vFILTER = vFILTER + "TIPOUC, ";
                    vFILTER = vFILTER + "CFOP, ";
                    vFILTER = vFILTER + "ITENSSERV, ";
                    vFILTER = vFILTER + "TA, ";
                    vFILTER = vFILTER + "GRUPO, ";
                    vFILTER = vFILTER + "FASE, ";
                    vFILTER = vFILTER + "TENSAO, ";
                    vFILTER = vFILTER + "NUM_MED, ";
                    vFILTER = vFILTER + "CONST_MED, ";
                    vFILTER = vFILTER + "MOTIVOCANC, ";
                    vFILTER = vFILTER + "BANCO_FATUR, ";
                    vFILTER = vFILTER + "SOCIAL, ";
                    vFILTER = vFILTER + "MULTA, ";
                    vFILTER = vFILTER + "DATAPRAZO, ";
                    vFILTER = vFILTER + "DIAS, ";
                    vFILTER = vFILTER + "VALORLIQ, ";
                    vFILTER = vFILTER + "NSU, ";
                    vFILTER = vFILTER + "CODBARRA, ";
                    vFILTER = vFILTER + "DVCODBARRA, ";
                    vFILTER = vFILTER + "AGRUPA, ";
                    vFILTER = vFILTER + "AGRUPA_ROTA, ";
                    vFILTER = vFILTER + "ITENSIMP, ";
                    vFILTER = vFILTER + "SGDENVIO, ";
                    vFILTER = vFILTER + "MOTIVOPGTO, ";
                    vFILTER = vFILTER + "MOTIVOSEGVIA, ";
                    vFILTER = vFILTER + "USUARIOSEGVIA, ";
                    vFILTER = vFILTER + "DATASEGVIA, ";
                    vFILTER = vFILTER + "CPF_CNPJ, ";
                    vFILTER = vFILTER + "PARCIALMENTE_CATIVO, ";
                    vFILTER = vFILTER + "LIVREESPECIAL, ";
                    vFILTER = vFILTER + "PERIODO_TESTE, ";
                    vFILTER = vFILTER + "MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO, ";
                    vFILTER = vFILTER + "IRRIGACAO_AQUICULTURA, ";
                    vFILTER = vFILTER + "AGRUPA_CADERNO, ";
                    vFILTER = vFILTER + "AGRUPA_SEQUENCIA, ";
                    vFILTER = vFILTER + "CADERNO, ";
                    vFILTER = vFILTER + "CADERNO_SEQUENCIA, ";
                    vFILTER = vFILTER + "ROTA_QUATRO, ";
                    vFILTER = vFILTER + "AGENCIA, ";
                    vFILTER = vFILTER + "CONTA_CORRENTE, ";
                    vFILTER = vFILTER + "LIVRE_VERDE, ";
                    vFILTER = vFILTER + "CANCELADA_MESANO, ";
                    vFILTER = vFILTER + "CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + "ENVIO_BCO, ";
                    vFILTER = vFILTER + "FATURA_FINAL, ";
                    vFILTER = vFILTER + "PAG_GOV, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_DISTRIBUIDA, ";
                    vFILTER = vFILTER + "USINA, ";
                    vFILTER = vFILTER + "SUPRIMENTO, ";
                    vFILTER = vFILTER + "BAIXA_NFV, ";
                    vFILTER = vFILTER + "SITE_ENVIO, ";
                    vFILTER = vFILTER + "TARIFA_BRANCA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_CONVENIO_SEFAZ, ";
                    vFILTER = vFILTER + "REGIAO, ";
                    vFILTER = vFILTER + "FATURA_COMPLEMENTAR  ";

                    vINNERJOIN = " ";

                    vORDERBY = "ORDER BY MID(MESANO,4,4) DESC, MID(MESANO,1,2) DESC LIMIT 0,3 ";


                    vAND = vAND + "   AND ZONA    = ?ZONA ";
                    vAND = vAND + "   AND LIVRO   = ?LIVRO ";
                    vAND = vAND + "   AND CONTA   = ?CONTA ";
                    vAND = vAND + "   AND UNID_ID = ?UNID_ID ";
                    vAND = vAND + "   AND CONS_ID = ?CONS_ID ";
                    vAND = vAND + "   AND TRIM(FLGST) = ''  ";
                    vAND = vAND + "   AND DATE_FORMAT( DATAVENC, '%d/%m/%Y' ) <  ?DATAVENC ";

                    cmd = new MySqlCommand(this.QuerySelect);


                    cmd.Parameters.AddWithValue("?ZONA", objNf.ZONA.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?LIVRO", objNf.LIVRO.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?CONTA", objNf.CONTA.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?UNID_ID", objNf.UNID_ID);
                    cmd.Parameters.AddWithValue("?CONS_ID", objNf.CONS_ID);
                    cmd.Parameters.AddWithValue("?DATAVENC", objNf.DATAVENC);

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_13(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {


                    vFILTER = vFILTER + "NUMNF, ";
                    vFILTER = vFILTER + "CODCLAS, ";
                    vFILTER = vFILTER + "MESANO, ";
                    vFILTER = vFILTER + "MESANO_REF, ";
                    vFILTER = vFILTER + "DATAVENC, ";
                    vFILTER = vFILTER + "DATADIVIDA, ";
                    vFILTER = vFILTER + "DATALEIT, ";
                    vFILTER = vFILTER + "DATAAPR, ";
                    vFILTER = vFILTER + "DATAEM, ";
                    vFILTER = vFILTER + "DATALEITANT, ";
                    vFILTER = vFILTER + "DATAPAG, ";
                    vFILTER = vFILTER + "CODHASH, ";
                    vFILTER = vFILTER + "DATAST, ";
                    vFILTER = vFILTER + "FLGST, ";
                    vFILTER = vFILTER + "VALORTOT, ";
                    vFILTER = vFILTER + "ZONA, ";
                    vFILTER = vFILTER + "LIVRO, ";
                    vFILTER = vFILTER + "CONTA, ";
                    vFILTER = vFILTER + "ROTA, ";
                    vFILTER = vFILTER + "CONS_ID, ";
                    vFILTER = vFILTER + "UNID_ID, ";
                    vFILTER = vFILTER + "DATAPROXLEIT, ";
                    vFILTER = vFILTER + "BANARREC, ";
                    vFILTER = vFILTER + "BANIDENT, ";
                    vFILTER = vFILTER + "FLGMEDFAT, ";
                    vFILTER = vFILTER + "TIPOUC, ";
                    vFILTER = vFILTER + "CFOP, ";
                    vFILTER = vFILTER + "ITENSSERV, ";
                    vFILTER = vFILTER + "TA, ";
                    vFILTER = vFILTER + "GRUPO, ";
                    vFILTER = vFILTER + "FASE, ";
                    vFILTER = vFILTER + "TENSAO, ";
                    vFILTER = vFILTER + "NUM_MED, ";
                    vFILTER = vFILTER + "CONST_MED, ";
                    vFILTER = vFILTER + "MOTIVOCANC, ";
                    vFILTER = vFILTER + "BANCO_FATUR, ";
                    vFILTER = vFILTER + "SOCIAL, ";
                    vFILTER = vFILTER + "MULTA, ";
                    vFILTER = vFILTER + "DATAPRAZO, ";
                    vFILTER = vFILTER + "DIAS, ";
                    vFILTER = vFILTER + "VALORLIQ, ";
                    vFILTER = vFILTER + "NSU, ";
                    vFILTER = vFILTER + "CODBARRA, ";
                    vFILTER = vFILTER + "DVCODBARRA, ";
                    vFILTER = vFILTER + "AGRUPA, ";
                    vFILTER = vFILTER + "AGRUPA_ROTA, ";
                    vFILTER = vFILTER + "ITENSIMP, ";
                    vFILTER = vFILTER + "SGDENVIO, ";
                    vFILTER = vFILTER + "MOTIVOPGTO, ";
                    vFILTER = vFILTER + "MOTIVOSEGVIA, ";
                    vFILTER = vFILTER + "USUARIOSEGVIA, ";
                    vFILTER = vFILTER + "DATASEGVIA, ";
                    vFILTER = vFILTER + "CPF_CNPJ, ";
                    vFILTER = vFILTER + "PARCIALMENTE_CATIVO, ";
                    vFILTER = vFILTER + "LIVREESPECIAL, ";
                    vFILTER = vFILTER + "PERIODO_TESTE, ";
                    vFILTER = vFILTER + "MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO, ";
                    vFILTER = vFILTER + "IRRIGACAO_AQUICULTURA, ";
                    vFILTER = vFILTER + "AGRUPA_CADERNO, ";
                    vFILTER = vFILTER + "AGRUPA_SEQUENCIA, ";
                    vFILTER = vFILTER + "CADERNO, ";
                    vFILTER = vFILTER + "CADERNO_SEQUENCIA, ";
                    vFILTER = vFILTER + "ROTA_QUATRO, ";
                    vFILTER = vFILTER + "AGENCIA, ";
                    vFILTER = vFILTER + "CONTA_CORRENTE, ";
                    vFILTER = vFILTER + "LIVRE_VERDE, ";
                    vFILTER = vFILTER + "CANCELADA_MESANO, ";
                    vFILTER = vFILTER + "CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + "ENVIO_BCO, ";
                    vFILTER = vFILTER + "FATURA_FINAL, ";
                    vFILTER = vFILTER + "PAG_GOV, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_DISTRIBUIDA, ";
                    vFILTER = vFILTER + "USINA, ";
                    vFILTER = vFILTER + "SUPRIMENTO, ";
                    vFILTER = vFILTER + "BAIXA_NFV, ";
                    vFILTER = vFILTER + "SITE_ENVIO, ";
                    vFILTER = vFILTER + "TARIFA_BRANCA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_CONVENIO_SEFAZ, ";
                    vFILTER = vFILTER + "REGIAO, ";
                    vFILTER = vFILTER + "FATURA_COMPLEMENTAR  ";

                    vINNERJOIN = " ";

                    vORDERBY = " ";


                    vAND = vAND + "   AND MESANO_REF    = ?MESANO_REF ";
                    vAND = vAND + "   AND GRUPO   = ?GRUPO ";


                    cmd = new MySqlCommand(this.QuerySelect);


                    cmd.Parameters.AddWithValue("?MESANO_REF", objNf.MESANO_REF);
                    cmd.Parameters.AddWithValue("?GRUPO", objNf.GRUPO.ToString().ToUpper());


                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_14(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {


                    vFILTER = vFILTER + "NUMNF, ";
                    vFILTER = vFILTER + "CODCLAS, ";
                    vFILTER = vFILTER + "MESANO, ";
                    vFILTER = vFILTER + "MESANO_REF, ";
                    vFILTER = vFILTER + "DATAVENC, ";
                    vFILTER = vFILTER + "DATADIVIDA, ";
                    vFILTER = vFILTER + "DATALEIT, ";
                    vFILTER = vFILTER + "DATAAPR, ";
                    vFILTER = vFILTER + "DATAEM, ";
                    vFILTER = vFILTER + "DATALEITANT, ";
                    vFILTER = vFILTER + "DATAPAG, ";
                    vFILTER = vFILTER + "CODHASH, ";
                    vFILTER = vFILTER + "DATAST, ";
                    vFILTER = vFILTER + "FLGST, ";
                    vFILTER = vFILTER + "VALORTOT, ";
                    vFILTER = vFILTER + "ZONA, ";
                    vFILTER = vFILTER + "LIVRO, ";
                    vFILTER = vFILTER + "CONTA, ";
                    vFILTER = vFILTER + "ROTA, ";
                    vFILTER = vFILTER + "CONS_ID, ";
                    vFILTER = vFILTER + "UNID_ID, ";
                    vFILTER = vFILTER + "DATAPROXLEIT, ";
                    vFILTER = vFILTER + "BANARREC, ";
                    vFILTER = vFILTER + "BANIDENT, ";
                    vFILTER = vFILTER + "FLGMEDFAT, ";
                    vFILTER = vFILTER + "TIPOUC, ";
                    vFILTER = vFILTER + "CFOP, ";
                    vFILTER = vFILTER + "ITENSSERV, ";
                    vFILTER = vFILTER + "TA, ";
                    vFILTER = vFILTER + "GRUPO, ";
                    vFILTER = vFILTER + "FASE, ";
                    vFILTER = vFILTER + "TENSAO, ";
                    vFILTER = vFILTER + "NUM_MED, ";
                    vFILTER = vFILTER + "CONST_MED, ";
                    vFILTER = vFILTER + "MOTIVOCANC, ";
                    vFILTER = vFILTER + "BANCO_FATUR, ";
                    vFILTER = vFILTER + "SOCIAL, ";
                    vFILTER = vFILTER + "MULTA, ";
                    vFILTER = vFILTER + "DATAPRAZO, ";
                    vFILTER = vFILTER + "DIAS, ";
                    vFILTER = vFILTER + "VALORLIQ, ";
                    vFILTER = vFILTER + "NSU, ";
                    vFILTER = vFILTER + "CODBARRA, ";
                    vFILTER = vFILTER + "DVCODBARRA, ";
                    vFILTER = vFILTER + "AGRUPA, ";
                    vFILTER = vFILTER + "AGRUPA_ROTA, ";
                    vFILTER = vFILTER + "ITENSIMP, ";
                    vFILTER = vFILTER + "SGDENVIO, ";
                    vFILTER = vFILTER + "MOTIVOPGTO, ";
                    vFILTER = vFILTER + "MOTIVOSEGVIA, ";
                    vFILTER = vFILTER + "USUARIOSEGVIA, ";
                    vFILTER = vFILTER + "DATASEGVIA, ";
                    vFILTER = vFILTER + "CPF_CNPJ, ";
                    vFILTER = vFILTER + "PARCIALMENTE_CATIVO, ";
                    vFILTER = vFILTER + "LIVREESPECIAL, ";
                    vFILTER = vFILTER + "PERIODO_TESTE, ";
                    vFILTER = vFILTER + "MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO, ";
                    vFILTER = vFILTER + "IRRIGACAO_AQUICULTURA, ";
                    vFILTER = vFILTER + "AGRUPA_CADERNO, ";
                    vFILTER = vFILTER + "AGRUPA_SEQUENCIA, ";
                    vFILTER = vFILTER + "CADERNO, ";
                    vFILTER = vFILTER + "CADERNO_SEQUENCIA, ";
                    vFILTER = vFILTER + "ROTA_QUATRO, ";
                    vFILTER = vFILTER + "AGENCIA, ";
                    vFILTER = vFILTER + "CONTA_CORRENTE, ";
                    vFILTER = vFILTER + "LIVRE_VERDE, ";
                    vFILTER = vFILTER + "CANCELADA_MESANO, ";
                    vFILTER = vFILTER + "CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + "ENVIO_BCO, ";
                    vFILTER = vFILTER + "FATURA_FINAL, ";
                    vFILTER = vFILTER + "PAG_GOV, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_DISTRIBUIDA, ";
                    vFILTER = vFILTER + "USINA, ";
                    vFILTER = vFILTER + "SUPRIMENTO, ";
                    vFILTER = vFILTER + "BAIXA_NFV, ";
                    vFILTER = vFILTER + "SITE_ENVIO, ";
                    vFILTER = vFILTER + "TARIFA_BRANCA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_CONVENIO_SEFAZ, ";
                    vFILTER = vFILTER + "REGIAO, ";
                    vFILTER = vFILTER + "FATURA_COMPLEMENTAR  ";

                    vINNERJOIN = " ";

                    vORDERBY = "ORDER BY MESANO_REF DESC LIMIT 0, 12 ";

                    vAND = vAND + "   AND MESANO_REF <= ?MESANO_REF  ";
                    vAND = vAND + "   AND ZONA    = ?ZONA ";
                    vAND = vAND + "   AND LIVRO   = ?LIVRO ";
                    vAND = vAND + "   AND CONTA   = ?CONTA ";
                    vAND = vAND + "   AND GRUPO = ?GRUPO ";
                    vAND = vAND + "   AND UNID_ID = ?UNID_ID ";



                    cmd = new MySqlCommand(this.QuerySelect);

                    cmd.Parameters.AddWithValue("?MESANO_REF", objNf.MESANO_REF);
                    cmd.Parameters.AddWithValue("?ZONA", objNf.ZONA.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?LIVRO", objNf.LIVRO.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?CONTA", objNf.CONTA.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?GRUPO", objNf.GRUPO.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?UNID_ID", objNf.UNID_ID);


                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_15(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {


                    vFILTER = vFILTER + "NUMNF, ";
                    vFILTER = vFILTER + "CODCLAS, ";
                    vFILTER = vFILTER + "MESANO, ";
                    vFILTER = vFILTER + "MESANO_REF, ";
                    vFILTER = vFILTER + "DATAVENC, ";
                    vFILTER = vFILTER + "DATADIVIDA, ";
                    vFILTER = vFILTER + "DATALEIT, ";
                    vFILTER = vFILTER + "DATAAPR, ";
                    vFILTER = vFILTER + "DATAEM, ";
                    vFILTER = vFILTER + "DATALEITANT, ";
                    vFILTER = vFILTER + "DATAPAG, ";
                    vFILTER = vFILTER + "CODHASH, ";
                    vFILTER = vFILTER + "DATAST, ";
                    vFILTER = vFILTER + "FLGST, ";
                    vFILTER = vFILTER + "VALORTOT, ";
                    vFILTER = vFILTER + "ZONA, ";
                    vFILTER = vFILTER + "LIVRO, ";
                    vFILTER = vFILTER + "CONTA, ";
                    vFILTER = vFILTER + "ROTA, ";
                    vFILTER = vFILTER + "CONS_ID, ";
                    vFILTER = vFILTER + "UNID_ID, ";
                    vFILTER = vFILTER + "DATAPROXLEIT, ";
                    vFILTER = vFILTER + "BANARREC, ";
                    vFILTER = vFILTER + "BANIDENT, ";
                    vFILTER = vFILTER + "FLGMEDFAT, ";
                    vFILTER = vFILTER + "TIPOUC, ";
                    vFILTER = vFILTER + "CFOP, ";
                    vFILTER = vFILTER + "ITENSSERV, ";
                    vFILTER = vFILTER + "TA, ";
                    vFILTER = vFILTER + "GRUPO, ";
                    vFILTER = vFILTER + "FASE, ";
                    vFILTER = vFILTER + "TENSAO, ";
                    vFILTER = vFILTER + "NUM_MED, ";
                    vFILTER = vFILTER + "CONST_MED, ";
                    vFILTER = vFILTER + "MOTIVOCANC, ";
                    vFILTER = vFILTER + "BANCO_FATUR, ";
                    vFILTER = vFILTER + "SOCIAL, ";
                    vFILTER = vFILTER + "MULTA, ";
                    vFILTER = vFILTER + "DATAPRAZO, ";
                    vFILTER = vFILTER + "DIAS, ";
                    vFILTER = vFILTER + "VALORLIQ, ";
                    vFILTER = vFILTER + "NSU, ";
                    vFILTER = vFILTER + "CODBARRA, ";
                    vFILTER = vFILTER + "DVCODBARRA, ";
                    vFILTER = vFILTER + "AGRUPA, ";
                    vFILTER = vFILTER + "AGRUPA_ROTA, ";
                    vFILTER = vFILTER + "ITENSIMP, ";
                    vFILTER = vFILTER + "SGDENVIO, ";
                    vFILTER = vFILTER + "MOTIVOPGTO, ";
                    vFILTER = vFILTER + "MOTIVOSEGVIA, ";
                    vFILTER = vFILTER + "USUARIOSEGVIA, ";
                    vFILTER = vFILTER + "DATASEGVIA, ";
                    vFILTER = vFILTER + "CPF_CNPJ, ";
                    vFILTER = vFILTER + "PARCIALMENTE_CATIVO, ";
                    vFILTER = vFILTER + "LIVREESPECIAL, ";
                    vFILTER = vFILTER + "PERIODO_TESTE, ";
                    vFILTER = vFILTER + "MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO, ";
                    vFILTER = vFILTER + "IRRIGACAO_AQUICULTURA, ";
                    vFILTER = vFILTER + "AGRUPA_CADERNO, ";
                    vFILTER = vFILTER + "AGRUPA_SEQUENCIA, ";
                    vFILTER = vFILTER + "CADERNO, ";
                    vFILTER = vFILTER + "CADERNO_SEQUENCIA, ";
                    vFILTER = vFILTER + "ROTA_QUATRO, ";
                    vFILTER = vFILTER + "AGENCIA, ";
                    vFILTER = vFILTER + "CONTA_CORRENTE, ";
                    vFILTER = vFILTER + "LIVRE_VERDE, ";
                    vFILTER = vFILTER + "CANCELADA_MESANO, ";
                    vFILTER = vFILTER + "CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + "ENVIO_BCO, ";
                    vFILTER = vFILTER + "FATURA_FINAL, ";
                    vFILTER = vFILTER + "PAG_GOV, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_DISTRIBUIDA, ";
                    vFILTER = vFILTER + "USINA, ";
                    vFILTER = vFILTER + "SUPRIMENTO, ";
                    vFILTER = vFILTER + "BAIXA_NFV, ";
                    vFILTER = vFILTER + "SITE_ENVIO, ";
                    vFILTER = vFILTER + "TARIFA_BRANCA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_CONVENIO_SEFAZ, ";
                    vFILTER = vFILTER + "REGIAO, ";
                    vFILTER = vFILTER + "FATURA_COMPLEMENTAR  ";

                    vINNERJOIN = " ";

                    vORDERBY = "  ORDER BY DATAPAG ASC ";

                    //vAND = vAND + "   AND DATE_FORMAT(DATAPAG, '%Y/%m/%d' ) BETWEEN ?DATAPAGINI AND ?DATAPAGFIM  ";
                    vAND = vAND + "   AND DATAPAG BETWEEN ?DATAPAGINI AND ?DATAPAGFIM  ";

                    cmd = new MySqlCommand(this.QuerySelect);

                    cmd.Parameters.AddWithValue("?DATAPAGINI", objNf.DATAINICIAL.ToString("yyyy/MM/dd"));
                    cmd.Parameters.AddWithValue("?DATAPAGFIM", objNf.DATAFINAL.ToString("yyyy/MM/dd"));


                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_16(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {
                    vFILTER = vFILTER + "NUMNF, ";
                    vFILTER = vFILTER + "CODCLAS, ";
                    vFILTER = vFILTER + "MESANO, ";
                    vFILTER = vFILTER + "MESANO_REF, ";
                    vFILTER = vFILTER + "DATAVENC, ";
                    vFILTER = vFILTER + "DATADIVIDA, ";
                    vFILTER = vFILTER + "DATALEIT, ";
                    vFILTER = vFILTER + "DATAAPR, ";
                    vFILTER = vFILTER + "DATAEM, ";
                    vFILTER = vFILTER + "DATALEITANT, ";
                    vFILTER = vFILTER + "DATAPAG, ";
                    vFILTER = vFILTER + "CODHASH, ";
                    vFILTER = vFILTER + "DATAST, ";
                    vFILTER = vFILTER + "FLGST, ";
                    vFILTER = vFILTER + "VALORTOT, ";
                    vFILTER = vFILTER + "ZONA, ";
                    vFILTER = vFILTER + "LIVRO, ";
                    vFILTER = vFILTER + "CONTA, ";
                    vFILTER = vFILTER + "ROTA, ";
                    vFILTER = vFILTER + "CONS_ID, ";
                    vFILTER = vFILTER + "UNID_ID, ";
                    vFILTER = vFILTER + "DATAPROXLEIT, ";
                    vFILTER = vFILTER + "BANARREC, ";
                    vFILTER = vFILTER + "BANIDENT, ";
                    vFILTER = vFILTER + "FLGMEDFAT, ";
                    vFILTER = vFILTER + "TIPOUC, ";
                    vFILTER = vFILTER + "CFOP, ";
                    vFILTER = vFILTER + "ITENSSERV, ";
                    vFILTER = vFILTER + "TA, ";
                    vFILTER = vFILTER + "GRUPO, ";
                    vFILTER = vFILTER + "FASE, ";
                    vFILTER = vFILTER + "TENSAO, ";
                    vFILTER = vFILTER + "NUM_MED, ";
                    vFILTER = vFILTER + "CONST_MED, ";
                    vFILTER = vFILTER + "MOTIVOCANC, ";
                    vFILTER = vFILTER + "BANCO_FATUR, ";
                    vFILTER = vFILTER + "SOCIAL, ";
                    vFILTER = vFILTER + "MULTA, ";
                    vFILTER = vFILTER + "DATAPRAZO, ";
                    vFILTER = vFILTER + "DIAS, ";
                    vFILTER = vFILTER + "VALORLIQ, ";
                    vFILTER = vFILTER + "NSU, ";
                    vFILTER = vFILTER + "CODBARRA, ";
                    vFILTER = vFILTER + "DVCODBARRA, ";
                    vFILTER = vFILTER + "AGRUPA, ";
                    vFILTER = vFILTER + "AGRUPA_ROTA, ";
                    vFILTER = vFILTER + "ITENSIMP, ";
                    vFILTER = vFILTER + "SGDENVIO, ";
                    vFILTER = vFILTER + "MOTIVOPGTO, ";
                    vFILTER = vFILTER + "MOTIVOSEGVIA, ";
                    vFILTER = vFILTER + "USUARIOSEGVIA, ";
                    vFILTER = vFILTER + "DATASEGVIA, ";
                    vFILTER = vFILTER + "CPF_CNPJ, ";
                    vFILTER = vFILTER + "PARCIALMENTE_CATIVO, ";
                    vFILTER = vFILTER + "LIVREESPECIAL, ";
                    vFILTER = vFILTER + "PERIODO_TESTE, ";
                    vFILTER = vFILTER + "MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO, ";
                    vFILTER = vFILTER + "IRRIGACAO_AQUICULTURA, ";
                    vFILTER = vFILTER + "AGRUPA_CADERNO, ";
                    vFILTER = vFILTER + "AGRUPA_SEQUENCIA, ";
                    vFILTER = vFILTER + "CADERNO, ";
                    vFILTER = vFILTER + "CADERNO_SEQUENCIA, ";
                    vFILTER = vFILTER + "ROTA_QUATRO, ";
                    vFILTER = vFILTER + "AGENCIA, ";
                    vFILTER = vFILTER + "CONTA_CORRENTE, ";
                    vFILTER = vFILTER + "LIVRE_VERDE, ";
                    vFILTER = vFILTER + "CANCELADA_MESANO, ";
                    vFILTER = vFILTER + "CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + "ENVIO_BCO, ";
                    vFILTER = vFILTER + "FATURA_FINAL, ";
                    vFILTER = vFILTER + "PAG_GOV, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_DISTRIBUIDA, ";
                    vFILTER = vFILTER + "USINA, ";
                    vFILTER = vFILTER + "SUPRIMENTO, ";
                    vFILTER = vFILTER + "BAIXA_NFV, ";
                    vFILTER = vFILTER + "SITE_ENVIO, ";
                    vFILTER = vFILTER + "TARIFA_BRANCA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_CONVENIO_SEFAZ, ";
                    vFILTER = vFILTER + "REGIAO, ";
                    vFILTER = vFILTER + "FATURA_COMPLEMENTAR  ";

                    vINNERJOIN = " ";

                    vORDERBY = "ORDER BY MESANO_REF DESC LIMIT 0, ?MESANO_REF  ";



                    if (objNf.CONS_ID != 0)
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND CONS_ID = ?CONS_ID "; }
                        else
                        { vAND = "AND CONS_ID = ?CONS_ID "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }


                    if (objNf.UNID_ID != 0)
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND UNID_ID = ?UNID_ID "; }
                        else
                        { vAND = "AND UNID_ID = ?UNID_ID "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }


                    if (!string.IsNullOrEmpty(objNf.ZONA))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND ZONA = ?ZONA "; }
                        else
                        { vAND = "AND ZONA = ?ZONA "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }

                    if (!string.IsNullOrEmpty(objNf.LIVRO))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND LIVRO = ?LIVRO "; }
                        else
                        { vAND = "AND LIVRO = ?LIVRO "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }


                    if (!string.IsNullOrEmpty(objNf.CONTA))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND CONTA = ?CONTA "; }
                        else
                        { vAND = "AND CONTA = ?CONTA "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }



                    cmd = new MySqlCommand(this.QuerySelect);


                    cmd.Parameters.AddWithValue("?MESANO_REF", objNf.MESANO_REF);


                    if (objNf.CONS_ID != 0)
                    {
                        cmd.Parameters.AddWithValue("?CONS_ID", objNf.CONS_ID);
                    }

                    if (objNf.UNID_ID != 0)
                    {
                        cmd.Parameters.AddWithValue("?UNID_ID", objNf.UNID_ID);
                    }

                    if (!string.IsNullOrEmpty(objNf.ZONA))
                    {
                        cmd.Parameters.AddWithValue("?ZONA", objNf.ZONA.ToString().ToUpper());
                    }

                    if (!string.IsNullOrEmpty(objNf.LIVRO))
                    {
                        cmd.Parameters.AddWithValue("?LIVRO", objNf.LIVRO.ToString().ToUpper());
                    }

                    if (!string.IsNullOrEmpty(objNf.CONTA))
                    {
                        cmd.Parameters.AddWithValue("?CONTA", objNf.CONTA.ToString().ToUpper());
                    }


                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_17(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {
                    vFILTER = vFILTER + "NUMNF, ";
                    vFILTER = vFILTER + "CODCLAS, ";
                    vFILTER = vFILTER + "MESANO, ";
                    vFILTER = vFILTER + "MESANO_REF, ";
                    vFILTER = vFILTER + "DATAVENC, ";
                    vFILTER = vFILTER + "DATADIVIDA, ";
                    vFILTER = vFILTER + "DATALEIT, ";
                    vFILTER = vFILTER + "DATAAPR, ";
                    vFILTER = vFILTER + "DATAEM, ";
                    vFILTER = vFILTER + "DATALEITANT, ";
                    vFILTER = vFILTER + "DATAPAG, ";
                    vFILTER = vFILTER + "CODHASH, ";
                    vFILTER = vFILTER + "DATAST, ";
                    vFILTER = vFILTER + "FLGST, ";
                    vFILTER = vFILTER + "VALORTOT, ";
                    vFILTER = vFILTER + "ZONA, ";
                    vFILTER = vFILTER + "LIVRO, ";
                    vFILTER = vFILTER + "CONTA, ";
                    vFILTER = vFILTER + "ROTA, ";
                    vFILTER = vFILTER + "CONS_ID, ";
                    vFILTER = vFILTER + "UNID_ID, ";
                    vFILTER = vFILTER + "DATAPROXLEIT, ";
                    vFILTER = vFILTER + "BANARREC, ";
                    vFILTER = vFILTER + "BANIDENT, ";
                    vFILTER = vFILTER + "FLGMEDFAT, ";
                    vFILTER = vFILTER + "TIPOUC, ";
                    vFILTER = vFILTER + "CFOP, ";
                    vFILTER = vFILTER + "ITENSSERV, ";
                    vFILTER = vFILTER + "TA, ";
                    vFILTER = vFILTER + "GRUPO, ";
                    vFILTER = vFILTER + "FASE, ";
                    vFILTER = vFILTER + "TENSAO, ";
                    vFILTER = vFILTER + "NUM_MED, ";
                    vFILTER = vFILTER + "CONST_MED, ";
                    vFILTER = vFILTER + "MOTIVOCANC, ";
                    vFILTER = vFILTER + "BANCO_FATUR, ";
                    vFILTER = vFILTER + "SOCIAL, ";
                    vFILTER = vFILTER + "MULTA, ";
                    vFILTER = vFILTER + "DATAPRAZO, ";
                    vFILTER = vFILTER + "DIAS, ";
                    vFILTER = vFILTER + "VALORLIQ, ";
                    vFILTER = vFILTER + "NSU, ";
                    vFILTER = vFILTER + "CODBARRA, ";
                    vFILTER = vFILTER + "DVCODBARRA, ";
                    vFILTER = vFILTER + "AGRUPA, ";
                    vFILTER = vFILTER + "AGRUPA_ROTA, ";
                    vFILTER = vFILTER + "ITENSIMP, ";
                    vFILTER = vFILTER + "SGDENVIO, ";
                    vFILTER = vFILTER + "MOTIVOPGTO, ";
                    vFILTER = vFILTER + "MOTIVOSEGVIA, ";
                    vFILTER = vFILTER + "USUARIOSEGVIA, ";
                    vFILTER = vFILTER + "DATASEGVIA, ";
                    vFILTER = vFILTER + "CPF_CNPJ, ";
                    vFILTER = vFILTER + "PARCIALMENTE_CATIVO, ";
                    vFILTER = vFILTER + "LIVREESPECIAL, ";
                    vFILTER = vFILTER + "PERIODO_TESTE, ";
                    vFILTER = vFILTER + "MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO, ";
                    vFILTER = vFILTER + "IRRIGACAO_AQUICULTURA, ";
                    vFILTER = vFILTER + "AGRUPA_CADERNO, ";
                    vFILTER = vFILTER + "AGRUPA_SEQUENCIA, ";
                    vFILTER = vFILTER + "CADERNO, ";
                    vFILTER = vFILTER + "CADERNO_SEQUENCIA, ";
                    vFILTER = vFILTER + "ROTA_QUATRO, ";
                    vFILTER = vFILTER + "AGENCIA, ";
                    vFILTER = vFILTER + "CONTA_CORRENTE, ";
                    vFILTER = vFILTER + "LIVRE_VERDE, ";
                    vFILTER = vFILTER + "CANCELADA_MESANO, ";
                    vFILTER = vFILTER + "CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + "ENVIO_BCO, ";
                    vFILTER = vFILTER + "FATURA_FINAL, ";
                    vFILTER = vFILTER + "PAG_GOV, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_DISTRIBUIDA, ";
                    vFILTER = vFILTER + "USINA, ";
                    vFILTER = vFILTER + "SUPRIMENTO, ";
                    vFILTER = vFILTER + "BAIXA_NFV, ";
                    vFILTER = vFILTER + "SITE_ENVIO, ";
                    vFILTER = vFILTER + "TARIFA_BRANCA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_CONVENIO_SEFAZ, ";
                    vFILTER = vFILTER + "REGIAO, ";
                    vFILTER = vFILTER + "FATURA_COMPLEMENTAR  ";
                 

                    vINNERJOIN = " ";

                    vORDERBY = " ";


                    vAND = vAND + "AND CONS_ID = ?CONS_ID ";
                    vAND = vAND + "AND UNID_ID = ?UNID_ID ";
                    vAND = vAND + "AND FATURA_COMPLEMENTAR = ?FATURA_COMPLEMENTAR ";
                    vAND = vAND + "AND GRUPO = ?GRUPO ";
                   


                    cmd = new MySqlCommand(this.QuerySelect);



                    cmd.Parameters.AddWithValue("?CONS_ID", objNf.CONS_ID);
                    cmd.Parameters.AddWithValue("?UNID_ID", objNf.UNID_ID);
                    cmd.Parameters.AddWithValue("?FATURA_COMPLEMENTAR", objNf.FATURA_COMPLEMENTAR);
                    cmd.Parameters.AddWithValue("?GRUPO", objNf.GRUPO.ToString().ToUpper());
                  


                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_18(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {
                    vFILTER = vFILTER + "NUMNF, ";
                    vFILTER = vFILTER + "CODCLAS, ";
                    vFILTER = vFILTER + "MESANO, ";
                    vFILTER = vFILTER + "MESANO_REF, ";
                    vFILTER = vFILTER + "DATAVENC, ";
                    vFILTER = vFILTER + "DATADIVIDA, ";
                    vFILTER = vFILTER + "DATALEIT, ";
                    vFILTER = vFILTER + "DATAAPR, ";
                    vFILTER = vFILTER + "DATAEM, ";
                    vFILTER = vFILTER + "DATALEITANT, ";
                    vFILTER = vFILTER + "DATAPAG, ";
                    vFILTER = vFILTER + "CODHASH, ";
                    vFILTER = vFILTER + "DATAST, ";
                    vFILTER = vFILTER + "FLGST, ";
                    vFILTER = vFILTER + "VALORTOT, ";
                    vFILTER = vFILTER + "ZONA, ";
                    vFILTER = vFILTER + "LIVRO, ";
                    vFILTER = vFILTER + "CONTA, ";
                    vFILTER = vFILTER + "ROTA, ";
                    vFILTER = vFILTER + "CONS_ID, ";
                    vFILTER = vFILTER + "UNID_ID, ";
                    vFILTER = vFILTER + "DATAPROXLEIT, ";
                    vFILTER = vFILTER + "BANARREC, ";
                    vFILTER = vFILTER + "BANIDENT, ";
                    vFILTER = vFILTER + "FLGMEDFAT, ";
                    vFILTER = vFILTER + "TIPOUC, ";
                    vFILTER = vFILTER + "CFOP, ";
                    vFILTER = vFILTER + "ITENSSERV, ";
                    vFILTER = vFILTER + "TA, ";
                    vFILTER = vFILTER + "GRUPO, ";
                    vFILTER = vFILTER + "FASE, ";
                    vFILTER = vFILTER + "TENSAO, ";
                    vFILTER = vFILTER + "NUM_MED, ";
                    vFILTER = vFILTER + "CONST_MED, ";
                    vFILTER = vFILTER + "MOTIVOCANC, ";
                    vFILTER = vFILTER + "BANCO_FATUR, ";
                    vFILTER = vFILTER + "SOCIAL, ";
                    vFILTER = vFILTER + "MULTA, ";
                    vFILTER = vFILTER + "DATAPRAZO, ";
                    vFILTER = vFILTER + "DIAS, ";
                    vFILTER = vFILTER + "VALORLIQ, ";
                    vFILTER = vFILTER + "NSU, ";
                    vFILTER = vFILTER + "CODBARRA, ";
                    vFILTER = vFILTER + "DVCODBARRA, ";
                    vFILTER = vFILTER + "AGRUPA, ";
                    vFILTER = vFILTER + "AGRUPA_ROTA, ";
                    vFILTER = vFILTER + "ITENSIMP, ";
                    vFILTER = vFILTER + "SGDENVIO, ";
                    vFILTER = vFILTER + "MOTIVOPGTO, ";
                    vFILTER = vFILTER + "MOTIVOSEGVIA, ";
                    vFILTER = vFILTER + "USUARIOSEGVIA, ";
                    vFILTER = vFILTER + "DATASEGVIA, ";
                    vFILTER = vFILTER + "CPF_CNPJ, ";
                    vFILTER = vFILTER + "PARCIALMENTE_CATIVO, ";
                    vFILTER = vFILTER + "LIVREESPECIAL, ";
                    vFILTER = vFILTER + "PERIODO_TESTE, ";
                    vFILTER = vFILTER + "MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO, ";
                    vFILTER = vFILTER + "IRRIGACAO_AQUICULTURA, ";
                    vFILTER = vFILTER + "AGRUPA_CADERNO, ";
                    vFILTER = vFILTER + "AGRUPA_SEQUENCIA, ";
                    vFILTER = vFILTER + "CADERNO, ";
                    vFILTER = vFILTER + "CADERNO_SEQUENCIA, ";
                    vFILTER = vFILTER + "ROTA_QUATRO, ";
                    vFILTER = vFILTER + "AGENCIA, ";
                    vFILTER = vFILTER + "CONTA_CORRENTE, ";
                    vFILTER = vFILTER + "LIVRE_VERDE, ";
                    vFILTER = vFILTER + "CANCELADA_MESANO, ";
                    vFILTER = vFILTER + "CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + "ENVIO_BCO, ";
                    vFILTER = vFILTER + "FATURA_FINAL, ";
                    vFILTER = vFILTER + "PAG_GOV, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_DISTRIBUIDA, ";
                    vFILTER = vFILTER + "USINA, ";
                    vFILTER = vFILTER + "SUPRIMENTO, ";
                    vFILTER = vFILTER + "BAIXA_NFV, ";
                    vFILTER = vFILTER + "SITE_ENVIO, ";
                    vFILTER = vFILTER + "TARIFA_BRANCA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_CONVENIO_SEFAZ, ";
                    vFILTER = vFILTER + "REGIAO, ";
                    vFILTER = vFILTER + "FATURA_COMPLEMENTAR  ";

                    vINNERJOIN = " ";

                    vORDERBY = "ORDER BY MESANO_REF ASC LIMIT 0, 6 ";


                    vAND = vAND + "AND ZONA = ?ZONA ";
                    vAND = vAND + "AND LIVRO = ?LIVRO ";
                    vAND = vAND + "AND CONTA = ?CONTA ";
                    vAND = vAND + "AND CONS_ID = ?CONS_ID ";
                    vAND = vAND + "AND UNID_ID = ?UNID_ID ";
                    vAND = vAND + "AND FLGST = ?FLGST ";



                    cmd = new MySqlCommand(this.QuerySelect);

                    cmd.Parameters.AddWithValue("?ZONA", objNf.ZONA.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?LIVRO", objNf.LIVRO.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?CONTA", objNf.CONTA.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?CONS_ID", objNf.CONS_ID);
                    cmd.Parameters.AddWithValue("?UNID_ID", objNf.UNID_ID);
                    cmd.Parameters.AddWithValue("?FLGST", objNf.FLGST.ToString().ToUpper());


                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_19(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vFILTER = vFILTER + "NUMNF, ";
                    vFILTER = vFILTER + "CODCLAS, ";
                    vFILTER = vFILTER + "MESANO, ";
                    vFILTER = vFILTER + "MESANO_REF, ";
                    vFILTER = vFILTER + "DATAVENC, ";
                    vFILTER = vFILTER + "DATADIVIDA, ";
                    vFILTER = vFILTER + "DATALEIT, ";
                    vFILTER = vFILTER + "DATAAPR, ";
                    vFILTER = vFILTER + "DATAEM, ";
                    vFILTER = vFILTER + "DATALEITANT, ";
                    vFILTER = vFILTER + "DATAPAG, ";
                    vFILTER = vFILTER + "CODHASH, ";
                    vFILTER = vFILTER + "DATAST, ";
                    vFILTER = vFILTER + "FLGST, ";
                    vFILTER = vFILTER + "VALORTOT, ";
                    vFILTER = vFILTER + "ZONA, ";
                    vFILTER = vFILTER + "LIVRO, ";
                    vFILTER = vFILTER + "CONTA, ";
                    vFILTER = vFILTER + "ROTA, ";
                    vFILTER = vFILTER + "CONS_ID, ";
                    vFILTER = vFILTER + "UNID_ID, ";
                    vFILTER = vFILTER + "DATAPROXLEIT, ";
                    vFILTER = vFILTER + "BANARREC, ";
                    vFILTER = vFILTER + "BANIDENT, ";
                    vFILTER = vFILTER + "FLGMEDFAT, ";
                    vFILTER = vFILTER + "TIPOUC, ";
                    vFILTER = vFILTER + "CFOP, ";
                    vFILTER = vFILTER + "ITENSSERV, ";
                    vFILTER = vFILTER + "TA, ";
                    vFILTER = vFILTER + "GRUPO, ";
                    vFILTER = vFILTER + "FASE, ";
                    vFILTER = vFILTER + "TENSAO, ";
                    vFILTER = vFILTER + "NUM_MED, ";
                    vFILTER = vFILTER + "CONST_MED, ";
                    vFILTER = vFILTER + "MOTIVOCANC, ";
                    vFILTER = vFILTER + "BANCO_FATUR, ";
                    vFILTER = vFILTER + "SOCIAL, ";
                    vFILTER = vFILTER + "MULTA, ";
                    vFILTER = vFILTER + "DATAPRAZO, ";
                    vFILTER = vFILTER + "DIAS, ";
                    vFILTER = vFILTER + "VALORLIQ, ";
                    vFILTER = vFILTER + "NSU, ";
                    vFILTER = vFILTER + "CODBARRA, ";
                    vFILTER = vFILTER + "DVCODBARRA, ";
                    vFILTER = vFILTER + "AGRUPA, ";
                    vFILTER = vFILTER + "AGRUPA_ROTA, ";
                    vFILTER = vFILTER + "ITENSIMP, ";
                    vFILTER = vFILTER + "SGDENVIO, ";
                    vFILTER = vFILTER + "MOTIVOPGTO, ";
                    vFILTER = vFILTER + "MOTIVOSEGVIA, ";
                    vFILTER = vFILTER + "USUARIOSEGVIA, ";
                    vFILTER = vFILTER + "DATASEGVIA, ";
                    vFILTER = vFILTER + "CPF_CNPJ, ";
                    vFILTER = vFILTER + "PARCIALMENTE_CATIVO, ";
                    vFILTER = vFILTER + "LIVREESPECIAL, ";
                    vFILTER = vFILTER + "PERIODO_TESTE, ";
                    vFILTER = vFILTER + "MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO, ";
                    vFILTER = vFILTER + "IRRIGACAO_AQUICULTURA, ";
                    vFILTER = vFILTER + "AGRUPA_CADERNO, ";
                    vFILTER = vFILTER + "AGRUPA_SEQUENCIA, ";
                    vFILTER = vFILTER + "CADERNO, ";
                    vFILTER = vFILTER + "CADERNO_SEQUENCIA, ";
                    vFILTER = vFILTER + "ROTA_QUATRO, ";
                    vFILTER = vFILTER + "AGENCIA, ";
                    vFILTER = vFILTER + "CONTA_CORRENTE, ";
                    vFILTER = vFILTER + "LIVRE_VERDE, ";
                    vFILTER = vFILTER + "CANCELADA_MESANO, ";
                    vFILTER = vFILTER + "CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + "ENVIO_BCO, ";
                    vFILTER = vFILTER + "FATURA_FINAL, ";
                    vFILTER = vFILTER + "PAG_GOV, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_DISTRIBUIDA, ";
                    vFILTER = vFILTER + "USINA, ";
                    vFILTER = vFILTER + "SUPRIMENTO, ";
                    vFILTER = vFILTER + "BAIXA_NFV, ";
                    vFILTER = vFILTER + "SITE_ENVIO, ";
                    vFILTER = vFILTER + "TARIFA_BRANCA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_CONVENIO_SEFAZ, ";
                    vFILTER = vFILTER + "REGIAO, ";
                    vFILTER = vFILTER + "FATURA_COMPLEMENTAR  ";

                    vINNERJOIN = " ";

                    vORDERBY = " ";

                    vAND = vAND + "AND FLGST NOT IN (?FLGST) ";
                    vAND = vAND + "AND CONS_ID = ?CONS_ID ";
                    vAND = vAND + "AND UNID_ID = ?UNID_ID ";
                    vAND = vAND + "AND MESANO = ?MESANO ";
                    vAND = vAND + "AND GRUPO = ?GRUPO ";
                    vAND = vAND + "AND FATURA_FINAL = ?FATURA_FINAL ";


                    cmd = new MySqlCommand(this.QuerySelect);

                    cmd.Parameters.AddWithValue("?FLGST", objNf.FLGST.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?CONS_ID", objNf.CONS_ID);
                    cmd.Parameters.AddWithValue("?UNID_ID", objNf.UNID_ID);
                    cmd.Parameters.AddWithValue("?MESANO", objNf.MESANO.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?GRUPO", objNf.GRUPO.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?FATURA_FINAL", objNf.FATURA_FINAL);



                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_20(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vFILTER = vFILTER + "NUMNF, ";
                    vFILTER = vFILTER + "CODCLAS, ";
                    vFILTER = vFILTER + "MESANO, ";
                    vFILTER = vFILTER + "MESANO_REF, ";
                    vFILTER = vFILTER + "DATAVENC, ";
                    vFILTER = vFILTER + "DATADIVIDA, ";
                    vFILTER = vFILTER + "DATALEIT, ";
                    vFILTER = vFILTER + "DATAAPR, ";
                    vFILTER = vFILTER + "DATAEM, ";
                    vFILTER = vFILTER + "DATALEITANT, ";
                    vFILTER = vFILTER + "DATAPAG, ";
                    vFILTER = vFILTER + "CODHASH, ";
                    vFILTER = vFILTER + "DATAST, ";
                    vFILTER = vFILTER + "FLGST, ";
                    vFILTER = vFILTER + "VALORTOT, ";
                    vFILTER = vFILTER + "ZONA, ";
                    vFILTER = vFILTER + "LIVRO, ";
                    vFILTER = vFILTER + "CONTA, ";
                    vFILTER = vFILTER + "ROTA, ";
                    vFILTER = vFILTER + "CONS_ID, ";
                    vFILTER = vFILTER + "UNID_ID, ";
                    vFILTER = vFILTER + "DATAPROXLEIT, ";
                    vFILTER = vFILTER + "BANARREC, ";
                    vFILTER = vFILTER + "BANIDENT, ";
                    vFILTER = vFILTER + "FLGMEDFAT, ";
                    vFILTER = vFILTER + "TIPOUC, ";
                    vFILTER = vFILTER + "CFOP, ";
                    vFILTER = vFILTER + "ITENSSERV, ";
                    vFILTER = vFILTER + "TA, ";
                    vFILTER = vFILTER + "GRUPO, ";
                    vFILTER = vFILTER + "FASE, ";
                    vFILTER = vFILTER + "TENSAO, ";
                    vFILTER = vFILTER + "NUM_MED, ";
                    vFILTER = vFILTER + "CONST_MED, ";
                    vFILTER = vFILTER + "MOTIVOCANC, ";
                    vFILTER = vFILTER + "BANCO_FATUR, ";
                    vFILTER = vFILTER + "SOCIAL, ";
                    vFILTER = vFILTER + "MULTA, ";
                    vFILTER = vFILTER + "DATAPRAZO, ";
                    vFILTER = vFILTER + "DIAS, ";
                    vFILTER = vFILTER + "VALORLIQ, ";
                    vFILTER = vFILTER + "NSU, ";
                    vFILTER = vFILTER + "CODBARRA, ";
                    vFILTER = vFILTER + "DVCODBARRA, ";
                    vFILTER = vFILTER + "AGRUPA, ";
                    vFILTER = vFILTER + "AGRUPA_ROTA, ";
                    vFILTER = vFILTER + "ITENSIMP, ";
                    vFILTER = vFILTER + "SGDENVIO, ";
                    vFILTER = vFILTER + "MOTIVOPGTO, ";
                    vFILTER = vFILTER + "MOTIVOSEGVIA, ";
                    vFILTER = vFILTER + "USUARIOSEGVIA, ";
                    vFILTER = vFILTER + "DATASEGVIA, ";
                    vFILTER = vFILTER + "CPF_CNPJ, ";
                    vFILTER = vFILTER + "PARCIALMENTE_CATIVO, ";
                    vFILTER = vFILTER + "LIVREESPECIAL, ";
                    vFILTER = vFILTER + "PERIODO_TESTE, ";
                    vFILTER = vFILTER + "MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO, ";
                    vFILTER = vFILTER + "IRRIGACAO_AQUICULTURA, ";
                    vFILTER = vFILTER + "AGRUPA_CADERNO, ";
                    vFILTER = vFILTER + "AGRUPA_SEQUENCIA, ";
                    vFILTER = vFILTER + "CADERNO, ";
                    vFILTER = vFILTER + "CADERNO_SEQUENCIA, ";
                    vFILTER = vFILTER + "ROTA_QUATRO, ";
                    vFILTER = vFILTER + "AGENCIA, ";
                    vFILTER = vFILTER + "CONTA_CORRENTE, ";
                    vFILTER = vFILTER + "LIVRE_VERDE, ";
                    vFILTER = vFILTER + "CANCELADA_MESANO, ";
                    vFILTER = vFILTER + "CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + "ENVIO_BCO, ";
                    vFILTER = vFILTER + "FATURA_FINAL, ";
                    vFILTER = vFILTER + "PAG_GOV, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_DISTRIBUIDA, ";
                    vFILTER = vFILTER + "USINA, ";
                    vFILTER = vFILTER + "SUPRIMENTO, ";
                    vFILTER = vFILTER + "BAIXA_NFV, ";
                    vFILTER = vFILTER + "SITE_ENVIO, ";
                    vFILTER = vFILTER + "TARIFA_BRANCA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_CONVENIO_SEFAZ, ";
                    vFILTER = vFILTER + "REGIAO, ";
                    vFILTER = vFILTER + "FATURA_COMPLEMENTAR  ";

                    vINNERJOIN = " ";

                    vORDERBY = " ";

                    vAND = vAND + "AND NUMNF = ?NUMNF ";
                    vAND = vAND + "AND MESANO = ?MESANO ";
                    vAND = vAND + "AND GRUPO = ?GRUPO ";

                    cmd = new MySqlCommand(this.QuerySelect);

                    cmd.Parameters.AddWithValue("?NUMNF", objNf.NUMNF);
                    cmd.Parameters.AddWithValue("?MESANO", objNf.MESANO.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?GRUPO", objNf.GRUPO.ToString().ToUpper());

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_21(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vFILTER = vFILTER + "NUMNF, ";
                    vFILTER = vFILTER + "CODCLAS, ";
                    vFILTER = vFILTER + "MESANO, ";
                    vFILTER = vFILTER + "MESANO_REF, ";
                    vFILTER = vFILTER + "DATAVENC, ";
                    vFILTER = vFILTER + "DATADIVIDA, ";
                    vFILTER = vFILTER + "DATALEIT, ";
                    vFILTER = vFILTER + "DATAAPR, ";
                    vFILTER = vFILTER + "DATAEM, ";
                    vFILTER = vFILTER + "DATALEITANT, ";
                    vFILTER = vFILTER + "DATAPAG, ";
                    vFILTER = vFILTER + "CODHASH, ";
                    vFILTER = vFILTER + "DATAST, ";
                    vFILTER = vFILTER + "FLGST, ";
                    vFILTER = vFILTER + "VALORTOT, ";
                    vFILTER = vFILTER + "ZONA, ";
                    vFILTER = vFILTER + "LIVRO, ";
                    vFILTER = vFILTER + "CONTA, ";
                    vFILTER = vFILTER + "ROTA, ";
                    vFILTER = vFILTER + "CONS_ID, ";
                    vFILTER = vFILTER + "UNID_ID, ";
                    vFILTER = vFILTER + "DATAPROXLEIT, ";
                    vFILTER = vFILTER + "BANARREC, ";
                    vFILTER = vFILTER + "BANIDENT, ";
                    vFILTER = vFILTER + "FLGMEDFAT, ";
                    vFILTER = vFILTER + "TIPOUC, ";
                    vFILTER = vFILTER + "CFOP, ";
                    vFILTER = vFILTER + "ITENSSERV, ";
                    vFILTER = vFILTER + "TA, ";
                    vFILTER = vFILTER + "GRUPO, ";
                    vFILTER = vFILTER + "FASE, ";
                    vFILTER = vFILTER + "TENSAO, ";
                    vFILTER = vFILTER + "NUM_MED, ";
                    vFILTER = vFILTER + "CONST_MED, ";
                    vFILTER = vFILTER + "MOTIVOCANC, ";
                    vFILTER = vFILTER + "BANCO_FATUR, ";
                    vFILTER = vFILTER + "SOCIAL, ";
                    vFILTER = vFILTER + "MULTA, ";
                    vFILTER = vFILTER + "DATAPRAZO, ";
                    vFILTER = vFILTER + "DIAS, ";
                    vFILTER = vFILTER + "VALORLIQ, ";
                    vFILTER = vFILTER + "NSU, ";
                    vFILTER = vFILTER + "CODBARRA, ";
                    vFILTER = vFILTER + "DVCODBARRA, ";
                    vFILTER = vFILTER + "AGRUPA, ";
                    vFILTER = vFILTER + "AGRUPA_ROTA, ";
                    vFILTER = vFILTER + "ITENSIMP, ";
                    vFILTER = vFILTER + "SGDENVIO, ";
                    vFILTER = vFILTER + "MOTIVOPGTO, ";
                    vFILTER = vFILTER + "MOTIVOSEGVIA, ";
                    vFILTER = vFILTER + "USUARIOSEGVIA, ";
                    vFILTER = vFILTER + "DATASEGVIA, ";
                    vFILTER = vFILTER + "CPF_CNPJ, ";
                    vFILTER = vFILTER + "PARCIALMENTE_CATIVO, ";
                    vFILTER = vFILTER + "LIVREESPECIAL, ";
                    vFILTER = vFILTER + "PERIODO_TESTE, ";
                    vFILTER = vFILTER + "MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO, ";
                    vFILTER = vFILTER + "IRRIGACAO_AQUICULTURA, ";
                    vFILTER = vFILTER + "AGRUPA_CADERNO, ";
                    vFILTER = vFILTER + "AGRUPA_SEQUENCIA, ";
                    vFILTER = vFILTER + "CADERNO, ";
                    vFILTER = vFILTER + "CADERNO_SEQUENCIA, ";
                    vFILTER = vFILTER + "ROTA_QUATRO, ";
                    vFILTER = vFILTER + "AGENCIA, ";
                    vFILTER = vFILTER + "CONTA_CORRENTE, ";
                    vFILTER = vFILTER + "LIVRE_VERDE, ";
                    vFILTER = vFILTER + "CANCELADA_MESANO, ";
                    vFILTER = vFILTER + "CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + "ENVIO_BCO, ";
                    vFILTER = vFILTER + "FATURA_FINAL, ";
                    vFILTER = vFILTER + "PAG_GOV, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_DISTRIBUIDA, ";
                    vFILTER = vFILTER + "USINA, ";
                    vFILTER = vFILTER + "SUPRIMENTO, ";
                    vFILTER = vFILTER + "BAIXA_NFV, ";
                    vFILTER = vFILTER + "SITE_ENVIO, ";
                    vFILTER = vFILTER + "TARIFA_BRANCA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_CONVENIO_SEFAZ, ";
                    vFILTER = vFILTER + "REGIAO, ";
                    vFILTER = vFILTER + "FATURA_COMPLEMENTAR  ";

                    vINNERJOIN = " ";

                    vORDERBY = " ";

                    vAND = vAND + "AND CANCELADA_NUMNF = ?CANCELADA_NUMNF ";
                    vAND = vAND + "AND CANCELADA_MESANO = ?CANCELADA_MESANO ";
                    vAND = vAND + "AND GRUPO = ?GRUPO ";

                    cmd = new MySqlCommand(this.QuerySelect);

                    cmd.Parameters.AddWithValue("?CANCELADA_NUMNF", objNf.CANCELADA_NUMNF);
                    cmd.Parameters.AddWithValue("?CANCELADA_MESANO", objNf.CANCELADA_MESANO.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?GRUPO", objNf.GRUPO.ToString().ToUpper());

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_22(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vFILTER = vFILTER + "NUMNF, ";
                    vFILTER = vFILTER + "CODCLAS, ";
                    vFILTER = vFILTER + "MESANO, ";
                    vFILTER = vFILTER + "MESANO_REF, ";
                    vFILTER = vFILTER + "DATAVENC, ";
                    vFILTER = vFILTER + "DATADIVIDA, ";
                    vFILTER = vFILTER + "DATALEIT, ";
                    vFILTER = vFILTER + "DATAAPR, ";
                    vFILTER = vFILTER + "DATAEM, ";
                    vFILTER = vFILTER + "DATALEITANT, ";
                    vFILTER = vFILTER + "DATAPAG, ";
                    vFILTER = vFILTER + "CODHASH, ";
                    vFILTER = vFILTER + "DATAST, ";
                    vFILTER = vFILTER + "FLGST, ";
                    vFILTER = vFILTER + "VALORTOT, ";
                    vFILTER = vFILTER + "ZONA, ";
                    vFILTER = vFILTER + "LIVRO, ";
                    vFILTER = vFILTER + "CONTA, ";
                    vFILTER = vFILTER + "ROTA, ";
                    vFILTER = vFILTER + "CONS_ID, ";
                    vFILTER = vFILTER + "UNID_ID, ";
                    vFILTER = vFILTER + "DATAPROXLEIT, ";
                    vFILTER = vFILTER + "BANARREC, ";
                    vFILTER = vFILTER + "BANIDENT, ";
                    vFILTER = vFILTER + "FLGMEDFAT, ";
                    vFILTER = vFILTER + "TIPOUC, ";
                    vFILTER = vFILTER + "CFOP, ";
                    vFILTER = vFILTER + "ITENSSERV, ";
                    vFILTER = vFILTER + "TA, ";
                    vFILTER = vFILTER + "GRUPO, ";
                    vFILTER = vFILTER + "FASE, ";
                    vFILTER = vFILTER + "TENSAO, ";
                    vFILTER = vFILTER + "NUM_MED, ";
                    vFILTER = vFILTER + "CONST_MED, ";
                    vFILTER = vFILTER + "MOTIVOCANC, ";
                    vFILTER = vFILTER + "BANCO_FATUR, ";
                    vFILTER = vFILTER + "SOCIAL, ";
                    vFILTER = vFILTER + "MULTA, ";
                    vFILTER = vFILTER + "DATAPRAZO, ";
                    vFILTER = vFILTER + "DIAS, ";
                    vFILTER = vFILTER + "VALORLIQ, ";
                    vFILTER = vFILTER + "NSU, ";
                    vFILTER = vFILTER + "CODBARRA, ";
                    vFILTER = vFILTER + "DVCODBARRA, ";
                    vFILTER = vFILTER + "AGRUPA, ";
                    vFILTER = vFILTER + "AGRUPA_ROTA, ";
                    vFILTER = vFILTER + "ITENSIMP, ";
                    vFILTER = vFILTER + "SGDENVIO, ";
                    vFILTER = vFILTER + "MOTIVOPGTO, ";
                    vFILTER = vFILTER + "MOTIVOSEGVIA, ";
                    vFILTER = vFILTER + "USUARIOSEGVIA, ";
                    vFILTER = vFILTER + "DATASEGVIA, ";
                    vFILTER = vFILTER + "CPF_CNPJ, ";
                    vFILTER = vFILTER + "PARCIALMENTE_CATIVO, ";
                    vFILTER = vFILTER + "LIVREESPECIAL, ";
                    vFILTER = vFILTER + "PERIODO_TESTE, ";
                    vFILTER = vFILTER + "MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO, ";
                    vFILTER = vFILTER + "IRRIGACAO_AQUICULTURA, ";
                    vFILTER = vFILTER + "AGRUPA_CADERNO, ";
                    vFILTER = vFILTER + "AGRUPA_SEQUENCIA, ";
                    vFILTER = vFILTER + "CADERNO, ";
                    vFILTER = vFILTER + "CADERNO_SEQUENCIA, ";
                    vFILTER = vFILTER + "ROTA_QUATRO, ";
                    vFILTER = vFILTER + "AGENCIA, ";
                    vFILTER = vFILTER + "CONTA_CORRENTE, ";
                    vFILTER = vFILTER + "LIVRE_VERDE, ";
                    vFILTER = vFILTER + "CANCELADA_MESANO, ";
                    vFILTER = vFILTER + "CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + "ENVIO_BCO, ";
                    vFILTER = vFILTER + "FATURA_FINAL, ";
                    vFILTER = vFILTER + "PAG_GOV, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_DISTRIBUIDA, ";
                    vFILTER = vFILTER + "USINA, ";
                    vFILTER = vFILTER + "SUPRIMENTO, ";
                    vFILTER = vFILTER + "BAIXA_NFV, ";
                    vFILTER = vFILTER + "SITE_ENVIO, ";
                    vFILTER = vFILTER + "TARIFA_BRANCA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_CONVENIO_SEFAZ, ";
                    vFILTER = vFILTER + "REGIAO, ";
                    vFILTER = vFILTER + "FATURA_COMPLEMENTAR  ";

                    vINNERJOIN = " ";

                    vORDERBY = " ";

                    vAND = vAND + "AND NUMNF = ?NUMNF ";

                    cmd = new MySqlCommand(this.QuerySelect);

                    cmd.Parameters.AddWithValue("?NUMNF", objNf.NUMNF);

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_23(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    //vFILTER = vFILTER + "NUMNF, ";
                    //vFILTER = vFILTER + "CODCLAS, ";
                    //vFILTER = vFILTER + "MESANO, ";
                    //vFILTER = vFILTER + "MESANO_REF, ";
                    //vFILTER = vFILTER + "DATAVENC, ";
                    //vFILTER = vFILTER + "DATADIVIDA, ";
                    //vFILTER = vFILTER + "ROTA, ";
                    //vFILTER = vFILTER + "CONS_ID, ";
                    //vFILTER = vFILTER + "REGIAO, ";
                    //vFILTER = vFILTER + "FATURA_COMPLEMENTAR  ";

                    //vINNERJOIN = " ";

                    //vORDERBY = "GROUP BY 1 ";


                    //vFILTER = vFILTER + " DISTINCT LEFT(ROTA ,4) AS ROTA ";
                    vFILTER = vFILTER + " DISTINCT CAST(LEFT(ROTA ,4) AS DECIMAL) AS ROTA  ";

                    vINNERJOIN = " ";

                    vORDERBY = "ORDER BY ROTA ASC  ";

                    vAND = vAND + "   AND  MESANO = ?MESANO ";
                    vAND = vAND + "   AND GRUPO = ?GRUPO ";
                    vAND = vAND + "   AND FATURA_COMPLEMENTAR " + objNf.AGENCIA.ToString() + " ?FATURA_COMPLEMENTAR ";


                    cmd = new MySqlCommand(this.QuerySelect);


                    cmd.Parameters.AddWithValue("?MESANO", objNf.MESANO);
                    cmd.Parameters.AddWithValue("?GRUPO", objNf.GRUPO.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?FATURA_COMPLEMENTAR", objNf.FATURA_COMPLEMENTAR);

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Select_24(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vFILTER = vFILTER + "NUMNF, ";
                    vFILTER = vFILTER + "DATAPAG ";

                    vINNERJOIN = " ";

                    vORDERBY = " ";

                    vAND = vAND + "AND  DATAPAG <> '0000-00-00'  ";
                    vAND = vAND + "AND  DATAPAG >= '" + objNf.DATAPAG.ToShortDateString() + "'";

                    cmd = new MySqlCommand(this.QuerySelect);

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable SelectCount(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {
                    vFILTER = vFILTER + "COUNT(*) ";

                    vINNERJOIN = " ";

                    vORDERBY = "LIMIT 0, 1 ";

                    if (objNf.UNID_ID != 0)
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND UNID_ID = ?UNID_ID "; }
                        else
                        { vAND = "AND UNID_ID = ?UNID_ID "; }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }

                    if (!string.IsNullOrEmpty(objNf.ZONA))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND ZONA = ?ZONA "; }
                        else
                        { vAND = "AND ZONA = ?ZONA "; }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }

                    if (!string.IsNullOrEmpty(objNf.LIVRO))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND LIVRO = ?LIVRO "; }
                        else
                        { vAND = "AND LIVRO = ?LIVRO "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }


                    if (!string.IsNullOrEmpty(objNf.CONTA))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND CONTA = ?CONTA "; }
                        else
                        { vAND = "AND CONTA = ?CONTA "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }


                    if (!string.IsNullOrEmpty(objNf.GRUPO))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND GRUPO = ?GRUPO "; }
                        else
                        { vAND = "AND GRUPO = ?GRUPO "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }

                    cmd = new MySqlCommand(this.QuerySelect);

                    if (objNf.UNID_ID != 0)
                    {
                        cmd.Parameters.AddWithValue("?UNID_ID", objNf.UNID_ID);
                    }

                    if (!string.IsNullOrEmpty(objNf.ZONA))
                    {
                        cmd.Parameters.AddWithValue("?ZONA", objNf.ZONA.ToString().ToUpper());
                    }

                    if (!string.IsNullOrEmpty(objNf.LIVRO))
                    {
                        cmd.Parameters.AddWithValue("?LIVRO", objNf.LIVRO.ToString().ToUpper());
                    }

                    if (!string.IsNullOrEmpty(objNf.CONTA))
                    {
                        cmd.Parameters.AddWithValue("?CONTA", objNf.CONTA.ToString().ToUpper());
                    }

                    if (!string.IsNullOrEmpty(objNf.GRUPO))
                    {
                        cmd.Parameters.AddWithValue("?GRUPO", objNf.GRUPO.ToString().ToUpper());
                    }

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable SelectCount_1(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {
                    vFILTER = vFILTER + "COUNT(*) ";

                    vINNERJOIN = " ";

                    vORDERBY = "ORDER BY CONCAT(RIGHT(mesano,4),LEFT(mesano,2)) DESC LIMIT 1";


                    if (!string.IsNullOrEmpty(objNf.ZONA))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND ZONA = ?ZONA "; }
                        else
                        { vAND = "AND ZONA = ?ZONA "; }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }

                    if (!string.IsNullOrEmpty(objNf.LIVRO))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND LIVRO = ?LIVRO "; }
                        else
                        { vAND = "AND LIVRO = ?LIVRO "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }


                    if (!string.IsNullOrEmpty(objNf.CONTA))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND CONTA = ?CONTA "; }
                        else
                        { vAND = "AND CONTA = ?CONTA "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }


                    if (!string.IsNullOrEmpty(objNf.FLGST))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND FLGST = ?FLGST "; }
                        else
                        { vAND = "AND FLGST = ?FLGST "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }

                    cmd = new MySqlCommand(this.QuerySelect);


                    if (!string.IsNullOrEmpty(objNf.ZONA))
                    {
                        cmd.Parameters.AddWithValue("?ZONA", objNf.ZONA.ToString().ToUpper());
                    }

                    if (!string.IsNullOrEmpty(objNf.LIVRO))
                    {
                        cmd.Parameters.AddWithValue("?LIVRO", objNf.LIVRO.ToString().ToUpper());
                    }

                    if (!string.IsNullOrEmpty(objNf.CONTA))
                    {
                        cmd.Parameters.AddWithValue("?CONTA", objNf.CONTA.ToString().ToUpper());
                    }

                    if (!string.IsNullOrEmpty(objNf.FLGST))
                    {
                        cmd.Parameters.AddWithValue("?FLGST", objNf.FLGST.ToString().ToUpper());
                    }

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable SelectCount_2(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {
                    vFILTER = vFILTER + "COUNT(*) ";

                    vINNERJOIN = " ";

                    vORDERBY = " ";

                    if (objNf.UNID_ID != 0)
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND UNID_ID = ?UNID_ID "; }
                        else
                        { vAND = "AND UNID_ID = ?UNID_ID "; }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }

                    if (objNf.CONS_ID != 0)
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND CONS_ID = ?CONS_ID "; }
                        else
                        { vAND = "AND CONS_ID = ?CONS_ID "; }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }

                    if (!string.IsNullOrEmpty(objNf.ZONA))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND ZONA = ?ZONA "; }
                        else
                        { vAND = "AND ZONA = ?ZONA "; }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }

                    if (!string.IsNullOrEmpty(objNf.LIVRO))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND LIVRO = ?LIVRO "; }
                        else
                        { vAND = "AND LIVRO = ?LIVRO "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }


                    if (!string.IsNullOrEmpty(objNf.CONTA))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND CONTA = ?CONTA "; }
                        else
                        { vAND = "AND CONTA = ?CONTA "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }


                    if (objNf.MESANO_REF != 0)
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND MESANO_REF = ?MESANO_REF "; }
                        else
                        { vAND = "AND MESANO_REF = ?MESANO_REF "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }

                    cmd = new MySqlCommand(this.QuerySelect);

                    if (objNf.UNID_ID != 0)
                    {
                        cmd.Parameters.AddWithValue("?UNID_ID", objNf.UNID_ID);
                    }

                    if (objNf.CONS_ID != 0)
                    {
                        cmd.Parameters.AddWithValue("?CONS_ID", objNf.CONS_ID);
                    }

                    if (!string.IsNullOrEmpty(objNf.ZONA))
                    {
                        cmd.Parameters.AddWithValue("?ZONA", objNf.ZONA.ToString().ToUpper());
                    }

                    if (!string.IsNullOrEmpty(objNf.LIVRO))
                    {
                        cmd.Parameters.AddWithValue("?LIVRO", objNf.LIVRO.ToString().ToUpper());
                    }

                    if (!string.IsNullOrEmpty(objNf.CONTA))
                    {
                        cmd.Parameters.AddWithValue("?CONTA", objNf.CONTA.ToString().ToUpper());
                    }

                    if (objNf.MESANO_REF != 0)
                    {
                        cmd.Parameters.AddWithValue("?MESANO_REF", objNf.MESANO_REF);
                    }

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable SelectCount_3(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vFILTER = vFILTER + "COUNT(*) AS QTD ";


                    vINNERJOIN = " ";

                    vORDERBY = " ";

                    vAND = vAND + "   AND MESANO_REF  = ?MESANO_REF ";
                    vAND = vAND + "   AND MESANO = ?MESANO ";
                    vAND = vAND + "   AND CONS_ID = ?CONS_ID ";
                    vAND = vAND + "   AND UNID_ID = ?UNID_ID ";
                    vAND = vAND + "   AND GRUPO = ?GRUPO ";
                    vAND = vAND + "   AND FLGMEDFAT <> ?FLGMEDFAT ";
                    vAND = vAND + "   AND FATURA_FINAL = ?FATURA_FINAL ";


                    cmd = new MySqlCommand(this.QuerySelect);


                    cmd.Parameters.AddWithValue("?MESANO_REF", objNf.MESANO_REF);
                    cmd.Parameters.AddWithValue("?MESANO", objNf.MESANO.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?CONS_ID", objNf.CONS_ID);
                    cmd.Parameters.AddWithValue("?UNID_ID", objNf.UNID_ID);
                    cmd.Parameters.AddWithValue("?GRUPO", objNf.GRUPO.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?FLGMEDFAT", objNf.FLGMEDFAT.ToString().ToUpper());
                    cmd.Parameters.AddWithValue("?FATURA_FINAL", objNf.FATURA_FINAL);


                    dt = objGerenciadorTransacao.GetDataTable(cmd);

                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable SelectOthersTables(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vFILTER = vFILTER + " NF.NUMNF, ";
                    vFILTER = vFILTER + " NF.GRUPO, ";
                    vFILTER = vFILTER + " NF.MESANO, ";
                    vFILTER = vFILTER + " NF.VALORTOT, ";
                    vFILTER = vFILTER + " NF.CODCLAS, ";
                    vFILTER = vFILTER + " NF.DATAST, ";
                    vFILTER = vFILTER + " NF.MOTIVOCANC, ";
                    vFILTER = vFILTER + " NF.TA, ";
                    vFILTER = vFILTER + " NF.ITENSSERV, ";
                    vFILTER = vFILTER + " NF.ZONA, ";
                    vFILTER = vFILTER + " NF.LIVRO, ";
                    vFILTER = vFILTER + " NF.CONTA, ";

                    vFILTER = vFILTER + " IFNULL(IFNULL(UC_FATURAMENTO.NOME, UC_FATURAMENTO.NOME), CONSUM.NOME) AS NOMEUC ";

                    vINNERJOIN = vINNERJOIN + " LEFT JOIN UC_FATURAMENTO ON (UC_FATURAMENTO.ID  = NF.UNID_ID AND UC_FATURAMENTO.CONS_ID = NF.CONS_ID AND MID(NF.TA, 3, 1) IN ('A','B')) ";
                    vINNERJOIN = vINNERJOIN + " LEFT JOIN CONSUM ON (CONSUM.CONSREG = NF.CONS_ID) ";

                    vORDERBY = " ";

                    if (!string.IsNullOrEmpty(objNf.FLGST))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND FLGST = ?FLGST "; }
                        else
                        { vAND = "AND FLGST = ?FLGST "; }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = " "; }
                    }


                    if (objNf.DATAINICIAL.Year != 1 && objNf.DATAFINAL.Year != 1)
                    {
                        vAND = vAND + " AND DATAST BETWEEN ?DATAINICIAL AND ?DATAFINAL ";
                    }

                    cmd = new MySqlCommand(this.QuerySelect);

                    if (!string.IsNullOrEmpty(objNf.FLGST))
                    {
                        cmd.Parameters.AddWithValue("?FLGST", objNf.FLGST);
                    }

                    if (objNf.DATAINICIAL.Year != 1 && objNf.DATAFINAL.Year != 1)
                    {
                        cmd.Parameters.AddWithValue("?DATAINICIAL", objNf.DATAINICIAL);
                        cmd.Parameters.AddWithValue("?DATAFINAL", objNf.DATAFINAL);
                    }

                    dt = objGerenciadorTransacao.GetDataTable(cmd);

                }

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable SelectOthersTables_1(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vFILTER = vFILTER + "SELECT  ";
                    vFILTER = vFILTER + "NUMNF, ";
                    vFILTER = vFILTER + "CODCLAS, ";
                    vFILTER = vFILTER + "MESANO, ";
                    vFILTER = vFILTER + "MESANO_REF, ";
                    vFILTER = vFILTER + "DATAVENC, ";
                    vFILTER = vFILTER + "DATADIVIDA, ";
                    vFILTER = vFILTER + "DATALEIT, ";
                    vFILTER = vFILTER + "DATAAPR, ";
                    vFILTER = vFILTER + "DATAEM, ";
                    vFILTER = vFILTER + "DATALEITANT, ";
                    vFILTER = vFILTER + "DATAPAG, ";
                    vFILTER = vFILTER + "CODHASH, ";
                    vFILTER = vFILTER + "DATAST, ";
                    vFILTER = vFILTER + "FLGST, ";
                    vFILTER = vFILTER + "VALORTOT, ";
                    vFILTER = vFILTER + "ZONA, ";
                    vFILTER = vFILTER + "LIVRO, ";
                    vFILTER = vFILTER + "CONTA, ";
                    vFILTER = vFILTER + "ROTA, ";
                    vFILTER = vFILTER + "CONS_ID, ";
                    vFILTER = vFILTER + "UNID_ID, ";
                    vFILTER = vFILTER + "DATAPROXLEIT, ";
                    vFILTER = vFILTER + "BANARREC, ";
                    vFILTER = vFILTER + "BANIDENT, ";
                    vFILTER = vFILTER + "FLGMEDFAT, ";
                    vFILTER = vFILTER + "TIPOUC, ";
                    vFILTER = vFILTER + "CFOP, ";
                    vFILTER = vFILTER + "ITENSSERV, ";
                    vFILTER = vFILTER + "TA, ";
                    vFILTER = vFILTER + "GRUPO, ";
                    vFILTER = vFILTER + "FASE, ";
                    vFILTER = vFILTER + "TENSAO, ";
                    vFILTER = vFILTER + "NUM_MED, ";
                    vFILTER = vFILTER + "CONST_MED, ";
                    vFILTER = vFILTER + "MOTIVOCANC, ";
                    vFILTER = vFILTER + "BANCO_FATUR, ";
                    vFILTER = vFILTER + "SOCIAL, ";
                    vFILTER = vFILTER + "MULTA, ";
                    vFILTER = vFILTER + "DATAPRAZO, ";
                    vFILTER = vFILTER + "DIAS, ";
                    vFILTER = vFILTER + "VALORLIQ, ";
                    vFILTER = vFILTER + "NSU, ";
                    vFILTER = vFILTER + "CODBARRA, ";
                    vFILTER = vFILTER + "DVCODBARRA, ";
                    vFILTER = vFILTER + "AGRUPA, ";
                    vFILTER = vFILTER + "AGRUPA_ROTA, ";
                    vFILTER = vFILTER + "ITENSIMP, ";
                    vFILTER = vFILTER + "SGDENVIO, ";
                    vFILTER = vFILTER + "MOTIVOPGTO, ";
                    vFILTER = vFILTER + "MOTIVOSEGVIA, ";
                    vFILTER = vFILTER + "USUARIOSEGVIA, ";
                    vFILTER = vFILTER + "DATASEGVIA, ";
                    vFILTER = vFILTER + "CPF_CNPJ, ";
                    vFILTER = vFILTER + "PARCIALMENTE_CATIVO, ";
                    vFILTER = vFILTER + "LIVREESPECIAL, ";
                    vFILTER = vFILTER + "PERIODO_TESTE, ";
                    vFILTER = vFILTER + "MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO, ";
                    vFILTER = vFILTER + "IRRIGACAO_AQUICULTURA, ";
                    vFILTER = vFILTER + "AGRUPA_CADERNO, ";
                    vFILTER = vFILTER + "AGRUPA_SEQUENCIA, ";
                    vFILTER = vFILTER + "CADERNO, ";
                    vFILTER = vFILTER + "CADERNO_SEQUENCIA, ";
                    vFILTER = vFILTER + "ROTA_QUATRO, ";
                    vFILTER = vFILTER + "AGENCIA, ";
                    vFILTER = vFILTER + "CONTA_CORRENTE, ";
                    vFILTER = vFILTER + "LIVRE_VERDE, ";
                    vFILTER = vFILTER + "CANCELADA_MESANO, ";
                    vFILTER = vFILTER + "CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + "ENVIO_BCO, ";
                    vFILTER = vFILTER + "FATURA_FINAL, ";
                    vFILTER = vFILTER + "PAG_GOV, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_DISTRIBUIDA, ";
                    vFILTER = vFILTER + "USINA, ";
                    vFILTER = vFILTER + "SUPRIMENTO, ";
                    vFILTER = vFILTER + "BAIXA_NFV, ";
                    vFILTER = vFILTER + "SITE_ENVIO, ";
                    vFILTER = vFILTER + "TARIFA_BRANCA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_CONVENIO_SEFAZ, ";
                    vFILTER = vFILTER + "REGIAO, ";
                    vFILTER = vFILTER + "FATURA_COMPLEMENTAR  ";
                    vFILTER = vFILTER + "FROM  NF ";
                    vFILTER = vFILTER + "WHERE 1 = 1 ";

                    vORDERBY = " ";

                    vAND = vFILTER;


                    if (!string.IsNullOrEmpty(objNf.ZONA))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND ZONA = ?ZONA "; }
                        else
                        { vAND = vAND + "AND ZONA = ?ZONA "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = vAND + " "; }
                    }

                    if (!string.IsNullOrEmpty(objNf.LIVRO))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND LIVRO = ?LIVRO "; }
                        else
                        { vAND = vAND + "AND LIVRO = ?LIVRO "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = vAND + " "; }
                    }


                    if (!string.IsNullOrEmpty(objNf.CONTA))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND CONTA = ?CONTA "; }
                        else
                        { vAND = vAND + "AND CONTA = ?CONTA "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = vAND + " "; }
                    }


                    if (!string.IsNullOrEmpty(objNf.GRUPO))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND GRUPO = ?GRUPO "; }
                        else
                        { vAND = vAND + "AND GRUPO = ?GRUPO "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = vAND + " "; }
                    }


                    vFILTER = vAND;

                    vFILTER = vFILTER + "UNION ALL ";
                    vFILTER = vFILTER + "SELECT  ";
                    vFILTER = vFILTER + "NUMNF, ";
                    vFILTER = vFILTER + "CODCLAS, ";
                    vFILTER = vFILTER + "MESANO, ";
                    vFILTER = vFILTER + "MESANO_REF, ";
                    vFILTER = vFILTER + "DATAVENC, ";
                    vFILTER = vFILTER + "DATADIVIDA, ";
                    vFILTER = vFILTER + "DATALEIT, ";
                    vFILTER = vFILTER + "DATAAPR, ";
                    vFILTER = vFILTER + "DATAEM, ";
                    vFILTER = vFILTER + "DATALEITANT, ";
                    vFILTER = vFILTER + "DATAPAG, ";
                    vFILTER = vFILTER + "CODHASH, ";
                    vFILTER = vFILTER + "DATAST, ";
                    vFILTER = vFILTER + "FLGST, ";
                    vFILTER = vFILTER + "VALORTOT, ";
                    vFILTER = vFILTER + "ZONA, ";
                    vFILTER = vFILTER + "LIVRO, ";
                    vFILTER = vFILTER + "CONTA, ";
                    vFILTER = vFILTER + "ROTA, ";
                    vFILTER = vFILTER + "CONS_ID, ";
                    vFILTER = vFILTER + "UNID_ID, ";
                    vFILTER = vFILTER + "DATAPROXLEIT, ";
                    vFILTER = vFILTER + "BANARREC, ";
                    vFILTER = vFILTER + "BANIDENT, ";
                    vFILTER = vFILTER + "FLGMEDFAT, ";
                    vFILTER = vFILTER + "TIPOUC, ";
                    vFILTER = vFILTER + "CFOP, ";
                    vFILTER = vFILTER + "ITENSSERV, ";
                    vFILTER = vFILTER + "TA, ";
                    vFILTER = vFILTER + "GRUPO, ";
                    vFILTER = vFILTER + "FASE, ";
                    vFILTER = vFILTER + "TENSAO, ";
                    vFILTER = vFILTER + "NUM_MED, ";
                    vFILTER = vFILTER + "CONST_MED, ";
                    vFILTER = vFILTER + "MOTIVOCANC, ";
                    vFILTER = vFILTER + "BANCO_FATUR, ";
                    vFILTER = vFILTER + "SOCIAL, ";
                    vFILTER = vFILTER + "MULTA, ";
                    vFILTER = vFILTER + "DATAPRAZO, ";
                    vFILTER = vFILTER + "DIAS, ";
                    vFILTER = vFILTER + "VALORLIQ, ";
                    vFILTER = vFILTER + "NSU, ";
                    vFILTER = vFILTER + "CODBARRA, ";
                    vFILTER = vFILTER + "DVCODBARRA, ";
                    vFILTER = vFILTER + "AGRUPA, ";
                    vFILTER = vFILTER + "AGRUPA_ROTA, ";
                    vFILTER = vFILTER + "ITENSIMP, ";
                    vFILTER = vFILTER + "SGDENVIO, ";
                    vFILTER = vFILTER + "MOTIVOPGTO, ";
                    vFILTER = vFILTER + "MOTIVOSEGVIA, ";
                    vFILTER = vFILTER + "USUARIOSEGVIA, ";
                    vFILTER = vFILTER + "DATASEGVIA, ";
                    vFILTER = vFILTER + "CPF_CNPJ, ";
                    vFILTER = vFILTER + "PARCIALMENTE_CATIVO, ";
                    vFILTER = vFILTER + "LIVREESPECIAL, ";
                    vFILTER = vFILTER + "PERIODO_TESTE, ";
                    vFILTER = vFILTER + "MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO, ";
                    vFILTER = vFILTER + "IRRIGACAO_AQUICULTURA, ";
                    vFILTER = vFILTER + "AGRUPA_CADERNO, ";
                    vFILTER = vFILTER + "AGRUPA_SEQUENCIA, ";
                    vFILTER = vFILTER + "CADERNO, ";
                    vFILTER = vFILTER + "CADERNO_SEQUENCIA, ";
                    vFILTER = vFILTER + "ROTA_QUATRO, ";
                    vFILTER = vFILTER + "AGENCIA, ";
                    vFILTER = vFILTER + "CONTA_CORRENTE, ";
                    vFILTER = vFILTER + "LIVRE_VERDE, ";
                    vFILTER = vFILTER + "CANCELADA_MESANO, ";
                    vFILTER = vFILTER + "CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + "ENVIO_BCO, ";
                    vFILTER = vFILTER + "FATURA_FINAL, ";
                    vFILTER = vFILTER + "PAG_GOV, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_DISTRIBUIDA, ";
                    vFILTER = vFILTER + "USINA, ";
                    vFILTER = vFILTER + "SUPRIMENTO, ";
                    vFILTER = vFILTER + "BAIXA_NFV, ";
                    vFILTER = vFILTER + "SITE_ENVIO, ";
                    vFILTER = vFILTER + "TARIFA_BRANCA, ";
                    vFILTER = vFILTER + "MICRO_GERACAO_CONVENIO_SEFAZ, ";
                    vFILTER = vFILTER + "REGIAO, ";
                    vFILTER = vFILTER + "FATURA_COMPLEMENTAR  ";
                    vFILTER = vFILTER + "FROM  NFTRANSICAO ";
                    vFILTER = vFILTER + "WHERE 1 = 1 ";


                    vAND = vFILTER;


                    if (!string.IsNullOrEmpty(objNf.ZONA))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND ZONA = ?ZONA "; }
                        else
                        { vAND = vAND + "AND ZONA = ?ZONA "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = vAND + " "; }
                    }

                    if (!string.IsNullOrEmpty(objNf.LIVRO))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND LIVRO = ?LIVRO "; }
                        else
                        { vAND = vAND + "AND LIVRO = ?LIVRO "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = vAND + " "; }
                    }


                    if (!string.IsNullOrEmpty(objNf.CONTA))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND CONTA = ?CONTA "; }
                        else
                        { vAND = vAND + "AND CONTA = ?CONTA "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = vAND + " "; }
                    }


                    if (!string.IsNullOrEmpty(objNf.GRUPO))
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + "AND GRUPO = ?GRUPO "; }
                        else
                        { vAND = vAND + "AND GRUPO = ?GRUPO "; }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(vAND))
                        { vAND = vAND + " "; }
                        else
                        { vAND = vAND + " "; }
                    }


                    cmd = new MySqlCommand(this.QueryExecute);


                    if (!string.IsNullOrEmpty(objNf.ZONA))
                    {
                        cmd.Parameters.AddWithValue("?ZONA", objNf.ZONA.ToString().ToUpper());
                    }

                    if (!string.IsNullOrEmpty(objNf.LIVRO))
                    {
                        cmd.Parameters.AddWithValue("?LIVRO", objNf.LIVRO.ToString().ToUpper());
                    }

                    if (!string.IsNullOrEmpty(objNf.CONTA))
                    {
                        cmd.Parameters.AddWithValue("?CONTA", objNf.CONTA.ToString().ToUpper());
                    }

                    if (!string.IsNullOrEmpty(objNf.GRUPO))
                    {
                        cmd.Parameters.AddWithValue("?GRUPO", objNf.GRUPO.ToString().ToUpper());
                    }


                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable SelectOthersTables_2(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vFILTER = vFILTER + " NF.*, CONSUM.CONSREG AS C_CONSREG, CONSUM.MATRICULA AS C_MATRICULA ";

                    vINNERJOIN = vINNERJOIN + " AS NF ";
                    vINNERJOIN = vINNERJOIN + " LEFT JOIN CONSUM ON(CONSUM.CONSREG = NF.CONS_ID) ";

                    vORDERBY = " ";

                    vAND = vAND + " AND NF.ITENSSERV IS NOT NULL ";

                    if (objNf.ROTA_QUATRO == "1")
                    {
                        if (objNf.ROTAINI != 0 || objNf.ROTAFIM != 0)
                        {
                            vAND = vAND + "   AND NF.ROTA_QUATRO BETWEEN ?ROTAINI AND ?ROTAFIM ";
                        }
                    }

                    if (objNf.CADERNO == 1)
                    {
                        if (objNf.ROTAINI != 0 || objNf.ROTAFIM != 0)
                        {
                            vAND = vAND + "   AND NF.CADERNO BETWEEN ?ROTAINI AND ?ROTAFIM ";
                        }
                    }


                    vAND = vAND + " AND ( ";

                    if (objNf.REN863_2019 == false)
                    {
                        vAND = vAND + "  (NF.MESANO_REF >= ?MESANOINI AND ";
                        vAND = vAND + "  NF.MESANO_REF <= ?MESANOFIM )";
                    }
                    else
                    {
                        vAND = vAND + "  (NF.ANOMES_FAT >= ?MESANOINI AND ";
                        vAND = vAND + "  NF.ANOMES_FAT <= ?MESANOFIM )";
                    }

                    vAND = vAND + " OR ( ";
                    vAND = vAND + "        NF.FLGST = ?FLGST AND ";
                    vAND = vAND + "         NF.MESANO_REF <> DATE_FORMAT(DATAST,'%Y%M') AND ";
                    vAND = vAND + "         NF.DATAST BETWEEN DATE_FORMAT(?DATAINICIAL,'%Y%M%D') AND DATE_FORMAT(?DATAFINAL,'%Y%M%D') ";
                    vAND = vAND + "   )";
                    vAND = vAND + " )";

                    if (objNf.ZONA != "")
                    {

                        vAND = vAND + " AND NF.ZONA IN ( " + objNf.ZONA + " ) ";

                    }

                    if (objNf.GRUPO != "")
                    {
                        vAND = vAND + " AND NF.GRUPO = ?GRUPO ";
                    }

                    if (objNf.MICRO_GERACAO != 0 && objNf.MICRO_GERACAO_DISTRIBUIDA != 0)
                    {
                        vAND = vAND + "   AND (NF.MICRO_GERACAO = ?MICRO_GERACAO OR NF.MICRO_GERACAO_DISTRIBUIDA = ?MICRO_GERACAO_DISTRIBUIDA) ";

                        if (objNf.MICRO_GERACAO_CONVENIO_SEFAZ == 1)
                        {
                            vAND = vAND + " AND NF.MICRO_GERACAO_CONVENIO_SEFAZ  = ?MICRO_GERACAO_CONVENIO_SEFAZ";
                        }
                        else if (objNf.MICRO_GERACAO_CONVENIO_SEFAZ == 0)
                        {
                            vAND = vAND + "  AND NF.MICRO_GERACAO_CONVENIO_SEFAZ  = ?MICRO_GERACAO_CONVENIO_SEFAZ";
                        }


                    }

                    if (objNf.REGIAO != "")
                    {
                        vAND = vAND + "  AND NF.REGIAO = ?REGIAO";
                    }

                    if (objNf.FATURA_COMPLEMENTAR == 1)
                    {
                        vAND = vAND + "  AND NF.FATURA_COMPLEMENTAR > 0";
                    }
                    else if (objNf.FATURA_COMPLEMENTAR == 0)
                    {
                        vAND = vAND + "  AND NF.FATURA_COMPLEMENTAR = 0";
                    }

                    vAND = vAND + objNf.FLGMEDFAT.ToString().ToUpper();


                    cmd = new MySqlCommand(this.QuerySelect);


                    if (objNf.REGIAO != "")
                    {
                        cmd.Parameters.AddWithValue("?REGIAO", objNf.REGIAO);
                    }

                    if (objNf.MICRO_GERACAO != 0 && objNf.MICRO_GERACAO_DISTRIBUIDA != 0)
                    {

                        cmd.Parameters.AddWithValue("?MICRO_GERACAO", objNf.MICRO_GERACAO);
                        cmd.Parameters.AddWithValue("?MICRO_GERACAO_DISTRIBUIDA", objNf.MICRO_GERACAO_DISTRIBUIDA);

                        if (objNf.MICRO_GERACAO_CONVENIO_SEFAZ == 1)
                        {
                            cmd.Parameters.AddWithValue("?MICRO_GERACAO_CONVENIO_SEFAZ", objNf.MICRO_GERACAO_CONVENIO_SEFAZ);
                        }
                        else if (objNf.MICRO_GERACAO_CONVENIO_SEFAZ == 0)
                        {
                            cmd.Parameters.AddWithValue("?MICRO_GERACAO_CONVENIO_SEFAZ", objNf.MICRO_GERACAO_CONVENIO_SEFAZ);
                        }

                    }
                    if (objNf.GRUPO != "")
                    {
                        cmd.Parameters.AddWithValue("?GRUPO", objNf.GRUPO);
                    }

                    if (objNf.ZONA != "")
                    {
                        cmd.Parameters.AddWithValue("?ZONA", objNf.ZONA);

                    }

                    cmd.Parameters.AddWithValue("?FLGST", objNf.FLGST);

                    if (objNf.DATAINICIAL.Year != 1 && objNf.DATAFINAL.Year != 1)
                    {
                        cmd.Parameters.AddWithValue("?DATAINICIAL", objNf.DATAINICIAL);
                        cmd.Parameters.AddWithValue("?DATAFINAL", objNf.DATAFINAL);
                    }

                    if (!string.IsNullOrEmpty(objNf.MESANOINI) && !string.IsNullOrEmpty(objNf.MESANOFIM))
                    {
                        cmd.Parameters.AddWithValue("?MESANOINI", objNf.MESANOINI);
                        cmd.Parameters.AddWithValue("?MESANOFIM", objNf.MESANOFIM);
                    }


                    if (objNf.CADERNO == 1)
                    {
                        if (objNf.ROTAINI != 0 || objNf.ROTAFIM != 0)
                        {
                            cmd.Parameters.AddWithValue("?ROTAINI", objNf.ROTAINI);
                            cmd.Parameters.AddWithValue("?ROTAFIM", objNf.ROTAFIM);
                        }
                    }

                    dt = objGerenciadorTransacao.GetDataTable(cmd);

                }

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable SelectOthersTables_4(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {
                    vFILTER = vFILTER + " SELECT DISTINCT CADERNO FROM NF ";
                    vFILTER = vFILTER + " UNION ";
                    vFILTER = vFILTER + " SELECT DISTINCT CADERNO FROM NFTRANSICAO ";

                    vORDERBY = " ORDER BY 1 ASC ";

                    vAND = vFILTER;

                    cmd = new MySqlCommand(this.QueryExecute);

                    dt = objGerenciadorTransacao.GetDataTable(cmd);

                }

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable SelectOthersTables_5(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {
                    vFILTER = vFILTER + " SELECT DISTINCT LEFT(ROTA_QUATRO, 2) PREFIXO  FROM NF ";
                    vFILTER = vFILTER + " UNION ";
                    vFILTER = vFILTER + " SELECT DISTINCT LEFT(ROTA_QUATRO, 2) PREFIXO FROM NFTRANSICAO ";

                    vORDERBY = " ORDER BY 1 ASC ";

                    vAND = vFILTER;

                    cmd = new MySqlCommand(this.QueryExecute);

                    dt = objGerenciadorTransacao.GetDataTable(cmd);

                }

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable SelectOthersTables_6(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vFILTER = vFILTER + "  DISTINCT DATALEIT  ";

                    vORDERBY = " GROUP BY 1 ";


                    if (objNf.REN863_2019)
                    {
                        vAND = vAND + "  AND ANOMES_FAT >= ?MESANOINI AND ";
                        vAND = vAND + "  ANOMES_FAT <= ?MESANOFIM ";
                    }
                    else
                    {
                        vAND = vAND + "  AND MESANO_REF >= ?MESANOINI AND ";
                        vAND = vAND + "  MESANO_REF <= ?MESANOFIM ";
                    }

                    cmd = new MySqlCommand(this.QuerySelect);

                    if (!string.IsNullOrEmpty(objNf.MESANOINI) && !string.IsNullOrEmpty(objNf.MESANOFIM))
                    {
                        cmd.Parameters.AddWithValue("?MESANOINI", objNf.MESANOINI);
                        cmd.Parameters.AddWithValue("?MESANOFIM", objNf.MESANOFIM);
                    }

                    dt = objGerenciadorTransacao.GetDataTable(cmd);

                }

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable SelectOthersTables_7(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {
                    vFILTER = vFILTER + "		SELECT ";
                    vFILTER = vFILTER + "	    FATURAS.NUMNF                    AS FATURAS_NUMNF, ";
                    vFILTER = vFILTER + "       FATURAS.CFOP                     AS FATURAS_CFOP, ";
                    vFILTER = vFILTER + "       FATURAS.ZONA                     AS FATURAS_ZONA, ";
                    vFILTER = vFILTER + "       FATURAS.LIVRO                    AS FATURAS_LIVRO, ";
                    vFILTER = vFILTER + "       FATURAS.CONTA                    AS FATURAS_CONTA, ";
                    vFILTER = vFILTER + "       FATURAS.ROTA                     AS FATURAS_ROTA, ";
                    vFILTER = vFILTER + "       FATURAS.VALORTOT                 AS FATURAS_VALORTOT, ";
                    vFILTER = vFILTER + "       FATURAS.VALORLIQ                 AS FATURAS_VALORLIQ, ";
                    vFILTER = vFILTER + "       FATURAS.CODCLAS                  AS FATURAS_CODCLAS, ";
                    vFILTER = vFILTER + "       FATURAS.DATAVENC                 AS FATURAS_DATAVENC, ";
                    vFILTER = vFILTER + "       FATURAS.BANIDENT                 AS FATURAS_BANIDENT, ";
                    vFILTER = vFILTER + "       FATURAS.BANCO_FATUR              AS FATURAS_BANCO_FATUR, ";
                    vFILTER = vFILTER + "       FATURAS.MESANO                   AS FATURAS_MESANO, ";
                    vFILTER = vFILTER + "       FATURAS.MESANO_REF               AS FATURAS_MESANO_REF, ";
                    vFILTER = vFILTER + "       FATURAS.ANOMES_FAT               AS FATURAS_ANOMES_FAT, ";
                    vFILTER = vFILTER + "       FATURAS.DATAAPR                  AS FATURAS_DATAAPR, ";
                    vFILTER = vFILTER + "       FATURAS.DATAEM                   AS FATURAS_DATAEM, ";
                    vFILTER = vFILTER + "       FATURAS.DATAPROXLEIT             AS FATURAS_DATAPROXLEIT, ";
                    vFILTER = vFILTER + "       FATURAS.DATALEIT                 AS FATURAS_DATALEIT, ";
                    vFILTER = vFILTER + "       FATURAS.DATALEITANT              AS FATURAS_DATALEITANT, ";
                    vFILTER = vFILTER + "       FATURAS.FASE                     AS FATURAS_FASE, ";
                    vFILTER = vFILTER + "       FATURAS.TA                       AS FATURAS_TA, ";
                    vFILTER = vFILTER + "       FATURAS.TENSAO                   AS FATURAS_TENSAO, ";
                    vFILTER = vFILTER + "       FATURAS.CODHASH                  AS FATURAS_CODHASH, ";
                    vFILTER = vFILTER + "       FATURAS.ITENSSERV                AS FATURAS_ITENSSERV, ";
                    vFILTER = vFILTER + "       FATURAS.ITENSIMP                 AS FATURAS_ITENSIMP, ";
                    vFILTER = vFILTER + "       FATURAS.NSU                      AS FATURAS_NSU, ";
                    vFILTER = vFILTER + "       FATURAS.CODBARRA                 AS FATURAS_CODBARRA, ";
                    vFILTER = vFILTER + "       FATURAS.DVCODBARRA               AS FATURAS_DVCODBARRA, ";
                    vFILTER = vFILTER + "       FATURAS.CODBARRA_IMAGEM          AS FATURAS_CODBARRA_IMAGEM, ";
                    vFILTER = vFILTER + "       FATURAS.GRUPO                    AS FATURAS_GRUPO, ";
                    vFILTER = vFILTER + "       FATURAS.CONS_ID                  AS FATURAS_CONS_ID, ";
                    vFILTER = vFILTER + "       FATURAS.UNID_ID                  AS FATURAS_UNID_ID, ";
                    vFILTER = vFILTER + "       FATURAS.PERIODO_TESTE            AS FATURAS_PERIODO_TESTE, ";
                    vFILTER = vFILTER + "       FATURAS.MENSAGEM_BANDEIRA        AS FATURAS_MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + "       FATURAS.LIVRE_VERDE              AS FATURAS_LIVRE_VERDE, ";
                    vFILTER = vFILTER + "       FATURAS.MICRO_GERACAO            AS FATURAS_MICRO_GERACAO, ";
                    vFILTER = vFILTER + "       FATURAS.PARCIALMENTE_CATIVO      AS FATURAS_PARCIALMENTE_CATIVO, ";
                    vFILTER = vFILTER + "       FATURAS.FLGST                    AS FATURAS_FLGST, ";
                    vFILTER = vFILTER + "       FATURAS.CANCELADA_MESANO         AS FATURAS_CANCELADA_MESANO, ";
                    vFILTER = vFILTER + "       FATURAS.CANCELADA_NUMNF          AS FATURAS_CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + "       CLASSE.CODIGO                    AS CLASSE_CODIGO, ";
                    vFILTER = vFILTER + "       CLASSE.DESCRICAO                 AS CLASSE_DESCRICAO, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.CPF_CNPJ          AS UC_FATURAMENTO_CPF_CNPJ, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.INSC              AS UC_FATURAMENTO_INSC, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.RG                AS UC_FATURAMENTO_RG, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.AGENC             AS UC_FATURAMENTO_AGENC, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.CC                AS UC_FATURAMENTO_CC, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.NOME              AS UC_FATURAMENTO_NOME, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.CIDADE            AS UC_FATURAMENTO_CIDADE, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.DISTRITO          AS UC_FATURAMENTO_DISTRITO, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.BAIRRO            AS UC_FATURAMENTO_BAIRRO, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.END               AS UC_FATURAMENTO_END, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.NUMERO            AS UC_FATURAMENTO_NUMERO, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.COMPLEMENT        AS UC_FATURAMENTO_COMPLEMENT, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.CEP               AS UC_FATURAMENTO_CEP, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.UF                AS UC_FATURAMENTO_UF, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.QUADRA            AS UC_FATURAMENTO_QUADRA, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.LOTE              AS UC_FATURAMENTO_LOTE, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.CID_ENTREGA       AS UC_FATURAMENTO_CID_ENTREGA, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.DIS_ENTREGA       AS UC_FATURAMENTO_DIS_ENTREGA, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.BAI_ENTREGA       AS UC_FATURAMENTO_BAI_ENTREGA, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.END_ENTREGA       AS UC_FATURAMENTO_END_ENTREGA, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.NUM_ENTREGA       AS UC_FATURAMENTO_NUM_ENTREGA, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.COMPL_ENTREGA     AS UC_FATURAMENTO_COMPL_ENTREGA, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.CEP_ENTREGA       AS UC_FATURAMENTO_CEP_ENTREGA, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.UF_ENTREGA        AS UC_FATURAMENTO_UF_ENTREGA, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.QUA_ENTREGA       AS UC_FATURAMENTO_QUA_ENTREGA, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.LOT_ENTREGA       AS UC_FATURAMENTO_LOT_ENTREGA, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.GRUPO             AS UC_FATURAMENTO_GRUPO, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.CONST_KW_P        AS UC_FATURAMENTO_CONST_DEMANDA, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.CONST_KW_FP       AS UC_FATURAMENTO_CONST_DEMANDA_FP, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.CONST_KWH_FP      AS UC_FATURAMENTO_CONST_CONS_FP, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.CONST_KWH_P       AS UC_FATURAMENTO_CONST_CONS_P, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.CONST_KVARH_P     AS UC_FATURAMENTO_CONST_REATIVO_P, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.CONST_KVARH_FP    AS UC_FATURAMENTO_CONST_REATIVO_FP, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.NUM               AS UC_FATURAMENTO_MEDIDORDEMANDA, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.MEDKVARK          AS UC_FATURAMENTO_MEDIDORREATIVO, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.ELETR             AS UC_FATURAMENTO_MEDIDORCONSUMO, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.DEM_CONTR_PONTA   AS UC_FATURAMENTO_DEM_CONTR_PONTA, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.DEM_CONTR_FP      AS UC_FATURAMENTO_DEM_CONTR_FP, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.CONST_KVARH       AS UC_FATURAMENTO_CONST_REATIVO, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.CONST_CONSUMO     AS UC_FATURAMENTO_CONST_CONSUMO, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.DEMANDA_CONTR     AS UC_FATURAMENTO_DEMANDA_CONTR, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.CARGA             AS UC_FATURAMENTO_CARGA, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.FINAL             AS UC_FATURAMENTO_FINAL, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.UNID_ID           AS UC_FATURAMENTO_UNID_ID, ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.SEQUENCIA         AS UC_FATURAMENTO_SEQUENCIA, ";
                    vFILTER = vFILTER + "       CONSUM.CONSREG                   AS CONSUMIDOR_CONSREG, ";
                    vFILTER = vFILTER + "       CONSUM.NOME                      AS CONSUMIDOR_NOME, ";
                    vFILTER = vFILTER + "       CONSUM.CEP                       AS CONSUMIDOR_CEP, ";
                    vFILTER = vFILTER + "       CONSUM.UF                        AS CONSUMIDOR_UF, ";
                    vFILTER = vFILTER + "       CONSUM.CGC                       AS CONSUMIDOR_CGC, ";
                    vFILTER = vFILTER + "       CONSUM.INSC                      AS CONSUMIDOR_INSC, ";
                    vFILTER = vFILTER + "       CONSUM.RG                        AS CONSUMIDOR_RG, ";
                    vFILTER = vFILTER + "       CONSUM.MATRICULA                 AS CONSUMIDOR_MATRICULA   ";
                    vFILTER = vFILTER + "   FROM NF AS FATURAS ";
                    vFILTER = vFILTER + "   LEFT JOIN UC_FATURAMENTO  ON(UC_FATURAMENTO.ID =  FATURAS.UNID_ID) ";
                    vFILTER = vFILTER + "   LEFT JOIN CONSUM ON(CONSUM.CONSREG =  FATURAS.CONS_ID) ";
                    vFILTER = vFILTER + "   LEFT JOIN CLASSE ON(CLASSE.CODIGO =  FATURAS.CODCLAS) ";
                    vFILTER = vFILTER + "   WHERE 1 = 1  ";

                    vFILTER = vFILTER + objNf.TA.ToString();

                    vORDERBY = "ORDER BY UC_FATURAMENTO.SEQUENCIA ASC ";

                    vAND = vFILTER;

                    cmd = new MySqlCommand(this.QueryExecute);
                    cmd.CommandTimeout = 3600;

                    dt = objGerenciadorTransacao.GetDataTable(cmd);

                }

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable SelectOthersTables_8(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {
                    vFILTER = vFILTER + "		SELECT ";
                    vFILTER = vFILTER + "       FATURAS.NUMNF                    AS FATURAS_NUMNF        , ";
                    vFILTER = vFILTER + "       FATURAS.CFOP                     AS FATURAS_CFOP         , ";
                    vFILTER = vFILTER + "       FATURAS.ZONA                     AS FATURAS_ZONA         , ";
                    vFILTER = vFILTER + "       FATURAS.LIVRO                    AS FATURAS_LIVRO        , ";
                    vFILTER = vFILTER + "       FATURAS.CONTA                    AS FATURAS_CONTA        , ";
                    vFILTER = vFILTER + "       FATURAS.ROTA                     AS FATURAS_ROTA         , ";
                    vFILTER = vFILTER + "       FATURAS.AGRUPA_ROTA              AS FATURAS_ROTA         , ";
                    vFILTER = vFILTER + "       FATURAS.UNID_ID                  AS FATURAS_UNID_ID      , ";
                    vFILTER = vFILTER + "       FATURAS.CONS_ID                  AS FATURAS_CONS_ID      , ";
                    vFILTER = vFILTER + "       FATURAS.VALORTOT                 AS FATURAS_VALORTOT     , ";
                    vFILTER = vFILTER + "       FATURAS.VALORLIQ                 AS FATURAS_VALORLIQ     , ";
                    vFILTER = vFILTER + "       FATURAS.CODCLAS                  AS FATURAS_CODCLAS      , ";
                    vFILTER = vFILTER + "       FATURAS.DATAVENC                 AS FATURAS_DATAVENC     , ";
                    vFILTER = vFILTER + "       FATURAS.BANIDENT                 AS FATURAS_BANIDENT     , ";
                    vFILTER = vFILTER + "       FATURAS.BANCO_FATUR              AS FATURAS_BANCO_FATUR  , ";
                    vFILTER = vFILTER + "       FATURAS.MESANO                   AS FATURAS_MESANO       , ";
                    vFILTER = vFILTER + "       FATURAS.ANOMES_FAT               AS FATURAS_ANOMES_FAT   , ";
                    vFILTER = vFILTER + "       FATURAS.MESANO_REF               AS FATURAS_MESANO_REF   , ";
                    vFILTER = vFILTER + "       FATURAS.DATAAPR                  AS FATURAS_DATAAPR      , ";
                    vFILTER = vFILTER + "       FATURAS.DATAEM                   AS FATURAS_DATAEM       , ";
                    vFILTER = vFILTER + "       FATURAS.DATAPROXLEIT             AS FATURAS_DATAPROXLEIT , ";
                    vFILTER = vFILTER + "       FATURAS.DATALEIT                 AS FATURAS_DATALEIT     , ";
                    vFILTER = vFILTER + "       FATURAS.DATALEITANT              AS FATURAS_DATALEITANT  , ";
                    vFILTER = vFILTER + "       FATURAS.FASE                     AS FATURAS_FASE         , ";
                    vFILTER = vFILTER + "       FATURAS.TA                       AS FATURAS_TA           , ";
                    vFILTER = vFILTER + "       FATURAS.TENSAO                   AS FATURAS_TENSAO       , ";
                    vFILTER = vFILTER + "       FATURAS.CODHASH                  AS FATURAS_CODHASH      , ";
                    vFILTER = vFILTER + "       FATURAS.ITENSSERV                AS FATURAS_ITENSSERV    , ";
                    vFILTER = vFILTER + "       FATURAS.NSU                      AS FATURAS_NSU          , ";
                    vFILTER = vFILTER + "       FATURAS.CODBARRA                 AS FATURAS_CODBARRA     , ";
                    vFILTER = vFILTER + "       FATURAS.DVCODBARRA               AS FATURAS_DVCODBARRA   , ";
                    vFILTER = vFILTER + "       FATURAS.CODBARRA_IMAGEM          AS FATURAS_CODBARRA_IMAGEM , ";
                    vFILTER = vFILTER + "       FATURAS.NUM_MED                  AS FATURAS_NUM_MED      , ";
                    vFILTER = vFILTER + "       FATURAS.CONST_MED                AS FATURAS_CONST_MED    , ";
                    vFILTER = vFILTER + "       FATURAS.ITENSIMP                 AS FATURAS_ITENSIMP     , ";
                    vFILTER = vFILTER + "       FATURAS.DIAS                     AS FATURAS_DIAS         , ";
                    vFILTER = vFILTER + "       FATURAS.MENSAGEM_BANDEIRA        AS FATURAS_MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + "       FATURAS.IRRIGACAO_AQUICULTURA    AS FATURAS_IRRIGACAO_AQUICULTURA, ";
                    vFILTER = vFILTER + "       FATURAS.MICRO_GERACAO            AS FATURAS_MICRO_GERACAO, ";
                    vFILTER = vFILTER + "       FATURAS.MICRO_GERACAO_DISTRIBUIDA AS FATURAS_MICRO_GERACAO_DISTRIBUIDA, ";
                    vFILTER = vFILTER + "       FATURAS.TARIFA_BRANCA            AS FATURAS_TARIFA_BRANCA, ";
                    vFILTER = vFILTER + "       FATURAS.FLGST                    AS FATURAS_FLGST, ";
                    vFILTER = vFILTER + "       FATURAS.CANCELADA_MESANO         AS FATURAS_CANCELADA_MESANO, ";
                    vFILTER = vFILTER + "       FATURAS.CANCELADA_NUMNF          AS FATURAS_CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + "       CLASSE.CODIGO                    AS CLASSE_CODIGO        , ";
                    vFILTER = vFILTER + "       CLASSE.DESCRICAO                 AS CLASSE_DESCRICAO     , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.CPF_CNPJ          AS UC_FATURAMENTO_CPF_CNPJ       , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.INSC              AS UC_FATURAMENTO_INSC           , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.RG                AS UC_FATURAMENTO_RG             , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.AGENC             AS UC_FATURAMENTO_AGENC          , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.CC                AS UC_FATURAMENTO_CC             , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.NOME              AS UC_FATURAMENTO_NOME           , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.CIDADE            AS UC_FATURAMENTO_CIDADE         , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.DISTRITO          AS UC_FATURAMENTO_DISTRITO       , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.BAIRRO            AS UC_FATURAMENTO_BAIRRO         , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.END               AS UC_FATURAMENTO_END            , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.NUMERO            AS UC_FATURAMENTO_NUMERO         , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.COMPLEMENT        AS UC_FATURAMENTO_COMPLEMENT     , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.CEP               AS UC_FATURAMENTO_CEP            , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.UF                AS UC_FATURAMENTO_UF             , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.QUADRA            AS UC_FATURAMENTO_QUADRA         , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.LOTE              AS UC_FATURAMENTO_LOTE           , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.ENTREGA           AS UC_FATURAMENTO_ENTREGA        , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.CID_ENTREGA       AS UC_FATURAMENTO_CID_ENTREGA    , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.DIS_ENTREGA       AS UC_FATURAMENTO_DIS_ENTREGA    , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.BAI_ENTREGA       AS UC_FATURAMENTO_BAI_ENTREGA    , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.END_ENTREGA       AS UC_FATURAMENTO_END_ENTREGA    , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.NUM_ENTREGA       AS UC_FATURAMENTO_NUM_ENTREGA    , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.COMPL_ENTREGA     AS UC_FATURAMENTO_COMPL_ENTREGA  , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.CEP_ENTREGA       AS UC_FATURAMENTO_CEP_ENTREGA    , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.UF_ENTREGA        AS UC_FATURAMENTO_UF_ENTREGA     , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.QUA_ENTREGA       AS UC_FATURAMENTO_QUA_ENTREGA    , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.LOT_ENTREGA       AS UC_FATURAMENTO_LOT_ENTREGA    , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.ULTLEIT           AS UC_FATURAMENTO_ULTLEIT        , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.LEIT              AS UC_FATURAMENTO_LEIT           , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.GRUPO             AS UC_FATURAMENTO_GRUPO          , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.REGIAO            AS UC_FATURAMENTO_REGIAO         , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.UNID_ID           AS UC_FATURAMENTO_UNID_ID        , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.CONS_ID           AS UC_FATURAMENTO_CONS_ID        , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.CONJUGE_FATURA    AS UC_FATURAMENTO_CONJUGE_FATURA , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.SEQUENCIA         AS UC_FATURAMENTO_SEQUENCIA      , ";
                    vFILTER = vFILTER + "       UC_FATURAMENTO.ORDEM_JUDICIAL    AS UC_FATURAMENTO_ORDEM_JUDICIAL , ";
                    vFILTER = vFILTER + "       CONSUM.NOME                      AS CONSUMIDOR_NOME      , ";
                    vFILTER = vFILTER + "       CONSUM.NOME_CONJUGE              AS CONSUMIDOR_CONJUGE   , ";
                    vFILTER = vFILTER + "       CONSUM.CEP                       AS CONSUMIDOR_CEP       , ";
                    vFILTER = vFILTER + "       CONSUM.UF                        AS CONSUMIDOR_UF        , ";
                    vFILTER = vFILTER + "       CONSUM.CGC                       AS CONSUMIDOR_CGC       , ";
                    vFILTER = vFILTER + "       CONSUM.INSC                      AS CONSUMIDOR_INSC      , ";
                    vFILTER = vFILTER + "       CONSUM.RG                        AS CONSUMIDOR_RG        , ";
                    vFILTER = vFILTER + "       CONSUM.MATRICULA                 AS CONSUMIDOR_MATRICULA   ";
                    vFILTER = vFILTER + "   FROM NF AS FATURAS ";
                    vFILTER = vFILTER + "   LEFT JOIN UC_FATURAMENTO ON(UC_FATURAMENTO.ID =  FATURAS.UNID_ID) ";
                    vFILTER = vFILTER + "   LEFT JOIN CONSUM ON(CONSUM.CONSREG =  FATURAS.CONS_ID) ";
                    vFILTER = vFILTER + "   LEFT JOIN CLASSE ON(CLASSE.CODIGO =  FATURAS.CODCLAS) ";
                    vFILTER = vFILTER + "   WHERE 1 = 1  ";
                    vFILTER = vFILTER + "   And Ifnull(flgst,'') <> 'C' ";

                    //vFILTER = vFILTER + "   AND FATURAS.ANOMES_FAT = 202307 ";
                    vFILTER = vFILTER + "   AND FATURAS.GRUPO = 'B' ";
                    vFILTER = vFILTER + "   and not exists(select 1 from temp_fatura_nf3e_b temp where FATURAS.numnf = temp.numnf) ";

                    //vFILTER = vFILTER + "   and FATURAS.numnf = '328145' ";

                    vFILTER = vFILTER + objNf.TA.ToString();


                    vORDERBY = "ORDER BY UC_FATURAMENTO.SEQUENCIA ASC ";
                    //vORDERBY = "ORDER BY UC_FATURAMENTO.SEQUENCIA ASC Limit 50 ";

                    vAND = vFILTER;

                    cmd = new MySqlCommand(this.QueryExecute);
                    cmd.CommandTimeout = 3600;

                    dt = objGerenciadorTransacao.GetDataTable(cmd);

                }

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable SelectOthersTables_9(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {


                    vFILTER = vFILTER + "       NF.NUMNF, ";
                    vFILTER = vFILTER + "       NF.MESANO, ";
                    vFILTER = vFILTER + "       NF.MESANO_REF, ";
                    vFILTER = vFILTER + "       NF.ROTA, ";
                    vFILTER = vFILTER + "       NF.ZONA, ";
                    vFILTER = vFILTER + "       NF.LIVRO, ";
                    vFILTER = vFILTER + "       NF.CONTA, ";
                    vFILTER = vFILTER + "       NF.CONS_ID, ";
                    vFILTER = vFILTER + "       NF.UNID_ID, ";
                    vFILTER = vFILTER + "       NF.FLGST, ";
                    vFILTER = vFILTER + "       NF.DATAPAG, ";
                    vFILTER = vFILTER + "       NF.DATAVENC, ";
                    vFILTER = vFILTER + "       NF.DATAST, ";
                    vFILTER = vFILTER + "       NF.CODCLAS, ";
                    vFILTER = vFILTER + "       NF.TA, ";
                    vFILTER = vFILTER + "       NF.GRUPO, ";
                    vFILTER = vFILTER + "       NF.ITENSSERV, ";
                    vFILTER = vFILTER + "       NF.VALORLIQ, ";
                    vFILTER = vFILTER + "       NF.VALORTOT, ";
                    vFILTER = vFILTER + "       NF.BANARREC, ";
                    vFILTER = vFILTER + "       NF.MICRO_GERACAO_CONVENIO_SEFAZ, ";
                    vFILTER = vFILTER + "       RESERVA.NUMNF   CADERNO, ";
                    vFILTER = vFILTER + "       RESERVA.MESANO  RESERVA_MESANO, ";
                    vFILTER = vFILTER + "       RESERVA.DATARES RESERVA_DATARES ";

                    vINNERJOIN = vINNERJOIN + "        LEFT JOIN RESERVA ";
                    vINNERJOIN = vINNERJOIN + "               ON((RESERVA.NUMNF = NF.NUMNF AND RESERVA.MESANO = NF.MESANO AND  DATE_FORMAT( RESERVA.DATARES, '%d/%m/%Y' ) <=  ?DATARESFIM ))";


                    vORDERBY = " ";

                    if (objNf.FLGST == "P")
                    {
                        vAND = vAND + "   AND (NF.FLGST = ?FLGST  AND DATE_FORMAT( NF.DATAPAG, '%Y-%m-%d' ) BETWEEN ?DATAPAGINI  AND ?DATAPAGFIM ) ";
                    }
                    else if (objNf.FLGST == "R")
                    { vAND = vAND + "   AND ((NF.FLGST = ?FLGST AND DATE_FORMAT(NF.DATAPAG, '%Y-%m-%d' ) BETWEEN ?DATAPAGINI AND ?DATAPAGFIM ) OR (NF.FLGST <> ?FLGST AND DATE_FORMAT(RESERVA.DATARES, '%Y-%m-%d' ) BETWEEN ?DATARESINI AND ?DATARESFIM )) "; }

                    if (objNf.MICRO_GERACAO == 1 && objNf.MICRO_GERACAO_DISTRIBUIDA == 1)
                    {
                        vAND = vAND + "   AND (NF.MICRO_GERACAO = ?MICRO_GERACAO OR NF.MICRO_GERACAO_DISTRIBUIDA = ?MICRO_GERACAO_DISTRIBUIDA ) ";

                        if (objNf.MICRO_GERACAO_CONVENIO_SEFAZ == 1)
                        { vAND = vAND + "  AND NF.MICRO_GERACAO_CONVENIO_SEFAZ  = ?MICRO_GERACAO_CONVENIO_SEFAZ "; }
                        else if (objNf.MICRO_GERACAO_CONVENIO_SEFAZ == 0)
                        { vAND = vAND + "  AND NF.MICRO_GERACAO_CONVENIO_SEFAZ  = ?MICRO_GERACAO_CONVENIO_SEFAZ "; }


                    }




                    //vAND = vAND + " AND NF.CODCLAS < 4000";
                    //vAND = vAND + " AND (NF.MICRO_GERACAO = 0 AND NF.MICRO_GERACAO_DISTRIBUIDA = 0) ";
                    //vAND = vAND + " AND NF.GRUPO = 'A' AND NF.TA <> '01AL00' AND NF.MICRO_GERACAO = 0  AND NF.MICRO_GERACAO_DISTRIBUIDA = 0";
                    //vAND = vAND + " AND NF.GRUPO = 'B' AND NF.MICRO_GERACAO = 0  AND NF.MICRO_GERACAO_DISTRIBUIDA = 0";
                    //vAND = vAND + " AND NF.TA = '01AL00' ";

                    cmd = new MySqlCommand(this.QuerySelect);
                    cmd.CommandTimeout = 3600;

                    cmd.Parameters.AddWithValue("?DATARESFIM", objNf.DATAFINAL.ToString("dd/MM/yyyy"));

                    if (objNf.FLGST == "P")
                    {
                        cmd.Parameters.AddWithValue("?FLGST", objNf.FLGST);
                        cmd.Parameters.AddWithValue("?DATAPAGINI", objNf.DATAINICIAL.ToString("yyyy-MM-dd"));
                        cmd.Parameters.AddWithValue("?DATAPAGFIM", objNf.DATAFINAL.ToString("yyyy-MM-dd"));
                    }
                    else if (objNf.FLGST == "R")
                    {
                        cmd.Parameters.AddWithValue("?FLGST", objNf.FLGST);
                        cmd.Parameters.AddWithValue("?DATAPAGINI", objNf.DATAINICIAL.ToString("yyyy-MM-dd"));
                        cmd.Parameters.AddWithValue("?DATAPAGFIM", objNf.DATAFINAL.ToString("yyyy-MM-dd"));
                        cmd.Parameters.AddWithValue("?DATARESINI", objNf.DATAINICIAL.ToString("yyyy-MM-dd"));
                        cmd.Parameters.AddWithValue("?DATARESFIM", objNf.DATAFINAL.ToString("yyyy-MM-dd"));
                    }

                    if (objNf.MICRO_GERACAO == 1 && objNf.MICRO_GERACAO_DISTRIBUIDA == 1)
                    {
                        cmd.Parameters.AddWithValue("?MICRO_GERACAO", objNf.MICRO_GERACAO);
                        cmd.Parameters.AddWithValue("?MICRO_GERACAO_DISTRIBUIDA", objNf.MICRO_GERACAO_DISTRIBUIDA);


                        if (objNf.MICRO_GERACAO_CONVENIO_SEFAZ == 1)
                        {
                            cmd.Parameters.AddWithValue("?MICRO_GERACAO_CONVENIO_SEFAZ", objNf.MICRO_GERACAO_CONVENIO_SEFAZ);
                        }
                        else if (objNf.MICRO_GERACAO_CONVENIO_SEFAZ == 0)
                        {
                            cmd.Parameters.AddWithValue("?MICRO_GERACAO_CONVENIO_SEFAZ", objNf.MICRO_GERACAO_CONVENIO_SEFAZ);
                        }


                    }

                    dt = objGerenciadorTransacao.GetDataTable(cmd);

                }

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable SelectOthersTables_10(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {


                    vFILTER = vFILTER + " NF.NUMNF, ";
                    vFILTER = vFILTER + " NF.CODCLAS, ";
                    vFILTER = vFILTER + " NF.MESANO, ";
                    vFILTER = vFILTER + " NF.MESANO_REF, ";
                    vFILTER = vFILTER + " NF.DATAVENC, ";
                    vFILTER = vFILTER + " NF.DATADIVIDA, ";
                    vFILTER = vFILTER + " NF.DATALEIT, ";
                    vFILTER = vFILTER + " NF.DATAAPR, ";
                    vFILTER = vFILTER + " NF.DATAEM, ";
                    vFILTER = vFILTER + " NF.DATALEITANT, ";
                    vFILTER = vFILTER + " NF.DATAPAG, ";
                    vFILTER = vFILTER + " NF.CODHASH, ";
                    vFILTER = vFILTER + " NF.DATAST, ";
                    vFILTER = vFILTER + " NF.FLGST, ";
                    vFILTER = vFILTER + " NF.VALORTOT, ";
                    vFILTER = vFILTER + " NF.ZONA, ";
                    vFILTER = vFILTER + " NF.LIVRO, ";
                    vFILTER = vFILTER + " NF.CONTA, ";
                    vFILTER = vFILTER + " NF.ROTA, ";
                    vFILTER = vFILTER + " NF.CONS_ID, ";
                    vFILTER = vFILTER + " NF.UNID_ID, ";
                    vFILTER = vFILTER + " NF.DATAPROXLEIT, ";
                    vFILTER = vFILTER + " NF.BANARREC, ";
                    vFILTER = vFILTER + " NF.BANIDENT, ";
                    vFILTER = vFILTER + " NF.FLGMEDFAT, ";
                    vFILTER = vFILTER + " NF.TIPOUC, ";
                    vFILTER = vFILTER + " NF.CFOP, ";
                    vFILTER = vFILTER + " NF.ITENSSERV, ";
                    vFILTER = vFILTER + " NF.TA, ";
                    vFILTER = vFILTER + " NF.GRUPO, ";
                    vFILTER = vFILTER + " NF.FASE, ";
                    vFILTER = vFILTER + " NF.TENSAO, ";
                    vFILTER = vFILTER + " NF.NUM_MED, ";
                    vFILTER = vFILTER + " NF.CONST_MED, ";
                    vFILTER = vFILTER + " NF.MOTIVOCANC, ";
                    vFILTER = vFILTER + " NF.BANCO_FATUR, ";
                    vFILTER = vFILTER + " NF.SOCIAL, ";
                    vFILTER = vFILTER + " NF.MULTA, ";
                    vFILTER = vFILTER + " NF.DATAPRAZO, ";
                    vFILTER = vFILTER + " NF.DIAS, ";
                    vFILTER = vFILTER + " NF.VALORLIQ, ";
                    vFILTER = vFILTER + " NF.NSU, ";
                    vFILTER = vFILTER + " NF.CODBARRA, ";
                    vFILTER = vFILTER + " NF.DVCODBARRA, ";
                    vFILTER = vFILTER + " NF.AGRUPA, ";
                    vFILTER = vFILTER + " NF.AGRUPA_ROTA, ";
                    vFILTER = vFILTER + " NF.ITENSIMP, ";
                    vFILTER = vFILTER + " NF.SGDENVIO, ";
                    vFILTER = vFILTER + " NF.MOTIVOPGTO, ";
                    vFILTER = vFILTER + " NF.MOTIVOSEGVIA, ";
                    vFILTER = vFILTER + " NF.USUARIOSEGVIA, ";
                    vFILTER = vFILTER + " NF.DATASEGVIA, ";
                    vFILTER = vFILTER + " NF.CPF_CNPJ, ";
                    vFILTER = vFILTER + " NF.PARCIALMENTE_CATIVO, ";
                    vFILTER = vFILTER + " NF.LIVREESPECIAL, ";
                    vFILTER = vFILTER + " NF.PERIODO_TESTE, ";
                    vFILTER = vFILTER + " NF.MENSAGEM_BANDEIRA, ";
                    vFILTER = vFILTER + " NF.MICRO_GERACAO, ";
                    vFILTER = vFILTER + " NF.IRRIGACAO_AQUICULTURA, ";
                    vFILTER = vFILTER + " NF.AGRUPA_CADERNO, ";
                    vFILTER = vFILTER + " NF.AGRUPA_SEQUENCIA, ";
                    vFILTER = vFILTER + " NF.CADERNO_SEQUENCIA, ";
                    vFILTER = vFILTER + " NF.ROTA_QUATRO, ";
                    vFILTER = vFILTER + " NF.AGENCIA, ";
                    vFILTER = vFILTER + " NF.CONTA_CORRENTE, ";
                    vFILTER = vFILTER + " NF.LIVRE_VERDE, ";
                    vFILTER = vFILTER + " NF.CANCELADA_MESANO, ";
                    vFILTER = vFILTER + " NF.CANCELADA_NUMNF, ";
                    vFILTER = vFILTER + " NF.ENVIO_BCO, ";
                    vFILTER = vFILTER + " NF.FATURA_FINAL, ";
                    vFILTER = vFILTER + " NF.PAG_GOV, ";
                    vFILTER = vFILTER + " NF.MICRO_GERACAO_DISTRIBUIDA, ";
                    vFILTER = vFILTER + " NF.USINA, ";
                    vFILTER = vFILTER + " NF.SUPRIMENTO, ";
                    vFILTER = vFILTER + " NF.BAIXA_NFV, ";
                    vFILTER = vFILTER + " NF.SITE_ENVIO, ";
                    vFILTER = vFILTER + " NF.TARIFA_BRANCA, ";
                    vFILTER = vFILTER + " NF.MICRO_GERACAO_CONVENIO_SEFAZ, ";
                    vFILTER = vFILTER + " NF.REGIAO, ";
                    vFILTER = vFILTER + " NF.FATURA_COMPLEMENTAR,  ";
                    vFILTER = vFILTER + "       0  AS CADERNO,  ";//RESERVA_NUMNF
                    vFILTER = vFILTER + "       '' AS RESERVA_MESANO , "; //RESERVA_MESANO
                    vFILTER = vFILTER + "       '0000-00-00' AS RESERVA_DATARES  "; //RESERVA_DATARES

                    vINNERJOIN = " ";

                    vORDERBY = " ";

                    vAND = vAND + " AND (FLGST <> ?FLGST OR ";
                    vAND = vAND + "        ( (FLGST = ?FLGST AND DATE_FORMAT(NF.DATAST, '%d/%m/%Y' )   >  DATE_FORMAT(?DATASTFIM, '%d/%m/%Y' )) ";
                    vAND = vAND + "        OR (FLGST = ?FLGST AND MESANO_REF <> REPLACE(LEFT(DATAST, 7),'-','') AND DATE_FORMAT(NF.DATAST, '%d/%m/%Y' ) BETWEEN DATE_FORMAT(?DATASTINI, '%d/%m/%Y' ) AND DATE_FORMAT(?DATASTFIM, '%d/%m/%Y' ))";
                    vAND = vAND + "         )";
                    vAND = vAND + "        )";
                    vAND = vAND + "   AND MESANO_REF <= ?MESANO_REF ";
                    vAND = vAND + "   AND ( DATE_FORMAT(NF.DATAPAG, '%d/%m/%Y' ) > DATE_FORMAT(?DATAPAGFIM, '%d/%m/%Y' ) OR DATE_FORMAT(NF.DATAPAG, '%d/%m/%Y' ) = DATE_FORMAT('0000-00-00', '%d/%m/%Y' )) ";
                    vAND = vAND + "   AND NOT EXISTS(SELECT * ";
                    vAND = vAND + "                    FROM RESERVA ";
                    vAND = vAND + "                   WHERE 1 = 1  ";
                    vAND = vAND + "                     AND RESERVA.NUMNF    = NF.NUMNF  ";
                    vAND = vAND + "                     AND RESERVA.MESANO   = NF.MESANO ";
                    vAND = vAND + "                     AND RESERVA.ZONA     = NF.ZONA   ";
                    vAND = vAND + "                     AND RESERVA.LIVRO    = NF.LIVRO  ";
                    vAND = vAND + "                     AND RESERVA.CONTA    = NF.CONTA  ";
                    vAND = vAND + "                     AND RESERVA.DATARES <=  DATE_FORMAT(?DATARESFIM, '%d/%m/%Y' ))";

                    if (objNf.MICRO_GERACAO == 1 && objNf.MICRO_GERACAO_DISTRIBUIDA == 1)
                    {
                        vAND = vAND + "   AND (NF.MICRO_GERACAO = ?MICRO_GERACAO OR NF.MICRO_GERACAO_DISTRIBUIDA = ?MICRO_GERACAO_DISTRIBUIDA ) ";

                        if (objNf.MICRO_GERACAO_CONVENIO_SEFAZ == 1)
                        { vAND = vAND + "  AND NF.MICRO_GERACAO_CONVENIO_SEFAZ  = ?MICRO_GERACAO_CONVENIO_SEFAZ "; }
                        else if (objNf.MICRO_GERACAO_CONVENIO_SEFAZ == 0)
                        { vAND = vAND + "  AND NF.MICRO_GERACAO_CONVENIO_SEFAZ  = ?MICRO_GERACAO_CONVENIO_SEFAZ "; }


                    }

                    if (objNf.GRUPO == "A" || objNf.GRUPO == "B")
                    {
                        vAND = vAND + " AND NF.GRUPO = ?GRUPO ";
                    }

                    //vAND = vAND +" AND CODCLAS= 5100";
                    //vAND = vAND +" AND NF.NUMNF = 3 ";
                    //vAND = vAND +" AND NF.TA = '01AL00' ";
                    //vAND = vAND +" AND NF.GRUPO = 'B' ";
                    //vAND = vAND +" AND NF.TA <> '01AL00' AND NF.MICRO_GERACAO = 0  AND NF.MICRO_GERACAO_DISTRIBUIDA = 0";
                    //vAND = vAND +" AND NF.GRUPO = 'B' AND NF.MICRO_GERACAO = 0  AND NF.MICRO_GERACAO_DISTRIBUIDA = 0";

                    //vAND = vAND +" AND ZONA = 001";
                    //vAND = vAND +" AND LIVRO = 09";
                    //vAND = vAND +" AND CONTA = 004133";
                    //vAND = vAND +" AND (NF.MICRO_GERACAO = 0 AND NF.MICRO_GERACAO_DISTRIBUIDA = 0) ";
                    //vAND = vAND +" AND MID(CODCLAS, 1, 1) BETWEEN 0 AND 3 AND CODCLAS <> 100";
                    //vAND = vAND +" AND CODCLAS BETWEEN '4000' AND '4999'";

                    cmd = new MySqlCommand(this.QuerySelect);
                    cmd.CommandTimeout = 3600;


                    cmd.Parameters.AddWithValue("?FLGST", objNf.FLGST);
                    cmd.Parameters.AddWithValue("?DATASTINI", objNf.DATAINICIAL);
                    cmd.Parameters.AddWithValue("?DATASTFIM", objNf.DATAFINAL);
                    cmd.Parameters.AddWithValue("?MESANO_REF", objNf.MESANO_REF);
                    cmd.Parameters.AddWithValue("?DATAPAGFIM", objNf.DATAFINAL);
                    cmd.Parameters.AddWithValue("?DATARESFIM", objNf.DATAFINAL);

                    if (objNf.MICRO_GERACAO == 1 && objNf.MICRO_GERACAO_DISTRIBUIDA == 1)
                    {
                        cmd.Parameters.AddWithValue("?MICRO_GERACAO", objNf.MICRO_GERACAO);
                        cmd.Parameters.AddWithValue("?MICRO_GERACAO_DISTRIBUIDA", objNf.MICRO_GERACAO_DISTRIBUIDA);


                        if (objNf.MICRO_GERACAO_CONVENIO_SEFAZ == 1)
                        {
                            cmd.Parameters.AddWithValue("?MICRO_GERACAO_CONVENIO_SEFAZ", objNf.MICRO_GERACAO_CONVENIO_SEFAZ);
                        }
                        else if (objNf.MICRO_GERACAO_CONVENIO_SEFAZ == 0)
                        {
                            cmd.Parameters.AddWithValue("?MICRO_GERACAO_CONVENIO_SEFAZ", objNf.MICRO_GERACAO_CONVENIO_SEFAZ);
                        }

                    }

                    cmd.Parameters.AddWithValue("?GRUPO", objNf.GRUPO);

                    dt = objGerenciadorTransacao.GetDataTable(cmd);

                }

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable SelectOthersTables_11(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {


                    vFILTER = vFILTER + "        NF.ZONA, ";
                    vFILTER = vFILTER + "        NF.LIVRO, ";
                    vFILTER = vFILTER + "        NF.CONTA, ";
                    vFILTER = vFILTER + "        NF.TA, ";
                    vFILTER = vFILTER + "        NF.FASE, ";
                    vFILTER = vFILTER + "        NF.CFOP, ";
                    vFILTER = vFILTER + "        NF.CODCLAS, ";
                    vFILTER = vFILTER + "        NF.FLGST, ";
                    vFILTER = vFILTER + "        NF.DATAST, ";
                    vFILTER = vFILTER + "        NF.ITENSSERV, ";
                    vFILTER = vFILTER + "        CONSUM.MATRICULA ";

                    vINNERJOIN = vINNERJOIN + " LEFT JOIN CONSUM ON(CONSUM.CONSREG = NF.CONS_ID) ";

                    vORDERBY = " ORDER BY NF.TA ASC, NF.CODCLAS ASC, NF.FASE ASC  ";

                    vAND = vAND + " AND (NF.MESANO_REF = ?MESANO_REF  OR ";
                    vAND = vAND + "      (NF.FLGST = ?FLGST AND NF.MESANO_REF <> REPLACE(LEFT(NF.DATAST, 7),'-','') AND DATE_FORMAT(NF.DATAST, '%d/%m/%Y' ) BETWEEN ?DATASTINI AND ?DATASTFIM ))";


                    if (objNf.MICRO_GERACAO_CONVENIO_SEFAZ == 1)
                    { vAND = vAND + "  AND NF.FATURA_COMPLEMENTAR > 0 "; }
                    else if (objNf.MICRO_GERACAO_CONVENIO_SEFAZ == 0)
                    { vAND = vAND + "  AND NF.FATURA_COMPLEMENTAR  = 0 "; }


                    //if (objNf.ZONA != string.Empty)
                    //{
                    //    vAND = vAND + " AND NF.ZONA =?ZONA ";
                    //}

                    //vAND = vAND & "  AND NF.NUMNF = 17 ";
                    //vAND = vAND & "  AND NF.TA LIKE '%AL%' ";
                    //vAND = vAND & "  AND NF.GRUPO = 'A' AND NF.TA NOT LIKE '%AL%' AND NF.MICRO_GERACAO = 0 ";
                    //vAND = vAND & "  AND NF.GRUPO = 'B' AND NF.MICRO_GERACAO = 0 AND NF.MICRO_GERACAO_DISTRIBUIDA = 0 AND NF.USINA = 0 ";
                    //vAND = vAND & "  AND NF.GRUPO = 'A' AND NF.MICRO_GERACAO = 1 AND NF.MICRO_GERACAO_CONVENIO_SEFAZ = 1 ";
                    //vAND = vAND & "  AND NF.GRUPO = 'B' AND (NF.MICRO_GERACAO = 1 OR NF.MICRO_GERACAO_DISTRIBUIDA = 1) AND NF.MICRO_GERACAO_CONVENIO_SEFAZ = 0 ";
                    //vAND = vAND & "  AND NF.CODCLAS = '9105'";


                    cmd = new MySqlCommand(this.QuerySelect);
                    cmd.CommandTimeout = 3600;


                    cmd.Parameters.AddWithValue("?FLGST", objNf.FLGST);
                    cmd.Parameters.AddWithValue("?DATASTINI", objNf.DATAINICIAL.ToString("dd/MM/yyyy"));
                    cmd.Parameters.AddWithValue("?DATASTFIM", objNf.DATAFINAL.ToString("dd/MM/yyyy"));
                    cmd.Parameters.AddWithValue("?MESANO_REF", objNf.MESANO_REF);
                    //cmd.Parameters.AddWithValue("?ZONA", objNf.ZONA);


                    dt = objGerenciadorTransacao.GetDataTable(cmd);

                }

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable SelectOthersTables_12(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;

            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {
                    vFILTER = vFILTER + "		SELECT ";
                    vFILTER = vFILTER + "       FATURAS.ROTA                       AS FATURAS_ROTA, ";
                    vFILTER = vFILTER + "       FATURAS.MESANO                     AS FATURAS_MESANO, ";
                    vFILTER = vFILTER + "       CONSUM.NOME                        AS CONSUMIDOR_NOME, ";
                    vFILTER = vFILTER + "       CONSUM.EMAIL                       AS CONSUMIDOR_EMAIL ";
                    vFILTER = vFILTER + "   FROM NF AS FATURAS ";
                    vFILTER = vFILTER + "   LEFT JOIN UC_FATURAMENTO  ON(UC_FATURAMENTO.ID =  FATURAS.UNID_ID) ";
                    vFILTER = vFILTER + "   LEFT JOIN CONSUM ON(CONSUM.CONSREG =  FATURAS.CONS_ID) ";
                    vFILTER = vFILTER + "   LEFT JOIN CLASSE ON(CLASSE.CODIGO =  FATURAS.CODCLAS) ";
                    vFILTER = vFILTER + "   WHERE 1 = 1  ";

                    vFILTER = vFILTER + "   AND FATURAS.NUMNF  = ?NUMNF ";
                    vFILTER = vFILTER + "   AND FATURAS.MESANO = ?MESANO ";

                    vORDERBY = " ";

                    vAND = vFILTER;

                    cmd = new MySqlCommand(this.QueryExecute);
                    cmd.Parameters.AddWithValue("?NUMNF", objNf.NUMNF);
                    cmd.Parameters.AddWithValue("?MESANO", objNf.MESANO);
                    cmd.CommandTimeout = 3600;

                    dt = objGerenciadorTransacao.GetDataTable(cmd);

                }

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Execute(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;


            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vORDERBY = " ";

                    vAND = vAND + " CALL CRIAITENSFATURA(" + objNf.NUMNF + ",'" + objNf.MESANO_REF.ToString().ToUpper() + "'," + objNf.ANOMES_FAT + ")";


                    cmd = new MySqlCommand(this.QueryExecute);

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public DataTable Execute_1(entidadeNf objNf)
        {
            DataTable dt = null;
            MySqlCommand cmd = null;
            IGerenciadorTransacao objGerenciadorTransacao = null;
            ConexaoBD objconexaoBD = null;


            try
            {
                objconexaoBD = new ConexaoBD();

                objconexaoBD.SetarConexaoBanco(TipoBDConexao.fat_BD);

                using (objGerenciadorTransacao = FabricaGerenciadorTransacao.GetGerenciador(TipoBDGerenciador.MySql))
                {

                    vORDERBY = " ";

                    vAND = vAND + " CALL CRIAITENSFATURA3(" + objNf.NUMNF + ",'" + objNf.MESANO_REF.ToString().ToUpper() + "'," + objNf.ANOMES_FAT + ")";


                    cmd = new MySqlCommand(this.QueryExecute);

                    dt = objGerenciadorTransacao.GetDataTable(cmd);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        #endregion

        #region " Comandos SQL "

        StringBuilder sb = new StringBuilder();

        protected string QueryInsert
        {
            get
            {
                this.sb.Clear();

                this.sb.AppendLine("INSERT INTO  ");
                this.sb.AppendLine("NF SET  ");
                this.sb.AppendLine(vFILTER);

                return sb.ToString();
            }
        }

        protected string QueryUpdate
        {
            get
            {
                this.sb.Clear();

                this.sb.AppendLine("UPDATE  ");
                this.sb.AppendLine("NF SET  ");
                this.sb.AppendLine(vFILTER);
                this.sb.AppendLine("WHERE 1 = 1 ");
                this.sb.AppendLine(vAND);

                return sb.ToString();
            }
        }

        protected string QueryDelete
        {
            get
            {
                this.sb.Clear();

                this.sb.AppendLine("DELETE FROM NF ");
                this.sb.AppendLine("WHERE 1 = 1 ");
                this.sb.AppendLine(vAND);
                this.sb.AppendLine(vORDERBY);

                return sb.ToString();
            }
        }

        protected string QuerySelect
        {
            get
            {
                this.sb.Clear();

                this.sb.AppendLine("SELECT ");
                this.sb.AppendLine(vFILTER);
                this.sb.AppendLine("FROM ");
                this.sb.AppendLine("NF  ");
                this.sb.AppendLine(vINNERJOIN);
                this.sb.AppendLine("WHERE 1 = 1  ");
                this.sb.AppendLine(vAND);
                this.sb.AppendLine(vORDERBY);

                return sb.ToString();
            }
        }

        protected string QueryExecute
        {
            get
            {
                this.sb.Clear();

                this.sb.AppendLine(vAND);
                this.sb.AppendLine(vORDERBY);

                return sb.ToString();
            }
        }

        #endregion


    }
}
