Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE

Public Class frmExportaContabilidade

    Dim DataAtiva As Date
    Private objAtualizaBancoDados As New entidadeAtualizaBancoDados
    Private repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados

    Private objExecAtualizaBancoDados As New entidadeAtualizaBancoDados
    Private repositorioExecAtualizaBancoDados As New repositorioAtualizaBancoDados
    Private dt, dt_1, dt_2, dt_3, dtinterno As DataTable
    Dim iblnTar_AL As Boolean
    Dim iblnTar_AV As Boolean
    Dim iblnTar_AA As Boolean
    Dim iblnTar_A4 As Boolean
    Dim iblnTar_AS As Boolean
    Dim iblnTar_AX As Boolean
    Dim iblnTar_B As Boolean
    Dim istrSocios As String
    Dim istrTabTemp As String
    Dim istrTabTempItens As String
    Dim istrTabNf As String
    Dim iblnTotalizado As Boolean
    Dim iblnAnual As Boolean
    Dim llngNconsFimAno As Integer
    Dim istrGrupoG As String
    Dim lstrTotGeral As String

    Private Sub cmdFechar_Click(sender As Object, e As EventArgs) Handles cmdFechar.Click
        Me.Close()
    End Sub

    Private Sub cmdListar_Click(sender As Object, e As EventArgs) Handles cmdListar.Click

        Dim lstrsql As String

        Try

            Me.ltNaoExportado.Items.Clear()
            Me.listTotaisPorConta.Items.Clear()

            Me.txtTotalCredito.Text = "0,00"
            Me.txtTotalDebito.Text = "0,00"

            cmdProcessar.Enabled = False

            lstrsql = ""
            lstrsql = "delete from temp_contabilidade"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = "SELECT * FROM NF left JOIN CONSUM on(nf.cons_id = consum.consreg)  WHERE anomes_fat = '" & Format(dt1.Value, "yyyyMM") & "' AND (MID(ta,3,2) = 'AS' OR MID(ta,3,2)  = 'A4' OR MID(ta,3,1)  = 'B' OR MID(ta,3,2) = 'AA' OR MID(ta,3,2) = 'AV' OR MID(ta,3,2) = 'AL' OR MID(ta,3,2) = 'AP' OR MID(ta,3,2) = 'AX') " 'AND codclas > '9100'" 'AND data_exportacao is null ORDER BY numnf"
            Call MontaTabela(lstrsql, "A")

            'lstrsql = ""
            'lstrsql = "SELECT * FROM NF left JOIN CONSUM on(nf.cons_id = consum.consreg) WHERE anomes_fat = '" & Format(dt1.Value, "yyyyMM") & "' AND (MID(ta,3,2) = 'AS' OR MID(ta,3,2)  = 'A4' OR MID(ta,3,1)  = 'B' OR MID(ta,3,2) = 'AA' OR MID(ta,3,2) = 'AV' OR MID(ta,3,2) = 'AL' OR MID(ta,3,2) = 'AP' OR MID(ta,3,2) = 'AX') AND codclas > '9100'" 'AND data_exportacao is null ORDER BY numnf"
            'Call MontaTabela(lstrsql, "A")

            'lstrsql = ""
            'lstrsql = "SELECT * FROM NF left JOIN CONSUM on(nf.cons_id = consum.consreg)  WHERE anomes_fat = '" & Format(dt1.Value, "yyyyMM") & "' AND (MID(ta,3,2) = 'AS' OR MID(ta,3,2)  = 'A4' OR MID(ta,3,1)  = 'B' OR MID(ta,3,2) = 'AA' OR MID(ta,3,2) = 'AV' OR MID(ta,3,2) = 'AL' OR MID(ta,3,2) = 'AP' OR MID(ta,3,2) = 'AX') AND codclas between 0000 and 3999 " 'AND data_exportacao is null ORDER BY numnf"
            'Call MontaTabela(lstrsql, "A")

            Call sFiltraTabTemporaria_Classes("B", "", "", "")

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdListar_Click" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub InsereListNaoExportado(ByVal id_docto As Integer, ByVal tipo As String, ByVal data As Date, ByVal descr_natureza As String, ByVal valor As Double, ByVal conta As String, ByVal debcred As String, ByVal id As Double)
        Try
            '// Se o valor for negativo, inverte aqui
            If valor < 0 Then
                valor = -valor
            End If

            Me.ltNaoExportado.Items.Add(Strings.Format(id_docto, "000000"))
            Me.ltNaoExportado.Items.Item(Me.ltNaoExportado.Items.Count - 1).SubItems.Add(tipo)
            Me.ltNaoExportado.Items.Item(Me.ltNaoExportado.Items.Count - 1).SubItems.Add(Strings.Format(data, "MM/yyyy"))
            Me.ltNaoExportado.Items.Item(Me.ltNaoExportado.Items.Count - 1).SubItems.Add(descr_natureza)
            Me.ltNaoExportado.Items.Item(Me.ltNaoExportado.Items.Count - 1).SubItems.Add(Strings.Format(valor, "Standard"))
            Me.ltNaoExportado.Items.Item(Me.ltNaoExportado.Items.Count - 1).SubItems.Add(conta)
            Me.ltNaoExportado.Items.Item(Me.ltNaoExportado.Items.Count - 1).SubItems.Add(IIf(debcred = "D", "Débito", "Crédito"))
            Me.ltNaoExportado.Items.Item(Me.ltNaoExportado.Items.Count - 1).SubItems.Add("")
            Me.ltNaoExportado.Items.Item(Me.ltNaoExportado.Items.Count - 1).BackColor = Interaction.IIf(Me.ltNaoExportado.Items.Count Mod 2 = 0, Color.WhiteSmoke, Color.White)

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: InsereListNaoExportado" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

    Private Sub AddLancto(ByVal pConta As String, ByVal pValor As Double, ByVal pTipo As String)
        Dim achou As Boolean = False
        Dim item As Integer = 0

        Try

            '[Verifica se a conta já está na lista]
            For Each pItem As ListViewItem In Me.listTotaisPorConta.Items
                If pItem.SubItems(0).Text = pConta And pItem.SubItems(3).Text = pTipo Then
                    achou = True
                    item = pItem.Index
                End If
            Next

            '[Se o valor for negativo, inverte o sinal]
            If pValor < 0 Then
                pValor = -pValor
            End If

            '[Se a conta estiver na lista, soma o valor do lançamento; senão, adiciona uma nova linha]
            If achou Then
                Me.listTotaisPorConta.Items.Item(item).SubItems(2).Text = Format(CDbl(Me.listTotaisPorConta.Items.Item(item).SubItems(2).Text) + pValor, "Standard")
            Else
                Me.listTotaisPorConta.Items.Add(pConta)
                Me.listTotaisPorConta.Items.Item(Me.listTotaisPorConta.Items.Count - 1).SubItems.Add("") '// Insere um item em branco; mais tarde este campo receberá o nome da conta
                Me.listTotaisPorConta.Items.Item(Me.listTotaisPorConta.Items.Count - 1).SubItems.Add(Format(pValor, "Standard"))
                Me.listTotaisPorConta.Items.Item(Me.listTotaisPorConta.Items.Count - 1).SubItems.Add(Strings.UCase(pTipo))

                '[Adiciona o efeito zebrado à listView]
                If Me.listTotaisPorConta.Items.Count Mod 2 = 0 Then
                    Me.listTotaisPorConta.Items.Item(Me.listTotaisPorConta.Items.Count - 1).BackColor = Color.WhiteSmoke
                End If
            End If

            If Me.txtTotalDebito.Text = "" Then Me.txtTotalDebito.Text = "0"
            If Me.txtTotalCredito.Text = "" Then Me.txtTotalCredito.Text = "0"

            Select Case pTipo
                Case "C"
                    Me.txtTotalCredito.Text = Format(CDbl(Me.txtTotalCredito.Text) + pValor, "Standard")
                Case "D"
                    Me.txtTotalDebito.Text = Format(CDbl(Me.txtTotalDebito.Text) + pValor, "Standard")
            End Select

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AddLancto" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub
    Private Sub cmdProcessar_Click(sender As Object, e As EventArgs) Handles cmdProcessar.Click
        Call sProcessaRegistros()
    End Sub

    Private Function fRetornaConta(ByVal sCodigo As String, ByVal sClasse As String, ByVal sTipo As String) As String
        Dim lstrsql As String
        Dim sRet As String

        Try
            lstrsql = ""
            lstrsql = "SELECT conta_credito as cred, conta_debito as deb FROM codigo_contas_classe WHERE num = '" & sCodigo & "' and Classe = '" & sClasse & "'"
            objAtualizaBancoDados.QUERY_NAME = lstrsql
            dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            If dt.Rows.Count > 0 Then
                For Each row As DataRow In dt.Rows
                    If sTipo = "D" Then
                        sRet = Trim(IIf(IsDBNull(row.Item("deb")), "", row.Item("deb")))
                    Else
                        sRet = Trim(IIf(IsDBNull(row.Item("cred")), "", row.Item("cred")))
                    End If
                Next
            End If

            fRetornaConta = sRet

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: fRetornaConta" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function
        End Try

    End Function
    Private Sub sProcessaRegistros()
        Dim bd_contab As String = GetConfig("integracao.bd.contab")

        Try

            If bd_contab = "" Then
                MsgBox("O banco de dados do sistema contábil não está configurado!" & vbCrLf & vbCrLf & "Insira o nome do banco de dados MySQL nas configurações com a propriedade integracao.bd.contab.", MsgBoxStyle.Exclamation)
                Exit Sub
            End If

            Dim id_docto As String = "0"
            Dim id_docto_anterior As String = "0"
            Dim id_header As Integer = 0
            Dim id_lote As Integer = 0
            Dim id_string_lote As String = ""
            Dim descricao_lote As String = ""
            Dim sUltimoDiaMes As String = UltimoDiaMes(Format(dt1.Value, "MM"), Format(dt1.Value, "yyyy"))
            Dim data_lcto As String = Format(dt1.Value, "yyyy-MM") & "-" & sUltimoDiaMes
            Dim cod_ident As Integer = 2
            Dim cod_lcto_ident As String
            Dim dtlancto As Date = Convert.ToDateTime(data_lcto)

            Dim anomes() As Integer = Nothing
            Dim i As Integer

            ReDim anomes(0)
            anomes(0) = Strings.Format(Me.dt1.Value, "yyyyMM")

            If Not Me.VerificaLote(anomes, True) Then
                Exit Sub
            End If

            Dim lstrsql As String
            Dim data1, data2

            data1 = Format(dt1.Value, "yyyy-MM") & "-01"
            data2 = Format(dt1.Value, "yyyy-MM") & "-31"

            lstrsql = ""
            lstrsql = "Delete from `" & bd_contab & "`.`diario` where `data` between '" & data1 & "' and '" & data2 & "' and id_origem =2 "
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            Dim f As New frmAguarde : f.Show(Me)
            Dim sql As String = ""
            Dim nr_lancto As Double = 0
            Dim itotalreg As Integer = ltNaoExportado.Items.Count - 1

            Me.Enabled = False

            Dim intconta As Integer
            For intconta = 0 To ltNaoExportado.Items.Count - 1

                Dim dValor As Double
                Dim sConta As String
                Dim sTipo As String

                id_header = 0
                id_lote = 0
                id_string_lote = ""
                descricao_lote = ""

                dValor = ltNaoExportado.Items.Item(intconta).SubItems(4).Text
                sConta = ltNaoExportado.Items.Item(intconta).SubItems(5).Text
                sTipo = ltNaoExportado.Items.Item(intconta).SubItems(6).Text

                If sTipo = "Débito" Then
                    sTipo = "D"
                End If

                If sTipo = "Crédito" Then
                    sTipo = "C"
                End If

                Me.SetCabecalho(id_header, id_lote, id_string_lote, descricao_lote, dtlancto, cod_ident, cod_lcto_ident, sTipo)

                ''// Lança o detalhe - Débito
                If sConta <> "" Then
                    Me.SetDetalhe(id_header, sConta, "", dValor, sTipo, intconta)
                End If

                '' chegou na última linha
                'If intconta = itotalreg Then Exit For

                '' 2º  Lançamento de Crédito ou débito
                'dValor = ltNaoExportado.Items.Item(intconta + 1).SubItems(4).Text
                'sConta = ltNaoExportado.Items.Item(intconta + 1).SubItems(5).Text
                'sTipo = ltNaoExportado.Items.Item(intconta + 1).SubItems(6).Text

                'If sTipo = "Débito" Then
                '    sTipo = "D"
                'End If

                'If sTipo = "Crédito" Then
                '    sTipo = "C"
                'End If

                '''// Lança o detalhe - Débito
                'If sConta <> "" Then
                '    Me.SetDetalhe(id_header, sConta, "", dValor, sTipo, intconta + 1)
                'End If


            Next intconta

            MessageBox.Show("Processo de Exportação Concluído", "Aviso de Finalização", MessageBoxButtons.OK)

            Me.cmdProcessar.Enabled = False
            Me.Enabled = True


            f.Close()

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: sProcessaRegistros" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub SetCabecalho(ByRef pIdHeader As Long, ByVal pIdLote As Integer, pIdStringLote As String, pDescricaoLote As String, ByVal pDataLcto As Date, ByVal pCodIdent As Long, ByVal pCodLctoIdent As String, ByVal sTipo As String)
        Dim sql As String = ""
        Dim sStringLote As String

        sStringLote = "FATF" & pDataLcto

        Try

            sql = "INSERT INTO `" & GetConfig("integracao.bd.contab") & "`.`diario` SET "
            sql &= "`data` = '" & Strings.Format(pDataLcto, "yyyy-MM-dd") & "', "
            sql &= "`tipo` = 'R', "
            sql &= "`partida` = '" & sTipo & "', "
            sql &= "`anomes` = " & Strings.Format(pDataLcto, "yyyyMM") & ", "
            sql &= "`id_lote` = 0, "
            sql &= "`id_string_lote` = '" & sStringLote & "', "
            sql &= "`descricao_lote` = '', "
            sql &= "`conciliado` = 0, "
            sql &= "`obs` = '', "
            sql &= "`id_origem` = " & pCodIdent & ", "
            sql &= "`descricao_origem` = 'FATF', "
            sql &= "`string_origem` = '0', "
            sql &= "`id_usuario` = " & vUsuario.ID & ", "
            sql &= "`nome_usuario` = '" & vUsuario.NOME & "', "
            sql &= "`data_hora_usuario` = '" & Format(Now, "yyyy-MM-dd HH:mm:ss") & "' "

            objExecAtualizaBancoDados.QUERY_NAME = sql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            'pIdHeader = ModuloGlobal.getLastInsertID("`" & GetConfig("integracao.bd.contab") & "`.`diario`")

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: SetCabecalho" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub SetDetalhe(ByVal pIdHeader As Long, ByVal pNrConta As String, ByVal pDescHp As String, ByVal pValor As Double, ByVal pDebCred As String, ByVal iLinha As Integer)
        Dim sql As String = ""
        Dim bValida As Boolean
        Dim sSitucao As String
        ' Dim rst As New ADODB.Recordset

        Try
            sql = "SELECT `id`, `descricao` "
            sql &= "FROM `ctb`.`plano_contas` "
            sql &= "WHERE `conta_impressao` = '" & pNrConta & "' "
            sql &= "AND `id_empresa` = " & gintEmpresa_Codigo & " "
            objAtualizaBancoDados.QUERY_NAME = sql
            dtinterno = repositorioExecAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            bValida = False

            If dtinterno.Rows.Count > 0 Then

                '// Se o valor for negativo, inverte aqui
                If pValor < 0 Then
                    pValor = -pValor
                End If

                For Each rowinterno As DataRow In dtinterno.Rows

                    sql = "INSERT INTO `" & GetConfig("integracao.bd.contab") & "`.`diario_det` SET "
                    sql &= "`id_diario` = " & pIdHeader & ", "
                    sql &= "`id_conta` = " & rowinterno.Item("id") & ", "
                    sql &= "`descricao_conta` = '" & rowinterno.Item("descricao") & "', "
                    sql &= "`conta_formatada` = '0', "
                    sql &= "`valor` = " & Replace(Arredondar(pValor, 2), ",", ".") & ", "
                    sql &= "`id_historico_padrao` = 0, "
                    sql &= "`descricao_historico_padrao` = 'EXPORTAÇÃO DO FATF', "
                    sql &= "`deb_cred` = '" & pDebCred & "' "
                    objExecAtualizaBancoDados.QUERY_NAME = sql
                    repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

                    bValida = True
                Next
            End If

            dtinterno = Nothing

            sConcluiSituacao(iLinha, IIf(bValida, "Exportado", "Conta Não Encontrada"))

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: SetDetalhe" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            sConcluiSituacao(iLinha, IIf(bValida, "Exportado", "Erro ao Exportar"))
            Exit Sub
        End Try

    End Sub

    Private Function VerificaLote(ByVal pAnoMes() As Integer, pLancto As Boolean) As Boolean
        Dim sql As String = ""
        'Dim consulta As New ADODB.Recordset
        Dim mensagem As String = ""
        Dim i As Integer

        Try

            For i = 0 To UBound(pAnoMes)
                If VerificaPeriodo(pAnoMes(i)) Then
                    sql = "SELECT * "
                    sql &= "FROM `" & GetConfig("integracao.bd.contab") & "`.`lote` "
                    sql &= "WHERE `anomes` = " & pAnoMes(i) & " "

                    If GetConfig("modo.Sistema") = "COMPRAS" Then
                        sql &= "AND `id_string` = 'COMPRAS' "
                    Else
                        sql &= "AND `id_string` = 'FATF' "
                    End If
                    ''sql &= "AND `id_string` = 'ALMOX' "
                    sql &= "AND `tipo` = 'P' "
                    objAtualizaBancoDados.QUERY_NAME = sql
                    dtinterno = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

                    If dtinterno.Rows.Count > 0 Then
                        For Each row As DataRow In dtinterno.Rows
                            If row.Item("status") = "F" Then
                                mensagem &= " - " & pAnoMes(i) & vbCrLf
                            End If
                        Next
                    Else
                        sql = "INSERT INTO `" & GetConfig("integracao.bd.contab") & "`.`lote` SET "
                        sql &= "id_string = 'FATF', "
                        sql &= "descricao = 'INTEGRAÇÃO FATURAMENTO " & Strings.Right(pAnoMes(0), 2) & "/" & Strings.Left(pAnoMes(0), 4) & "', "
                        sql &= "anomes = " & pAnoMes(i) & ", "
                        sql &= "status = 'A', "
                        sql &= "tipo = 'P', "
                        sql &= "ativo = 1, "
                        sql &= "id_usuario = " & vUsuario.ID & ", "
                        sql &= "data_hora_usuario = '" & Strings.Format(Now, "yyyy-MM-dd HH:mm:ss") & "' "
                        objExecAtualizaBancoDados.QUERY_NAME = sql
                        repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
                        Return True
                    End If

                    dtinterno = Nothing

                Else
                    mensagem &= " - " & pAnoMes(i) & vbCrLf
                End If
            Next

            If mensagem <> "" Then
                MsgBox("Um ou mais períodos que você está tentando " & Interaction.IIf(pLancto, "exportar", "estornar") & " já foram finalizados!" & vbCrLf & vbCrLf & "Períodos finalizados:" & vbCrLf & mensagem, MsgBoxStyle.Critical)
                Return False
            End If

            Return True

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: VerificaLote" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function
        End Try
    End Function

    Private Sub frmExportaContabilidade_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        istrTabTemp = "temp_estatis" '& VB6.Format(Date.Now, "yyyymmddHHMMSS")
        istrTabTempItens = istrTabTemp & "_itens"
        '  sCriaTabelaTemp()
    End Sub

    Private Function VerificaPeriodo(anoMes As Integer) As Boolean
        Dim sql As String = ""
       

        Try

            sql = "SELECT `valor` "
            sql &= "FROM `" & GetConfig("integracao.bd.contab") & "`.`configuracoes` "
            sql &= "WHERE `nome` = 'ano_mes' "
            objAtualizaBancoDados.QUERY_NAME = sql
            dtinterno = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            If dtinterno.Rows.Count > 0 Then
                For Each rowinterno As DataRow In dtinterno.Rows
                    If anoMes < rowinterno.Item("valor") Then
                        MsgBox("O período de importação é inferior ao em aberto." & vbCrLf & vbCrLf & "Verique o período em aberto na contabilidade. Atual: " & rowinterno.Item("valor"), MsgBoxStyle.Exclamation, "Atenção")
                        Return False
                    Else
                        Return True
                    End If
                Next
            Else
                Return False
            End If

            dtinterno = Nothing

            Return True

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: VerificaPeriodo" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function
        End Try

    End Function

    Private Sub MarcaDoctoComoExportado(ByVal id_docto As Double, ByVal tipo As String)
        Dim sql As String = ""

        Try
            sql = "update "
            If tipo = "N" Then
                sql &= "NF"
            ElseIf tipo = "E" Then
                sql &= "entrada"
            ElseIf tipo = "TS" Then
                sql &= "transf_sucata"
            ElseIf tipo = "TA" Then
                sql &= "transf_almox"
            Else
                Exit Sub
            End If
            sql &= " set data_exportacao='" & Format(Date.Now, "yyyy-MM-dd") & "'"
            sql &= " where id=" & id_docto
            objExecAtualizaBancoDados.QUERY_NAME = sql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: MarcaDoctoComoExportado" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub
    Private Sub sCriaTabelaTemp()
        Dim lstrsql As String

        Try

            objExecAtualizaBancoDados.QUERY_NAME = "drop table if exists temp_contabilidade"
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "CREATE TABLE temp_contabilidade (" & vbCrLf
            lstrsql = lstrsql & "id Int(11) Not NULL AUTO_INCREMENT, " & vbCrLf
            lstrsql = lstrsql & "numnf VARCHAR(10) Default NULL," & vbCrLf
            lstrsql = lstrsql & "codigo VARCHAR(20) Default NULL," & vbCrLf
            lstrsql = lstrsql & "valor Decimal(10, 2) Default 0," & vbCrLf
            lstrsql = lstrsql & "contadeb VARCHAR(30) Default NULL," & vbCrLf
            lstrsql = lstrsql & "contacred VARCHAR(30) Default NULL," & vbCrLf
            lstrsql = lstrsql & "classe VARCHAR(20) Default NULL, " & vbCrLf
            lstrsql = lstrsql & "PRIMARY KEY(id)" & vbCrLf
            lstrsql = lstrsql & ")" & vbCrLf
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: sCriaTabelaTemp" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub
    Private Function MontaTabela(ByVal sSQL As String, ByVal sTipo As String) As Boolean

        Dim lstrFilPref As String
        Dim lblnControl As Boolean 'Controle de socios, não socios ou todos
        Dim lblnRetorno As Boolean
        Dim ldteIni As Date
        Dim ldteFim As Date
        Dim lstrMesAno As String
        Dim lintContInsertPri As Short
        Dim lintContInsertItens As Short
        '
        Dim lstrItens As String
        Dim lstrClasse As String
        Dim lstrZona As String
        Dim lstrLivro As String
        Dim lstrConta As String
        Dim lstrNome As String
        Dim llngUnidId As Integer
        Dim llngConsId As Integer
        Dim lstrGrupo As String
        Dim lstrGrupoT As String
        Dim lintLivreEspecial As Short
        Dim lintParcialmenteCativo As Short
        Dim lintLivreVerde As Boolean
        Dim lintIrrigacaoAquicultura As Short
        Dim lintMicroGeracaoConvenioSefaz As Short
        Dim lintTarifaBranca As Short
        Dim lstrFlgSt As String
        Dim ldteFlgSt As Date
        Dim lintMpr As Short 'Usado para caso tiver alguma fatura cancelada no mês de faturamento de outro mês
        Dim lstrFase As String
        Dim lblnInclui As Boolean
        Dim ldblValorTot As Double
        Dim llngMatricula As Integer
        Dim lstrFlgMedFat As String
        Dim lintFaturaFinal As Short
        Dim lintMicroGeracao As Short
        Dim lintMicroGeracaoDistribuida As Short
        '
        Dim ldblTarPSeca As Double
        Dim ldblTarFPSeca As Double
        Dim ldblTarPUmida As Double
        Dim ldblTarFPUmida As Double
        '
        'Convencional
        'Quantidade
        Dim ldblQKwhCons As Double 'Qtde Consumo lido
        Dim ldblQKwhFat As Double 'Qtde Consumo faturado
        Dim ldblQKwhFatBan As Double 'Qtde Consumo faturado Bandeira
        Dim ldblQKwhFatTU As Double 'Qtde consumo faturado TUSD
        Dim ldblQkWhFatTE As Double 'Qtde consumo faturado TE
        Dim ldblQkWhFatTEBan As Double 'Qtde consumo faturado TE Bandeira
        'Dim ldblQKwhReverso As Double  'Qtde consumo reverso
        Dim ldblQKwContr As Double 'Qtde Demanda Contratada
        Dim ldblQKwFat As Double 'Qtde Demanda
        '
        Dim ldblVKwhCons As Double 'Valor Consumo lido
        Dim ldblVKwhFat As Double 'Valor Consumo faturado
        Dim ldblVKwhFatBan As Double 'Valor Consumo faturado Bandeira
        Dim ldblVkWhFatTU As Double 'Valor consumo faturado TUSD
        Dim ldblVkWhFatTE As Double 'Valor consumo faturado TE
        Dim ldblVkWhFatTEBan As Double 'Valor consumo faturado TE Bandeira
        'Dim ldblVKwhReverso As Double  'Valor consumo reverso
        Dim ldblVKwFat As Double 'Valor Demanda
        '
        'Horosazonal
        Dim ldblQKwhFatp As Double 'Qtde Consumo Faturado p
        Dim ldblQKwhFati As Double 'Qtde Consumo Faturado i
        Dim ldblQKwhFatfp As Double 'Qtde Consumo Faturado fp

        Dim ldblQKwhFatpAUX As Double 'Qtde Consumo Faturado p
        Dim ldblQKwhFatfpAUX As Double 'Qtde Consumo Faturado fp

        Dim ldblQKwhFatpBan As Double 'Qtde Consumo Faturado p Bandeira
        Dim ldblQKwhFatiBan As Double 'Qtde Consumo Faturado i Bandeira
        Dim ldblQKwhFatfpBan As Double 'Qtde Consumo Faturado fp Bandeira
        Dim ldblQKwhFatpLiv As Double 'Qtde Consumo Faturado p Livre
        Dim ldblQKwhFatfpLiv As Double 'Qtde Consumo Faturado fp Livre
        '
        Dim ldblQKwhDevp As Double 'Qtde Consumo Devolvido p
        Dim ldblQKwhDevfp As Double 'Qtde Consumo Devolvido fp
        Dim ldblQKwhUtrp As Double 'Qtde Consumo Ultrapassado p
        Dim ldblQKwhUtrfp As Double 'Qtde Consumo Ultrapassado fp
        '
        Dim ldblQKwContrp As Double 'Qtde Demanda Contratada Ponta
        Dim ldblQKwContrfp As Double 'Qtde Demanda Contratada Fora Ponta
        Dim ldblQKwFatp As Double 'Qtde Demanda p
        Dim ldblQKwFatfp As Double 'Qtde Demanda fp
        Dim ldblQKwDifFatp As Double 'Qtde Dif. Demanda p
        Dim ldblQKwDifFatfp As Double 'Qtde Dif. Demanda fp
        Dim ldblQKwFatUp As Double 'Qtde Demanda Ultrapassagem p
        Dim ldblQKwFatUfp As Double 'Qtde Demanda Ultrapassagem fp
        '
        Dim ldblVKwhFatp As Double 'Valor Consumo faturado p
        Dim ldblVKwhFati As Double 'Valor Consumo faturado i
        Dim ldblVKwhFatfp As Double 'Valor Consumo faturado fp
        Dim ldblVKwhFatpBan As Double 'Valor Consumo faturado p Badeira
        Dim ldblVKwhFatiBan As Double 'Valor Consumo faturado i Badeira
        Dim ldblVKwhFatfpBan As Double 'Valor Consumo faturado fp Bandeira
        Dim ldblVKwhFatpLiv As Double 'Valor Consumo faturado p Livre
        Dim ldblVKwhFatfpLiv As Double 'Valor Consumo faturado fp Livre
        '
        Dim ldblVKwhDevp As Double 'Valor Consumo devolvido p
        Dim ldblVKwhDevfp As Double 'Valor Consumo devolvido fp
        Dim ldblVKwhUtrp As Double 'Valor Consumo Ultrapassado p
        Dim ldblVKwhUtrfp As Double 'Valor Consumo Ultrapassado fp
        '
        Dim ldblVKwFatp As Double 'Valor Demanda p
        Dim ldblVKwFatfp As Double 'Valor Demanda fp
        Dim ldblVKwDifFatp As Double 'Valor Dif. Demanda p
        Dim ldblVKwDifFatfp As Double 'Valor Dif. Demanda fp
        Dim ldblVKwFatUp As Double 'Valor Demanda Ultrapassagem p
        Dim ldblVKwFatUfp As Double 'Valor Demanda Ultrapassagem fp
        'Micro Geração
        Dim ldblQKwhReversop As Double 'Qtde consumo reverso p
        Dim ldblQKwhReversoi As Double 'Qtde consumo reverso i
        Dim ldblQKwhReversofp As Double 'Qtde consumo reverso fp
        Dim ldblVKwhReversop As Double 'Valor consumo reverso p
        Dim ldblVKwhReversoi As Double 'Valor consumo reverso i
        Dim ldblVKwhReversofp As Double 'Valor consumo reverso fp

        Dim ldblQKwhReversop_sefaz As Double 'Qtde consumo reverso p
        Dim ldblQKwhReversoi_sefaz As Double 'Qtde consumo reverso i
        Dim ldblQKwhReversofp_sefaz As Double 'Qtde consumo reverso fp
        Dim ldblVKwhReversop_sefaz As Double 'Valor consumo reverso p
        Dim ldblVKwhReversoi_sefaz As Double 'Valor consumo reverso i
        Dim ldblVKwhReversofp_sefaz As Double 'Valor consumo reverso fp

        '
        'pela demanda
        Dim ldblVkwDscProv As Double 'Desconto provisorio
        Dim ldblVkwDscReal As Double 'Desconto real
        Dim ldblVkwDevCcee As Double 'Diferença a devolver
        Dim ldblVkwCobCcee As Double 'Diferença a cobrar
        '
        'pelo consumo
        Dim ldblVkwhDscProv As Double 'Desconto provisorio
        Dim ldblVkwhDscReal As Double 'Desconto real
        Dim ldblVkwhDevCcee As Double 'Diferença a devolver
        Dim ldblVkwhCobCcee As Double 'Diferença a cobrar
        '
        Dim ldblTip As Double 'Valor de iluminação publica.
        Dim ldblIcms1 As Double 'Valor de icms.
        Dim ldblIcms2 As Double 'Valor de icms.
        Dim ldblPis As Double 'Valor de PIS.
        Dim ldblCofins As Double 'Valor de COFINS.
        '
        Dim ldblIcmsSt As Double 'Icms Sistema Transmissão
        Dim ldblPisSt As Double 'Pis Sistema Transmissão
        Dim ldblCofinsSt As Double 'Cofns Sistema Transmissão
        '
        Dim ldblBaseIcms1 As Double 'valor da base ICMS.
        Dim ldblBaseIcms2 As Double 'valor da base ICMS.
        Dim ldblBasePis As Double 'Valor Base PIS.
        Dim ldblBaseCofins As Double 'Valor Base COFINS.
        '
        Dim ldblIcmsPisCof As Double 'valor de icms/pis/cofins.
        Dim ldblSaldoArre As Double
        Dim ldblEncFinan As Double
        '
        Dim dblAliquota As Double 'valor aliquota.
        Dim ldblIsentoIcms As Double 'valor Isento ICMS.
        Dim ldblDiferido As Double 'valor Diferido.
        Dim dblConReativReserv As Double 'valor consumo reativo reservado
        Dim ldblIcmsSobretaxa As Double 'icms sobre taxa
        '
        Dim ldblDifEltrbr As Double '
        Dim ldblJuros As Double 'juros verificar ainda com o calculo.
        Dim ldblEncargo As Double 'encargos confirma ainda com o calculo
        Dim ldblMust As Double
        '
        Dim ldblConsumoReativo As Double 'Consumo Reativo (Convencional)
        Dim ldblDemandaReativa As Double 'Demanda Reativo (Convencional)
        Dim ldblConsumoReativop As Double 'Consumo Reativo na ponta (Horosazonal)
        Dim ldblConsumoReativofp As Double 'Consumo Reativo fora da ponta (Horosazonal)
        Dim ldblDemandaReativap As Double 'Demanda Reativo na ponta (Horosazonal)
        Dim ldblDemandaReativafp As Double 'Demanda Reativo fora da ponta (Horosazonal)
        '
        Dim ldblDiversos As Double
        Dim ldblTaxados As Double
        Dim ldblConvenios As Double
        Dim ldblDevolucao As Double
        Dim ldblDevolucao_1 As Double
        Dim ldblRetencao As Double
        Dim ldblAux As Double
        Dim ldbVistoria As Double
        Dim ldbVistoria_bif As Double
        Dim ldbVistoria_trif As Double
        Dim ldbVISTORIA_GRUPOA As Double
        Dim ldbSERVTAXADO As Double
        Dim ldbSERVTAXADO_BIF As Double
        Dim ldbSERVTAXADO_TRIF As Double
        Dim ldbVERIFNIVEL As Double
        Dim ldbVERIFNIVEL_BIF As Double
        Dim ldbVERIFNIVEL_TRIF As Double
        Dim ldbSERVTAXADO_GA As Double
        Dim ldbSERVTAXADO_EM2 As Double
        Dim ldbSERVTAXADO_RN As Double
        Dim ldbSERVTAXADO_RN_BIF As Double
        Dim ldbSERVTAXADO_RN_TRIF As Double
        Dim ldbSERVTAXADO_RN_GA As Double
        Dim ldbSERVTAXADO_EM2_BIF As Double
        Dim ldbSERVTAXADO_EM2_TRIF As Double
        Dim ldbSERVTAXADO_EM2_A4 As Double
        Dim ldbDESLIGPROGRAMADO As Double
        Dim ldbDESLIGPROGRAMADO_TRIF As Double
        Dim ldbDESLIGPROGRAMADO_GRUPOA As Double
        Dim ldbRELIGPROGRAMADA As Double
        Dim ldbRELIGPROGRAMADA_TRIF As Double
        Dim ldbRELIGPROGRAMADA_GRUPOA As Double
        Dim ldbFORNECIMENTOPULSO_GRUPOA As Double
        Dim ldbCUSTOADM As Double
        Dim ldbCUSTOADM_GRUPOA As Double
        Dim ldbAcrescimoMultaAtrasos As Double
        Dim ldbJurosAtraso As Double
        Dim ldbCorrecaoMonetaria As Double
        Dim ldbServicosPretados As Double
        Dim ldbDevolucaoaMaior As Double
        Dim ldbDevolucaoConsumo As Double
        Dim ldbDevolucaoCobrancaaMaior As Double
        Dim ldbDevolucaoCobranca2via As Double
        Dim ldbDevolucaoAfericaoMedidor As Double
        Dim ldbDescontoIrriganteAquicultor As Double
        Dim ldbDevolucaoCorrecao As Double
        Dim ldbRessarcimentoDMIC As Double
        Dim ldbRessarcimentoDICFIC As Double
        Dim ldbRessarcimentoBonusItaipu As Double
        Dim ldbRessarcimentoNivelTensao As Double
        Dim ldbRessarcimentoViolacaoPrazo As Double
        Dim ldbBonusItaipu911 As Double
        Dim ldbBonusItaipuDevolver911 As Double
        Dim ldbMicroResidencial As Double
        Dim ldbMicroIndustrial As Double
        Dim ldbMicroComercial As Double
        Dim ldbMicroRural As Double
        Dim ldbMicroPoderPublico As Double
        Dim ldbReceitaSistemaDistribuicao As Double
        Dim ldbReceitaSistemaDistribuicaoTaxa As Double
        Dim ldbtudsmust As Double
        Dim ldbreceitabandeira As Double
        Dim ldbDiferenca As Double
        Dim ldbICMSResidencial As Double
        Dim ldbICMSIndustrial As Double
        Dim ldbICMSComercial As Double
        Dim ldbICMSRural As Double
        Dim ldbICMSPoderPublica As Double
        Dim ldbICMSIluminacaoPublica As Double
        Dim ldbICMSServicoPublico As Double
        Dim ldbICMSConsumidorLivre As Double
        Dim ldbICMSConsumidorPotencialmenteLivre As Double
        Dim ldbICMSImpCirMercServ As Double
        Dim ldbICMSMesPosteriorIlum As Double
        Dim ldbICMSMesPosteriorGrupoA As Double
        Dim ldbtotparcCMSImpCirMercServA As Double
        Dim ldbtotparcCMSImpCirMercServB As Double
        Dim ldbicmsConsumoLivre As Double
        Dim ldbicmsConsumoPotenciaLivre As Double
        Dim ldbicmsImpCircMercServST As Double
        Dim ldbicmsImpCircMercServPosterior As Double

        '
        Dim lstrSql As String
        Dim lstrSqlPri As String
        Dim lstrSqlPAx As String
        Dim lstrSqlIte As String
        Dim lstrSqlIAx As String
        Dim lnumnf As Long
        Dim anomesfat As String


        Dim lintSequencia As Short

        Dim vlist

        Dim objNf As New entidadeNf
        Dim repositorioNf As New repositorioNf

        Dim objNfTransicao As New entidadeNfTransicao
        Dim repositorioNfTransicao As New repositorioNfTransicao

        Dim objTempEstatisItens As New entidadeTempEstatisItens
        Dim repositorioTempEstatisItens As New repositorioTempEstatisItens

        Dim objTempEstatis As New entidadeTempEstatis
        Dim repositorioTempEstatis As New repositorioTempEstatis

        Try

            iblnTar_A4 = False
            iblnTar_AA = False
            iblnTar_AL = False
            iblnTar_AS = False
            iblnTar_AV = False
            iblnTar_AX = False
            iblnTar_B = False

            lstrFilPref = GetFiltroPrefixo()

            ' ldteFim = CDate(UltimoDiaMes(CShort(VB6.GetItemString(Me.cmbMesFim, Me.cmbMesFim.SelectedIndex)), CShort(Me.txtAno.Text)) & "/" & CShort(VB6.GetItemString(Me.cmbMesFim, Me.cmbMesFim.SelectedIndex)) & "/" & CShort(Me.txtAno.Text))
            ldteIni = CDate("01/" & VB6.Format(ldteFim, "MM/yyyy"))

            istrTabNf = "NF"
            If istrTabNf = "NF" Then

                objExecAtualizaBancoDados.QUERY_NAME = sSQL
                dt = repositorioExecAtualizaBancoDados.Consultar(objExecAtualizaBancoDados)

                If dt.Rows.Count > 0 Then

                    BarraProgresso(Me.ProgressBar, Me.lblPerc, Me.lblInfo, 0, dt.Rows.Count)

                    If sTipo = "A" Then

                        If CriaTabelas(lstrSqlPri, lstrSqlIte, True, True, True) Then
                            lstrSqlIAx = ""
                            lstrSqlPAx = ""
                            lintContInsertPri = 1
                            lintContInsertItens = 1

                            For Each row As DataRow In dt.Rows 'entidadeNf In vlist

                                'Busca a matricula do socios caso seja nula ou em branco sempre sera ZERO
                                lblnControl = True


                                lstrFlgSt = Trim(IIf(IsDBNull(row.Item("FLGST")), 0, row.Item("FLGST")))
                                ldteFlgSt = CDate(IIf(IsDBNull(row.Item("DATAST")), "1900-01-01", row.Item("DATAST")))
                                lstrMesAno = Trim(IIf(IsDBNull(row.Item("MESANO")), "01/1900", row.Item("MESANO")))
                                llngMatricula = Trim(IIf(IsDBNull(row.Item("MATRICULA")), 0, row.Item("MATRICULA")))

                                lnumnf = Trim(IIf(IsDBNull(row.Item("numnf")), 0, row.Item("numnf")))
                                anomesfat = Trim(IIf(IsDBNull(row.Item("anomes_fat")), 0, row.Item("anomes_fat")))

                                If lstrFlgSt = "C" Then
                                    If ldteFlgSt >= CDate("01/" & lstrMesAno) And ldteFlgSt <= CDate(UltimoDiaMes(Microsoft.VisualBasic.Left(lstrMesAno, 2), Microsoft.VisualBasic.Right(lstrMesAno, 4)) & "/" & lstrMesAno) Then
                                        lblnControl = False
                                    Else
                                        lblnControl = True
                                    End If

                                End If

                                'If istrSocios = "1" Then
                                '    'If Me.cmbSocios.SelectedIndex = 1 Then 'Somente Socios
                                '    '    lblnControl = (llngMatricula > 0)
                                '    'ElseIf Me.cmbSocios.SelectedIndex = 2 Then  'Somente não socios
                                '    '    lblnControl = (llngMatricula = 0)
                                '    'End If
                                'End If

                                If lblnControl = True Then

                                    lstrItens = Trim(IIf(IsDBNull(row.Item("ITENSSERV")), "", row.Item("ITENSSERV")))
                                    lstrClasse = Trim(IIf(IsDBNull(row.Item("CODCLAS")), "0000", row.Item("CODCLAS")))
                                    lstrZona = Trim(IIf(IsDBNull(row.Item("ZONA")), "000", row.Item("ZONA")))
                                    lstrLivro = Trim(IIf(IsDBNull(row.Item("LIVRO")), "00", row.Item("LIVRO")))
                                    lstrConta = Trim(IIf(IsDBNull(row.Item("CONTA")), "000000", row.Item("CONTA")))
                                    llngUnidId = Trim(IIf(IsDBNull(row.Item("UNID_ID")), 0, row.Item("UNID_ID")))

                                    llngConsId = Trim(IIf(IsDBNull(row.Item("CONS_ID")), 0, row.Item("CONS_ID")))
                                    lstrGrupo = Mid(IIf(IsDBNull(row.Item("ta")), "01B1", row.Item("ta")), 3, 2)
                                    lstrNome = BuscaNome(llngConsId, llngUnidId, Microsoft.VisualBasic.Left(lstrGrupo, 1))
                                    lintLivreEspecial = Convert.ToInt32(IIf(IsDBNull(row.Item("LIVREESPECIAL")), 0, row.Item("LIVREESPECIAL")))
                                    lintLivreVerde = Convert.ToInt32(IIf(IsDBNull(row.Item("LIVRE_VERDE")), 0, row.Item("LIVRE_VERDE")))
                                    lintParcialmenteCativo = Convert.ToInt32(IIf(IsDBNull(row.Item("PARCIALMENTE_CATIVO")), 0, row.Item("PARCIALMENTE_CATIVO")))
                                    lstrFase = Trim(IIf(IsDBNull(row.Item("FASE")), "M", row.Item("FASE")))
                                    lstrFlgSt = Trim(IIf(IsDBNull(row.Item("FLGST")), "", row.Item("FLGST")))
                                    ldteFlgSt = CDate(IIf(IsDBNull(row.Item("DATAST")), "1900-01-01", row.Item("DATAST")))
                                    lstrFlgMedFat = Trim(IIf(IsDBNull(row.Item("FLGMEDFAT")), "", row.Item("FLGMEDFAT")))
                                    lintFaturaFinal = Convert.ToInt32(IIf(IsDBNull(row.Item("FATURA_FINAL")), 0, row.Item("FATURA_FINAL")))
                                    lintIrrigacaoAquicultura = Convert.ToInt32(IIf(IsDBNull(row.Item("IRRIGACAO_AQUICULTURA")), 0, row.Item("IRRIGACAO_AQUICULTURA")))
                                    lintMicroGeracaoConvenioSefaz = Convert.ToInt32(IIf(IsDBNull(row.Item("MICRO_GERACAO_CONVENIO_SEFAZ")), 0, row.Item("MICRO_GERACAO_CONVENIO_SEFAZ")))
                                    lintMicroGeracao = Convert.ToInt32(IIf(IsDBNull(row.Item("MICRO_GERACAO")), 0, row.Item("MICRO_GERACAO")))
                                    lintMicroGeracaoDistribuida = Convert.ToInt32(IIf(IsDBNull(row.Item("MICRO_GERACAO_DISTRIBUIDA")), 0, row.Item("MICRO_GERACAO_DISTRIBUIDA")))
                                    lintTarifaBranca = Convert.ToInt32(IIf(IsDBNull(row.Item("TARIFA_BRANCA")), 0, row.Item("TARIFA_BRANCA")))

                                    If lstrFlgSt = "C" Then
                                        If ldteFlgSt <= ldteFim Then
                                            lintMpr = -1
                                        Else
                                            lintMpr = 1
                                        End If
                                    Else
                                        lintMpr = 1
                                    End If

                                    ldblValorTot = Trim(IIf(IsDBNull(row.Item("VALORTOT")), "", row.Item("VALORTOT"))) * lintMpr
                                    'ldblValorTot = IIf(IsDBNull(objNfFor.VALORTOT), 0, objNfFor.VALORTOT) * lintMpr
                                    lstrGrupoT = Microsoft.VisualBasic.Left(lstrGrupo, IIf(Microsoft.VisualBasic.Left(lstrGrupo, 1) = "B", 1, 2))

                                    If lstrGrupoT = "A4" Then
                                        lstrGrupoT = "B"
                                    End If

                                    '
                                    If lstrGrupo = "A4" Then iblnTar_A4 = True
                                    If lstrGrupo = "AA" Then iblnTar_AA = True
                                    If lstrGrupo = "AL" Then iblnTar_AL = True
                                    If lstrGrupo = "AS" Then iblnTar_AS = True
                                    If lstrGrupo = "AV" Then iblnTar_AV = True
                                    If lstrGrupo = "AX" Then iblnTar_AX = True
                                    If iblnTar_B = False And Microsoft.VisualBasic.Left(lstrGrupo, 1) = "B" Then iblnTar_B = True
                                    '

                                    'Convencional
                                    'Quantidade
                                    ldblQKwhCons = 0 'Consumo lido
                                    ldblQKwhFat = 0 'Consumo faturado
                                    ldblQKwhFatBan = 0 'Consumo Faturado Bandeira
                                    ldblVKwhFatBan = 0 'Consumo faturado bandeira
                                    ldblQKwhFatTU = 0 'Consumo faturado TUSD
                                    ldblQkWhFatTE = 0 'Consumo faturado TE
                                    ldblQkWhFatTEBan = 0 'Consumo faturado TE bandeira
                                    'ldblQKwhReverso = 0  'Consumo reverso
                                    ldblQKwContr = 0 'Demanda Contratada
                                    ldblQKwFat = 0 'Demanda

                                    'Valor
                                    ldblVKwhCons = 0 'Consumo lido
                                    ldblVKwhFat = 0 'Consumo faturado
                                    ldblVKwhFatBan = 0 'Consumo faturado Bandeira
                                    ldblVkWhFatTU = 0 'Consumo faturado TUSD
                                    ldblVkWhFatTE = 0 'Consumo faturado TE
                                    ldblVkWhFatTEBan = 0 'Consumo faturado TE bandeira
                                    'ldblVKwhReverso = 0  'Consumo reverso
                                    ldblVKwFat = 0 'Demanda
                                    '

                                    'Horosazonal
                                    'Quantidade
                                    ldblQKwhFatp = 0 'Qtde Consumo faturado p
                                    ldblQKwhFati = 0 'Qtde Consumo faturado i
                                    ldblQKwhFatfp = 0 'Qtde Consumo faturado fp
                                    ldblQKwhFatpBan = 0 'Qtde Consumo faturado p Bandeira
                                    ldblQKwhFatiBan = 0 'Qtde Consumo faturado i Bandeira
                                    ldblQKwhFatfpBan = 0 'Qtde Consumo faturado fp Bandeira
                                    ldblQKwhFatpLiv = 0 'Qtde Consumo faturado p Livre
                                    ldblQKwhFatfpLiv = 0 'Qtde Consumo faturado fp Livre						
                                    '
                                    ldblQKwhDevp = 0 'Qtde Consumo Devolvido p
                                    ldblQKwhDevfp = 0 'Qtde Consumo Devolvido fp
                                    ldblQKwhUtrp = 0 'Qtde Consumo Ultrapassado p
                                    ldblQKwhUtrfp = 0 'Qtde Consumo Ultrapassado fp
                                    '							
                                    ldblQKwContrp = 0 'Qtde Demanda Contratada p
                                    ldblQKwContrfp = 0 'Qtde Demanda Contratada fp
                                    ldblQKwFatp = 0 'Qtde Demanda p
                                    ldblQKwFatfp = 0 'Qtde Demanda fp
                                    ldblQKwDifFatp = 0 'Qtde Dif. Demanda p
                                    ldblQKwDifFatfp = 0 'Qtde Dif. Demanda fp
                                    ldblQKwFatUp = 0 'Qtde Demanda de ultrapassagem p
                                    ldblQKwFatUfp = 0 'Qtde Demanda de ultrapassagem fp
                                    ldblQKwhReversop = 0 'Qtde consumo reverso p
                                    ldblQKwhReversoi = 0 'Qtde consumo reverso i
                                    ldblQKwhReversofp = 0 'Qtde consumo reverso fp
                                    ldblQKwhReversop_sefaz = 0 'Qtde consumo reverso p
                                    ldblQKwhReversoi_sefaz = 0 'Qtde consumo reverso i
                                    ldblQKwhReversofp_sefaz = 0 'Qtde consumo reverso fp

                                    'Valor
                                    ldblVKwhFatp = 0 'Valor Consumo faturado p
                                    ldblVKwhFati = 0 'Valor Consumo faturado i
                                    ldblVKwhFatfp = 0 'Valor Consumo faturado fp
                                    ldblVKwhFatpBan = 0 'Valor Consumo faturado p Bandeira
                                    ldblVKwhFatiBan = 0 'Valor Consumo faturado i Bandeira
                                    ldblVKwhFatfpBan = 0 'Valor Consumo faturado fp Bandeira
                                    ldblVKwhFatpLiv = 0 'Valor Consumo faturado p Livre
                                    ldblVKwhFatfpLiv = 0 'Valor Consumo faturado fp Livre
                                    '
                                    ldblVKwhDevp = 0 'Valor Consumo devolvido p
                                    ldblVKwhDevfp = 0 'Valor Consumo devolvido fp
                                    ldblVKwhUtrp = 0 'Valor Consumo Ultrapassado p
                                    ldblVKwhUtrfp = 0 'Valor Consumo Ultrapassado fp
                                    '
                                    ldblVKwFatp = 0 'Valor Demanda p
                                    ldblVKwFatfp = 0 'Valor Demanda fp
                                    ldblVKwDifFatp = 0 'Valor Dif. Demanda p
                                    ldblVKwDifFatfp = 0 'Valor Dif. Demanda fp
                                    ldblVKwFatUp = 0 'Valor Demanda de ultrapassagem p
                                    ldblVKwFatUfp = 0 'Valor Demanda de ultrapassagem fp
                                    '
                                    ldblVKwhReversop = 0 'valor consumo reverso p
                                    ldblVKwhReversoi = 0 'valor consumo reverso i
                                    ldblVKwhReversofp = 0 'valor consumo reverso fp

                                    ldblVKwhReversop_sefaz = 0 'valor consumo reverso p
                                    ldblVKwhReversoi_sefaz = 0 'valor consumo reverso i
                                    ldblVKwhReversofp_sefaz = 0 'valor consumo reverso fp

                                    'Desconto Provisorio
                                    ldblVkwDscProv = 0 'Desconto provisorio
                                    ldblVkwDscReal = 0 'Desconto real
                                    ldblVkwDevCcee = 0 'Diferença a devolver
                                    ldblVkwCobCcee = 0 'Diferença a cobrar			
                                    '
                                    ldblVkwhDscProv = 0 'Desconto provisorio
                                    ldblVkwhDscReal = 0 'Desconto real
                                    ldblVkwhDevCcee = 0 'Diferença a devolver
                                    ldblVkwhCobCcee = 0 'Diferença a cobrar
                                    '----
                                    ldblTip = 0 'Vlr Iluminação Pública.
                                    ldblIcms1 = 0 'Vlr Icms.
                                    ldblIcms2 = 0 'Vlr Icms.
                                    ldblPis = 0 'Vlr PIS.
                                    ldblCofins = 0 'Vlr COFINS.
                                    '
                                    ldblIcmsSt = 0 'Vlr Icms Substituição Tributária
                                    ldblPisSt = 0 'Vlr Pis Substituição Tributária
                                    ldblCofinsSt = 0 'Vlr Cofins Substituição Tributária
                                    '			
                                    ldblBaseIcms1 = 0 'Vlr Base Icms.
                                    ldblBaseIcms2 = 0 'Vlr Base Icms.
                                    ldblBasePis = 0 'Vlr Base PIS.
                                    ldblBaseCofins = 0 'Vlr Base COFINS.					
                                    '
                                    ldblIcmsPisCof = 0 'Vlr Icms/Pis/Cofins.
                                    ldblSaldoArre = 0
                                    ldblEncFinan = 0
                                    ldblDifEltrbr = 0
                                    ldblJuros = 0 'Juros verificar ainda com o calculo.
                                    ldblEncargo = 0 'Encargos confirma ainda com o calculo
                                    ldblMust = 0 'Valor de MUST
                                    '	
                                    ldblConsumoReativo = 0 'Consumo Reativo
                                    ldblDemandaReativa = 0 'Demanda Reativo
                                    ldblConsumoReativop = 0 'Consumo Reativo p
                                    ldblConsumoReativofp = 0 'Consumo Reativo fp
                                    ldblDemandaReativap = 0 'Demanda Reativo p
                                    ldblDemandaReativafp = 0 'Demanda Reativo fp

                                    ldbVistoria = 0 ' Vistoria 9040000011
                                    ldbVistoria_bif = 0 ' Vistoria 9040000012
                                    ldbVistoria_trif = 0 ' Vistoria 9040000013
                                    ldbVISTORIA_GRUPOA = 0 ' Vistoria 9040000014
                                    ldbSERVTAXADO = 0 ' Vistoria 9040000021
                                    ldbSERVTAXADO_BIF = 0 ' Vistoria 9040000022
                                    ldbSERVTAXADO_TRIF = 0 ' Vistoria 9040000023
                                    ldbVERIFNIVEL = 0 ' Vistoria 9040000031
                                    ldbVERIFNIVEL_BIF = 0 ' Vistoria 9040000032
                                    ldbVERIFNIVEL_TRIF = 0 ' Vistoria 9040000033
                                    ldbSERVTAXADO_RN = 0 ' Vistoria 9040000041
                                    ldbSERVTAXADO_RN_BIF = 0 ' Vistoria 9040000042
                                    ldbSERVTAXADO_RN_TRIF = 0 ' Vistoria 9040000043
                                    ldbSERVTAXADO_RN_GA = 0 ' Vistoria 9040000044
                                    ldbSERVTAXADO_EM2_A4 = 0 ' Vistoria 9040000061
                                    ldbDESLIGPROGRAMADO = 0 ' Vistoria 9040000062
                                    ldbDESLIGPROGRAMADO_TRIF = 0 ' Vistoria 9040000063
                                    ldbDESLIGPROGRAMADO_GRUPOA = 0 ' Vistoria 9040000064
                                    ldbRELIGPROGRAMADA = 0
                                    ldbRELIGPROGRAMADA_TRIF = 0
                                    ldbRELIGPROGRAMADA_GRUPOA = 0
                                    ldbFORNECIMENTOPULSO_GRUPOA = 0
                                    ldbCUSTOADM = 0
                                    ldbCUSTOADM_GRUPOA = 0
                                    ldbAcrescimoMultaAtrasos = 0
                                    ldbJurosAtraso = 0
                                    ldbCorrecaoMonetaria = 0
                                    ldbServicosPretados = 0
                                    ldbDevolucaoaMaior = 0
                                    ldbDevolucaoConsumo = 0
                                    ldbDevolucaoCobrancaaMaior = 0
                                    ldbDevolucaoCobranca2via = 0
                                    ldbDevolucaoAfericaoMedidor = 0
                                    ldbDescontoIrriganteAquicultor = 0
                                    ldbDevolucaoCorrecao = 0
                                    ldbRessarcimentoDMIC = 0
                                    ldbRessarcimentoDICFIC = 0
                                    ldbRessarcimentoBonusItaipu = 0
                                    ldbRessarcimentoNivelTensao = 0
                                    ldbRessarcimentoViolacaoPrazo = 0
                                    ldbBonusItaipu911 = 0
                                    ldbBonusItaipuDevolver911 = 0
                                    ldbMicroResidencial = 0
                                    ldbMicroIndustrial = 0
                                    ldbMicroComercial = 0
                                    ldbMicroRural = 0
                                    ldbMicroPoderPublico = 0

                                    ldbICMSResidencial = 0
                                    ldbICMSIndustrial = 0
                                    ldbICMSComercial = 0
                                    ldbICMSRural = 0
                                    ldbICMSPoderPublica = 0
                                    ldbICMSIluminacaoPublica = 0
                                    ldbICMSServicoPublico = 0
                                    ldbICMSConsumidorLivre = 0
                                    ldbICMSConsumidorPotencialmenteLivre = 0
                                    ldbICMSImpCirMercServ = 0
                                    ldbICMSMesPosteriorIlum = 0
                                    ldbICMSMesPosteriorGrupoA = 0

                                    ldbtotparcCMSImpCirMercServA = 0
                                    ldbtotparcCMSImpCirMercServB = 0

                                    ldbicmsConsumoLivre = 0
                                    ldbicmsConsumoPotenciaLivre = 0
                                    ldbicmsImpCircMercServST = 0
                                    ldbicmsImpCircMercServPosterior = 0


                                    '************
                                    ldblBaseIcms1 = (BuscaVlrItem(lstrItens, "0302030001") + BuscaVlrItem(lstrItens, "0302063001")) * lintMpr
                                    ldblBaseIcms1 = ldblBaseIcms1 + BuscaVlrItem(lstrItens, "0302063035") * lintMpr 'Marreta por problemas de calculo na fatura 21 de 12/2019
                                    ldblBaseIcms2 = BuscaVlrItem(lstrItens, "0302030003") * lintMpr
                                    ldblBasePis = BuscaVlrItem(lstrItens, "0302020033") * lintMpr
                                    ldblBaseCofins = BuscaVlrItem(lstrItens, "302020034") * lintMpr

                                    If lstrGrupo = "AS" Or lstrGrupo = "A4" Or Microsoft.VisualBasic.Left(lstrGrupo, 1) = "B" Then

                                        If Microsoft.VisualBasic.Left(lstrGrupo, 1) <> "B" Then

                                            'Quantidade
                                            ldblQKwhCons = BuscaVlrItem(lstrItens, "0302010008") * lintMpr 'Consumo lido
                                            ldblQKwhFat = BuscaVlrItem(lstrItens, "0302010009") * lintMpr 'Consumo faturado
                                            ldblQKwhFatBan = BuscaVlrItem(lstrItens, "0302110009") * lintMpr 'Consumo faturado bandeira

                                            'Valor
                                            ldblVKwhCons = BuscaVlrItem(lstrItens, "0302020008") * lintMpr 'Consumo lido
                                            ldblVKwhFat = BuscaVlrItem(lstrItens, "0302020009") * lintMpr 'Consumo faturado
                                            ldblVKwhFatBan = BuscaVlrItem(lstrItens, "0302120009") * lintMpr 'Consumo faturado bandeira

                                            ldbVistoria = BuscaVlrItem(lstrItens, "9040000011") * lintMpr
                                            ldbVistoria_bif = BuscaVlrItem(lstrItens, "9040000012") * lintMpr
                                            ldbVistoria_trif = BuscaVlrItem(lstrItens, "9040000013") * lintMpr
                                            ldbVISTORIA_GRUPOA = BuscaVlrItem(lstrItens, "9040000014") * lintMpr
                                            ldbSERVTAXADO = BuscaVlrItem(lstrItens, "9040000021") * lintMpr
                                            ldbSERVTAXADO_BIF = BuscaVlrItem(lstrItens, "9040000022") * lintMpr
                                            ldbSERVTAXADO_TRIF = BuscaVlrItem(lstrItens, "9040000023") * lintMpr
                                            ldbVERIFNIVEL = BuscaVlrItem(lstrItens, "9040000031") * lintMpr
                                            ldbVERIFNIVEL_BIF = BuscaVlrItem(lstrItens, "9040000032") * lintMpr
                                            ldbVERIFNIVEL_TRIF = BuscaVlrItem(lstrItens, "9040000033") * lintMpr
                                            ldbSERVTAXADO_RN = BuscaVlrItem(lstrItens, "9040000041") * lintMpr
                                            ldbSERVTAXADO_RN_BIF = BuscaVlrItem(lstrItens, "9040000042") * lintMpr
                                            ldbSERVTAXADO_RN_TRIF = BuscaVlrItem(lstrItens, "9040000043") * lintMpr
                                            ldbSERVTAXADO_RN_GA = BuscaVlrItem(lstrItens, "9040000044") * lintMpr
                                            ldbSERVTAXADO_EM2 = BuscaVlrItem(lstrItens, "9040000061") * lintMpr
                                            ldbSERVTAXADO_EM2_BIF = BuscaVlrItem(lstrItens, "9040000062") * lintMpr
                                            ldbSERVTAXADO_EM2_TRIF = BuscaVlrItem(lstrItens, "9040000063") * lintMpr
                                            ldbSERVTAXADO_EM2_A4 = BuscaVlrItem(lstrItens, "9040000064") * lintMpr
                                            ldbDESLIGPROGRAMADO = BuscaVlrItem(lstrItens, "9040000121") * lintMpr
                                            ldbDESLIGPROGRAMADO_TRIF = BuscaVlrItem(lstrItens, "9040000123") * lintMpr
                                            ldbDESLIGPROGRAMADO_GRUPOA = BuscaVlrItem(lstrItens, "9040000124") * lintMpr
                                            ldbRELIGPROGRAMADA = BuscaVlrItem(lstrItens, "9040000111") * lintMpr
                                            ldbRELIGPROGRAMADA_TRIF = BuscaVlrItem(lstrItens, "9040000113") * lintMpr
                                            ldbRELIGPROGRAMADA_GRUPOA = BuscaVlrItem(lstrItens, "9040000124") * lintMpr
                                            ldbFORNECIMENTOPULSO_GRUPOA = BuscaVlrItem(lstrItens, "9040000134") * lintMpr
                                            ldbCUSTOADM = BuscaVlrItem(lstrItens, "9040000161") * lintMpr
                                            ldbCUSTOADM_GRUPOA = BuscaVlrItem(lstrItens, "9040000163") * lintMpr
                                            ldbAcrescimoMultaAtrasos = BuscaVlrItem(lstrItens, "9080000061") * lintMpr
                                            ldbJurosAtraso = BuscaVlrItem(lstrItens, "9080000066") * lintMpr
                                            ldbCorrecaoMonetaria = BuscaVlrItem(lstrItens, "9080000067") * lintMpr
                                            ldbServicosPretados = BuscaVlrItem(lstrItens, "9080000208") * lintMpr
                                            ldbDevolucaoaMaior = BuscaVlrItem(lstrItens, "9070000082") * lintMpr
                                            ldbDevolucaoConsumo = BuscaVlrItem(lstrItens, "9070000084") * lintMpr
                                            ldbDevolucaoCobrancaaMaior = BuscaVlrItem(lstrItens, "9070000004") * lintMpr
                                            ldbDevolucaoCobranca2via = BuscaVlrItem(lstrItens, "9070000063") * lintMpr
                                            ldbDevolucaoAfericaoMedidor = BuscaVlrItem(lstrItens, "9070000098") * lintMpr
                                            ldbDescontoIrriganteAquicultor = (BuscaVlrItem(lstrItens, "9070000008") + BuscaVlrItem(lstrItens, "9070000009")) * lintMpr
                                            ldbDevolucaoCorrecao = BuscaVlrItem(lstrItens, "9070000030") * lintMpr
                                            ldbRessarcimentoDMIC = BuscaVlrItem(lstrItens, "9070000037") * lintMpr
                                            ldbRessarcimentoDICFIC = (BuscaVlrItem(lstrItens, "9070000033") + BuscaVlrItem(lstrItens, "9070000034")) * lintMpr
                                            ldbRessarcimentoBonusItaipu = 0 'BuscaVlrItem(lstrItens, "9080000208") * lintMpr
                                            ldbRessarcimentoNivelTensao = BuscaVlrItem(lstrItens, "9070000040") * lintMpr
                                            ldbRessarcimentoViolacaoPrazo = BuscaVlrItem(lstrItens, "9070000041") * lintMpr
                                            ldbBonusItaipu911 = BuscaVlrItem(lstrItens, "9070000079") * lintMpr
                                            ldbBonusItaipuDevolver911 = 0 'BuscaVlrItem(lstrItens, "9070000079") * lintMpr

                                            If lstrClasse > "9100" Then
                                                ldbMicroResidencial = (BuscaVlrItem(lstrItens, "302022010") + BuscaVlrItem(lstrItens, "0302022011")) * lintMpr
                                                ldbICMSResidencial = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                            End If
                                            If lstrClasse > "0000" And lstrClasse < "3999" Then
                                                ldbMicroIndustrial = (BuscaVlrItem(lstrItens, "302022010") + BuscaVlrItem(lstrItens, "0302022011")) * lintMpr
                                                ldbICMSIndustrial = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                            End If
                                            If lstrClasse > "4000" And lstrClasse < "4999" Then
                                                ldbMicroComercial = (BuscaVlrItem(lstrItens, "302022010") + BuscaVlrItem(lstrItens, "0302022011")) * lintMpr
                                                ldbICMSComercial = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                            End If
                                            If lstrClasse > "5000" And lstrClasse < "5999" Then
                                                ldbMicroRural = (BuscaVlrItem(lstrItens, "302022010") + BuscaVlrItem(lstrItens, "0302022011")) * lintMpr
                                                ldbICMSRural = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                            End If

                                            If lstrClasse > "6100" And lstrClasse < "6199" Then
                                                ldbMicroPoderPublico = (BuscaVlrItem(lstrItens, "302022010") + BuscaVlrItem(lstrItens, "0302022011")) * lintMpr
                                                ldbICMSPoderPublica = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                            End If

                                            If lstrClasse > "6400" And lstrClasse < "6499" Then
                                                ldbICMSIluminacaoPublica = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                                ldbICMSMesPosteriorIlum = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                                ldbtotparcCMSImpCirMercServB = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                            End If

                                            If lstrClasse > "7000" And lstrClasse < "7999" Then
                                                ldbICMSServicoPublico = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                            End If


                                            If lstrClasse > "8000" And lstrClasse < "8999" Then
                                                ldbtotparcCMSImpCirMercServA = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                            End If

                                            ldbReceitaSistemaDistribuicao = (BuscaVlrItem(lstrItens, "9070000087") + BuscaVlrItem(lstrItens, "9070000091")) * lintMpr
                                            ldbReceitaSistemaDistribuicaoTaxa = BuscaVlrItem(lstrItens, "9080000098") * lintMpr
                                            ldbtudsmust = (BuscaVlrItem(lstrItens, "9070000087") + BuscaVlrItem(lstrItens, "9070000091")) * lintMpr
                                            ldbreceitabandeira = (BuscaVlrItem(lstrItens, "030212019") + BuscaVlrItem(lstrItens, "030212029") + BuscaVlrItem(lstrItens, "030212039")) * lintMpr

                                            If lstrClasse = "0100" Then
                                                ldbicmsConsumoLivre = BuscaVlrItem(lstrItens, "602020001") * lintMpr
                                                ldbicmsConsumoPotenciaLivre = BuscaVlrItem(lstrItens, "602020001") * lintMpr
                                                ldbicmsImpCircMercServPosterior = BuscaVlrItem(lstrItens, "602020001") * lintMpr
                                                ldbICMSConsumidorPotencialmenteLivre = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                                ldbICMSMesPosteriorGrupoA = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                            End If

                                            ' ldbicmsConsumoPotenciaLivre = BuscaVlrItem(lstrItens, "602020001") * lintMpr
                                            ldbDiferenca = fRetornaValorItemFatura(lnumnf, anomesfat)
                                            ldbICMSConsumidorLivre = 0
                                            ldbICMSImpCirMercServ = ldbtotparcCMSImpCirMercServA - ldbtotparcCMSImpCirMercServB
                                            ldbicmsImpCircMercServST = ldbtotparcCMSImpCirMercServA - ldbtotparcCMSImpCirMercServB


                                        Else
                                                'Quantidade
                                                ldblQKwhCons = (BuscaVlrItem(lstrItens, "030201008") + BuscaVlrItem(lstrItens, "030201018") + BuscaVlrItem(lstrItens, "030201028") + BuscaVlrItem(lstrItens, "030201038")) * lintMpr 'Consumo lido

                                            If lintTarifaBranca = 0 Then
                                                ldblQKwhFat = (BuscaVlrItem(lstrItens, "030201009")) * lintMpr 'Consumo faturado
                                                ldblQKwhFatBan = (BuscaVlrItem(lstrItens, "030211009")) * lintMpr 'Consumo faturado bandeira
                                            Else
                                                ldblQKwhFatp = (BuscaVlrItem(lstrItens, "030201019")) * lintMpr 'Consumo faturado
                                                ldblQKwhFati = (BuscaVlrItem(lstrItens, "030201029")) * lintMpr 'Consumo faturado
                                                ldblQKwhFatfp = (BuscaVlrItem(lstrItens, "030201039")) * lintMpr 'Consumo faturado   
                                                ldblQKwhFatpBan = (BuscaVlrItem(lstrItens, "0302110190")) * lintMpr 'Consumo faturado bandeira
                                                ldblQKwhFatiBan = (BuscaVlrItem(lstrItens, "0302110290")) * lintMpr 'Consumo faturado bandeira
                                                ldblQKwhFatfpBan = (BuscaVlrItem(lstrItens, "0302110390")) * lintMpr 'Consumo faturado bandeira
                                            End If

                                            ldbVistoria = BuscaVlrItem(lstrItens, "9040000011") * lintMpr
                                            ldbVistoria_bif = BuscaVlrItem(lstrItens, "9040000012") * lintMpr
                                            ldbVistoria_trif = BuscaVlrItem(lstrItens, "9040000013") * lintMpr
                                            ldbVISTORIA_GRUPOA = BuscaVlrItem(lstrItens, "9040000014") * lintMpr
                                            ldbSERVTAXADO = BuscaVlrItem(lstrItens, "9040000021") * lintMpr
                                            ldbSERVTAXADO_BIF = BuscaVlrItem(lstrItens, "9040000022") * lintMpr
                                            ldbSERVTAXADO_TRIF = BuscaVlrItem(lstrItens, "9040000023") * lintMpr
                                            ldbVERIFNIVEL = BuscaVlrItem(lstrItens, "9040000031") * lintMpr
                                            ldbVERIFNIVEL_BIF = BuscaVlrItem(lstrItens, "9040000032") * lintMpr
                                            ldbVERIFNIVEL_TRIF = BuscaVlrItem(lstrItens, "9040000033") * lintMpr
                                            ldbSERVTAXADO_RN = BuscaVlrItem(lstrItens, "9040000041") * lintMpr
                                            ldbSERVTAXADO_RN_BIF = BuscaVlrItem(lstrItens, "9040000042") * lintMpr
                                            ldbSERVTAXADO_RN_TRIF = BuscaVlrItem(lstrItens, "9040000043") * lintMpr
                                            ldbSERVTAXADO_RN_GA = BuscaVlrItem(lstrItens, "9040000044") * lintMpr
                                            ldbSERVTAXADO_EM2 = BuscaVlrItem(lstrItens, "9040000061") * lintMpr
                                            ldbSERVTAXADO_EM2_BIF = BuscaVlrItem(lstrItens, "9040000062") * lintMpr
                                            ldbSERVTAXADO_EM2_TRIF = BuscaVlrItem(lstrItens, "9040000063") * lintMpr
                                            ldbSERVTAXADO_EM2_A4 = BuscaVlrItem(lstrItens, "9040000064") * lintMpr
                                            ldbDESLIGPROGRAMADO = BuscaVlrItem(lstrItens, "9040000121") * lintMpr
                                            ldbDESLIGPROGRAMADO_TRIF = BuscaVlrItem(lstrItens, "9040000123") * lintMpr
                                            ldbDESLIGPROGRAMADO_GRUPOA = BuscaVlrItem(lstrItens, "9040000124") * lintMpr
                                            ldbRELIGPROGRAMADA = BuscaVlrItem(lstrItens, "9040000111") * lintMpr
                                            ldbRELIGPROGRAMADA_TRIF = BuscaVlrItem(lstrItens, "9040000113") * lintMpr
                                            ldbRELIGPROGRAMADA_GRUPOA = BuscaVlrItem(lstrItens, "9040000124") * lintMpr
                                            ldbFORNECIMENTOPULSO_GRUPOA = BuscaVlrItem(lstrItens, "9040000134") * lintMpr
                                            ldbCUSTOADM = BuscaVlrItem(lstrItens, "9040000161") * lintMpr
                                            ldbCUSTOADM_GRUPOA = BuscaVlrItem(lstrItens, "9040000163") * lintMpr
                                            ldbAcrescimoMultaAtrasos = BuscaVlrItem(lstrItens, "9080000061") * lintMpr
                                            ldbJurosAtraso = BuscaVlrItem(lstrItens, "9080000066") * lintMpr
                                            ldbCorrecaoMonetaria = BuscaVlrItem(lstrItens, "9080000067") * lintMpr
                                            ldbServicosPretados = BuscaVlrItem(lstrItens, "9080000208") * lintMpr
                                            ldbDevolucaoaMaior = BuscaVlrItem(lstrItens, "9070000082") * lintMpr
                                            ldbDevolucaoConsumo = BuscaVlrItem(lstrItens, "9070000084") * lintMpr
                                            ldbDevolucaoCobrancaaMaior = BuscaVlrItem(lstrItens, "9070000004") * lintMpr
                                            ldbDevolucaoCobranca2via = BuscaVlrItem(lstrItens, "9070000063") * lintMpr
                                            ldbDevolucaoAfericaoMedidor = BuscaVlrItem(lstrItens, "9070000098") * lintMpr
                                            ldbDescontoIrriganteAquicultor = (BuscaVlrItem(lstrItens, "9070000008") + BuscaVlrItem(lstrItens, "9070000009")) * lintMpr
                                            ldbDevolucaoCorrecao = BuscaVlrItem(lstrItens, "9070000030") * lintMpr
                                            ldbRessarcimentoDMIC = BuscaVlrItem(lstrItens, "9070000037") * lintMpr
                                            ldbRessarcimentoDICFIC = (BuscaVlrItem(lstrItens, "9070000033") + BuscaVlrItem(lstrItens, "9070000034")) * lintMpr
                                            ldbRessarcimentoBonusItaipu = 0 'BuscaVlrItem(lstrItens, "9080000208") * lintMpr
                                            ldbRessarcimentoNivelTensao = BuscaVlrItem(lstrItens, "9070000040") * lintMpr
                                            ldbRessarcimentoViolacaoPrazo = BuscaVlrItem(lstrItens, "9070000041") * lintMpr
                                            ldbBonusItaipu911 = BuscaVlrItem(lstrItens, "9070000079") * lintMpr
                                            ldbBonusItaipuDevolver911 = 0 'BuscaVlrItem(lstrItens, "9070000079") * lintMpr

                                            If lstrClasse > "9100" Then
                                                ldbMicroResidencial = (BuscaVlrItem(lstrItens, "302022010") + BuscaVlrItem(lstrItens, "0302022011")) * lintMpr
                                                ldbICMSResidencial = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                            End If
                                            If lstrClasse > "0000" And lstrClasse < "3999" Then
                                                ldbMicroIndustrial = (BuscaVlrItem(lstrItens, "302022010") + BuscaVlrItem(lstrItens, "0302022011")) * lintMpr
                                                ldbICMSIndustrial = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                            End If
                                            If lstrClasse > "4000" And lstrClasse < "4999" Then
                                                ldbMicroComercial = (BuscaVlrItem(lstrItens, "302022010") + BuscaVlrItem(lstrItens, "0302022011")) * lintMpr
                                                ldbICMSComercial = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                            End If
                                            If lstrClasse > "5000" And lstrClasse < "5999" Then
                                                ldbMicroRural = (BuscaVlrItem(lstrItens, "302022010") + BuscaVlrItem(lstrItens, "0302022011")) * lintMpr
                                                ldbICMSRural = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                            End If

                                            If lstrClasse > "6100" And lstrClasse < "6199" Then
                                                ldbMicroPoderPublico = (BuscaVlrItem(lstrItens, "302022010") + BuscaVlrItem(lstrItens, "0302022011")) * lintMpr
                                                ldbICMSPoderPublica = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                            End If

                                            If lstrClasse > "6400" And lstrClasse < "6499" Then
                                                ldbICMSIluminacaoPublica = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                                ldbICMSMesPosteriorIlum = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                                ldbtotparcCMSImpCirMercServB = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                            End If

                                            If lstrClasse > "7000" And lstrClasse < "7999" Then
                                                ldbICMSServicoPublico = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                            End If

                                            If lstrClasse > "8000" And lstrClasse < "8999" Then
                                                ldbtotparcCMSImpCirMercServA = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                            End If

                                            ldbReceitaSistemaDistribuicao = (BuscaVlrItem(lstrItens, "9070000087") + BuscaVlrItem(lstrItens, "9070000091")) * lintMpr
                                            ldbReceitaSistemaDistribuicaoTaxa = BuscaVlrItem(lstrItens, "9080000098") * lintMpr
                                            ldbtudsmust = (BuscaVlrItem(lstrItens, "9070000087") + BuscaVlrItem(lstrItens, "9070000091")) * lintMpr
                                            ldbreceitabandeira = (BuscaVlrItem(lstrItens, "030212019") + BuscaVlrItem(lstrItens, "030212029") + BuscaVlrItem(lstrItens, "030212039")) * lintMpr
                                            ldbDiferenca = fRetornaValorItemFatura(lnumnf, anomesfat)
                                            ldbICMSConsumidorLivre = 0

                                            ldbICMSImpCirMercServ = ldbtotparcCMSImpCirMercServA - ldbtotparcCMSImpCirMercServB
                                            ldbicmsImpCircMercServST = ldbtotparcCMSImpCirMercServA - ldbtotparcCMSImpCirMercServB

                                            If lstrClasse = "0100" Then
                                                ldbicmsConsumoLivre = BuscaVlrItem(lstrItens, "602020001") * lintMpr
                                                ldbicmsConsumoPotenciaLivre = BuscaVlrItem(lstrItens, "602020001") * lintMpr
                                                ldbicmsImpCircMercServPosterior = BuscaVlrItem(lstrItens, "602020001") * lintMpr
                                                ldbICMSConsumidorPotencialmenteLivre = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                                ldbICMSMesPosteriorGrupoA = (BuscaVlrItem(lstrItens, "302030009") + BuscaVlrItem(lstrItens, "0302062009") + BuscaVlrItem(lstrItens, "0302030010")) * lintMpr
                                            End If


                                            'Reverso
                                            ldblQKwhFatTU = BuscaVlrItem(lstrItens, "030301009") * lintMpr 'consumo faturado TUSD
                                                ldblQkWhFatTE = BuscaVlrItem(lstrItens, "030401009") * lintMpr 'consumo faturado TE
                                                ldblQkWhFatTEBan = BuscaVlrItem(lstrItens, "030411009") * lintMpr 'consumo faturado TE bandeira

                                                If lintMicroGeracao = 1 Or lintMicroGeracaoDistribuida = 1 Then

                                                    'ldblQKwhReverso = BuscaVlrItem(lstrItens, "0302020996") * lintMpr 'Qtde Reverso

                                                    If lintMicroGeracaoConvenioSefaz = 0 Then
                                                        ldblQKwhReversop = 0 'implementar na rotina de calculo
                                                        ldblQKwhReversoi = 0 'implementar na rotina de calculo
                                                        ldblQKwhReversofp = BuscaVlrItem(lstrItens, "0302020992") * lintMpr 'Qtde Utilizado
                                                    Else
                                                        ldblQKwhReversop_sefaz = 0 'implementar na rotina de calculo
                                                        ldblQKwhReversoi_sefaz = 0 'implementar na rotina de calculo
                                                        ldblQKwhReversofp_sefaz = BuscaVlrItem(lstrItens, "0302020992") * lintMpr 'Qtde Utilizado
                                                    End If

                                                End If

                                                'Valor
                                                ldblVKwhCons = (BuscaVlrItem(lstrItens, "030202008") + BuscaVlrItem(lstrItens, "030202018") + BuscaVlrItem(lstrItens, "030202028") + BuscaVlrItem(lstrItens, "030202038")) * lintMpr 'Consumo lido

                                                If lintTarifaBranca = 0 Then
                                                    ldblVKwhFat = (BuscaVlrItem(lstrItens, "030202009")) * lintMpr 'Consumo faturado
                                                    ldblVKwhFatBan = (BuscaVlrItem(lstrItens, "030212009")) * lintMpr 'Consumo faturado bandeira
                                                Else
                                                    ldblVKwhFatp = (BuscaVlrItem(lstrItens, "030202019")) * lintMpr 'Consumo faturado
                                                    ldblVKwhFati = (BuscaVlrItem(lstrItens, "030202029")) * lintMpr 'Consumo faturado
                                                    ldblVKwhFatfp = (BuscaVlrItem(lstrItens, "030202039")) * lintMpr 'Consumo faturado
                                                    ldblVKwhFatpBan = (BuscaVlrItem(lstrItens, "030212019")) * lintMpr 'Consumo faturado bandeira
                                                    ldblVKwhFatiBan = (BuscaVlrItem(lstrItens, "030212029")) * lintMpr 'Consumo faturado bandeira
                                                    ldblVKwhFatfpBan = (BuscaVlrItem(lstrItens, "030212039")) * lintMpr 'Consumo faturado bandeira
                                                End If

                                                'Reverso
                                                ldblVkWhFatTU = BuscaVlrItem(lstrItens, "030302009") * lintMpr 'consumo faturado TUSD
                                                ldblVkWhFatTE = BuscaVlrItem(lstrItens, "030402009") * lintMpr 'consumo faturado TE
                                                ldblVkWhFatTEBan = BuscaVlrItem(lstrItens, "030412009") * lintMpr 'consumo faturado TE bandeira

                                                If lintMicroGeracao = 1 Or lintMicroGeracaoDistribuida = 1 Then

                                                    If lintMicroGeracaoConvenioSefaz = 0 Then

                                                        ldblQKwhReversop = 0 'implementar na rotina de calculo
                                                        ldblQKwhReversoi = 0 'implementar na rotina de calculo
                                                        ldblVKwhReversofp = BuscaVlrItem(lstrItens, "0302020994") * lintMpr 'Valor Reverso
                                                        'ldblVKwhReversofp = ldblVKwhReversofp + BuscaVlrItem(lstrItens, "0302020993") * lintMpr 'Valor Reverso bandeira

                                                    Else

                                                        ldblQKwhReversop_sefaz = 0 'implementar na rotina de calculo
                                                        ldblQKwhReversoi_sefaz = 0 'implementar na rotina de calculo
                                                        ldblVKwhReversofp_sefaz = BuscaVlrItem(lstrItens, "0302020994") * lintMpr 'Valor Reverso
                                                        'ldblVKwhReversofp_sefaz = ldblVKwhReversofp_sefaz + BuscaVlrItem(lstrItens, "0302020993") * lintMpr 'Valor Reverso bandeira

                                                    End If

                                                End If

                                                If lintMicroGeracaoConvenioSefaz = 0 Then

                                                    If ldblVKwhReversofp = 0 Then
                                                        If ldblVkWhFatTU + ldblVkWhFatTE > 0 Then
                                                            ldblVKwhReversofp = (ldblVKwhFat - (ldblVkWhFatTU + ldblVkWhFatTE)) * lintMpr 'Valor Reverso
                                                            If ldblVKwhFat - (ldblVkWhFatTU + ldblVkWhFatTE) > 0 Then
                                                                ldblVKwhReversofp = ldblVKwhReversofp + (ldblVKwhFatBan - ldblVkWhFatTEBan)
                                                            End If
                                                        End If
                                                    End If

                                                Else

                                                    If ldblVKwhReversofp_sefaz = 0 Then
                                                        If ldblVkWhFatTU + ldblVkWhFatTE > 0 Then
                                                            ldblVKwhReversofp_sefaz = (ldblVKwhFat - (ldblVkWhFatTU + ldblVkWhFatTE)) * lintMpr 'Valor Reverso
                                                            If ldblVKwhFat - (ldblVkWhFatTU + ldblVkWhFatTE) > 0 Then
                                                                ldblVKwhReversofp_sefaz = ldblVKwhReversofp_sefaz + (ldblVKwhFatBan - ldblVkWhFatTEBan)
                                                            End If
                                                        End If
                                                    End If

                                                End If

                                            End If

                                            'Demanda
                                            ldblQKwContr = BuscaVlrItem(lstrItens, "0302010016") 'Demanda Contratada
                                        ldblQKwFat = BuscaVlrItem(lstrItens, "0302010017") * lintMpr 'Demanda
                                        ldblQKwFatUfp = BuscaVlrItem(lstrItens, "0302010018") * lintMpr 'Demanda de ultrapassagem fp

                                        'Valor
                                        ldblVKwFat = BuscaVlrItem(lstrItens, "0302020015") * lintMpr 'Demanda
                                        ldblVKwFatUfp = BuscaVlrItem(lstrItens, "0302020018") * lintMpr 'Demanda de ultrapassagem Convencional
                                        ldblConsumoReativo = BuscaVlrItem(lstrItens, "0302020012") * lintMpr 'Consumo Reativo
                                        ldblDemandaReativa = BuscaVlrItem(lstrItens, "0302020031") * lintMpr 'Demanda Reativo



                                    ElseIf lstrGrupo = "AA" Or lstrGrupo = "AV" Or lstrGrupo = "AL" Or lstrGrupo = "AP" Or lstrGrupo = "AX" Then

                                        'Quantidade
                                        ldblQKwhFatp = 0
                                        ldblQKwhFatfp = 0
                                        ldblQKwhFatpLiv = 0
                                        ldblQKwhFatfpLiv = 0
                                        ldblQKwhFatpAUX = 0
                                        ldblQKwhFatfpAUX = 0

                                        'Micro Geração
                                        If lintMicroGeracaoConvenioSefaz = 0 Then
                                            ldblQKwhReversop = BuscaVlrItem(lstrItens, "0302012010") * lintMpr 'Qtde Medido Reverso na ponta
                                            ldblQKwhReversofp = BuscaVlrItem(lstrItens, "0302012011") * lintMpr 'Qtde Medido Reverso fora ponta
                                        Else
                                            ldblQKwhReversop_sefaz = BuscaVlrItem(lstrItens, "0302012010") * lintMpr 'Qtde Medido Reverso na ponta
                                            ldblQKwhReversofp_sefaz = BuscaVlrItem(lstrItens, "0302012011") * lintMpr 'Qtde Medido Reverso fora ponta
                                        End If

                                        'PLD
                                        ldblQKwhDevp = BuscaVlrItem(lstrItens, "0302010041") 'Consumo devolvido na p
                                        ldblQKwhDevfp = BuscaVlrItem(lstrItens, "0302010042") 'Consumo devolvido na fp
                                        If ldblQKwhDevp < 0 Then ldblQKwhDevp = ldblQKwhDevp * -1
                                        If ldblQKwhDevfp < 0 Then ldblQKwhDevfp = ldblQKwhDevfp * -1
                                        ldblQKwhDevp = ldblQKwhDevp * lintMpr 'Consumo devolvido na p
                                        ldblQKwhDevfp = ldblQKwhDevfp * lintMpr 'Consumo devolvido na fp
                                        ldblQKwhUtrp = BuscaVlrItem(lstrItens, "0302010043") * lintMpr 'Consumo ultrapassado na p
                                        ldblQKwhUtrfp = BuscaVlrItem(lstrItens, "0302010044") * lintMpr 'Consumo ultrapassado na fp

                                        If ldblQKwhFatp = 0 Then

                                            ldblQKwhFatp = (BuscaVlrItem(lstrItens, "0302010010")) * lintMpr 'Consumo faturado p

                                            If lintMicroGeracaoConvenioSefaz = 0 Then
                                                If ldblQKwhFatp <= ldblQKwhReversop Then
                                                    ldblQKwhReversop = ldblQKwhFatp
                                                    ldblQKwhFatp = 0
                                                Else
                                                    ldblQKwhFatp = ldblQKwhFatp - ldblQKwhReversop
                                                End If
                                            Else
                                                If ldblQKwhFatp <= ldblQKwhReversop_sefaz Then
                                                    ldblQKwhReversop_sefaz = ldblQKwhFatp
                                                    ldblQKwhFatp = 0
                                                Else
                                                    ldblQKwhFatp = ldblQKwhFatp - ldblQKwhReversop_sefaz
                                                End If
                                            End If

                                            ldblQKwhFatpBan = (BuscaVlrItem(lstrItens, "0302110010")) * lintMpr 'Consumo faturado p bandeira

                                            If ldblQKwhFatpBan > 0 Then
                                                If ldblQKwhFatpBan <= ldblQKwhFatpAUX Then
                                                    ldblQKwhFatpBan = 0
                                                Else
                                                    ldblQKwhFatpBan = ldblQKwhFatpBan - ldblQKwhFatpAUX
                                                End If
                                            End If

                                            If lstrGrupo = "AL" Then
                                                If lintParcialmenteCativo = 1 Then
                                                    ldblQKwhFatpLiv = BuscaVlrItem(lstrItens, "0302010060") * lintMpr 'Consumo faturado p
                                                    ldblQKwhFatp = ldblQKwhFatp - ldblQKwhFatpLiv
                                                    'ldblQKwhFatpBan = ldblQKwhFatpBan - ldblQKwhFatpLiv
                                                Else
                                                    ldblQKwhFatpLiv = BuscaVlrItem(lstrItens, "0302010010") * lintMpr 'Consumo faturado p
                                                    ldblQKwhFatp = ldblQKwhFatp - ldblQKwhFatpLiv
                                                    If ldblQKwhFatpBan >= 0 Then ldblQKwhFatpBan = 0
                                                End If
                                            End If

                                        End If

                                        If ldblQKwhFatfp = 0 Then

                                            ldblQKwhFatfp = (BuscaVlrItem(lstrItens, "0302010011")) * lintMpr 'Consumo faturado fp

                                            If lintMicroGeracaoConvenioSefaz = 0 Then
                                                If ldblQKwhFatfp <= ldblQKwhReversofp Then
                                                    ldblQKwhReversofp = ldblQKwhFatfp
                                                    ldblQKwhFatfp = 0
                                                Else
                                                    ldblQKwhFatfp = ldblQKwhFatfp - ldblQKwhReversofp
                                                End If
                                            Else
                                                If ldblQKwhFatfp <= ldblQKwhReversofp_sefaz Then
                                                    ldblQKwhReversofp_sefaz = ldblQKwhFatfp
                                                    ldblQKwhFatfp = 0
                                                Else
                                                    ldblQKwhFatfp = ldblQKwhFatfp - ldblQKwhReversofp_sefaz
                                                End If
                                            End If

                                            ldblQKwhFatfpBan = (BuscaVlrItem(lstrItens, "0302110011")) * lintMpr 'Consumo faturado fp

                                            If ldblQKwhFatfpBan > 0 Then
                                                If ldblQKwhFatfpBan <= ldblQKwhFatfpAUX Then
                                                    ldblQKwhFatfpBan = 0
                                                Else
                                                    ldblQKwhFatfpBan = ldblQKwhFatfpBan - ldblQKwhFatfpAUX
                                                End If
                                            End If

                                            If lstrGrupo = "AL" Then
                                                If lintParcialmenteCativo = 1 Then
                                                    ldblQKwhFatfpLiv = BuscaVlrItem(lstrItens, "0302010061") * lintMpr 'Consumo faturado fp
                                                    ldblQKwhFatfp = ldblQKwhFatfp - ldblQKwhFatfpLiv
                                                    'ldblQKwhFatfpBan = ldblQKwhFatfpBan - ldblQKwhFatfpLiv
                                                Else
                                                    ldblQKwhFatfpLiv = BuscaVlrItem(lstrItens, "0302010011") * lintMpr 'Consumo faturado fp
                                                    ldblQKwhFatfp = ldblQKwhFatfp - ldblQKwhFatfpLiv
                                                    If ldblQKwhFatfpBan >= 0 Then ldblQKwhFatfpBan = 0
                                                End If
                                            End If

                                        End If

                                        ldblQKwhCons = ldblQKwhFatp + ldblQKwhFatfp '+ ldblQKwFatp + ldblQKwFatfp

                                        'Micro Geração
                                        'ldblQKwhReversop = BuscaVlrItem(lstrItens, "0302012010") * lintMpr 'Qtde Medido Reverso na ponta
                                        'ldblQKwhReversofp = BuscaVlrItem(lstrItens, "0302012011") * lintMpr 'Qtde Medido Reverso fora ponta

                                        If lintMicroGeracaoConvenioSefaz = 0 Then

                                            ldblVKwhReversop = BuscaVlrItem(lstrItens, "0302022010") * lintMpr 'valor Medido Reverso na ponta
                                            'ldblVKwhReversop = ldblVKwhReversop + BuscaVlrItem(lstrItens, "0302022110") * lintMpr 'valor Medido Reverso na ponta bandeira
                                            ldblVKwhReversofp = BuscaVlrItem(lstrItens, "0302022011") * lintMpr 'valor Medido Reverso fora ponta
                                            'ldblVKwhReversofp = ldblVKwhReversofp + BuscaVlrItem(lstrItens, "0302022111") * lintMpr 'valor Medido Reverso fora ponta bandeira

                                        Else

                                            ldblVKwhReversop_sefaz = BuscaVlrItem(lstrItens, "0302022010") * lintMpr 'valor Medido Reverso na ponta
                                            'ldblVKwhReversop_sefaz = ldblVKwhReversop_sefaz + BuscaVlrItem(lstrItens, "0302022110") * lintMpr 'valor Medido Reverso na ponta bandeira
                                            ldblVKwhReversofp_sefaz = BuscaVlrItem(lstrItens, "0302022011") * lintMpr 'valor Medido Reverso fora ponta
                                            'ldblVKwhReversofp_sefaz = ldblVKwhReversofp_sefaz + BuscaVlrItem(lstrItens, "0302022111") * lintMpr 'valor Medido Reverso fora ponta bandeira

                                        End If

                                        '
                                        ldblQKwContr = BuscaVlrItem(lstrItens, "0302010020") 'Demanda Contratada p
                                        ldblQKwContr = BuscaVlrItem(lstrItens, "0302010024") 'Demanda Contratada fp
                                        ldblQKwFatp = BuscaVlrItem(lstrItens, "0302010021") * lintMpr ' + BuscaVlrItem(lstrItens, "0302010022")  'Demanda p
                                        ldblQKwFatfp = BuscaVlrItem(lstrItens, "0302010025") * lintMpr ' + BuscaVlrItem(lstrItens, "0302010026") 'Demanda fp

                                        ' Arlindo dif. demanda
                                        ldblQKwDifFatp = BuscaVlrItem(lstrItens, "0302010121") * lintMpr 'Dif. Demanda p
                                        ldblQKwDifFatfp = BuscaVlrItem(lstrItens, "0302010123") * lintMpr 'Dif. Demanda fp

                                        '
                                        'Arlindo
                                        'ldblQKwhCons = ldblQKwhFatp + ldblQKwhFatfp
                                        'ldblQKwhCons = ldblQKwhFatpBan + ldblQKwhFatfpBan '+ ldblQKwFatp + ldblQKwFatfp
                                        'ldblQKwhCons = ldblQKwhCons + ldblQKwhFatpBan + ldblQKwhFatfpBan

                                        ldblQKwFatUp = BuscaVlrItem(lstrItens, "0302010022") * lintMpr 'Demanda de ultrapassagem p
                                        ldblQKwFatUfp = BuscaVlrItem(lstrItens, "0302010026") * lintMpr 'Demanda de ultrapassagem fp

                                        'Valor
                                        ldblVKwhFatp = 0
                                        ldblVKwhFatfp = 0
                                        ldblVKwhFatpLiv = 0
                                        ldblVKwhFatfpLiv = 0
                                        ldblTarPSeca = BuscaVlrItem(lstrItens, "01AV011101") 'Tarifa Consumo Lido na Ponta
                                        ldblTarFPSeca = BuscaVlrItem(lstrItens, "01AV011102") 'Tarifa Consumo Lido na Fora Ponta
                                        ldblTarPUmida = BuscaVlrItem(lstrItens, "01AV011105") 'Tarifa Consumo Lido na Ponta
                                        ldblTarFPUmida = BuscaVlrItem(lstrItens, "01AV011106") 'Tarifa Consumo Lido na Fora Ponta
                                        ldblVKwhFatp = (BuscaVlrItem(lstrItens, "0302020010")) * lintMpr 'Consumo faturado p
                                        ldblVKwhFatpBan = BuscaVlrItem(lstrItens, "0302120010") * lintMpr 'Consumo faturado p

                                        If lstrGrupo = "AL" Then
                                            If lintParcialmenteCativo = 1 Then
                                                ldblVKwhFatpLiv = BuscaVlrItem(lstrItens, "0302020060") * lintMpr 'Consumo faturado p
                                            Else
                                                ldblVKwhFatpLiv = BuscaVlrItem(lstrItens, "0302020010") * lintMpr 'Consumo faturado p
                                                ldblVKwhFatp = ldblVKwhFatp - ldblVKwhFatpLiv
                                                If ldblVKwhFatpBan >= 0 Then ldblVKwhFatpBan = 0
                                            End If
                                        End If

                                        ldblVKwhFatfp = BuscaVlrItem(lstrItens, "0302020011") * lintMpr 'Consumo faturado fp
                                        ldblVKwhFatfpBan = BuscaVlrItem(lstrItens, "0302120011") * lintMpr 'Consumo faturado fp

                                        If lstrGrupo = "AL" Then
                                            If lintParcialmenteCativo = 1 Then
                                                ldblVKwhFatfpLiv = BuscaVlrItem(lstrItens, "0302020061") * lintMpr 'Consumo faturado fp
                                            Else
                                                ldblVKwhFatfpLiv = BuscaVlrItem(lstrItens, "0302020011") * lintMpr 'Consumo faturado fp
                                                ldblVKwhFatfp = ldblVKwhFatfp - ldblVKwhFatfpLiv
                                                If ldblVKwhFatfpBan >= 0 Then ldblVKwhFatfpBan = 0
                                            End If
                                        End If

                                        ldblVKwhDevp = BuscaVlrItem(lstrItens, "0302020041") * lintMpr 'Valor Consumo devolvido na p
                                        ldblVKwhDevfp = BuscaVlrItem(lstrItens, "0302020042") * lintMpr 'Valor Consumo devolvido na fp
                                        ldblVKwhUtrp = BuscaVlrItem(lstrItens, "0302020043") * lintMpr 'Valor Consumo ultrapassado na p
                                        ldblVKwhUtrfp = BuscaVlrItem(lstrItens, "0302020044") * lintMpr 'Valor Consumo ultrapassado na fp

                                        If lstrGrupo = "AV" Then
                                            ldblVKwhDevfp = ldblVKwhDevfp + (BuscaVlrItem(lstrItens, "0302060023") * lintMpr)
                                        End If

                                        ldblVKwhCons = ldblVKwhFatp + ldblVKwhFatfp '+ ldblVKwFatp + ldblVKwFatfp
                                        ldblVKwFatp = BuscaVlrItem(lstrItens, "0302020021") * lintMpr ' + BuscaVlrItem(lstrItens, "0302020022")  'Demanda p
                                        ldblVKwFatfp = (BuscaVlrItem(lstrItens, "0302020023") * lintMpr) '+ (BuscaVlrItem(lstrItens, "0302020072") * lintMpr) ' + BuscaVlrItem(lstrItens, "0302020026") 'Demanda fp

                                        'Arlindo dif. demanda
                                        ldblVKwDifFatp = BuscaVlrItem(lstrItens, "0302020121") * lintMpr ' 'Dif. Demanda p
                                        ldblVKwDifFatfp = (BuscaVlrItem(lstrItens, "0302020123") * lintMpr) 'Dif. Demanda fp
                                        '

                                        If lstrGrupo = "AL" Then
                                            ldblVKwFatp = ldblVKwFatp + (BuscaVlrItem(lstrItens, "0302060021") * lintMpr) 'dif.Demanda p
                                            ldblVKwFatfp = ldblVKwFatfp + (BuscaVlrItem(lstrItens, "0302060025") * lintMpr) 'dif.Demanda fp
                                            ldblVkwDscProv = BuscaVlrItem(lstrItens, "0302020051") * lintMpr 'Desconto Provisorio
                                            ldblVkwDscReal = BuscaVlrItem(lstrItens, "0302020052") * lintMpr 'Desconto real
                                            ldblVkwDevCcee = BuscaVlrItem(lstrItens, "0302020053") * lintMpr 'diferença a devolver
                                            ldblVkwCobCcee = BuscaVlrItem(lstrItens, "0302020054") * lintMpr 'diferença a cobrar
                                            '
                                            ldblVkwhDscProv = BuscaVlrItem(lstrItens, "0302020055") * lintMpr 'Desconto Provisorio
                                            ldblVkwhDscReal = BuscaVlrItem(lstrItens, "0302020056") * lintMpr 'Desconto real
                                            ldblVkwhDevCcee = BuscaVlrItem(lstrItens, "0302020057") * lintMpr 'diferença a devolver
                                            ldblVkwhCobCcee = BuscaVlrItem(lstrItens, "0302020058") * lintMpr 'diferença a cobrar
                                        End If

                                        ldblVKwFatUp = BuscaVlrItem(lstrItens, "0302020022") * lintMpr 'Demanda de ultrapassagem p
                                        ldblVKwFatUfp = BuscaVlrItem(lstrItens, "0302020026") * lintMpr 'Demanda de ultrapassagem fp

                                        'Reativo
                                        ldblConsumoReativop = BuscaVlrItem(lstrItens, "0302020013") * lintMpr 'Consumo Reativo p
                                        ldblConsumoReativofp = BuscaVlrItem(lstrItens, "0302020014") * lintMpr 'Consumo Reativo fp
                                        ldblDemandaReativap = BuscaVlrItem(lstrItens, "0302020027") * lintMpr 'Demanda Reativo p
                                        ldblDemandaReativafp = BuscaVlrItem(lstrItens, "0302020028") * lintMpr 'Demanda Reativo fp

                                    End If

                                    ldblTip = BuscaVlrItem(lstrItens, "0301000003") * lintMpr 'Vlr Iluminação Pública.

                                    'Tributos
                                    ldblIcms1 = (BuscaVlrItem(lstrItens, "0302030009") + BuscaVlrItem(lstrItens, "0302062009")) * lintMpr 'Vlr Icms.
                                    ldblIcms2 = BuscaVlrItem(lstrItens, "0302030010") * lintMpr 'Vlr Icms.
                                    ldblPis = (BuscaVlrItem(lstrItens, "0302020029") + BuscaVlrItem(lstrItens, "0302062029")) * lintMpr 'Vlr PIS.
                                    ldblCofins = (BuscaVlrItem(lstrItens, "0302020030") + BuscaVlrItem(lstrItens, "0302062030")) * lintMpr 'Vlr COFINS.
                                    ldblIcmsSt = (BuscaVlrItem(lstrItens, "0602020001") * lintMpr) + (BuscaVlrItem(lstrItens, "0604020001") * lintMpr)
                                    ldblPisSt = (BuscaVlrItem(lstrItens, "0602020002") * lintMpr) + (BuscaVlrItem(lstrItens, "0604020002") * lintMpr)
                                    ldblCofinsSt = (BuscaVlrItem(lstrItens, "0602020003") * lintMpr) + (BuscaVlrItem(lstrItens, "0604020003") * lintMpr)
                                    ldblIcmsPisCof = BuscaVlrItem(lstrItens, "0302030012") * lintMpr 'Vlr Icms/Pis/Cofins.
                                    ldblSaldoArre = BuscaVlrItem(lstrItens, "0302990041") * lintMpr
                                    ldblJuros = BuscaVlrItem(lstrItens, "0302050002") * lintMpr 'Juros verificar ainda com o calculo.
                                    ldblEncargo = (BuscaVlrItem(lstrItens, "0302070002") + BuscaVlrItem(lstrItens, "0302070005")) * lintMpr 'Encargos confirma ainda com o calculo
                                    ldblMust = BuscaVlrItem(lstrItens, "0302020032") * lintMpr 'EUSD - Must das usinas (EFLUL)

                                    'Isento icms

                                    ldblIsentoIcms = 0

                                    If ldblIcms1 = 0 Then
                                        ldblIsentoIcms = ldblBaseIcms1
                                    End If
                                    If ldblIcms2 = 0 Then
                                        ldblIsentoIcms = ldblIsentoIcms + ldblBaseIcms2
                                    End If
                                    If lstrGrupo = "AL" Then
                                        'ldblIsentoIcms = BuscaVlrItem(lstrItens, "0302030030") * lintMpr
                                        ldblDiferido = BuscaVlrItem(lstrItens, "0302030030") * lintMpr
                                    End If


                                    ldblDiversos = 0
                                    ldblTaxados = 0
                                    ldblConvenios = 0
                                    ldblRetencao = 0
                                    ldblDevolucao = 0
                                    ldblDevolucao_1 = 0
                                    ldblEncFinan = 0

                                    'Busca valor de Taxas, Diversos e Convenios
                                    For IndCodServ = 1 To Len(Trim(lstrItens))

                                        BuscaCodItem(IndCodServ, lstrItens)
                                        If IsNumeric(VlrCodServ) Then
                                            ldblAux = Val(VlrCodServ)
                                        Else
                                            ldblAux = 0
                                        End If

                                        If ldblAux < 0 Then
                                            ldblAux = ldblAux * -1
                                        End If

                                        lblnInclui = False

                                        'taxados

                                        If Mid(CodServ, 1, 4) = "9040" Or Mid(CodServ, 1, 4) = "9050" Then
                                            ldblTaxados = ldblTaxados + (ldblAux * lintMpr)
                                            lblnInclui = True
                                        End If

                                        'diversos
                                        If Mid(CodServ, 1, 4) = "9080" And Not (Mid(CodServ, 1, 10) = "9080000061" Or Mid(CodServ, 1, 10) = "9080000066" Or Mid(CodServ, 1, 10) = "9080000067") Then
                                            ldblDiversos = ldblDiversos + (ldblAux * lintMpr)
                                            lblnInclui = True
                                        End If

                                        'diversos
                                        If Mid(CodServ, 1, 10) = "9080000061" Or Mid(CodServ, 1, 10) = "9080000066" Or Mid(CodServ, 1, 10) = "9080000067" Then
                                            ldblEncFinan = ldblEncFinan + (ldblAux * lintMpr)
                                            lblnInclui = True
                                        End If

                                        'convenios
                                        If Mid(CodServ, 1, 4) = "9030" Then
                                            ldblConvenios = ldblConvenios + (ldblAux * lintMpr)
                                            lblnInclui = True
                                        End If

                                        'EUSD - must
                                        If Mid(CodServ, 1, 10) = "0302020032" Then
                                            lblnInclui = True
                                        End If

                                        'retencoes
                                        If Mid(CodServ, 1, 10) = "9070000088" Then 'Não será mais usado (futuro)
                                            ldblRetencao = ldblRetencao + (ldblAux * lintMpr)
                                            lblnInclui = True
                                        End If

                                        If Mid(CodServ, 1, 4) = "9090" Then
                                            ldblRetencao = ldblRetencao + (ldblAux * lintMpr)
                                            lblnInclui = True
                                        End If

                                        'Devoluções
                                        If Mid(CodServ, 1, 4) = "9070" And Mid(CodServ, 1, 10) <> "9070000088" And Mid(CodServ, 1, 10) <> "9070000008" And Mid(CodServ, 1, 10) <> "9070000009" And Mid(CodServ, 1, 10) <> "9070000002" And Mid(CodServ, 1, 10) <> "9070000094" And Mid(CodServ, 1, 10) <> "9070000095" And Mid(CodServ, 1, 10) <> "9070000001" And Mid(CodServ, 1, 10) <> "9070000003" And Mid(CodServ, 1, 10) <> "9070000062" Then
                                            ldblDevolucao = ldblDevolucao + (ldblAux * lintMpr)
                                            lblnInclui = True
                                        End If

                                        'Diferença



                                        'Arlindo 20/05/21
                                        If Mid(CodServ, 1, 10) = "9070000008" Or Mid(CodServ, 1, 10) = "9070000009" Or Mid(CodServ, 1, 10) = "9070000002" Or Mid(CodServ, 1, 10) = "9070000094" Or Mid(CodServ, 1, 10) = "9070000095" Or Mid(CodServ, 1, 10) = "9070000001" Or Mid(CodServ, 1, 10) = "9070000003" Or Mid(CodServ, 1, 10) = "9070000062" Then
                                            ldblDevolucao_1 = ldblDevolucao_1 + (ldblAux * lintMpr)
                                            lblnInclui = True
                                        End If

                                        If lblnInclui = True Then

                                            lstrSqlIAx = lstrSqlIAx & "('" & CodServ & "','" & codTodescri(CodServ) & "'" & "," & Replace(CStr(ldblAux * lintMpr), ",", ".") & ",'" & lstrZona & "'" & ",'" & Microsoft.VisualBasic.Left(lstrGrupo, 1) & "'), "

                                            lintContInsertItens = lintContInsertItens + 1

                                            If lintContInsertItens > 300 Then
                                                lstrSqlIte = lstrSqlIte & Microsoft.VisualBasic.Left(lstrSqlIAx, Len(lstrSqlIAx) - 2)
                                                objTempEstatisItens.TABLE = lstrSqlIte
                                                repositorioTempEstatisItens.Execute_2(objTempEstatisItens)

                                                lstrSqlIte = ""


                                                If CriaTabelas(lstrSqlPri, lstrSqlIte, False, False, True) Then
                                                    lstrSqlIAx = ""
                                                    lintContInsertItens = 1
                                                End If


                                            End If

                                        End If

                                    Next IndCodServ

                                    lstrSqlPAx = lstrSqlPAx & "('" & lstrClasse & "', " 'classe

                                    'Convencional
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhCons), ",", ".") & "', " 'qkwh_cons
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhFat), ",", ".") & "', " 'qkwh_fat
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhFatBan), ",", ".") & "', " 'qkwh_fat_ban
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwContr), ",", ".") & "', " 'qkw_contr
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwFat), ",", ".") & "', " 'qkw_fat
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwhCons), ",", ".") & "', " 'vkwh_cons

                                    If (ldblVkWhFatTU + ldblVkWhFatTE) > 0 Then
                                        lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVkWhFatTU + ldblVkWhFatTE), ",", ".") & "', " 'vkwh_fat
                                        lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVkWhFatTEBan), ",", ".") & "', " 'vkwh_fat_ban
                                    Else
                                        lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwhFat), ",", ".") & "', " 'vkwh_fat
                                        lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwhFatBan), ",", ".") & "', " 'vkwh_fat_ban
                                    End If

                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwFat), ",", ".") & "', " 'vkw_fat
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblConsumoReativo), ",", ".") & "', " 'vconreat
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblDemandaReativa), ",", ".") & "', " 'vdemreat

                                    'Horosazonal
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhFatp), ",", ".") & "', " 'qkwh_fat_p
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhFati), ",", ".") & "', " 'qkwh_fat_i
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhFatfp), ",", ".") & "', " 'qkwh_fat_fp
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhFatpBan), ",", ".") & "', " 'qkwh_fat_p_ban
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhFatiBan), ",", ".") & "', " 'qkwh_fat_i_ban
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhFatfpBan), ",", ".") & "', " 'qkwh_fat_fp_ban
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhFatpLiv), ",", ".") & "', " 'qkwh_fat_p_liv
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhFatfpLiv), ",", ".") & "', " 'qkwh_fat_fp_liv
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhDevp), ",", ".") & "', " 'qkwh_dev_p
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhDevfp), ",", ".") & "', " 'qkwh_dev_fp
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhUtrp), ",", ".") & "', " 'qkwh_utr_p
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhUtrfp), ",", ".") & "', " 'qkwh_utr_fp
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwContrp), ",", ".") & "', " 'qkw_contr_p
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwContrfp), ",", ".") & "', " 'qkw_contr_fp
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwFatp), ",", ".") & "', " 'qkw_fat_p
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwFatfp), ",", ".") & "', " 'qkw_fat_fp

                                    ' Arlindo dif. demanda
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwDifFatp), ",", ".") & "', " 'qkw_dif_fat_p
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwDifFatfp), ",", ".") & "', " 'qkw_dif_fat_fp
                                    '

                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwFatUp), ",", ".") & "', " 'qkw_fatu_p
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwFatUfp), ",", ".") & "', " 'qkw_fatu_fp

                                    'valor
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwhFatp), ",", ".") & "', " 'vkwh_fat_p
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwhFati), ",", ".") & "', " 'vkwh_fat_i
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwhFatfp), ",", ".") & "', " 'vkwh_fat_fp
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwhFatpBan), ",", ".") & "', " 'vkwh_fat_p_ban
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwhFatiBan), ",", ".") & "', " 'vkwh_fat_i_ban
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwhFatfpBan), ",", ".") & "', " 'vkwh_fat_fp_ban
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwhFatpLiv), ",", ".") & "', " 'vkwh_fat_p_liv
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwhFatfpLiv), ",", ".") & "', " 'vkwh_fat_fp_liv
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwhDevp), ",", ".") & "', " 'vkwh_dev_p
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwhDevfp), ",", ".") & "', " 'vkwh_dev_fp
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwhUtrp), ",", ".") & "', " 'vkwh_utr_p
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwhUtrfp), ",", ".") & "', " 'vkwh_utr_fp
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwFatp), ",", ".") & "', " 'vkw_fat_p
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwFatfp), ",", ".") & "', " 'vkw_fat_fp

                                    ' Arlindo dif. demanda
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwDifFatp), ",", ".") & "', " 'vkw_dif_fat_p
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwDifFatfp), ",", ".") & "', " 'vkw_dif_fat_fp
                                    '

                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwFatUp), ",", ".") & "', " 'vkw_fatu_p
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwFatUfp), ",", ".") & "', " 'vkw_fatu_fp
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVkwDscProv), ",", ".") & "', " 'vkw_dsc_prov
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVkwDscReal), ",", ".") & "', " 'vkw_dsc_real
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVkwDevCcee), ",", ".") & "', " 'vkw_dev_ccee
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVkwCobCcee), ",", ".") & "', " 'vkw_cob_ccee

                                    '
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVkwhDscProv), ",", ".") & "', " 'vkwh_dsc_prov
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVkwhDscReal), ",", ".") & "', " 'vkwh_dsc_real
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVkwhDevCcee), ",", ".") & "', " 'vkwh_dev_ccee
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVkwhCobCcee), ",", ".") & "', " 'vkwh_cob_ccee
                                    '

                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblConsumoReativop), ",", ".") & "', " 'vconreat_p = Consumo Reativo ponta
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblConsumoReativofp), ",", ".") & "', " 'vconreat_fp = Consumo Reativo fora ponta
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblDemandaReativap), ",", ".") & "', " 'vdemreat_p = Demanda Reativo ponta
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblDemandaReativafp), ",", ".") & "', " 'vdemreat_fp = Demanda Reativo fora ponta
                                    'lstrSqlPAx = lstrSqlPAx & "'" & Replace(ldblQKwhReverso, ",", ".") & "', " 'reverso
                                    'lstrSqlPAx = lstrSqlPAx & "'" & Replace(ldblVKwhReverso, ",", ".") & "', " 'valor
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhReversop), ",", ".") & "', " 'Qtd Medido reverso ponta
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhReversoi), ",", ".") & "', " 'Qtd Medido reverso intermediario
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhReversofp), ",", ".") & "', " 'Qtd Medido reverso fora ponta
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwhReversop), ",", ".") & "', " 'valor Medido reverso ponta
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwhReversoi), ",", ".") & "', " 'valor Medido reverso intermediario
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwhReversofp), ",", ".") & "', " 'valor Medido reverso fora ponta
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhReversop_sefaz), ",", ".") & "', " 'Qtd Medido reverso ponta
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhReversoi_sefaz), ",", ".") & "', " 'Qtd Medido reverso intermediario
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhReversofp_sefaz), ",", ".") & "', " 'Qtd Medido reverso fora ponta
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwhReversop_sefaz), ",", ".") & "', " 'valor Medido reverso ponta
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwhReversoi_sefaz), ",", ".") & "', " 'valor Medido reverso intermediario
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwhReversofp_sefaz), ",", ".") & "', " 'valor Medido reverso fora ponta

                                    'Outros
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblTip), ",", ".") & "', " 'tip
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblSaldoArre), ",", ".") & "', " 'arrendodamento
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblEncFinan), ",", ".") & "', " 'encargos financeiros
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblIcms1 + ldblIcms2), ",", ".") & "', " 'icms
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblPis), ",", ".") & "', " 'pis
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblCofins), ",", ".") & "', " 'cofins
                                    '
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblIcmsSt), ",", ".") & "', " 'icms - st
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblPisSt), ",", ".") & "', " 'pis - st
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblCofinsSt), ",", ".") & "', " 'cofins - st
                                    '
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblBaseIcms1 + ldblBaseIcms2), ",", ".") & "', " 'base icms
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblBasePis), ",", ".") & "', " 'base pis
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblBaseCofins), ",", ".") & "', " 'base cofins
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblIcmsPisCof), ",", ".") & "', " 'icms/pis/cofins
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblIsentoIcms), ",", ".") & "', " 'Isento icms
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblDiferido), ",", ".") & "', " 'diferido
                                    '
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblDifEltrbr), ",", ".") & "', " 'dif_eltrbr
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblJuros), ",", ".") & "', " 'juros
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblEncargo), ",", ".") & "', " 'encargo
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblMust), ",", ".") & "', " 'must
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldblValorTot)), ",", ".") & "', " 'total
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldblTaxados)), ",", ".") & "', " 'taxados
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldblDiversos)), ",", ".") & "', " 'diversos
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldblConvenios)), ",", ".") & "', " 'convenios
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldblDevolucao)), ",", ".") & "', " 'descontos
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldblDevolucao_1)), ",", ".") & "', " 'descontos_1
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldblRetencao)), ",", ".") & "', " 'retencao
                                    '
                                    lstrSqlPAx = lstrSqlPAx & "'" & lstrFlgMedFat & "', " 'flgmedtfat = Fatura que foi cancelada e reprocessada
                                    lstrSqlPAx = lstrSqlPAx & "'" & lintFaturaFinal & "', " 'fatura_final
                                    lstrSqlPAx = lstrSqlPAx & "'" & lintIrrigacaoAquicultura & "', " 'irrigacao_aquicultura

                                    lstrSqlPAx = lstrSqlPAx & "'" & lintMicroGeracao & "', " 'micro geracao
                                    lstrSqlPAx = lstrSqlPAx & "'" & lintMicroGeracaoDistribuida & "', " 'micro geracao
                                    lstrSqlPAx = lstrSqlPAx & "'" & lintMicroGeracaoConvenioSefaz & "', " 'micro geracao convenio Sefaz
                                    lstrSqlPAx = lstrSqlPAx & "'" & lintTarifaBranca & "', " 'tarifa_branca

                                    '************************************

                                    If lintMicroGeracaoConvenioSefaz = 0 Then
                                        'lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldblVKwhReverso + ldblVKwhReversop + ldblVKwhReversofp)), ",", ".") & "', " 'reverso para o importe
                                        lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldblVKwhReversop + ldblVKwhReversoi + ldblVKwhReversofp)), ",", ".") & "', " 'reverso para o importe
                                    Else
                                        lstrSqlPAx = lstrSqlPAx & "'0',"
                                    End If

                                    lstrSqlPAx = lstrSqlPAx & "'" & lstrFlgSt & "', " 'situação
                                    '
                                    lstrSqlPAx = lstrSqlPAx & "'" & lstrFase & "', " 'fase

                                    lstrGrupo = IIf(Microsoft.VisualBasic.Left(lstrGrupo, 1) = "A", lstrGrupo, Microsoft.VisualBasic.Left(lstrGrupo, 1))

                                    If lstrGrupo = "AL" Then
                                        lstrGrupo = lstrGrupo & IIf(lintParcialmenteCativo = 1, "P", "")
                                        lstrGrupo = lstrGrupo & IIf(lintLivreVerde = 1, "V", "A")
                                    End If

                                    lstrSqlPAx = lstrSqlPAx & "'" & lstrGrupo & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & lstrZona & "','" & lstrLivro & "','" & lstrConta & "','" & Replace(lstrNome, "'", "\'") & "',"

                                    If lstrGrupoT = "AL" Then
                                        lstrGrupoT = lstrGrupoT & IIf(lintParcialmenteCativo = 1, "P", "")
                                        lstrGrupoT = lstrGrupoT & IIf(lintLivreVerde = 1, "V", "A")
                                    End If

                                    lstrSqlPAx = lstrSqlPAx & "'" & lstrGrupoT & "',"

                                    If lstrGrupoT = "ALA" Then
                                        lintSequencia = 1
                                    ElseIf lstrGrupoT = "ALPA" Then
                                        lintSequencia = 2
                                    ElseIf lstrGrupoT = "ALV" Then
                                        lintSequencia = 3
                                    ElseIf lstrGrupoT = "ALPV" Then
                                        lintSequencia = 4
                                    Else
                                        lintSequencia = 0
                                    End If

                                    lstrSqlPAx = lstrSqlPAx & "'" & lintSequencia & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & lstrMesAno & "',"

                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbVistoria)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbVistoria_bif)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbVistoria_trif)), ",", ".") & "'," ' FEITO
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbVISTORIA_GRUPOA)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbSERVTAXADO)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbSERVTAXADO_BIF)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbSERVTAXADO_TRIF)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbVERIFNIVEL)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbVERIFNIVEL_BIF)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbVERIFNIVEL_TRIF)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbSERVTAXADO_RN)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbSERVTAXADO_RN_BIF)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbSERVTAXADO_RN_TRIF)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbSERVTAXADO_RN_GA)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbSERVTAXADO_EM2)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbSERVTAXADO_EM2_BIF)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbSERVTAXADO_EM2_TRIF)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbSERVTAXADO_EM2_A4)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbDESLIGPROGRAMADO)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbDESLIGPROGRAMADO_TRIF)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbDESLIGPROGRAMADO_GRUPOA)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbRELIGPROGRAMADA)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbRELIGPROGRAMADA_TRIF)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbRELIGPROGRAMADA_GRUPOA)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbFORNECIMENTOPULSO_GRUPOA)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbCUSTOADM)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbCUSTOADM_GRUPOA)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbAcrescimoMultaAtrasos)), ",", ".") & "', " ' ULTIMO SEM VÍRGULA
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbJurosAtraso)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbCorrecaoMonetaria)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbServicosPretados)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbDevolucaoaMaior)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbDevolucaoConsumo)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbDevolucaoCobrancaaMaior)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbDevolucaoCobranca2via)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbDevolucaoAfericaoMedidor)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbDescontoIrriganteAquicultor)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbDevolucaoCorrecao)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbRessarcimentoDMIC)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbRessarcimentoDICFIC)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbRessarcimentoNivelTensao)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbRessarcimentoViolacaoPrazo)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbBonusItaipu911)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbBonusItaipuDevolver911)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbMicroResidencial)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbMicroIndustrial)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbMicroComercial)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbMicroRural)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbMicroPoderPublico)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbReceitaSistemaDistribuicao)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbReceitaSistemaDistribuicaoTaxa)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbtudsmust)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbreceitabandeira)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbDiferenca)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbICMSResidencial)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbICMSIndustrial)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbICMSComercial)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbICMSRural)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbICMSPoderPublica)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbICMSIluminacaoPublica)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbICMSServicoPublico)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbICMSConsumidorLivre)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbICMSConsumidorPotencialmenteLivre)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbICMSImpCirMercServ)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbICMSMesPosteriorIlum)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbICMSMesPosteriorGrupoA)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbicmsConsumoLivre)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbicmsConsumoPotenciaLivre)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbicmsImpCircMercServST)), ",", ".") & "',"
                                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(Trim(Str(ldbicmsImpCircMercServPosterior)), ",", ".") & "'"
                                    lstrSqlPAx = lstrSqlPAx & "), "

                                    lintContInsertPri = lintContInsertPri + 1

                                    If lintContInsertPri > 300 Then
                                        lstrSqlPri = lstrSqlPri & Microsoft.VisualBasic.Left(lstrSqlPAx, Len(lstrSqlPAx) - 2)
                                        objTempEstatis.TABLE = lstrSqlPri
                                        repositorioTempEstatis.Execute_2(objTempEstatis)

                                        lstrSqlPri = ""

                                        If CriaTabelas(lstrSqlPri, lstrSqlIte, False, True, False) Then
                                            lstrSqlPAx = ""
                                            lintContInsertPri = 1
                                        End If

                                    End If

                                End If

                                BarraProgresso(Me.ProgressBar, Me.lblPerc, Me.lblInfo, 1)

                                System.Windows.Forms.Application.DoEvents()

                            Next

                            If lintContInsertItens > 0 And Len(Trim(lstrSqlIAx)) > 0 Then
                                lstrSqlIte = lstrSqlIte & Microsoft.VisualBasic.Left(lstrSqlIAx, Len(lstrSqlIAx) - 2)
                                objTempEstatisItens.TABLE = lstrSqlIte
                                repositorioTempEstatisItens.Execute_2(objTempEstatisItens)
                            End If

                            If lintContInsertPri > 0 And Len(Trim(lstrSqlPAx)) > 0 Then
                                lstrSqlPri = lstrSqlPri & Microsoft.VisualBasic.Left(lstrSqlPAx, Len(lstrSqlPAx) - 2)
                                objTempEstatis.TABLE = lstrSqlPri
                                repositorioTempEstatis.Execute_2(objTempEstatis)
                            End If

                            lblnRetorno = True

                        End If 'Criatabelas


                    End If

                Else
                    MessageBox.Show("Não existem faturas no período selecionado!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    lblnRetorno = False
                End If

            End If

            MontaTabela = lblnRetorno

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: MontaTabela" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function
        End Try

    End Function

    Private Sub frmExportaContabilidade_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed

        Try

            objExecAtualizaBancoDados.QUERY_NAME = "drop table if exists " & istrTabTemp
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            objExecAtualizaBancoDados.QUERY_NAME = "drop table if exists " & istrTabTempItens
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            'objExecAtualizaBancoDados.QUERY_NAME = "drop table if exists temp_contabilidade"
            'repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: FrmRelEFechamento_FormClosed" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

    Private Function CriaTabelas(ByRef astrSqlPri As String, ByRef astrSqlIte As String, ByRef aboolCriarTabela As Boolean, ByRef aboolCriarSqlPri As Boolean, ByRef aboolCriarSqlIte As Boolean) As Boolean

        Dim lblnRet As Boolean
        Dim lstrsql As String

        Dim objTempEstatisItens As New entidadeTempEstatisItens
        Dim repositorioTempEstatisItens As New repositorioTempEstatisItens

        Dim objTempEstatis As New entidadeTempEstatis
        Dim repositorioTempEstatis As New repositorioTempEstatis

        Try

            lblnRet = False

            If aboolCriarTabela = True Then


                objExecAtualizaBancoDados.QUERY_NAME = "drop table if exists temp_estatis"
                repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
                lstrsql = ""
                lstrsql = lstrsql & " CREATE  TABLE temp_estatis(CLASSE       VARCHAR(6), " & vbCrLf
                lstrsql = lstrsql & "QKWH_CONS    Double, " & vbCrLf
                lstrsql = lstrsql & "QKWH_FAT     Double, " & vbCrLf
                lstrsql = lstrsql & "QKWH_FAT_BAN Double, " & vbCrLf
                lstrsql = lstrsql & "QKW_CONTR    Double, " & vbCrLf
                lstrsql = lstrsql & "QKW_FAT      Double, " & vbCrLf
                lstrsql = lstrsql & "VKWH_CONS    Double,  " & vbCrLf
                lstrsql = lstrsql & "VKWH_FAT     Double, " & vbCrLf
                lstrsql = lstrsql & "VKWH_FAT_BAN Double, " & vbCrLf
                lstrsql = lstrsql & "VKW_FAT      Double, " & vbCrLf
                lstrsql = lstrsql & "VCONREAT     Double, " & vbCrLf
                lstrsql = lstrsql & "VDEMREAT     Double, " & vbCrLf
                lstrsql = lstrsql & "QKWH_FAT_P      Double," & vbCrLf
                lstrsql = lstrsql & "QKWH_FAT_I      Double, " & vbCrLf
                lstrsql = lstrsql & "QKWH_FAT_FP     Double, " & vbCrLf
                lstrsql = lstrsql & "QKWH_FAT_P_BAN  Double, " & vbCrLf
                lstrsql = lstrsql & "QKWH_FAT_I_BAN  Double, " & vbCrLf
                lstrsql = lstrsql & "QKWH_FAT_FP_BAN Double, " & vbCrLf
                lstrsql = lstrsql & "QKWH_FAT_P_LIV  Double, " & vbCrLf
                lstrsql = lstrsql & "QKWH_FAT_FP_LIV Double, " & vbCrLf
                lstrsql = lstrsql & "QKWH_DEV_P   Double, " & vbCrLf
                lstrsql = lstrsql & "QKWH_DEV_FP  Double, " & vbCrLf
                lstrsql = lstrsql & "QKWH_UTR_P   Double, " & vbCrLf
                lstrsql = lstrsql & "QKWH_UTR_FP  Double, " & vbCrLf
                lstrsql = lstrsql & "QKW_CONTR_P  Double, " & vbCrLf
                lstrsql = lstrsql & "QKW_CONTR_FP Double, " & vbCrLf
                lstrsql = lstrsql & "QKW_FAT_P    Double, " & vbCrLf
                lstrsql = lstrsql & "QKW_FAT_FP   Double, " & vbCrLf
                lstrsql = lstrsql & "QKW_DIF_FAT_P    Double," & vbCrLf
                lstrsql = lstrsql & "QKW_DIF_FAT_FP   Double, " & vbCrLf
                lstrsql = lstrsql & "QKW_FATU_P   Double, " & vbCrLf
                lstrsql = lstrsql & "QKW_FATU_FP  Double, " & vbCrLf
                lstrsql = lstrsql & "VKWH_FAT_P      Double, " & vbCrLf
                lstrsql = lstrsql & "VKWH_FAT_I      Double,  " & vbCrLf
                lstrsql = lstrsql & "VKWH_FAT_FP     Double, " & vbCrLf
                lstrsql = lstrsql & "VKWH_FAT_P_BAN  Double,  " & vbCrLf
                lstrsql = lstrsql & "VKWH_FAT_I_BAN  Double,  " & vbCrLf
                lstrsql = lstrsql & "VKWH_FAT_FP_BAN Double, " & vbCrLf
                lstrsql = lstrsql & "VKWH_FAT_P_LIV  Double,  " & vbCrLf
                lstrsql = lstrsql & "VKWH_FAT_FP_LIV Double,  " & vbCrLf
                lstrsql = lstrsql & "VKWH_DEV_P   Double, " & vbCrLf
                lstrsql = lstrsql & "VKWH_DEV_FP  Double,  " & vbCrLf
                lstrsql = lstrsql & "VKWH_UTR_P   Double, " & vbCrLf
                lstrsql = lstrsql & "VKWH_UTR_FP  Double,  " & vbCrLf
                lstrsql = lstrsql & "VKW_FAT_P    Double, " & vbCrLf
                lstrsql = lstrsql & "VKW_FAT_FP   Double,  " & vbCrLf
                lstrsql = lstrsql & "VKW_DIF_FAT_P    Double, "& vbCrLf
                lstrsql = lstrsql & "VKW_DIF_FAT_FP   Double,  " & vbCrLf
                lstrsql = lstrsql & "VKW_FATU_P   Double,  " & vbCrLf
                lstrsql = lstrsql & "VKW_FATU_FP  Double, " & vbCrLf
                lstrsql = lstrsql & "VKW_DSC_PROV Double,  " & vbCrLf
                lstrsql = lstrsql & "VKW_DSC_REAL Double, " & vbCrLf
                lstrsql = lstrsql & "VKW_DEV_CCEE Double,  " & vbCrLf
                lstrsql = lstrsql & "VKW_COB_CCEE Double, " & vbCrLf
                lstrsql = lstrsql & "VKWH_DSC_PROV Double,  " & vbCrLf
                lstrsql = lstrsql & "VKWH_DSC_REAL Double, " & vbCrLf
                lstrsql = lstrsql & "VKWH_DEV_CCEE Double,  " & vbCrLf
                lstrsql = lstrsql & "VKWH_COB_CCEE Double, " & vbCrLf
                lstrsql = lstrsql & "QKWH_REVERSOP  Double,  " & vbCrLf
                lstrsql = lstrsql & "QKWH_REVERSOI  Double, " & vbCrLf
                lstrsql = lstrsql & "QKWH_REVERSOFP  Double,  " & vbCrLf
                lstrsql = lstrsql & "VKWH_REVERSOP  Double, " & vbCrLf
                lstrsql = lstrsql & "VKWH_REVERSOI  Double,  " & vbCrLf
                lstrsql = lstrsql & "VKWH_REVERSOFP  Double, " & vbCrLf
                lstrsql = lstrsql & "QKWH_REVERSOP_SEFAZ  Double,  " & vbCrLf
                lstrsql = lstrsql & "QKWH_REVERSOI_SEFAZ  Double, " & vbCrLf
                lstrsql = lstrsql & "QKWH_REVERSOFP_SEFAZ  Double,  " & vbCrLf
                lstrsql = lstrsql & "VKWH_REVERSOP_SEFAZ  Double, " & vbCrLf
                lstrsql = lstrsql & "VKWH_REVERSOI_SEFAZ  Double,  " & vbCrLf
                lstrsql = lstrsql & "VKWH_REVERSOFP_SEFAZ  Double, " & vbCrLf
                lstrsql = lstrsql & "VCONREAT_P   Double,  " & vbCrLf
                lstrsql = lstrsql & "VCONREAT_FP  Double, " & vbCrLf
                lstrsql = lstrsql & "VDEMREAT_P   Double,  " & vbCrLf
                lstrsql = lstrsql & "VDEMREAT_FP  Double, " & vbCrLf
                lstrsql = lstrsql & "TIP          Double,  " & vbCrLf
                lstrsql = lstrsql & "SLD_ARRED    Double, " & vbCrLf
                lstrsql = lstrsql & "ENC_FINAN    Double,  " & vbCrLf
                lstrsql = lstrsql & "ICMS         Double, " & vbCrLf
                lstrsql = lstrsql & "PIS          Double,  " & vbCrLf
                lstrsql = lstrsql & "COFINS       Double, " & vbCrLf
                lstrsql = lstrsql & "ICMS_ST      Double, " & vbCrLf
                lstrsql = lstrsql & "PIS_ST       Double, " & vbCrLf
                lstrsql = lstrsql & "COFINS_ST    Double, " & vbCrLf
                lstrsql = lstrsql & "BASEICMS     Double, " & vbCrLf
                lstrsql = lstrsql & "BASEPIS      Double, " & vbCrLf
                lstrsql = lstrsql & "BASECOFINS   Double, " & vbCrLf
                lstrsql = lstrsql & "ICMSPISCOF   Double, " & vbCrLf
                lstrsql = lstrsql & "ISENTOICMS   Double, " & vbCrLf
                lstrsql = lstrsql & "DIFERIDO   Double, " & vbCrLf
                lstrsql = lstrsql & "DIF_ELTRBR   Double, " & vbCrLf
                lstrsql = lstrsql & "JUROS        Double, " & vbCrLf
                lstrsql = lstrsql & "MULTA        Double, " & vbCrLf
                lstrsql = lstrsql & "ENCARGO      Double, " & vbCrLf
                lstrsql = lstrsql & "MUST         Double, " & vbCrLf
                lstrsql = lstrsql & "TOTAL        Double, " & vbCrLf
                lstrsql = lstrsql & "TAXADOS      Double, " & vbCrLf
                lstrsql = lstrsql & "DIVERSOS     Double, " & vbCrLf
                lstrsql = lstrsql & "CONVENIOS    Double, " & vbCrLf
                lstrsql = lstrsql & "DEVOLUCOES   Double, " & vbCrLf
                lstrsql = lstrsql & "DESCONTOS    Double, " & vbCrLf
                lstrsql = lstrsql & "DESCONTOS_1  Double, " & vbCrLf
                lstrsql = lstrsql & "RETENCAO     Double, " & vbCrLf
                lstrsql = lstrsql & "FLGMEDFAT    CHAR(1), " & vbCrLf
                lstrsql = lstrsql & "FATURA_FINAL TINYINT(1), " & vbCrLf
                lstrsql = lstrsql & "IRRIGACAO_AQUICULTURA TINYINT(1)," & vbCrLf
                lstrsql = lstrsql & "MICRO_GERACAO TINYINT(1), " & vbCrLf
                lstrsql = lstrsql & "MICRO_GERACAO_DISTRIBUIDA TINYINT(1), " & vbCrLf
                lstrsql = lstrsql & "MICRO_GERACAO_CONVENIO_SEFAZ TINYINT(1), " & vbCrLf
                lstrsql = lstrsql & "TARIFA_BRANCA TINYINT(1), " & vbCrLf
                lstrsql = lstrsql & "VKWH_REVERSO_IMPORTE  Double, " & vbCrLf
                lstrsql = lstrsql & "FLGST        CHAR(1), " & vbCrLf
                lstrsql = lstrsql & "FASE         Char(1), " & vbCrLf
                lstrsql = lstrsql & "GRUPO        CHAR(4), " & vbCrLf
                lstrsql = lstrsql & "ZONA         Char(3), " & vbCrLf
                lstrsql = lstrsql & "LIVRO        CHAR(2), " & vbCrLf
                lstrsql = lstrsql & "CONTA        Char(6), " & vbCrLf
                lstrsql = lstrsql & "NOME         VARCHAR(100)," & vbCrLf
                lstrsql = lstrsql & "GRUPOT       Char(4), " & vbCrLf
                lstrsql = lstrsql & "SEQUENCIA    CHAR(1), " & vbCrLf
                lstrsql = lstrsql & "MESANO       VARCHAR(7), " & vbCrLf

                lstrsql = lstrsql & "VISTORIA                  Double, " & vbCrLf
                lstrsql = lstrsql & "VISTORIA_BIF              Double, " & vbCrLf
                lstrsql = lstrsql & "VISTORIA_TRIF             Double, " & vbCrLf
                lstrsql = lstrsql & "VISTORIA_GRUPOA           Double, " & vbCrLf
                ' lstrsql = lstrsql & "SERVTAXADOS               Double, " & vbCrLf
                'lstrsql = lstrsql & "SERVTAXADOS_BIF           Double, " & vbCrLf
                'lstrsql = lstrsql & "SERVTAXADOS_TRIF          Double, " & vbCrLf
                lstrsql = lstrsql & "VERIFNIVEL                Double, " & vbCrLf
                lstrsql = lstrsql & "VERIFNIVEL_BIF            Double, " & vbCrLf
                lstrsql = lstrsql & "VERIFNIVEL_TRIF           Double, " & vbCrLf
                lstrsql = lstrsql & "SERVTAXADO                Double, " & vbCrLf
                lstrsql = lstrsql & "SERVTAXADO_BIF            Double, " & vbCrLf
                lstrsql = lstrsql & "SERVTAXADO_TRIF           Double, " & vbCrLf
                lstrsql = lstrsql & "SERVTAXADO_RN             Double, " & vbCrLf
                lstrsql = lstrsql & "SERVTAXADO_RN_BIF         Double, " & vbCrLf
                lstrsql = lstrsql & "SERVTAXADO_RN_TRIF        Double, " & vbCrLf
                lstrsql = lstrsql & "SERVTAXADO_RN_GA          Double, " & vbCrLf
                lstrsql = lstrsql & "SERVTAXADO_EM2            Double, " & vbCrLf
                lstrsql = lstrsql & "SERVTAXADO_EM2_BIF        Double, " & vbCrLf
                lstrsql = lstrsql & "SERVTAXADO_EM2_TRIF       Double, " & vbCrLf
                lstrsql = lstrsql & "SERVTAXADO_EM2_A4         Double, " & vbCrLf
                lstrsql = lstrsql & "DESLIGPROGRAMADO          Double, " & vbCrLf
                lstrsql = lstrsql & "DESLIGPROGRAMADO_TRIF     Double, " & vbCrLf
                lstrsql = lstrsql & "DESLIGPROGRAMADO_GRUPOA4  Double, " & vbCrLf
                lstrsql = lstrsql & "RELIGPROGRAMADA           Double, " & vbCrLf
                lstrsql = lstrsql & "RELIGPROGRAMADA_TRIF      Double, " & vbCrLf
                lstrsql = lstrsql & "RELIGPROGRAMADA_GRUPOA    Double, " & vbCrLf
                lstrsql = lstrsql & "FORNECIMENTOPULSO_GRUPOA  Double, " & vbCrLf
                lstrsql = lstrsql & "CUSTOADM                  Double, " & vbCrLf
                lstrsql = lstrsql & "CUSTOADM_GRUPOA           Double, " & vbCrLf
                lstrsql = lstrsql & "ACRESCIMOMULTAATRASO      Double, " & vbCrLf
                lstrsql = lstrsql & "JUROSATRASO               Double, " & vbCrLf
                lstrsql = lstrsql & "CORRECAOMONETARIA         Double, " & vbCrLf
                lstrsql = lstrsql & "SERVICOPRESTADOS          Double, " & vbCrLf
                lstrsql = lstrsql & "DEVOLUCAOAMAIOR           Double, " & vbCrLf
                lstrsql = lstrsql & "DEVOLUCAOCONSUMO          Double, " & vbCrLf
                lstrsql = lstrsql & "DEVOLUCAOCOBRANCAAMAIOR   Double, " & vbCrLf
                lstrsql = lstrsql & "DEVOLUCAOCOBRANCA2VIA              Double, " & vbCrLf
                lstrsql = lstrsql & "DEVOLUCAOAFERICAOMEDIDOR           Double, " & vbCrLf
                lstrsql = lstrsql & "DESCONTOIRRIGANTEAQUICULTOR        Double, " & vbCrLf
                lstrsql = lstrsql & "DEVOLUCAOCORRECAO                  Double, " & vbCrLf
                lstrsql = lstrsql & "RESSARCIMENTODMIC                  Double, " & vbCrLf
                lstrsql = lstrsql & "RESSARCIMENTODICFIC                Double, " & vbCrLf
                lstrsql = lstrsql & "RESSARCIMENTONIVELTENSAO                 Double, " & vbCrLf
                lstrsql = lstrsql & "RESSARCIMENTOVIOLACAOPRAZO               Double, " & vbCrLf
                lstrsql = lstrsql & "BONUITAIPU                               Double, " & vbCrLf
                lstrsql = lstrsql & "BONUITAIPUDEVOLVER                       Double, " & vbCrLf
                lstrsql = lstrsql & "MICRORESIDENCIAL                         Double, " & vbCrLf
                lstrsql = lstrsql & "MICROINDUSTRIAL                          Double, " & vbCrLf
                lstrsql = lstrsql & "MICROCOMERCIAL                           Double, " & vbCrLf
                lstrsql = lstrsql & "MICRORURAL                               Double, " & vbCrLf
                lstrsql = lstrsql & "MICROPODERPUBLICO                        Double, " & vbCrLf
                lstrsql = lstrsql & "RECEITASISTEMADISTRIBUICAO               Double, " & vbCrLf
                lstrsql = lstrsql & "RECEITASISTEMADISTRIBUICAOTAXA           Double, " & vbCrLf
                lstrsql = lstrsql & "TUSDMUST                                 Double, " & vbCrLf
                lstrsql = lstrsql & "RECEITABANDEIRA                          Double, " & vbCrLf
                lstrsql = lstrsql & "DIFERENCA                                Double, " & vbCrLf
                lstrsql = lstrsql & "ICMSRESIDENCIAL                               Double, " & vbCrLf
                lstrsql = lstrsql & "ICMSINDUSTRIAL                                Double, " & vbCrLf
                lstrsql = lstrsql & "ICMSCOMERCIAL                                 Double, " & vbCrLf
                lstrsql = lstrsql & "ICMSRURAL                                     Double, " & vbCrLf
                lstrsql = lstrsql & "ICMSPODERPUBLICO                              Double, " & vbCrLf
                lstrsql = lstrsql & "ICMSILUMINACAOPUBLICA                         Double, " & vbCrLf
                lstrsql = lstrsql & "ICMSSERVICOPUBLICO                            Double, " & vbCrLf
                lstrsql = lstrsql & "ICMSCONSUMIDORLIVRE                           Double, " & vbCrLf
                lstrsql = lstrsql & "ICMSCONSUMIDORPOTENCIALMENTELIVRE             Double, " & vbCrLf
                lstrsql = lstrsql & "ICMSIMPCIRMERSERV                             Double, " & vbCrLf
                lstrsql = lstrsql & "ICMSMESPOSTERIORILUM                          Double, " & vbCrLf
                lstrsql = lstrsql & "ICMSMESPOSTERIORGRUPOA                        Double, " & vbCrLf

                lstrsql = lstrsql & "ICMSCONSUMIDORLIVREST                         Double, " & vbCrLf
                lstrsql = lstrsql & "ICMSCONSUMIDORPOTENCIALMENTELIVREST           Double, " & vbCrLf
                lstrsql = lstrsql & "ICMSIMPCIRMERSERVST                           Double, " & vbCrLf
                lstrsql = lstrsql & "ICMSIMPCIRMERSERVPOSTERIORST                  Double, " & vbCrLf

                lstrsql = lstrsql & "KEY `KEYFLGMEDFAT`  (`FLGMEDFAT`), " & vbCrLf
                lstrsql = lstrsql & "KEY `KEYFATURAFINAL`  (`FATURA_FINAL`),  " & vbCrLf
                lstrsql = lstrsql & "KEY `KEYFLGST`  (`FLGST`), " & vbCrLf
                lstrsql = lstrsql & "KEY `KEYCLASSE`  (`CLASSE`),  " & vbCrLf
                lstrsql = lstrsql & "KEY `KEYKHWCONS` (`QKWH_CONS`))" & vbCrLf
                objExecAtualizaBancoDados.QUERY_NAME = lstrsql
                repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)


                objExecAtualizaBancoDados.QUERY_NAME = "drop table if exists temp_estatis_itens"
                repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

                lstrsql = ""
                lstrsql = lstrsql & " CREATE  TABLE temp_estatis_itens(CODIGO VARCHAR(50), " & vbCrLf
                lstrsql = lstrsql & "NOME   VARCHAR(100)," & vbCrLf
                lstrsql = lstrsql & "VALOR  Double, " & vbCrLf
                lstrsql = lstrsql & "GRUPO  Char(2)," & vbCrLf
                lstrsql = lstrsql & "ZONA   CHAR(3), " & vbCrLf
                lstrsql = lstrsql & "KEY `KEYCOD` (`CODIGO`))"
                objExecAtualizaBancoDados.QUERY_NAME = lstrsql
                repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

                lblnRet = True

            Else
                lblnRet = True
            End If


            If lblnRet Then

                'PREPARA CONTROLE DE INSERCAO TEMPORARIO NO MYSQL

                If aboolCriarSqlIte = True Then
                    astrSqlIte = "INSERT INTO " & istrTabTempItens & " (CODIGO, NOME, VALOR, ZONA, GRUPO) VALUES "
                End If

                If aboolCriarSqlPri = True Then

                    astrSqlPri = "INSERT INTO " & istrTabTemp & " (CLASSE, "
                    'CONVENCIONAL
                    'QTDE
                    astrSqlPri = astrSqlPri & "QKWH_CONS, QKWH_FAT, QKWH_FAT_BAN, QKW_CONTR, QKW_FAT, "
                    'VALOR
                    astrSqlPri = astrSqlPri & "VKWH_CONS, VKWH_FAT, VKWH_FAT_BAN, VKW_FAT, VCONREAT, VDEMREAT, "
                    'HOROSAZONAL
                    'QTDE
                    astrSqlPri = astrSqlPri & "QKWH_FAT_P,QKWH_FAT_I, QKWH_FAT_FP, QKWH_FAT_P_BAN, QKWH_FAT_I_BAN, QKWH_FAT_FP_BAN, QKWH_FAT_P_LIV, QKWH_FAT_FP_LIV, "
                    astrSqlPri = astrSqlPri & "QKWH_DEV_P, QKWH_DEV_FP, QKWH_UTR_P, QKWH_UTR_FP, QKW_CONTR_P, QKW_CONTR_FP, "
                    astrSqlPri = astrSqlPri & "QKW_FAT_P, QKW_FAT_FP, QKW_DIF_FAT_P, QKW_DIF_FAT_FP, QKW_FATU_P, QKW_FATU_FP, "
                    'VALOR
                    astrSqlPri = astrSqlPri & "VKWH_FAT_P, VKWH_FAT_I, VKWH_FAT_FP, VKWH_FAT_P_BAN, VKWH_FAT_I_BAN, VKWH_FAT_FP_BAN, VKWH_FAT_P_LIV, VKWH_FAT_FP_LIV, "
                    astrSqlPri = astrSqlPri & "VKWH_DEV_P, VKWH_DEV_FP, VKWH_UTR_P, VKWH_UTR_FP, VKW_FAT_P, VKW_FAT_FP, VKW_DIF_FAT_P, VKW_DIF_FAT_FP, VKW_FATU_P, VKW_FATU_FP, "
                    astrSqlPri = astrSqlPri & "VKW_DSC_PROV, VKW_DSC_REAL, VKW_DEV_CCEE, VKW_COB_CCEE,VKWH_DSC_PROV, VKWH_DSC_REAL, VKWH_DEV_CCEE, VKWH_COB_CCEE, "
                    'REATIVO
                    astrSqlPri = astrSqlPri & "VCONREAT_P, VCONREAT_FP, VDEMREAT_P, VDEMREAT_FP, "
                    'MICRO GERAÇÃO
                    'ASTRSQLPRI = ASTRSQLPRI & "QKWH_REVERSO, VKWH_REVERSO, QKWH_REVERSOP, QKWH_REVERSOFP, VKWH_REVERSOP, VKWH_REVERSOFP, "
                    astrSqlPri = astrSqlPri & "QKWH_REVERSOP, QKWH_REVERSOI, QKWH_REVERSOFP, VKWH_REVERSOP, VKWH_REVERSOI, VKWH_REVERSOFP, QKWH_REVERSOP_SEFAZ, QKWH_REVERSOI_SEFAZ, QKWH_REVERSOFP_SEFAZ, VKWH_REVERSOP_SEFAZ, VKWH_REVERSOI_SEFAZ, VKWH_REVERSOFP_SEFAZ,"
                    'OUTROS
                    astrSqlPri = astrSqlPri & "TIP, SLD_ARRED, ENC_FINAN, ICMS, PIS, COFINS, "
                    'TRIBUTACAO COMERCIALIZADOR USADO PARA O LIVRE
                    astrSqlPri = astrSqlPri & "ICMS_ST, PIS_ST, COFINS_ST, "
                    'BASE DE CALCULO
                    astrSqlPri = astrSqlPri & "BASEICMS, BASEPIS, BASECOFINS, ICMSPISCOF, ISENTOICMS, DIFERIDO, "
                    '
                    astrSqlPri = astrSqlPri & "DIF_ELTRBR, JUROS, ENCARGO, MUST, TOTAL, TAXADOS, DIVERSOS, CONVENIOS, DESCONTOS, DESCONTOS_1, RETENCAO, FLGMEDFAT, FATURA_FINAL, IRRIGACAO_AQUICULTURA, "
                    astrSqlPri = astrSqlPri & "MICRO_GERACAO, MICRO_GERACAO_DISTRIBUIDA, MICRO_GERACAO_CONVENIO_SEFAZ, TARIFA_BRANCA, VKWH_REVERSO_IMPORTE, "
                    astrSqlPri = astrSqlPri & "FLGST, FASE, GRUPO, ZONA, LIVRO,CONTA, NOME, GRUPOT, SEQUENCIA, MESANO, VISTORIA, VISTORIA_BIF, VISTORIA_TRIF, VISTORIA_GRUPOA, VERIFNIVEL,  "
                    astrSqlPri = astrSqlPri & "VERIFNIVEL_BIF, VERIFNIVEL_TRIF, SERVTAXADO, SERVTAXADO_BIF, SERVTAXADO_TRIF, SERVTAXADO_RN, SERVTAXADO_RN_BIF, SERVTAXADO_RN_TRIF, SERVTAXADO_RN_GA, SERVTAXADO_EM2 ,SERVTAXADO_EM2_BIF, "
                    astrSqlPri = astrSqlPri & "SERVTAXADO_EM2_A4, SERVTAXADO_EM2_TRIF,DESLIGPROGRAMADO, DESLIGPROGRAMADO_TRIF, DESLIGPROGRAMADO_GRUPOA4, "
                    astrSqlPri = astrSqlPri & "RELIGPROGRAMADA, RELIGPROGRAMADA_TRIF, RELIGPROGRAMADA_GRUPOA, FORNECIMENTOPULSO_GRUPOA, CUSTOADM, CUSTOADM_GRUPOA, ACRESCIMOMULTAATRASO, JUROSATRASO, "
                    astrSqlPri = astrSqlPri & "CORRECAOMONETARIA, SERVICOPRESTADOS, DEVOLUCAOAMAIOR, DEVOLUCAOCONSUMO ,DEVOLUCAOCOBRANCAAMAIOR, DEVOLUCAOCOBRANCA2VIA, DEVOLUCAOAFERICAOMEDIDOR,  "
                    astrSqlPri = astrSqlPri & "DESCONTOIRRIGANTEAQUICULTOR, DEVOLUCAOCORRECAO, RESSARCIMENTODMIC, RESSARCIMENTODICFIC, RESSARCIMENTONIVELTENSAO, RESSARCIMENTOVIOLACAOPRAZO, BONUITAIPU, BONUITAIPUDEVOLVER,  "
                    astrSqlPri = astrSqlPri & "MICRORESIDENCIAL, MICROINDUSTRIAL, MICROCOMERCIAL, MICRORURAL, MICROPODERPUBLICO, RECEITASISTEMADISTRIBUICAO, RECEITASISTEMADISTRIBUICAOTAXA, TUSDMUST, RECEITABANDEIRA, DIFERENCA, "
                    astrSqlPri = astrSqlPri & "ICMSRESIDENCIAL, ICMSINDUSTRIAL, ICMSCOMERCIAL, ICMSRURAL, ICMSPODERPUBLICO, ICMSILUMINACAOPUBLICA, ICMSSERVICOPUBLICO, ICMSCONSUMIDORLIVRE, ICMSCONSUMIDORPOTENCIALMENTELIVRE, ICMSIMPCIRMERSERV, "
                    astrSqlPri = astrSqlPri & "ICMSMESPOSTERIORILUM, ICMSMESPOSTERIORGRUPOA, ICMSCONSUMIDORLIVREST ,ICMSCONSUMIDORPOTENCIALMENTELIVREST,ICMSIMPCIRMERSERVST,ICMSIMPCIRMERSERVPOSTERIORST  )  VALUES "


                End If

            End If

            CriaTabelas = lblnRet

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: CriaTabelas" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function
        End Try

    End Function
    Private Function GetFiltroPrefixo() As String

        Dim lblnSelecionou As Boolean
        Dim llngMenor As Integer
        Dim llngMaior As Integer
        Dim llngAux As Integer
        Dim lstrRet As String
        Dim llngIdx As Integer

        Try

            lstrRet = ""
            llngMenor = 99
            llngMaior = 0
            lblnSelecionou = False
            'With Me.lsvRotas
            '    For llngIdx = 1 To .Items.Count
            '        If .Items.Item(llngIdx - 1).Checked = True Then
            '            lblnSelecionou = True
            '            llngAux = CInt(.Items.Item(llngIdx - 1).Text)
            '            If llngAux <= llngMenor Then
            '                llngMenor = llngAux
            '            End If
            '            If llngAux >= llngMaior Then
            '                llngMaior = llngAux
            '            End If
            '        End If
            '    Next llngIdx
            'End With
            If lblnSelecionou Then
                lstrRet = VB6.Format(llngMenor, "00") & "-" & VB6.Format(llngMaior, "00")
            Else
                lstrRet = ""
            End If
            GetFiltroPrefixo = lstrRet

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: GetFiltroPrefixo" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function
        End Try

    End Function
    Private Sub Imprime(ByRef row As DataRow, ByVal tit As String, ByVal astrGrupo As String, Optional ByVal astrFase As String = "", Optional ByVal astrTipo As String = "", Optional ByVal astrGrpImp As String = "", Optional ByVal astrCabecTitulo As String = "", Optional ByVal astrCabecGrupo As String = "", Optional ByVal sTipo As String = "", Optional ByVal titCredito As String = "", Optional ByVal sContasDeb As String = "", Optional ByVal sContasCred As String = "")

        Dim strTamCol As String
        Dim strTemp As String
        Dim ldblNCons As Double
        Dim ldblQKwhCons As Double
        Dim ldblQKwhFat As Double
        Dim ldblQKwhFatBan As Double
        Dim ldblQKwContr As Double
        Dim ldblQKwFat As Double
        Dim ldblQKwDifFat As Double
        Dim ldblVKwhCons As Double
        Dim ldblVKwhFat As Double
        Dim ldblVKwhFatBan As Double
        Dim ldblVKwFat As Double
        Dim ldblVConReat As Double
        Dim ldblVDemReat As Double
        Dim ldblQKwhFatp As Double
        Dim ldblQKwhFati As Double
        Dim ldblQKwhFatfp As Double
        Dim ldblQKwhFatpBan As Double
        Dim ldblQKwhFatiBan As Double
        Dim ldblQKwhFatfpBan As Double
        Dim ldblQKwhFatpLiv As Double
        Dim ldblQKwhFatfpLiv As Double
        Dim ldblQKwhDevp As Double
        Dim ldblQKwhDevfp As Double
        Dim ldblQKwhUtrp As Double
        Dim ldblQKwhUtrfp As Double
        Dim ldblQKwContrp As Double
        Dim ldblQKwContrfp As Double
        Dim ldblQKwFatp As Double
        Dim ldblQKwFatfp As Double
        Dim ldblQKwDifFatp As Double
        Dim ldblQKwDifFatfp As Double
        Dim ldblQKwFatUp As Double
        Dim ldblQKwFatUfp As Double
        Dim ldblVKwhFatp As Double
        Dim ldblVKwhFati As Double
        Dim ldblVKwhFatfp As Double
        Dim ldblVKwhFatpBan As Double
        Dim ldblVKwhFatiBan As Double
        Dim ldblVKwhFatfpBan As Double
        Dim ldblVKwhFatpLiv As Double
        Dim ldblVKwhFatfpLiv As Double
        Dim ldblVKwhDevp As Double
        Dim ldblVKwhDevfp As Double
        Dim ldblVKwhUtrp As Double
        Dim ldblVKwhUtrfp As Double
        Dim ldblVKwFatp As Double
        Dim ldblVKwFatfp As Double
        Dim ldblVKwDifFatp As Double
        Dim ldblVKwDifFatfp As Double
        Dim ldblVKwFatUp As Double
        Dim ldblVKwFatUfp As Double
        '
        Dim ldblVkwDscProv As Double
        Dim ldblVkwDscReal As Double
        Dim ldblVkwDevCcee As Double
        Dim ldblVkwCobCcee As Double
        '
        Dim ldblVkwhDscProv As Double
        Dim ldblVkwhDscReal As Double
        Dim ldblVkwhDevCcee As Double
        Dim ldblVkwhCobCcee As Double
        '
        Dim ldblVConReatp As Double
        Dim ldblVConReatfp As Double
        Dim ldblVDemReatp As Double
        Dim ldblVDemReatfp As Double
        'Micro geração
        'Dim ldblQKwhReverso As Double
        'Dim ldblVKwhReverso As Double
        Dim ldblQKwhReversop As Double
        Dim ldblQKwhReversoi As Double
        Dim ldblQKwhReversofp As Double
        Dim ldblQKwhReversotot As Double
        Dim ldblVKwhReversop As Double
        Dim ldblVKwhReversoi As Double
        Dim ldblVKwhReversofp As Double
        Dim ldblVKwhReversotot As Double
        '
        Dim ldblQKwhReversop_sefaz As Double
        Dim ldblQKwhReversoi_sefaz As Double
        Dim ldblQKwhReversofp_sefaz As Double
        Dim ldblQKwhReversotot_sefaz As Double
        Dim ldblVKwhReversop_sefaz As Double
        Dim ldblVKwhReversoi_sefaz As Double
        Dim ldblVKwhReversofp_sefaz As Double
        Dim ldblVKwhReversotot_sefaz As Double
        '
        Dim ldblVkwhReversoImporte As Double
        '
        Dim ldblTip As Double
        Dim ldblSldArred As Double
        Dim ldblEncFinan As Double
        Dim ldblIcms As Double
        Dim ldblPis As Double
        Dim ldblCofins As Double
        Dim ldblIcmsSt As Double
        Dim ldblPisSt As Double
        Dim ldblCofinsSt As Double
        Dim ldblBaseIcms As Double
        Dim ldblBasePis As Double
        Dim ldblBaseCofins As Double
        Dim ldblIcmsPisCof As Double
        Dim ldblIsentoIcms As Double
        Dim ldblDiferido As Double
        Dim ldblDifEltrbr As Double
        Dim ldblJuros As Double
        Dim ldblMulta As Double
        Dim ldblEncargo As Double
        Dim ldblMust As Double
        Dim ldblTotal As Double
        Dim ldblTaxados As Double
        Dim ldblDiversos As Double
        Dim ldblConvenios As Double
        Dim ldblDescontos As Double
        Dim ldblDescontos_1 As Double
        Dim ldblRetencao As Double

        Dim ldblVistoria As Double
        Dim ldblVistoria_bif As Double
        Dim ldblVistoria_trif As Double
        Dim ldblSERVTAXADOS_RN As Double
        Dim ldblVISTORIA_GRUPOA As Double
        Dim ldblSERVTAXADOS As Double
        Dim ldblSERVTAXADOS_TRIF As Double
        Dim ldblSERVTAXADO_RN_BIF As Double
        Dim ldblSERVTAXADO_RN_TRIF As Double
        Dim ldblSERVTAXADO_RN_GA As Double
        Dim ldblVERIFNIVEL As Double
        Dim ldblVERIFNIVEL_BIF As Double
        Dim ldblVERIFNIVEL_TRIF As Double
        Dim ldblSERVTAXADO As Double
        Dim ldblSERVTAXADO_BIF As Double
        Dim ldblSERVTAXADO_TRIF As Double
        Dim ldblSERVTAXADO_GA As Double
        Dim ldblSERVTAXADO_EM2 As Double
        Dim ldblSERVTAXADO_EM2_BIF As Double
        Dim ldblSERVTAXADO_EM2_TRIF As Double
        Dim ldblSERVTAXADO_EM2_A4 As Double
        Dim ldblDESLIGPROGRAMADO As Double
        Dim ldblDESLIGPROGRAMADO_TRIF As Double
        Dim ldblDESLIGPROGRAMADO_GRUPOA As Double
        Dim ldblRELIGPROGRAMADA As Double
        Dim ldblRELIGPROGRAMADA_TRIF As Double
        Dim ldblRELIGPROGRAMADA_GRUPOA As Double
        Dim ldblFORNECIMENTOPULSO_GRUPOA As Double
        Dim ldblCUSTOADM As Double
        Dim ldblCUSTOADM_GRUPOA As Double

        Dim ldblACRESCIMOMULTAATRASO As Double

        Dim ldblDIFERENCA As Double

        '
        Dim ldblReceitaCompanhia As Double
        Dim ldblImporte As Double
        Dim ldblValorFinal As Double

        Dim bytBLOBData() As Byte

        Dim vlist

        Dim objEmpresa As New entidadeEmpresa
        Dim repositorioEmpresa As New repositorioEmpresa

        Try

            If sTipo = "" Then

                ldblNCons = CDbl(IIf(IsDBNull(row("ncons")), 0, row("ncons")))
                ldblQKwhCons = CDbl(IIf(IsDBNull(row("qkwh_cons")), 0, row("qkwh_cons")))
                ldblQKwhFat = CDbl(IIf(IsDBNull(row("qkwh_fat")), 0, row("qkwh_fat")))
                ldblQKwhFatBan = CDbl(IIf(IsDBNull(row("qkwh_fat_ban")), 0, row("qkwh_fat_ban")))
                'ldblQkwContr = Conversion.CDbl(IIf(IsNull(row("qkw_contr")), 0, row("qkw_contr")))
                ldblQKwFat = CDbl(IIf(IsDBNull(row("qkw_fat")), 0, row("qkw_fat")))
                ldblVKwhCons = CDbl(IIf(IsDBNull(row("vkwh_cons")), 0, row("vkwh_cons")))
                ldblVKwhFat = CDbl(IIf(IsDBNull(row("vkwh_fat")), 0, row("vkwh_fat")))
                ldblVKwhFatBan = CDbl(IIf(IsDBNull(row("vkwh_fat_ban")), 0, row("vkwh_fat_ban")))
                ldblVKwFat = CDbl(IIf(IsDBNull(row("vkw_fat")), 0, row("vkw_fat")))
                ldblVConReat = CDbl(IIf(IsDBNull(row("vconreat")), 0, row("vconreat")))
                ldblVDemReat = CDbl(IIf(IsDBNull(row("vdemreat")), 0, row("vdemreat")))
                ldblQKwhFatp = CDbl(IIf(IsDBNull(row("qkwh_fat_p")), 0, row("qkwh_fat_p")))
                ldblQKwhFati = CDbl(IIf(IsDBNull(row("qkwh_fat_i")), 0, row("qkwh_fat_i")))
                ldblQKwhFatfp = CDbl(IIf(IsDBNull(row("qkwh_fat_fp")), 0, row("qkwh_fat_fp")))
                ldblQKwhFatpBan = CDbl(IIf(IsDBNull(row("qkwh_fat_p_ban")), 0, row("qkwh_fat_p_ban")))
                ldblQKwhFatiBan = CDbl(IIf(IsDBNull(row("qkwh_fat_i_ban")), 0, row("qkwh_fat_i_ban")))
                ldblQKwhFatfpBan = CDbl(IIf(IsDBNull(row("qkwh_fat_fp_ban")), 0, row("qkwh_fat_fp_ban")))
                ldblQKwhFatpLiv = CDbl(IIf(IsDBNull(row("qkwh_fat_p_liv")), 0, row("qkwh_fat_p_liv")))
                ldblQKwhFatfpLiv = CDbl(IIf(IsDBNull(row("qkwh_fat_fp_liv")), 0, row("qkwh_fat_fp_liv")))
                ldblQKwhDevp = CDbl(IIf(IsDBNull(row("qkwh_dev_p")), 0, row("qkwh_dev_p")))
                ldblQKwhDevfp = CDbl(IIf(IsDBNull(row("qkwh_dev_fp")), 0, row("qkwh_dev_fp")))
                ldblQKwhUtrp = CDbl(IIf(IsDBNull(row("qkwh_utr_p")), 0, row("qkwh_utr_p")))
                ldblQKwhUtrfp = CDbl(IIf(IsDBNull(row("qkwh_utr_fp")), 0, row("qkwh_utr_fp")))
                'ldblQkwContrp = Conversion.CDbl(IIf(IsNull(row("qkw_contr_p")), 0, row("qkw_contr_p")))
                'ldblQkwContrfp = Conversion.CDbl(IIf(IsNull(row("qkw_contr_fp")), 0, row("qkw_contr_fp")))
                ldblQKwFatp = CDbl(IIf(IsDBNull(row("qkw_fat_p")), 0, row("qkw_fat_p")))
                ldblQKwFatfp = CDbl(IIf(IsDBNull(row("qkw_fat_fp")), 0, row("qkw_fat_fp")))
                ' Arlindo dif. demanda
                ldblQKwDifFatp = CDbl(IIf(IsDBNull(row("qkw_dif_fat_p")), 0, row("qkw_dif_fat_p")))
                ldblQKwDifFatfp = CDbl(IIf(IsDBNull(row("qkw_dif_fat_fp")), 0, row("qkw_dif_fat_fp")))
                ldblQKwFatUp = CDbl(IIf(IsDBNull(row("qkw_fatu_p")), 0, row("qkw_fatu_p")))
                ldblQKwFatUfp = CDbl(IIf(IsDBNull(row("qkw_fatu_fp")), 0, row("qkw_fatu_fp")))
                ldblVKwhFatp = CDbl(IIf(IsDBNull(row("vkwh_fat_p")), 0, row("vkwh_fat_p")))
                ldblVKwhFati = CDbl(IIf(IsDBNull(row("vkwh_fat_i")), 0, row("vkwh_fat_i")))
                ldblVKwhFatfp = CDbl(IIf(IsDBNull(row("vkwh_fat_fp")), 0, row("vkwh_fat_fp")))
                ldblVKwhFatpBan = CDbl(IIf(IsDBNull(row("vkwh_fat_p_ban")), 0, row("vkwh_fat_p_ban")))
                ldblVKwhFatiBan = CDbl(IIf(IsDBNull(row("vkwh_fat_i_ban")), 0, row("vkwh_fat_i_ban")))
                ldblVKwhFatfpBan = CDbl(IIf(IsDBNull(row("vkwh_fat_fp_ban")), 0, row("vkwh_fat_fp_ban")))
                ldblVKwhFatpLiv = CDbl(IIf(IsDBNull(row("vkwh_fat_p_liv")), 0, row("vkwh_fat_p_liv")))
                ldblVKwhFatfpLiv = CDbl(IIf(IsDBNull(row("vkwh_fat_fp_liv")), 0, row("vkwh_fat_fp_liv")))
                ldblVKwhDevp = CDbl(IIf(IsDBNull(row("vkwh_dev_p")), 0, row("vkwh_dev_p")))
                ldblVKwhDevfp = CDbl(IIf(IsDBNull(row("vkwh_dev_fp")), 0, row("vkwh_dev_fp")))
                ldblVKwhUtrp = CDbl(IIf(IsDBNull(row("vkwh_utr_p")), 0, row("vkwh_utr_p")))
                ldblVKwhUtrfp = CDbl(IIf(IsDBNull(row("vkwh_utr_fp")), 0, row("vkwh_utr_fp")))
                ldblVKwFatp = CDbl(IIf(IsDBNull(row("vkw_fat_p")), 0, row("vkw_fat_p")))
                ldblVKwFatfp = CDbl(IIf(IsDBNull(row("vkw_fat_fp")), 0, row("vkw_fat_fp")))
                ' Arlindo dif. demanda
                ldblVKwDifFatp = CDbl(IIf(IsDBNull(row("vkw_dif_fat_p")), 0, row("vkw_dif_fat_p")))
                ldblVKwDifFatfp = CDbl(IIf(IsDBNull(row("vkw_dif_fat_fp")), 0, row("vkw_dif_fat_fp")))
                ldblVKwFatUp = CDbl(IIf(IsDBNull(row("vkw_fatu_p")), 0, row("vkw_fatu_p")))
                ldblVKwFatUfp = CDbl(IIf(IsDBNull(row("vkw_fatu_fp")), 0, row("vkw_fatu_fp")))
                ldblVkwDscProv = CDbl(IIf(IsDBNull(row("vkw_dsc_prov")), 0, row("vkw_dsc_prov")))
                ldblVkwDscReal = CDbl(IIf(IsDBNull(row("vkw_dsc_real")), 0, row("vkw_dsc_real")))
                ldblVkwDevCcee = CDbl(IIf(IsDBNull(row("vkw_dev_ccee")), 0, row("vkw_dev_ccee")))
                ldblVkwCobCcee = CDbl(IIf(IsDBNull(row("vkw_cob_ccee")), 0, row("vkw_cob_ccee")))
                ldblVkwhDscProv = CDbl(IIf(IsDBNull(row("vkwh_dsc_prov")), 0, row("vkwh_dsc_prov")))
                ldblVkwhDscReal = CDbl(IIf(IsDBNull(row("vkwh_dsc_real")), 0, row("vkwh_dsc_real")))
                ldblVkwhDevCcee = CDbl(IIf(IsDBNull(row("vkwh_dev_ccee")), 0, row("vkwh_dev_ccee")))
                ldblVkwhCobCcee = CDbl(IIf(IsDBNull(row("vkwh_cob_ccee")), 0, row("vkwh_cob_ccee")))
                ldblVConReatp = CDbl(IIf(IsDBNull(row("vconreat_p")), 0, row("vconreat_p")))
                ldblVConReatfp = CDbl(IIf(IsDBNull(row("vconreat_fp")), 0, row("vconreat_fp")))
                ldblVDemReatp = CDbl(IIf(IsDBNull(row("vdemreat_p")), 0, row("vdemreat_p")))
                ldblVDemReatfp = CDbl(IIf(IsDBNull(row("vdemreat_fp")), 0, row("vdemreat_fp")))
                'ldblQKwhReverso = Conversion.CDbl(IIf(IsNull(row("qkwh_reverso")), 0, row("qkwh_reverso")))
                'ldblVKwhReverso = Conversion.CDbl(IIf(IsNull(row("vkwh_reverso")), 0, row("vkwh_reverso")))
                ldblQKwhReversop = CDbl(IIf(IsDBNull(row("qkwh_reversop")), 0, row("qkwh_reversop")))
                ldblQKwhReversoi = CDbl(IIf(IsDBNull(row("qkwh_reversoi")), 0, row("qkwh_reversoi")))
                ldblQKwhReversofp = CDbl(IIf(IsDBNull(row("qkwh_reversofp")), 0, row("qkwh_reversofp")))
                ldblVKwhReversop = CDbl(IIf(IsDBNull(row("vkwh_reversop")), 0, row("vkwh_reversop")))
                ldblVKwhReversoi = CDbl(IIf(IsDBNull(row("vkwh_reversoi")), 0, row("vkwh_reversoi")))
                ldblVKwhReversofp = CDbl(IIf(IsDBNull(row("vkwh_reversofp")), 0, row("vkwh_reversofp")))
                ldblQKwhReversop_sefaz = CDbl(IIf(IsDBNull(row("qkwh_reversop_sefaz")), 0, row("qkwh_reversop_sefaz")))
                ldblQKwhReversoi_sefaz = CDbl(IIf(IsDBNull(row("qkwh_reversoi_sefaz")), 0, row("qkwh_reversoi_sefaz")))
                ldblQKwhReversofp_sefaz = CDbl(IIf(IsDBNull(row("qkwh_reversofp_sefaz")), 0, row("qkwh_reversofp_sefaz")))
                ldblVKwhReversop_sefaz = CDbl(IIf(IsDBNull(row("vkwh_reversop_sefaz")), 0, row("vkwh_reversop_sefaz")))
                ldblVKwhReversoi_sefaz = CDbl(IIf(IsDBNull(row("vkwh_reversoi_sefaz")), 0, row("vkwh_reversoi_sefaz")))
                ldblVKwhReversofp_sefaz = CDbl(IIf(IsDBNull(row("vkwh_reversofp_sefaz")), 0, row("vkwh_reversofp_sefaz")))
                ldblTip = CDbl(IIf(IsDBNull(row("tip")), 0, row("tip")))
                ldblSldArred = CDbl(IIf(IsDBNull(row("sld_arred")), 0, row("sld_arred")))
                ldblEncFinan = CDbl(IIf(IsDBNull(row("enc_finan")), 0, row("enc_finan")))
                ldblIcms = CDbl(IIf(IsDBNull(row("icms")), 0, row("icms")))
                ldblPis = CDbl(IIf(IsDBNull(row("pis")), 0, row("pis")))
                ldblCofins = CDbl(IIf(IsDBNull(row("cofins")), 0, row("cofins")))
                ldblIcmsSt = CDbl(IIf(IsDBNull(row("icms_st")), 0, row("icms_st")))
                ldblPisSt = CDbl(IIf(IsDBNull(row("pis_st")), 0, row("pis_st")))
                ldblCofinsSt = CDbl(IIf(IsDBNull(row("cofins_st")), 0, row("cofins_st")))
                ldblBaseIcms = CDbl(IIf(IsDBNull(row("baseicms")), 0, row("baseicms")))
                ldblBasePis = CDbl(IIf(IsDBNull(row("basepis")), 0, row("basepis")))
                ldblBaseCofins = CDbl(IIf(IsDBNull(row("basecofins")), 0, row("basecofins")))
                ldblIcmsPisCof = CDbl(IIf(IsDBNull(row("icmspiscof")), 0, row("icmspiscof")))
                ldblIsentoIcms = CDbl(IIf(IsDBNull(row("isentoicms")), 0, row("isentoicms")))
                ldblDiferido = CDbl(IIf(IsDBNull(row("diferido")), 0, row("diferido")))
                ldblDifEltrbr = CDbl(IIf(IsDBNull(row("dif_eltrbr")), 0, row("dif_eltrbr")))
                ldblJuros = CDbl(IIf(IsDBNull(row("juros")), 0, row("juros")))
                'ldblMulta = Conversion.CDbl(IIf(IsNull(row("multa")), 0, row("multa")))
                ldblEncargo = CDbl(IIf(IsDBNull(row("encargo")), 0, row("encargo")))
                ldblMust = CDbl(IIf(IsDBNull(row("must")), 0, row("must")))
                ldblTotal = CDbl(IIf(IsDBNull(row("total")), 0, row("total")))
                ldblTaxados = CDbl(IIf(IsDBNull(row("taxados")), 0, row("taxados")))
                ldblDiversos = CDbl(IIf(IsDBNull(row("diversos")), 0, row("diversos")))
                ldblConvenios = CDbl(IIf(IsDBNull(row("convenios")), 0, row("convenios")))
                ldblDescontos = CDbl(IIf(IsDBNull(row("descontos")), 0, row("descontos")))
                ldblDescontos_1 = CDbl(IIf(IsDBNull(row("descontos_1")), 0, row("descontos_1")))
                ldblRetencao = CDbl(IIf(IsDBNull(row("retencao")), 0, row("retencao")))
                ldblVkwhReversoImporte = CDbl(IIf(IsDBNull(row("vkwh_reverso_importe")), 0, row("vkwh_reverso_importe")))
                'ldblQKwhReversotot = Arredondar(ldblQKwhReverso,2) + Arredondar(ldblQKwhReversop,2) + Arredondar(ldblQKwhReversofp,2)
                'ldblVKwhReversotot = Arredondar(ldblVKwhReverso,2) + Arredondar(ldblVKwhReversop,2) + Arredondar(ldblVKwhReversofp,2)
                ldblQKwhReversotot = Arredondar(ldblQKwhReversop, 2) + Arredondar(ldblQKwhReversoi, 2) + Arredondar(ldblQKwhReversofp, 2)
                ldblVKwhReversotot = Arredondar(ldblVKwhReversop, 2) + Arredondar(ldblVKwhReversoi, 2) + Arredondar(ldblVKwhReversofp, 2)
                ldblQKwhReversotot_sefaz = Arredondar(ldblQKwhReversop_sefaz, 2) + Arredondar(ldblQKwhReversoi_sefaz, 2) + Arredondar(ldblQKwhReversofp_sefaz, 2)
                ldblVKwhReversotot_sefaz = Arredondar(ldblVKwhReversop_sefaz, 2) + Arredondar(ldblVKwhReversoi_sefaz, 2) + Arredondar(ldblVKwhReversofp_sefaz, 2)


                ldblVistoria = CDbl(IIf(IsDBNull(row("vistoria")), 0, row("vistoria")))
                ldblVistoria_bif = CDbl(IIf(IsDBNull(row("vistoria_bif")), 0, row("vistoria_bif")))
                ldblVistoria_trif = CDbl(IIf(IsDBNull(row("vistoria_trif")), 0, row("vistoria_trif")))
                ldblVISTORIA_GRUPOA = CDbl(IIf(IsDBNull(row("VISTORIA_GRUPOA")), 0, row("VISTORIA_GRUPOA")))
                ldblSERVTAXADOS_RN = CDbl(IIf(IsDBNull(row("SERVTAXADO_RN")), 0, row("SERVTAXADO_RN")))
                'ldblSERVTAXADOS_TRIF = CDbl(IIf(IsDBNull(row("SERVTAXADOS_TRIF")), 0, row("SERVTAXADOS_TRIF")))
                ldblSERVTAXADO_RN_BIF = CDbl(IIf(IsDBNull(row("SERVTAXADO_RN_BIF")), 0, row("SERVTAXADO_RN_BIF")))
                ldblSERVTAXADO_RN_TRIF = CDbl(IIf(IsDBNull(row("SERVTAXADO_RN_TRIF")), 0, row("SERVTAXADO_RN_TRIF")))
                ldblSERVTAXADO_RN_GA = CDbl(IIf(IsDBNull(row("SERVTAXADO_RN_GA")), 0, row("SERVTAXADO_RN_GA")))
                ldblVERIFNIVEL = CDbl(IIf(IsDBNull(row("VERIFNIVEL")), 0, row("VERIFNIVEL")))
                ldblVERIFNIVEL_BIF = CDbl(IIf(IsDBNull(row("VERIFNIVEL_BIF")), 0, row("VERIFNIVEL_BIF")))
                ldblVERIFNIVEL_TRIF = CDbl(IIf(IsDBNull(row("VERIFNIVEL_TRIF")), 0, row("VERIFNIVEL_TRIF")))
                ldblSERVTAXADO = CDbl(IIf(IsDBNull(row("SERVTAXADO")), 0, row("SERVTAXADO")))
                ldblSERVTAXADO_BIF = CDbl(IIf(IsDBNull(row("SERVTAXADO_BIF")), 0, row("SERVTAXADO_BIF")))
                ldblSERVTAXADO_TRIF = CDbl(IIf(IsDBNull(row("SERVTAXADO_TRIF")), 0, row("SERVTAXADO_TRIF")))
                ldblSERVTAXADO_EM2 = CDbl(IIf(IsDBNull(row("SERVTAXADO_EM2")), 0, row("SERVTAXADO_EM2")))
                ldblSERVTAXADO_EM2_A4 = CDbl(IIf(IsDBNull(row("SERVTAXADO_EM2_A4")), 0, row("SERVTAXADO_EM2_A4")))
                ldblSERVTAXADO_EM2_BIF = CDbl(IIf(IsDBNull(row("SERVTAXADO_EM2_BIF")), 0, row("SERVTAXADO_EM2_BIF")))
                ldblSERVTAXADO_EM2_TRIF = CDbl(IIf(IsDBNull(row("SERVTAXADO_EM2_TRIF")), 0, row("SERVTAXADO_EM2_TRIF")))
                ldblDESLIGPROGRAMADO = CDbl(IIf(IsDBNull(row("DESLIGPROGRAMADO")), 0, row("DESLIGPROGRAMADO")))
                ldblDESLIGPROGRAMADO_TRIF = CDbl(IIf(IsDBNull(row("DESLIGPROGRAMADO_TRIF")), 0, row("DESLIGPROGRAMADO_TRIF")))
                ldblDESLIGPROGRAMADO_GRUPOA = CDbl(IIf(IsDBNull(row("DESLIGPROGRAMADO_GRUPOA4")), 0, row("DESLIGPROGRAMADO_GRUPOA4")))
                ldblRELIGPROGRAMADA = CDbl(IIf(IsDBNull(row("RELIGPROGRAMADA")), 0, row("RELIGPROGRAMADA")))
                ldblRELIGPROGRAMADA_TRIF = CDbl(IIf(IsDBNull(row("RELIGPROGRAMADA_TRIF")), 0, row("RELIGPROGRAMADA_TRIF")))
                ldblRELIGPROGRAMADA_GRUPOA = CDbl(IIf(IsDBNull(row("RELIGPROGRAMADA_GRUPOA")), 0, row("RELIGPROGRAMADA_GRUPOA")))
                ldblFORNECIMENTOPULSO_GRUPOA = CDbl(IIf(IsDBNull(row("FORNECIMENTOPULSO_GRUPOA")), 0, row("FORNECIMENTOPULSO_GRUPOA")))
                ldblCUSTOADM = CDbl(IIf(IsDBNull(row("CUSTOADM")), 0, row("CUSTOADM")))
                ldblCUSTOADM_GRUPOA = CDbl(IIf(IsDBNull(row("CUSTOADM_GRUPOA")), 0, row("CUSTOADM_GRUPOA")))
                ldblACRESCIMOMULTAATRASO = CDbl(IIf(IsDBNull(row("ACRESCIMOMULTAATRASO")), 0, row("ACRESCIMOMULTAATRASO")))

                ldblDIFERENCA = CDbl(IIf(IsDBNull(row("DIFERENCA")), 0, row("DIFERENCA")))


                '
                ldblReceitaCompanhia = Arredondar(ldblVKwhFatp, 2) + Arredondar(ldblVKwhFati, 2) + Arredondar(ldblVKwhFatfp, 2) + Arredondar(ldblVKwhFatpBan, 2) + Arredondar(ldblVKwhFatiBan, 2) + Arredondar(ldblVKwhFatfpBan, 2) + Arredondar(ldblVKwhFatpLiv, 2) + Arredondar(ldblVKwhFatfpLiv, 2) + Arredondar(ldblVKwhDevp, 2) + Arredondar(ldblVKwhDevfp, 2) + Arredondar(ldblVKwhUtrp, 2) + Arredondar(ldblVKwhUtrfp, 2)
                ldblReceitaCompanhia = Arredondar(ldblReceitaCompanhia, 2) + Arredondar(ldblVKwhFat, 2) + Arredondar(ldblVKwhFatBan, 2) + Arredondar(ldblVKwFatp, 2) + Arredondar(ldblVKwFatfp, 2) + Arredondar(ldblVKwFat, 2) + Arredondar(ldblVKwDifFatp, 2) + Arredondar(ldblVKwDifFatfp, 2) + Arredondar(ldblVKwFatUp, 2) + Arredondar(ldblVKwFatUfp, 2)
                ldblReceitaCompanhia = Arredondar(ldblReceitaCompanhia, 2) + (Arredondar(ldblVConReatp, 2) + Arredondar(ldblVConReatfp, 2) + Arredondar(ldblVDemReatp, 2) + Arredondar(ldblVDemReatfp, 2) + Arredondar(ldblVConReat, 2) + Arredondar(ldblVDemReat, 2))
                'ldblReceitaCompanhia = Arredondar(ldblReceitaCompanhia, 2) - Arredondar(ldblVKwhReversotot,2) - Arredondar(ldblVKwhReversotot_sefaz, 2)
                'ldblReceitaCompanhia = Arredondar(ldblReceitaCompanhia, 2) + ((Arredondar(ldblDiversos,2) + Arredondar(ldblEncFinan, 2) + Arredondar(ldblTaxados, 2)) - Arredondar(ldblDescontos, 2))
                'ldblReceitaCompanhia = Arredondar(ldblReceitaCompanhia, 2) - (Arredondar(ldblVkwDscProv,2) + Arredondar(ldblVkwDscReal, 2) + Arredondar(ldblVkwDevCcee, 2) + Arredondar(ldblVkwhDscProv, 2) + Arredondar(ldblVkwhDscReal , 2) + Arredondar(ldblVkwhDevCcee, 2)) + Arredondar(ldblVkwCobCcee, 2) + Arredondar(ldblVkwhCobCcee, 2)
                '
                'Fazer alguma coisa para o caso da CCEE da portaria para o valor do importe
                'If CLng(Right(Me.txtMesAno.Text, 4) & Left(Me.txtMesAno.Text, 2)) < glngPortaria_Sef_344_2019 Then
                '

                'ldblImporte = ldblVKwhFatp + ldblVKwhFati + ldblVKwhFatfp + ldblVKwhFatpBan + ldblVKwhFatiBan + ldblVKwhFatfpBan + ldblVKwhFatpLiv + ldblVKwhFatfpLiv + ldblVKwhDevp + ldblVKwhDevfp + ldblVKwhUtrp + ldblVKwhUtrfp
                'ldblImporte = ldblImporte + ldblVKwhFat + ldblVKwhFatBan + ldblVKwFatp + ldblVKwFatfp + ldblVKwFat + ldblVKwFatUp + ldblVKwFatUfp
                'ldblImporte = ldblImporte + (ldblVConReatp + ldblVConReatfp + ldblVDemReatp + ldblVDemReatfp + ldblVConReat + ldblVDemReat) + ldblIcms + ldblPis + ldblCofins
                'ldblImporte = ldblImporte - (ldblVkwDscProv + ldblVkwDscReal + ldblVkwDevCcee + ldblVkwhDscProv + ldblVkwhDscReal + ldblVkwhDevCcee) + ldblVkwCobCcee + ldblVkwhCobCcee + ldblVkwhReversoImporte
                ldblImporte = Arredondar(ldblReceitaCompanhia, 2) + Arredondar(ldblIcms, 2) + Arredondar(ldblPis, 2) + Arredondar(ldblCofins, 2) + Arredondar(ldblIcmsSt, 2) + Arredondar(ldblPisSt, 2) + Arredondar(ldblCofinsSt, 2) - (Arredondar(ldblVkwDscProv, 2) + Arredondar(ldblVkwDscReal, 2) + Arredondar(ldblVkwDevCcee, 2) + Arredondar(ldblVkwhDscProv, 2) + Arredondar(ldblVkwhDscReal, 2) + Arredondar(ldblVkwhDevCcee, 2)) + Arredondar(ldblVkwCobCcee, 2) + Arredondar(ldblVkwhCobCcee, 2) - Arredondar(ldblDescontos_1, 2) + Arredondar(ldblVKwhReversotot, 2) + Arredondar(ldblVKwhReversotot_sefaz, 2)
                ldblImporte = ldblImporte + Arredondar(ldblDIFERENCA, 2)
            Else

                ldblValorFinal = CDbl(IIf(IsDBNull(row("valorFinal")), 0, row("valorFinal")))
                ldblValorFinal = Arredondar(ldblValorFinal, 2)
            End If

            'If sContaDeb <> "" And sContaCred <> "" Then

            If sTipo = "" Then
                InsereListNaoExportado("10", "A", Format(dt1.Value, "MM/yyyy"), tit, ldblImporte, sContasDeb, "D", "10")
                InsereListNaoExportado("20", "A", Format(dt1.Value, "MM/yyyy"), tit, ldblImporte, sContasCred, "C", "20")

                Me.AddLancto(sContasDeb, ldblImporte, "D")
                Me.AddLancto(sContasCred, ldblImporte, "C")

            ElseIf sTipo = "B" Then
                InsereListNaoExportado("10", "A", Format(dt1.Value, "MM/yyyy"), tit, ldblValorFinal, sContasDeb, "D", "10")
                InsereListNaoExportado("20", "A", Format(dt1.Value, "MM/yyyy"), IIf(titCredito = "", tit, titCredito), ldblValorFinal, sContasCred, "C", "20")

                Me.AddLancto(sContasDeb, ldblValorFinal, "D")
                Me.AddLancto(sContasCred, ldblValorFinal, "C")

            ElseIf sTipo = "C" Then

                InsereListNaoExportado("20", "A", Format(dt1.Value, "MM/yyyy"), IIf(titCredito = "", tit, titCredito), ldblValorFinal, sContasCred, "C", "20")
                Me.AddLancto(sContasCred, ldblValorFinal, "C")

            ElseIf sTipo = "D" Then

                InsereListNaoExportado("10", "A", Format(dt1.Value, "MM/yyyy"), tit, ldblValorFinal, sContasDeb, "D", "10")
                Me.AddLancto(sContasDeb, ldblValorFinal, "D")

            End If
            cmdProcessar.Enabled = True
            'End If



        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: Imprime" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub sFiltraTabTemporaria_Classes(ByVal astrGrupo As String, Optional ByVal astrZona As String = "", Optional ByVal astrCabecTitulo As String = "", Optional ByVal astrCabecGrupo As String = "")

        Dim lstrSqlP As String
        Dim lstrSql As String
        Dim lstrSql0 As String
        Dim lstrSql1 As String
        Dim lstrSql1V As String
        Dim lstrSql1A As String
        Dim lstrSqlFimAno As String
        Dim lstrSql2 As String
        Dim lstrSql3 As String
        Dim strTexto As String
        Dim lngIni As Integer
        Dim lngFim As Integer
        Dim intLinha As Short
        Dim lintClas As Short
        Dim lintAux As Short
        Dim lintAuxVerde As Short
        Dim lintAuxAzul As Short
        Dim lblnCanBX As Boolean 'se teve cancelada na classe baixa renda
        Dim lblnCanRE As Boolean 'se teve cancelada na classe residencial
        Dim lblnCanGE As Boolean 'se teve cancelada na classe geral
        Dim lblnCanIN As Boolean 'se teve cancelada na classe industrial
        Dim lblnCanLV As Boolean 'se teve cancelada na classe livre
        Dim lblnCanCO As Boolean 'se teve cancelada na classe comercial
        Dim lblnCanRU As Boolean 'se teve cancelada na classe rural
        Dim lblnCanPP As Boolean 'se teve cancelada na poder publico
        Dim lblnCanIP As Boolean 'se teve cancelada na iluminacao publica
        Dim lblnCanSP As Boolean 'se teve cancelada na serviço publico
        Dim lblnCanCP As Boolean 'se teve cancelada na consumo proprio
        Dim lstrAux As String
        Dim lintGrupoVerde As Short
        Dim lintGrupoAzul As Short

        Dim dt As DataTable
        Dim dt_1 As DataTable

        Dim objTempEstatis As New entidadeTempEstatis
        Dim repositorioTempEstatis As New repositorioTempEstatis

        Try

            lstrAux = Me.lblInfo.Text

            lintGrupoVerde = 0
            lintGrupoAzul = 0
            llngNconsFimAno = 0
            lblnCanBX = False
            lblnCanRE = False
            lblnCanGE = False
            lblnCanIN = False
            lblnCanLV = False
            lblnCanCO = False
            lblnCanRU = False
            lblnCanPP = False
            lblnCanIP = False
            lblnCanSP = False
            lblnCanCP = False

            istrGrupoG = astrGrupo

            lstrSql = ""

            'Convencional
            'Consumo
            lstrSql = lstrSql & "       SUM(qkwh_cons) qkwh_cons, "
            lstrSql = lstrSql & "       SUM(vkwh_cons) vkwh_cons, "
            lstrSql = lstrSql & "       SUM(qkwh_fat) qkwh_fat, "
            lstrSql = lstrSql & "       SUM(vkwh_fat) vkwh_fat, "
            lstrSql = lstrSql & "       SUM(qkwh_fat_ban) qkwh_fat_ban, "
            lstrSql = lstrSql & "       SUM(vkwh_fat_ban) vkwh_fat_ban, "

            'Demanda
            lstrSql = lstrSql & "       SUM(qkw_fat) qkw_fat, "
            lstrSql = lstrSql & "       SUM(vkw_fat) vkw_fat, "

            'Reativo
            lstrSql = lstrSql & "       SUM(vconreat) vconreat, "
            lstrSql = lstrSql & "       SUM(vdemreat) vdemreat, "

            'Horosazonal
            'Consumo
            lstrSql = lstrSql & "       SUM(qkwh_fat_p) qkwh_fat_p, "
            lstrSql = lstrSql & "       SUM(vkwh_fat_p) vkwh_fat_p, "
            lstrSql = lstrSql & "       SUM(qkwh_fat_i) qkwh_fat_i, "
            lstrSql = lstrSql & "       SUM(vkwh_fat_i) vkwh_fat_i, "
            lstrSql = lstrSql & "       SUM(qkwh_fat_fp) qkwh_fat_fp, "
            lstrSql = lstrSql & "       SUM(vkwh_fat_fp) vkwh_fat_fp, "
            lstrSql = lstrSql & "       SUM(qkwh_fat_p_ban) qkwh_fat_p_ban, "
            lstrSql = lstrSql & "       SUM(vkwh_fat_p_ban) vkwh_fat_p_ban, "
            lstrSql = lstrSql & "       SUM(qkwh_fat_i_ban) qkwh_fat_i_ban, "
            lstrSql = lstrSql & "       SUM(vkwh_fat_i_ban) vkwh_fat_i_ban, "
            lstrSql = lstrSql & "       SUM(qkwh_fat_fp_ban) qkwh_fat_fp_ban, "
            lstrSql = lstrSql & "       SUM(vkwh_fat_fp_ban) vkwh_fat_fp_ban, "
            lstrSql = lstrSql & "       SUM(qkwh_fat_p_liv) qkwh_fat_p_liv, "
            lstrSql = lstrSql & "       SUM(vkwh_fat_p_liv) vkwh_fat_p_liv, "
            lstrSql = lstrSql & "       SUM(qkwh_fat_fp_liv) qkwh_fat_fp_liv, "
            lstrSql = lstrSql & "       SUM(vkwh_fat_fp_liv) vkwh_fat_fp_liv, "
            lstrSql = lstrSql & "       SUM(qkwh_dev_p) qkwh_dev_p, "
            lstrSql = lstrSql & "       SUM(vkwh_dev_p) vkwh_dev_p, "
            lstrSql = lstrSql & "       SUM(qkwh_dev_fp) qkwh_dev_fp, "
            lstrSql = lstrSql & "       SUM(vkwh_dev_fp) vkwh_dev_fp, "
            lstrSql = lstrSql & "       SUM(qkwh_utr_p) qkwh_utr_p, "
            lstrSql = lstrSql & "       SUM(vkwh_utr_p) vkwh_utr_p, "
            lstrSql = lstrSql & "       SUM(qkwh_utr_fp) qkwh_utr_fp, "
            lstrSql = lstrSql & "       SUM(vkwh_utr_fp) vkwh_utr_fp, "

            'Demanda
            lstrSql = lstrSql & "       SUM(qkw_fat_p) qkw_fat_p, "
            lstrSql = lstrSql & "       SUM(vkw_fat_p) vkw_fat_p, "
            lstrSql = lstrSql & "       SUM(qkw_fat_fp) qkw_fat_fp, "
            lstrSql = lstrSql & "       SUM(vkw_fat_fp) vkw_fat_fp, "

            ' Arlindo dif. demanda
            lstrSql = lstrSql & "     SUM(qkw_dif_fat_p) qkw_dif_fat_p, "
            lstrSql = lstrSql & "     SUM(vkw_dif_fat_p) vkw_dif_fat_p, "
            lstrSql = lstrSql & "     SUM(qkw_dif_fat_fp) qkw_dif_fat_fp, "
            lstrSql = lstrSql & "     SUM(vkw_dif_fat_fp) vkw_dif_fat_fp, "

            '
            lstrSql = lstrSql & "       SUM(qkw_fatu_p) qkw_fatu_p, "
            lstrSql = lstrSql & "       SUM(vkw_fatu_p) vkw_fatu_p, "
            lstrSql = lstrSql & "       SUM(qkw_fatu_fp) qkw_fatu_fp, "
            lstrSql = lstrSql & "       SUM(vkw_fatu_fp) vkw_fatu_fp, "

            'Livres - CCEE
            lstrSql = lstrSql & "       SUM(vkw_dsc_prov) vkw_dsc_prov, "
            lstrSql = lstrSql & "       SUM(vkw_dsc_real) vkw_dsc_real, "
            lstrSql = lstrSql & "       SUM(vkw_dev_ccee) vkw_dev_ccee, "
            lstrSql = lstrSql & "       SUM(vkw_cob_ccee) vkw_cob_ccee, "

            '
            lstrSql = lstrSql & "       SUM(vkwh_dsc_prov) vkwh_dsc_prov, "
            lstrSql = lstrSql & "       SUM(vkwh_dsc_real) vkwh_dsc_real, "
            lstrSql = lstrSql & "       SUM(vkwh_dev_ccee) vkwh_dev_ccee, "
            lstrSql = lstrSql & "       SUM(vkwh_cob_ccee) vkwh_cob_ccee, "

            'Reativos
            lstrSql = lstrSql & "       SUM(vconreat_p) vconreat_p, "
            lstrSql = lstrSql & "       SUM(vconreat_fp) vconreat_fp, "
            lstrSql = lstrSql & "       SUM(vdemreat_p) vdemreat_p, "
            lstrSql = lstrSql & "       SUM(vdemreat_fp) vdemreat_fp, "

            'Micro geração
            'lstrSql = lstrSql & "       SUM(qkwh_reverso) qkwh_reverso, "
            'lstrSql = lstrSql & "       SUM(vkwh_reverso) vkwh_reverso, "
            lstrSql = lstrSql & "       SUM(qkwh_reversop) qkwh_reversop, "
            lstrSql = lstrSql & "       SUM(qkwh_reversoi) qkwh_reversoi, "
            lstrSql = lstrSql & "       SUM(qkwh_reversofp) qkwh_reversofp, "
            lstrSql = lstrSql & "       SUM(vkwh_reversop) vkwh_reversop, "
            lstrSql = lstrSql & "       SUM(vkwh_reversoi) vkwh_reversoi, "
            lstrSql = lstrSql & "       SUM(vkwh_reversofp) vkwh_reversofp, "
            lstrSql = lstrSql & "       SUM(vkwh_reverso_importe) vkwh_reverso_importe, "

            '
            lstrSql = lstrSql & "       SUM(qkwh_reversop_sefaz) qkwh_reversop_sefaz, "
            lstrSql = lstrSql & "       SUM(qkwh_reversoi_sefaz) qkwh_reversoi_sefaz, "
            lstrSql = lstrSql & "       SUM(qkwh_reversofp_sefaz) qkwh_reversofp_sefaz, "
            lstrSql = lstrSql & "       SUM(vkwh_reversop_sefaz) vkwh_reversop_sefaz, "
            lstrSql = lstrSql & "       SUM(vkwh_reversoi_sefaz) vkwh_reversoi_sefaz, "
            lstrSql = lstrSql & "       SUM(vkwh_reversofp_sefaz) vkwh_reversofp_sefaz, "

            'Tributação
            lstrSql = lstrSql & "       SUM(icms) icms, "
            lstrSql = lstrSql & "       SUM(pis) pis, "
            lstrSql = lstrSql & "       SUM(cofins) cofins, "
            lstrSql = lstrSql & "       SUM(baseicms) baseicms, "
            lstrSql = lstrSql & "       SUM(basepis) basepis, "
            lstrSql = lstrSql & "       SUM(basecofins) basecofins, "
            lstrSql = lstrSql & "       SUM(icmspiscof) icmspiscof, "
            lstrSql = lstrSql & "       SUM(isentoicms) isentoicms, "
            lstrSql = lstrSql & "       SUM(diferido) diferido, "

            'Tributação - Substituição tributária
            lstrSql = lstrSql & "       SUM(icms_st) icms_st, "
            lstrSql = lstrSql & "       SUM(pis_st) pis_st, "
            lstrSql = lstrSql & "       SUM(cofins_st) cofins_st, "

            'Outros
            lstrSql = lstrSql & "       SUM(tip) tip, "
            lstrSql = lstrSql & "       SUM(sld_arred) sld_arred, "
            lstrSql = lstrSql & "       SUM(enc_finan) enc_finan, "
            lstrSql = lstrSql & "       SUM(dif_eltrbr) dif_eltrbr, "
            lstrSql = lstrSql & "       SUM(juros) juros, "
            lstrSql = lstrSql & "       SUM(encargo) encargo, "
            lstrSql = lstrSql & "       SUM(must) must, "
            lstrSql = lstrSql & "       SUM(convenios) convenios, "
            lstrSql = lstrSql & "       SUM(descontos) descontos, "
            lstrSql = lstrSql & "       SUM(descontos_1) descontos_1, "
            lstrSql = lstrSql & "       SUM(retencao) retencao, "
            lstrSql = lstrSql & "       SUM(diversos) diversos, "
            lstrSql = lstrSql & "       SUM(taxados) taxados, "
            lstrSql = lstrSql & "       SUM(total) total, "

            lstrSql = lstrSql & "       SUM(VISTORIA) vistoria, "
            lstrSql = lstrSql & "       SUM(VISTORIA_BIF) vistoria_bif, "
            lstrSql = lstrSql & "       SUM(VISTORIA_TRIF) vistoria_trif, "
            lstrSql = lstrSql & "       SUM(VISTORIA_GRUPOA) VISTORIA_GRUPOA, "
            lstrSql = lstrSql & "       SUM(SERVTAXADO) SERVTAXADO, "
            lstrSql = lstrSql & "       SUM(SERVTAXADO_BIF) SERVTAXADO_BIF, "
            lstrSql = lstrSql & "       SUM(SERVTAXADO_TRIF) SERVTAXADO_TRIF, "
            lstrSql = lstrSql & "       SUM(SERVTAXADO_RN) SERVTAXADO_RN, "
            lstrSql = lstrSql & "       SUM(SERVTAXADO_RN_BIF) SERVTAXADO_RN_BIF, "
            lstrSql = lstrSql & "       SUM(SERVTAXADO_RN_TRIF) SERVTAXADO_RN_TRIF, "
            lstrSql = lstrSql & "       SUM(SERVTAXADO_RN_GA) SERVTAXADO_RN_GA, "
            lstrSql = lstrSql & "       SUM(VERIFNIVEL) VERIFNIVEL, "
            lstrSql = lstrSql & "       SUM(VERIFNIVEL_BIF) VERIFNIVEL_BIF, "
            lstrSql = lstrSql & "       SUM(VERIFNIVEL_TRIF) VERIFNIVEL_TRIF, "
            lstrSql = lstrSql & "       SUM(SERVTAXADO_EM2) SERVTAXADO_EM2, "
            lstrSql = lstrSql & "       SUM(SERVTAXADO_EM2_BIF) SERVTAXADO_EM2_BIF, "
            lstrSql = lstrSql & "       SUM(SERVTAXADO_EM2_TRIF) SERVTAXADO_EM2_TRIF, "
            lstrSql = lstrSql & "       SUM(SERVTAXADO_EM2_A4) SERVTAXADO_EM2_A4, "
            lstrSql = lstrSql & "       SUM(DESLIGPROGRAMADO) DESLIGPROGRAMADO, "
            lstrSql = lstrSql & "       SUM(DESLIGPROGRAMADO_TRIF) DESLIGPROGRAMADO_TRIF, "
            lstrSql = lstrSql & "       SUM(DESLIGPROGRAMADO_GRUPOA4) DESLIGPROGRAMADO_GRUPOA4, "
            lstrSql = lstrSql & "       SUM(RELIGPROGRAMADA) RELIGPROGRAMADA, "
            lstrSql = lstrSql & "       SUM(RELIGPROGRAMADA_TRIF) RELIGPROGRAMADA_TRIF, "
            lstrSql = lstrSql & "       SUM(RELIGPROGRAMADA_GRUPOA) RELIGPROGRAMADA_GRUPOA, "
            lstrSql = lstrSql & "       SUM(FORNECIMENTOPULSO_GRUPOA) FORNECIMENTOPULSO_GRUPOA, "
            lstrSql = lstrSql & "       SUM(CUSTOADM) CUSTOADM, "
            lstrSql = lstrSql & "       SUM(CUSTOADM_GRUPOA) CUSTOADM_GRUPOA, "
            lstrSql = lstrSql & "       SUM(ACRESCIMOMULTAATRASO) ACRESCIMOMULTAATRASO, "
            lstrSql = lstrSql & "       SUM(JUROSATRASO) JUROSATRASO, "
            lstrSql = lstrSql & "       SUM(CORRECAOMONETARIA) CORRECAOMONETARIA, "
            lstrSql = lstrSql & "       SUM(SERVICOPRESTADOS) SERVICOPRESTADOS, "
            lstrSql = lstrSql & "       SUM(DEVOLUCAOAMAIOR) DEVOLUCAOAMAIOR, "
            lstrSql = lstrSql & "       SUM(DEVOLUCAOCONSUMO) DEVOLUCAOCONSUMO, "
            lstrSql = lstrSql & "       SUM(DEVOLUCAOCOBRANCAAMAIOR) DEVOLUCAOCOBRANCAAMAIOR, "
            lstrSql = lstrSql & "       SUM(DEVOLUCAOCOBRANCA2VIA) DEVOLUCAOCOBRANCA2VIA, "
            lstrSql = lstrSql & "       SUM(DEVOLUCAOAFERICAOMEDIDOR) DEVOLUCAOAFERICAOMEDIDOR, "
            lstrSql = lstrSql & "       SUM(DESCONTOIRRIGANTEAQUICULTOR) DESCONTOIRRIGANTEAQUICULTOR, "
            lstrSql = lstrSql & "       SUM(DEVOLUCAOCORRECAO) DEVOLUCAOCORRECAO, "
            lstrSql = lstrSql & "       SUM(RESSARCIMENTODMIC) RESSARCIMENTODMIC, "
            lstrSql = lstrSql & "       SUM(RESSARCIMENTODICFIC) RESSARCIMENTODICFIC, "
            lstrSql = lstrSql & "       SUM(RESSARCIMENTONIVELTENSAO) RESSARCIMENTONIVELTENSAO, "
            lstrSql = lstrSql & "       SUM(RESSARCIMENTOVIOLACAOPRAZO) RESSARCIMENTOVIOLACAOPRAZO, "
            lstrSql = lstrSql & "       SUM(BONUITAIPU) BONUITAIPU, "
            lstrSql = lstrSql & "       SUM(BONUITAIPUDEVOLVER) BONUITAIPUDEVOLVER, "
            lstrSql = lstrSql & "       SUM(MICRORESIDENCIAL) MICRORESIDENCIAL, "
            lstrSql = lstrSql & "       SUM(MICROINDUSTRIAL) MICROINDUSTRIAL, "
            lstrSql = lstrSql & "       SUM(MICROCOMERCIAL) MICROCOMERCIAL, "
            lstrSql = lstrSql & "       SUM(MICRORURAL) MICRORURAL, "
            lstrSql = lstrSql & "       SUM(MICROPODERPUBLICO) MICROPODERPUBLICO, "
            lstrSql = lstrSql & "       SUM(RECEITASISTEMADISTRIBUICAO) RECEITASISTEMADISTRIBUICAO, "
            lstrSql = lstrSql & "       SUM(RECEITASISTEMADISTRIBUICAOTAXA) RECEITASISTEMADISTRIBUICAOTAXA, "
            lstrSql = lstrSql & "       SUM(TUSDMUST) TUSDMUST, "
            lstrSql = lstrSql & "       SUM(RECEITABANDEIRA) RECEITABANDEIRA, "
            lstrSql = lstrSql & "       SUM(DIFERENCA) DIFERENCA, "


            lstrSql = lstrSql & "       SUM(ICMSRESIDENCIAL) ICMSRESIDENCIAL, "
            lstrSql = lstrSql & "       SUM(ICMSINDUSTRIAL) ICMSINDUSTRIAL, "
            lstrSql = lstrSql & "       SUM(ICMSCOMERCIAL) ICMSCOMERCIAL, "
            lstrSql = lstrSql & "       SUM(ICMSRURAL) ICMSRURAL, "
            lstrSql = lstrSql & "       SUM(ICMSPODERPUBLICO) ICMSPODERPUBLICO, "
            lstrSql = lstrSql & "       SUM(ICMSILUMINACAOPUBLICA) ICMSILUMINACAOPUBLICA, "
            lstrSql = lstrSql & "       SUM(ICMSSERVICOPUBLICO) ICMSSERVICOPUBLICO, "
            lstrSql = lstrSql & "       SUM(ICMSCONSUMIDORLIVRE) ICMSCONSUMIDORLIVRE, "
            lstrSql = lstrSql & "       SUM(ICMSCONSUMIDORPOTENCIALMENTELIVRE) ICMSCONSUMIDORPOTENCIALMENTELIVRE, "
            lstrSql = lstrSql & "       SUM(ICMSIMPCIRMERSERV) ICMSIMPCIRMERSERV, "
            lstrSql = lstrSql & "       SUM(ICMSMESPOSTERIORILUM) ICMSMESPOSTERIORILUM, "
            lstrSql = lstrSql & "       SUM(ICMSMESPOSTERIORGRUPOA) ICMSMESPOSTERIORGRUPOA, "

            lstrSql = lstrSql & "       SUM(ICMSCONSUMIDORLIVREST) ICMSCONSUMIDORLIVREST, "
            lstrSql = lstrSql & "       SUM(ICMSCONSUMIDORPOTENCIALMENTELIVREST) ICMSCONSUMIDORPOTENCIALMENTELIVREST, "
            lstrSql = lstrSql & "       SUM(ICMSIMPCIRMERSERVST) ICMSIMPCIRMERSERVST, "
            lstrSql = lstrSql & "       SUM(ICMSIMPCIRMERSERVPOSTERIORST) ICMSIMPCIRMERSERVPOSTERIORST "

            If astrGrupo = "AB" Then
                lstrSql0 = ", IF(LEFT(grupot,1)='B','B',grupot) grupot "
            ElseIf astrGrupo = "AL" Then
                lstrSql0 = ", grupo "
            Else
                lstrSql0 = ", IF(LEFT(grupot,1)='B','B',grupo) grupo "
            End If

            lstrSql1 = " FROM " & istrTabTemp & " WHERE 1 = 1 AND "

            'If Me.cmbModelo.SelectedIndex = 1 Then
            '    lstrSql1 = lstrSql1 & " zona = '" & astrZona & "' AND "
            'End If

            If astrGrupo <> "AB" Then

                If astrGrupo = "AL" Then
                    lstrSql1 = lstrSql1 & "LEFT(grupo,2) = '" & Microsoft.VisualBasic.Left(astrGrupo, 2) & "' AND " ' Pode ser AL(livre) ou ALS(livre especial)
                Else
                    If astrGrupo = "A" Then
                        lstrSql1 = lstrSql1 & "LEFT(grupo,1) = '" & Microsoft.VisualBasic.Left(astrGrupo, 1) & "' AND LEFT(grupo,2) <> 'AL' AND "
                    Else
                        lstrSql1 = lstrSql1 & "LEFT(grupo,1) = '" & Microsoft.VisualBasic.Left(astrGrupo, 1) & "' AND "
                    End If
                End If

            End If

            If astrGrupo = "AB" Then
                lstrSql3 = "GROUP BY grupot ASC "
            ElseIf astrGrupo = "A" Then
                lstrSql3 = "GROUP BY grupo ASC "
            ElseIf astrGrupo = "B" Then
                lstrSql3 = "GROUP BY LEFT(grupo,1) ASC "
            ElseIf astrGrupo = "AL" Then
                lstrSql3 = "GROUP BY LEFT(grupo,4) ASC "
                lstrSql3 = lstrSql3 & "ORDER BY sequencia"
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Residencial "
                Me.lblInfo.Refresh()

                ' For lintAux = 0 To 1

                lstrSqlP = ""
                lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, "
                lstrSql2 = " classe > 9100 "

                objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
                dt = repositorioTempEstatis.Execute_2(objTempEstatis)

                If dt.Rows.Count <> 0 Then
                    For Each row As DataRow In dt.Rows
                        If row("ncons") > 0 Then
                            lblnCanRE = (lintAux = 1)
                        Imprime(row, "RESIDENCIAL NORMAL (9100)" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo,,, "1102.1.01.01.00.0.001", "6101.3.01.01.00.1.001")
                    End If
                    Next
                End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Industrial "
                Me.lblInfo.Refresh()

                lstrSqlP = ""
                    lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, "
                'lstrSql2 = " classe between 0 and 3999 "
                lstrSql2 = "classe BETWEEN 0 AND 3999 AND classe <> '0100' AND (qkwh_fat+qkwh_fat_p+qkwh_fat_i+qkwh_fat_fp) " & IIf(lintAux = 0, ">=", "<") & " 0 "

                objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
                dt = repositorioTempEstatis.Execute_2(objTempEstatis)

                If dt.Rows.Count <> 0 Then
                    For Each row As DataRow In dt.Rows
                        If row("ncons") > 0 Then
                            lblnCanRE = (lintAux = 1)
                        Imprime(row, "INDUSTRIAL" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo,,, "1102.1.01.02.00.0.001", "6101.3.01.02.00.1.001")
                    End If
                    Next
                End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Com. Serviços e Outras Atividades"
                Me.lblInfo.Refresh()

                ' For lintAux = 0 To 1

                lstrSqlP = ""
                lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, "
                lstrSql2 = " classe between 4000 and 4999 "

                objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
                dt = repositorioTempEstatis.Execute_2(objTempEstatis)

                If dt.Rows.Count <> 0 Then
                    For Each row As DataRow In dt.Rows
                        If row("ncons") > 0 Then
                            lblnCanRE = (lintAux = 1)
                        Imprime(row, "Com. Serviços e Outras Atividades" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo,,, "1102.1.01.03.00.0.001", "6101.3.01.03.00.1.001")
                    End If
                    Next
                End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Rural"
                Me.lblInfo.Refresh()

                ' For lintAux = 0 To 1

                lstrSqlP = ""
                lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, "
                lstrSql2 = " classe between 5000 and 5999 "

                objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
                dt = repositorioTempEstatis.Execute_2(objTempEstatis)

                If dt.Rows.Count <> 0 Then
                    For Each row As DataRow In dt.Rows
                        If row("ncons") > 0 Then
                            lblnCanRE = (lintAux = 1)
                        Imprime(row, "Rural" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo,,, "1102.1.01.04.00.0.001", "6101.3.01.04.00.1.001")
                    End If
                    Next
                End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Poder Público"
                Me.lblInfo.Refresh()

                ' For lintAux = 0 To 1

                lstrSqlP = ""
                lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, "
                lstrSql2 = " classe between 6100 and 6199 "

                objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
                dt = repositorioTempEstatis.Execute_2(objTempEstatis)

                If dt.Rows.Count <> 0 Then
                    For Each row As DataRow In dt.Rows
                        If row("ncons") > 0 Then
                            lblnCanRE = (lintAux = 1)
                        Imprime(row, "Poder Público" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo,,, "1102.1.01.05.00.0.001", "6101.3.01.05.00.1.001")
                    End If
                    Next
                End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Iluminação Pública"
                Me.lblInfo.Refresh()

                ' For lintAux = 0 To 1

                lstrSqlP = ""
                lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, "
                lstrSql2 = " classe between 6400 and 6499 "

                objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
                dt = repositorioTempEstatis.Execute_2(objTempEstatis)

                If dt.Rows.Count <> 0 Then
                    For Each row As DataRow In dt.Rows
                        If row("ncons") > 0 Then
                            lblnCanRE = (lintAux = 1)
                        Imprime(row, "Iluminação Pública" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo,,, "1102.1.01.06.00.0.001", "6101.3.01.06.00.1.001")
                    End If
                    Next
                End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Serviço Público"
                Me.lblInfo.Refresh()

                ' For lintAux = 0 To 1

                lstrSqlP = ""
                lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, "
                lstrSql2 = " classe between 7000 and 7999 "

                objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
                dt = repositorioTempEstatis.Execute_2(objTempEstatis)

                If dt.Rows.Count <> 0 Then
                    For Each row As DataRow In dt.Rows
                        If row("ncons") > 0 Then
                            lblnCanRE = (lintAux = 1)
                        Imprime(row, "Serviço Público" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo,,, "1102.1.01.07.00.0.001", "6101.3.01.07.00.1.001")
                    End If
                    Next
                End If


                ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
                Me.lblInfo.Text = lstrAux & " - Consumidor Livre"
                Me.lblInfo.Refresh()

                ' For lintAux = 0 To 1

                lstrSqlP = ""
                lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, "
                lstrSql2 = " classe = 0100 "

                objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
                dt = repositorioTempEstatis.Execute_2(objTempEstatis)

                If dt.Rows.Count <> 0 Then
                    For Each row As DataRow In dt.Rows
                        If row("ncons") > 0 Then
                            lblnCanRE = (lintAux = 1)
                        Imprime(row, "Consumidor Livre" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo,,, "1102.1.01.02.00.0.002", "6101.3.05.02.00.0.001")
                    End If
                    Next
                End If



                ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
                Me.lblInfo.Text = lstrAux & " - Vistorio da Unidade Consumidora – monofásico"
                Me.lblInfo.Refresh()


                lstrSqlP = ""
                lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(vistoria) as valorFinal "
                lstrSql2 = " vistoria <> 0 "

                'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
                objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
                dt = repositorioTempEstatis.Execute_2(objTempEstatis)

                If dt.Rows.Count <> 0 Then
                    For Each row As DataRow In dt.Rows
                        If row("ncons") > 0 Then
                            lblnCanRE = (lintAux = 1)
                        Imprime(row, "Vistorio da Unidade Consumidora – monofásico" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.002", "6101.3.20.01.00.1.015")
                    End If
                    Next
                End If



                ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
                Me.lblInfo.Text = lstrAux & " - Vistorio da Unidade Consumidora – bifásico"
                Me.lblInfo.Refresh()


                lstrSqlP = ""
                lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(vistoria_bif) as valorFinal "
                lstrSql2 = " vistoria_bif <> 0 "

                ' objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
                objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
                dt = repositorioTempEstatis.Execute_2(objTempEstatis)

                If dt.Rows.Count <> 0 Then
                    For Each row As DataRow In dt.Rows
                        If row("ncons") > 0 Then
                            lblnCanRE = (lintAux = 1)
                        Imprime(row, "Vistorio da Unidade Consumidora – bifásico" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.015", "6101.3.20.01.00.1.026")
                    End If
                    Next
                End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Vistorio da Unidade Consumidora – trifásico"
                Me.lblInfo.Refresh()


                lstrSqlP = ""
                lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(vistoria_trif) as valorFinal "
                lstrSql2 = " vistoria_trif <> 0 "

                ' objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
                objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
                dt = repositorioTempEstatis.Execute_2(objTempEstatis)

                If dt.Rows.Count <> 0 Then
                    For Each row As DataRow In dt.Rows
                        If row("ncons") > 0 Then
                            lblnCanRE = (lintAux = 1)
                        Imprime(row, "Vistorio da Unidade Consumidora – trifásico" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.003", "6101.3.20.01.00.1.016")
                    End If
                    Next
                End If


                ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
                Me.lblInfo.Text = lstrAux & " - Vistorio da Unidade Consumidora – Grupo A"
                Me.lblInfo.Refresh()


                lstrSqlP = ""
                lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(VISTORIA_GRUPOA) as valorFinal "
                lstrSql2 = " VISTORIA_GRUPOA <> 0 "

                'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
                objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
                dt = repositorioTempEstatis.Execute_2(objTempEstatis)

                If dt.Rows.Count <> 0 Then
                    For Each row As DataRow In dt.Rows
                        If row("ncons") > 0 Then
                            lblnCanRE = (lintAux = 1)
                        Imprime(row, "Vistorio da Unidade Consumidora – Grupo A" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.025", "6101.3.20.01.00.1.027")
                    End If
                    Next
                End If



            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Serviço Taxado – Aferição medidor – monofásico"
                Me.lblInfo.Refresh()


                lstrSqlP = ""
                lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(SERVTAXADO) as valorFinal "
                lstrSql2 = " SERVTAXADO <> 0 "

                'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
                objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
                dt = repositorioTempEstatis.Execute_2(objTempEstatis)

                If dt.Rows.Count <> 0 Then
                    For Each row As DataRow In dt.Rows
                        If row("ncons") > 0 Then
                            lblnCanRE = (lintAux = 1)
                        Imprime(row, "Serviço Taxado – Aferição medidor – monofásico" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.009", "6101.3.20.01.00.1.005")
                    End If
                    Next
                End If


                ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
                Me.lblInfo.Text = lstrAux & " - Serviço Taxado – Aferição medidor – bifásico"
                Me.lblInfo.Refresh()


                lstrSqlP = ""
                lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(SERVTAXADO_BIF) as valorFinal "
                lstrSql2 = " SERVTAXADO_BIF <> 0 "

                'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
                objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
                dt = repositorioTempEstatis.Execute_2(objTempEstatis)

                If dt.Rows.Count <> 0 Then
                    For Each row As DataRow In dt.Rows
                        If row("ncons") > 0 Then
                            lblnCanRE = (lintAux = 1)
                        Imprime(row, "Serviço Taxado – Aferição medidor – bifásico" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.022", "6101.3.20.01.00.1.023")
                    End If
                    Next
                End If



            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Serviço Taxado – Aferição medidor – trifásico"
                Me.lblInfo.Refresh()


                lstrSqlP = ""
                lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(SERVTAXADO_TRIF) as valorFinal "
            lstrSql2 = " SERVTAXADO_TRIF <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
                dt = repositorioTempEstatis.Execute_2(objTempEstatis)

                If dt.Rows.Count <> 0 Then
                    For Each row As DataRow In dt.Rows
                        If row("ncons") > 0 Then
                            lblnCanRE = (lintAux = 1)
                        Imprime(row, "Serviço Taxado – Aferição medidor – bifásico" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.011", "6101.3.20.01.00.1.007")
                    End If
                    Next
                End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Verificação de Nível de Tensão - monofásico"
            Me.lblInfo.Refresh()


                lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(VERIFNIVEL) as valorFinal "
            lstrSql2 = " VERIFNIVEL <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
                dt = repositorioTempEstatis.Execute_2(objTempEstatis)

                If dt.Rows.Count <> 0 Then
                    For Each row As DataRow In dt.Rows
                        If row("ncons") > 0 Then
                            lblnCanRE = (lintAux = 1)
                        Imprime(row, "Verificação de Nível de Tensão - monofásico" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.016", "6101.3.20.01.00.1.017")
                    End If
                    Next
                End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Verificação de Nível de Tensão - bifásico"
            Me.lblInfo.Refresh()


            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(VERIFNIVEL_BIF) as valorFinal "
            lstrSql2 = " VERIFNIVEL_BIF <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

                If dt.Rows.Count <> 0 Then
                    For Each row As DataRow In dt.Rows
                        If row("ncons") > 0 Then
                            lblnCanRE = (lintAux = 1)
                        Imprime(row, "Verificação de Nível de Tensão - bifásico" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.024", "6101.3.20.01.00.1.025")
                    End If
                    Next
                End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Verificação de Nível de Tensão - trifásico"
            Me.lblInfo.Refresh()


                lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(VERIFNIVEL_TRIF) as valorFinal "
            lstrSql2 = " VERIFNIVEL_TRIF <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
                dt = repositorioTempEstatis.Execute_2(objTempEstatis)

                If dt.Rows.Count <> 0 Then
                    For Each row As DataRow In dt.Rows
                        If row("ncons") > 0 Then
                            lblnCanRE = (lintAux = 1)
                        Imprime(row, "Verificação de Nível de Tensão - trifásico" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.017", "6101.3.20.01.00.1.018")
                    End If
                    Next
                End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Serviço Taxado – Religação normal - monofásica"
            Me.lblInfo.Refresh()


                lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(SERVTAXADO_RN) as valorFinal "
            lstrSql2 = " SERVTAXADO_RN <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
                dt = repositorioTempEstatis.Execute_2(objTempEstatis)

                If dt.Rows.Count <> 0 Then
                    For Each row As DataRow In dt.Rows
                        If row("ncons") > 0 Then
                            lblnCanRE = (lintAux = 1)
                        Imprime(row, "Serviço Taxado – Religação normal - monofásica" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.004", "6101.3.20.01.00.1.008")
                    End If
                    Next
                End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Serviço Taxado – Religação normal – Bifásica"
            Me.lblInfo.Refresh()


                lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(SERVTAXADO_RN_BIF) as valorFinal "
            lstrSql2 = " SERVTAXADO_RN_BIF <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
                dt = repositorioTempEstatis.Execute_2(objTempEstatis)

                If dt.Rows.Count <> 0 Then
                    For Each row As DataRow In dt.Rows
                        If row("ncons") > 0 Then
                            lblnCanRE = (lintAux = 1)
                        Imprime(row, "Serviço Taxado – Religação normal – Bifásica" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.012", "6101.3.20.01.00.1.012")
                    End If
                    Next
                End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Serviço Taxado – Religação normal – trifásica"
            Me.lblInfo.Refresh()


                lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(SERVTAXADO_RN_TRIF) as valorFinal "
            lstrSql2 = " SERVTAXADO_RN_TRIF <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
                dt = repositorioTempEstatis.Execute_2(objTempEstatis)

                If dt.Rows.Count <> 0 Then
                    For Each row As DataRow In dt.Rows
                        If row("ncons") > 0 Then
                            lblnCanRE = (lintAux = 1)
                        Imprime(row, "Serviço Taxado – Religação normal – trifásica" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.005", "6101.3.20.01.00.1.010")
                    End If
                    Next
                End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Serviço Taxado – Religação Normal GA"
            Me.lblInfo.Refresh()


                lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(SERVTAXADO_RN_GA) as valorFinal "
            lstrSql2 = " SERVTAXADO_RN_GA <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
                dt = repositorioTempEstatis.Execute_2(objTempEstatis)

                If dt.Rows.Count <> 0 Then
                    For Each row As DataRow In dt.Rows
                        If row("ncons") > 0 Then
                            lblnCanRE = (lintAux = 1)
                        Imprime(row, "Serviço Taxado – Religação Normal GA" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.013", "6101.3.20.01.00.1.013")
                    End If
                    Next
                End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Serviço Taxado – Emissão 2ª via -monofásico"
            Me.lblInfo.Refresh()


            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(SERVTAXADO_EM2) as valorFinal "
            lstrSql2 = " SERVTAXADO_EM2 <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
                dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Serviço Taxado – Emissão 2ª via -monofásico" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.006", "6101.3.20.01.00.1.002")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Serviço Taxado – Emissão 2ª via – bifásico"
            Me.lblInfo.Refresh()


            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(SERVTAXADO_EM2_BIF) as valorFinal "
            lstrSql2 = " SERVTAXADO_EM2_BIF <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Serviço Taxado – Emissão 2ª via – bifásico" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.007", "6101.3.20.01.00.1.003")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Serviço Taxado – Emissão 2ª via -trifásico"
            Me.lblInfo.Refresh()


            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(SERVTAXADO_EM2_TRIF) as valorFinal "
            lstrSql2 = " SERVTAXADO_EM2_TRIF <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Serviço Taxado – Emissão 2ª via -trifásico" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.008", "6101.3.20.01.00.1.004")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Serviço Taxado – Emissão 2ª via - grupo A4"
            Me.lblInfo.Refresh()


            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(SERVTAXADO_EM2_A4) as valorFinal "
            lstrSql2 = " SERVTAXADO_EM2_A4 <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Serviço Taxado – Emissão 2ª via - grupo A4" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.014", "6101.3.20.01.00.1.014")
                    End If
                Next
            End If

            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            'Me.lblInfo.Text = lstrAux & " - Serviço Taxado -  Disp.dados(mem. De massa)"
            'Me.lblInfo.Refresh()


            'lstrSqlP = ""
            'lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(SERVTAXADO_EM2) as valorFinal "
            'lstrSql2 = " SERVTAXADO_EM2 <> 0 "

            ''objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            'objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            'dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            'If dt.Rows.Count <> 0 Then
            '    For Each row As DataRow In dt.Rows
            '        If row("ncons") > 0 Then
            '            lblnCanRE = (lintAux = 1)
            '            Imprime(row, "Serviço Taxado -  Disp.dados(mem. De massa)" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B")
            '        End If
            '    Next
            'End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Desligamento Programado - monofásico"
            Me.lblInfo.Refresh()


            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(DESLIGPROGRAMADO) as valorFinal "
            lstrSql2 = " DESLIGPROGRAMADO <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Desligamento Programado - monofásico" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.026", "6101.3.20.01.00.1.028")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Desligamento Programado - trifásico"
            Me.lblInfo.Refresh()


            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(DESLIGPROGRAMADO_TRIF) as valorFinal "
            lstrSql2 = " DESLIGPROGRAMADO_TRIF <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Desligamento Programado - trifásico" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.018", "6101.3.20.01.00.1.019")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Desligamento Programado - Grupo A"
            Me.lblInfo.Refresh()


            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(DESLIGPROGRAMADO_GRUPOA4) as valorFinal "
            lstrSql2 = " DESLIGPROGRAMADO_GRUPOA4 <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Desligamento Programado - Grupo A" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.019", "6101.3.20.01.00.1.020")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Religação Programada - Grupo A"
            Me.lblInfo.Refresh()


            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(RELIGPROGRAMADA_GRUPOA) as valorFinal "
            lstrSql2 = " RELIGPROGRAMADA_GRUPOA <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Religação Programada - Grupo A" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.020", "6101.3.20.01.00.1.021")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Religação Programada - monofásico"
            Me.lblInfo.Refresh()


            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(RELIGPROGRAMADA) as valorFinal "
            lstrSql2 = " RELIGPROGRAMADA <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Religação Programada - monofásico" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.027", "6101.3.20.01.00.1.029")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Religação Programada - trifásico"
            Me.lblInfo.Refresh()


            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(RELIGPROGRAMADA_TRIF) as valorFinal "
            lstrSql2 = " RELIGPROGRAMADA_TRIF <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Religação Programada - trifásico" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.021", "6101.3.20.01.00.1.022")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Fornecimento de pulsos pot. sincronismo Grupo A"
            Me.lblInfo.Refresh()


            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(FORNECIMENTOPULSO_GRUPOA) as valorFinal "
            lstrSql2 = " FORNECIMENTOPULSO_GRUPOA <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Fornecimento de pulsos pot. sincronismo Grupo A" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.023", "6101.3.20.01.00.1.024")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Custo Adm. de inspeção - Monofásico"
            Me.lblInfo.Refresh()


            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(CUSTOADM) as valorFinal "
            lstrSql2 = " CUSTOADM <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Custo Adm. de inspeção - Monofásico" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.028", "6101.3.20.01.00.1.030")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Custo Adm. de inspeção - Trifásico"
            Me.lblInfo.Refresh()


            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(CUSTOADM_GRUPOA) as valorFinal "
            lstrSql2 = " CUSTOADM_GRUPOA   <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Custo Adm. de inspeção - Trifásico" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.2.01.00.00.0.029", "6101.3.20.01.00.1.031")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Multa s/ atraso"
            Me.lblInfo.Refresh()


            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(ACRESCIMOMULTAATRASO) as valorFinal "
            lstrSql2 = " ACRESCIMOMULTAATRASO   <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Multa s/ atraso" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B", "Acréscimo. Moratório Contas de Energia (Multa)", "1102.9.00.00.00.0.005", "6301.3.03.00.00.0.001")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Juros sobre Atraso"
            Me.lblInfo.Refresh()


            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(JUROSATRASO) as valorFinal "
            lstrSql2 = " JUROSATRASO   <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Juros sobre Atraso" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B", , "1102.9.00.00.00.0.006 ", "6301.3.03.00.00.0.002")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Correção Monetária"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(CORRECAOMONETARIA) as valorFinal "
            lstrSql2 = " CORRECAOMONETARIA   <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Correção Monetária" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B", "Correção Monetária sobre Atraso", "1102.9.00.00.00.0.007", "6301.3.03.00.00.0.002")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Serviços Prestados"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(SERVICOPRESTADOS) as valorFinal "
            lstrSql2 = " SERVICOPRESTADOS  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Serviços Prestados" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B", "Convênio de Arrecadação-Hospital N.Sra.Conceição (1%)", "1102.9.00.00.00.0.008", "6111.1.20.08.00.0.001")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Devolução a maior - consum. a compensar (dup.pgto)"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(DEVOLUCAOAMAIOR) as valorFinal "
            lstrSql2 = " DEVOLUCAOAMAIOR   <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Devolução a maior - consum. a compensar (dup.pgto)" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1119.1.03.00.00.0.006", "")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - (-) Devolução de Consumo"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(DEVOLUCAOCONSUMO) as valorFinal "
            lstrSql2 = " DEVOLUCAOCONSUMO   <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "(-) Devolução de Consumo" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "6101.3.01.01.00.1.003", "")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - (-) Devolução de cobrança a maior"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(DEVOLUCAOCOBRANCAAMAIOR) as valorFinal "
            lstrSql2 = " DEVOLUCAOCOBRANCAAMAIOR  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "(-) Devolução de cobrança a maior" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B", "Devolução de cobrança a maior", "6101.3.01.01.00.1.004", "2119.1.00.00.00.0.018")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - (-) Devolução de cobrança segunda via"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(DEVOLUCAOCOBRANCA2VIA) as valorFinal "
            lstrSql2 = " DEVOLUCAOCOBRANCA2VIA   <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "(-) Devolução de cobrança segunda via" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B", "Devolução de cobrança segunda via", "6101.3.01.01.00.1.005", "2119.1.00.00.00.0.031")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - (-) Devolução - Aferição de medidor"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(DEVOLUCAOAFERICAOMEDIDOR) as valorFinal "
            lstrSql2 = " DEVOLUCAOAFERICAOMEDIDOR  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "(-) Devolução - Aferição de medidor" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B", , "6101.3.01.01.00.1.006", "2119.1.00.00.00.0.036")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - (-) Desconto - Irrigante/Aquicultor"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(DESCONTOIRRIGANTEAQUICULTOR) as valorFinal "
            lstrSql2 = " DESCONTOIRRIGANTEAQUICULTOR  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "(-) Desconto - Irrigante/Aquicultor" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B", "Desconto - Irrigante/Aquicultor", "6101.3.01.04.00.1.002", "2119.1.00.00.00.0.021")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Devolução Correção Ressarcimento Indicadores"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(DEVOLUCAOCORRECAO) as valorFinal "
            lstrSql2 = " DEVOLUCAOCORRECAO  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Devolução Correção Ressarcimento Indicadores" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "6305.3.99.00.00.1.009", "")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Ressarcimento DMIC
"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(RESSARCIMENTODMIC) as valorFinal "
            lstrSql2 = " RESSARCIMENTODMIC  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Ressarcimento DMIC" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B", , "6105.3.19.06.00.0.001", "2119.1.00.00.00.0.015")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Ressarcimento DIC/FIC"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(RESSARCIMENTODICFIC) as valorFinal "
            lstrSql2 = " RESSARCIMENTODICFIC  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Ressarcimento DIC/FIC" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B", , "6305.3.99.00.00.1.011", "2119.1.00.00.00.0.014")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Ressarcimento Nível de Tensão"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(RESSARCIMENTONIVELTENSAO) as valorFinal "
            lstrSql2 = " RESSARCIMENTONIVELTENSAO  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Ressarcimento Nível de Tensão" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B", , "6105.3.19.06.00.0.003", "2119.1.00.00.00.0.012")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Ressarcimento Violação de Prazo"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(RESSARCIMENTOVIOLACAOPRAZO) as valorFinal "
            lstrSql2 = " RESSARCIMENTOVIOLACAOPRAZO  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Ressarcimento Violação de Prazo" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "6105.3.19.06.00.0.004", "2119.1.00.00.00.0.011")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Bonus Itaipu (9.11)"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(BONUITAIPU) as valorFinal "
            lstrSql2 = " BONUITAIPU  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Bonus Itaipu (9.11)" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "2119.1.00.00.00.0.004", "")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Bonus Itaipu a devolver (9.11)"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(BONUITAIPUDEVOLVER) as valorFinal "
            lstrSql2 = " BONUITAIPUDEVOLVER  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Bonus Itaipu a devolver (9.11)" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "2119.1.00.00.00.0.009", "")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Micro residencial"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(MICRORESIDENCIAL) as valorFinal "
            lstrSql2 = " MICRORESIDENCIAL  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Micro residencial" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "6105.3.01.01.13.1.002", "2119.1.00.00.00.0.019")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Micro Industrial"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(MICROINDUSTRIAL) as valorFinal "
            lstrSql2 = " MICROINDUSTRIAL  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Micro Industrial" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "6105.3.01.01.13.1.005", "2119.1.00.00.00.0.035")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Micro Comercial"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(MICROCOMERCIAL) as valorFinal "
            lstrSql2 = " MICROCOMERCIAL  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Micro Comercial" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "6105.3.01.01.13.1.004", "2119.1.00.00.00.0.034")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Micro Rural"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(MICRORURAL) as valorFinal "
            lstrSql2 = " MICRORURAL  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Micro Rural" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "6105.3.01.01.13.1.003", "2119.1.00.00.00.0.024")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Micro Poder Público"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(MICROPODERPUBLICO) as valorFinal "
            lstrSql2 = " MICROPODERPUBLICO  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Micro Poder Público" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "6105.3.01.01.13.1.001", "2119.1.00.00.00.0.016")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Receita Uso Sistema de Distribuição (MUST)"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(RECEITASISTEMADISTRIBUICAO) as valorFinal "
            lstrSql2 = " RECEITASISTEMADISTRIBUICAO  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Receita Uso Sistema de Distribuição (MUST)" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.1.01.02.00.0.006", "6101.3.05.03.00.0.001")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Receita Uso Sistema de Distribuição (Taxa de Conexão)"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(RECEITASISTEMADISTRIBUICAOTAXA) as valorFinal "
            lstrSql2 = " RECEITASISTEMADISTRIBUICAOTAXA  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Receita Uso Sistema de Distribuição (Taxa de Conexão)" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "1102.1.01.02.00.0.007", "6101.3.06.02.00.1.003")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - TUSD / MUST"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(TUSDMUST) as valorFinal "
            lstrSql2 = " TUSDMUST  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "TUSD / MUST" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B", "TUSD / MUST - Formação", "1119.1.09.01.00.8.002", "6101.3.21.02.00.0.001")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - Receita Bandeira"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(RECEITABANDEIRA) as valorFinal "
            lstrSql2 = " RECEITABANDEIRA  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "Receita Bandeira" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "B",, "6101.3.09.01.00.2.001", "2111.2.07.00.00.0.001")
                    End If
                Next
            End If



            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - ICMS - Residencial"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(ICMSRESIDENCIAL) as valorFinal "
            lstrSql2 = " ICMSRESIDENCIAL  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "ICMS - Residencial" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "D",, "6101.3.30.04.00.1.006", "")
                    End If
                Next
            End If



            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - ICMS - Industrial"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(ICMSINDUSTRIAL) as valorFinal "
            lstrSql2 = " ICMSINDUSTRIAL  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "ICMS - Industrial" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "D",, "6101.3.30.04.00.1.007", "")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - ICMS - Com, Serviços e Outras Atividades"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(ICMSCOMERCIAL) as valorFinal "
            lstrSql2 = " ICMSCOMERCIAL  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "ICMS - Com, Serviços e Outras Atividades" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "D",, "6101.3.30.04.00.1.008", "")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - ICMS - Rural"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(ICMSRURAL) as valorFinal "
            lstrSql2 = " ICMSRURAL  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "ICMS - Rural" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "D",, "6101.3.30.04.00.1.009", "")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - ICMS - Poder Público"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(ICMSPODERPUBLICO) as valorFinal "
            lstrSql2 = " ICMSPODERPUBLICO  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "ICMS - Poder Público" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "D",, "6101.3.30.04.00.1.010", "")
                    End If
                Next
            End If

            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - ICMS - iluminação Pública"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(ICMSILUMINACAOPUBLICA) as valorFinal "
            lstrSql2 = " ICMSILUMINACAOPUBLICA  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "ICMS - iluminação Pública" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "D",, "6101.3.30.04.00.1.011", "")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - ICMS - Serviço Público"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(ICMSSERVICOPUBLICO) as valorFinal "
            lstrSql2 = " ICMSSERVICOPUBLICO  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "ICMS - Serviço Público" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "D",, "6101.3.30.04.00.1.012", "")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - ICMS - Consumidor Livre"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(ICMSCONSUMIDORLIVRE) as valorFinal "
            lstrSql2 = " ICMSCONSUMIDORLIVRE  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "ICMS - Consumidor Livre" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "D",, "6101.3.30.04.00.0.001", "")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - ICMS - Consumidor Potencialmente Livre"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(ICMSCONSUMIDORPOTENCIALMENTELIVRE) as valorFinal "
            lstrSql2 = " ICMSCONSUMIDORPOTENCIALMENTELIVRE  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "ICMS - Consumidor Potencialmente Livre" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "D",, "6101.3.30.04.00.2.001", "")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - ICMS - Imp. s/ Circulação de Merc. e Serviços"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, (SUM(icms) - CASE WHEN SUM(ICMSIMPCIRMERSERV) < 0 THEN SUM(ICMSIMPCIRMERSERV) * -1 ELSE SUM(ICMSIMPCIRMERSERV)END) AS valorFinal  "
            'lstrSql2 = " (sum(icms) - sum(ICMSIMPCIRMERSERV))  <> 0 "
            lstrSql2 = " icms > 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "ICMS - Imp. s/ Circulação de Merc. e Serviços" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "C",, "", "2105.2.01.00.00.0.001")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - ICMS - Mês posterior - ILUM.PUBLICA GRUPO B"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(ICMSMESPOSTERIORILUM) as valorFinal "
            lstrSql2 = " ICMSMESPOSTERIORILUM  <> 0 "

            'objTempEstatis.TABLE = lstrSqlP & lstrSql & lstrSql1 & lstrSql2 & lstrSql3
            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "ICMS - Mês posterior - ILUM.PUBLICA GRUPO B" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "C",, "", "2105.2.01.00.00.0.004")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - ICMS - Mês posterior - GRUPO A"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(icms) as valorFinal FROM temp_estatis where LEFT(grupo,1) = 'A' GROUP BY LEFT(grupo,1)  ASC"
            'lstrSql2 = " ICMSMESPOSTERIORGRUPOA  <> 0 "

            objTempEstatis.TABLE = lstrSqlP '& lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "ICMS - Mês posterior - GRUPO A" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "C",, "", "2105.2.01.00.00.0.004")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - ICMS - Consumidor Livre - ST"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(ICMSCONSUMIDORLIVREST) as valorFinal "
            lstrSql2 = " ICMSCONSUMIDORLIVREST  <> 0 "

            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "ICMS - Consumidor Livre - ST" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "D",, "6101.3.30.04.00.0.004", "")
                    End If
                Next
            End If



            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - ICMS - Consumidor Potencialmente Livre - ST"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(ICMSCONSUMIDORPOTENCIALMENTELIVREST) as valorFinal "
            lstrSql2 = " ICMSCONSUMIDORPOTENCIALMENTELIVREST  <> 0 "

            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "ICMS - Consumidor Potencialmente Livre - ST" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "D",, "6101.3.30.04.00.2.002", "")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - ICMS - Imp.s/Circulação de Merc. e Serviços - ST"
            Me.lblInfo.Refresh()


            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, (SUM(icms) - CASE WHEN SUM(ICMSIMPCIRMERSERVST) < 0 THEN SUM(ICMSIMPCIRMERSERVST) * -1 ELSE SUM(ICMSIMPCIRMERSERVST)END) AS valorFinal  "
            'lstrSql2 = " (sum(icms) - sum(ICMSIMPCIRMERSERV))  <> 0 "
            lstrSql2 = " icms > 0 "

            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "ICMS - Imp.s/Circulação de Merc. e Serviços - ST" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "C",, "", "2105.2.01.00.00.0.003")
                    End If
                Next
            End If


            ' ==============================  xxxxxxxxxxxxxxxxxxxxxxxxxx ================================================================================
            Me.lblInfo.Text = lstrAux & " - ICMS - Imp.s/Circul. de Merc. e Serv. - ST - Mês posterior"
            Me.lblInfo.Refresh()

            lstrSqlP = ""
            lstrSqlP = lstrSqlP & "SELECT COUNT(*) ncons, sum(ICMSIMPCIRMERSERVPOSTERIORST) as valorFinal "
            lstrSql2 = " ICMSIMPCIRMERSERVPOSTERIORST  <> 0 "

            objTempEstatis.TABLE = lstrSqlP & lstrSql1 & lstrSql2 & lstrSql3
            dt = repositorioTempEstatis.Execute_2(objTempEstatis)

            If dt.Rows.Count <> 0 Then
                For Each row As DataRow In dt.Rows
                    If row("ncons") > 0 Then
                        lblnCanRE = (lintAux = 1)
                        Imprime(row, "ICMS - Imp.s/Circul. de Merc. e Serv. - ST - Mês posterior" & IIf(lintAux = 1, " (C)", " "), astrGrupo,  , "T", astrGrupo, astrCabecTitulo, astrCabecGrupo, "C",, "", "2105.2.01.00.00.0.005")
                    End If
                Next
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: sFiltraTabTemporaria_classe" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Function BuscaNome(ByVal alngConsId As Integer, ByVal alngUnidId As Integer, ByVal astrGrupo As String) As String

        Dim lstrRet As String

        Dim vlist

        Dim objUCFaturamento As New entidadeUCFaturamento
        Dim repositorioUCFaturamento As New repositorioUCFaturamento

        Dim objConsum As New entidadeConsum
        Dim repositorioConsum As New repositorioConsum

        Try

            lstrRet = ""

            objUCFaturamento.GRUPO = LCase(astrGrupo)
            objUCFaturamento.UNID_ID = alngUnidId
            objUCFaturamento.CONS_ID = alngConsId

            vlist = repositorioUCFaturamento.Select_13(objUCFaturamento)

            If vlist.Count > 0 Then
                For Each objUCFaturamentoFor As entidadeUCFaturamento In vlist
                    lstrRet = Trim(IIf(IsDBNull(objUCFaturamentoFor.NOME), "", objUCFaturamentoFor.NOME))
                Next
            End If

            If lstrRet = "" Then
                objConsum.CONSREG = alngConsId

                vlist = repositorioConsum.Select(objConsum)

                If vlist.Count > 0 Then
                    For Each objConsumFor As entidadeConsum In vlist
                        lstrRet = Trim(IIf(IsDBNull(objConsumFor.NOME), "", objConsumFor.NOME))
                    Next
                End If
            End If


            If lstrRet = "" Then lstrRet = "NOME INDEFINIDO"

            BuscaNome = lstrRet


        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: BuscaNome" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function
        End Try

    End Function

    Private Function fRetornaValorItemFatura(ByVal numnf As Long, ByVal anomes As String) As Double

        Try

            Dim lstrsql As String
            Dim ret As Double

            ret = 0
            lstrsql = ""
            lstrsql = "select sum(valor_item) as valor from temp_item_fatura where numnf = " & numnf & " and anomes_fat = '" & anomes & "' "
            objAtualizaBancoDados.QUERY_NAME = lstrsql
            dtinterno = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            If dtinterno.Rows.Count > 0 Then
                For Each rowinterno As DataRow In dtinterno.Rows
                    ret = Trim(IIf(IsDBNull(rowinterno.Item("valor")), 0, rowinterno.Item("valor")))
                Next
            End If

            fRetornaValorItemFatura = ret

            dtinterno = Nothing

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: fRetornaValorItemFatura" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function
        End Try

    End Function

    Public Function getLote(pAnoMes As Integer, ByRef pIdString As String, ByRef pDescricao As String) As Integer
        Dim sql As String = ""
        'Dim rst As New ADODB.Recordset

        Try

            sql = "SELECT `id`, `id_string`, `descricao` "
            sql &= "FROM `" & GetConfig("integracao.bd.contab") & "`.`lote` "
            sql &= "WHERE `anomes` = " & pAnoMes & " "

            'If GetConfig("modo.Sistema") = "COMPRAS" Then
            '    sql &= "AND `id_string` = 'COMPRAS' "
            'Else
            '    sql &= "AND `id_string` = 'ALMOX' "
            'End If
            sql &= "AND `id_string` = 'FATF' "
            objAtualizaBancoDados.QUERY_NAME = sql
            dtinterno = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            If dtinterno.Rows.Count > 0 Then
                For Each rowinterno As DataRow In dtinterno.Rows
                    pIdString = Trim(IIf(IsDBNull(rowinterno.Item("id_string")), "", rowinterno.Item("id_string")))
                    pDescricao = Trim(IIf(IsDBNull(rowinterno.Item("descricao")), "", rowinterno.Item("descricao")))

                    Return rowinterno.Item("id")
                Next
            Else
                Return 0
            End If

            dtinterno = Nothing

            Return 0

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: getLote" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function
        End Try

    End Function

    Private Sub sConcluiSituacao(ByVal ilinha As Integer, ByVal sSituacao As String)

        'Me.ltNaoExportado.Items.Add(Strings.Format(id_docto, "000000"))
        'Me.ltNaoExportado.Items.Item(ilinha).SubItems().Add(sSituacao)
        ltNaoExportado.Items.Item(ilinha).SubItems(7).Text = sSituacao
        'ltNaoExportado.Items.Item(ilinha).SubItems(7).ForeColor = Color.Red

    End Sub


End Class