Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE
Imports System.Data
Imports System.Linq
Imports System.Text.RegularExpressions

Imports CrystalDecisions.CrystalReports.Engine
Imports System.IO
Friend Class frmRelSampNovo
    Inherits System.Windows.Forms.Form
    'Private ifrmRelSamp As FrmRelatorio
    Private ifrmDir As frmDir
    Private istrCaminho As String
    Private istrTabela As String
    Private istrTab945 As String
    Private ilngLimRes As Integer
    Private ilngLimRur As Integer
    Private iblnExporta As Boolean
    Private objAtualizaBancoDados As New entidadeAtualizaBancoDados
    Private repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados

    Private objExecAtualizaBancoDados As New entidadeAtualizaBancoDados
    Private repositorioExecAtualizaBancoDados As New repositorioAtualizaBancoDados
    Private dt, dt1, dt2, dt3 As DataTable
    Private LImagem() As Byte
    Private sNomeEmpresa As String
    Private sEnderecoEmpresa As String
    Private sCepEmpresa As String
    Private sCidadeEmpresa As String
    Private sUFempresa As String
    Private sBairroEmpresa As String
    Private sFoneEmpresa As String
    Private sWebEmpresa As String


    Private Sub cmdExporta_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExporta.Click
        Dim lintRef As Short
        Dim lintCla As Short
        Dim lintMod As Short
        Dim lintSub As Short
        Dim lintDet As Short
        Dim lintPos As Short
        Dim ldblVlr As Double
        Dim lstrVlr As String

        Dim lintFluxo As Short
        Dim lintModalidade As Short
        Dim lintCaracteristica As Short
        Dim lintDetalhe As Short


        'Controle de arquivos
        Dim arquivos(1, 3) As Short
        Dim lintArqBE As Short = FreeFile()
        Dim diretorio As String = Me.txtDir.Text
        Dim lstrLin As String
        Dim lstrSql As String
        Dim lstrSql1 As String

        Dim lintGrupo As Integer


        'Dim lrstAux As ADODB.Recordset
        'On Error GoTo ErroGeraArquivos
        Try
            Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
            Call LigDes(False)
            'lrstAux = New ADODB.Recordset
            Me.lblInfoArq.Text = " Buscando os dados para exportação do samp referente ao mês/ano " & VB6.Format(Me.dtpMesAno.Value, "MM/yyyy") & ", aguarde... "
            Call Me.lblInfoArq.Refresh()

            If Not Directory.Exists(txtDir.Text) Then
                MsgBox("O caminho informado para exportação dos arquivos não existe, favor verifique sua tabela configuraçõe", vbInformation + vbOK, "Aviso")
                Exit Sub
            End If

            lstrSql = ""
            lstrSql = "SELECT * FROM `samp_exportacao` WHERE `ano_mes` = " & VB6.Format(Me.dtpMesAno.Value, "yyyyMM") & " ORDER BY `sequencia` ASC "
            objAtualizaBancoDados.QUERY_NAME = lstrSql
            dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            lstrSql = ""
            lstrSql1 = "SELECT * FROM `samp_exportacao_be` WHERE `ano_mes` = " & VB6.Format(Me.dtpMesAno.Value, "yyyyMM") & " ORDER BY `sequencia` ASC "
            objAtualizaBancoDados.QUERY_NAME = lstrSql1
            dt1 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            If dt.Rows.Count <= 0 Then
                Me.Cursor = System.Windows.Forms.Cursors.Default
                Call MsgBox("Não foram encotrados dados para serem exportados para o Arquivo de Sistema de Compensação!", MsgBoxStyle.Information, gstrTituloMensagem)
                Exit Sub
            ElseIf dt1.Rows.Count <= 0 Then
                Me.Cursor = System.Windows.Forms.Cursors.Default
                Call MsgBox("Não foram encontrados dados para serem exportados para o Arquivo de Balanço Energetico!", MsgBoxStyle.Information, gstrTituloMensagem)
                Exit Sub
            End If


            If dt.Rows.Count > 0 Then

                Call BarraProgresso((Me.ProgressBarArq), (Me.lblPercArq), (Me.lblInfoArq), 0, dt.Rows.Count, " Exportando Sistema Compensação ")
                ' Regular
                Dim lintArqReg As Short = FreeFile()
                arquivos(0, 0) = lintArqReg
                FileOpen(lintArqReg, diretorio & "\" & Me.txtArquivoRegular.Text, OpenMode.Output)

                Dim lintArqRef As Short = FreeFile()
                arquivos(1, 0) = lintArqRef
                FileOpen(lintArqRef, diretorio & "\" & Me.txtArquivoRefaturado.Text, OpenMode.Output)

                ' Regular GD I
                Dim lintArq_GD_I As Short = FreeFile()
                arquivos(0, 1) = lintArq_GD_I
                FileOpen(lintArq_GD_I, diretorio & "\" & Replace(Me.txtArquivoRegular.Text, "Faturamento_Regular", "Sistema_Compensação_GD_I"), OpenMode.Output)

                Dim lintArqRef_GD_I As Short = FreeFile()
                arquivos(1, 1) = lintArqRef_GD_I
                FileOpen(lintArqRef_GD_I, diretorio & "\" & Replace(Me.txtArquivoRefaturado.Text, "Faturamento_Refaturamento", "Sistema_Compensação_GD_I_Refaturado"), OpenMode.Output)

                ' Regular GD II
                Dim lintArq_GD_II As Short = FreeFile()
                arquivos(0, 2) = lintArq_GD_II
                FileOpen(lintArq_GD_II, diretorio & "\" & Replace(Me.txtArquivoRegular.Text, "Faturamento_Regular", "Sistema_Compensação_GD_II"), OpenMode.Output)

                Dim lintArqRef_GD_II As Short = FreeFile()
                arquivos(1, 2) = lintArqRef_GD_II
                FileOpen(lintArqRef_GD_II, diretorio & "\" & Replace(Me.txtArquivoRefaturado.Text, "Faturamento_Refaturamento", "Sistema_Compensação_GD_II_Refaturado"), OpenMode.Output)

                ' Regular GD III
                Dim lintArq_GD_III As Short = FreeFile()
                arquivos(0, 3) = lintArq_GD_III
                FileOpen(lintArq_GD_III, diretorio & "\" & Replace(Me.txtArquivoRegular.Text, "Faturamento_Regular", "Sistema_Compensação_GD_III"), OpenMode.Output)

                Dim lintArqRef_GD_III As Short = FreeFile()
                arquivos(1, 3) = lintArqRef_GD_III
                FileOpen(lintArqRef_GD_III, diretorio & "\" & Replace(Me.txtArquivoRefaturado.Text, "Faturamento_Refaturamento", "Sistema_Compensação_GD_III_Refaturado"), OpenMode.Output)

                For Each row As DataRow In dt.Rows
                    lintRef = Convert.ToInt32(IIf(IsDBNull(row.Item("refaturado")), 0, row.Item("refaturado")))
                    lintCla = IIf(IsDBNull(row.Item("classe")), 0, row.Item("classe"))
                    lintMod = IIf(IsDBNull(row.Item("modalidade")), 0, row.Item("modalidade"))
                    lintSub = IIf(IsDBNull(row.Item("sub_grupo")), 0, row.Item("sub_grupo"))
                    lintDet = IIf(IsDBNull(row.Item("detalhe")), 0, row.Item("detalhe"))
                    lintPos = IIf(IsDBNull(row.Item("posto")), 0, row.Item("posto"))
                    ldblVlr = IIf(IsDBNull(row.Item("valor")), 0, row.Item("valor"))
                    lintGrupo = IIf(IsDBNull(row.Item("grpGD")), 0, row.Item("grpGD"))

                    ' lintGrupo '
                    ' 0 - Regular.
                    ' 1 - GD_1.
                    ' 2 - GD_2.
                    ' 3 - GD_3.

                    If lintDet = 1 Or lintDet = 2 Or lintDet = 3 Or lintDet = 4 Or lintDet = 7 Or lintDet = 10 Or lintDet = 15 Or lintDet = 21 Or lintDet = 22 Or lintDet = 23 Or lintDet = 24 Or lintDet = 26 Or lintDet = 28 Then
                        lstrVlr = Replace(Replace(VB6.Format(ldblVlr, "#,##0"), ".", ""), ",", VB6.GetItemString(Me.cmbCaracter, Me.cmbCaracter.SelectedIndex))
                    Else
                        If Me.chkCasaDecimal.CheckState = System.Windows.Forms.CheckState.Unchecked Then
                            If lintDet = 5 Or lintDet = 6 Or lintDet = 8 Or lintDet = 9 Or lintDet = 11 Or lintDet = 12 Or lintDet = 13 Or lintDet = 14 Or lintDet = 16 Or lintDet = 17 Or lintDet = 18 Or lintDet = 19 Or lintDet = 25 Or lintDet = 27 Then
                                lstrVlr = Replace(VB6.Format(ldblVlr, "#,##0.00"), ".", "")
                            Else
                                lstrVlr = Replace(Replace(VB6.Format(ldblVlr * 100, "###,###0"), ".", ""), ",", "")
                            End If
                        Else
                            lstrVlr = Replace(Replace(VB6.Format(ldblVlr, "#,##0.00"), ".", ""), ",", VB6.GetItemString(Me.cmbCaracter, Me.cmbCaracter.SelectedIndex))
                        End If
                    End If
                    If Me.chkExportaDescritivo.CheckState = System.Windows.Forms.CheckState.Unchecked Then
                        lstrLin = lintCla & ";" & lintMod & ";" & lintSub & ";" & lintDet & ";" & lintPos & ";" & lstrVlr
                    Else
                        lstrLin = ""
                        lstrLin = lstrLin & lintCla & ";" & GetExpSampClasses(lintCla) & ";"
                        lstrLin = lstrLin & lintMod & ";" & GetExpModalidade(lintMod) & ";"
                        lstrLin = lstrLin & lintSub & ";" & GetExpSubGrupo(lintSub) & ";"
                        lstrLin = lstrLin & lintDet & ";" & GetExpDetalheFornecimento(lintDet) & ";"
                        lstrLin = lstrLin & lintPos & ";" & GetExpPostoTarifario(lintPos) & ";" & lstrVlr
                    End If

                    If lintRef = 0 Then
                        PrintLine(arquivos(0, lintGrupo), lstrLin)
                    ElseIf lintRef = 1 Then
                        PrintLine(arquivos(1, lintGrupo), lstrLin)
                    End If

                    Call BarraProgresso((Me.ProgressBarArq), (Me.lblPercArq), (Me.lblInfoArq), 1,  , " Exportando Sistema Compensação ")
                Next


                Dim numRows As Integer = arquivos.GetLength(0)
                Dim numCols As Integer = arquivos.GetLength(1)

                For i As Integer = 0 To numRows - 1
                    For j As Integer = 0 To numCols - 1
                        FileClose(arquivos(i, j))
                    Next
                Next

                Me.Cursor = System.Windows.Forms.Cursors.Default
                Call LigDes(True)

                Me.lblInfoArq.Text = Me.lblInfoArq.Text & " *** Finalizado Sistema de Compensação **** "
                'Call MsgBox("Arquivos gerados com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
            Else
                Me.Cursor = System.Windows.Forms.Cursors.Default
                Call MsgBox("Não foram encontrados dados para serem exportados!", MsgBoxStyle.Information, gstrTituloMensagem)
            End If

            If dt1.Rows.Count > 0 Then

                Call BarraProgresso((Me.ProgressBarArq), (Me.lblPercArq), (Me.lblInfoArq), 0, dt1.Rows.Count, " Exportando Balanço Energetico ")


                FileOpen(lintArqBE, diretorio & "\" & Replace(Me.txtArquivoRegular.Text, "Faturamento_Regular", "Balanço_Energetico"), OpenMode.Output)

                For Each row1 As DataRow In dt1.Rows
                    lintFluxo = Convert.ToInt32(IIf(IsDBNull(row1.Item("fluxo")), 0, row1.Item("fluxo")))
                    lintModalidade = IIf(IsDBNull(row1.Item("modalidade")), 0, row1.Item("modalidade"))
                    lintCaracteristica = IIf(IsDBNull(row1.Item("caracteristica")), 0, row1.Item("caracteristica"))
                    lintDetalhe = IIf(IsDBNull(row1.Item("detalhe")), 0, row1.Item("detalhe"))
                    ldblVlr = IIf(IsDBNull(row1.Item("valor")), 0, row1.Item("valor"))


                    If Me.chkCasaDecimal.CheckState = System.Windows.Forms.CheckState.Unchecked Then
                        lstrVlr = Math.Round(ldblVlr, 0)
                        'lstrVlr = Replace(Replace(VB6.Format(ldblVlr * 100, "###,###0"), ".", ""), ",", "")
                    Else
                        lstrVlr = Replace(Replace(VB6.Format(ldblVlr, "#,##0.00"), ".", ""), ",", VB6.GetItemString(Me.cmbCaracter, Me.cmbCaracter.SelectedIndex))
                    End If


                    If Me.chkExportaDescritivo.CheckState = System.Windows.Forms.CheckState.Unchecked Then
                        lstrLin = lintFluxo & ";" & lintModalidade & ";" & lintCaracteristica & ";" & lintDetalhe & ";" & lstrVlr
                    Else
                        lstrLin = ""
                        lstrLin = lstrLin & lintFluxo & ";" & GetExpFluxosBalancoEnergetico(lintFluxo) & ";"
                        lstrLin = lstrLin & lintModalidade & ";" & GetExpModalidadesBalancoEnergetico(lintModalidade) & ";"
                        lstrLin = lstrLin & lintCaracteristica & ";" & GetExpCaracteristicasBalancoEnergetico(lintCaracteristica) & ";"
                        lstrLin = lstrLin & lintDetalhe & ";" & GetExpDetalheBalancoEnergetico(lintDetalhe) & ";" & lstrVlr
                    End If

                    PrintLine(lintArqBE, lstrLin)


                    Call BarraProgresso((Me.ProgressBarArq), (Me.lblPercArq), (Me.lblInfoArq), 1,  , " Exportando Balanço Energetico ")
                Next

                FileClose(lintArqBE)

                Me.Cursor = System.Windows.Forms.Cursors.Default
                Call LigDes(True)

                Me.lblInfoArq.Text = Me.lblInfoArq.Text & " *** Finalizado **** "
                Call MsgBox("Arquivos gerados com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
            Else
                Me.Cursor = System.Windows.Forms.Cursors.Default
                Call MsgBox("Não foram encontrados dados para serem exportados!", MsgBoxStyle.Information, gstrTituloMensagem)
            End If

            dt = Nothing
            dt1 = Nothing

        Catch ex As Exception

            Dim numRows As Integer = arquivos.GetLength(0)
            Dim numCols As Integer = arquivos.GetLength(1)

            For i As Integer = 0 To numRows - 1
                For j As Integer = 0 To numCols - 1
                    FileClose(arquivos(i, j))
                Next
            Next

            FileClose(lintArqBE)

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

    Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click

        Call Me.Close()
    End Sub

    Private Sub cmdImprimir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdImprimir.Click
        Try

            If txt_Energ_Inj_Enel.Text = "" Then
                MessageBox.Show(" Antes de gerar o SAMP, é necessário que seja preenchido o campo Energia Injetada Enel, para o calculo do Balanço Energetico!", "Falta de Informações", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txt_Energ_Inj_Enel.Focus()
                Exit Sub
            Else
                ' Para não mexer muito no Samp, resolvi fazer um metodo novo para o Balanço Energetico'
                Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
                Call LigDes(False)
                Me.lblInfo.Text = " Buscando Balanço Energetico aguarde..."
                Call Me.lblInfo.Refresh()

                Dim ldteFim = CDate(UltimoDiaMes(Month(Me.dtpMesAno.Value), Year(Me.dtpMesAno.Value)) & "/" & VB6.Format(Me.dtpMesAno.Value, "MM/yyyy"))
                Dim ldteIni = CDate("01/" & VB6.Format(ldteFim, "MM/yyyy"))

                If Exporta_Grava_Balanco_Energetico(ldteFim, ldteIni) = False Then
                    Exit Sub
                End If
            End If

            Call CriaTabelaTemporaria()

            If MontaTabela() Then

                Call sDadosEmpresa(LImagem, sNomeEmpresa, sEnderecoEmpresa, sCepEmpresa, sCidadeEmpresa, sUFempresa, sBairroEmpresa, sFoneEmpresa, sWebEmpresa)
                Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
                Call LigDes(False)
                Call sDeletaTabelasAuxiliares()

                If Me.chkGrupoBx.CheckState = System.Windows.Forms.CheckState.Checked Then
                    Call GeraImpressaoBaixaRenda()
                    Call lPrint("rptRelSampNovoBaixaRenda")
                End If
                If Me.chkGrupoB.CheckState = System.Windows.Forms.CheckState.Checked Then
                    Call GeraImpressaoGrupoB()
                    Call lPrint("rptRelSampNovoGrupoB")
                End If
                If Me.chkMicroGeracao.CheckState = System.Windows.Forms.CheckState.Checked Then
                    Call GeraImpressaoGrupoBMicroGeracao()
                    Call lPrint("rptRelSampNovoGrupoBMicroGeracao")
                End If
                If Me.chkIrrigante.CheckState = System.Windows.Forms.CheckState.Checked Then
                    Call GeraImpressaoGrupoBIrrigacaoAquicultura()
                    Call lPrint("rptRelSampNovoGrupoBIrrigacaoAquicultura")
                End If
                If Me.chkTarifaBranca.CheckState = System.Windows.Forms.CheckState.Checked Then
                    Call GeraImpressaoGrupoBTarifaBranca()
                    Call lPrint("rptRelSampNovoGrupoBTarifaBranca")
                End If
                If Me.chkUsina.CheckState = System.Windows.Forms.CheckState.Checked Then
                    Call GeraImpressaoUsinas()
                    Call lPrint("rptRelSampNovoUsinas")
                End If
                If Me.chkGrupoAConvencional.CheckState = System.Windows.Forms.CheckState.Checked Then
                    Call GeraImpressaoGrupoAConvencional()
                    Call lPrint("rptRelSampNovoGrupoAConvencional")
                End If
                If Me.chkGrupoAHorosazonal.CheckState = System.Windows.Forms.CheckState.Checked Then
                    Call GeraImpressaoGrupoAHorossazonal()
                    Call lPrint("rptRelSampNovoGrupoAHorossazonal")
                End If
                If Me.chkGrupoALivre.CheckState = System.Windows.Forms.CheckState.Checked Then
                    Call GeraImpressaoGrupoALivre()
                    Call lPrint("rptRelSampNovoGrupoALivre")
                End If
                '
                Call GravaExportacao()

                Call LigDes(True)
                Me.Cursor = System.Windows.Forms.Cursors.Default
            End If
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdImprimir_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub GeraImpressaoBaixaRenda()
        Dim lstrTabAux As String
        Dim lstrTabTit As String
        Dim lstrTamCol As String
        Dim lintRefatu As Short
        Dim lintOrdCla As Short
        Dim lstrDesCla As String
        '
        Dim ldblQtdCon As Double
        'Consumos
        Dim ldblQtdkWh As Double
        Dim ldblVlrkWh As Double
        Dim ldblVlrBan As Double
        Dim ldblVlrRec As Double
        'Reativos
        Dim ldblQtdRev As Double
        Dim ldblVlrRev As Double
        'Tributos
        Dim ldblVlrIcm As Double
        Dim ldblVlrPis As Double
        Dim ldblVlrCof As Double
        'Totais
        Dim ldblTotQtdCon As Double
        'Consumos
        Dim ldblTotQtdkWh As Double
        Dim ldblTotVlrkWh As Double
        Dim ldblTotVlrBan As Double
        Dim ldblTotVlrRec As Double
        'Reativos
        Dim ldblTotQtdRev As Double
        Dim ldblTotVlrRev As Double
        'Tributos
        Dim ldblTotVlrIcm As Double
        Dim ldblTotVlrPis As Double
        Dim ldblTotVlrCof As Double
        'Auxiliares
        Dim lintAux As Short
        Dim lstrSql As String
        Try

            For lintRefatu = 0 To 1
                If lintRefatu = 1 Then
                    For lintAux = 1 To 3
                    Next
                End If
                '
                lstrSql = ""
                lstrSql = lstrSql & "  SELECT `ord_classe`, `ds_classe`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`faixa`) `ncons`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`consumo`) `qkwh_fat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`valor`) `vkwh_fat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`bandeira`) `vkwh_fat_ban`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`receita`) `vreceita`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`icms`) `icms`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`pis`) `pis`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`cofins`) `cofins` " & vbCrLf
                lstrSql = lstrSql & "    FROM " & istrTab945 & " " & vbCrLf
                lstrSql = lstrSql & "   WHERE `refaturado` = " & lintRefatu & " " & vbCrLf
                lstrSql = lstrSql & "GROUP BY `ord_classe`, `ds_classe` " & vbCrLf
                lstrSql = lstrSql & "ORDER BY `ord_classe` ASC, `ds_classe` ASC "
                objAtualizaBancoDados.QUERY_NAME = lstrSql
                dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

                If dt.Rows.Count > 0 Then
                    For Each row As DataRow In dt.Rows

                        lintOrdCla = CShort(IIf(IsDBNull(row.Item("ord_classe")), 0, row.Item("ord_classe")))
                        lstrDesCla = CStr(IIf(IsDBNull(row.Item("ds_classe")), "", row.Item("ds_classe")))
                        ldblQtdCon = CDbl(IIf(IsDBNull(row.Item("ncons")), 0, row.Item("ncons")))
                        ldblQtdkWh = CDbl(IIf(IsDBNull(row.Item("qkwh_fat")), 0, row.Item("qkwh_fat")))
                        ldblVlrkWh = CDbl(IIf(IsDBNull(row.Item("vkwh_fat")), 0, row.Item("vkwh_fat")))
                        ldblVlrBan = CDbl(IIf(IsDBNull(row.Item("vkwh_fat_ban")), 0, row.Item("vkwh_fat_ban")))
                        ldblVlrRec = CDbl(IIf(IsDBNull(row.Item("vreceita")), 0, row.Item("vreceita")))
                        ldblVlrIcm = CDbl(IIf(IsDBNull(row.Item("icms")), 0, row.Item("icms")))
                        ldblVlrPis = CDbl(IIf(IsDBNull(row.Item("pis")), 0, row.Item("pis")))
                        ldblVlrCof = CDbl(IIf(IsDBNull(row.Item("cofins")), 0, row.Item("cofins")))


                        lstrSql = "INSERT INTO AUX_SAMP_NOVO_BAIXA_RENDA SET "
                        lstrSql = lstrSql & "CLASSE = '" & lstrDesCla & "' ,"
                        lstrSql = lstrSql & "NCONS = '" & ldblQtdCon & "',"
                        lstrSql = lstrSql & "KWH = '" & ldblQtdkWh & "',"
                        lstrSql = lstrSql & "KWHRS = '" & ldblVlrkWh & "',"
                        lstrSql = lstrSql & "BANDEIRARS = '" & ldblVlrBan & "',"
                        lstrSql = lstrSql & "RECEITA = '" & ldblVlrRec & "',"
                        lstrSql = lstrSql & "REVERSO = '" & ldblQtdRev & "',"
                        lstrSql = lstrSql & "REVRS = '" & ldblVlrRev & "',"
                        lstrSql = lstrSql & "ICMS = '" & ldblVlrIcm & "',"
                        lstrSql = lstrSql & "PIS = '" & ldblVlrPis & "',"
                        lstrSql = lstrSql & "COFINS = '" & ldblVlrCof & "',"
                        lstrSql = lstrSql & "REFATURAMENTO = '" & lintRefatu & "'"
                        objExecAtualizaBancoDados.QUERY_NAME = lstrSql
                        repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

                    Next

                    ' Else

                End If

            Next lintRefatu

            dt = Nothing

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

    Private Sub GeraImpressaoGrupoB()
        Dim lstrTabAux As String
        Dim lstrTabTit As String
        Dim lstrTamCol As String
        Dim lintRefatu As Short
        Dim lintOrdCla As Short
        Dim lstrDesCla As String
        '
        Dim ldblQtdCon As Double
        'Consumos
        Dim ldblQtdkWh As Double
        Dim ldblVlrkWh As Double
        Dim ldblVlrBan As Double
        Dim ldblVlrRec As Double
        'Reativos
        Dim ldblQtdRev As Double
        Dim ldblVlrRev As Double
        'Tributos
        Dim ldblVlrIcm As Double
        Dim ldblVlrPis As Double
        Dim ldblVlrCof As Double
        'Totais
        Dim ldblTotQtdCon(2) As Double
        'Consumos
        Dim ldblTotQtdkWh(2) As Double
        Dim ldblTotVlrkWh(2) As Double
        Dim ldblTotVlrBan(2) As Double
        Dim ldblTotVlrRec(2) As Double
        'Reativos
        Dim ldblTotQtdRev(2) As Double
        Dim ldblTotVlrRev(2) As Double
        'Tributos
        Dim ldblTotVlrIcm(2) As Double
        Dim ldblTotVlrPis(2) As Double
        Dim ldblTotVlrCof(2) As Double
        'Auxiliares
        Dim lblnSubTot As Boolean
        Dim lintTotAux As Short
        Dim lintAux As Short
        Dim lstrSql As String

        Try

            For lintRefatu = 0 To 1
                lblnSubTot = True
                If lintRefatu = 1 Then
                    For lintAux = 1 To 3
                        ' ifrmRelSamp.VS.Paragraph = ""
                    Next
                End If
                For lintTotAux = 0 To 2
                    ldblTotQtdCon(lintTotAux) = 0
                    ldblTotQtdkWh(lintTotAux) = 0
                    ldblTotVlrkWh(lintTotAux) = 0
                    ldblTotVlrBan(lintTotAux) = 0
                    ldblTotVlrRec(lintTotAux) = 0
                    ldblTotQtdRev(lintTotAux) = 0
                    ldblTotVlrRev(lintTotAux) = 0
                    ldblTotVlrIcm(lintTotAux) = 0
                    ldblTotVlrPis(lintTotAux) = 0
                    ldblTotVlrCof(lintTotAux) = 0
                Next lintTotAux
                '
                lstrSql = ""
                lstrSql = lstrSql & "  SELECT " & vbCrLf
                lstrSql = lstrSql & "        `ord_classe`, " & vbCrLf
                lstrSql = lstrSql & "        `ds_classe`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(if(`qkwh_fat` < 0, 0, 1)) `ncons`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`qkwh_fat`) `qkwh_fat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkwh_fat`) `vkwh_fat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkwh_fat_ban`) `vkwh_fat_ban`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`receita_fp`) `vreceita`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`qkwh_reverso`) `qkwh_reverso`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkwh_reverso`) `vkwh_reverso`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`icms`) `icms`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`pis`) `pis`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`cofins`) `cofins` " & vbCrLf
                lstrSql = lstrSql & "    FROM " & istrTabela & " " & vbCrLf
                lstrSql = lstrSql & "   WHERE `grupo` = 'B' " & vbCrLf
                lstrSql = lstrSql & "     AND `refaturado` = " & lintRefatu & " " & vbCrLf
                lstrSql = lstrSql & "     AND `usinas` <> 1 " & vbCrLf
                lstrSql = lstrSql & "     AND `irrigacao_aquicultura` <> 1 " & vbCrLf
                lstrSql = lstrSql & "     AND `tarifa_branca` <> 1 " & vbCrLf
                lstrSql = lstrSql & "GROUP BY `ord_classe`, `ds_classe` " & vbCrLf
                lstrSql = lstrSql & "ORDER BY `ord_classe` ASC, `ds_classe` ASC "
                objAtualizaBancoDados.QUERY_NAME = lstrSql
                dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

                If dt.Rows.Count > 0 Then
                    For Each row As DataRow In dt.Rows

                        lintOrdCla = CShort(IIf(IsDBNull(row.Item("ord_classe")), 0, row.Item("ord_classe")))
                        lstrDesCla = CStr(IIf(IsDBNull(row.Item("ds_classe")), "", row.Item("ds_classe")))
                        ldblQtdCon = CDbl(IIf(IsDBNull(row.Item("ncons")), 0, row.Item("ncons")))
                        ldblQtdkWh = CDbl(IIf(IsDBNull(row.Item("qkwh_fat")), 0, row.Item("qkwh_fat")))
                        ldblVlrkWh = CDbl(IIf(IsDBNull(row.Item("vkwh_fat")), 0, row.Item("vkwh_fat")))
                        ldblVlrBan = CDbl(IIf(IsDBNull(row.Item("vkwh_fat_ban")), 0, row.Item("vkwh_fat_ban")))
                        ldblVlrRec = CDbl(IIf(IsDBNull(row.Item("vreceita")), 0, row.Item("vreceita")))
                        ldblQtdRev = CDbl(IIf(IsDBNull(row.Item("qkwh_reverso")), 0, row.Item("qkwh_reverso")))
                        ldblVlrRev = CDbl(IIf(IsDBNull(row.Item("vkwh_reverso")), 0, row.Item("vkwh_reverso")))
                        ldblVlrIcm = CDbl(IIf(IsDBNull(row.Item("icms")), 0, row.Item("icms")))
                        ldblVlrPis = CDbl(IIf(IsDBNull(row.Item("pis")), 0, row.Item("pis")))
                        ldblVlrCof = CDbl(IIf(IsDBNull(row.Item("cofins")), 0, row.Item("cofins")))

                        If lintOrdCla = 10 Then
                            lintTotAux = 1
                        ElseIf lintOrdCla >= 11 And lintOrdCla < 30 Then
                            lintTotAux = 0
                        Else
                            lintTotAux = 2
                        End If

                        ldblTotQtdCon(lintTotAux) = ldblTotQtdCon(lintTotAux) + ldblQtdCon
                        ldblTotQtdkWh(lintTotAux) = ldblTotQtdkWh(lintTotAux) + ldblQtdkWh
                        ldblTotVlrkWh(lintTotAux) = ldblTotVlrkWh(lintTotAux) + ldblVlrkWh
                        ldblTotVlrBan(lintTotAux) = ldblTotVlrBan(lintTotAux) + ldblVlrBan
                        ldblTotVlrRec(lintTotAux) = ldblTotVlrRec(lintTotAux) + ldblVlrRec
                        ldblTotQtdRev(lintTotAux) = ldblTotQtdRev(lintTotAux) + ldblQtdRev
                        ldblTotVlrRev(lintTotAux) = ldblTotVlrRev(lintTotAux) + ldblVlrRev
                        ldblTotVlrIcm(lintTotAux) = ldblTotVlrIcm(lintTotAux) + ldblVlrIcm
                        ldblTotVlrPis(lintTotAux) = ldblTotVlrPis(lintTotAux) + ldblVlrPis
                        ldblTotVlrCof(lintTotAux) = ldblTotVlrCof(lintTotAux) + ldblVlrCof

                        'Subtotais
                        If lintOrdCla >= 30 And lblnSubTot Then

                            lstrDesCla = "Total Bx.Renda"
                            ldblQtdCon = VB6.Format(ldblTotQtdCon(0), "###,###,###,##0")
                            ldblQtdkWh = VB6.Format(ldblTotQtdkWh(0), "#,###0.000")
                            ldblVlrkWh = VB6.Format(ldblTotVlrkWh(0), "#,##0.00")
                            ldblVlrBan = VB6.Format(ldblTotVlrBan(0), "#,##0.00")
                            ldblVlrRec = VB6.Format(ldblTotVlrRec(0), "#,##0.00")
                            ldblQtdRev = VB6.Format(ldblTotQtdRev(0), "#,###0.000")
                            ldblVlrRev = VB6.Format(ldblTotVlrRev(0), "#,##0.00")
                            ldblVlrIcm = VB6.Format(ldblTotVlrIcm(0), "#,##0.00")
                            ldblVlrPis = VB6.Format(ldblTotVlrPis(0), "#,##0.00")
                            ldblVlrCof = VB6.Format(ldblTotVlrCof(0), "#,##0.00")

                            lstrSql = "INSERT INTO AUX_SAMP_NOVO_IMPRESSAOB SET "
                            lstrSql = lstrSql & "CLASSE = '" & lstrDesCla & "' ,"
                            lstrSql = lstrSql & "NCONS = '" & ldblQtdCon & "',"
                            lstrSql = lstrSql & "KWH = '" & ldblQtdkWh & "',"
                            lstrSql = lstrSql & "KWHRS = '" & ldblVlrkWh & "',"
                            lstrSql = lstrSql & "BANDEIRARS = '" & ldblVlrBan & "',"
                            lstrSql = lstrSql & "RECEITA = '" & ldblVlrRec & "',"
                            lstrSql = lstrSql & "REVERSO = '" & ldblQtdRev & "',"
                            lstrSql = lstrSql & "REVRS = '" & ldblVlrRev & "',"
                            lstrSql = lstrSql & "ICMS = '" & ldblVlrIcm & "',"
                            lstrSql = lstrSql & "PIS = '" & ldblVlrPis & "',"
                            lstrSql = lstrSql & "COFINS = '" & ldblVlrCof & "',"
                            lstrSql = lstrSql & "REFATURAMENTO = '" & lintRefatu & "'"
                            objExecAtualizaBancoDados.QUERY_NAME = lstrSql
                            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)


                            lstrDesCla = "Total Residencial"
                            ldblQtdCon = VB6.Format(ldblTotQtdCon(0) + ldblTotQtdCon(1), "###,###,###,##0")
                            ldblQtdkWh = VB6.Format(ldblTotQtdkWh(0) + ldblTotQtdkWh(1), "#,###0.000")
                            ldblVlrkWh = VB6.Format(ldblTotVlrkWh(0) + ldblTotVlrkWh(1), "#,##0.00")
                            ldblVlrBan = VB6.Format(ldblTotVlrBan(0) + ldblTotVlrBan(1), "#,##0.00")
                            ldblVlrRec = VB6.Format(ldblTotVlrRec(0) + ldblTotVlrRec(1), "#,##0.00")
                            ldblQtdRev = VB6.Format(ldblTotQtdRev(0) + ldblTotQtdRev(1), "#,###0.000")
                            ldblVlrRev = VB6.Format(ldblTotVlrRev(0) + ldblTotVlrRev(1), "#,##0.00")                        'lstrTabAux = lstrTabAux & VB6.Format(ldblTotVlrIcm(0) + ldblTotVlrIcm(1), "#,##0.00") & "|"
                            ldblVlrPis = VB6.Format(ldblTotVlrPis(0) + ldblTotVlrPis(1), "#,##0.00")
                            ldblVlrCof = VB6.Format(ldblTotVlrCof(0) + ldblTotVlrCof(1), "#,##0.00")

                            lstrSql = "INSERT INTO AUX_SAMP_NOVO_IMPRESSAOB SET "
                            lstrSql = lstrSql & "CLASSE = '" & lstrDesCla & "' ,"
                            lstrSql = lstrSql & "NCONS = '" & ldblQtdCon & "',"
                            lstrSql = lstrSql & "KWH = '" & ldblQtdkWh & "',"
                            lstrSql = lstrSql & "KWHRS = '" & ldblVlrkWh & "',"
                            lstrSql = lstrSql & "BANDEIRARS = '" & ldblVlrBan & "',"
                            lstrSql = lstrSql & "RECEITA = '" & ldblVlrRec & "',"
                            lstrSql = lstrSql & "REVERSO = '" & ldblQtdRev & "',"
                            lstrSql = lstrSql & "REVRS = '" & ldblVlrRev & "',"
                            lstrSql = lstrSql & "ICMS = '" & ldblVlrIcm & "',"
                            lstrSql = lstrSql & "PIS = '" & ldblVlrPis & "',"
                            lstrSql = lstrSql & "COFINS = '" & ldblVlrCof & "',"
                            lstrSql = lstrSql & "REFATURAMENTO = '" & lintRefatu & "'"
                            objExecAtualizaBancoDados.QUERY_NAME = lstrSql
                            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

                            lblnSubTot = False

                        End If

                        lstrSql = "INSERT INTO AUX_SAMP_NOVO_IMPRESSAOB SET "
                        lstrSql = lstrSql & "CLASSE = '" & lstrDesCla & "' ,"
                        lstrSql = lstrSql & "NCONS = '" & ldblQtdCon & "',"
                        lstrSql = lstrSql & "KWH = '" & ldblQtdkWh & "',"
                        lstrSql = lstrSql & "KWHRS = '" & ldblVlrkWh & "',"
                        lstrSql = lstrSql & "BANDEIRARS = '" & ldblVlrBan & "',"
                        lstrSql = lstrSql & "RECEITA = '" & ldblVlrRec & "',"
                        lstrSql = lstrSql & "REVERSO = '" & ldblQtdRev & "',"
                        lstrSql = lstrSql & "REVRS = '" & ldblVlrRev & "',"
                        lstrSql = lstrSql & "ICMS = '" & ldblVlrIcm & "',"
                        lstrSql = lstrSql & "PIS = '" & ldblVlrPis & "',"
                        lstrSql = lstrSql & "COFINS = '" & ldblVlrCof & "',"
                        lstrSql = lstrSql & "REFATURAMENTO = '" & lintRefatu & "'"
                        objExecAtualizaBancoDados.QUERY_NAME = lstrSql
                        repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

                    Next

                    'Else

                End If

            Next lintRefatu

            dt = Nothing

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

    End Sub

    Private Sub GeraImpressaoGrupoBMicroGeracao()
        Dim lstrTabAux As String
        Dim lstrTabTit As String
        Dim lstrTamCol As String
        Dim lintRefatu As Short
        Dim lintOrdCla As Short
        Dim lstrDesCla As String
        '
        Dim ldblQtdCon As Double
        'Consumos
        Dim ldblQtdkWhLido As Double
        Dim ldblQtdkWh As Double
        Dim ldblVlrkWh As Double
        Dim ldblVlrBan As Double
        'Reativos
        Dim ldblQtdRev As Double
        Dim ldblVlrRev As Double
        'Tributos
        Dim ldblVlrIcm As Double
        Dim ldblVlrPis As Double
        Dim ldblVlrCof As Double
        '
        Dim ldblVlrRec As Double
        'Totais
        Dim ldblTotQtdCon(2) As Double
        'Consumos
        Dim ldblTotQtdkWhLido(2) As Double
        Dim ldblTotQtdkWh(2) As Double
        Dim ldblTotVlrkWh(2) As Double
        Dim ldblTotVlrBan(2) As Double
        'Reativos
        Dim ldblTotQtdRev(2) As Double
        Dim ldblTotVlrRev(2) As Double
        'Tributos
        Dim ldblTotVlrIcm(2) As Double
        Dim ldblTotVlrPis(2) As Double
        Dim ldblTotVlrCof(2) As Double
        '
        Dim ldblTotReceita(2) As Double
        'Auxiliares
        Dim lblnSubTot As Boolean
        Dim lintTotAux As Short
        Dim lintAux As Short
        Dim lstrSql As String

        Try

            For lintRefatu = 0 To 1
                lblnSubTot = True
                If lintRefatu = 1 Then
                    For lintAux = 1 To 3
                        ' ifrmRelSamp.VS.Paragraph = ""
                    Next
                End If
                '
                lstrSql = ""
                lstrSql = lstrSql & "  SELECT " & vbCrLf
                lstrSql = lstrSql & "        `ord_classe`, " & vbCrLf
                lstrSql = lstrSql & "        `ds_classe`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(if(qkwh_fat<0,0,1)) `ncons`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(qkwh_cons) `qkwh_lid`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(qkwh_fat) `qkwh_fat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(vkwh_fat) `vkwh_fat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(vkwh_fat_ban) `vkwh_fat_ban`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(receita_fp) `vreceita`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(qkwh_reverso) `qkwh_reverso`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(vkwh_reverso) `vkwh_reverso`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(icms) `icms`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(pis) `pis`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(cofins) `cofins` " & vbCrLf
                lstrSql = lstrSql & "    FROM " & istrTabela & " " & vbCrLf
                lstrSql = lstrSql & "   WHERE `grupo` = 'B' " & vbCrLf
                lstrSql = lstrSql & "     AND `refaturado` = " & lintRefatu & " " & vbCrLf
                lstrSql = lstrSql & "     AND `micro_geracao` = 1 " & vbCrLf
                lstrSql = lstrSql & "GROUP BY `ord_classe`, `ds_classe` " & vbCrLf
                lstrSql = lstrSql & "ORDER BY `ord_classe` ASC, `ds_classe` ASC "
                objAtualizaBancoDados.QUERY_NAME = lstrSql
                dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)
                'Call Conectar(lstrSql, lrstAux)

                If dt.Rows.Count > 0 Then
                    For Each row As DataRow In dt.Rows

                        lintOrdCla = CShort(IIf(IsDBNull(row.Item("ord_classe")), 0, row.Item("ord_classe")))
                        lstrDesCla = CStr(IIf(IsDBNull(row.Item("ds_classe")), "", row.Item("ds_classe")))
                        ldblQtdCon = CDbl(IIf(IsDBNull(row.Item("ncons")), 0, row.Item("ncons")))
                        ldblQtdkWhLido = CDbl(IIf(IsDBNull(row.Item("qkwh_lid")), 0, row.Item("qkwh_lid")))
                        ldblQtdkWh = CDbl(IIf(IsDBNull(row.Item("qkwh_fat")), 0, row.Item("qkwh_fat")))
                        ldblVlrkWh = CDbl(IIf(IsDBNull(row.Item("vkwh_fat")), 0, row.Item("vkwh_fat")))
                        ldblVlrBan = CDbl(IIf(IsDBNull(row.Item("vkwh_fat_ban")), 0, row.Item("vkwh_fat_ban")))
                        ldblVlrRec = CDbl(IIf(IsDBNull(row.Item("vreceita")), 0, row.Item("vreceita")))
                        ldblQtdRev = CDbl(IIf(IsDBNull(row.Item("qkwh_reverso")), 0, row.Item("qkwh_reverso")))
                        ldblVlrRev = CDbl(IIf(IsDBNull(row.Item("vkwh_reverso")), 0, row.Item("vkwh_reverso")))
                        ldblVlrIcm = CDbl(IIf(IsDBNull(row.Item("icms")), 0, row.Item("icms")))
                        ldblVlrPis = CDbl(IIf(IsDBNull(row.Item("pis")), 0, row.Item("pis")))
                        ldblVlrCof = CDbl(IIf(IsDBNull(row.Item("cofins")), 0, row.Item("cofins")))
                        If lintOrdCla = 10 Then
                            lintTotAux = 1
                        ElseIf lintOrdCla >= 20 And lintOrdCla < 30 Then
                            lintTotAux = 0
                        Else
                            lintTotAux = 2
                        End If

                        ldblTotQtdCon(lintTotAux) = ldblTotQtdCon(lintTotAux) + ldblQtdCon
                        ldblTotQtdkWhLido(lintTotAux) = ldblTotQtdkWhLido(lintTotAux) + ldblQtdkWhLido
                        ldblTotQtdkWh(lintTotAux) = ldblTotQtdkWh(lintTotAux) + ldblQtdkWh
                        ldblTotVlrkWh(lintTotAux) = ldblTotVlrkWh(lintTotAux) + ldblVlrkWh
                        ldblTotVlrBan(lintTotAux) = ldblTotVlrBan(lintTotAux) + ldblVlrBan
                        ldblTotQtdRev(lintTotAux) = ldblTotQtdRev(lintTotAux) + ldblQtdRev
                        ldblTotVlrRev(lintTotAux) = ldblTotVlrRev(lintTotAux) + ldblVlrRev
                        ldblTotReceita(lintTotAux) = ldblTotReceita(lintTotAux) + ldblVlrRec
                        ldblTotVlrIcm(lintTotAux) = ldblTotVlrIcm(lintTotAux) + ldblVlrIcm
                        ldblTotVlrPis(lintTotAux) = ldblTotVlrPis(lintTotAux) + ldblVlrPis
                        ldblTotVlrCof(lintTotAux) = ldblTotVlrCof(lintTotAux) + ldblVlrCof

                        'Subtotais
                        If lintOrdCla >= 30 And lblnSubTot Then
                            lstrDesCla = "Total Bx.Renda"

                            lstrSql = "INSERT INTO AUX_SAMP_NOVO_IMPRESSAOB_MG SET "
                            lstrSql = lstrSql & "CLASSE = '" & lstrDesCla & "' ,"
                            lstrSql = lstrSql & "NCONS = '" & ldblTotQtdCon(0) & "',"
                            lstrSql = lstrSql & "KWHLIDO = '" & ldblTotQtdkWhLido(0) & "',"
                            lstrSql = lstrSql & "KWHFAT = '" & ldblTotQtdkWh(0) & "',"
                            lstrSql = lstrSql & "KWHRS = '" & ldblTotVlrkWh(0) & "',"
                            lstrSql = lstrSql & "BANDEIRARS = '" & ldblTotVlrBan(0) & "',"
                            lstrSql = lstrSql & "RECEITA = '" & ldblTotReceita(0) & "',"
                            lstrSql = lstrSql & "REVERSO = '" & ldblTotQtdRev(0) & "',"
                            lstrSql = lstrSql & "REVERSOKWH = '" & ldblTotVlrRev(0) & "',"
                            lstrSql = lstrSql & "RECEITAREV = '" & ldblQtdRev & "',"
                            lstrSql = lstrSql & "ICMS = '" & ldblTotVlrIcm(0) & "',"
                            lstrSql = lstrSql & "PIS = '" & ldblTotVlrPis(0) & "',"
                            lstrSql = lstrSql & "COFINS = '" & ldblTotVlrCof(0) & "',"
                            lstrSql = lstrSql & "REFATURAMENTO = '" & lintRefatu & "'"
                            objExecAtualizaBancoDados.QUERY_NAME = lstrSql
                            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

                            lstrDesCla = "Total Residencial"

                            lstrSql = "INSERT INTO AUX_SAMP_NOVO_IMPRESSAOB_MG SET "
                            lstrSql = lstrSql & "CLASSE = '" & lstrDesCla & "' ,"
                            lstrSql = lstrSql & "NCONS = '" & ldblTotQtdCon(0) + ldblTotQtdCon(1) & "',"
                            lstrSql = lstrSql & "KWHLIDO = '" & ldblTotQtdkWhLido(0) + ldblTotQtdkWhLido(1) & "',"
                            lstrSql = lstrSql & "KWHFAT = '" & ldblTotQtdkWh(0) + ldblTotQtdkWh(1) & "',"
                            lstrSql = lstrSql & "KWHRS = '" & ldblTotVlrkWh(0) + ldblTotVlrkWh(1) & "',"
                            lstrSql = lstrSql & "BANDEIRARS = '" & ldblTotVlrBan(0) + ldblTotVlrBan(1) & "',"
                            lstrSql = lstrSql & "RECEITA = '" & ldblTotReceita(0) + ldblTotReceita(1) & "',"
                            lstrSql = lstrSql & "REVERSO = '" & ldblTotQtdRev(0) + ldblTotQtdRev(1) & "',"
                            lstrSql = lstrSql & "REVERSOKWH = '" & ldblTotVlrRev(0) + ldblTotVlrRev(1) & "',"
                            lstrSql = lstrSql & "RECEITAREV = '" & ldblQtdRev & "',"
                            lstrSql = lstrSql & "ICMS = '" & ldblTotVlrIcm(0) + ldblTotVlrIcm(1) & "',"
                            lstrSql = lstrSql & "PIS = '" & ldblTotVlrPis(0) + ldblTotVlrPis(1) & "',"
                            lstrSql = lstrSql & "COFINS = '" & ldblTotVlrCof(0) + ldblTotVlrCof(1) & "',"
                            lstrSql = lstrSql & "REFATURAMENTO = '" & lintRefatu & "'"
                            objExecAtualizaBancoDados.QUERY_NAME = lstrSql
                            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

                            lblnSubTot = False
                        End If


                        lstrSql = "INSERT INTO AUX_SAMP_NOVO_IMPRESSAOB_MG SET "
                        lstrSql = lstrSql & "CLASSE = '" & lstrDesCla & "' ,"
                        lstrSql = lstrSql & "NCONS = '" & ldblQtdCon & "',"
                        lstrSql = lstrSql & "KWHLIDO = '" & ldblQtdkWh & "',"
                        lstrSql = lstrSql & "KWHFAT = '" & ldblVlrkWh & "',"
                        lstrSql = lstrSql & "KWHRS = '" & ldblVlrkWh & "',"
                        lstrSql = lstrSql & "BANDEIRARS = '" & ldblVlrkWh & "',"
                        lstrSql = lstrSql & "RECEITA = '" & ldblVlrBan & "',"
                        lstrSql = lstrSql & "REVERSO = '" & ldblVlrRec & "',"
                        lstrSql = lstrSql & "REVERSOKWH = '" & ldblQtdRev & "',"
                        lstrSql = lstrSql & "RECEITAREV = '" & ldblQtdRev & "',"
                        lstrSql = lstrSql & "ICMS = '" & ldblVlrIcm & "',"
                        lstrSql = lstrSql & "PIS = '" & ldblVlrPis & "',"
                        lstrSql = lstrSql & "COFINS = '" & ldblVlrCof & "',"
                        lstrSql = lstrSql & "REFATURAMENTO = '" & lintRefatu & "'"
                        objExecAtualizaBancoDados.QUERY_NAME = lstrSql
                        repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

                    Next

                    'Else

                End If

            Next lintRefatu

            dt = Nothing

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

    Private Sub GeraImpressaoGrupoBIrrigacaoAquicultura()
        Dim lstrTabAux As String
        Dim lstrTabTit As String
        Dim lstrTamCol As String
        Dim lintRefatu As Short
        Dim lintOrdCla As Short
        Dim lstrDesCla As String
        '
        Dim ldblQtdCon As Double
        'Consumos
        Dim ldblQtdkWh As Double
        Dim ldblVlrkWh As Double
        Dim ldblVlrBan As Double
        '
        Dim ldblQtdkWh_n As Double
        Dim ldblVlrkWh_n As Double
        Dim ldblVlrBan_n As Double
        Dim ldblQtdkWh_r As Double
        Dim ldblVlrkWh_r As Double
        Dim ldblVlrBan_r As Double
        'Tributos
        Dim ldblVlrIcm As Double
        Dim ldblVlrPis As Double
        Dim ldblVlrCof As Double
        '
        'Totais
        Dim ldblTotQtdCon(2) As Double
        'Consumos
        Dim ldblTotQtdkWh(2) As Double
        Dim ldblTotVlrkWh(2) As Double
        Dim ldblTotVlrBan(2) As Double
        '
        Dim ldblTotQtdkWh_n(2) As Double
        Dim ldblTotVlrkWh_n(2) As Double
        Dim ldblTotVlrBan_n(2) As Double
        Dim ldblTotQtdkWh_r(2) As Double
        Dim ldblTotVlrkWh_r(2) As Double
        Dim ldblTotVlrBan_r(2) As Double
        'Tributos
        Dim ldblTotVlrIcm(2) As Double
        Dim ldblTotVlrPis(2) As Double
        Dim ldblTotVlrCof(2) As Double
        '
        'Auxiliares
        Dim lblnSubTot As Boolean
        Dim lintTotAux As Short
        Dim lintAux As Short
        Dim lstrSql As String

        Try

            For lintRefatu = 0 To 1
                lblnSubTot = True
                If lintRefatu = 1 Then
                    For lintAux = 1 To 3
                        ' ifrmRelSamp.VS.Paragraph = ""
                    Next
                End If
                '
                lstrSql = ""
                lstrSql = lstrSql & "  SELECT " & vbCrLf
                lstrSql = lstrSql & "        `ord_classe`, " & vbCrLf
                lstrSql = lstrSql & "        `ds_classe`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(if(qkwh_fat<0,0,1)) `ncons`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(qkwh_fat) `qkwh_fat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(vkwh_fat) `vkwh_fat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(vkwh_fat_ban) `vkwh_fat_ban`, " & vbCrLf
                '
                lstrSql = lstrSql & "        SUM(qirr_nor_kwh_fat) `qkwh_n_fat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(qirr_res_kwh_fat) `qkwh_r_fat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(virr_nor_kwh_fat) `vkwh_n_fat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(virr_nor_kwh_fat_ban) `vkwh_n_fat_ban`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(virr_res_kwh_fat) `vkwh_r_fat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(virr_res_kwh_fat_ban) `vkwh_r_fat_ban`, " & vbCrLf
                '
                lstrSql = lstrSql & "        SUM(icms) `icms`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(pis) `pis`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(cofins) `cofins` " & vbCrLf
                lstrSql = lstrSql & "    FROM " & istrTabela & " " & vbCrLf
                lstrSql = lstrSql & "   WHERE `grupo` = 'B' " & vbCrLf
                lstrSql = lstrSql & "     AND `refaturado` = " & lintRefatu & " " & vbCrLf
                lstrSql = lstrSql & "     AND `irrigacao_aquicultura` = 1 " & vbCrLf
                lstrSql = lstrSql & "GROUP BY `ord_classe`, `ds_classe` " & vbCrLf
                lstrSql = lstrSql & "ORDER BY `ord_classe` ASC, `ds_classe` ASC "
                objAtualizaBancoDados.QUERY_NAME = lstrSql
                dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)


                If dt.Rows.Count > 0 Then
                    For Each row As DataRow In dt.Rows

                        lintOrdCla = CShort(IIf(IsDBNull(row.Item("ord_classe")), 0, row.Item("ord_classe")))
                        lstrDesCla = CStr(IIf(IsDBNull(row.Item("ds_classe")), "", row.Item("ds_classe")))
                        ldblQtdCon = CDbl(IIf(IsDBNull(row.Item("ncons")), 0, row.Item("ncons")))
                        ldblQtdkWh = CDbl(IIf(IsDBNull(row.Item("qkwh_fat")), 0, row.Item("qkwh_fat")))
                        ldblVlrkWh = CDbl(IIf(IsDBNull(row.Item("vkwh_fat")), 0, row.Item("vkwh_fat")))
                        ldblVlrBan = CDbl(IIf(IsDBNull(row.Item("vkwh_fat_ban")), 0, row.Item("vkwh_fat_ban")))
                        ldblQtdkWh_n = CDbl(IIf(IsDBNull(row.Item("qkwh_n_fat")), 0, row.Item("qkwh_n_fat")))
                        ldblVlrkWh_n = CDbl(IIf(IsDBNull(row.Item("vkwh_n_fat")), 0, row.Item("vkwh_n_fat")))
                        ldblVlrBan_n = CDbl(IIf(IsDBNull(row.Item("vkwh_n_fat_ban")), 0, row.Item("vkwh_n_fat_ban")))
                        ldblQtdkWh_r = CDbl(IIf(IsDBNull(row.Item("qkwh_r_fat")), 0, row.Item("qkwh_r_fat")))
                        ldblVlrkWh_r = CDbl(IIf(IsDBNull(row.Item("vkwh_r_fat")), 0, row.Item("vkwh_r_fat")))
                        ldblVlrBan_r = CDbl(IIf(IsDBNull(row.Item("vkwh_r_fat_ban")), 0, row.Item("vkwh_r_fat_ban")))
                        ldblVlrIcm = CDbl(IIf(IsDBNull(row.Item("icms")), 0, row.Item("icms")))
                        ldblVlrPis = CDbl(IIf(IsDBNull(row.Item("pis")), 0, row.Item("pis")))
                        ldblVlrCof = CDbl(IIf(IsDBNull(row.Item("cofins")), 0, row.Item("cofins")))

                        If lintOrdCla = 10 Then
                            lintTotAux = 1
                        ElseIf lintOrdCla >= 20 And lintOrdCla < 30 Then
                            lintTotAux = 0
                        Else
                            lintTotAux = 2
                        End If

                        ldblTotQtdCon(lintTotAux) = ldblTotQtdCon(lintTotAux) + ldblQtdCon
                        ldblTotQtdkWh(lintTotAux) = ldblTotQtdkWh(lintTotAux) + ldblQtdkWh
                        ldblTotVlrkWh(lintTotAux) = ldblTotVlrkWh(lintTotAux) + ldblVlrkWh
                        ldblTotVlrBan(lintTotAux) = ldblTotVlrBan(lintTotAux) + ldblVlrBan
                        '
                        ldblTotQtdkWh_n(lintTotAux) = ldblTotQtdkWh_n(lintTotAux) + ldblQtdkWh_n
                        ldblTotVlrkWh_n(lintTotAux) = ldblTotVlrkWh_n(lintTotAux) + ldblVlrkWh_n
                        ldblTotVlrBan_n(lintTotAux) = ldblTotVlrBan_n(lintTotAux) + ldblVlrBan_n
                        ldblTotQtdkWh_r(lintTotAux) = ldblTotQtdkWh_r(lintTotAux) + ldblQtdkWh_r
                        ldblTotVlrkWh_r(lintTotAux) = ldblTotVlrkWh_r(lintTotAux) + ldblVlrkWh_r
                        ldblTotVlrBan_r(lintTotAux) = ldblTotVlrBan_r(lintTotAux) + ldblVlrBan_r
                        '
                        ldblTotVlrIcm(lintTotAux) = ldblTotVlrIcm(lintTotAux) + ldblVlrIcm
                        ldblTotVlrPis(lintTotAux) = ldblTotVlrPis(lintTotAux) + ldblVlrPis
                        ldblTotVlrCof(lintTotAux) = ldblTotVlrCof(lintTotAux) + ldblVlrCof

                        '            'Subtotais
                        '            If lintOrdCla >= 30 And lblnSubTot Then
                        '              lstrTabAux = ""
                        '              lstrTabAux = lstrTabAux & "Total Bx.Renda|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotQtdCon(0), "###,###,###,##0") & "|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotQtdkWh(0), "#,###0.000") & "|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotVlrkWh(0), "#,##0.00") & "|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotVlrBan(0), "#,##0.00") & "|"
                        '
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotQtdkWh_n(0), "#,###0.000") & "|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotVlrBan_n(0), "#,##0.00") & "|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotVlrkWh_n(0), "#,##0.00") & "|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotVlrkWh_n(0) + ldblTotVlrBan_n(0), "#,##0.00") & "|"
                        '
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotQtdkWh_r(0), "#,###0.000") & "|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotVlrBan_r(0), "#,##0.00") & "|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotVlrkWh_r(0), "#,##0.00") & "|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotVlrkWh_r(0) + ldblTotVlrBan_r(0), "#,##0.00") & "|"
                        '
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotVlrIcm(0), "#,##0.00") & "|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotVlrPis(0), "#,##0.00") & "|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotVlrCof(0), "#,##0.00")
                        '
                        '              ifrmRelSamp.VS.FontBold = True
                        '              ifrmRelSamp.VS.Table = lstrTamCol & lstrTabAux
                        '              lstrTabAux = ""
                        '              lstrTabAux = lstrTabAux & "Total Residencial|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotQtdCon(0) + ldblTotQtdCon(1), "###,###,###,##0") & "|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotQtdkWh(0) + ldblTotQtdkWh(1), "#,###0.000") & "|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotVlrkWh(0) + ldblTotVlrkWh(1), "#,##0.00") & "|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotVlrBan(0) + ldblTotVlrBan(1), "#,##0.00") & "|"
                        '
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotQtdkWh_n(0) + ldblTotQtdkWh_n(1), "#,###0.000") & "|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotVlrBan_n(0) + ldblTotVlrBan_n(1), "#,##0.00") & "|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotVlrkWh_n(0) + ldblTotVlrkWh_n(1), "#,##0.00") & "|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotVlrkWh_n(0) + ldblTotVlrkWh_n(1) + ldblTotVlrBan_n(0) + ldblTotVlrBan_n(1), "#,##0.00") & "|"
                        '
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotQtdkWh_r(0) + ldblTotQtdkWh_r(1), "#,###0.000") & "|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotVlrBan_r(0) + ldblTotVlrBan_r(1), "#,##0.00") & "|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotVlrkWh_r(0) + ldblTotVlrkWh_r(1), "#,##0.00") & "|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotVlrkWh_r(0) + ldblTotVlrkWh_r(1) + ldblTotVlrBan_r(0) + ldblTotVlrBan_r(1), "#,##0.00") & "|"
                        '
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotVlrIcm(0) + ldblTotVlrIcm(1), "#,##0.00") & "|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotVlrPis(0) + ldblTotVlrPis(1), "#,##0.00") & "|"
                        '              lstrTabAux = lstrTabAux & Strings.Format(ldblTotVlrCof(0) + ldblTotVlrCof(1), "#,##0.00")
                        '
                        '              ifrmRelSamp.VS.Table = lstrTamCol & lstrTabAux
                        '              ifrmRelSamp.VS.FontBold = False
                        '              lblnSubTot = False
                        '            End If
                        'Detalhe



                        'lstrTabAux = ""
                        'lstrTabAux = lstrTabAux & lstrDesCla & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblQtdCon, "###,###,###,##0") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblQtdkWh, "#,###0.000") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrkWh, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrBan, "#,##0.00") & "|"

                        'lstrTabAux = lstrTabAux & VB6.Format(ldblQtdkWh_n, "#,###0.000") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrBan_n, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrkWh_n, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrkWh_n + ldblVlrBan_n, "#,##0.00") & "|"

                        'lstrTabAux = lstrTabAux & VB6.Format(ldblQtdkWh_r, "#,###0.000") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrBan_r, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrkWh_r, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrkWh_r + ldblVlrBan_r, "#,##0.00") & "|"

                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrIcm, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrPis, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrCof, "#,##0.00")

                        lstrSql = "INSERT INTO AUX_SAMP_NOVO_IMPRESSAOB_IA SET "
                        lstrSql = lstrSql & "CLASSE = '" & lstrDesCla & "' ,"
                        lstrSql = lstrSql & "NCONS = '" & ldblQtdCon & "',"
                        lstrSql = lstrSql & "KWHFAT = '" & ldblQtdkWh & "',"
                        lstrSql = lstrSql & "KWHRS = '" & ldblVlrkWh & "',"
                        lstrSql = lstrSql & "BANRS = '" & ldblVlrBan & "',"
                        lstrSql = lstrSql & "KWHPNORMAL = '" & ldblQtdkWh_n & "',"
                        lstrSql = lstrSql & "BANPNORMAL = '" & ldblVlrBan_n & "',"
                        lstrSql = lstrSql & "KWHRSPNORMAL = '" & ldblVlrkWh_n & "',"
                        lstrSql = lstrSql & "RECEITAPNORMAL = '" & ldblVlrkWh_n + ldblVlrBan_n & "',"
                        lstrSql = lstrSql & "BANPRESERVA = '" & ldblVlrBan_r & "',"
                        lstrSql = lstrSql & "KWHRSPRESERVA = '" & ldblVlrkWh_r & "',"
                        lstrSql = lstrSql & "KWHPRESERVA = '" & ldblQtdkWh_r & "',"
                        lstrSql = lstrSql & "RECEITAPRESERVA = '" & ldblVlrkWh_r + ldblVlrBan_r & "',"
                        lstrSql = lstrSql & "ICMS = '" & ldblVlrIcm & "',"
                        lstrSql = lstrSql & "PIS = '" & ldblVlrPis & "',"
                        lstrSql = lstrSql & "COFINS = '" & ldblVlrCof & "',"
                        lstrSql = lstrSql & "REFATURAMENTO = '" & lintRefatu & "'"
                        objExecAtualizaBancoDados.QUERY_NAME = lstrSql
                        repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

                    Next

                End If

            Next lintRefatu

            dt = Nothing

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

    End Sub

    Private Sub GeraImpressaoGrupoBTarifaBranca()
        Dim lstrTabAux As String
        Dim lstrTabTit As String
        Dim lstrTamCol As String
        Dim lintRefatu As Short
        Dim lintOrdCla As Short
        Dim lstrDesCla As String
        '
        Dim ldblQtdCon As Double
        'Consumos
        Dim ldblQtdkWh As Double
        Dim ldblVlrkWh As Double
        Dim ldblVlrBan As Double
        '
        Dim ldblQtdkWh_p As Double
        Dim ldblVlrkWh_p As Double
        Dim ldblVlrBan_p As Double
        Dim ldblQtdkWh_i As Double
        Dim ldblVlrkWh_i As Double
        Dim ldblVlrBan_i As Double
        Dim ldblQtdkWh_f As Double
        Dim ldblVlrkWh_f As Double
        Dim ldblVlrBan_f As Double
        'Tributos
        Dim ldblVlrIcm As Double
        Dim ldblVlrPis As Double
        Dim ldblVlrCof As Double
        '
        'Totais
        Dim ldblTotQtdCon(2) As Double
        'Consumos
        Dim ldblTotQtdkWh(2) As Double
        Dim ldblTotVlrkWh(2) As Double
        Dim ldblTotVlrBan(2) As Double
        '
        Dim ldblTotQtdkWh_p(2) As Double
        Dim ldblTotVlrkWh_p(2) As Double
        Dim ldblTotVlrBan_p(2) As Double
        Dim ldblTotQtdkWh_i(2) As Double
        Dim ldblTotVlrkWh_i(2) As Double
        Dim ldblTotVlrBan_i(2) As Double
        Dim ldblTotQtdkWh_f(2) As Double
        Dim ldblTotVlrkWh_f(2) As Double
        Dim ldblTotVlrBan_f(2) As Double
        'Tributos
        Dim ldblTotVlrIcm(2) As Double
        Dim ldblTotVlrPis(2) As Double
        Dim ldblTotVlrCof(2) As Double
        '
        'Auxiliares
        Dim lblnSubTot As Boolean
        Dim lintTotAux As Short
        Dim lintAux As Short
        Dim lstrSql As String

        Try
            For lintRefatu = 0 To 1
                lblnSubTot = True
                If lintRefatu = 1 Then
                    For lintAux = 1 To 3
                        ' ifrmRelSamp.VS.Paragraph = ""
                    Next
                End If
                '
                lstrSql = ""
                lstrSql = lstrSql & "  SELECT " & vbCrLf
                lstrSql = lstrSql & "        `ord_classe`, " & vbCrLf
                lstrSql = lstrSql & "        `ds_classe`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(if(qkwh_fat<0,0,1)) `ncons`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(qkwh_fat) `qkwh_fat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(vkwh_fat) `vkwh_fat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(vkwh_fat_ban) `vkwh_fat_ban`, " & vbCrLf
                '
                lstrSql = lstrSql & "        SUM(qtarbranca_kwh_ponta) `qkwh_p_fat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(qtarbranca_kwh_inter) `qkwh_i_fat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(qtarbranca_kwh_forap) `qkwh_f_fat`, " & vbCrLf

                lstrSql = lstrSql & "        SUM(vtarbranca_kwh_ponta) `vkwh_p_fat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(vtarbranca_kwh_ponta_ban) `vkwh_p_fat_ban`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(vtarbranca_kwh_inter) `vkwh_i_fat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(vtarbranca_kwh_inter_ban) `vkwh_i_fat_ban`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(vtarbranca_kwh_forap) `vkwh_f_fat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(vtarbranca_kwh_forap_ban) `vkwh_f_fat_ban`, " & vbCrLf
                '
                lstrSql = lstrSql & "        SUM(icms) `icms`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(pis) `pis`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(cofins) `cofins` " & vbCrLf
                lstrSql = lstrSql & "    FROM " & istrTabela & " " & vbCrLf
                lstrSql = lstrSql & "   WHERE `grupo` = 'B' " & vbCrLf
                lstrSql = lstrSql & "     AND `refaturado` = " & lintRefatu & " " & vbCrLf
                lstrSql = lstrSql & "     AND `tarifa_branca` = 1 " & vbCrLf
                lstrSql = lstrSql & "GROUP BY `ord_classe`, `ds_classe` " & vbCrLf
                lstrSql = lstrSql & "ORDER BY `ord_classe` ASC, `ds_classe` ASC "
                'Call Conectar(lstrSql, lrstAux)
                objAtualizaBancoDados.QUERY_NAME = lstrSql
                dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

                If dt.Rows.Count > 0 Then
                    For Each row As DataRow In dt.Rows

                        lintOrdCla = CShort(IIf(IsDBNull(row.Item("ord_classe")), 0, row.Item("ord_classe")))
                        lstrDesCla = CStr(IIf(IsDBNull(row.Item("ds_classe")), "", row.Item("ds_classe")))
                        ldblQtdCon = CDbl(IIf(IsDBNull(row.Item("ncons")), 0, row.Item("ncons")))
                        ldblQtdkWh = CDbl(IIf(IsDBNull(row.Item("qkwh_fat")), 0, row.Item("qkwh_fat")))
                        ldblVlrkWh = CDbl(IIf(IsDBNull(row.Item("vkwh_fat")), 0, row.Item("vkwh_fat")))
                        ldblVlrBan = CDbl(IIf(IsDBNull(row.Item("vkwh_fat_ban")), 0, row.Item("vkwh_fat_ban")))
                        ldblQtdkWh_p = CDbl(IIf(IsDBNull(row.Item("qkwh_p_fat")), 0, row.Item("qkwh_p_fat")))
                        ldblVlrkWh_p = CDbl(IIf(IsDBNull(row.Item("vkwh_p_fat")), 0, row.Item("vkwh_p_fat")))
                        ldblVlrBan_p = CDbl(IIf(IsDBNull(row.Item("vkwh_p_fat_ban")), 0, row.Item("vkwh_p_fat_ban")))
                        ldblQtdkWh_i = CDbl(IIf(IsDBNull(row.Item("qkwh_i_fat")), 0, row.Item("qkwh_i_fat")))
                        ldblVlrkWh_i = CDbl(IIf(IsDBNull(row.Item("vkwh_i_fat")), 0, row.Item("vkwh_i_fat")))
                        ldblVlrBan_i = CDbl(IIf(IsDBNull(row.Item("vkwh_i_fat_ban")), 0, row.Item("vkwh_i_fat_ban")))
                        ldblQtdkWh_f = CDbl(IIf(IsDBNull(row.Item("qkwh_f_fat")), 0, row.Item("qkwh_f_fat")))
                        ldblVlrkWh_f = CDbl(IIf(IsDBNull(row.Item("vkwh_f_fat")), 0, row.Item("vkwh_f_fat")))
                        ldblVlrBan_f = CDbl(IIf(IsDBNull(row.Item("vkwh_f_fat_ban")), 0, row.Item("vkwh_f_fat_ban")))
                        ldblVlrIcm = CDbl(IIf(IsDBNull(row.Item("icms")), 0, row.Item("icms")))
                        ldblVlrPis = CDbl(IIf(IsDBNull(row.Item("pis")), 0, row.Item("pis")))
                        ldblVlrCof = CDbl(IIf(IsDBNull(row.Item("cofins")), 0, row.Item("cofins")))

                        If lintOrdCla = 10 Then
                            lintTotAux = 1
                        ElseIf lintOrdCla >= 20 And lintOrdCla < 30 Then
                            lintTotAux = 0
                        Else
                            lintTotAux = 2
                        End If
                        ldblTotQtdCon(lintTotAux) = ldblTotQtdCon(lintTotAux) + ldblQtdCon
                        ldblTotQtdkWh(lintTotAux) = ldblTotQtdkWh(lintTotAux) + ldblQtdkWh
                        ldblTotVlrkWh(lintTotAux) = ldblTotVlrkWh(lintTotAux) + ldblVlrkWh
                        ldblTotVlrBan(lintTotAux) = ldblTotVlrBan(lintTotAux) + ldblVlrBan
                        '
                        ldblTotQtdkWh_p(lintTotAux) = ldblTotQtdkWh_p(lintTotAux) + ldblQtdkWh_p
                        ldblTotVlrkWh_p(lintTotAux) = ldblTotVlrkWh_p(lintTotAux) + ldblVlrkWh_p
                        ldblTotVlrBan_p(lintTotAux) = ldblTotVlrBan_p(lintTotAux) + ldblVlrBan_p
                        ldblTotQtdkWh_i(lintTotAux) = ldblTotQtdkWh_i(lintTotAux) + ldblQtdkWh_i
                        ldblTotVlrkWh_i(lintTotAux) = ldblTotVlrkWh_i(lintTotAux) + ldblVlrkWh_i
                        ldblTotVlrBan_i(lintTotAux) = ldblTotVlrBan_i(lintTotAux) + ldblVlrBan_i
                        ldblTotQtdkWh_f(lintTotAux) = ldblTotQtdkWh_f(lintTotAux) + ldblQtdkWh_f
                        ldblTotVlrkWh_f(lintTotAux) = ldblTotVlrkWh_f(lintTotAux) + ldblVlrkWh_f
                        ldblTotVlrBan_f(lintTotAux) = ldblTotVlrBan_f(lintTotAux) + ldblVlrBan_f
                        '
                        ldblTotVlrIcm(lintTotAux) = ldblTotVlrIcm(lintTotAux) + ldblVlrIcm
                        ldblTotVlrPis(lintTotAux) = ldblTotVlrPis(lintTotAux) + ldblVlrPis
                        ldblTotVlrCof(lintTotAux) = ldblTotVlrCof(lintTotAux) + ldblVlrCof

                        'Detalhe

                        lstrSql = "INSERT INTO AUX_SAMP_NOVO_IMPRESSAOB_TB SET "
                        lstrSql = lstrSql & "CLASSE = '" & lstrDesCla & "' ,"
                        lstrSql = lstrSql & "NCONS = '" & ldblQtdCon & "',"
                        lstrSql = lstrSql & "KWHFAT = '" & ldblQtdkWh & "',"
                        lstrSql = lstrSql & "KWHRS = '" & ldblVlrkWh & "',"
                        lstrSql = lstrSql & "BANRS = '" & ldblVlrBan & "',"
                        lstrSql = lstrSql & "KWHPONTA = '" & ldblQtdkWh_p & "',"
                        lstrSql = lstrSql & "BANPONTA = '" & ldblVlrBan_p & "',"
                        lstrSql = lstrSql & "KWHRSPONTA = '" & ldblVlrkWh_p & "',"
                        lstrSql = lstrSql & "RECEITAPONTA = '" & ldblVlrkWh_p + ldblVlrBan_p & "',"
                        lstrSql = lstrSql & "KWHINTER = '" & ldblQtdkWh_i & "',"
                        lstrSql = lstrSql & "BANINTER = '" & ldblVlrBan_i & "',"
                        lstrSql = lstrSql & "KWHRSINTER = '" & ldblVlrkWh_i & "',"
                        lstrSql = lstrSql & "RECEITAINTER = '" & ldblVlrkWh_i + ldblVlrBan_i & "',"
                        lstrSql = lstrSql & "KWHFORAP = '" & ldblQtdkWh_f & "',"
                        lstrSql = lstrSql & "BANFORAP = '" & ldblVlrBan_f & "',"
                        lstrSql = lstrSql & "KWHRSFORAP = '" & ldblVlrkWh_f & "',"
                        lstrSql = lstrSql & "RECEITAFORAP = '" & ldblVlrkWh_f + ldblVlrBan_f & "',"
                        lstrSql = lstrSql & "ICMS = '" & ldblVlrIcm & "',"
                        lstrSql = lstrSql & "PIS = '" & ldblVlrPis & "',"
                        lstrSql = lstrSql & "COFINS = '" & ldblVlrCof & "',"
                        lstrSql = lstrSql & "REFATURAMENTO = '" & lintRefatu & "'"
                        objExecAtualizaBancoDados.QUERY_NAME = lstrSql
                        repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)


                    Next

                End If

            Next lintRefatu

            dt = Nothing

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

    End Sub

    Private Sub GeraImpressaoUsinas()
        Dim lstrTabAux As String
        Dim lstrTabTit As String
        Dim lstrTamCol As String
        Dim lintRefatu As Short
        Dim lstrComCla As String
        '
        Dim ldblQtdCon As Double
        'Must
        Dim ldblQtdMus As Double
        Dim ldblVlrMus As Double
        Dim ldblVlrDes As Double
        Dim ldblVlrEnc As Double
        'Totais
        Dim ldblTotQtdCon As Double
        'Must
        Dim ldblTotQtdMus As Double
        Dim ldblTotVlrMus As Double
        Dim ldblTotVlrDes As Double
        Dim ldblTotVlrEnc As Double
        'Auxiliares
        Dim lintAux As Short
        Dim lstrSql As String


        Try
            For lintRefatu = 0 To 1
                If lintRefatu = 1 Then
                    For lintAux = 1 To 3
                        'ifrmRelSamp.VS.Paragraph = ""
                    Next
                End If
                lstrSql = ""
                lstrSql = lstrSql & "  SELECT " & vbCrLf
                lstrSql = lstrSql & "        `com_classe`, " & vbCrLf
                lstrSql = lstrSql & "        COUNT(*) `ncons`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`must_qtd`) `must_qtd`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`must_vlr`) `must_vlr`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`must_des`) `must_des`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`must_enc`) `must_enc` " & vbCrLf
                lstrSql = lstrSql & "    FROM " & istrTabela & " " & vbCrLf
                lstrSql = lstrSql & "   WHERE `refaturado` = " & lintRefatu & " " & vbCrLf
                lstrSql = lstrSql & "     AND `usinas` = 1 " & vbCrLf
                lstrSql = lstrSql & "GROUP BY `com_classe` " & vbCrLf
                lstrSql = lstrSql & "ORDER BY `com_classe` ASC "
                objAtualizaBancoDados.QUERY_NAME = lstrSql
                dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)
                ' Call Conectar(lstrSql, lrstAux)

                If dt.Rows.Count > 0 Then
                    For Each row As DataRow In dt.Rows

                        lstrComCla = CStr(IIf(IsDBNull(row.Item("com_classe")), "", row.Item("com_classe")))
                        ldblQtdCon = CDbl(IIf(IsDBNull(row.Item("ncons")), 0, row.Item("ncons")))
                        ldblQtdMus = CDbl(IIf(IsDBNull(row.Item("must_qtd")), 0, row.Item("must_qtd")))
                        ldblVlrMus = CDbl(IIf(IsDBNull(row.Item("must_vlr")), 0, row.Item("must_vlr")))
                        ldblVlrDes = CDbl(IIf(IsDBNull(row.Item("must_des")), 0, row.Item("must_des")))
                        ldblVlrEnc = CDbl(IIf(IsDBNull(row.Item("must_enc")), 0, row.Item("must_enc")))
                        '
                        ldblTotQtdCon = ldblTotQtdCon + ldblQtdCon
                        ldblTotQtdMus = ldblTotQtdMus + ldblQtdMus
                        ldblTotVlrMus = ldblTotVlrMus + ldblVlrMus
                        ldblTotVlrDes = ldblTotVlrDes + ldblVlrDes
                        ldblTotVlrEnc = ldblTotVlrEnc + ldblVlrEnc

                        lstrSql = "INSERT INTO AUX_SAMP_NOVO_IMPRESSAO_USINAS SET "
                        lstrSql = lstrSql & "CLASSE = '" & lstrComCla & "' ,"
                        lstrSql = lstrSql & "NCONS = '" & ldblQtdCon & "',"
                        lstrSql = lstrSql & "CONTRATADO = '" & ldblQtdMus & "',"
                        lstrSql = lstrSql & "MUSTRS = '" & ldblVlrMus & "',"
                        lstrSql = lstrSql & "DESCONTORS = '" & ldblVlrDes & "',"
                        lstrSql = lstrSql & "RECEITARS = '" & ldblVlrMus - ldblVlrDes & "',"
                        lstrSql = lstrSql & "ENCARGORS = '" & ldblVlrEnc & "',"
                        lstrSql = lstrSql & "REFATURAMENTO = '" & lintRefatu & "'"
                        objExecAtualizaBancoDados.QUERY_NAME = lstrSql
                        repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

                    Next

                    'Else

                End If

            Next lintRefatu

            dt = Nothing

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

    End Sub

    Private Sub GeraImpressaoGrupoAConvencional()
        Dim lstrTabAux As String
        Dim lstrTabTit As String
        Dim lstrTamCol As String
        Dim lintRefatu As Short
        Dim lintOrdCla As Short
        Dim lstrDesCla As String
        '
        Dim ldblQtdCon As Double
        'Consumos
        Dim ldblQtdkWh As Double
        Dim ldblVlrkWh As Double
        Dim ldblVlrBan As Double
        'Demadas
        Dim ldblQtdkW As Double
        Dim ldblVlrkW As Double
        Dim ldblQtdkWu As Double
        Dim ldblVlrkWu As Double
        'Receita
        Dim ldblVlrRec As Double
        'Reativos
        Dim ldblVlrCRe As Double
        Dim ldblVlrDRe As Double
        'Tributos
        Dim ldblVlrIcm As Double
        Dim ldblVlrPis As Double
        Dim ldblVlrCof As Double
        'Totais
        Dim ldblTotQtdCon As Double
        'Consumos
        Dim ldblTotQtdkWh As Double
        Dim ldblTotVlrkWh As Double
        Dim ldblTotVlrBan As Double
        'Demandas
        Dim ldblTotQtdkW As Double
        Dim ldblTotVlrkW As Double
        Dim ldblTotQtdkWu As Double
        Dim ldblTotVlrkWu As Double
        '
        Dim ldblTotVlrRec As Double
        'Reativos
        Dim ldblTotVlrCRe As Double
        Dim ldblTotVlrDRe As Double
        'Tributos
        Dim ldblTotVlrIcm As Double
        Dim ldblTotVlrPis As Double
        Dim ldblTotVlrCof As Double
        'Auxliares
        Dim lintAux As Short
        Dim lstrSql As String

        Try

            For lintRefatu = 0 To 1
                If lintRefatu = 1 Then
                    For lintAux = 1 To 3
                        ' ifrmRelSamp.VS.Paragraph = ""
                    Next
                End If
                '
                lstrSql = ""
                lstrSql = lstrSql & "  SELECT " & vbCrLf
                lstrSql = lstrSql & "        `ord_classe`, " & vbCrLf
                lstrSql = lstrSql & "        `ds_classe`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(if((`qkwh_fat` + `qkw_fat`) < 0, 0, 1)) `ncons`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`qkwh_fat`) `qkwh_fat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkwh_fat`) `vkwh_fat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkwh_fat_ban`) `vkwh_fat_ban`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`qkw_fat`) `qkw_fat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkw_fat`) `vkw_fat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`qkw_fatu_fp`) `qkw_fatu_fp`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkw_fatu_fp`) `vkw_fatu_fp`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`receita_fp`) `vreceita`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vconreat`) `vconreat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vdemreat`) `vdemreat`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`icms`) `icms`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`pis`) `pis`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`cofins`) `cofins` " & vbCrLf
                lstrSql = lstrSql & "    FROM " & istrTabela & " " & vbCrLf
                lstrSql = lstrSql & "   WHERE `grupo` = 'A4' " & vbCrLf
                lstrSql = lstrSql & "     AND `refaturado` = " & lintRefatu & " " & vbCrLf
                lstrSql = lstrSql & "     AND `usinas` = 0 " & vbCrLf
                lstrSql = lstrSql & "GROUP BY `ord_classe`, `ds_classe` " & vbCrLf
                lstrSql = lstrSql & "ORDER BY `ord_classe` ASC, `ds_classe` ASC "
                'Call Conectar(lstrSql, lrstAux)
                objAtualizaBancoDados.QUERY_NAME = lstrSql
                dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

                If dt.Rows.Count > 0 Then

                    For Each row As DataRow In dt.Rows

                        lintOrdCla = CShort(IIf(IsDBNull(row.Item("ord_classe")), 0, row.Item("ord_classe")))
                        lstrDesCla = CStr(IIf(IsDBNull(row.Item("ds_classe")), "", row.Item("ds_classe")))
                        ldblQtdCon = CDbl(IIf(IsDBNull(row.Item("ncons")), 0, row.Item("ncons")))
                        ldblQtdkWh = CDbl(IIf(IsDBNull(row.Item("qkwh_fat")), 0, row.Item("qkwh_fat")))
                        ldblVlrkWh = CDbl(IIf(IsDBNull(row.Item("vkwh_fat")), 0, row.Item("vkwh_fat")))
                        ldblVlrBan = CDbl(IIf(IsDBNull(row.Item("vkwh_fat_ban")), 0, row.Item("vkwh_fat_ban")))
                        ldblQtdkW = CDbl(IIf(IsDBNull(row.Item("qkw_fat")), 0, row.Item("qkw_fat")))
                        ldblVlrkW = CDbl(IIf(IsDBNull(row.Item("vkw_fat")), 0, row.Item("vkw_fat")))
                        ldblQtdkWu = CDbl(IIf(IsDBNull(row.Item("qkw_fatu_fp")), 0, row.Item("qkw_fatu_fp")))
                        ldblVlrkWu = CDbl(IIf(IsDBNull(row.Item("vkw_fatu_fp")), 0, row.Item("vkw_fatu_fp")))
                        ldblVlrRec = CDbl(IIf(IsDBNull(row.Item("vreceita")), 0, row.Item("vreceita")))
                        ldblVlrCRe = CDbl(IIf(IsDBNull(row.Item("vconreat")), 0, row.Item("vconreat")))
                        ldblVlrDRe = CDbl(IIf(IsDBNull(row.Item("vdemreat")), 0, row.Item("vdemreat")))
                        ldblVlrIcm = CDbl(IIf(IsDBNull(row.Item("icms")), 0, row.Item("icms")))
                        ldblVlrPis = CDbl(IIf(IsDBNull(row.Item("pis")), 0, row.Item("pis")))
                        ldblVlrCof = CDbl(IIf(IsDBNull(row.Item("cofins")), 0, row.Item("cofins")))
                        ldblTotQtdCon = ldblTotQtdCon + ldblQtdCon
                        ldblTotQtdkWh = ldblTotQtdkWh + ldblQtdkWh
                        ldblTotVlrkWh = ldblTotVlrkWh + ldblVlrkWh
                        ldblTotVlrBan = ldblTotVlrBan + ldblVlrBan
                        ldblTotQtdkW = ldblTotQtdkW + ldblQtdkW
                        ldblTotVlrkW = ldblTotVlrkW + ldblVlrkW
                        ldblTotQtdkWu = ldblTotQtdkWu + ldblQtdkWu
                        ldblTotVlrkWu = ldblTotVlrkWu + ldblVlrkWu
                        ldblTotVlrRec = ldblTotVlrRec + ldblVlrRec
                        ldblTotVlrCRe = ldblTotVlrCRe + ldblVlrCRe
                        ldblTotVlrDRe = ldblTotVlrDRe + ldblVlrDRe
                        ldblTotVlrIcm = ldblTotVlrIcm + ldblVlrIcm
                        ldblTotVlrPis = ldblTotVlrPis + ldblVlrPis
                        ldblTotVlrCof = ldblTotVlrCof + ldblVlrCof
                        'Detalhe

                        lstrSql = "INSERT INTO AUX_SAMP_NOVO_IMPRESSAOA_CONVENCIONAL SET "
                        lstrSql = lstrSql & "CLASSE = '" & lstrDesCla & "' ,"
                        lstrSql = lstrSql & "NCONS = '" & ldblQtdCon & "',"
                        lstrSql = lstrSql & "KWH = '" & ldblQtdkWh & "',"
                        lstrSql = lstrSql & "KW = '" & ldblQtdkW & "',"
                        lstrSql = lstrSql & "KWULT = '" & ldblQtdkWu & "',"
                        lstrSql = lstrSql & "KWHRS = '" & ldblVlrkWh & "',"
                        lstrSql = lstrSql & "BANDEIRARS = '" & ldblVlrBan & "',"
                        lstrSql = lstrSql & "RECEITA = '" & ldblVlrRec & "',"
                        lstrSql = lstrSql & "KWRS = '" & ldblVlrkW & "',"
                        lstrSql = lstrSql & "KWULTRS = '" & ldblVlrkWu & "',"
                        lstrSql = lstrSql & "PIS = '" & ldblVlrPis & "',"
                        lstrSql = lstrSql & "ICMS = '" & ldblVlrIcm & "',"
                        lstrSql = lstrSql & "COFINS = '" & ldblVlrCof & "',"
                        lstrSql = lstrSql & "REFATURAMENTO = '" & lintRefatu & "'"
                        objExecAtualizaBancoDados.QUERY_NAME = lstrSql
                        repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

                    Next

                    ' Else

                End If

            Next lintRefatu

            dt = Nothing

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

    End Sub

    Private Sub GeraImpressaoGrupoAHorossazonal()
        Dim lstrTabAux As String
        Dim lstrTabTit1 As String
        Dim lstrTamCol1 As String
        Dim lstrTabTit2 As String
        Dim lstrTamCol2 As String
        Dim lintRefatu As Short
        Dim lintOrdCla As Short
        Dim lstrDesCla As String

        Dim ldblQtdCon As Double
        'Consumos
        Dim ldblQtdkWhp As Double
        Dim ldblQtdkWhfp As Double
        Dim ldblVlrkWhp As Double
        Dim ldblVlrkWhfp As Double
        Dim ldblVlrBanp As Double
        Dim ldblVlrBanfp As Double
        'Demandas
        Dim ldblQtdkWp As Double
        Dim ldblQtdkWfp As Double
        Dim ldblVlrkWp As Double
        Dim ldblVlrkWfp As Double
        Dim ldblQtdkWup As Double
        Dim ldblQtdkWufp As Double
        Dim ldblVlrkWup As Double
        Dim ldblVlrkWufp As Double
        'Reativos
        Dim ldblVlrCRep As Double
        Dim ldblVlrCRefp As Double
        Dim ldblVlrDRep As Double
        Dim ldblVlrDRefp As Double
        'Receita
        Dim ldblVlrRecp As Double
        Dim ldblVlrRecfp As Double
        'Tributos
        Dim ldblVlrIcm As Double
        Dim ldblVlrPis As Double
        Dim ldblVlrCof As Double
        'Totais
        Dim ldblTotQtdCon As Double
        'Consumos
        Dim ldblTotQtdkWhp As Double
        Dim ldblTotQtdkWhfp As Double
        Dim ldblTotVlrkWhp As Double
        Dim ldblTotVlrkWhfp As Double
        Dim ldblTotVlrBanp As Double
        Dim ldblTotVlrBanfp As Double
        'Demandas
        Dim ldblTotQtdkWp As Double
        Dim ldblTotQtdkWfp As Double
        Dim ldblTotVlrkWp As Double
        Dim ldblTotVlrkWfp As Double
        Dim ldblTotQtdkWup As Double
        Dim ldblTotQtdkWufp As Double
        Dim ldblTotVlrkWup As Double
        Dim ldblTotVlrkWufp As Double
        'Reativos
        Dim ldblTotVlrCRep As Double
        Dim ldblTotVlrCRefp As Double
        Dim ldblTotVlrDRep As Double
        Dim ldblTotVlrDRefp As Double
        'Receita
        Dim ldblTotVlrRecp As Double
        Dim ldblTotVlrRecfp As Double
        'Tributos
        Dim ldblTotVlrIcm As Double
        Dim ldblTotVlrPis As Double
        Dim ldblTotVlrCof As Double
        'Auxiliares
        Dim lintAux As Short
        Dim lstrSql As String

        Try
            For lintRefatu = 0 To 1
                If lintRefatu = 1 Then
                    For lintAux = 1 To 3
                        'ifrmRelSamp.VS.Paragraph = ""
                    Next
                End If
                '
                lstrSql = ""
                lstrSql = lstrSql & "  SELECT " & vbCrLf
                lstrSql = lstrSql & "        `ord_classe`, " & vbCrLf
                lstrSql = lstrSql & "        `ds_classe`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(if((`qkwh_fat_p` + `qkwh_fat_fp` + `qkw_fat_p` + `qkw_fat_fp`) < 0, 0, 1)) `ncons`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`qkwh_fat_p`) `qkwh_fat_p`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`qkwh_fat_fp`) `qkwh_fat_fp`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkwh_fat_p`) `vkwh_fat_p`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkwh_fat_fp`) `vkwh_fat_fp`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkwh_fat_p_ban`) `vkwh_fat_p_ban`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkwh_fat_fp_ban`) `vkwh_fat_fp_ban`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`qkw_fat_p`) `qkw_fat_p`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`qkw_fat_fp`) `qkw_fat_fp`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkw_fat_p`) `vkw_fat_p`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkw_fat_fp`) `vkw_fat_fp`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`qkw_fatu_p`) `qkw_fatu_p`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`qkw_fatu_fp`) `qkw_fatu_fp`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkw_fatu_p`) `vkw_fatu_p`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkw_fatu_fp`) `vkw_fatu_fp`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vconreat_p`) `vconreat_p`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vconreat_fp`) `vconreat_fp`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vdemreat_p`) `vdemreat_p`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vdemreat_fp`) `vdemreat_fp`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`receita_p`) `vreceita_p`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`receita_fp`) `vreceita_fp`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`icms`) `icms`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`pis`) `pis`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`cofins`) `cofins` " & vbCrLf
                lstrSql = lstrSql & "    FROM " & istrTabela & " " & vbCrLf
                lstrSql = lstrSql & "   WHERE `grupo` IN( 'AA','AV','AX') " & vbCrLf
                lstrSql = lstrSql & "     AND `refaturado` = " & lintRefatu & " " & vbCrLf
                lstrSql = lstrSql & "GROUP BY `ord_classe`, `ds_classe` " & vbCrLf
                lstrSql = lstrSql & "ORDER BY `ord_classe` ASC, `ds_classe` ASC "
                objAtualizaBancoDados.QUERY_NAME = lstrSql
                dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

                If dt.Rows.Count > 0 Then
                    For Each row As DataRow In dt.Rows

                        lintOrdCla = CShort(IIf(IsDBNull(row.Item("ord_classe")), 0, row.Item("ord_classe")))
                        lstrDesCla = CStr(IIf(IsDBNull(row.Item("ds_classe")), "", row.Item("ds_classe")))
                        ldblQtdCon = CDbl(IIf(IsDBNull(row.Item("ncons")), 0, row.Item("ncons")))
                        ldblQtdkWhp = CDbl(IIf(IsDBNull(row.Item("qkwh_fat_p")), 0, row.Item("qkwh_fat_p")))
                        ldblQtdkWhfp = CDbl(IIf(IsDBNull(row.Item("qkwh_fat_fp")), 0, row.Item("qkwh_fat_fp")))
                        ldblVlrkWhp = CDbl(IIf(IsDBNull(row.Item("vkwh_fat_p")), 0, row.Item("vkwh_fat_p")))
                        ldblVlrkWhfp = CDbl(IIf(IsDBNull(row.Item("vkwh_fat_fp")), 0, row.Item("vkwh_fat_fp")))
                        ldblVlrBanp = CDbl(IIf(IsDBNull(row.Item("vkwh_fat_p_ban")), 0, row.Item("vkwh_fat_p_ban")))
                        ldblVlrBanfp = CDbl(IIf(IsDBNull(row.Item("vkwh_fat_fp_ban")), 0, row.Item("vkwh_fat_fp_ban")))
                        ldblQtdkWp = CDbl(IIf(IsDBNull(row.Item("qkw_fat_p")), 0, row.Item("qkw_fat_p")))
                        ldblQtdkWfp = CDbl(IIf(IsDBNull(row.Item("qkw_fat_fp")), 0, row.Item("qkw_fat_fp")))
                        ldblVlrkWp = CDbl(IIf(IsDBNull(row.Item("vkw_fat_p")), 0, row.Item("vkw_fat_p")))
                        ldblVlrkWfp = CDbl(IIf(IsDBNull(row.Item("vkw_fat_fp")), 0, row.Item("vkw_fat_fp")))
                        ldblQtdkWup = CDbl(IIf(IsDBNull(row.Item("qkw_fatu_p")), 0, row.Item("qkw_fatu_p")))
                        ldblQtdkWufp = CDbl(IIf(IsDBNull(row.Item("qkw_fatu_fp")), 0, row.Item("qkw_fatu_fp")))
                        ldblVlrkWup = CDbl(IIf(IsDBNull(row.Item("vkw_fatu_p")), 0, row.Item("vkw_fatu_p")))
                        ldblVlrkWufp = CDbl(IIf(IsDBNull(row.Item("vkw_fatu_fp")), 0, row.Item("vkw_fatu_fp")))
                        ldblVlrCRep = CDbl(IIf(IsDBNull(row.Item("vconreat_p")), 0, row.Item("vconreat_p")))
                        ldblVlrCRefp = CDbl(IIf(IsDBNull(row.Item("vconreat_fp")), 0, row.Item("vconreat_fp")))
                        ldblVlrDRep = CDbl(IIf(IsDBNull(row.Item("vdemreat_p")), 0, row.Item("vdemreat_p")))
                        ldblVlrDRefp = CDbl(IIf(IsDBNull(row.Item("vdemreat_fp")), 0, row.Item("vdemreat_fp")))
                        ldblVlrRecp = CDbl(IIf(IsDBNull(row.Item("vreceita_p")), 0, row.Item("vreceita_p")))
                        ldblVlrRecfp = CDbl(IIf(IsDBNull(row.Item("vreceita_fp")), 0, row.Item("vreceita_fp")))
                        ldblVlrIcm = CDbl(IIf(IsDBNull(row.Item("icms")), 0, row.Item("icms")))
                        ldblVlrPis = CDbl(IIf(IsDBNull(row.Item("pis")), 0, row.Item("pis")))
                        ldblVlrCof = CDbl(IIf(IsDBNull(row.Item("cofins")), 0, row.Item("cofins")))

                        ldblTotQtdCon = ldblTotQtdCon + ldblQtdCon
                        ldblTotQtdkWhp = ldblTotQtdkWhp + ldblQtdkWhp
                        ldblTotQtdkWhfp = ldblTotQtdkWhfp + ldblQtdkWhfp
                        ldblTotVlrkWhp = ldblTotVlrkWhp + ldblVlrkWhp
                        ldblTotVlrkWhfp = ldblTotVlrkWhfp + ldblVlrkWhfp
                        ldblTotVlrBanp = ldblTotVlrBanp + ldblVlrBanp
                        ldblTotVlrBanfp = ldblTotVlrBanfp + ldblVlrBanfp
                        ldblTotQtdkWp = ldblTotQtdkWp + ldblQtdkWp
                        ldblTotQtdkWfp = ldblTotQtdkWfp + ldblQtdkWfp
                        ldblTotVlrkWp = ldblTotVlrkWp + ldblVlrkWp
                        ldblTotVlrkWfp = ldblTotVlrkWfp + ldblVlrkWfp
                        ldblTotQtdkWup = ldblTotQtdkWup + ldblQtdkWup
                        ldblTotQtdkWufp = ldblTotQtdkWufp + ldblQtdkWufp
                        ldblTotVlrkWup = ldblTotVlrkWup + ldblVlrkWup
                        ldblTotVlrkWufp = ldblTotVlrkWufp + ldblVlrkWufp
                        ldblTotVlrCRep = ldblTotVlrCRep + ldblVlrCRep
                        ldblTotVlrCRefp = ldblTotVlrCRefp + ldblVlrCRefp
                        ldblTotVlrDRep = ldblTotVlrDRep + ldblVlrDRep
                        ldblTotVlrDRefp = ldblTotVlrDRefp + ldblVlrDRefp
                        ldblTotVlrRecp = ldblTotVlrRecp + ldblVlrRecp
                        ldblTotVlrRecfp = ldblTotVlrRecfp + ldblVlrRecfp
                        ldblTotVlrIcm = ldblTotVlrIcm + ldblVlrIcm
                        ldblTotVlrPis = ldblTotVlrPis + ldblVlrPis
                        ldblTotVlrCof = ldblTotVlrCof + ldblVlrCof

                        ''Detalhe
                        lstrSql = "INSERT INTO AUX_SAMP_NOVO_IMPRESSAOA_HOROSSAZONAL SET "
                        lstrSql = lstrSql & "CLASSE = '" & lstrDesCla & "' ,"
                        lstrSql = lstrSql & "NCONS = '" & ldblQtdCon & "',"
                        lstrSql = lstrSql & "KWHPONTA = '" & ldblQtdkWhp & "',"
                        lstrSql = lstrSql & "KWHFORAP = '" & ldblQtdkWhfp & "',"
                        lstrSql = lstrSql & "KWPONTA = '" & ldblQtdkWp & "',"
                        lstrSql = lstrSql & "KWFORAP = '" & ldblQtdkWfp & "',"
                        lstrSql = lstrSql & "KWULTPONTA = '" & ldblQtdkWup & "',"
                        lstrSql = lstrSql & "KWULTFORAP = '" & ldblQtdkWufp & "',"
                        lstrSql = lstrSql & "KWHRSPONTA = '" & ldblVlrkWhp & "',"
                        lstrSql = lstrSql & "KWHRSFORAP = '" & ldblVlrkWhfp & "',"
                        lstrSql = lstrSql & "BANDEIRAPONTA = '" & ldblVlrBanp & "',"
                        lstrSql = lstrSql & "BANDEIRAFORAP = '" & ldblVlrBanfp & "',"
                        lstrSql = lstrSql & "RECEITAPONTA = '" & ldblVlrRecp & "',"
                        lstrSql = lstrSql & "RECEITAFORAP = '" & ldblVlrRecfp & "',"
                        lstrSql = lstrSql & "KWRSPONTA = '" & ldblVlrkWp & "',"
                        lstrSql = lstrSql & "KWRSFORAP = '" & ldblVlrkWfp & "',"
                        lstrSql = lstrSql & "KWRSULTPONTA = '" & ldblVlrkWup & "',"
                        lstrSql = lstrSql & "KWRSULTFORAP = '" & ldblVlrkWufp & "',"
                        lstrSql = lstrSql & "ICMS = '" & ldblVlrIcm & "',"
                        lstrSql = lstrSql & "PIS = '" & ldblVlrPis & "',"
                        lstrSql = lstrSql & "COFINS = '" & ldblVlrCof & "',"
                        lstrSql = lstrSql & "REFATURAMENTO = '" & lintRefatu & "'"
                        objExecAtualizaBancoDados.QUERY_NAME = lstrSql
                        repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

                    Next


                    ' Else


                End If

            Next lintRefatu

            dt = Nothing

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

    End Sub

    Private Sub GeraImpressaoGrupoALivre()
        Dim lstrTabAux As String
        Dim lstrTabTit1 As String
        Dim lstrTamCol1 As String
        Dim lstrTabTit2 As String
        Dim lstrTamCol2 As String
        Dim lintRefatu As Short
        Dim lintOrdCla As Short
        Dim lstrDesCla As String
        '
        Dim ldblQtdCon As Double
        'Consumos
        Dim ldblQtdkWhp As Double
        Dim ldblQtdkWhfp As Double
        Dim ldblVlrkWhp As Double
        Dim ldblVlrkWhfp As Double
        Dim ldblVlrBanp As Double
        Dim ldblVlrBanfp As Double
        'Livre - Encargos
        Dim ldblQtdLivp As Double
        Dim ldblQtdLivfp As Double
        Dim ldblVlrLivp As Double
        Dim ldblVlrLivfp As Double
        'Demandas
        Dim ldblQtdkWp As Double
        Dim ldblQtdkWfp As Double
        Dim ldblVlrkWp As Double
        Dim ldblVlrkWfp As Double
        Dim ldblQtdkWup As Double
        Dim ldblQtdkWufp As Double
        Dim ldblVlrkWup As Double
        Dim ldblVlrkWufp As Double
        'CCEE
        Dim ldblVlrDscProv As Double
        Dim ldblVlrDscReal As Double
        Dim ldblVlrDevCcee As Double
        Dim ldblVlrCobCcee As Double
        Dim ldblVlrkwhDscProv As Double
        Dim ldblVlrkwhDscReal As Double
        Dim ldblVlrkwhDevCcee As Double
        Dim ldblVlrkwhCobCcee As Double
        'Receita
        Dim ldblVlrRecp As Double
        Dim ldblVlrRecfp As Double
        'Reativos
        Dim ldblVlrCRep As Double
        Dim ldblVlrCRefp As Double
        Dim ldblVlrDRep As Double
        Dim ldblVlrDRefp As Double
        'Tributos
        Dim ldblVlrIcm As Double
        Dim ldblVlrPis As Double
        Dim ldblVlrCof As Double
        'Totais
        Dim ldblTotQtdCon As Double
        'Consumo
        Dim ldblTotQtdkWhp As Double
        Dim ldblTotQtdkWhfp As Double
        Dim ldblTotVlrkWhp As Double
        Dim ldblTotVlrkWhfp As Double
        Dim ldblTotVlrBanp As Double
        Dim ldblTotVlrBanfp As Double
        'Livre - Encargos
        Dim ldblTotQtdLivp As Double
        Dim ldblTotQtdLivfp As Double
        Dim ldblTotVlrLivp As Double
        Dim ldblTotVlrLivfp As Double
        'Demandas
        Dim ldblTotQtdkWp As Double
        Dim ldblTotQtdkWfp As Double
        Dim ldblTotVlrkWp As Double
        Dim ldblTotVlrkWfp As Double
        Dim ldblTotQtdkWup As Double
        Dim ldblTotQtdkWufp As Double
        Dim ldblTotVlrkWup As Double
        Dim ldblTotVlrkWufp As Double
        'CCEE
        Dim ldblTotVlrDscProv As Double
        Dim ldblTotVlrDscReal As Double
        Dim ldblTotVlrDevCcee As Double
        Dim ldblTotVlrCobCcee As Double

        Dim ldblTotVlrkwhDscProv As Double
        Dim ldblTotVlrkwhDscReal As Double
        Dim ldblTotVlrkwhDevCcee As Double
        Dim ldblTotVlrkwhCobCcee As Double
        'Receita
        Dim ldblTotVlrRecp As Double
        Dim ldblTotVlrRecfp As Double
        'Reativos
        Dim ldblTotVlrCRep As Double
        Dim ldblTotVlrCRefp As Double
        Dim ldblTotVlrDRep As Double
        Dim ldblTotVlrDRefp As Double
        'Tributos
        Dim ldblTotVlrIcm As Double
        Dim ldblTotVlrPis As Double
        Dim ldblTotVlrCof As Double
        'Auxiliares
        Dim lintAux As Short
        Dim lstrSql As String


        Try '

            For lintRefatu = 0 To 1
                If lintRefatu = 1 Then
                    For lintAux = 1 To 3
                        'ifrmRelSamp.VS.Paragraph = ""
                    Next
                End If
                '
                lstrSql = ""
                lstrSql = lstrSql & "  SELECT " & vbCrLf
                lstrSql = lstrSql & "        `parc_cativo`, " & vbCrLf
                lstrSql = lstrSql & "        `ord_classe`, " & vbCrLf
                lstrSql = lstrSql & "        `ds_classe`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(if((`qkwh_fat_p` + `qkwh_fat_fp` + `qkwh_fat_p_liv` + `qkwh_fat_fp_liv` + `qkw_fat_p` + `qkw_fat_fp`) < 0, 0, 1)) `ncons`, " & vbCrLf
                'Consumos
                lstrSql = lstrSql & "        SUM(`qkwh_fat_p`) `qkwh_fat_p`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`qkwh_fat_fp`) `qkwh_fat_fp`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkwh_fat_p`) `vkwh_fat_p`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkwh_fat_fp`) `vkwh_fat_fp`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkwh_fat_p_ban`) `vkwh_fat_p_ban`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkwh_fat_fp_ban`) `vkwh_fat_fp_ban`, " & vbCrLf
                'Livre - Encargos
                lstrSql = lstrSql & "        SUM(`qkwh_fat_p_liv`) `qkwh_fat_p_liv`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`qkwh_fat_fp_liv`) `qkwh_fat_fp_liv`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkwh_fat_p_liv`) `vkwh_fat_p_liv`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkwh_fat_fp_liv`) `vkwh_fat_fp_liv`, " & vbCrLf
                'Demandas
                lstrSql = lstrSql & "        SUM(`qkw_fat_p`) `qkw_fat_p`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`qkw_fat_fp`) `qkw_fat_fp`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkw_fat_p`) `vkw_fat_p`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkw_fat_fp`) `vkw_fat_fp`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`qkw_fatu_p`) `qkw_fatu_p`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`qkw_fatu_fp`) `qkw_fatu_fp`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkw_fatu_p`) `vkw_fatu_p`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkw_fatu_fp`) `vkw_fatu_fp`, " & vbCrLf
                'CCEE
                lstrSql = lstrSql & "        SUM(`vkw_dsc_prov`) `vkw_dsc_prov`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkw_dsc_real`) `vkw_dsc_real`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkw_dev_ccee`) `vkw_dev_ccee`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkw_cob_ccee`) `vkw_cob_ccee`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkwh_dsc_prov`) `vkwh_dsc_prov`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkwh_dsc_real`) `vkwh_dsc_real`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkwh_dev_ccee`) `vkwh_dev_ccee`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vkwh_cob_ccee`) `vkwh_cob_ccee`, " & vbCrLf
                'Receita
                lstrSql = lstrSql & "        SUM(`receita_p`) `vreceita_p`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`receita_fp`) `vreceita_fp`, " & vbCrLf
                'Reativos
                lstrSql = lstrSql & "        SUM(`vconreat_p`) `vconreat_p`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vconreat_fp`) `vconreat_fp`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vdemreat_p`) `vdemreat_p`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`vdemreat_fp`) `vdemreat_fp`, " & vbCrLf
                'Tributos
                lstrSql = lstrSql & "        SUM(`icms`) `icms`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`pis`) `pis`, " & vbCrLf
                lstrSql = lstrSql & "        SUM(`cofins`) `cofins` " & vbCrLf
                lstrSql = lstrSql & "    FROM " & istrTabela & " " & vbCrLf
                lstrSql = lstrSql & "   WHERE `grupo` IN( 'AL','ALP', 'ALS','ALV') " & vbCrLf
                lstrSql = lstrSql & "     AND `refaturado` = " & lintRefatu & " " & vbCrLf
                lstrSql = lstrSql & "GROUP BY `ord_classe`, `ds_classe` " & vbCrLf
                lstrSql = lstrSql & "ORDER BY `ord_classe` ASC, `ds_classe` ASC "
                ' Call Conectar(lstrSql, lrstAux)
                objAtualizaBancoDados.QUERY_NAME = lstrSql
                dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

                If dt.Rows.Count > 0 Then
                    For Each row As DataRow In dt.Rows

                        lintOrdCla = CShort(IIf(IsDBNull(row.Item("ord_classe")), 0, row.Item("ord_classe")))
                        lstrDesCla = CStr(IIf(IsDBNull(row.Item("ds_classe")), "", row.Item("ds_classe")))
                        ldblQtdCon = CDbl(IIf(IsDBNull(row.Item("ncons")), 0, row.Item("ncons")))
                        'Consumo
                        ldblQtdkWhp = CDbl(IIf(IsDBNull(row.Item("qkwh_fat_p")), 0, row.Item("qkwh_fat_p")))
                        ldblQtdkWhfp = CDbl(IIf(IsDBNull(row.Item("qkwh_fat_fp")), 0, row.Item("qkwh_fat_fp")))
                        ldblVlrkWhp = CDbl(IIf(IsDBNull(row.Item("vkwh_fat_p")), 0, row.Item("vkwh_fat_p")))
                        ldblVlrkWhfp = CDbl(IIf(IsDBNull(row.Item("vkwh_fat_fp")), 0, row.Item("vkwh_fat_fp")))
                        ldblVlrBanp = CDbl(IIf(IsDBNull(row.Item("vkwh_fat_p_ban")), 0, row.Item("vkwh_fat_p_ban")))
                        ldblVlrBanfp = CDbl(IIf(IsDBNull(row.Item("vkwh_fat_fp_ban")), 0, row.Item("vkwh_fat_fp_ban")))
                        'Livre - Encargos
                        ldblQtdLivp = CDbl(IIf(IsDBNull(row.Item("qkwh_fat_p_liv")), 0, row.Item("qkwh_fat_p_liv")))
                        ldblQtdLivfp = CDbl(IIf(IsDBNull(row.Item("qkwh_fat_fp_liv")), 0, row.Item("qkwh_fat_fp_liv")))
                        ldblVlrLivp = CDbl(IIf(IsDBNull(row.Item("vkwh_fat_p_liv")), 0, row.Item("vkwh_fat_p_liv")))
                        ldblVlrLivfp = CDbl(IIf(IsDBNull(row.Item("vkwh_fat_fp_liv")), 0, row.Item("vkwh_fat_fp_liv")))
                        'Demandas
                        ldblQtdkWp = CDbl(IIf(IsDBNull(row.Item("qkw_fat_p")), 0, row.Item("qkw_fat_p")))
                        ldblQtdkWfp = CDbl(IIf(IsDBNull(row.Item("qkw_fat_fp")), 0, row.Item("qkw_fat_fp")))
                        ldblVlrkWp = CDbl(IIf(IsDBNull(row.Item("vkw_fat_p")), 0, row.Item("vkw_fat_p")))
                        ldblVlrkWfp = CDbl(IIf(IsDBNull(row.Item("vkw_fat_fp")), 0, row.Item("vkw_fat_fp")))
                        ldblQtdkWup = CDbl(IIf(IsDBNull(row.Item("qkw_fatu_p")), 0, row.Item("qkw_fatu_p")))
                        ldblQtdkWufp = CDbl(IIf(IsDBNull(row.Item("qkw_fatu_fp")), 0, row.Item("qkw_fatu_fp")))
                        ldblVlrkWup = CDbl(IIf(IsDBNull(row.Item("vkw_fatu_p")), 0, row.Item("vkw_fatu_p")))
                        ldblVlrkWufp = CDbl(IIf(IsDBNull(row.Item("vkw_fatu_fp")), 0, row.Item("vkw_fatu_fp")))
                        'CCEE
                        ldblVlrDscProv = CDbl(IIf(IsDBNull(row.Item("vkw_dsc_prov")), 0, row.Item("vkw_dsc_prov")))
                        ldblVlrDscReal = CDbl(IIf(IsDBNull(row.Item("vkw_dsc_real")), 0, row.Item("vkw_dsc_real")))
                        ldblVlrDevCcee = CDbl(IIf(IsDBNull(row.Item("vkw_dev_ccee")), 0, row.Item("vkw_dev_ccee")))
                        ldblVlrCobCcee = CDbl(IIf(IsDBNull(row.Item("vkw_cob_ccee")), 0, row.Item("vkw_cob_ccee")))
                        ldblVlrkwhDscProv = CDbl(IIf(IsDBNull(row.Item("vkwh_dsc_prov")), 0, row.Item("vkwh_dsc_prov")))
                        ldblVlrkwhDscReal = CDbl(IIf(IsDBNull(row.Item("vkwh_dsc_real")), 0, row.Item("vkwh_dsc_real")))
                        ldblVlrkwhDevCcee = CDbl(IIf(IsDBNull(row.Item("vkwh_dev_ccee")), 0, row.Item("vkwh_dev_ccee")))
                        ldblVlrkwhCobCcee = CDbl(IIf(IsDBNull(row.Item("vkwh_cob_ccee")), 0, row.Item("vkwh_cob_ccee")))
                        'Receita
                        ldblVlrRecp = CDbl(IIf(IsDBNull(row.Item("vreceita_p")), 0, row.Item("vreceita_p")))
                        ldblVlrRecfp = CDbl(IIf(IsDBNull(row.Item("vreceita_fp")), 0, row.Item("vreceita_fp")))
                        'Reativos
                        ldblVlrCRep = CDbl(IIf(IsDBNull(row.Item("vconreat_p")), 0, row.Item("vconreat_p")))
                        ldblVlrCRefp = CDbl(IIf(IsDBNull(row.Item("vconreat_fp")), 0, row.Item("vconreat_fp")))
                        ldblVlrDRep = CDbl(IIf(IsDBNull(row.Item("vdemreat_p")), 0, row.Item("vdemreat_p")))
                        ldblVlrDRefp = CDbl(IIf(IsDBNull(row.Item("vdemreat_fp")), 0, row.Item("vdemreat_fp")))
                        'Tributos
                        ldblVlrIcm = CDbl(IIf(IsDBNull(row.Item("icms")), 0, row.Item("icms")))
                        ldblVlrPis = CDbl(IIf(IsDBNull(row.Item("pis")), 0, row.Item("pis")))
                        ldblVlrCof = CDbl(IIf(IsDBNull(row.Item("cofins")), 0, row.Item("cofins")))

                        'Totais
                        ldblTotQtdCon = ldblTotQtdCon + ldblQtdCon
                        'Consumos
                        ldblTotQtdkWhp = ldblTotQtdkWhp + ldblQtdkWhp
                        ldblTotQtdkWhfp = ldblTotQtdkWhfp + ldblQtdkWhfp
                        ldblTotVlrkWhp = ldblTotVlrkWhp + ldblVlrkWhp
                        ldblTotVlrkWhfp = ldblTotVlrkWhfp + ldblVlrkWhfp
                        ldblTotVlrBanp = ldblTotVlrBanp + ldblVlrBanp
                        ldblTotVlrBanfp = ldblTotVlrBanfp + ldblVlrBanfp
                        'Livre - Encargos
                        ldblTotQtdLivp = ldblTotQtdLivp + ldblQtdLivp
                        ldblTotQtdLivfp = ldblTotQtdLivfp + ldblQtdLivfp
                        ldblTotVlrLivp = ldblTotVlrLivp + ldblVlrLivp
                        ldblTotVlrLivfp = ldblTotVlrLivfp + ldblVlrLivfp
                        'Demandas
                        ldblTotQtdkWp = ldblTotQtdkWp + ldblQtdkWp
                        ldblTotQtdkWfp = ldblTotQtdkWfp + ldblQtdkWfp
                        ldblTotVlrkWp = ldblTotVlrkWp + ldblVlrkWp
                        ldblTotVlrkWfp = ldblTotVlrkWfp + ldblVlrkWfp
                        ldblTotQtdkWup = ldblTotQtdkWup + ldblQtdkWup
                        ldblTotQtdkWufp = ldblTotQtdkWufp + ldblQtdkWufp
                        ldblTotVlrkWup = ldblTotVlrkWup + ldblVlrkWup
                        ldblTotVlrkWufp = ldblTotVlrkWufp + ldblVlrkWufp
                        'CCEE
                        ldblTotVlrDscProv = ldblTotVlrDscProv + ldblVlrDscProv
                        ldblTotVlrDscReal = ldblTotVlrDscReal + ldblVlrDscReal
                        ldblTotVlrDevCcee = ldblTotVlrDevCcee + ldblVlrDevCcee
                        ldblTotVlrCobCcee = ldblTotVlrCobCcee + ldblVlrCobCcee

                        ldblTotVlrkwhDscProv = ldblTotVlrkwhDscProv + ldblVlrkwhDscProv
                        ldblTotVlrkwhDscReal = ldblTotVlrkwhDscReal + ldblVlrkwhDscReal
                        ldblTotVlrkwhDevCcee = ldblTotVlrkwhDevCcee + ldblVlrkwhDevCcee
                        ldblTotVlrkwhCobCcee = ldblTotVlrkwhCobCcee + ldblVlrkwhCobCcee
                        'Receita
                        ldblTotVlrRecp = ldblTotVlrRecp + ldblVlrRecp
                        ldblTotVlrRecfp = ldblTotVlrRecfp + ldblVlrRecfp
                        'Reativos
                        ldblTotVlrCRep = ldblTotVlrCRep + ldblVlrCRep
                        ldblTotVlrCRefp = ldblTotVlrCRefp + ldblVlrCRefp
                        ldblTotVlrDRep = ldblTotVlrDRep + ldblVlrDRep
                        ldblTotVlrDRefp = ldblTotVlrDRefp + ldblVlrDRefp
                        'Tributos
                        ldblTotVlrIcm = ldblTotVlrIcm + ldblVlrIcm
                        ldblTotVlrPis = ldblTotVlrPis + ldblVlrPis
                        ldblTotVlrCof = ldblTotVlrCof + ldblVlrCof

                        'Detalhe
                        'lstrTabAux = lstrDesCla & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblQtdCon, "###,###,###,##0") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrDscProv, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrDscReal, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrDevCcee, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrCobCcee, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrIcm, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrPis, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrCof, "#,##0.00")

                        'lstrTabAux = "|"
                        'lstrTabAux = lstrTabAux & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrkwhDscProv, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrkwhDscReal, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrkwhDevCcee, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrkwhCobCcee, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & "|"
                        'lstrTabAux = lstrTabAux & "|"
                        ''''ifrmRelSamp.VS.Table = lstrTamCol1 & lstrTabAux

                        'lstrTabAux = "Ponta|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblQtdkWhp, "#,###0.000") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblQtdkWp, "#,###0.000") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblQtdkWup, "#,###0.000") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblQtdLivp, "#,###0.000") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrkWhp, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrBanp, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrRecp, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrLivp, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrkWp, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrkWup, "#,##0.00") & "|"
                        ''lstrTabAux = lstrTabAux & Strings.Format(ldblVlrCRep, "#,###0.000") & "|"
                        ''lstrTabAux = lstrTabAux & Strings.Format(ldblVlrDRep, "#,##0.00") & "|"

                        'lstrTabAux = "Fora Ponta|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblQtdkWhfp, "#,###0.000") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblQtdkWfp, "#,###0.000") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblQtdkWufp, "#,###0.000") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblQtdLivfp, "#,###0.000") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrkWhfp, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrBanfp, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrRecfp, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrLivfp, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrkWfp, "#,##0.00") & "|"
                        'lstrTabAux = lstrTabAux & VB6.Format(ldblVlrkWufp, "#,##0.00") & "|"
                        ''lstrTabAux = lstrTabAux & Strings.Format(ldblVlrCRefp, "#,###0.000") & "|"
                        ''lstrTabAux = lstrTabAux & Strings.Format(ldblVlrDRefp, "#,##0.00") & "|"


                        lstrSql = "INSERT INTO AUX_SAMP_NOVO_IMPRESSAOA_LIVRE SET "
                        lstrSql = lstrSql & "CLASSE = '" & lstrDesCla & "' ,"
                        lstrSql = lstrSql & "NCONS = '" & ldblQtdCon & "',"
                        lstrSql = lstrSql & "DESCPROV = '" & ldblVlrDscProv & "',"
                        lstrSql = lstrSql & "DESCREAL = '" & ldblVlrDscReal & "',"
                        lstrSql = lstrSql & "DEVCCEE = '" & ldblVlrDevCcee & "',"
                        lstrSql = lstrSql & "COBCCEE = '" & ldblVlrCobCcee & "',"
                        lstrSql = lstrSql & "DESCPROV_KWH = '" & ldblVlrkwhDscProv & "',"
                        lstrSql = lstrSql & "DESCREAL_KWH = '" & ldblVlrkwhDscReal & "',"
                        lstrSql = lstrSql & "DEVCCEE_KWH = '" & ldblVlrkwhDevCcee & "',"
                        lstrSql = lstrSql & "COBCCEE_KWH = '" & ldblVlrkwhCobCcee & "',"
                        lstrSql = lstrSql & "KWHPONTA = '" & ldblQtdkWhp & "',"
                        lstrSql = lstrSql & "KWHFORAP = '" & ldblQtdkWhfp & "',"
                        lstrSql = lstrSql & "KWPONTA = '" & ldblQtdkWp & "',"
                        lstrSql = lstrSql & "KWFORAP = '" & ldblQtdkWfp & "',"
                        lstrSql = lstrSql & "KWULTPONTA = '" & ldblQtdkWup & "',"
                        lstrSql = lstrSql & "KWULTFORAP = '" & ldblQtdkWufp & "',"
                        lstrSql = lstrSql & "KWHRSPONTA = '" & ldblVlrkWhp & "',"
                        lstrSql = lstrSql & "KWHRSFORAP = '" & ldblVlrkWhfp & "',"
                        lstrSql = lstrSql & "BANDEIRAPONTA = '" & ldblVlrBanp & "',"
                        lstrSql = lstrSql & "BANDEIRAFORAP = '" & ldblVlrBanfp & "',"
                        lstrSql = lstrSql & "RECEITAPONTA = '" & ldblVlrRecp & "',"
                        lstrSql = lstrSql & "RECEITAFORAP = '" & ldblVlrRecfp & "',"
                        lstrSql = lstrSql & "KWRSPONTA = '" & ldblVlrkWp & "',"
                        lstrSql = lstrSql & "KWRSFORAP = '" & ldblVlrkWfp & "',"
                        lstrSql = lstrSql & "KWRSULTPONTA = '" & ldblVlrkWup & "',"
                        lstrSql = lstrSql & "KWRSULTFORAP = '" & ldblVlrkWufp & "',"
                        lstrSql = lstrSql & "ENCARGOPONTA = '" & ldblVlrkWufp & "',"
                        lstrSql = lstrSql & "ENCARGOFORAP = '" & ldblVlrkWufp & "',"
                        lstrSql = lstrSql & "ENCARGORSPONTA = '" & ldblVlrkWufp & "',"
                        lstrSql = lstrSql & "ENCARGORSFORAP = '" & ldblVlrkWufp & "',"
                        lstrSql = lstrSql & "ICMS = '" & ldblVlrIcm & "',"
                        lstrSql = lstrSql & "PIS = '" & ldblVlrPis & "',"
                        lstrSql = lstrSql & "COFINS = '" & ldblVlrCof & "',"
                        lstrSql = lstrSql & "REFATURAMENTO = '" & lintRefatu & "'"
                        objExecAtualizaBancoDados.QUERY_NAME = lstrSql
                        repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)


                    Next


                End If

            Next lintRefatu

            dt = Nothing

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

    End Sub

    Private Sub cmdPasta_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdPasta.Click
        Try

            fbdSamp.ShowDialog()
            txtDir.Text = fbdSamp.SelectedPath

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

    End Sub

    Private Sub cmdSiase_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSiase.Click
        Call frmGerarDadosSiase.Show()
    End Sub

    Private Sub frmRelSampNovo_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
        Try
            Select Case KeyAscii
                Case System.Windows.Forms.Keys.Return
                    KeyAscii = 0
                    TabEnter(KeyAscii, eventArgs)
                Case System.Windows.Forms.Keys.Escape
                    Call cmdFechar_Click(cmdFechar, New System.EventArgs())
            End Select
            eventArgs.KeyChar = Chr(KeyAscii)
            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

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

    Private Sub frmRelSampNovo_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        Dim lstrAux As String
        Try
            'Call CenterForm(Me)
            'ifrmRelSamp = New FrmRelatorio
            ifrmDir = New frmDir
            istrTabela = "tmp_samp"
            istrTab945 = "tmp_samp_945"
            lstrAux = GetConfig("mes.ano.faturamento")
            If Len(Trim(lstrAux)) < 1 Then
                lstrAux = "01/" & VB6.Format(Now, "MM/yyyy")
            Else
                lstrAux = "01/" & lstrAux
            End If
            Me.dtpMesAno.Value = CDate(IIf(IsDate(lstrAux), lstrAux, Today))
            ilngLimRes = CInt(GetConfig("limite.residencial"))
            ilngLimRur = CInt(GetConfig("limite.rural"))
            istrCaminho = Trim(GetConfig("dir.geracao.samp"))
            If istrCaminho = "-1" Then
                istrCaminho = My.Application.Info.DirectoryPath
                ' Call CriaParConfig("DIR_GERACAO_SAMP", Replace(istrCaminho, "\", "\\"), False, "Caminho para gerar os arquivos samp.")
            ElseIf istrCaminho = "" Then
                istrCaminho = My.Application.Info.DirectoryPath
                'Call GravaConfig("DIR_GERACAO_SAMP", Replace(istrCaminho, "\", "\\"))
            End If
            Me.txtDir.Text = istrCaminho
            Call MontaNomeArquivo()
            iblnExporta = VerificaSeExisteExportacao()
            Me.cmdExporta.Enabled = iblnExporta
            Me.cmbCaracter.SelectedIndex = 0
            Call sCriaTabelaAuxiliares()

            lbl_Perc_Desc_Perda.Text += " " & GetConfig("percentual_perdas_tec_samp") & " %"
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmRelSampNovo_Load" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    'Private Sub frmRelSampNovo_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
    '    Call Executar("DROP TEMPORARY TABLE IF EXISTS " & istrTabela, True)
    '    Call Executar("DROP TABLE IF EXISTS " & istrTabela, True)
    '    Call Executar("DROP TEMPORARY TABLE IF EXISTS " & istrTab945)
    '    Call Executar("DROP TABLE IF EXISTS " & istrTab945)
    '    Call GravaConfig("DIR_GERACAO_SAMP", Replace(Me.txtDir.Text, "\", "\\"))
    '    UPGRADE_NOTE: Object ifrmDir may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
    '    ifrmDir = Nothing
    '    UPGRADE_NOTE: Object ifrmRelSamp may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
    '    ifrmRelSamp = Nothing
    'End Sub

    Private Sub CriaTabelaTemporaria()
        Dim lstrSql As String
        Try
            'Call Executar("DROP TABLE IF EXISTS " & istrTabela, True)
            'Call Executar("DROP TABLE IF EXISTS " & istrTabela, True)

            lstrSql = "DROP TABLE IF EXISTS " & istrTabela
            objExecAtualizaBancoDados.QUERY_NAME = lstrSql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrSql = ""
            lstrSql = lstrSql & "CREATE TABLE IF NOT EXISTS " & istrTabela & "("
            lstrSql = lstrSql & "refaturado            BIT DEFAULT 0,"
            lstrSql = lstrSql & "grupo                 VARCHAR(3)   DEFAULT NULL, "
            lstrSql = lstrSql & "classe                VARCHAR(4)   DEFAULT NULL, "
            lstrSql = lstrSql & "ds_classe             VARCHAR(200) DEFAULT NULL, "
            lstrSql = lstrSql & "com_classe            VARCHAR(200) DEFAULT NULL, "
            lstrSql = lstrSql & "ord_classe            TINYINT(2)   DEFAULT NULL, "
            lstrSql = lstrSql & "parc_cativo           BIT          DEFAULT 0, "
            lstrSql = lstrSql & "usinas                BIT          DEFAULT 0, "
            lstrSql = lstrSql & "micro_geracao         BIT          DEFAULT 0, "
            lstrSql = lstrSql & "irrigacao_aquicultura BIT          DEFAULT 0, "
            lstrSql = lstrSql & "tarifa_branca         BIT          DEFAULT 0, "

            'Convencional

            'Qtde
            lstrSql = lstrSql & "qkwh_cons    DOUBLE DEFAULT '0', " 'Kwh CONSUMIDO (ENERGIA CONSUMIDA)
            lstrSql = lstrSql & "qkwh_fat     DOUBLE DEFAULT '0', " 'Kwh FATURADO
            lstrSql = lstrSql & "qkwh_fat_ban DOUBLE DEFAULT '0', " 'Kwh FATURADO Bandeira
            lstrSql = lstrSql & "qkw_fat      DOUBLE DEFAULT '0', " 'Kw FATURADO

            'Irrigacao
            lstrSql = lstrSql & "qirr_nor_kwh_fat     DOUBLE DEFAULT '0', " 'Kwh FATURADO
            lstrSql = lstrSql & "qirr_nor_kwh_fat_ban DOUBLE DEFAULT '0', " 'Kwh FATURADO Bandeira
            lstrSql = lstrSql & "qirr_res_kwh_fat     DOUBLE DEFAULT '0', " 'Kwh FATURADO
            lstrSql = lstrSql & "qirr_res_kwh_fat_ban DOUBLE DEFAULT '0', " 'Kwh FATURADO Bandeira

            'Tarifa Branca
            lstrSql = lstrSql & "qtarbranca_kwh_ponta DOUBLE DEFAULT '0', " 'Kwh FATURADO PONTA
            lstrSql = lstrSql & "qtarbranca_kwh_ponta_ban DOUBLE DEFAULT '0', " 'Kwh FATURADO PONTA Bandeira
            lstrSql = lstrSql & "qtarbranca_kwh_inter DOUBLE DEFAULT '0', " 'Kwh FATURADO INTERMEDIARIO
            lstrSql = lstrSql & "qtarbranca_kwh_inter_ban DOUBLE DEFAULT '0', " 'Kwh FATURADO INTERMEDIARIO Bandeira
            lstrSql = lstrSql & "qtarbranca_kwh_forap DOUBLE DEFAULT '0', " 'Kwh FATURADO FORA PONTA
            lstrSql = lstrSql & "qtarbranca_kwh_forap_ban DOUBLE DEFAULT '0', " 'Kwh FATURADO FORA PONTA Bandeira

            'Valor
            lstrSql = lstrSql & "vkwh_cons    DECIMAL(14,2) DEFAULT '0.00', " 'Kwh CONSUMIDO (ENERGIA CONSUMIDA)
            lstrSql = lstrSql & "vkwh_fat     DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO
            lstrSql = lstrSql & "vkwh_fat_ban DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO Bandeira
            lstrSql = lstrSql & "vkw_fat      DECIMAL(14,2) DEFAULT '0.00', " 'Kw FATURADO
            lstrSql = lstrSql & "vconreat     DECIMAL(14,2) DEFAULT '0.00', " 'REATIVO
            lstrSql = lstrSql & "vdemreat     DECIMAL(14,2) DEFAULT '0.00', " 'REATIVO

            'Irrigacao
            lstrSql = lstrSql & "virr_nor_kwh_fat     DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO
            lstrSql = lstrSql & "virr_nor_kwh_fat_ban DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO Bandeira
            lstrSql = lstrSql & "virr_res_kwh_fat     DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO
            lstrSql = lstrSql & "virr_res_kwh_fat_ban DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO Bandeira

            'Tarifa Branca
            lstrSql = lstrSql & "vtarbranca_kwh_ponta DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO PONTA
            lstrSql = lstrSql & "vtarbranca_kwh_ponta_ban DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO PONTA Bandeira
            lstrSql = lstrSql & "vtarbranca_kwh_inter DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO INTERMEDIARIO
            lstrSql = lstrSql & "vtarbranca_kwh_inter_ban DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO INTERMEDIARIO Bandeira
            lstrSql = lstrSql & "vtarbranca_kwh_forap DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO FORA PONTA
            lstrSql = lstrSql & "vtarbranca_kwh_forap_ban DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO FORA PONTA Bandeira

            'Horosazonal

            'Qtde
            lstrSql = lstrSql & "qkwh_cons_p     DECIMAL(14,2) DEFAULT '0.00', " 'Kwh CONSUMIDO PONTA (ENERGIA CONSUMIDA)
            lstrSql = lstrSql & "qkwh_cons_fp    DECIMAL(14,2) DEFAULT '0.00', " 'Kwh CONSUMIDO FORA PONTA (ENERGIA CONSUMIDA)
            lstrSql = lstrSql & "qkwh_fat_p      DOUBLE DEFAULT '0', " 'Kwh FATURADO (ENERGIA FAT PONTA)
            lstrSql = lstrSql & "qkwh_fat_fp     DOUBLE DEFAULT '0', " 'Kwh FATURADO (ENERGIA FAT FORA PONTA)
            lstrSql = lstrSql & "qkwh_fat_p_ban  DOUBLE DEFAULT '0', " 'Kwh FATURADO (ENERGIA FAT PONTA) Bandeira
            lstrSql = lstrSql & "qkwh_fat_fp_ban DOUBLE DEFAULT '0', " 'Kwh FATURADO (ENERGIA FAT FORA PONTA) Bandeira
            lstrSql = lstrSql & "qkwh_fat_p_liv  DOUBLE DEFAULT '0', " 'Kwh FATURADO (ENERGIA FAT PONTA) Livre
            lstrSql = lstrSql & "qkwh_fat_fp_liv DOUBLE DEFAULT '0', " 'Kwh FATURADO (ENERGIA FAT FORA PONTA) Livre
            lstrSql = lstrSql & "qkwh_dev_p      DOUBLE DEFAULT '0', " 'Kwh Devolvido PONTA
            lstrSql = lstrSql & "qkwh_dev_fp     DOUBLE DEFAULT '0', " 'Kwh Devolvido FORA PONTA
            lstrSql = lstrSql & "qkwh_utr_p      DOUBLE DEFAULT '0', " 'Kwh Ultrapassado PONTA
            lstrSql = lstrSql & "qkwh_utr_fp     DOUBLE DEFAULT '0', " 'Kwh Ultrapassado FORA PONTA
            lstrSql = lstrSql & "qkw_fat_p       DOUBLE DEFAULT '0', " 'Kw FATURADO (DEMANDA FAT PONTA)
            lstrSql = lstrSql & "qkw_fat_fp      DOUBLE DEFAULT '0', " 'Kw FATURADO (DEMANDA FAT FORA PONTA)
            lstrSql = lstrSql & "qkw_fatu_p      DOUBLE DEFAULT '0', " 'Kw FATURADO ULTRAPASSAGEM P
            lstrSql = lstrSql & "qkw_fatu_fp     DOUBLE DEFAULT '0', " 'Kw FATURADO ULTRAPASSAGEM FP

            'Valor
            lstrSql = lstrSql & "vkwh_fat_p      DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO (ENERGIA FAT PONTA)
            lstrSql = lstrSql & "vkwh_fat_fp     DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO (ENERGIA FAT FORA PONTA)
            lstrSql = lstrSql & "vkwh_fat_p_ban  DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO (ENERGIA FAT PONTA) Bandeira
            lstrSql = lstrSql & "vkwh_fat_fp_ban DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO (ENERGIA FAT FORA PONTA) Bandeira
            lstrSql = lstrSql & "vkwh_fat_p_liv  DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO (ENERGIA FAT PONTA) Livre
            lstrSql = lstrSql & "vkwh_fat_fp_liv DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO (ENERGIA FAT FORA PONTA) Livre
            lstrSql = lstrSql & "vkwh_dev_p      DECIMAL(14,2) DEFAULT '0.00', " 'Kwh DEVOLVIDO (ENERGIA FAT PONTA)
            lstrSql = lstrSql & "vkwh_dev_fp     DECIMAL(14,2) DEFAULT '0.00', " 'Kwh DEVOLVIDO (ENERGIA FAT FORA PONTA)
            lstrSql = lstrSql & "vkwh_utr_p      DECIMAL(14,2) DEFAULT '0.00', " 'Kwh ULTRAPASSADO (ENERGIA FAT PONTA)
            lstrSql = lstrSql & "vkwh_utr_fp     DECIMAL(14,2) DEFAULT '0.00', " 'Kwh ULTRAPASSADO (ENERGIA FAT FORA PONTA)
            lstrSql = lstrSql & "vkw_fat_p       DECIMAL(14,2) DEFAULT '0.00', " 'Kw FATURADO (DEMANDA FAT PONTA)
            lstrSql = lstrSql & "vkw_fat_fp      DECIMAL(14,2) DEFAULT '0.00', " 'Kw FATURADO (DEMANDA FAT FORA PONTA)
            lstrSql = lstrSql & "vkw_fatu_p      DECIMAL(14,2) DEFAULT '0.00', " 'Kw FATURADO ULTRAPASSAGEM P
            lstrSql = lstrSql & "vkw_fatu_fp     DECIMAL(14,2) DEFAULT '0.00', " 'Kw FATURADO ULTRAPASSAGEM FP
            lstrSql = lstrSql & "vkw_dsc_prov    DECIMAL(14,2) DEFAULT '0.00', " 'Kw - Desconto Provisorio CCEE
            lstrSql = lstrSql & "vkw_dsc_real    DECIMAL(14,2) DEFAULT '0.00', " 'Kw - Desconto Real CCEE
            lstrSql = lstrSql & "vkw_dev_ccee    DECIMAL(14,2) DEFAULT '0.00', " 'Kw - Devolução da difereça entre o real e o provisório
            lstrSql = lstrSql & "vkw_cob_ccee    DECIMAL(14,2) DEFAULT '0.00', " 'Kw - Cobrança da diferença entre o real e o provisório
            lstrSql = lstrSql & "vkwh_dsc_prov   DECIMAL(14,2) DEFAULT '0.00', " 'Kwh - Desconto Provisorio CCEE
            lstrSql = lstrSql & "vkwh_dsc_real   DECIMAL(14,2) DEFAULT '0.00', " 'Kwh - Desconto Real CCEE
            lstrSql = lstrSql & "vkwh_dev_ccee   DECIMAL(14,2) DEFAULT '0.00', " 'Kwh - Devolução da difereça entre o real e o provisório
            lstrSql = lstrSql & "vkwh_cob_ccee   DECIMAL(14,2) DEFAULT '0.00', " 'Kwh - Cobrança da diferença entre o real e o provisório

            'Receita
            lstrSql = lstrSql & "receita_p       DECIMAL(14,2) DEFAULT '0.00', "
            lstrSql = lstrSql & "receita_i       DECIMAL(14,2) DEFAULT '0.00', "
            lstrSql = lstrSql & "receita_fp      DECIMAL(14,2) DEFAULT '0.00', "

            'Irrigacao
            lstrSql = lstrSql & "vrec_irr_nor     DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO
            lstrSql = lstrSql & "vrec_irr_nor_ban DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO Bandeira
            lstrSql = lstrSql & "vrec_irr_res     DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO

            'Tarifa Branca
            lstrSql = lstrSql & "vrectarbranca_kwh_ponta DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO PONTA
            lstrSql = lstrSql & "vrectarbranca_kwh_ponta_ban DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO PONTA Bandeira
            lstrSql = lstrSql & "vrectarbranca_kwh_inter DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO INTERMEDIARIO
            lstrSql = lstrSql & "vrectarbranca_kwh_inter_ban DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO INTERMEDIARIO Bandeira
            lstrSql = lstrSql & "vrectarbranca_kwh_forap DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO FORA PONTA
            lstrSql = lstrSql & "vrectarbranca_kwh_forap_ban DECIMAL(14,2) DEFAULT '0.00', " 'Kwh FATURADO FORA PONTA Bandeira

            '------ Reverso
            lstrSql = lstrSql & "qkwh_reverso DOUBLE, " 'Qtde kWh Reverso
            lstrSql = lstrSql & "vkwh_reverso DECIMAL(14,2) DEFAULT '0.00', " 'Vallor kWh Reverso
            lstrSql = lstrSql & "qkwh_reverso_p DOUBLE, " 'Qtde kWh Reverso ponta
            lstrSql = lstrSql & "qkwh_reverso_fp DOUBLE, " 'Qtde kWh Reverso fora ponta

            'Reativo
            lstrSql = lstrSql & "vconreat_p  DECIMAL(14,2) DEFAULT '0.00', " 'REATIVO
            lstrSql = lstrSql & "vconreat_fp DECIMAL(14,2) DEFAULT '0.00', " 'REATIVO
            lstrSql = lstrSql & "vdemreat_p  DECIMAL(14,2) DEFAULT '0.00', " 'REATIVO
            lstrSql = lstrSql & "vdemreat_fp DECIMAL(14,2) DEFAULT '0.00', " 'REATIVO

            'Tributos
            lstrSql = lstrSql & "icms         DECIMAL(14,2) DEFAULT '0.00', " 'ICMS R$
            lstrSql = lstrSql & "pis          DECIMAL(14,2) DEFAULT '0.00', " 'PIS R$
            lstrSql = lstrSql & "cofins       DECIMAL(14,2) DEFAULT '0.00', " 'COFINS R$

            'Tributacao Comercializador usado para o Livre
            lstrSql = lstrSql & "icms_st      DECIMAL(14,2) DEFAULT '0.00', " 'Icms - ST
            lstrSql = lstrSql & "pis_st       DECIMAL(14,2) DEFAULT '0.00', " 'Pis - ST
            lstrSql = lstrSql & "cofins_st    DECIMAL(14,2) DEFAULT '0.00', " 'Cofins - ST
            '*********************************************
            lstrSql = lstrSql & "baseicms     DECIMAL(14,2) DEFAULT '0.00', " 'Base ICMS R$
            lstrSql = lstrSql & "basepis      DECIMAL(14,2) DEFAULT '0.00', " 'Base PIS R$
            lstrSql = lstrSql & "basecofins   DECIMAL(14,2) DEFAULT '0.00', " 'Base Cofins R$
            lstrSql = lstrSql & "icmspiscof   DECIMAL(14,2) DEFAULT '0.00', " 'ICMS/PIS/COFINS R$

            'Compensados
            lstrSql = lstrSql & "qkwhCompensada         DECIMAL(14,2) DEFAULT '0.00', "
            lstrSql = lstrSql & "vkwhCompensada         DECIMAL(14,2) DEFAULT '0.00', "
            lstrSql = lstrSql & "vDescPercCompensada    DECIMAL(14,2) DEFAULT '0.00',"

            lstrSql = lstrSql & "qkwhCompensada_p         DECIMAL(14,2) DEFAULT '0.00', "
            lstrSql = lstrSql & "vkwhCompensada_p         DECIMAL(14,2) DEFAULT '0.00', "
            lstrSql = lstrSql & "vDescPercCompensada_p    DECIMAL(14,2) DEFAULT '0.00',"

            lstrSql = lstrSql & "qkwhCompensada_fp         DECIMAL(14,2) DEFAULT '0.00', "
            lstrSql = lstrSql & "vkwhCompensada_fp         DECIMAL(14,2) DEFAULT '0.00', "
            lstrSql = lstrSql & "vDescPercCompensada_fp    DECIMAL(14,2) DEFAULT '0.00',"

            lstrSql = lstrSql & "qkwhCompensada_i         DECIMAL(14,2) DEFAULT '0.00', "
            lstrSql = lstrSql & "vkwhCompensada_i         DECIMAL(14,2) DEFAULT '0.00', "
            lstrSql = lstrSql & "vDescPercCompensada_i    DECIMAL(14,2) DEFAULT '0.00',"

            lstrSql = lstrSql & "grpGD INT DEFAULT 0,"

            'Usinas
            lstrSql = lstrSql & "must_qtd DOUBLE DEFAULT '0', " 'MUST
            lstrSql = lstrSql & "must_vlr DECIMAL(14,2) DEFAULT '0.00', " 'MUST
            lstrSql = lstrSql & "must_des DECIMAL(14,2) DEFAULT '0.00', " 'MUST
            lstrSql = lstrSql & "must_enc DECIMAL(14,2) DEFAULT '0.00', " 'MUST

            lstrSql = lstrSql & " KEY `keyrefat` (`refaturado`), "
            lstrSql = lstrSql & " KEY `keygrupo` (`grupo`), "
            lstrSql = lstrSql & " KEY `keyclasse` (`classe`), "
            lstrSql = lstrSql & " KEY `keydsclas` (`ds_classe`), "
            lstrSql = lstrSql & " KEY `keycomcla` (`com_classe`), "
            lstrSql = lstrSql & " KEY `keyordcla` (`ord_classe`), "
            lstrSql = lstrSql & " KEY `keyparcat` (`parc_cativo`), "
            lstrSql = lstrSql & " KEY `keyunsina` (`usinas`) "
            lstrSql = lstrSql & ") ENGINE=InnoDB DEFAULT CHARSET=latin1;"
            objExecAtualizaBancoDados.QUERY_NAME = lstrSql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
            'Call Executar(lstrSql, True)


            lstrSql = "DROP TABLE IF EXISTS " & istrTab945
            objExecAtualizaBancoDados.QUERY_NAME = lstrSql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            'Call Executar("DROP TABLE IF EXISTS " & istrTab945)

            lstrSql = ""
            lstrSql = lstrSql & " CREATE TABLE IF NOT EXISTS " & istrTab945 & "("
            lstrSql = lstrSql & " refaturado              TINYINT(1)   DEFAULT 0,"
            lstrSql = lstrSql & " ds_classe               VARCHAR(200) DEFAULT '', "
            lstrSql = lstrSql & " ord_classe              TINYINT(2)   DEFAULT 0, "
            lstrSql = lstrSql & " faixa                   INT(11)      DEFAULT 0,"
            lstrSql = lstrSql & " consumo                 DOUBLE       DEFAULT 0,"
            lstrSql = lstrSql & " valor                   DOUBLE       DEFAULT 0,"
            lstrSql = lstrSql & " bandeira                DOUBLE       DEFAULT 0,"
            lstrSql = lstrSql & " receita                 DOUBLE       DEFAULT 0,"
            lstrSql = lstrSql & " icms                    DOUBLE       DEFAULT 0,"
            lstrSql = lstrSql & " pis                     DOUBLE       DEFAULT 0,"
            lstrSql = lstrSql & " cofins                  DOUBLE       DEFAULT 0,"
            lstrSql = lstrSql & " consumo_lido            DOUBLE       DEFAULT 0,"
            lstrSql = lstrSql & " qkwhCompensada          DECIMAL(14,2) DEFAULT '0.00', "
            lstrSql = lstrSql & " vkwhCompensada          DECIMAL(14,2) DEFAULT '0.00', "
            lstrSql = lstrSql & " vDescPercCompensada     DECIMAL(14,2) DEFAULT '0.00', "
            lstrSql = lstrSql & " grpGD      INT          DEFAULT 0,"
            lstrSql = lstrSql & " KEY `keyrefat` (`refaturado`),"
            lstrSql = lstrSql & " KEY `keydsclas` (`ds_classe`), "
            lstrSql = lstrSql & " KEY `keyordcla` (`ord_classe`) "
            lstrSql = lstrSql & ") ENGINE=InnoDB DEFAULT CHARSET=latin1;"
            objExecAtualizaBancoDados.QUERY_NAME = lstrSql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

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

    Private Function MontaTabela() As Boolean
        Dim lblnRetorno As Boolean
        Dim ldteIni As Date
        Dim ldteFim As Date
        Dim lintContInsertPri As Short
        '
        Dim llngAnoMes As Integer
        Dim lstrFase As String
        Dim lstrItens As String 'itens faturados
        Dim lintOrdCla As Short 'Ordem de impressao
        Dim lstrClasse As String 'Código da classe
        Dim lstrDsClas As String 'Descrição classe
        Dim lstrCmClas As String 'Complemento classe
        Dim lstrGrupo As String 'Grupo
        Dim lstrTensao As String
        Dim llngUnidId As Integer
        Dim lintLivreEspecial As Short 'Se é consumidor livre especial
        Dim lintParcialmenteCativo As Short 'Se é consumidor livre mas parcialmente cativo
        Dim lintLivreVerde As Short
        Dim lintMicroGeracao As Short
        Dim lintIrrigacaoAquicultura As Short
        Dim lintTarifaBranca As Short
        Dim lstrFlgst As String 'Situação Fatura
        Dim ldteFlgSt As Date
        Dim lstrFlgRe As String 'Refaturamento
        Dim lintFlgRe As Short
        Dim lintMpr As Short 'Usado para caso tiver alguma fatura cancelada no mês de faturamento de outro mês

        'Controle Baixa Renda REH 945
        Dim lintFaixa As Short
        Dim lstrDes945(1, 3) As String
        Dim lintQtdFxa(1, 3) As Short

        Dim ldblQtdkWhLido(1, 3) As Double
        Dim ldblQtdkWh(1, 3) As Double
        Dim ldblVlrkWh(1, 3) As Double
        Dim ldblBankWh(1, 3) As Double

        Dim ldblQtd945(1, 3) As Double
        Dim ldblVlr945(1, 3) As Double
        Dim ldblBan945(1, 3) As Double

        Dim ldblVlrIcm(1, 3) As Double
        Dim ldblVlrPis(1, 3) As Double
        Dim ldblVlrCof(1, 3) As Double

        Dim ldbl_qkwhCompensada(1, 3) As Double
        Dim ldbl_vkwhCompensada(1, 3) As Double
        Dim ldbl_vDescPercCompensada(1, 3) As Double
        Dim lintgrpGD(1, 3) As String

        'Convencional
        'Qiantidade
        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 ldblQKwhFatfp As Double 'Qtde Consumo Faturado fp
        Dim ldblQKwhFatpBan As Double 'Qtde Consumo Faturado p 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 ldblQKwFatUp As Double 'Qtde Demanda Ultrapassagem p
        Dim ldblQKwFatUfp As Double 'Qtde Demanda Ultrapassagem fp
        '
        Dim ldblVKwhFatp As Double 'Valor Consumo faturado p
        Dim ldblVKwhFatfp As Double 'Valor Consumo faturado fp
        Dim ldblVKwhFatpBan As Double 'Valor Consumo faturado p 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 ldblVKwFatUp As Double 'Valor Demanda Ultrapassagem p
        Dim ldblVKwFatUfp As Double 'Valor Demanda Ultrapassagem fp
        '
        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
        '
        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 ldblReceita_p As Double
        Dim ldblReceita_I As Double
        Dim ldblReceita_fp As Double
        '
        Dim ldblIcms 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 ldblBaseIcms As Double 'Valor 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 ldblQtdMust As Double 'qtde
        Dim ldblVlrMust As Double 'valor
        Dim ldblEncMust As Double 'encargo
        Dim ldblDesMust As Double 'desconto
        '
        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)
        'Irrigacao
        Dim ldblQIrr_Normal_kWh_fat As Double
        Dim ldblQIrr_Reserv_kWh_fat As Double
        Dim ldblVIrr_Normal_kWh_fat As Double
        Dim ldblVIrr_Normal_kWh_fat_ban As Double
        Dim ldblVIrr_Reserv_kWh_fat As Double
        Dim ldblVIrr_Reserv_kWh_fat_ban As Double
        Dim ldblTarifa_irrigante As Double
        Dim ldblTarifa_irrigante_ban As Double
        Dim ldblVIrr_Desconto As Double
        Dim ldblVIrr_Desconto_ban As Double
        'Tarifa Branca
        Dim ldblQTarBranca_kWh_Ponta As Double
        Dim ldblQTarBranca_kWh_Ponta_Ban As Double
        Dim ldblQTarBranca_kWh_Inter As Double
        Dim ldblQTarBranca_kWh_Inter_Ban As Double
        Dim ldblQTarBranca_kWh_ForaP As Double
        Dim ldblQTarBranca_kWh_ForaP_Ban As Double
        Dim ldblVTarBranca_kWh_Ponta As Double
        Dim ldblVTarBranca_kWh_Ponta_Ban As Double
        Dim ldblVTarBranca_kWh_Inter As Double
        Dim ldblVTarBranca_kWh_Inter_Ban As Double
        Dim ldblVTarBranca_kWh_ForaP As Double
        Dim ldblVTarBranca_kWh_ForaP_Ban As Double

        Dim ldblTarifa As Double

        'Compensados
        Dim ldblQKwhReversop As Double
        Dim ldblQKwhReversofp As Double

        Dim ldblQKwhConsumidop As Double
        Dim ldblQKwhConsumidofp As Double

        Dim ldblQKwhComp As Double
        Dim ldblVKwhComp As Double
        Dim ldblVDescPercComp As Double

        Dim ldblQKwhComp_p As Double
        Dim ldblVKwhComp_p As Double
        Dim ldblVDescPercComp_p As Double

        Dim ldblQKwhComp_fp As Double
        Dim ldblVKwhComp_fp As Double
        Dim ldblVDescPercComp_fp As Double

        Dim ldblQKwhComp_i As Double
        Dim ldblVKwhComp_i As Double
        Dim ldblVDescPercComp_i As Double

        Dim lstrGrupoGD As String


        Dim lintRef As Short
        Dim lintAux As Short
        Dim lstrSql As String
        Dim lstrSqlPri As String
        Dim lstrSqlPAx As String

        Try
            '
            Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
            Call LigDes(False)
            Me.lblInfo.Text = " Buscando faturas aguarde..."
            Call Me.lblInfo.Refresh()
            Me.lblPerc.Text = ""
            Call Me.lblPerc.Refresh()
            Me.lblInfoArq.Text = ""
            Call Me.lblInfoArq.Refresh()
            Me.lblPercArq.Text = ""
            Call Me.lblPercArq.Refresh()
            Me.lblInfoPre.Text = ""
            Call Me.lblInfoPre.Refresh()
            Me.lblPercPre.Text = ""
            Call Me.lblPercPre.Refresh()
            Me.ProgressBar.Value = 0
            Me.ProgressBarArq.Value = 0
            Me.ProgressBarPre.Value = 0
            System.Windows.Forms.Application.DoEvents()
            '
            ldteFim = CDate(UltimoDiaMes(Month(Me.dtpMesAno.Value), Year(Me.dtpMesAno.Value)) & "/" & VB6.Format(Me.dtpMesAno.Value, "MM/yyyy"))
            ldteIni = CDate("01/" & VB6.Format(ldteFim, "MM/yyyy"))

            lstrSql = ""
            lstrSql = lstrSql & " SELECT * " & vbCrLf
            lstrSql = lstrSql & "  FROM `nf` " & vbCrLf
            lstrSql = lstrSql & " WHERE ((`anomes_fat` = " & CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")) & " " & vbCrLf
            lstrSql = lstrSql & "   AND `mesano` = '" & VB6.Format(Me.dtpMesAno.Value, "MM/yyyy") & "') " & vbCrLf
            lstrSql = lstrSql & "   AND (`flgst` <> 'C' OR " & vbCrLf
            lstrSql = lstrSql & "        ( (`flgst` = 'C' AND `datast` > '" & VB6.Format(ldteFim, "yyyy-MM-dd") & "') OR " & vbCrLf
            lstrSql = lstrSql & "          (`flgst` = 'C' AND `mesano_ref` <> REPLACE(LEFT(`datast`, 7),'-','') AND `datast` BETWEEN '" & VB6.Format(ldteIni, "yyyy-MM-dd") & "' AND '" & VB6.Format(ldteFim, "yyyy-MM-dd") & "')" & vbCrLf
            lstrSql = lstrSql & "         )" & vbCrLf
            lstrSql = lstrSql & "       ))" & vbCrLf
            'lstrSql = lstrSql & " and (`ta` like '%AA%' OR `ta` like '%AV%') AND `MICRO_GERACAO` = 0"

            objAtualizaBancoDados.QUERY_NAME = lstrSql
            dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            If dt.Rows.Count > 0 Then
                Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, dt.Rows.Count, " Faturas ")
                lstrSqlPri = "INSERT INTO " & istrTabela
                lstrSqlPri = lstrSqlPri & " (refaturado, grupo, classe, ds_classe, com_classe, ord_classe, parc_cativo, usinas, micro_geracao, irrigacao_aquicultura, tarifa_branca, "
                'Convencional
                'Qtde
                lstrSqlPri = lstrSqlPri & "qkwh_cons, qkwh_fat, qkwh_fat_ban, qkw_fat, "
                'Valor
                lstrSqlPri = lstrSqlPri & "vkwh_cons, vkwh_fat, vkwh_fat_ban, vkw_fat, vconreat, vdemreat, "
                'Horosazonal
                'Qtde
                lstrSqlPri = lstrSqlPri & "qkwh_cons_p,     qkwh_cons_fp, "
                lstrSqlPri = lstrSqlPri & "qkwh_fat_p,      qkwh_fat_fp,    qkwh_fat_p_ban,     qkwh_fat_fp_ban,    qkwh_fat_p_liv,     qkwh_fat_fp_liv, "
                lstrSqlPri = lstrSqlPri & "qkwh_dev_p,      qkwh_dev_fp,    qkwh_utr_p,         qkwh_utr_fp, "
                lstrSqlPri = lstrSqlPri & "qkw_fat_p,       qkw_fat_fp,     qkw_fatu_p,         qkw_fatu_fp, "
                'Valor
                lstrSqlPri = lstrSqlPri & "vkwh_fat_p,      vkwh_fat_fp,    vkwh_fat_p_ban,     vkwh_fat_fp_ban,    vkwh_fat_p_liv,     vkwh_fat_fp_liv, "
                lstrSqlPri = lstrSqlPri & "vkwh_dev_p,      vkwh_dev_fp,    vkwh_utr_p,         vkwh_utr_fp,        vkw_fat_p,          vkw_fat_fp,         vkw_fatu_p,         vkw_fatu_fp, "
                lstrSqlPri = lstrSqlPri & "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
                lstrSqlPri = lstrSqlPri & "vconreat_p,      vconreat_fp,    vdemreat_p,         vdemreat_fp, "
                'Receita
                lstrSqlPri = lstrSqlPri & "receita_p,       receita_i,      receita_fp, "
                'Reverso
                lstrSqlPri = lstrSqlPri & "qkwh_reverso,    vkwh_reverso,   qkwh_reverso_p,     qkwh_reverso_fp,"
                'Irrigacao
                lstrSqlPri = lstrSqlPri & "qirr_nor_kwh_fat, qirr_nor_kwh_fat_ban, qirr_res_kwh_fat, qirr_res_kwh_fat_ban, "
                lstrSqlPri = lstrSqlPri & "virr_nor_kwh_fat, virr_nor_kwh_fat_ban, virr_res_kwh_fat, virr_res_kwh_fat_ban, "
                'Tarifa Branca
                lstrSqlPri = lstrSqlPri & "qtarbranca_kwh_ponta, qtarbranca_kwh_ponta_ban, qtarbranca_kwh_inter, qtarbranca_kwh_inter_ban, qtarbranca_kwh_forap, qtarbranca_kwh_forap_ban, "
                lstrSqlPri = lstrSqlPri & "vtarbranca_kwh_ponta, vtarbranca_kwh_ponta_ban, vtarbranca_kwh_inter, vtarbranca_kwh_inter_ban, vtarbranca_kwh_forap, vtarbranca_kwh_forap_ban, "

                'Compensado
                lstrSqlPri = lstrSqlPri & "qkwhCompensada, "
                lstrSqlPri = lstrSqlPri & "vkwhCompensada, "
                lstrSqlPri = lstrSqlPri & "vDescPercCompensada, "

                lstrSqlPri = lstrSqlPri & "qkwhCompensada_p, "
                lstrSqlPri = lstrSqlPri & "vkwhCompensada_p, "
                lstrSqlPri = lstrSqlPri & "vDescPercCompensada_p, "

                lstrSqlPri = lstrSqlPri & "qkwhCompensada_fp, "
                lstrSqlPri = lstrSqlPri & "vkwhCompensada_fp, "
                lstrSqlPri = lstrSqlPri & "vDescPercCompensada_fp, "

                lstrSqlPri = lstrSqlPri & "qkwhCompensada_i, "
                lstrSqlPri = lstrSqlPri & "vkwhCompensada_i, "
                lstrSqlPri = lstrSqlPri & "vDescPercCompensada_i, "

                lstrSqlPri = lstrSqlPri & "grpGD, "

                'Tributos
                lstrSqlPri = lstrSqlPri & "icms, pis, cofins, "

                'Tributacao Comercializador usado para o Livre
                lstrSqlPri = lstrSqlPri & "icms_st, pis_st, cofins_st, "

                'Base de calculo
                lstrSqlPri = lstrSqlPri & "baseicms, basepis, basecofins, icmspiscof, "

                'Usinas
                lstrSqlPri = lstrSqlPri & "must_qtd, must_vlr, must_des, must_enc) VALUES "
                lstrSqlPAx = ""
                lintContInsertPri = 1
                For Each row As DataRow In dt.Rows

                    llngAnoMes = IIf(IsDBNull(row.Item("mesano_ref")), 0, row.Item("mesano_ref"))
                    lstrFlgRe = Trim(IIf(IsDBNull(row.Item("flgmedfat")), "N", row.Item("flgmedfat")))
                    If lstrFlgRe = "" Then lstrFlgRe = "N"
                    If lstrFlgRe = "S" Then
                        lintFlgRe = 1
                    Else
                        lintFlgRe = 0
                    End If
                    lstrItens = IIf(IsDBNull(row.Item("itensserv")), "", row.Item("itensserv"))
                    lstrClasse = IIf(IsDBNull(row.Item("codclas")), "0000", row.Item("codclas"))
                    lstrGrupo = Mid(IIf(IsDBNull(row.Item("ta")), "", row.Item("ta")), 3, 2)
                    lstrFase = IIf(IsDBNull(row.Item("fase")), "M", row.Item("fase"))
                    If Mid(lstrGrupo, 1) = "B" Then
                        If lstrFase = "M" Then
                            lstrTensao = "220"
                        ElseIf lstrFase = "B" Then
                            lstrTensao = "220"
                        ElseIf lstrFase = "T" Then
                            lstrTensao = "380"
                        End If
                    ElseIf Mid(lstrGrupo, 1) = "A" Then
                        lstrTensao = IIf(IsDBNull(row.Item("tensao")), "380", row.Item("tensao"))
                    End If

                    llngUnidId = IIf(IsDBNull(row.Item("unid_id")), 0, row.Item("unid_id"))
                    lintLivreEspecial = IIf(IsDBNull(row.Item("livreespecial")), 0, row.Item("livreespecial"))
                    lintParcialmenteCativo = IIf(IsDBNull(row.Item("parcialmente_cativo")), 0, row.Item("parcialmente_cativo"))
                    lintLivreVerde = IIf(IsDBNull(row.Item("livre_verde")), 0, row.Item("livre_verde"))
                    lintMicroGeracao = IIf(IsDBNull(row.Item("micro_geracao")), 0, row.Item("micro_geracao"))
                    lintIrrigacaoAquicultura = IIf(IsDBNull(row.Item("irrigacao_aquicultura")), 0, row.Item("irrigacao_aquicultura"))
                    lintTarifaBranca = IIf(IsDBNull(row.Item("tarifa_branca")), 0, row.Item("tarifa_branca"))
                    lstrFlgst = Trim(IIf(IsDBNull(row.Item("flgst")), "", row.Item("flgst")))
                    ldteFlgSt = CDate(IIf(IsDBNull(row.Item("datast")), "1900-01-01", row.Item("datast")))
                    If lstrFlgst = "C" Then
                        If ldteFlgSt <= ldteFim Then
                            lintMpr = -1
                        Else
                            lintMpr = 1
                        End If
                    Else
                        lintMpr = 1
                    End If
                    'Convencional
                    'Quantidade
                    ldblQKwhCons = 0 'Consumo lido
                    ldblQKwhFat = 0 'Consumo faturado
                    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
                    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
                    ldblQKwhFatfp = 0 'Qtde Consumo faturado fp
                    ldblQKwhFatpBan = 0 'Qtde Consumo faturado p 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
                    '
                    ldblQKwFatp = 0 'Qtde Demanda p
                    ldblQKwFatfp = 0 'Qtde Demanda fp
                    ldblQKwFatUp = 0 'Qtde Demanda de ultrapassagem p
                    ldblQKwFatUfp = 0 'Qtde Demanda de ultrapassagem fp
                    'Valor
                    ldblVKwhFatp = 0 'Valor Consumo faturado p
                    ldblVKwhFatfp = 0 'Valor Consumo faturado fp
                    ldblVKwhFatpBan = 0 'Valor Consumo faturado p 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
                    ldblVKwFatUp = 0 'Valor Demanda de ultrapassagem p
                    ldblVKwFatUfp = 0 'Valor Demanda de ultrapassagem 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
                    '
                    ldblReceita_p = 0
                    ldblReceita_fp = 0
                    '----
                    ldblIcms = 0 'Vlr Icms.
                    ldblPis = 0 'Vlr PIS.
                    ldblCofins = 0 'Vlr COFINS.
                    '
                    ldblIcmsSt = 0
                    ldblPisSt = 0
                    ldblCofinsSt = 0
                    '
                    ldblBaseIcms = 0 'Vlr Base Icms.
                    ldblBasePis = 0 'Vlr Base PIS.
                    ldblBaseCofins = 0 'Vlr Base COFINS.
                    '
                    ldblIcmsPisCof = 0 'Vlr Icms/Pis/Cofins.
                    ldblQtdMust = 0 'MUST
                    ldblVlrMust = 0 'MUST
                    ldblEncMust = 0 'MUST
                    ldblDesMust = 0 '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
                    'Irrigacao
                    ldblQIrr_Normal_kWh_fat = 0
                    ldblQIrr_Reserv_kWh_fat = 0
                    ldblVIrr_Normal_kWh_fat = 0
                    ldblVIrr_Normal_kWh_fat_ban = 0
                    ldblVIrr_Reserv_kWh_fat = 0
                    ldblVIrr_Reserv_kWh_fat_ban = 0
                    ldblTarifa_irrigante = 0
                    ldblTarifa_irrigante_ban = 0
                    ldblVIrr_Desconto = 0
                    ldblVIrr_Desconto_ban = 0
                    'Tarifa Branca
                    ldblQTarBranca_kWh_Ponta = 0
                    ldblQTarBranca_kWh_Ponta_Ban = 0
                    ldblQTarBranca_kWh_Inter = 0
                    ldblQTarBranca_kWh_Inter_Ban = 0
                    ldblQTarBranca_kWh_ForaP = 0
                    ldblQTarBranca_kWh_ForaP_Ban = 0
                    ldblVTarBranca_kWh_Ponta = 0
                    ldblVTarBranca_kWh_Ponta_Ban = 0
                    ldblVTarBranca_kWh_Inter = 0
                    ldblVTarBranca_kWh_Inter_Ban = 0
                    ldblVTarBranca_kWh_ForaP = 0
                    ldblVTarBranca_kWh_ForaP_Ban = 0

                    ldblQKwhConsumidop = 0
                    ldblQKwhConsumidofp = 0



                    ldbl_qkwhCompensada(lintFlgRe, 0) = 0
                    ldbl_qkwhCompensada(lintFlgRe, 1) = 0
                    ldbl_qkwhCompensada(lintFlgRe, 2) = 0
                    ldbl_qkwhCompensada(lintFlgRe, 3) = 0
                    ldbl_vkwhCompensada(lintFlgRe, 0) = 0
                    ldbl_vkwhCompensada(lintFlgRe, 1) = 0
                    ldbl_vkwhCompensada(lintFlgRe, 2) = 0
                    ldbl_vkwhCompensada(lintFlgRe, 3) = 0
                    ldbl_vDescPercCompensada(lintFlgRe, 0) = 0
                    ldbl_vDescPercCompensada(lintFlgRe, 1) = 0
                    ldbl_vDescPercCompensada(lintFlgRe, 2) = 0
                    ldbl_vDescPercCompensada(lintFlgRe, 3) = 0
                    ldblQKwhComp = 0
                    ldblVKwhComp = 0
                    ldblVDescPercComp = 0
                    'ldblVDescPercCompTE = 0
                    ldblQKwhComp_p = 0
                    ldblVKwhComp_p = 0
                    ldblVDescPercComp_p = 0
                    'ldblVDescPercComp_p_TE = 0
                    ldblQKwhComp_fp = 0
                    ldblVKwhComp_fp = 0
                    ldblVDescPercComp_fp = 0
                    'ldblVDescPercComp_fp_TE = 0
                    ldblQKwhComp_i = 0
                    ldblVKwhComp_i = 0
                    ldblVDescPercComp_i = 0
                    'ldblVDescPercComp_i_TE = 0

                    ldblTarifa = 0

                    lstrGrupoGD = BuscaVlrItem(lstrItens, "0315010090")

                    '************
                    ldblBaseIcms = (BuscaVlrItem(lstrItens, "0302030001") + BuscaVlrItem(lstrItens, "0302030003")) * lintMpr
                    ldblBasePis = BuscaVlrItem(lstrItens, "0302020033") * lintMpr
                    ldblBaseCofins = BuscaVlrItem(lstrItens, "302020034") * lintMpr
                    Select Case VB.Left(lstrClasse, 1)
                        Case Is = CStr(9)
                            If VB.Right(lstrClasse, 1) <> "1" Then
                                lstrDsClas = "Bx.Renda"
                                lintOrdCla = 20
                            Else
                                lstrDsClas = "Residencial Normal"
                                lintOrdCla = 10
                            End If
                        Case Is = CStr(4)
                            lstrDsClas = "Comercial"
                            lintOrdCla = 40
                        Case Is = CStr(5)
                            lstrDsClas = "Rural"
                            lintOrdCla = 50
                            If VB.Left(lstrClasse, 2) = "55" And lintIrrigacaoAquicultura = 1 Then
                                lstrDsClas = "Rural Irrigante"
                                lintOrdCla = 55
                            ElseIf VB.Left(lstrClasse, 2) = "57" And lintIrrigacaoAquicultura = 1 Then
                                lstrDsClas = "Rural Aquicultor"
                                lintOrdCla = 57
                            ElseIf VB.Left(lstrClasse, 2) = "52" Then
                                lstrDsClas = "Rural Coop. Eletrif. Rural"
                                lintOrdCla = 52
                            ElseIf VB.Left(lstrClasse, 2) = "54" Then
                                lstrDsClas = "Rural Serv. Púb. Irrigação"
                                lintOrdCla = 54
                            End If
                        Case Is = CStr(7)
                            lstrDsClas = "Serviço Público"
                            lintOrdCla = 70
                            If VB.Left(lstrClasse, 2) = "71" Then
                                lstrDsClas = "Serviço Público (tração elétrica)"
                                lintOrdCla = 71
                            ElseIf VB.Left(lstrClasse, 2) = "73" Then
                                lstrDsClas = "Serviço Público de Irrigação"
                                lintOrdCla = 73
                            End If
                        Case Is = CStr(8)
                            lstrDsClas = "Consumo Próprio"
                            lintOrdCla = 80
                        Case Is < CStr(4)
                            lstrDsClas = "Industrial"
                            lintOrdCla = 30
                        Case Is = CStr(6)
                            If VB.Left(lstrClasse, 4) = "6401" Then
                                lstrDsClas = "Ilum.Púb.(Rede)"
                                lintOrdCla = 61
                            ElseIf VB.Left(lstrClasse, 4) = "6402" Then
                                lstrDsClas = "Ilum.Púb.(Bulbo)"
                                lintOrdCla = 62
                            Else
                                lstrDsClas = "Poder Público"
                                lintOrdCla = 60
                            End If
                        Case Else
                            lstrDsClas = "Indefinido"
                            lintOrdCla = 90
                    End Select


                    If lstrGrupo = "AS" Or lstrGrupo = "A4" Or VB.Left(lstrGrupo, 1) = "B" Then
                        'Quantidade
                        If VB.Left(lstrGrupo, 1) <> "B" Then
                            ldblQKwhCons = BuscaVlrItem(lstrItens, "030201008") * lintMpr 'Consumo lido
                            ldblQKwhFat = BuscaVlrItem(lstrItens, "030201009") * lintMpr 'Consumo faturado

                            ldblQKwhFatBan = BuscaVlrItem(lstrItens, "0302110009") * lintMpr 'Consumo faturado bandeira
                            ldblVKwhCons = BuscaVlrItem(lstrItens, "0302020008") * lintMpr 'Consumo lido

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

                            ldblReceita_fp = ldblReceita_fp + ldblVKwhFat + ldblVKwhFatBan

                            If lstrGrupo = "AS" Then
                                lstrDsClas = "Suprimento"
                            End If
                        Else
                            ldblQKwhCons = BuscaVlrItem(lstrItens, "030201008") * lintMpr 'Consumo lido
                            ldblQKwhFat = BuscaVlrItem(lstrItens, "030201009") * lintMpr 'Consumo faturado
                            ldblQKwhFatBan = BuscaVlrItem(lstrItens, "030211009") * lintMpr 'Consumo faturado bandeira
                            ldblVKwhCons = BuscaVlrItem(lstrItens, "030202008") * lintMpr 'Consumo lido
                            ldblVKwhFat = BuscaVlrItem(lstrItens, "030202009") * lintMpr 'Consumo faturado
                            ldblVKwhFatBan = BuscaVlrItem(lstrItens, "030212009") * lintMpr 'Consumo faturado bandeira
                            '
                            If lintIrrigacaoAquicultura = 1 Then
                                ldblTarifa_irrigante = BuscaVlrItem(lstrItens, "01B2011101")
                                ldblTarifa_irrigante_ban = BuscaVlrItem(lstrItens, "10B2011101")
                                If ldblTarifa_irrigante_ban = 0 Then
                                    ldblTarifa_irrigante_ban = BuscaVlrItem(lstrItens, "20B2011101")
                                End If
                                If ldblTarifa_irrigante_ban = 0 Then
                                    ldblTarifa_irrigante_ban = BuscaVlrItem(lstrItens, "30B2011101")
                                End If
                                ldblVIrr_Desconto = (BuscaVlrItem(lstrItens, "9070000009") * -1) * lintMpr
                                ldblVIrr_Desconto_ban = (BuscaVlrItem(lstrItens, "9070000008") * -1) * lintMpr

                                ldblQIrr_Reserv_kWh_fat = BuscaVlrItem(lstrItens, "0302990054") * lintMpr
                                ldblQIrr_Normal_kWh_fat = ldblQKwhFat - ldblQIrr_Reserv_kWh_fat

                                ldblVIrr_Normal_kWh_fat = Truncate(ldblQIrr_Normal_kWh_fat * ldblTarifa_irrigante, 2)
                                ldblVIrr_Normal_kWh_fat_ban = Truncate(ldblQIrr_Normal_kWh_fat * ldblTarifa_irrigante_ban, 2)

                                ldblVIrr_Reserv_kWh_fat = Truncate(ldblQIrr_Reserv_kWh_fat * ldblTarifa_irrigante, 2) '+ ldblVIrr_Desconto
                                ldblVIrr_Reserv_kWh_fat_ban = Truncate(ldblQIrr_Reserv_kWh_fat * ldblTarifa_irrigante_ban, 2) '+ ldblVIrr_Desconto_ban
                            End If

                            If lintTarifaBranca = 1 Then
                                ldblQTarBranca_kWh_Ponta = BuscaValorItem(lstrItens, "0302010190") * lintMpr
                                ldblQTarBranca_kWh_Ponta_Ban = BuscaValorItem(lstrItens, "0302110190") * lintMpr
                                ldblQTarBranca_kWh_Inter = BuscaValorItem(lstrItens, "0302010290") * lintMpr
                                ldblQTarBranca_kWh_Inter_Ban = BuscaValorItem(lstrItens, "0302110290") * lintMpr
                                ldblQTarBranca_kWh_ForaP = BuscaValorItem(lstrItens, "0302010390") * lintMpr
                                ldblQTarBranca_kWh_ForaP_Ban = BuscaValorItem(lstrItens, "0302110390") * lintMpr

                                ldblVTarBranca_kWh_Ponta = BuscaValorItem(lstrItens, "0302020190") * lintMpr
                                ldblVTarBranca_kWh_Ponta_Ban = BuscaValorItem(lstrItens, "0302120190") * lintMpr
                                ldblVTarBranca_kWh_Inter = BuscaValorItem(lstrItens, "0302020290") * lintMpr
                                ldblVTarBranca_kWh_Inter_Ban = BuscaValorItem(lstrItens, "0302120290") * lintMpr
                                ldblVTarBranca_kWh_ForaP = BuscaValorItem(lstrItens, "0302020390") * lintMpr
                                ldblVTarBranca_kWh_ForaP_Ban = BuscaValorItem(lstrItens, "0302120390") * lintMpr


                                ldblReceita_p = ldblReceita_p + ldblVTarBranca_kWh_Ponta + ldblVTarBranca_kWh_Ponta_Ban
                                ldblReceita_I = ldblReceita_I + ldblVTarBranca_kWh_Inter + ldblVTarBranca_kWh_Inter_Ban
                                ldblReceita_fp = ldblReceita_fp + ldblVTarBranca_kWh_ForaP + ldblVTarBranca_kWh_ForaP_Ban
                            End If
                            '
                            ldblReceita_fp = ldblReceita_fp + ldblVKwhFat + ldblVKwhFatBan
                            '
                            ldblQKwhFatTU = BuscaVlrItem(lstrItens, "0303010090") * lintMpr 'consumo faturado TUSD
                            ldblQkWhFatTE = BuscaVlrItem(lstrItens, "0304010090") * lintMpr 'consumo faturado TE
                            ldblQkWhFatTEBan = BuscaVlrItem(lstrItens, "030411009") * lintMpr 'consumo faturado TE bandeira
                            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
                            ldblQKwhReverso = BuscaVlrItem(lstrItens, "0302020996") * lintMpr 'Qtde Reverso
                            ldblVKwhReverso = BuscaVlrItem(lstrItens, "0302020994") * lintMpr 'Valor Reverso

                            If lstrGrupo = "B1" Then
                                ldblTarifa = BuscaVlrItem(lstrItens, "01B1011101") * lintMpr 'Tarifa
                            ElseIf lstrGrupo = "B2" Then
                                ldblTarifa = BuscaVlrItem(lstrItens, "01B2011101") * lintMpr 'Tarifa
                            ElseIf lstrGrupo = "B3" Then
                                ldblTarifa = BuscaVlrItem(lstrItens, "01B3301101") * lintMpr 'Tarifa
                            End If

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

                            If ldblQKwhReverso > 0 Then
                                If ldblVKwhReverso = 0 Then ldblVKwhReverso = Truncate(ldblQKwhReverso * ldblTarifa, 2)
                                If ldblVKwhReverso < 0 Then ldblVKwhReverso *= -1
                            End If

                            'fator de conversao 1ah em 220 = 0.22 em 380 = .38
                            '1436Ah em 220v = 315.92 kWh
                            '1436Ah em 380v = 545.68 kWh
                            '820Ah em 220v = 180.4 kWh
                            '820Ah em 380v = 311.6 kWh
                            'If llngAnoMes <= 201512 And lintMicroGeracao = -1 And Mid(lstrGrupo, 1, 1) = "B" Then
                            If lintMicroGeracao = 1 And Mid(lstrGrupo, 1, 1) = "B" Then

                                If ldblQKwhFat = 1158 And llngAnoMes = 202211 Then
                                    GoTo pula
                                End If

                                'COMENTADO POR 
                                'ldblQKwhFat = ldblQKwhFat - ldblQKwhReverso
                                'If lstrFase = "M" And ldblQKwhFat < 30 Then
                                '    ldblQKwhFat = 30
                                'ElseIf lstrFase = "B" And ldblQKwhFat < 50 Then
                                '    ldblQKwhFat = 50
                                'ElseIf lstrFase = "T" And ldblQKwhFat < 100 Then
                                '    ldblQKwhFat = 100
                                'End If
pula:

                            End If
                            If VB.Left(lstrClasse, 1) = "4" Then
                                If ((lstrTensao = "220" And ldblVKwhFat <= 180.4) Or (lstrTensao = "380" And ldblVKwhFat <= 311.6)) Then
                                    lintOrdCla = 41
                                ElseIf ((lstrTensao = "220" And ldblVKwhFat <= 315.92) Or (lstrTensao = "380" And ldblVKwhFat <= 545.68)) Then
                                    lintOrdCla = 42
                                End If
                            End If

                            If lstrFlgRe = "S" Then
                                lintFlgRe = 1
                            Else
                                lintFlgRe = 0
                            End If

                            If InStr(1, lstrDsClas, "Bx.Renda") > 0 Then

                                ldblQtdkWhLido(lintFlgRe, 0) = BuscaVlrItem(lstrItens, "0302010080") 'faixa1
                                ldblQtdkWhLido(lintFlgRe, 1) = BuscaVlrItem(lstrItens, "0302010081") 'faixa2
                                ldblQtdkWhLido(lintFlgRe, 2) = BuscaVlrItem(lstrItens, "0302010082") 'faixa3
                                ldblQtdkWhLido(lintFlgRe, 3) = BuscaVlrItem(lstrItens, "0302010083") 'faixa4

                                ldblQtdkWh(lintFlgRe, 0) = BuscaVlrItem(lstrItens, "0302010090") 'faixa1
                                ldblQtdkWh(lintFlgRe, 1) = BuscaVlrItem(lstrItens, "0302010091") 'faixa2
                                ldblQtdkWh(lintFlgRe, 2) = BuscaVlrItem(lstrItens, "0302010092") 'faixa3
                                ldblQtdkWh(lintFlgRe, 3) = BuscaVlrItem(lstrItens, "0302010093") 'faixa4

                                ldblVlrkWh(lintFlgRe, 0) = BuscaVlrItem(lstrItens, "0302020090") 'faixa1
                                ldblVlrkWh(lintFlgRe, 1) = BuscaVlrItem(lstrItens, "0302020091") 'faixa2
                                ldblVlrkWh(lintFlgRe, 2) = BuscaVlrItem(lstrItens, "0302020092") 'faixa3
                                ldblVlrkWh(lintFlgRe, 3) = BuscaVlrItem(lstrItens, "0302020093") 'faixa4

                                ldblBankWh(lintFlgRe, 0) = BuscaVlrItem(lstrItens, "0302120090") 'faixa1
                                ldblBankWh(lintFlgRe, 1) = BuscaVlrItem(lstrItens, "0302120091") 'faixa2
                                ldblBankWh(lintFlgRe, 2) = BuscaVlrItem(lstrItens, "0302120092") 'faixa3
                                ldblBankWh(lintFlgRe, 3) = BuscaVlrItem(lstrItens, "0302120093") 'faixa4

                                If lstrGrupoGD <> "0" Then



                                    If gstrEmpresa_Sigla = "EFLUL" Then
                                        ldbl_qkwhCompensada(lintFlgRe, 0) = BuscaVlrItem(lstrItens, "0303010090")
                                        ldbl_qkwhCompensada(lintFlgRe, 1) = BuscaVlrItem(lstrItens, "0303010090")
                                        ldbl_qkwhCompensada(lintFlgRe, 2) = BuscaVlrItem(lstrItens, "0303010090")
                                        ldbl_qkwhCompensada(lintFlgRe, 3) = BuscaVlrItem(lstrItens, "0303010090")

                                        ldbl_vkwhCompensada(lintFlgRe, 0) = (BuscaVlrItem(lstrItens, "0303020090") + BuscaVlrItem(lstrItens, "0304020090")) * lintMpr
                                        ldbl_vkwhCompensada(lintFlgRe, 1) = (BuscaVlrItem(lstrItens, "0303020090") + BuscaVlrItem(lstrItens, "0304020090")) * lintMpr
                                        ldbl_vkwhCompensada(lintFlgRe, 2) = (BuscaVlrItem(lstrItens, "0303020090") + BuscaVlrItem(lstrItens, "0304020090")) * lintMpr
                                        ldbl_vkwhCompensada(lintFlgRe, 3) = (BuscaVlrItem(lstrItens, "0303020090") + BuscaVlrItem(lstrItens, "0304020090")) * lintMpr

                                        ldbl_vDescPercCompensada(lintFlgRe, 0) = BuscaVlrItem(lstrItens, "0303030090")
                                        ldbl_vDescPercCompensada(lintFlgRe, 1) = BuscaVlrItem(lstrItens, "0303030090")
                                        ldbl_vDescPercCompensada(lintFlgRe, 2) = BuscaVlrItem(lstrItens, "0303030090")
                                        ldbl_vDescPercCompensada(lintFlgRe, 3) = BuscaVlrItem(lstrItens, "0303030090")
                                    Else
                                        ldbl_qkwhCompensada(lintFlgRe, 0) = BuscaVlrItem(lstrItens, "0313010090")
                                        ldbl_qkwhCompensada(lintFlgRe, 1) = BuscaVlrItem(lstrItens, "0313010090")
                                        ldbl_qkwhCompensada(lintFlgRe, 2) = BuscaVlrItem(lstrItens, "0313010090")
                                        ldbl_qkwhCompensada(lintFlgRe, 3) = BuscaVlrItem(lstrItens, "0313010090")

                                        ldbl_vkwhCompensada(lintFlgRe, 0) = (BuscaVlrItem(lstrItens, "0313020090") + BuscaVlrItem(lstrItens, "0304020090")) * lintMpr
                                        ldbl_vkwhCompensada(lintFlgRe, 1) = (BuscaVlrItem(lstrItens, "0313020090") + BuscaVlrItem(lstrItens, "0304020090")) * lintMpr
                                        ldbl_vkwhCompensada(lintFlgRe, 2) = (BuscaVlrItem(lstrItens, "0313020090") + BuscaVlrItem(lstrItens, "0304020090")) * lintMpr
                                        ldbl_vkwhCompensada(lintFlgRe, 3) = (BuscaVlrItem(lstrItens, "0313020090") + BuscaVlrItem(lstrItens, "0304020090")) * lintMpr

                                        ldbl_vDescPercCompensada(lintFlgRe, 0) = BuscaVlrItem(lstrItens, "0313030090")
                                        ldbl_vDescPercCompensada(lintFlgRe, 1) = BuscaVlrItem(lstrItens, "0313030090")
                                        ldbl_vDescPercCompensada(lintFlgRe, 2) = BuscaVlrItem(lstrItens, "0313030090")
                                        ldbl_vDescPercCompensada(lintFlgRe, 3) = BuscaVlrItem(lstrItens, "0313030090")
                                    End If
                                End If


                                lintgrpGD(lintFlgRe, 0) = lstrGrupoGD 'grpGD que foi calculada a fatura
                                lintgrpGD(lintFlgRe, 1) = lstrGrupoGD 'grpGD que foi calculada a fatura
                                lintgrpGD(lintFlgRe, 2) = lstrGrupoGD 'grpGD que foi calculada a fatura
                                lintgrpGD(lintFlgRe, 3) = lstrGrupoGD 'grpGD que foi calculada a fatura


                                If ldblQKwhFat <= 30 Then
                                    lintOrdCla = 21
                                    lintFaixa = 0
                                    lstrDes945(lintFlgRe, lintFaixa) = lstrDsClas & " 0 até 30"
                                ElseIf ldblQKwhFat > 30 And ldblQKwhFat <= 100 Then
                                    lintOrdCla = 22
                                    lintFaixa = 1
                                    lstrDes945(lintFlgRe, lintFaixa) = lstrDsClas & " 31 até 100"
                                ElseIf ldblQKwhFat > 100 And ldblQKwhFat <= 220 Then
                                    lintOrdCla = 23
                                    lintFaixa = 2
                                    lstrDes945(lintFlgRe, lintFaixa) = lstrDsClas & " 101 até 220"
                                ElseIf ldblQKwhFat > 220 Then
                                    lintOrdCla = 24
                                    lintFaixa = 3
                                    lstrDes945(lintFlgRe, lintFaixa) = lstrDsClas & " acima de 220"
                                End If
                                lintQtdFxa(lintFlgRe, lintFaixa) = lintQtdFxa(lintFlgRe, lintFaixa) + 1
                                If lintFaixa >= 0 And lintFaixa <= 3 Then
                                    ldblQtd945(lintFlgRe, 0) = ldblQtd945(lintFlgRe, 0) + ldblQtdkWh(lintFlgRe, 0)
                                    ldblVlr945(lintFlgRe, 0) = ldblVlr945(lintFlgRe, 0) + ldblVlrkWh(lintFlgRe, 0)
                                    ldblBan945(lintFlgRe, 0) = ldblBan945(lintFlgRe, 0) + ldblBankWh(lintFlgRe, 0)
                                    If lintFaixa = 0 Then
                                        ldblVlrIcm(lintFlgRe, 0) = ldblVlrIcm(lintFlgRe, 0) + (BuscaVlrItem(lstrItens, "0302030009") + BuscaVlrItem(lstrItens, "0302030010"))
                                        ldblVlrPis(lintFlgRe, 0) = ldblVlrPis(lintFlgRe, 0) + BuscaVlrItem(lstrItens, "0302020029")
                                        ldblVlrCof(lintFlgRe, 0) = ldblVlrCof(lintFlgRe, 0) + BuscaVlrItem(lstrItens, "0302020030")
                                    End If
                                End If
                                If lintFaixa >= 1 And lintFaixa <= 3 Then
                                    ldblQtd945(lintFlgRe, 1) = ldblQtd945(lintFlgRe, 1) + ldblQtdkWh(lintFlgRe, 1)
                                    ldblVlr945(lintFlgRe, 1) = ldblVlr945(lintFlgRe, 1) + ldblVlrkWh(lintFlgRe, 1)
                                    ldblBan945(lintFlgRe, 1) = ldblBan945(lintFlgRe, 1) + ldblBankWh(lintFlgRe, 1)
                                    If lintFaixa = 1 Then
                                        ldblVlrIcm(lintFlgRe, 1) = ldblVlrIcm(lintFlgRe, 1) + (BuscaVlrItem(lstrItens, "0302030009") + BuscaVlrItem(lstrItens, "0302030010"))
                                        ldblVlrPis(lintFlgRe, 1) = ldblVlrPis(lintFlgRe, 1) + BuscaVlrItem(lstrItens, "0302020029")
                                        ldblVlrCof(lintFlgRe, 1) = ldblVlrCof(lintFlgRe, 1) + BuscaVlrItem(lstrItens, "0302020030")
                                    End If
                                End If
                                If lintFaixa >= 2 And lintFaixa <= 3 Then
                                    ldblQtd945(lintFlgRe, 2) = ldblQtd945(lintFlgRe, 2) + ldblQtdkWh(lintFlgRe, 2)
                                    ldblVlr945(lintFlgRe, 2) = ldblVlr945(lintFlgRe, 2) + ldblVlrkWh(lintFlgRe, 2)
                                    ldblBan945(lintFlgRe, 2) = ldblBan945(lintFlgRe, 2) + ldblBankWh(lintFlgRe, 2)
                                    If lintFaixa = 2 Then
                                        ldblVlrIcm(lintFlgRe, 2) = ldblVlrIcm(lintFlgRe, 2) + (BuscaVlrItem(lstrItens, "0302030009") + BuscaVlrItem(lstrItens, "0302030010"))
                                        ldblVlrPis(lintFlgRe, 2) = ldblVlrPis(lintFlgRe, 2) + BuscaVlrItem(lstrItens, "0302020029")
                                        ldblVlrCof(lintFlgRe, 2) = ldblVlrCof(lintFlgRe, 2) + BuscaVlrItem(lstrItens, "0302020030")
                                    End If
                                End If
                                If lintFaixa = 3 Then
                                    ldblQtd945(lintFlgRe, 3) = ldblQtd945(lintFlgRe, 3) + ldblQtdkWh(lintFlgRe, 3)
                                    ldblVlr945(lintFlgRe, 3) = ldblVlr945(lintFlgRe, 3) + ldblVlrkWh(lintFlgRe, 3)
                                    ldblBan945(lintFlgRe, 3) = ldblBan945(lintFlgRe, 3) + ldblBankWh(lintFlgRe, 3)
                                    If lintFaixa = 3 Then
                                        ldblVlrIcm(lintFlgRe, 3) = ldblVlrIcm(lintFlgRe, 3) + (BuscaVlrItem(lstrItens, "0302030009") + BuscaVlrItem(lstrItens, "0302030010"))
                                        ldblVlrPis(lintFlgRe, 3) = ldblVlrPis(lintFlgRe, 3) + BuscaVlrItem(lstrItens, "0302020029")
                                        ldblVlrCof(lintFlgRe, 3) = ldblVlrCof(lintFlgRe, 3) + BuscaVlrItem(lstrItens, "0302020030")
                                    End If
                                End If
                                If ldblQKwhFat <= 30 Then
                                    lstrDsClas = lstrDsClas & " 0 até 30"
                                    lintOrdCla = 11
                                ElseIf ldblQKwhFat > 30 And ldblQKwhFat < 80 Then
                                    lstrDsClas = lstrDsClas & " 31 até 79"
                                    lintOrdCla = 12
                                ElseIf ldblQKwhFat >= 80 And ldblQKwhFat <= 100 Then
                                    lstrDsClas = lstrDsClas & " 80 até 100"
                                    lintOrdCla = 13
                                ElseIf ldblQKwhFat >= 101 And ldblQKwhFat <= 140 Then
                                    lstrDsClas = "Bx.Renda 101 até 140"
                                    lintOrdCla = 15
                                ElseIf ldblQKwhFat >= 141 And ldblQKwhFat <= 150 Then
                                    lstrDsClas = "Bx.Renda 141 até 150"
                                    lintOrdCla = 16
                                ElseIf ldblQKwhFat >= 151 And ldblQKwhFat <= 160 Then
                                    lstrDsClas = "Bx.Renda 151 até 160"
                                    lintOrdCla = 17
                                ElseIf ldblQKwhFat > 160 Then
                                    lstrDsClas = "Bx.Renda acima de 160"
                                    lintOrdCla = 23
                                End If
                            End If
                        End If
                        ldblQKwFat = BuscaVlrItem(lstrItens, "0302010017") * lintMpr 'Demanda
                        'Valor
                        ldblVKwFat = BuscaVlrItem(lstrItens, "0302020015") * lintMpr 'Demanda
                        ldblQKwFatUfp = BuscaVlrItem(lstrItens, "0302010018") * lintMpr 'Demanda de ultrapassagem fp
                        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
                        ldblQKwhDevp = BuscaVlrItem(lstrItens, "0302010041") * lintMpr 'Consumo devolvido na p
                        ldblQKwhDevfp = BuscaVlrItem(lstrItens, "0302010042") * lintMpr 'Consumo devolvido na fp
                        ldblQKwhUtrp = BuscaVlrItem(lstrItens, "0302010043") * lintMpr 'Consumo ultrapassado na p
                        ldblQKwhUtrfp = BuscaVlrItem(lstrItens, "0302010044") * lintMpr 'Consumo ultrapassado na fp

                        ldblQKwhReversop = BuscaVlrItem(lstrItens, "0302012010") * lintMpr 'Reverso em ponta
                        ldblQKwhReversofp = BuscaVlrItem(lstrItens, "0302012011") * lintMpr 'Reverso em fora ponta

                        ldblQKwhConsumidop = BuscaVlrItem(lstrItens, "0302012010") * lintMpr 'Consumido em ponta
                        ldblQKwhConsumidofp = BuscaVlrItem(lstrItens, "0302010011") * lintMpr 'Consumido em fora ponta


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

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

                        ldblQKwFatp = BuscaVlrItem(lstrItens, "0302010021") * lintMpr ' + BuscaVlrItem(lstrItens, "0302010022")  'Demanda p
                        ldblQKwFatfp = BuscaVlrItem(lstrItens, "0302010025") * lintMpr ' + BuscaVlrItem(lstrItens, "0302010026") 'Demanda fp
                        ldblQKwhCons = (BuscaVlrItem(lstrItens, "0302010010") + BuscaVlrItem(lstrItens, "0302010011")) * lintMpr '+ ldblQKwFatp + ldblQKwFatfp
                        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
                        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
                                ldblReceita_p = ldblReceita_p + ldblVKwhFatp + ldblVKwhFatpBan
                            Else
                                ldblVKwhFatpLiv = BuscaVlrItem(lstrItens, "0302020010") * lintMpr 'Consumo faturado p
                                ldblVKwhFatp = ldblVKwhFatp - ldblVKwhFatpLiv
                                ldblVKwhFatpBan = 0
                            End If
                        Else
                            ldblReceita_p = ldblReceita_p + ldblVKwhFatp + ldblVKwhFatpBan
                        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
                                ldblReceita_fp = ldblReceita_fp + ldblVKwhFatfp + ldblVKwhFatfpBan
                            Else
                                ldblVKwhFatfpLiv = BuscaVlrItem(lstrItens, "0302020011") * lintMpr 'Consumo faturado fp
                                ldblVKwhFatfp = ldblVKwhFatfp - ldblVKwhFatfpLiv
                                ldblVKwhFatfpBan = 0
                            End If
                        Else
                            ldblReceita_fp = ldblReceita_fp + ldblVKwhFatfp + ldblVKwhFatfpBan
                        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
                        ldblReceita_p = ldblReceita_p + ldblVKwhDevp + ldblVKwhUtrp
                        ldblReceita_fp = ldblReceita_fp + ldblVKwhDevfp + ldblVKwhUtrfp
                        ldblVKwhCons = ldblVKwhFatp + ldblVKwhFatfp '+ ldblVKwFatp + ldblVKwFatfp
                        ldblVKwFatp = BuscaVlrItem(lstrItens, "0302020021") * lintMpr ' + BuscaVlrItem(lstrItens, "0302020022")  'Demanda p
                        ldblVKwFatfp = (BuscaVlrItem(lstrItens, "0302020023") + BuscaVlrItem(lstrItens, "0302020072")) * lintMpr '+ (BuscaVlrItem(lstrItens, "0302020072") * lintMpr) ' + BuscaVlrItem(lstrItens, "0302020026") '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
                        ldblConsumoReativop = BuscaVlrItem(lstrItens, "0302020013") * lintMpr 'Consumo Reativo p
                        ldblConsumoReativofp = (BuscaVlrItem(lstrItens, "0302020014") + BuscaVlrItem(lstrItens, "0302990044")) * lintMpr 'Consumo Reativo fp + Consumo reservado
                        ldblDemandaReativap = BuscaVlrItem(lstrItens, "0302020027") * lintMpr 'Demanda Reativo p
                        ldblDemandaReativafp = BuscaVlrItem(lstrItens, "0302020028") * lintMpr 'Demanda Reativo fp
                        If lstrGrupo = "AA" Then
                            lstrDsClas = lstrDsClas & " Azul"
                        ElseIf lstrGrupo = "AV" Then
                            lstrDsClas = lstrDsClas & " Verde"
                        ElseIf lstrGrupo = "AL" Then
                            If lintParcialmenteCativo = 1 Then
                                lstrDsClas = "Livre Parc.Cat."
                            ElseIf lintLivreVerde = 1 Then
                                lstrDsClas = "Livre Verde"
                            ElseIf lintLivreEspecial = 1 Then
                                lstrDsClas = "Livre Especial"
                            Else
                                lstrDsClas = "Livre"
                            End If
                        ElseIf lstrGrupo = "AX" Then
                            lstrDsClas = "Suprimento Horo"
                            lintOrdCla = 99
                        End If
                    End If
                    ldblIcms = BuscaVlrItem(lstrItens, "0302030009") * lintMpr 'Vlr Icms.
                    ldblIcms = ldblIcms + (BuscaVlrItem(lstrItens, "0302030010") * lintMpr) 'Vlr Icms.
                    ldblPis = BuscaVlrItem(lstrItens, "0302020029") * lintMpr 'Vlr PIS.
                    ldblCofins = BuscaVlrItem(lstrItens, "0302020030") * lintMpr 'Vlr COFINS.
                    '
                    ldblIcmsSt = BuscaVlrItem(lstrItens, "0602020001") * lintMpr
                    ldblPisSt = BuscaVlrItem(lstrItens, "0602020002") * lintMpr
                    ldblCofinsSt = BuscaVlrItem(lstrItens, "0602020003") * lintMpr
                    '
                    ldblIcmsPisCof = BuscaVlrItem(lstrItens, "0302030012") * lintMpr 'Vlr Icms/Pis/Cofins.
                    'Usinas
                    ldblQtdMust = BuscaVlrItem(lstrItens, "0302010032") * lintMpr 'EUSD - Must qtde
                    ldblVlrMust = BuscaVlrItem(lstrItens, "0302020032") * lintMpr 'EUSD - Must valor
                    ldblEncMust = BuscaVlrItem(lstrItens, "9080000098") * lintMpr 'EUSD - Must encargo
                    ldblDesMust = BuscaVlrItem(lstrItens, "9070000087") + BuscaVlrItem(lstrItens, "9070000091") * lintMpr 'EUSD - Must descontos

                    If ldblVlrMust > 0 Then
                        lstrCmClas = BuscaComplemento(llngUnidId)
                    Else
                        lstrCmClas = ""
                    End If

                    If lstrGrupoGD <> "0" Then
                        If gstrEmpresa_Sigla = "EFLUL" Then
                            ldblQKwhComp = BuscaVlrItem(lstrItens, "0303010090") * lintMpr
                            ldblVKwhComp = (BuscaVlrItem(lstrItens, "0303020090") + BuscaVlrItem(lstrItens, "0304020090")) * lintMpr
                            ldblVDescPercComp = BuscaVlrItem(lstrItens, "0303030090")
                        Else
                            ldblQKwhComp = BuscaVlrItem(lstrItens, "0313010090") * lintMpr
                            '0314010090
                            ldblVKwhComp = (BuscaVlrItem(lstrItens, "0313020090") + BuscaVlrItem(lstrItens, "0314020090")) * lintMpr
                            ldblVDescPercComp = BuscaVlrItem(lstrItens, "0313030090") 'TUSD
                            'ldblVDescPercCompTE = BuscaVlrItem(lstrItens, "0314030090") 'TE

                            ldblQKwhComp_p = BuscaVlrItem(lstrItens, "0313010091") * lintMpr
                            ldblVKwhComp_p = (BuscaVlrItem(lstrItens, "0313020091") + BuscaVlrItem(lstrItens, "0314020091")) * lintMpr
                            ldblVDescPercComp_p = BuscaVlrItem(lstrItens, "0313030091")
                            'ldblVDescPercComp_p_TE = BuscaVlrItem(lstrItens, "0314030091") 'TE

                            ldblQKwhComp_fp = BuscaVlrItem(lstrItens, "0313010092") * lintMpr
                            ldblVKwhComp_fp = (BuscaVlrItem(lstrItens, "0313020092") + BuscaVlrItem(lstrItens, "0314020092")) * lintMpr
                            ldblVDescPercComp_fp = BuscaVlrItem(lstrItens, "0313030092")
                            'ldblVDescPercComp_fp_TE = BuscaVlrItem(lstrItens, "0314030092") 'TE

                            ldblQKwhComp_i = BuscaVlrItem(lstrItens, "0313010093") * lintMpr
                            ldblVKwhComp_i = (BuscaVlrItem(lstrItens, "0313020093") + BuscaVlrItem(lstrItens, "0314020093")) * lintMpr
                            ldblVDescPercComp_i = BuscaVlrItem(lstrItens, "0313030093")
                            'ldblVDescPercComp_i_TE = BuscaVlrItem(lstrItens, "0314030093") 'TE
                        End If
                    End If

                    lstrSqlPAx = lstrSqlPAx & "("
                    lstrSqlPAx = lstrSqlPAx & " " & lintFlgRe & ", " 'refaturado
                    lstrSqlPAx = lstrSqlPAx & "'" & IIf(VB.Left(lstrGrupo, 1) = "A", lstrGrupo, VB.Left(lstrGrupo, 1)) & IIf(lstrGrupo = "AL", IIf(lintLivreVerde = 1, "V", IIf(lintParcialmenteCativo = 1, "P", IIf(lintLivreEspecial = 1, "S", ""))), "") & "'," 'grupo
                    lstrSqlPAx = lstrSqlPAx & "'" & lstrClasse & "', " 'classe
                    lstrSqlPAx = lstrSqlPAx & "'" & lstrDsClas & "', " 'ds_classe
                    lstrSqlPAx = lstrSqlPAx & "'" & lstrCmClas & "', " 'com_classe
                    lstrSqlPAx = lstrSqlPAx & " " & lintOrdCla & ", " 'ord_classe
                    lstrSqlPAx = lstrSqlPAx & " " & lintParcialmenteCativo & ", " 'parc_cativo
                    lstrSqlPAx = lstrSqlPAx & " " & IIf(ldblVlrMust > 0, 1, 0) & ", " 'usinas
                    lstrSqlPAx = lstrSqlPAx & " " & lintMicroGeracao & ", " 'micro_geracao
                    lstrSqlPAx = lstrSqlPAx & " " & lintIrrigacaoAquicultura & ", " 'Irrigante
                    lstrSqlPAx = lstrSqlPAx & " " & lintTarifaBranca & ", " 'Tarifa Branca
                    'Convencional

                    'Qtde
                    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(ldblQKwFat), ",", ".") & "', " 'qkw_fat

                    'Valor
                    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
                    'Qtde
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhConsumidop), ",", ".") & "', " 'qkwh_cons_p
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhConsumidofp), ",", ".") & "', " 'qkwh_cons_fp

                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhFatp), ",", ".") & "', " 'qkwh_fat_p
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhFatfp), ",", ".") & "', " 'qkwh_fat_fp
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhFatpBan), ",", ".") & "', " 'qkwh_fat_p_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(ldblQKwFatp), ",", ".") & "', " 'qkw_fat_p
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwFatfp), ",", ".") & "', " 'qkw_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(ldblVKwhFatfp), ",", ".") & "', " 'vkwh_fat_fp
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwhFatpBan), ",", ".") & "', " 'vkwh_fat_p_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
                    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), ",", ".") & "', " 'vkw_dsc_prov
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVkwhDscReal), ",", ".") & "', " 'vkw_dsc_real
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVkwhDevCcee), ",", ".") & "', " 'vkw_dev_ccee
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVkwhCobCcee), ",", ".") & "', " 'vkw_cob_ccee
                    'Reativo
                    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
                    'Receita
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblReceita_p), ",", ".") & "', " 'receita
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblReceita_I), ",", ".") & "', " 'receita
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblReceita_fp), ",", ".") & "', " 'receita
                    'Reverso
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhReverso), ",", ".") & "', " 'reverso
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVKwhReverso), ",", ".") & "', " 'valor
                    'Adicionado por Nader
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhReversop), ",", ".") & "', " 'reverso ponta
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQKwhReversofp), ",", ".") & "', " 'reverso fp
                    'Irrigacao
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQIrr_Normal_kWh_fat), ",", ".") & "', " 'qirr_nor_kwh_fat
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQIrr_Normal_kWh_fat), ",", ".") & "', " 'qirr_nor_kwh_fat_ban
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQIrr_Reserv_kWh_fat), ",", ".") & "', " 'qirr_res_kwh_fat
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQIrr_Reserv_kWh_fat), ",", ".") & "', " 'qirr_res_kwh_fat_ban
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVIrr_Normal_kWh_fat), ",", ".") & "', " 'virr_nor_kwh_fat
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVIrr_Normal_kWh_fat_ban), ",", ".") & "', " 'virr_nor_kwh_fat_ban
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVIrr_Reserv_kWh_fat), ",", ".") & "', " 'virr_res_kwh_fat
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVIrr_Reserv_kWh_fat_ban), ",", ".") & "', " 'virr_res_kwh_fat_ban

                    'Tarifa Branca
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQTarBranca_kWh_Ponta), ",", ".") & "', "
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQTarBranca_kWh_Ponta_Ban), ",", ".") & "', "
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQTarBranca_kWh_Inter), ",", ".") & "', "
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQTarBranca_kWh_Inter_Ban), ",", ".") & "', "
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQTarBranca_kWh_ForaP), ",", ".") & "', "
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQTarBranca_kWh_ForaP_Ban), ",", ".") & "', "
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVTarBranca_kWh_Ponta), ",", ".") & "', "
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVTarBranca_kWh_Ponta_Ban), ",", ".") & "', "
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVTarBranca_kWh_Inter), ",", ".") & "', "
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVTarBranca_kWh_Inter_Ban), ",", ".") & "', "
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVTarBranca_kWh_ForaP), ",", ".") & "', "
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVTarBranca_kWh_ForaP_Ban), ",", ".") & "', "

                    'Energia Compensada
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(ldblQKwhComp, ",", ".") & "', " 'qtd energia compensada
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(ldblVKwhComp, ",", ".") & "', " 'valor energia compensada
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(ldblVDescPercComp, ",", ".") & "', " 'desconto percentual energia compensada

                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(ldblQKwhComp_p, ",", ".") & "', " 'qtd energia compensada ponta
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(ldblVKwhComp_p, ",", ".") & "', " 'valor energia compensada ponta
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(ldblVDescPercComp_p, ",", ".") & "', " 'desconto percentual energia compensada ponta

                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(ldblQKwhComp_fp, ",", ".") & "', " 'qtd energia compensada fora ponta
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(ldblVKwhComp_fp, ",", ".") & "', " 'valor energia compensada fora ponta
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(ldblVDescPercComp_fp, ",", ".") & "', " 'desconto percentual energia compensada fora ponta

                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(ldblQKwhComp_i, ",", ".") & "', " 'qtd energia compensada interm
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(ldblVKwhComp_i, ",", ".") & "', " 'valor energia compensada interm
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(ldblVDescPercComp_i, ",", ".") & "', " 'desconto percentual energia compensada interm


                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(lstrGrupoGD, ",", ".") & "', " 'GRP

                    'Tributos
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblIcms), ",", ".") & "', " 'icms
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblPis), ",", ".") & "', " 'pis
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblCofins), ",", ".") & "', " 'cofins

                    'Tributacao Comercializador usado para o Livre
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblIcmsSt), ",", ".") & "', " 'icms - st
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblPisSt), ",", ".") & "', " 'pis - st
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblCofinsSt), ",", ".") & "', " 'cofins - st

                    'Base de calculo
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblBaseIcms), ",", ".") & "', " 'base icms
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblBasePis), ",", ".") & "', " 'base pis
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblBaseCofins), ",", ".") & "', " 'base cofins
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblIcmsPisCof), ",", ".") & "', " 'icms/pis/cofins

                    'Usinas
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblQtdMust), ",", ".") & "', " 'must_qtd
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblVlrMust), ",", ".") & "', " 'must_vlr
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblDesMust), ",", ".") & "', " 'must_des
                    lstrSqlPAx = lstrSqlPAx & "'" & Replace(CStr(ldblEncMust), ",", ".") & "' " 'must_enc
                    lstrSqlPAx = lstrSqlPAx & "), "
                    lintContInsertPri = lintContInsertPri + 1

                    If lintContInsertPri > 300 Then
                        objExecAtualizaBancoDados.QUERY_NAME = lstrSqlPri & VB.Left(lstrSqlPAx, Len(lstrSqlPAx) - 2)
                        repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
                        ' Call Executar(lstrSqlPri & VB.Left(lstrSqlPAx, Len(lstrSqlPAx) - 2), False)
                        lstrSqlPAx = ""
                        lintContInsertPri = 1
                    End If
                    ' Call .MoveNext()
                    Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " Faturas ")
                    System.Windows.Forms.Application.DoEvents()
                Next

                If lintContInsertPri > 0 And Len(Trim(lstrSqlPAx)) > 0 Then
                    objExecAtualizaBancoDados.QUERY_NAME = lstrSqlPri & VB.Left(lstrSqlPAx, Len(lstrSqlPAx) - 2)
                    repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
                    'Call Executar(lstrSqlPri & VB.Left(lstrSqlPAx, Len(lstrSqlPAx) - 2), False)
                End If

                lblnRetorno = True
            Else
                Call LigDes(True)
                Me.Cursor = System.Windows.Forms.Cursors.Default
                Call MsgBox("Não existem faturas no período selecionado!", MsgBoxStyle.Exclamation, gstrTituloMensagem)
                lblnRetorno = False
            End If

            dt = Nothing

            If lblnRetorno Then
                For lintRef = 0 To 1
                    For lintAux = 0 To 3
                        'Grava 945
                        If lintQtdFxa(lintRef, lintAux) > 0 Then
                            lstrSql = ""
                            lstrSql = lstrSql & "INSERT INTO " & istrTab945 & " SET " & vbCrLf
                            lstrSql = lstrSql & "refaturado             = " & lintRef & ", " & vbCrLf
                            lstrSql = lstrSql & "ds_classe              = '" & lstrDes945(lintRef, lintAux) & "', " & vbCrLf
                            lstrSql = lstrSql & "ord_classe             =  " & lintAux + 20 & ", " & vbCrLf
                            lstrSql = lstrSql & "faixa                  = " & lintQtdFxa(lintRef, lintAux) & ", " & vbCrLf
                            lstrSql = lstrSql & "consumo                = " & Replace(CStr(ldblQtd945(lintRef, lintAux)), ",", ".") & ", " & vbCrLf
                            lstrSql = lstrSql & "valor                  = " & Replace(CStr(ldblVlr945(lintRef, lintAux)), ",", ".") & ", " & vbCrLf
                            lstrSql = lstrSql & "bandeira               = " & Replace(CStr(ldblBan945(lintRef, lintAux)), ",", ".") & ", " & vbCrLf
                            lstrSql = lstrSql & "receita                = " & Replace(CStr(ldblVlr945(lintRef, lintAux) + ldblBan945(lintRef, lintAux)), ",", ".") & ", " & vbCrLf
                            lstrSql = lstrSql & "icms                   = " & Replace(CStr(ldblVlrIcm(lintRef, lintAux)), ",", ".") & ", " & vbCrLf
                            lstrSql = lstrSql & "pis                    = " & Replace(CStr(ldblVlrPis(lintRef, lintAux)), ",", ".") & ", " & vbCrLf
                            lstrSql = lstrSql & "cofins                 = " & Replace(CStr(ldblVlrCof(lintRef, lintAux)), ",", ".") & ", " & vbCrLf
                            lstrSql = lstrSql & "consumo_lido           = " & Replace(CStr(ldblQtdkWhLido(lintRef, lintAux)), ",", ".") & ", " & vbCrLf
                            lstrSql = lstrSql & "qkwhCompensada         = " & Replace(CStr(ldbl_qkwhCompensada(lintRef, lintAux)), ",", ".") & ", " & vbCrLf
                            lstrSql = lstrSql & "vkwhCompensada         = " & Replace(CStr(ldbl_vkwhCompensada(lintRef, lintAux)), ",", ".") & ", " & vbCrLf
                            lstrSql = lstrSql & "vDescPercCompensada    = " & Replace(CStr(ldbl_vDescPercCompensada(lintRef, lintAux)), ",", ".") & ", " & vbCrLf
                            lstrSql = lstrSql & "grpGD    = " & lintgrpGD(lintRef, lintAux) & "; " & vbCrLf
                            objExecAtualizaBancoDados.QUERY_NAME = lstrSql
                            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
                        End If
                    Next lintAux
                Next lintRef
                Me.lblInfo.Text = Me.lblInfo.Text & " *** Finalizado *** "
                Call Me.lblInfo.Refresh()
            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 Function BuscaComplemento(ByVal astrUnidid As Long) As String
        Dim lstrRet As String
        Dim lstrSql As String
        Try
            'Dim lrstAux As ADODB.Recordset
            'lrstAux = New ADODB.Recordset
            lstrRet = ""
            lstrSql = ""
            lstrSql = lstrSql & "SELECT * " & vbCrLf
            lstrSql = lstrSql & "  FROM `uc_faturamento` " & vbCrLf
            lstrSql = lstrSql & " WHERE `id` = " & astrUnidid & " " & vbCrLf
            objAtualizaBancoDados.QUERY_NAME = lstrSql
            dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)


            If dt.Rows.Count > 0 Then
                For Each row As DataRow In dt.Rows
                    lstrRet = IIf(IsDBNull(row.Item("complement")), "", row.Item("complement"))
                Next
            End If
            dt = Nothing
            BuscaComplemento = lstrRet

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

    Private Sub LigDes(ByVal ablnModo As Boolean)
        Try
            Me.dtpMesAno.Enabled = ablnModo
            Me.cmdImprimir.Enabled = ablnModo
            Me.cmdExporta.Enabled = IIf(Not iblnExporta, False, ablnModo)
            Me.cmdPasta.Enabled = ablnModo
            Me.txtDir.Enabled = ablnModo
            Me.cmdFechar.Enabled = ablnModo
            Me.chkGrupoAConvencional.Enabled = ablnModo
            Me.chkGrupoAHorosazonal.Enabled = ablnModo
            Me.chkGrupoALivre.Enabled = ablnModo
            Me.chkGrupoB.Enabled = ablnModo
            Me.chkGrupoBx.Enabled = ablnModo
            Me.chkMicroGeracao.Enabled = ablnModo
            Me.chkUsina.Enabled = ablnModo
            Me.chkIrrigante.Enabled = ablnModo
            Me.chkReceitakWhBan.Enabled = ablnModo
            Me.chkExportaDescritivo.Enabled = ablnModo
            Me.chkCasaDecimal.Enabled = ablnModo
            Me.chkTarifaBranca.Enabled = ablnModo
            Me.cmbCaracter.Enabled = ablnModo

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

    Private Function GetSampClasses(ByVal classe As Short) As Short
        Dim lintRet As Short
        Try
            If classe >= 10 And classe < 30 Then
                lintRet = 1 'Residencial
            ElseIf classe = 30 Then
                lintRet = 2 'Industrial
            ElseIf classe = 40 Then
                lintRet = 3 'Comercial, Serviços e Outras
            ElseIf classe = 55 Then
                lintRet = 9 'Rural Irrigante
            ElseIf classe = 57 Then
                lintRet = 10 'Rural Aquicultor
            ElseIf classe = 50 Then
                lintRet = 4 'Rural
            ElseIf classe = 61 Or classe = 62 Then
                lintRet = 6 'Iluminação Pública
            ElseIf classe = 60 Then
                lintRet = 5 'Poder Público
            ElseIf classe = 71 Then
                lintRet = 13 'Serviço Público (tração elétrica)
            ElseIf classe = 70 Or classe = 73 Then
                lintRet = 7 'Serviço Público (água, esgoto e saneamento)
            ElseIf classe = 80 Then
                lintRet = 8 'Consumo Próprio
            Else
                lintRet = 0
            End If
            GetSampClasses = lintRet

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

    Private Function GetExpSampClasses(ByVal classe As Short) As String
        Dim lstrRet As String
        Try
            If classe = 1 Then
                lstrRet = "Residencial"
            ElseIf classe = 2 Then
                lstrRet = "Industrial"
            ElseIf classe = 3 Then
                lstrRet = "Comercial, Serviços e Outras"
            ElseIf classe = 4 Then
                lstrRet = "Rural"
            ElseIf classe = 5 Then
                lstrRet = "Poder Público"
            ElseIf classe = 6 Then
                lstrRet = "Iluminação Pública"
            ElseIf classe = 7 Then
                lstrRet = "Serviço Público (água, esgoto e saneamento)"
            ElseIf classe = 8 Then
                lstrRet = "Consumo Próprio"
            ElseIf classe = 9 Then
                lstrRet = "Rural Irrigante"
            ElseIf classe = 10 Then
                lstrRet = "Rural Aquicultor"
            ElseIf classe = 13 Then
                lstrRet = "Serviço Público (tração elétrica)"
            Else
                lstrRet = "Outras"
            End If
            GetExpSampClasses = lstrRet
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: GetExpSampClasses" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function
        End Try
    End Function

    Private Function GetModalidade(ByVal astrModalidade As String) As Short
        Dim lintRet As Short
        Try
            If astrModalidade = "AA" Then
                lintRet = 4 'Horo-Sazonal Azul
            ElseIf astrModalidade = "AV" Or astrModalidade = "ALV" Then
                lintRet = 5 'Horo-Sazonal Verde
            ElseIf astrModalidade = "BB" Then
                lintRet = 13 'Branca
            Else
                lintRet = 1 'Convencional
            End If
            '  If astrModalidade = "" Then lintRet = 15 'Horo-Sazonal Azul - ABRACE
            '  If astrModalidade = "" Then lintRet = 16 'Horo-Sazonal Verde - ABRACE
            '  If astrModalidade = "" Then lintRet = 17 'Horo-Sazonal Azul - INPA
            '  If astrModalidade = "" Then lintRet = 14 'Convencional - ABRACE
            '  If astrModalidade = "" Then lintRet = 12 'Energia Fotovoltaica (Res. 083/2004)
            '  If astrModalidade = "" Then lintRet = 2 'Tensão Secundária de Distribuição em 127 V
            '  If astrModalidade = "" Then lintRet = 8 'Com Controlador de Demanda
            '  If astrModalidade = "" Then lintRet = 9 'Tarifa Amarela
            '  If astrModalidade = "" Then lintRet = 10 'Tarifa da Madrugada
            '  If astrModalidade = "" Then lintRet = 3 'Tensão Secundária de Distribuição em 220 V
            '  If astrModalidade = "" Then lintRet = 6 'Tarifa de Emergência Horo-Sazonal Azul
            '  If astrModalidade = "" Then lintRet = 7 'Tarifa de Emergência Horo-Sazonal Verde
            '  If astrModalidade = "" Then lintRet = 11 'Não se aplica
            GetModalidade = lintRet
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: GetModalidade" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function
        End Try
    End Function

    Private Function GetExpModalidade(ByVal aintModalidade As Short) As String
        Dim lstrRet As String
        Try
            If aintModalidade = 4 Then
                lstrRet = "Horo-Sazonal Azul"
            ElseIf aintModalidade = 5 Then
                lstrRet = "Horo-Sazonal Verde"
            ElseIf aintModalidade = 13 Then
                lstrRet = "Branca"
            Else
                lstrRet = "Convencional"
            End If

            'ElseIf aintModalidade = 15 Then lstrRet = "Horo-Sazonal Azul - ABRACE"
            'ElseIf aintModalidade = 16 Then lstrRet = "Horo-Sazonal Verde - ABRACE"
            'ElseIf aintModalidade = 17 Then lstrRet = "Horo-Sazonal Azul - INPA"
            'ElseIf aintModalidade = 14 Then lstrRet = "Convencional - ABRACE"
            'ElseIf aintModalidade = 12 Then lstrRet = "Energia Fotovoltaica (Res. 083/2004)"
            'ElseIf aintModalidade =  2 Then lstrRet = "Tensão Secundária de Distribuição em 127 V"
            'ElseIf aintModalidade =  8 Then lstrRet = "Com Controlador de Demanda"
            'ElseIf aintModalidade =  9 Then lstrRet = "Tarifa Amarela"
            'ElseIf aintModalidade = 10 Then lstrRet = "Tarifa da Madrugada"
            'ElseIf aintModalidade =  3 Then lstrRet = "Tensão Secundária de Distribuição em 220 V"
            'ElseIf aintModalidade =  6 Then lstrRet = "Tarifa de Emergência Horo-Sazonal Azul"
            'ElseIf aintModalidade =  7 Then lstrRet = "Tarifa de Emergência Horo-Sazonal Verde"
            'ElseIf aintModalidade = 11 Then lstrRet = "Não se aplica"
            '
            GetExpModalidade = lstrRet
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: GetExpModalidade" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function
        End Try
    End Function

    Private Function GetSubGrupo(ByVal SubGrupo As String, ByVal classe As Short, ByVal abln945 As Boolean) As Short
        Dim lintRet As Short
        Try
            If VB.Left(SubGrupo, 1) = "A" Then
                If classe = 73 Then
                    lintRet = 56 'A4 - Serviço Público de Irrigação (2,3 kV a 25 kV)
                ElseIf classe = 52 Then
                    lintRet = 52 'A4 - Cooperativa de Eletrificação Rural (2,3 kV a 25 kV)
                Else
                    lintRet = 25 'A4 (2,3 a 25 kV)
                End If
            ElseIf VB.Left(SubGrupo, 1) = "B" Then
                If classe = 10 Then
                    lintRet = 1 'B1 - Residencial
                ElseIf (classe > 10 And classe < 30) And Not abln945 Then
                    If classe = 11 Then
                        lintRet = 2 'B1 - Residencial Baixa Renda (Consumo mensal até 30 kWh)
                    ElseIf classe = 12 Then
                        lintRet = 3 'B1 - Residencial Baixa Renda (Consumo mensal superior a 30 kWh e inferior a 80kWh)
                    ElseIf classe = 13 Then
                        lintRet = 41 'B1 - Residencial Baixa Renda (Consumo mensal de 80 a 100 kWh)
                    ElseIf classe = 14 Then
                        lintRet = 60 'B1 - Residencial Baixa Renda (Consumo mensal superior a 100 até 200 kWh)
                    ElseIf classe = 15 Then
                        lintRet = 4 'B1 - Residencial Baixa Renda (Consumo mensal de 101 a 140 kWh)
                    ElseIf classe = 16 Then
                        lintRet = 5 'B1 - Residencial Baixa Renda (Consumo mensal de 141 a 150 kWh)
                    ElseIf classe = 17 Then
                        lintRet = 6 'B1 - Residencial Baixa Renda (Consumo mensal de 151 a 160 kWh)
                    ElseIf classe = 18 Then
                        lintRet = 7 'B1 - Residencial Baixa Renda (Consumo mensal de 161 a 180 kWh)
                    ElseIf classe = 19 Then
                        lintRet = 8 'B1 - Residencial Baixa Renda (Consumo mensal de 181 a 200 kWh)
                    ElseIf classe = 20 Then
                        lintRet = 9 'B1 - Residencial Baixa Renda (Consumo mensal de 201 a 220 kWh)
                    ElseIf classe = 21 Then
                        lintRet = 45 'B1 - Residencial Baixa Renda(Consumo Mensal Superior ao Limite Regional de 140kWh)
                    ElseIf classe = 22 Then
                        lintRet = 46 'B1 - Residencial Baixa Renda(Consumo Mensal Superior ao Limite Regional de 150kWh)
                    ElseIf classe = 23 Then
                        lintRet = 47 'B1 - Residencial Baixa Renda(Consumo Mensal Superior ao Limite Regional de 160kWh)
                    ElseIf classe = 24 Then
                        lintRet = 48 'B1 - Residencial Baixa Renda(Consumo Mensal Superior ao Limite Regional de 180kWh)
                    ElseIf classe = 25 Then
                        lintRet = 49 'B1 - Residencial Baixa Renda(Consumo Mensal Superior ao Limite Regional de 200kWh)
                    ElseIf classe = 26 Then
                        lintRet = 50 'B1 - Residencial Baixa Renda(Consumo Mensal Superior ao Limite Regional de 220kWh)
                    End If
                ElseIf (classe >= 20 And classe < 30) And abln945 Then

                    ' Trocado a pedido do Arlindo - Antes

                    'lintRet = 57 'B1 - Residencial Baixa Renda

                    'If classe = 21 Then
                    '    lintRet = 62 'B1- Baixa Renda - Consumo mensal inferior ou igual a 30 kWh (RES. 945/2010)
                    'ElseIf classe = 22 Then
                    '    lintRet = 63 'B1- Baixa Renda - Consumo mensal superior a 30 kWh e inferior ou igual a 100 kWh (RES. 945/2010)
                    'ElseIf classe = 23 Then
                    '    lintRet = 64 'B1- Baixa Renda - Consumo mensal superior a 100 kWh e inferior ou igual a 220 kWh (RES. 945/2010)
                    'ElseIf classe = 24 Then
                    '    lintRet = 65 'B1- Baixa Renda - Consumo mensal superior a 220 kWh (RES. 945/2010)
                    'End If

                    ' Trocado a pedido do Arlindo - Depois

                    If classe = 20 Then
                        lintRet = 62 'B1- Baixa Renda - Consumo mensal inferior ou igual a 30 kWh (RES. 945/2010)
                    ElseIf classe = 21 Then
                        lintRet = 63 'B1- Baixa Renda - Consumo mensal superior a 30 kWh e inferior ou igual a 100 kWh (RES. 945/2010)
                    ElseIf classe = 22 Then
                        lintRet = 64 'B1- Baixa Renda - Consumo mensal superior a 100 kWh e inferior ou igual a 220 kWh (RES. 945/2010)
                    ElseIf classe = 23 Then
                        lintRet = 65 'B1- Baixa Renda - Consumo mensal superior a 220 kWh (RES. 945/2010)
                    End If

                ElseIf classe = 52 Then
                    lintRet = 31 'B2 - Cooperativa de Eletrificação Rural
                ElseIf classe = 54 Then
                    lintRet = 32 'B2 - Serviço Público de Irrigação
                ElseIf CDbl(VB.Left(CStr(classe), 1)) = 5 Then
                    lintRet = 30 'B2 - Rural
                ElseIf classe = 41 Then
                    lintRet = 33 'B3 - Classe Comercial (Consumo mensal até 820 Ah)
                ElseIf classe = 42 Then
                    lintRet = 34 'B3 - Classe Comercial (Consumo mensal até 1.436 Ah)
                ElseIf classe = 61 Then
                    lintRet = 36 'B4a - Iluminação Pública - Rede de Distribuição
                ElseIf classe = 62 Then
                    lintRet = 37 'B4b - Iluminação Pública - Bulbo de Lâmpada
                Else
                    lintRet = 35 'B3 - Demais Classes
                End If
            End If

            '  If SubGrupo = "" Then lintRet = 10 'B1 - Residencial Baixa Renda (Consumo mensal até 136 Ah)
            '  If SubGrupo = "" Then lintRet = 11 'B1 - Residencial Baixa Renda (Consumo mensal de 137 a 236 Ah)
            '  If SubGrupo = "" Then lintRet = 17 'B1 - Residencial Baixa Renda (Consumo mensal de 137 a 341 Ah)
            '  If SubGrupo = "" Then lintRet = 12 'B1 - Residencial Baixa Renda (Consumo mensal de 237 a 341 Ah)
            '  If SubGrupo = "" Then lintRet = 13 'B1 - Residencial Baixa Renda (Consumo mensal de 342 a 590 Ah)
            '  If SubGrupo = "" Then lintRet = 14 'B1 - Residencial Baixa Renda (Consumo mensal de 591 a 1.135 Ah)
            '  If SubGrupo = "" Then lintRet = 51 'B1 - Residencial Baixa Renda (Consumo mensal de 342 a 655 Ah)
            '  If SubGrupo = "" Then lintRet = 18 'B1 - Residencial Baixa Renda (Consumo mensal de 656 a 830 Ah)
            '  If SubGrupo = "" Then lintRet = 15 'B1 - Residencial Baixa Renda (Consumo mensal de 1.136 a 1.436 Ah)
            '  If SubGrupo = "" Then lintRet = 19 'B1 - Residencial Baixa Renda (Consumo mensal de 831 a 1.245 Ah)
            '  If SubGrupo = "" Then lintRet = 16 'B1 - Residencial Baixa Renda (Consumo mensal de 1.437 a 2.154 Ah)


            '  If SubGrupo = "B4" Then lintRet = 38 'B4c - Iluminação Pública - Nível de IP acima do Padrão
            '  If SubGrupo = "" Then lintRet = 26 'A4a - Cooperativa de Eletrificação Rural Tipo 1
            '  If SubGrupo = "" Then lintRet = 27 'A4b - Cooperativa de Eletrificação Rural Tipo 2
            '  If SubGrupo = "" Then lintRet = 28 'A4c - Cooperativa de Eletrificação Rural Tipo 3
            '  If SubGrupo = "" Then lintRet = 20 'A1 (230 kV ou mais)
            '  If SubGrupo = "" Then lintRet = 21 'A2 (88 a 138 kV)
            '  If SubGrupo = "" Then lintRet = 58 'A2 - Cooperativa de Eletrificação Rural (88 a 138 kV)
            '  If SubGrupo = "" Then lintRet = 22 'A3 ( 69 kV)
            '  If SubGrupo = "" Then lintRet = 23 'A3 - Cooperativa de Eletrificação Rural (69 kV)
            '  If SubGrupo = "" Then lintRet = 24 'A3a (30 kV a 44 kV)
            '  If SubGrupo = "" Then lintRet = 53 'A3 - Serviço Público de Irrigação (69 kV)
            '  If SubGrupo = "" Then lintRet = 54 'A3a - Cooperativa de Eletrificação Rural (30 kV a 44 kV)
            '  If SubGrupo = "" Then lintRet = 55 'A3a - Serviço Público de Irrigação (30 kV a 44 kV)
            '  If SubGrupo = "" Then lintRet = 29 'AS (Subterrâneo)
            GetSubGrupo = lintRet
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: GetSubGrupo" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function
        End Try
    End Function

    Private Function GetExpSubGrupo(ByVal aintSubGrupo As Short) As String
        Dim lstrRet As String
        Try
            If aintSubGrupo = 1 Then
                lstrRet = "B1 - Residencial"
            ElseIf aintSubGrupo = 2 Then
                lstrRet = "B1 - Residencial Baixa Renda (Consumo mensal até 30 kWh)"
            ElseIf aintSubGrupo = 3 Then
                lstrRet = "B1 - Residencial Baixa Renda (Consumo mensal superior a 30 kWh e inferior a 80kWh)"
            ElseIf aintSubGrupo = 4 Then
                lstrRet = "B1 - Residencial Baixa Renda (Consumo mensal de 101 a 140 kWh)"
            ElseIf aintSubGrupo = 5 Then
                lstrRet = "B1 - Residencial Baixa Renda (Consumo mensal de 141 a 150 kWh)"
            ElseIf aintSubGrupo = 6 Then
                lstrRet = "B1 - Residencial Baixa Renda (Consumo mensal de 151 a 160 kWh)"
            ElseIf aintSubGrupo = 7 Then
                lstrRet = "B1 - Residencial Baixa Renda (Consumo mensal de 161 a 180 kWh)"
            ElseIf aintSubGrupo = 8 Then
                lstrRet = "B1 - Residencial Baixa Renda (Consumo mensal de 181 a 200 kWh)"
            ElseIf aintSubGrupo = 9 Then
                lstrRet = "B1 - Residencial Baixa Renda (Consumo mensal de 201 a 220 kWh)"
            ElseIf aintSubGrupo = 25 Then
                lstrRet = "A4 (2,3 a 25 kV)"
            ElseIf aintSubGrupo = 30 Then
                lstrRet = "B2 - Rural"
            ElseIf aintSubGrupo = 31 Then
                lstrRet = "B2 - Cooperativa de Eletrificação Rural"
            ElseIf aintSubGrupo = 32 Then
                lstrRet = "B2 - Serviço Público de Irrigação"
            ElseIf aintSubGrupo = 33 Then
                lstrRet = "B3 - Classe Comercial (Consumo mensal até 820 Ah)"
            ElseIf aintSubGrupo = 34 Then
                lstrRet = "B3 - Classe Comercial (Consumo mensal até 1.436 Ah)"
            ElseIf aintSubGrupo = 35 Then
                lstrRet = "B3 - Demais Classes"
            ElseIf aintSubGrupo = 36 Then
                lstrRet = "B4a - Iluminação Pública - Rede de Distribuição"
            ElseIf aintSubGrupo = 37 Then
                lstrRet = "B4b - Iluminação Pública - Bulbo de Lâmpada"
            ElseIf aintSubGrupo = 41 Then
                lstrRet = "B1 - Residencial Baixa Renda (Consumo mensal de 80 a 100 kWh)"
            ElseIf aintSubGrupo = 45 Then
                lstrRet = "B1 - Residencial Baixa Renda(Consumo Mensal Superior ao Limite Regional de 140kWh)"
            ElseIf aintSubGrupo = 46 Then
                lstrRet = "B1 - Residencial Baixa Renda(Consumo Mensal Superior ao Limite Regional de 150kWh)"
            ElseIf aintSubGrupo = 47 Then
                lstrRet = "B1 - Residencial Baixa Renda(Consumo Mensal Superior ao Limite Regional de 160kWh)"
            ElseIf aintSubGrupo = 48 Then
                lstrRet = "B1 - Residencial Baixa Renda(Consumo Mensal Superior ao Limite Regional de 180kWh)"
            ElseIf aintSubGrupo = 49 Then
                lstrRet = "B1 - Residencial Baixa Renda(Consumo Mensal Superior ao Limite Regional de 200kWh)"
            ElseIf aintSubGrupo = 50 Then
                lstrRet = "B1 - Residencial Baixa Renda(Consumo Mensal Superior ao Limite Regional de 220kWh)"
            ElseIf aintSubGrupo = 52 Then
                lstrRet = "A4 - Cooperativa de Eletrificação Rural (2,3 kV a 25 kV)"
            ElseIf aintSubGrupo = 56 Then
                lstrRet = "A4 - Serviço Público de Irrigação (2,3 kV a 25 kV)"
            ElseIf aintSubGrupo = 57 Then
                lstrRet = "B1 - Residencial Baixa Renda"
            ElseIf aintSubGrupo = 60 Then
                lstrRet = "B1 - Residencial Baixa Renda (Consumo mensal superior a 100 até 200 kWh)"
            ElseIf aintSubGrupo = 62 Then
                lstrRet = "B1- Baixa Renda - Consumo mensal inferior ou igual a 30 kWh (RES. 945/2010)"
            ElseIf aintSubGrupo = 63 Then
                lstrRet = "B1- Baixa Renda - Consumo mensal superior a 30 kWh e inferior ou igual a 100 kWh (RES. 945/2010)"
            ElseIf aintSubGrupo = 64 Then
                lstrRet = "B1- Baixa Renda - Consumo mensal superior a 100 kWh e inferior ou igual a 220 kWh (RES. 945/2010)"
            ElseIf aintSubGrupo = 65 Then
                lstrRet = "B1- Baixa Renda - Consumo mensal superior a 220 kWh (RES. 945/2010)"
            End If

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

    Private Function GetDetalheFornecimento(ByVal astrTipo As String) As Short
        Dim lintRet As Short
        Try
            If astrTipo = "QC" Then
                lintRet = 1 'Número de consumidores
            ElseIf astrTipo = "EG" Then
                lintRet = 21 'Antigo Energia Gerada / Novo Energia Injetada (kWh)
            ElseIf astrTipo = "EC" Then
                lintRet = 24 'Energia Consumida (kWh)
            ElseIf astrTipo = "EF" Then
                lintRet = 22 'Energia Faturada (kWh)
            ElseIf astrTipo = "CF" Then
                lintRet = 3 'Consumo Faturado (kWh)
            ElseIf astrTipo = "DC" Then
                lintRet = 20 'Desconto Consumo %
            ElseIf astrTipo = "RC" Then
                lintRet = 6 'Receita Consumo (R$)
            ElseIf astrTipo = "RB" Then
                lintRet = 25 'Receita Bandeiras (R$)
            ElseIf astrTipo = "DF" Then
                lintRet = 2 'Demanda Faturada(kW)
            ElseIf astrTipo = "RD" Then
                lintRet = 5 'Receita Demanda (R$)
            ElseIf astrTipo = "DD" Then
                lintRet = 19 'Desconto Demanda %
            ElseIf astrTipo = "UD" Then
                lintRet = 7 'Ultrapassagem Demanda (kW)
            ElseIf astrTipo = "RU" Then
                lintRet = 8 'Receita Ultrapassagem Demanda (R$)
            ElseIf astrTipo = "I" Then
                lintRet = 9 'ICMS (R$)
            ElseIf astrTipo = "P" Then
                lintRet = 17 'PIS/PASEP (R$)
            ElseIf astrTipo = "C" Then
                lintRet = 18 'COFINS (R$)
            ElseIf astrTipo = "L" Then
                lintRet = 10 'Limite Regional (kWh)
            ElseIf astrTipo = "E_COMP" Then
                lintRet = 26 'Energia Compensada (kWh)
            ElseIf astrTipo = "RC_COMP" Then
                lintRet = 27 'Receita energia compensada (R$);
            ElseIf astrTipo = "DESC_COMP" Then
                lintRet = 28 'Desconto de energia compensada %.;
            ElseIf astrTipo = "ENERG_DEB_SCEE" Then
                lintRet = 23 'Desconto de energia compensada %.;
            Else
                lintRet = 0
            End If

            '  If astrTipo = "" Then lintRet = 4  'Consumo Faturado (kWh Equivalentes)
            '  If astrTipo = "" Then lintRet = 23 'Energia debitada do SCEE (kWh)
            '  If astrTipo = "" Then lintRet = 15 'Energia Adicional Temporária (kWh)
            '  If astrTipo = "" Then lintRet = 16 'Receita Energia Adicional Temporária (R$)
            '  If astrTipo = "" Then lintRet = 11 'Subsídio Mês de Competência Sm(%)
            '  If astrTipo = "" Then lintRet = 12 'Subsídio Mês de Abril/2002 Sref(%)
            '  If astrTipo = "" Then lintRet = 13 'Faturamento Real Residencial Mês de Competência Zm(R$)
            '  If astrTipo = "" Then lintRet = 14 'Diferença Mensal Receita Dm(R$)
            GetDetalheFornecimento = lintRet
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: GetDetalheFornecimento" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function
        End Try
    End Function

    Private Function GetExpDetalheFornecimento(ByVal aintDetalhe As Short) As String
        Dim lstrRet As String
        Try
            If aintDetalhe = 1 Then
                lstrRet = "Número de consumidores"
            ElseIf aintDetalhe = 2 Then
                lstrRet = "Demanda Faturada(kW)"
            ElseIf aintDetalhe = 3 Then
                lstrRet = "Consumo Faturado (kWh)"
            ElseIf aintDetalhe = 5 Then
                lstrRet = "Receita Demanda (R$)"
            ElseIf aintDetalhe = 6 Then
                lstrRet = "Receita Consumo (R$)"
            ElseIf aintDetalhe = 7 Then
                lstrRet = "Ultrapassagem Demanda (kW)"
            ElseIf aintDetalhe = 8 Then
                lstrRet = "Receita Ultrapassagem Demanda (R$)"
            ElseIf aintDetalhe = 9 Then
                lstrRet = "ICMS (R$)"
            ElseIf aintDetalhe = 10 Then
                lstrRet = "Limite Regional (kWh)"
            ElseIf aintDetalhe = 17 Then
                lstrRet = "PIS/PASEP (R$)"
            ElseIf aintDetalhe = 18 Then
                lstrRet = "COFINS (R$)"
            ElseIf aintDetalhe = 19 Then
                lstrRet = "Desconto Demanda %"
            ElseIf aintDetalhe = 20 Then
                lstrRet = "Desconto Consumo %"
            ElseIf aintDetalhe = 21 Then
                lstrRet = "Energia Gerada (kWh)"
            ElseIf aintDetalhe = 22 Then
                lstrRet = "Energia Faturada (kWh)"
            ElseIf aintDetalhe = 24 Then
                lstrRet = "Energia Consumida (kWh)"
            ElseIf aintDetalhe = 25 Then
                lstrRet = "Receita Bandeiras (R$)"
            ElseIf aintDetalhe = 26 Then
                lstrRet = "Energia compensada (kWh)"
            ElseIf aintDetalhe = 27 Then
                lstrRet = "Receita energia compensada (R$)"
            ElseIf aintDetalhe = 28 Then
                lstrRet = "Desconto de energia compensada %"
            Else
                lstrRet = ""
            End If
            GetExpDetalheFornecimento = lstrRet
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: GetExpDetalheFornecimento" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function
        End Try
    End Function

    Private Function GetPostoTarifario(ByVal astrTipo As String) As Short
        Dim lintRet As Short
        Try
            If astrTipo = "P" Then
                lintRet = 1 'Ponta
            ElseIf astrTipo = "FP" Then
                lintRet = 2 'Fora de Ponta
            ElseIf astrTipo = "ND" Then
                lintRet = 3 'Não se aplica
            ElseIf astrTipo = "N" Then
                lintRet = 4 'Período Normal
            ElseIf astrTipo = "R" Then
                lintRet = 5 'Período Reservado
            ElseIf astrTipo = "I" Then
                lintRet = 6 'Intermediário
            Else
                lintRet = 3
            End If
            GetPostoTarifario = lintRet
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: GetPostoTarifario" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function
        End Try
    End Function

    Private Function GetExpPostoTarifario(ByVal aintPosto As Short) As String
        Dim lstrRet As String
        Try
            If aintPosto = 1 Then
                lstrRet = "Ponta"
            ElseIf aintPosto = 2 Then
                lstrRet = "Fora de Ponta"
            ElseIf aintPosto = 3 Then
                lstrRet = "Não se aplica"
            ElseIf aintPosto = 4 Then
                lstrRet = "Período Normal"
            ElseIf aintPosto = 5 Then
                lstrRet = "Período Reservado"
            ElseIf aintPosto = 6 Then
                lstrRet = "Intermediário"
            End If
            GetExpPostoTarifario = lstrRet
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: GetExpPostoTarifario" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function
        End Try
    End Function


#Region "BE"
    Private Function GetDetalheBalancoEnergetico(ByVal astrTipo As String) As Short
        Dim lintRet As Short
        Try
            If astrTipo = "EG" Then
                lintRet = 1 'Energia GeradA(KWh)
            ElseIf astrTipo = "EM" Then
                lintRet = 2 'Energia Medida (kWh)
            ElseIf astrTipo = "EC" Then
                lintRet = 3 'Energia Calculada (kWh)
            ElseIf astrTipo = "EF" Then
                lintRet = 4 'Energia Faturada (kWh)
            ElseIf astrTipo = "EM_A1" Then
                lintRet = 5 'Energia Medida (kWh) - A1 (230 kV ou mais)
            ElseIf astrTipo = "EM_A2" Then
                lintRet = 6 'Energia Medida (kWh) - A2 (88 a 138 kV)
            ElseIf astrTipo = "EM_A3" Then
                lintRet = 7 'Energia Medida (kWh) - A3 (69 kV)
            ElseIf astrTipo = "EM_A3a" Then
                lintRet = 8 'Energia Medida (kWh) - A3a (30 a 44 kV)
            ElseIf astrTipo = "EM_A4" Then
                lintRet = 9 'Energia Medida (kWh) - A4 (2,3 a 25 kV)
            ElseIf astrTipo = "EM_BT" Then
                lintRet = 10 'Energia Medida (kWh) - BT (Menor que 2,3 kV)

            ElseIf astrTipo = "EM_AS" Then
                lintRet = 11 'Energia Medida (kWh) - AS (Subterrâneo) - (Menor que 2,3 kV)
            ElseIf astrTipo = "EM_A2_AS" Then
                lintRet = 12 'Energia Medida (kWh) - AS (Subterrâneo) - A2
            ElseIf astrTipo = "EM_A3_AS" Then
                lintRet = 13 'Energia Medida (kWh) - AS (Subterrâneo) - A3
            ElseIf astrTipo = "EM_A3a_AS" Then
                lintRet = 14 'Energia Medida (kWh) - AS (Subterrâneo) - A3a
            ElseIf astrTipo = "EM_A4_AS" Then
                lintRet = 15 'Energia Medida (kWh) - AS (Subterrâneo) - A4
            ElseIf astrTipo = "EM_BT_AS" Then
                lintRet = 16 'Energia Medida (kWh) - AS (Subterrâneo) - BT

            ElseIf astrTipo = "EF_A1" Then
                lintRet = 17 'Energia Faturada (kWh) - A1 (230 kV ou mais)
            ElseIf astrTipo = "EF_A2" Then
                lintRet = 18 'Energia Faturada (kWh) - A2 (88 a 138 kV)
            ElseIf astrTipo = "EF_A3" Then
                lintRet = 19 'Energia Faturada (kWh) - A3 (69 kV)
            ElseIf astrTipo = "EF_A3a" Then
                lintRet = 20 'Energia Faturada (kWh) - A3a (30 a 44 kV)
            ElseIf astrTipo = "EF_A4" Then
                lintRet = 21 'Energia Faturada (kWh) - A4 (2,3 a 25 kV)
            ElseIf astrTipo = "EF_BT" Then
                lintRet = 22 'Energia Faturada (kWh) - BT (Menor que 2,3 kV
            ElseIf astrTipo = "EF_AS" Then
                lintRet = 23 'Energia Faturada (kWh) - AS (Subterrâneo) - (Menor que 2,3 kV)
            Else
                lintRet = 0
            End If

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

    Private Function GetCaracteristicasBalancoEnergetico(ByVal astrTipo As String) As Short
        Dim lintRet As Short
        Try

            If astrTipo = "HD" Then
                lintRet = 1 'Hidroelétrica
            ElseIf astrTipo = "TERM_NUC" Then
                lintRet = 2 'Termo Nuclear
            ElseIf astrTipo = "OL_DIE" Then
                lintRet = 3 'Óleo Diesel
            ElseIf astrTipo = "OL_COMB" Then
                lintRet = 4 'Óleo Combustível (Fuel Oil)
            ElseIf astrTipo = "CONT_INI" Then
                lintRet = 9 'Contrato Inicial
            ElseIf astrTipo = "IT" Then
                lintRet = 12 'Itaipu
            ElseIf astrTipo = "CONS_CAT" Then
                lintRet = 13 'Consumidor Cativo - Mercado de Referência
            ElseIf astrTipo = "CONS_PROP" Then
                lintRet = 15 'Consumo Próprio
            ElseIf astrTipo = "CONS_LIV" Then
                lintRet = 16 'Consumidor Livre
            ElseIf astrTipo = "PE_TOT" Then
                lintRet = 17 'Perdas Totais
            ElseIf astrTipo = "GAS" Then
                lintRet = 19 'Gás
            ElseIf astrTipo = "CV" Then
                lintRet = 20 'Carvão
            ElseIf astrTipo = "CONS_BOMB" Then
                lintRet = 21 'Consumo de Bombas
            ElseIf astrTipo = "SL_AE" Then
                lintRet = 22 'Solar Aeólica
            ElseIf astrTipo = "PART_CONS" Then
                lintRet = 23 'Participação no Consórcio
            ElseIf astrTipo = "COMP/REP_ITA/IMP" Then
                lintRet = 24 'Compra/Repasse Itaipu/Importação
            ElseIf astrTipo = "CONT_BI" Then
                lintRet = 25 'Contrato Bilateral
            ElseIf astrTipo = "EOL" Then
                lintRet = 26 'Eólica
            ElseIf astrTipo = "BIO" Then
                lintRet = 27 'Biomassa
            ElseIf astrTipo = "FORN_CAT_TOT" Then
                lintRet = 28 'Fornecimento - Cativo TOTAL
            ElseIf astrTipo = "SUP_TOT" Then
                lintRet = 29 'Suprimento TOTAL
            ElseIf astrTipo = "MERC_LIV_TOT" Then
                lintRet = 30 'Mercado Livre TOTAL
            ElseIf astrTipo = "USO_DIST_TOT" Then
                lintRet = 31 'Uso Distribuidoras TOTAL
            ElseIf astrTipo = "PERD_N_TEC" Then
                lintRet = 32 'Perdas Não-Técnicas
            ElseIf astrTipo = "PERD_TEC" Then
                lintRet = 33 'Perdas Técnicas
            ElseIf astrTipo = "ENERG_INJ_TOT" Then
                lintRet = 34 'Energia Injetada Total
            ElseIf astrTipo = "FORN_CONS_PROP_TOT" Then
                lintRet = 35 'Fornecimento - consumo próprio TOTAL
            ElseIf astrTipo = "ENER_INJ" Then
                lintRet = 36 'Energia Injetada
            ElseIf astrTipo = "MERC_LIVRE" Then
                lintRet = 37 'Mercado Livre
            ElseIf astrTipo = "USO_DIST" Then
                lintRet = 38 'Uso Distribuidoras
            ElseIf astrTipo = "SUP" Then
                lintRet = 39 'Suprimento
            ElseIf astrTipo = "FORN_CAT" Then
                lintRet = 40 'Fornecimento - Cativo
            ElseIf astrTipo = "FORN_CONS_PROP" Then
                lintRet = 41 'Fornecimento - consumo próprio
            ElseIf astrTipo = "ENER_IN_MICRO_MINI" Then
                lintRet = 42 'Energia Injetada na rede pela Micro e Mini Geração (REN 482/12)
            ElseIf astrTipo = "ENER_IN_MICRO_MINI_TOT" Then
                lintRet = 43 'Energia Injetada na rede pela Micro e Mini Geração (REN 482/12) TOTAL
            ElseIf astrTipo = "ENER_ASSOC_COB" Then
                lintRet = 45 'Energia associada à cobrança por procedimento irregular
            Else
                lintRet = 0
            End If

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

    Private Function GetModalidadesBalancoEnergetico(ByVal astrTipo As String) As Short
        Dim lintRet As Short
        Try

            If astrTipo = "GP" Then
                lintRet = 1 'Geração Própria (Bruta)
            ElseIf astrTipo = "FR" Then
                lintRet = 3 'Fornecimento
            ElseIf astrTipo = "EV" Then
                lintRet = 4 'Energia Vendida
            ElseIf astrTipo = "PERD" Then
                lintRet = 5 'Perdas
            ElseIf astrTipo = "BM" Then
                lintRet = 6 'Bombeamento
            ElseIf astrTipo = "GC" Then
                lintRet = 7 'Geração Compartilhada
            ElseIf astrTipo = "ER" Then
                lintRet = 8 'Energia Recebida
            ElseIf astrTipo = "EI" Then
                lintRet = 9 'Energia Injetada
            ElseIf astrTipo = "GD" Then
                lintRet = 11 'Geração Distribuída de Terceiros
            ElseIf astrTipo = "EE" Then
                lintRet = 12 'Energia Entregue
            ElseIf astrTipo = "PDM" Then
                lintRet = 13 'Perdas na Distribuição (valor medido)
            ElseIf astrTipo = "PDF" Then
                lintRet = 14 'Perdas na Distribuição (valor faturado)
            Else
                lintRet = 0
            End If

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

    Private Function GetFluxosBalancoEnergetico(ByVal astrTipo As String) As Short
        Dim lintRet As Short
        Try

            If astrTipo = "D" Then
                lintRet = 1 'Disponibilidades
            ElseIf astrTipo = "R" Then
                lintRet = 2 'Requisitos
            ElseIf astrTipo = "S" Then
                lintRet = 3 'Salvos
            Else
                lintRet = 0
            End If

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

    Private Function GetExpDetalheBalancoEnergetico(ByVal astrTipo As String) As String
        Dim lstrRet As String
        Try
            If astrTipo = "1" Then
                lstrRet = "Energia Gerada(KWh)"
            ElseIf astrTipo = "2" Then
                lstrRet = "Energia Medida (kWh)"
            ElseIf astrTipo = "3" Then
                lstrRet = "Energia Calculada (kWh)"
            ElseIf astrTipo = "4" Then
                lstrRet = "Energia Faturada (kWh)"
            ElseIf astrTipo = "5" Then
                lstrRet = "Energia Medida (kWh) - A1 (230 kV ou mais)"
            ElseIf astrTipo = "6" Then
                lstrRet = "Energia Medida (kWh) - A2 (88 a 138 kV)"
            ElseIf astrTipo = "7" Then
                lstrRet = "Energia Medida (kWh) - A3 (69 kV)"
            ElseIf astrTipo = "8" Then
                lstrRet = "Energia Medida (kWh) - A3a (30 a 44 kV)"
            ElseIf astrTipo = "9" Then
                lstrRet = "Energia Medida (kWh) - A4 (2,3 a 25 kV)"
            ElseIf astrTipo = "10" Then
                lstrRet = "Energia Medida (kWh) - BT (Menor que 2,3 kV)"

            ElseIf astrTipo = "11" Then
                lstrRet = "Energia Medida (kWh) - AS (Subterrâneo) - (Menor que 2,3 kV)"
            ElseIf astrTipo = "12" Then
                lstrRet = "Energia Medida (kWh) - AS (Subterrâneo) - A2"
            ElseIf astrTipo = "13" Then
                lstrRet = "Energia Medida (kWh) - AS (Subterrâneo) - A3"
            ElseIf astrTipo = "14" Then
                lstrRet = "Energia Medida (kWh) - AS (Subterrâneo) - A3a"
            ElseIf astrTipo = "15" Then
                lstrRet = "Energia Medida (kWh) - AS (Subterrâneo) - A4"
            ElseIf astrTipo = "16" Then
                lstrRet = "Energia Medida (kWh) - AS (Subterrâneo) - BT"

            ElseIf astrTipo = "17" Then
                lstrRet = "Energia Faturada (kWh) - A1 (230 kV ou mais)"
            ElseIf astrTipo = "18" Then
                lstrRet = "Energia Faturada (kWh) - A2 (88 a 138 kV)"
            ElseIf astrTipo = "19" Then
                lstrRet = "Energia Faturada (kWh) - A3 (69 kV)"
            ElseIf astrTipo = "20" Then
                lstrRet = "Energia Faturada (kWh) - A3a (30 a 44 kV)"
            ElseIf astrTipo = "21" Then
                lstrRet = "Energia Faturada (kWh) - A4 (2,3 a 25 kV)"
            ElseIf astrTipo = "22" Then
                lstrRet = "Energia Faturada (kWh) - BT (Menor que 2,3 kV"
            ElseIf astrTipo = "23" Then
                lstrRet = "Energia Faturada (kWh) - AS (Subterrâneo) - (Menor que 2,3 kV)"
            Else
                lstrRet = ""
            End If

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

    Private Function GetExpCaracteristicasBalancoEnergetico(ByVal astrTipo As String) As String
        Dim lstrRet As String
        Try

            If astrTipo = "1" Then
                lstrRet = "Hidroelétrica"
            ElseIf astrTipo = "2" Then
                lstrRet = "Termo Nuclear"
            ElseIf astrTipo = "3" Then
                lstrRet = "Óleo Diesel"
            ElseIf astrTipo = "4" Then
                lstrRet = "Óleo Combustível (Fuel Oil)"
            ElseIf astrTipo = "9" Then
                lstrRet = "Contrato Inicial"
            ElseIf astrTipo = "12" Then
                lstrRet = "Itaipu"
            ElseIf astrTipo = "13" Then
                lstrRet = "Consumidor Cativo - Mercado de Referência"
            ElseIf astrTipo = "15" Then
                lstrRet = "Consumo Próprio"
            ElseIf astrTipo = "16" Then
                lstrRet = "Consumidor Livre"
            ElseIf astrTipo = "17" Then
                lstrRet = "Perdas Totais"
            ElseIf astrTipo = "19" Then
                lstrRet = "Gás"
            ElseIf astrTipo = "20" Then
                lstrRet = "Carvão"
            ElseIf astrTipo = "21" Then
                lstrRet = "Consumo de Bombas"
            ElseIf astrTipo = "22" Then
                lstrRet = "Solar Aeólica"
            ElseIf astrTipo = "23" Then
                lstrRet = "Participação no Consórcio"
            ElseIf astrTipo = "24" Then
                lstrRet = "Compra/Repasse Itaipu/Importação"
            ElseIf astrTipo = "25" Then
                lstrRet = "Contrato Bilateral"
            ElseIf astrTipo = "26" Then
                lstrRet = "Eólica"
            ElseIf astrTipo = "27" Then
                lstrRet = "Biomassa"
            ElseIf astrTipo = "28" Then
                lstrRet = "Fornecimento - Cativo TOTAL"
            ElseIf astrTipo = "29" Then
                lstrRet = "Suprimento TOTAL"
            ElseIf astrTipo = "30" Then
                lstrRet = "Mercado Livre TOTAL"
            ElseIf astrTipo = "31" Then
                lstrRet = "Uso Distribuidoras TOTAL"
            ElseIf astrTipo = "32" Then
                lstrRet = "Perdas Não-Técnicas"
            ElseIf astrTipo = "33" Then
                lstrRet = "Perdas Técnicas"
            ElseIf astrTipo = "34" Then
                lstrRet = "Energia Injetada Total"
            ElseIf astrTipo = "35" Then
                lstrRet = "Fornecimento - consumo próprio TOTAL"
            ElseIf astrTipo = "36" Then
                lstrRet = "Energia Injetada"
            ElseIf astrTipo = "37" Then
                lstrRet = "Mercado Livre"
            ElseIf astrTipo = "38" Then
                lstrRet = "Uso Distribuidoras"
            ElseIf astrTipo = "39" Then
                lstrRet = "Suprimento"
            ElseIf astrTipo = "40" Then
                lstrRet = "Fornecimento - Cativo"
            ElseIf astrTipo = "41" Then
                lstrRet = "Fornecimento - consumo próprio"
            ElseIf astrTipo = "42" Then
                lstrRet = "Energia Injetada na rede pela Micro e Mini Geração (REN 482/12)"
            ElseIf astrTipo = "43" Then
                lstrRet = "Energia Injetada na rede pela Micro e Mini Geração (REN 482/12) TOTAL"
            ElseIf astrTipo = "45" Then
                lstrRet = "Energia associada à cobrança por procedimento irregular"
            Else
                lstrRet = ""
            End If

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

    Private Function GetExpModalidadesBalancoEnergetico(ByVal astrTipo As String) As String
        Dim lstrRet As String
        Try

            If astrTipo = "1" Then
                lstrRet = "Geração Própria (Bruta)"
            ElseIf astrTipo = "3" Then
                lstrRet = "Fornecimento"
            ElseIf astrTipo = "4" Then
                lstrRet = "Energia Vendida"
            ElseIf astrTipo = "5" Then
                lstrRet = "Perdas"
            ElseIf astrTipo = "6" Then
                lstrRet = "Bombeamento"
            ElseIf astrTipo = "7" Then
                lstrRet = "Geração Compartilhada"
            ElseIf astrTipo = "8" Then
                lstrRet = "Energia Recebida"
            ElseIf astrTipo = "9" Then
                lstrRet = "Energia Injetada"
            ElseIf astrTipo = "11" Then
                lstrRet = "Geração Distribuída de Terceiros"
            ElseIf astrTipo = "12" Then
                lstrRet = "Energia Entregue"
            ElseIf astrTipo = "13" Then
                lstrRet = "Perdas na Distribuição (valor medido)"
            ElseIf astrTipo = "14" Then
                lstrRet = "Perdas na Distribuição (valor faturado)"
            Else
                lstrRet = ""
            End If

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

    Private Function GetExpFluxosBalancoEnergetico(ByVal astrTipo As String) As String
        Dim lstrRet As String
        Try

            If astrTipo = "1" Then
                lstrRet = "Disponibilidades"
            ElseIf astrTipo = "2" Then
                lstrRet = "Requisitos"
            ElseIf astrTipo = "3" Then
                lstrRet = "Salvos"
            Else
                lstrRet = 0
            End If

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

    Private Function Exporta_Grava_Balanco_Energetico(ByVal ldteFim As Date, ByVal ldteIni As Date) As Boolean

        Dim ldblEnergia_Injetada_Enel As Double
        Dim ldblEnergia_Injetada_Grp_A As Double
        Dim ldblEnergia_Injetada_Grp_B As Double
        Dim ldblSubTot_Energia_Injetada_Grp_A_B As Double
        Dim ldblTotal_Energia_Injetada As Double

        Dim ldblTotal_Consumo_Medido_Grp_A As Double
        Dim ldblTotal_Consumo_Medido_Grp_B As Double
        Dim ldblTotal_Consumo_Medido_Grp_A_B As Double

        Dim ldblTotal_Consumo_Faturado_Grp_A As Double
        Dim ldblTotal_Consumo_Faturado_Grp_B As Double
        Dim ldblTotal_Consumo_Faturado_Grp_A_B As Double

        Dim ldblTotal_Consumo_Proprio As Double

        Dim ldblPerdasTotais_Medido As Double
        Dim ldblPerdasTecnicas_Medido As Double
        Dim ldblPerdasNaoTecnicas_Medido As Double

        Dim ldblPerdasTotais_Faturado As Double
        Dim ldblPerdasTecnicas_Faturado As Double
        Dim ldblPerdasNaoTecnicas_Faturado As Double

        '
        Dim lstrSql As String

        Try
            Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
            Call LigDes(False)
            iblnExporta = False

            lstrSql = ""
            lstrSql = "DELETE FROM `samp_exportacao_be` WHERE `ano_mes` = " & VB6.Format(Me.dtpMesAno.Value, "yyyyMM")
            objExecAtualizaBancoDados.QUERY_NAME = lstrSql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            Me.lblInfoPre.Text = " Buscando Balanço Energetico, aguarde.... "
            Call Me.lblInfoPre.Refresh()

            lstrSql = ""
            lstrSql = lstrSql & " SELECT "
            lstrSql = lstrSql & " CASE "
            lstrSql = lstrSql & "   WHEN LEFT(codclas, 1) = '9' AND RIGHT(codclas, 1) <> '1' THEN 'Bx.Renda' " & vbCrLf
            lstrSql = lstrSql & "   WHEN LEFT(codclas, 1) = '9' AND RIGHT(codclas, 1) = '1' THEN 'Residencial Normal' " & vbCrLf
            lstrSql = lstrSql & "   WHEN LEFT(codclas, 1) = '4' THEN 'Comercial' " & vbCrLf
            lstrSql = lstrSql & "   WHEN LEFT(codclas, 1) = '5' THEN  " & vbCrLf
            lstrSql = lstrSql & "       CASE  " & vbCrLf
            lstrSql = lstrSql & "           WHEN LEFT(codclas, 2) = '55' THEN 'Rural Irrigante' " & vbCrLf
            lstrSql = lstrSql & "           WHEN LEFT(codclas, 2) = '57' THEN 'Rural Aquicultor'  " & vbCrLf
            lstrSql = lstrSql & "           WHEN LEFT(codclas, 2) = '52' THEN 'Rural Coop. Eletrif. Rural'  " & vbCrLf
            lstrSql = lstrSql & "           WHEN LEFT(codclas, 2) = '54' THEN 'Rural Serv. Púb. Irrigação'  " & vbCrLf
            lstrSql = lstrSql & "           ELSE 'Rural'  " & vbCrLf
            lstrSql = lstrSql & "       END  " & vbCrLf
            lstrSql = lstrSql & "   WHEN LEFT(codclas, 1) = '7' THEN  " & vbCrLf
            lstrSql = lstrSql & "       CASE  " & vbCrLf
            lstrSql = lstrSql & "           WHEN LEFT(codclas, 2) = '71' THEN 'Serviço Público (tração elétrica)'  " & vbCrLf
            lstrSql = lstrSql & "           WHEN LEFT(codclas, 2) = '73' THEN 'Serviço Público de Irrigação'  " & vbCrLf
            lstrSql = lstrSql & "           ELSE 'Serviço Público'  " & vbCrLf
            lstrSql = lstrSql & "       END  " & vbCrLf

            lstrSql = lstrSql & "   WHEN LEFT(codclas, 1) = '8' THEN 'Consumo Próprio'  " & vbCrLf
            lstrSql = lstrSql & "   WHEN LEFT(codclas, 1) < '4' THEN 'Industrial' " & vbCrLf
            lstrSql = lstrSql & "   WHEN LEFT(codclas, 1) = '6' THEN  " & vbCrLf
            lstrSql = lstrSql & "       CASE  " & vbCrLf
            lstrSql = lstrSql & "           WHEN LEFT(codclas, 4) = '6401' THEN 'Ilum.Púb.(Rede)' " & vbCrLf
            lstrSql = lstrSql & "           WHEN LEFT(codclas, 4) = '6402' THEN 'Ilum.Púb.(Bulbo)'  " & vbCrLf
            lstrSql = lstrSql & "           ELSE 'Poder Público' " & vbCrLf
            lstrSql = lstrSql & "       END  " & vbCrLf
            lstrSql = lstrSql & "       ELSE 'Indefinido'  " & vbCrLf
            lstrSql = lstrSql & " END AS Classe, " & vbCrLf


            lstrSql = lstrSql & " grupo, " & vbCrLf

            'lstrSql = lstrSql & " 2064956 AS 'kWh_Injetado_Enel', " & vbCrLf

            lstrSql = lstrSql & " IF(`grupo` = 'A', SUM(buscavaloritem(`itensserv`, '0302012010') + buscavaloritem(`itensserv`, '0302012011')), 0.00) AS 'kWh_Injetado_MicroGeracao_A', " & vbCrLf
            lstrSql = lstrSql & " IF(`grupo` = 'B', SUM(buscavaloritem(`itensserv`, '0302020996')), 0.00) AS 'kWh_Injetado_MicroGeracao_B', " & vbCrLf

            lstrSql = lstrSql & " IF(`grupo` = 'A', SUM(buscavaloritem(`itensserv`, '0302010010')), 0.00) AS 'kWh_Consumo_Lido_A_Ponta', " & vbCrLf
            lstrSql = lstrSql & " IF(`grupo` = 'A', SUM(buscavaloritem(`itensserv`, '0302010011')), 0.00) AS 'kWh_Consumo_Lido_A_FPonta', " & vbCrLf

            lstrSql = lstrSql & " IF(`grupo` = 'A', SUM(buscavaloritem(`itensserv`, '0302010010') - buscavaloritem(`itensserv`, '0302012010')), 0.00)  AS 'kWh_Consumo_Faturado_A_Ponta', " & vbCrLf
            lstrSql = lstrSql & " IF(`grupo` = 'A', SUM(buscavaloritem(`itensserv`, '0302010011') - buscavaloritem(`itensserv`, '0302012011')), 0.00)  AS 'kWh_Consumo_Faturado_A_FPonta', " & vbCrLf

            lstrSql = lstrSql & " IF(`grupo` = 'B', SUM(buscavaloritem(`itensserv`, '030201008')), 0.00) AS 'kWh_Consumo_Medido_B', " & vbCrLf
            lstrSql = lstrSql & " IF(`grupo` = 'B', SUM(buscavaloritem(`itensserv`, '030201009')), 0.00) AS 'kWh_Consumo_Faturado_B' " & vbCrLf

            lstrSql = lstrSql & "  FROM `nf` " & vbCrLf
            lstrSql = lstrSql & " WHERE ((`anomes_fat` = " & CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")) & " " & vbCrLf
            lstrSql = lstrSql & "   AND `mesano` = '" & VB6.Format(Me.dtpMesAno.Value, "MM/yyyy") & "') " & vbCrLf
            lstrSql = lstrSql & "   AND (`flgst` <> 'C' OR " & vbCrLf
            lstrSql = lstrSql & "        ( (`flgst` = 'C' AND `datast` > '" & VB6.Format(ldteFim, "yyyy-MM-dd") & "') OR " & vbCrLf
            lstrSql = lstrSql & "          (`flgst` = 'C' AND `mesano_ref` <> REPLACE(LEFT(`datast`, 7),'-','') AND `datast` BETWEEN '" & VB6.Format(ldteIni, "yyyy-MM-dd") & "' AND '" & VB6.Format(ldteFim, "yyyy-MM-dd") & "')" & vbCrLf
            lstrSql = lstrSql & "         )" & vbCrLf
            lstrSql = lstrSql & "       ))" & vbCrLf
            lstrSql = lstrSql & " AND flgmedfat <> 'S' " & vbCrLf

            lstrSql = lstrSql & " GROUP BY `Classe`, `Grupo` " & vbCrLf
            lstrSql = lstrSql & " ORDER BY `Grupo`, `Classe` ;" & vbCrLf

            objAtualizaBancoDados.QUERY_NAME = lstrSql
            dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            If dt.Rows.Count > 0 Then

                ldblEnergia_Injetada_Enel = txt_Energ_Inj_Enel.Text

                ldblEnergia_Injetada_Grp_A = dt.AsEnumerable().Sum(Function(row) Math.Round(row.Field(Of Double)("kWh_Injetado_MicroGeracao_A"), 2))
                ldblEnergia_Injetada_Grp_B = dt.AsEnumerable().Sum(Function(row) Math.Round(row.Field(Of Double)("kWh_Injetado_MicroGeracao_B"), 2))

                ldblSubTot_Energia_Injetada_Grp_A_B = ldblEnergia_Injetada_Grp_A + ldblEnergia_Injetada_Grp_B
                ldblTotal_Energia_Injetada = ldblEnergia_Injetada_Enel + ldblSubTot_Energia_Injetada_Grp_A_B


                ldblTotal_Consumo_Medido_Grp_A = dt.AsEnumerable().Sum(Function(row) Math.Round(row.Field(Of Double)("kWh_Consumo_Lido_A_Ponta"), 2) + Math.Round(row.Field(Of Double)("kWh_Consumo_Lido_A_FPonta"), 2))
                ldblTotal_Consumo_Medido_Grp_B = dt.AsEnumerable().Where(Function(row) row.Field(Of String)("Classe") <> "Consumo Próprio".ToUpper).Sum(Function(row) Math.Round(row.Field(Of Double)("kWh_Consumo_Medido_B"), 2))
                ldblTotal_Consumo_Faturado_Grp_A = dt.AsEnumerable().Sum(Function(row) Math.Round(row.Field(Of Double)("kWh_Consumo_Faturado_A_Ponta"), 2) + Math.Round(row.Field(Of Double)("kWh_Consumo_Faturado_A_FPonta"), 2))

                ldblTotal_Consumo_Faturado_Grp_B = dt.AsEnumerable().Where(Function(row) row.Field(Of String)("Classe") <> "Consumo Próprio".ToUpper).Sum(Function(row) Math.Round(row.Field(Of Double)("kWh_Consumo_Faturado_B"), 2))


                ldblTotal_Consumo_Proprio = dt.AsEnumerable().Where(Function(row) row.Field(Of String)("Classe") = "Consumo Próprio".ToUpper).Sum(Function(row) Math.Round(row.Field(Of Double)("kWh_Consumo_Faturado_B"), 2))
                ldblTotal_Consumo_Medido_Grp_A_B = ldblTotal_Consumo_Medido_Grp_A + ldblTotal_Consumo_Medido_Grp_B + ldblTotal_Consumo_Proprio
                ldblTotal_Consumo_Faturado_Grp_A_B = ldblTotal_Consumo_Faturado_Grp_A + ldblTotal_Consumo_Faturado_Grp_B + ldblTotal_Consumo_Proprio

                Dim perc As Double = GetConfig("percentual_perdas_tec_samp", False).Replace(".", ",")

                ldblPerdasTotais_Medido = ldblTotal_Energia_Injetada - ldblTotal_Consumo_Medido_Grp_A_B
                ldblPerdasTecnicas_Medido = Math.Round(ldblPerdasTotais_Medido * (perc / 100), 2)
                ldblPerdasNaoTecnicas_Medido = Math.Round(ldblPerdasTotais_Medido * ((100 - perc) / 100), 2)

                ldblPerdasTotais_Faturado = ldblTotal_Energia_Injetada - ldblTotal_Consumo_Faturado_Grp_A_B
                ldblPerdasTecnicas_Faturado = Math.Round(ldblPerdasTotais_Faturado * (perc / 100), 2)
                ldblPerdasNaoTecnicas_Faturado = Math.Round(ldblPerdasTotais_Faturado * ((100 - perc) / 100), 2)

                'O ARQUIVO SAMP_BALANÇO_ENERGETICO ESTA COM O PADRÃO QUE O JOSE RICARDO NOS ENCAMINHOU, POR ISSO ELE ESTA CHUMBADO.
                'PARA ADICIONAR NOVOS CAMPOS QUANDO NECESSÁRIO, SERA NECESSARIO CHAMR A FUNÇÃO INSERT SAMP COM OS PARAMETROS....
                'PARA UM FUTURO TALVEZ POSSAMOS MONTAR MELHOR ISTO, JÁ QUE ESTE FOI FEITO NA PRESSA DE ULTIMA HORA


                'Disponibilidades
                Call InsertSampExportacaoBE(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), 1, GetFluxosBalancoEnergetico("D"), GetModalidadesBalancoEnergetico("EI"), GetCaracteristicasBalancoEnergetico("ENERG_INJ_TOT"), GetDetalheBalancoEnergetico("EM"), ldblTotal_Energia_Injetada)
                Call InsertSampExportacaoBE(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), 2, GetFluxosBalancoEnergetico("D"), GetModalidadesBalancoEnergetico("EI"), GetCaracteristicasBalancoEnergetico("ENER_INJ"), GetDetalheBalancoEnergetico("EM_A4"), ldblEnergia_Injetada_Enel)
                Call InsertSampExportacaoBE(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), 3, GetFluxosBalancoEnergetico("D"), GetModalidadesBalancoEnergetico("GD"), GetCaracteristicasBalancoEnergetico("ENER_IN_MICRO_MINI"), GetDetalheBalancoEnergetico("EM_A4"), ldblEnergia_Injetada_Grp_A)
                Call InsertSampExportacaoBE(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), 4, GetFluxosBalancoEnergetico("D"), GetModalidadesBalancoEnergetico("GD"), GetCaracteristicasBalancoEnergetico("ENER_IN_MICRO_MINI"), GetDetalheBalancoEnergetico("EM_BT"), ldblEnergia_Injetada_Grp_B)
                Call InsertSampExportacaoBE(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), 5, GetFluxosBalancoEnergetico("D"), GetModalidadesBalancoEnergetico("GD"), GetCaracteristicasBalancoEnergetico("ENER_IN_MICRO_MINI_TOT"), GetDetalheBalancoEnergetico("EM"), ldblSubTot_Energia_Injetada_Grp_A_B)
                Call InsertSampExportacaoBE(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), 6, GetFluxosBalancoEnergetico("D"), GetModalidadesBalancoEnergetico("ER"), GetCaracteristicasBalancoEnergetico("COMP/REP_ITA/IMP"), GetDetalheBalancoEnergetico("EM"), ldblEnergia_Injetada_Enel) 'aqui é para ser a soma de todas energias compradas

                'Requisitos
                Call InsertSampExportacaoBE(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), 7, GetFluxosBalancoEnergetico("R"), GetModalidadesBalancoEnergetico("EV"), GetCaracteristicasBalancoEnergetico("FORN_CAT_TOT"), GetDetalheBalancoEnergetico("EF"), ldblTotal_Consumo_Faturado_Grp_A_B)
                Call InsertSampExportacaoBE(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), 8, GetFluxosBalancoEnergetico("R"), GetModalidadesBalancoEnergetico("EV"), GetCaracteristicasBalancoEnergetico("FORN_CAT_TOT"), GetDetalheBalancoEnergetico("EM"), ldblTotal_Consumo_Medido_Grp_A_B)
                Call InsertSampExportacaoBE(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), 9, GetFluxosBalancoEnergetico("R"), GetModalidadesBalancoEnergetico("EV"), GetCaracteristicasBalancoEnergetico("FORN_CONS_PROP_TOT"), GetDetalheBalancoEnergetico("EF"), ldblTotal_Consumo_Proprio)
                Call InsertSampExportacaoBE(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), 10, GetFluxosBalancoEnergetico("R"), GetModalidadesBalancoEnergetico("EV"), GetCaracteristicasBalancoEnergetico("FORN_CONS_PROP_TOT"), GetDetalheBalancoEnergetico("EM"), ldblTotal_Consumo_Proprio)
                Call InsertSampExportacaoBE(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), 11, GetFluxosBalancoEnergetico("R"), GetModalidadesBalancoEnergetico("EV"), GetCaracteristicasBalancoEnergetico("FORN_CAT"), GetDetalheBalancoEnergetico("EM_A4"), ldblTotal_Consumo_Medido_Grp_A)
                Call InsertSampExportacaoBE(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), 12, GetFluxosBalancoEnergetico("R"), GetModalidadesBalancoEnergetico("EV"), GetCaracteristicasBalancoEnergetico("FORN_CAT"), GetDetalheBalancoEnergetico("EM_BT"), ldblTotal_Consumo_Medido_Grp_B)
                Call InsertSampExportacaoBE(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), 13, GetFluxosBalancoEnergetico("R"), GetModalidadesBalancoEnergetico("EV"), GetCaracteristicasBalancoEnergetico("FORN_CONS_PROP"), GetDetalheBalancoEnergetico("EM_BT"), ldblTotal_Consumo_Proprio)

                'Saldo
                Call InsertSampExportacaoBE(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), 14, GetFluxosBalancoEnergetico("S"), GetModalidadesBalancoEnergetico("PDM"), GetCaracteristicasBalancoEnergetico("PERD_N_TEC"), GetDetalheBalancoEnergetico("EC"), ldblPerdasNaoTecnicas_Medido)
                Call InsertSampExportacaoBE(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), 15, GetFluxosBalancoEnergetico("S"), GetModalidadesBalancoEnergetico("PDM"), GetCaracteristicasBalancoEnergetico("PERD_TEC"), GetDetalheBalancoEnergetico("EC"), ldblPerdasTecnicas_Medido)
                Call InsertSampExportacaoBE(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), 16, GetFluxosBalancoEnergetico("S"), GetModalidadesBalancoEnergetico("PDM"), GetCaracteristicasBalancoEnergetico("PE_TOT"), GetDetalheBalancoEnergetico("EC"), ldblPerdasTotais_Medido)
                Call InsertSampExportacaoBE(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), 17, GetFluxosBalancoEnergetico("S"), GetModalidadesBalancoEnergetico("PDF"), GetCaracteristicasBalancoEnergetico("PERD_N_TEC"), GetDetalheBalancoEnergetico("EC"), ldblPerdasNaoTecnicas_Faturado)
                Call InsertSampExportacaoBE(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), 18, GetFluxosBalancoEnergetico("S"), GetModalidadesBalancoEnergetico("PDF"), GetCaracteristicasBalancoEnergetico("PERD_TEC"), GetDetalheBalancoEnergetico("EC"), ldblPerdasTecnicas_Faturado)
                Call InsertSampExportacaoBE(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), 19, GetFluxosBalancoEnergetico("S"), GetModalidadesBalancoEnergetico("PDF"), GetCaracteristicasBalancoEnergetico("PE_TOT"), GetDetalheBalancoEnergetico("EC"), ldblPerdasTotais_Faturado)

            End If

            dt = Nothing

            iblnExporta = True
            Return True

        Catch ex As Exception
            'Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: Exporta_Grava_Balanco_Energetico")
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: Exporta_Grava_Balanco_Energetico" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End Try
    End Function

    Private Sub InsertSampExportacaoBE(ByVal Ano_Mes As Integer, ByRef Sequencia As Short, ByVal Fluxo As Short,
                                       ByVal Modalidade As Short, ByVal Caracteristica As Short, ByVal Detalhe As Short, Optional ByVal adblValor As Double = 0)
        Dim lstrSql As String
        Try
            lstrSql = ""
            lstrSql = lstrSql & "INSERT INTO `samp_exportacao_be` SET "
            lstrSql = lstrSql & " `ano_mes` = " & Ano_Mes & ", "
            lstrSql = lstrSql & " `sequencia` = " & Sequencia & ", "
            lstrSql = lstrSql & " `fluxo` = " & Fluxo & ", "
            lstrSql = lstrSql & " `modalidade` = " & Modalidade & ", "
            lstrSql = lstrSql & " `caracteristica` = " & Caracteristica & ", "
            lstrSql = lstrSql & " `detalhe` = " & Detalhe & ", "
            lstrSql = lstrSql & " `valor` = " & Replace(CStr(adblValor), ",", ".") & "; "
            objExecAtualizaBancoDados.QUERY_NAME = lstrSql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
            Sequencia = Sequencia + 1
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: InsertSampExportacaoBE" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

#End Region


    Private Sub GravaExportacao()
        Dim lintSeq As Short
        Dim lintRef As Short
        Dim lstrCla As String
        Dim lintOrd As Short
        Dim ldblQtd As Double
        Dim ldblkWh As Double
        Dim ldblVlr As Double
        Dim ldblBan As Double
        Dim ldblRec As Double
        '
        Dim ldblkWhN As Double
        Dim ldblkWhR As Double
        Dim ldblVlrN As Double
        Dim ldblVlrR As Double
        Dim ldblBanN As Double
        Dim ldblBanR As Double
        '
        Dim ldblIcm As Double
        Dim ldblPis As Double
        Dim ldblCof As Double
        'Consumos
        Dim ldblQtdkWhp As Double
        Dim ldblQtdkWhfp As Double
        Dim ldblVlrkWhp As Double
        Dim ldblVlrkWhfp As Double
        Dim ldblVlrBanp As Double
        Dim ldblVlrBanfp As Double
        Dim ldblVlrRecp As Double
        Dim ldblVlrRecfp As Double
        'Demandas
        Dim ldblQtdkWp As Double
        Dim ldblQtdkWfp As Double
        Dim ldblVlrkWp As Double
        Dim ldblVlrkWfp As Double
        Dim ldblQtdkWup As Double
        Dim ldblQtdkWufp As Double
        Dim ldblVlrkWup As Double
        Dim ldblVlrkWufp As Double
        'Reativos
        Dim ldblVlrCRep As Double
        Dim ldblVlrCRefp As Double
        Dim ldblVlrDRep As Double
        Dim ldblVlrDRefp As Double

        'Compensados
        Dim ldblQtdKwReverso As Double
        Dim ldblQtdKwReversop As Double
        Dim ldblQtdKwReversofp As Double

        Dim ldblQtdKwConsumido As Double
        Dim ldblQtdKwConsumidop As Double
        Dim ldblQtdKwConsumidofp As Double

        Dim ldblQtdkWComp As Double
        Dim ldblVlrkWComp As Double
        Dim ldblVDescPercComp As Double

        Dim ldblQtdkWComp_p As Double
        Dim ldblVlrkWComp_p As Double
        Dim ldblVDescPercComp_p As Double

        Dim ldblQtdkWComp_fp As Double
        Dim ldblVlrkWComp_fp As Double
        Dim ldblVDescPercComp_fp As Double

        Dim lintgrpGD As Integer
        '
        Dim lintSampCla As Short
        Dim lintSampMod As Short
        Dim lintSampPos As Short
        Dim lintSampSub As Short
        Dim lblnSampAux As Boolean


        Dim lblnTarifaBranca As Boolean
        Dim ldblqtarbranca_kwh_ponta As Double
        Dim ldblqtarbranca_kwh_forap As Double
        Dim ldblqtarbranca_kwh_inter As Double

        Dim ldblqtarbranca_kwh_ponta_ban As Double
        Dim ldblqtarbranca_kwh_forap_ban As Double
        Dim ldblqtarbranca_kwh_inter_ban As Double

        Dim ldblvtarbranca_kwh_ponta As Double
        Dim ldblvtarbranca_kwh_forap As Double
        Dim ldblvtarbranca_kwh_inter As Double

        Dim ldblvtarbranca_kwh_ponta_ban As Double
        Dim ldblvtarbranca_kwh_forap_ban As Double
        Dim ldblvtarbranca_kwh_inter_ban As Double

        '
        Dim lstrSql As String

        Try
            Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
            Call LigDes(False)
            iblnExporta = False

            lstrSql = ""
            lstrSql = "DELETE FROM  `samp_exportacao` WHERE `ano_mes` = " & VB6.Format(Me.dtpMesAno.Value, "yyyyMM")
            objExecAtualizaBancoDados.QUERY_NAME = lstrSql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            Me.lblInfoPre.Text = " Buscando Baixa Renda 945, aguarde...."
            Call Me.lblInfoPre.Refresh()
            lintSeq = 1
            'Grupo B Baixa Renda 945
            lstrSql = ""
            lstrSql = lstrSql & "SELECT * FROM " & istrTab945 & " ORDER BY `ord_classe` ASC"
            'Call Conectar(lstrSql, lrstAux)
            objAtualizaBancoDados.QUERY_NAME = lstrSql
            dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            If dt.Rows.Count > 0 Then
                Call BarraProgresso((Me.ProgressBarPre), (Me.lblPercPre), (Me.lblInfoPre), 0, dt.Rows.Count, " Baixa Renda 945 ")
                lintSampCla = GetSampClasses(10)
                lintSampMod = GetModalidade("B")
                lintSampPos = GetPostoTarifario("ND")
                For Each row As DataRow In dt.Rows

                    lintRef = Convert.ToInt32(IIf(IsDBNull(row.Item("refaturado")), 0, row.Item("refaturado")))
                    lstrCla = IIf(IsDBNull(row.Item("ds_classe")), "", row.Item("ds_classe"))
                    lintOrd = IIf(IsDBNull(row.Item("ord_classe")), 20, row.Item("ord_classe"))
                    ldblQtd = IIf(IsDBNull(row.Item("faixa")), 0, row.Item("faixa"))
                    ldblkWh = IIf(IsDBNull(row.Item("consumo")), 0, row.Item("consumo"))
                    ldblVlr = IIf(IsDBNull(row.Item("valor")), 0, row.Item("valor"))
                    ldblBan = IIf(IsDBNull(row.Item("bandeira")), 0, row.Item("bandeira"))
                    ldblRec = IIf(IsDBNull(row.Item("receita")), 0, row.Item("receita"))
                    ldblIcm = IIf(IsDBNull(row.Item("icms")), 0, row.Item("icms"))
                    ldblPis = IIf(IsDBNull(row.Item("pis")), 0, row.Item("pis"))
                    ldblCof = IIf(IsDBNull(row.Item("cofins")), 0, row.Item("cofins"))
                    lintgrpGD = IIf(IsDBNull(row.Item("grpGD")), 0, row.Item("grpGD"))

                    ldblQtdkWComp = IIf(IsDBNull(row.Item("qkwhCompensada")), 0, row.Item("qkwhCompensada"))
                    ldblVlrkWComp = IIf(IsDBNull(row.Item("vkwhCompensada")), 0, row.Item("vkwhCompensada"))
                    ldblVDescPercComp = IIf(IsDBNull(row.Item("vDescPercCompensada")), 0, row.Item("vDescPercCompensada"))
                    ldblQtdKwConsumido = IIf(IsDBNull(row.Item("consumo_lido")), 0, row.Item("consumo_lido"))

                    If lintgrpGD = 0 Then
                        'Quantidade Consumidores
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 2, lintSampCla, lintSampMod, GetSubGrupo("B", lintOrd, True), GetDetalheFornecimento("QC"), lintSampPos, ldblQtd, lintgrpGD)
                        'Quantidade kWh
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 2, lintSampCla, lintSampMod, GetSubGrupo("B", lintOrd, True), GetDetalheFornecimento("CF"), lintSampPos, ldblkWh, lintgrpGD)
                        'Receita kWh R$
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 2, lintSampCla, lintSampMod, GetSubGrupo("B", lintOrd, True), GetDetalheFornecimento("RC"), lintSampPos, IIf(Me.chkReceitakWhBan.CheckState = System.Windows.Forms.CheckState.Checked, ldblRec, ldblVlr), lintgrpGD)
                        'Receita Bandeira R$
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 2, lintSampCla, lintSampMod, GetSubGrupo("B", lintOrd, True), GetDetalheFornecimento("RB"), lintSampPos, ldblBan, lintgrpGD)
                        If ldblIcm > 0 Then
                            'ICMS R$
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 2, lintSampCla, lintSampMod, GetSubGrupo("B", lintOrd, True), GetDetalheFornecimento("I"), lintSampPos, ldblIcm, lintgrpGD)
                        End If
                        If ldblPis > 0 Then
                            'PIS R$
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 2, lintSampCla, lintSampMod, GetSubGrupo("B", lintOrd, True), GetDetalheFornecimento("P"), lintSampPos, ldblPis, lintgrpGD)
                        End If
                        If ldblCof > 0 Then
                            'COFINS R$
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 2, lintSampCla, lintSampMod, GetSubGrupo("B", lintOrd, True), GetDetalheFornecimento("C"), lintSampPos, ldblCof, lintgrpGD)
                        End If

                    Else

                        '03 - Consumo Faturado

                        '21 - Energia Injetada (kWh) - (antigo Energia Gerada)

                        '22 - Energia Faturada (kWh)
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 2, lintSampCla, lintSampMod, GetSubGrupo("B", lintOrd, True), GetDetalheFornecimento("EF"), lintSampPos, ldblkWh, lintgrpGD)


                        '23 - Energia debitada do SCEE (kWh)

                        '24 - Energia Consumida (kWh)
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 2, lintSampCla, lintSampMod, GetSubGrupo("B", lintOrd, True), GetDetalheFornecimento("EC"), lintSampPos, ldblQtdKwConsumido, lintgrpGD)


                        '01 - Numero de Consumidores
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 2, lintSampCla, lintSampMod, GetSubGrupo("B", lintOrd, True), GetDetalheFornecimento("QC"), lintSampPos, ldblQtd, lintgrpGD)


                        '02 - Demanda Faturada (kW)
                        '05 - Receita Demanda (R$) • Vinculado ao 2 (Demanda Faturada(kW))

                        '06 - Receita Consumo (R$) • Vinculado ao 22 (Energia Faturada (kWh))
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 2, lintSampCla, lintSampMod, GetSubGrupo("B", lintOrd, True), GetDetalheFornecimento("RC"), lintSampPos, IIf(Me.chkReceitakWhBan.CheckState = System.Windows.Forms.CheckState.Checked, ldblRec, ldblVlr), lintgrpGD)


                        '07 - Ultrapassagem Demanda (kW) • Vinculado ao 2 (Demanda Faturada(kW))
                        '08 - Receita Ultrapassagem Demanda (R$) • Vinculado ao 7 (Ultrapassagem Demanda (kW))

                        '09 - ICMS (R$)
                        If ldblIcm > 0 Then
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 2, lintSampCla, lintSampMod, GetSubGrupo("B", lintOrd, True), GetDetalheFornecimento("I"), lintSampPos, ldblIcm, lintgrpGD)
                        End If

                        '26 - Energia compensada (kWh)
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 2, lintSampCla, lintSampMod, GetSubGrupo("B", lintOrd, True), GetDetalheFornecimento("E_COMP"), lintSampPos, ldblQtdkWComp, lintgrpGD)

                        '27 - Receita energia compensada (R$)
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 2, lintSampCla, lintSampMod, GetSubGrupo("B", lintOrd, True), GetDetalheFornecimento("RC_COMP"), lintSampPos, IIf(lintgrpGD = 1, 0, ldblVlrkWComp), lintgrpGD)

                        '28 - Desconto de energia compensada %
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 2, lintSampCla, lintSampMod, GetSubGrupo("B", lintOrd, True), GetDetalheFornecimento("DESC_COMP"), lintSampPos, IIf(lintgrpGD = 1, 100, ldblVDescPercComp), lintgrpGD)

                        '20 - Desconto Consumo %

                        '19 - Desconto Demanda %

                        '25 - Receita Bandeiras (R$)
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 2, lintSampCla, lintSampMod, GetSubGrupo("B", lintOrd, True), GetDetalheFornecimento("RB"), lintSampPos, ldblBan, lintgrpGD)

                        '17 - PIS/PASEP (R$)
                        If ldblPis > 0 Then
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 2, lintSampCla, lintSampMod, GetSubGrupo("B", lintOrd, True), GetDetalheFornecimento("P"), lintSampPos, ldblPis, lintgrpGD)
                        End If

                        '18 - COFINS (R$)
                        If ldblCof > 0 Then
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 2, lintSampCla, lintSampMod, GetSubGrupo("B", lintOrd, True), GetDetalheFornecimento("C"), lintSampPos, ldblCof, lintgrpGD)
                        End If

                    End If

                    Call BarraProgresso((Me.ProgressBarPre), (Me.lblPercPre), (Me.lblInfoPre), 1,  , " Baixa Renda 945 ")
                Next
            End If

            dt = Nothing

            'Grupo B normal
            lstrSql = ""
            lstrSql = lstrSql & "   SELECT "
            lstrSql = lstrSql & "   `ord_classe`," & vbCrLf
            lstrSql = lstrSql & "   `ds_classe`," & vbCrLf
            lstrSql = lstrSql & "   `refaturado`," & vbCrLf
            lstrSql = lstrSql & "   `tarifa_branca`," & vbCrLf
            lstrSql = lstrSql & "   `irrigacao_aquicultura`," & vbCrLf

            lstrSql = lstrSql & "   SUM(IF(`qkwh_fat` < 0, 0, 1)) `ncons`," & vbCrLf
            lstrSql = lstrSql & "   SUM(`qkwh_fat`) `qkwh_fat`," & vbCrLf
            lstrSql = lstrSql & "   SUM(`vkwh_fat`) `vkwh_fat`," & vbCrLf
            lstrSql = lstrSql & "   SUM(`vkwh_fat_ban`) `vkwh_fat_ban`," & vbCrLf
            lstrSql = lstrSql & "   SUM(`receita_fp`) `vreceita`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(`qkwh_reverso`) `qkwh_reverso`," & vbCrLf
            lstrSql = lstrSql & "   SUM(`vkwh_reverso`) `vkwh_reverso`," & vbCrLf
            '
            lstrSql = lstrSql & "   SUM(qirr_nor_kwh_fat) `qkwh_n_fat`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(qirr_res_kwh_fat) `qkwh_r_fat`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(virr_nor_kwh_fat) `vkwh_n_fat`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(virr_nor_kwh_fat_ban) `vkwh_n_fat_ban`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(virr_res_kwh_fat) `vkwh_r_fat`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(virr_res_kwh_fat_ban) `vkwh_r_fat_ban`, " & vbCrLf

            'Tarifa Branca
            lstrSql = lstrSql & "   SUM(qtarbranca_kwh_ponta) `qtarbranca_kwh_ponta`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(qtarbranca_kwh_forap) `qtarbranca_kwh_forap`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(qtarbranca_kwh_inter) `qtarbranca_kwh_inter`, " & vbCrLf

            lstrSql = lstrSql & "   SUM(qtarbranca_kwh_ponta_ban) `qtarbranca_kwh_ponta_ban`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(qtarbranca_kwh_forap_ban) `qtarbranca_kwh_forap_ban`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(qtarbranca_kwh_inter_ban) `qtarbranca_kwh_inter_ban`, " & vbCrLf

            'Tarifa Branca
            lstrSql = lstrSql & "   SUM(vtarbranca_kwh_ponta) `vtarbranca_kwh_ponta`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(vtarbranca_kwh_forap) `vtarbranca_kwh_forap`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(vtarbranca_kwh_inter) `vtarbranca_kwh_inter`, " & vbCrLf

            lstrSql = lstrSql & "   SUM(vtarbranca_kwh_ponta_ban) `vtarbranca_kwh_ponta_ban`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(vtarbranca_kwh_forap_ban) `vtarbranca_kwh_forap_ban`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(vtarbranca_kwh_inter_ban) `vtarbranca_kwh_inter_ban`, " & vbCrLf

            '
            lstrSql = lstrSql & "   SUM(qkwh_cons) `qkwh_cons`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(qkwhCompensada) `qkwh_comp`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(vkwhCompensada) `vkwh_comp`, " & vbCrLf
            lstrSql = lstrSql & "   `vDescPercCompensada`, " & vbCrLf
            lstrSql = lstrSql & "   `grpGD`, " & vbCrLf
            '
            lstrSql = lstrSql & "   SUM(`icms`) `icms`," & vbCrLf
            lstrSql = lstrSql & "   SUM(`pis`) `pis`," & vbCrLf
            lstrSql = lstrSql & "   SUM(`cofins`) `cofins`" & vbCrLf
            lstrSql = lstrSql & "   FROM " & istrTabela & " " & vbCrLf
            lstrSql = lstrSql & "   WHERE `usinas` = 0 " & vbCrLf
            lstrSql = lstrSql & "   AND `grupo` = 'B' " & vbCrLf
            lstrSql = lstrSql & "   GROUP BY `ord_classe`, `ds_classe`, `refaturado`, `tarifa_branca`, `irrigacao_aquicultura`, `grpGD` " & vbCrLf
            lstrSql = lstrSql & "   ORDER BY 1 ASC, 2 ASC, 3 ASC  " & vbCrLf
            ' Call Conectar(lstrSql, lrstAux)
            objAtualizaBancoDados.QUERY_NAME = lstrSql
            dt1 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            If dt1.Rows.Count > 0 Then

                Call BarraProgresso((Me.ProgressBarPre), (Me.lblPercPre), (Me.lblInfoPre), 0, dt1.Rows.Count, " Grupo B ")
                For Each row1 As DataRow In dt1.Rows

                    lintRef = Convert.ToInt32(IIf(IsDBNull(row1.Item("refaturado")), 0, row1.Item("refaturado")))
                    lstrCla = IIf(IsDBNull(row1.Item("ds_classe")), "", row1.Item("ds_classe"))
                    lintOrd = IIf(IsDBNull(row1.Item("ord_classe")), 20, row1.Item("ord_classe"))
                    ldblQtd = IIf(IsDBNull(row1.Item("ncons")), 0, row1.Item("ncons"))
                    ldblkWh = IIf(IsDBNull(row1.Item("qkwh_fat")), 0, row1.Item("qkwh_fat"))
                    ldblVlr = IIf(IsDBNull(row1.Item("vkwh_fat")), 0, row1.Item("vkwh_fat"))
                    ldblBan = IIf(IsDBNull(row1.Item("vkwh_fat_ban")), 0, row1.Item("vkwh_fat_ban"))
                    ldblRec = IIf(IsDBNull(row1.Item("vreceita")), 0, row1.Item("vreceita"))
                    ldblkWhN = IIf(IsDBNull(row1.Item("qkwh_n_fat")), 0, row1.Item("qkwh_n_fat"))
                    ldblkWhR = IIf(IsDBNull(row1.Item("qkwh_r_fat")), 0, row1.Item("qkwh_r_fat"))
                    ldblVlrN = IIf(IsDBNull(row1.Item("vkwh_n_fat")), 0, row1.Item("vkwh_n_fat"))
                    ldblVlrR = IIf(IsDBNull(row1.Item("vkwh_r_fat")), 0, row1.Item("vkwh_r_fat"))
                    ldblBanN = IIf(IsDBNull(row1.Item("vkwh_n_fat_ban")), 0, row1.Item("vkwh_n_fat_ban"))
                    ldblBanR = IIf(IsDBNull(row1.Item("vkwh_r_fat_ban")), 0, row1.Item("vkwh_r_fat_ban"))
                    ldblIcm = IIf(IsDBNull(row1.Item("icms")), 0, row1.Item("icms"))
                    ldblPis = IIf(IsDBNull(row1.Item("pis")), 0, row1.Item("pis"))
                    ldblCof = IIf(IsDBNull(row1.Item("cofins")), 0, row1.Item("cofins"))

                    ldblQtdKwReverso = IIf(IsDBNull(row1.Item("qkwh_reverso")), 0, row1.Item("qkwh_reverso"))
                    ldblQtdKwConsumido = IIf(IsDBNull(row1.Item("qkwh_cons")), 0, row1.Item("qkwh_cons"))
                    ldblQtdkWComp = IIf(IsDBNull(row1.Item("qkwh_comp")), 0, row1.Item("qkwh_comp"))
                    ldblVlrkWComp = IIf(IsDBNull(row1.Item("vkwh_comp")), 0, row1.Item("vkwh_comp"))
                    ldblVDescPercComp = IIf(IsDBNull(row1.Item("vDescPercCompensada")), 0, row1.Item("vDescPercCompensada"))
                    lintgrpGD = IIf(IsDBNull(row1.Item("grpGD")), 0, row1.Item("grpGD"))
                    lblnTarifaBranca = IIf(IsDBNull(row1.Item("tarifa_branca")), 0, row1.Item("tarifa_branca"))


                    ldblqtarbranca_kwh_ponta = IIf(IsDBNull(row1.Item("qtarbranca_kwh_ponta")), 0, row1.Item("qtarbranca_kwh_ponta"))
                    ldblqtarbranca_kwh_forap = IIf(IsDBNull(row1.Item("qtarbranca_kwh_forap")), 0, row1.Item("qtarbranca_kwh_forap"))
                    ldblqtarbranca_kwh_inter = IIf(IsDBNull(row1.Item("qtarbranca_kwh_inter")), 0, row1.Item("qtarbranca_kwh_inter"))

                    ldblqtarbranca_kwh_ponta_ban = IIf(IsDBNull(row1.Item("qtarbranca_kwh_ponta_ban")), 0, row1.Item("qtarbranca_kwh_ponta_ban"))
                    ldblqtarbranca_kwh_forap_ban = IIf(IsDBNull(row1.Item("qtarbranca_kwh_forap_ban")), 0, row1.Item("qtarbranca_kwh_forap_ban"))
                    ldblqtarbranca_kwh_inter_ban = IIf(IsDBNull(row1.Item("qtarbranca_kwh_inter_ban")), 0, row1.Item("qtarbranca_kwh_inter_ban"))

                    ldblvtarbranca_kwh_ponta = IIf(IsDBNull(row1.Item("vtarbranca_kwh_ponta")), 0, row1.Item("vtarbranca_kwh_ponta"))
                    ldblvtarbranca_kwh_forap = IIf(IsDBNull(row1.Item("vtarbranca_kwh_forap")), 0, row1.Item("vtarbranca_kwh_forap"))
                    ldblvtarbranca_kwh_inter = IIf(IsDBNull(row1.Item("vtarbranca_kwh_inter")), 0, row1.Item("vtarbranca_kwh_inter"))

                    ldblvtarbranca_kwh_ponta_ban = IIf(IsDBNull(row1.Item("vtarbranca_kwh_ponta_ban")), 0, row1.Item("vtarbranca_kwh_ponta_ban"))
                    ldblvtarbranca_kwh_forap_ban = IIf(IsDBNull(row1.Item("vtarbranca_kwh_forap_ban")), 0, row1.Item("vtarbranca_kwh_forap_ban"))
                    ldblvtarbranca_kwh_inter_ban = IIf(IsDBNull(row1.Item("vtarbranca_kwh_inter_ban")), 0, row1.Item("vtarbranca_kwh_inter_ban"))

                    lintSampMod = GetModalidade(If(Not lblnTarifaBranca, "B", "BB"))
                    lintSampPos = GetPostoTarifario("ND")

                    lblnSampAux = True
                    lintSampCla = GetSampClasses(lintOrd)
                    lintSampSub = GetSubGrupo("B", lintOrd, False)
                    If lintSampCla = 1 And InStr(1, "02,03,41,04,05,06,47", VB6.Format(lintSampSub, "00")) > 0 Then
                        lblnSampAux = False
                    Else
                        lblnSampAux = True
                    End If

                    If lblnSampAux Then

                        If lintgrpGD = 0 Then

                            'Quantidade Consumidores
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("QC"), lintSampPos, ldblQtd, lintgrpGD)

                            If lintSampCla = 9 Or lintSampCla = 10 Then
                                'Quantidade kWh - Período Normal
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("CF"), GetPostoTarifario("N"), ldblkWhN, lintgrpGD)
                                'Receita kWh R$ - Período Normal
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("RC"), GetPostoTarifario("N"), IIf(Me.chkReceitakWhBan.CheckState = System.Windows.Forms.CheckState.Checked, (ldblVlrN + ldblBanN), ldblVlrN), lintgrpGD)
                                'Quantidade kWh - Período Reservado
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("CF"), GetPostoTarifario("R"), ldblkWhR, lintgrpGD)
                                'Receita kWh R$ - Período Reservado
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("RC"), GetPostoTarifario("R"), IIf(Me.chkReceitakWhBan.CheckState = System.Windows.Forms.CheckState.Checked, (ldblVlrR + ldblBanR), ldblVlrR), lintgrpGD)
                                'Receita Bandeira R$
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("RB"), lintSampPos, ldblBanN + ldblBanR, lintgrpGD)
                            Else

                                'If lblnTarifaBranca Then
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("CF"), GetPostoTarifario("P"), ldblqtarbranca_kwh_ponta, lintgrpGD)      'Quantidade kWh - Ponta
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("RC"), GetPostoTarifario("P"), ldblvtarbranca_kwh_ponta, lintgrpGD)      'Receita kWh R$ - Ponta
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("RB"), GetPostoTarifario("P"), ldblvtarbranca_kwh_ponta_ban, lintgrpGD)  'Receita Bandeira R$ - Ponta

                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("CF"), GetPostoTarifario("FP"), ldblqtarbranca_kwh_forap, lintgrpGD)     'Quantidade kWh - Fora Ponta
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("RC"), GetPostoTarifario("FP"), ldblvtarbranca_kwh_forap, lintgrpGD)     'Receita kWh R$ - Fora Ponta
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("RB"), GetPostoTarifario("FP"), ldblvtarbranca_kwh_forap_ban, lintgrpGD) 'Receita Bandeira R$ - Fora Ponta

                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("CF"), GetPostoTarifario("I"), ldblqtarbranca_kwh_inter, lintgrpGD)      'Quantidade kWh - Intermediario
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("RC"), GetPostoTarifario("I"), ldblvtarbranca_kwh_inter, lintgrpGD)      'Receita kWh R$ - Intermediario
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("RB"), GetPostoTarifario("I"), ldblvtarbranca_kwh_inter_ban, lintgrpGD)  'Receita Bandeira R$ - Intermediario
                                'Else
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("CF"), lintSampPos, ldblkWh, lintgrpGD) 'Quantidade kWh
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("RC"), lintSampPos, IIf(Me.chkReceitakWhBan.CheckState = System.Windows.Forms.CheckState.Checked, ldblRec, ldblVlr), lintgrpGD) 'Receita kWh R$
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("RB"), lintSampPos, ldblBan, lintgrpGD) 'Receita Bandeira R$
                                'End If

                            End If

                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("I"), lintSampPos, ldblIcm, lintgrpGD) ' -- ICMS R$
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("P"), lintSampPos, ldblPis, lintgrpGD) ' -- PIS R$
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("C"), lintSampPos, ldblCof, lintgrpGD) ' -- COFINS R$

                        Else


                            '01 - Numero de Consumidores
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("QC"), lintSampPos, ldblQtd, lintgrpGD)

                            '02 - Demanda Faturada (kW)
                            '03 - Consumo Faturado
                            '05 - Receita Demanda (R$) • Vinculado ao 2 (Demanda Faturada(kW))

                            '06 - Receita Consumo (R$) • Vinculado ao 22 (Energia Faturada (kWh))
                            If lintSampCla = 9 Or lintSampCla = 10 Then
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("RC"), GetPostoTarifario("N"), IIf(Me.chkReceitakWhBan.CheckState = System.Windows.Forms.CheckState.Checked, (ldblVlrN + ldblBanN), ldblVlrN), lintgrpGD)
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("RC"), GetPostoTarifario("R"), IIf(Me.chkReceitakWhBan.CheckState = System.Windows.Forms.CheckState.Checked, (ldblVlrR + ldblBanR), ldblVlrR), lintgrpGD)
                            Else
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("RC"), lintSampPos, IIf(Me.chkReceitakWhBan.CheckState = System.Windows.Forms.CheckState.Checked, ldblRec, ldblVlr), lintgrpGD)
                            End If

                            '07 - Ultrapassagem Demanda (kW) • Vinculado ao 2 (Demanda Faturada(kW))
                            '08 - Receita Ultrapassagem Demanda (R$) • Vinculado ao 7 (Ultrapassagem Demanda (kW))

                            '09 - ICMS (R$)
                            If ldblIcm > 0 Then
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("I"), lintSampPos, ldblIcm, lintgrpGD)
                            End If

                            '17 - PIS/PASEP (R$)
                            If ldblPis > 0 Then
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("P"), lintSampPos, ldblPis, lintgrpGD)
                            End If

                            '18 - COFINS (R$)
                            If ldblCof > 0 Then
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("C"), lintSampPos, ldblCof, lintgrpGD)
                            End If

                            '19 - Desconto Demanda %

                            '20 - Desconto Consumo %

                            '21 - Energia Injetada (kWh) - (antigo Energia Gerada)
                            If lintSampCla = 9 Or lintSampCla = 10 Then
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("EG"), GetPostoTarifario("N"), ldblQtdKwReverso, lintgrpGD)
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("EG"), GetPostoTarifario("R"), ldblQtdKwReverso, lintgrpGD)
                            Else
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("EG"), lintSampPos, ldblQtdKwReverso, lintgrpGD)
                            End If

                            '22 - Energia Faturada (kWh)
                            If lintSampCla = 9 Or lintSampCla = 10 Then
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("EF"), GetPostoTarifario("N"), ldblkWhN, lintgrpGD)
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("EF"), GetPostoTarifario("R"), ldblkWhR, lintgrpGD)
                            Else
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("EF"), lintSampPos, ldblkWh, lintgrpGD)
                            End If

                            '23 - Energia debitada do SCEE (kWh)
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("ENERG_DEB_SCEE"), lintSampPos, 0, lintgrpGD)

                            '24 - Energia Consumida (kWh)
                            If lintSampCla = 9 Or lintSampCla = 10 Then
                                'Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("EC"), GetPostoTarifario("N"), ldblQtdKwConsumido, lintgrpGD)
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("EC"), GetPostoTarifario("R"), ldblQtdKwConsumido, lintgrpGD)
                            Else
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("EC"), lintSampPos, ldblQtdKwConsumido, lintgrpGD)
                            End If


                            '25 - Receita Bandeiras (R$)
                            If lintSampCla = 9 Or lintSampCla = 10 Then
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("RB"), lintSampPos, ldblBanN + ldblBanR, lintgrpGD)
                            Else
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("RB"), lintSampPos, ldblBan, lintgrpGD)
                            End If



                            '26 - Energia compensada (kWh)
                            If lintSampCla = 9 Or lintSampCla = 10 Then
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("E_COMP"), GetPostoTarifario("N"), ldblQtdkWComp, lintgrpGD)
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("E_COMP"), GetPostoTarifario("R"), ldblQtdkWComp, lintgrpGD)
                            Else
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("E_COMP"), lintSampPos, ldblQtdkWComp, lintgrpGD)
                            End If


                            '27 - Receita energia compensada (R$)
                            If lintSampCla = 9 Or lintSampCla = 10 Then
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("RC_COMP"), GetPostoTarifario("N"), IIf(lintgrpGD = 1, 0, ldblVlrkWComp), lintgrpGD)
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("RC_COMP"), GetPostoTarifario("R"), IIf(lintgrpGD = 1, 0, ldblVlrkWComp), lintgrpGD)
                            Else
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("RC_COMP"), lintSampPos, IIf(lintgrpGD = 1, 0, ldblVlrkWComp), lintgrpGD)
                            End If

                            '28 - Desconto de energia compensada %
                            If lintSampCla = 9 Or lintSampCla = 10 Then
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("DESC_COMP"), GetPostoTarifario("N"), IIf(lintgrpGD = 1, 100, ldblVDescPercComp), lintgrpGD)
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("DESC_COMP"), GetPostoTarifario("R"), IIf(lintgrpGD = 1, 100, ldblVDescPercComp), lintgrpGD)
                            Else
                                Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, lintSampCla, lintSampMod, lintSampSub, GetDetalheFornecimento("DESC_COMP"), lintSampPos, IIf(lintgrpGD = 1, 100, ldblVDescPercComp), lintgrpGD)
                            End If

                        End If

                    End If

                    Call BarraProgresso((Me.ProgressBarPre), (Me.lblPercPre), (Me.lblInfoPre), 1,  , " Grupo B ")
                Next
            End If

            dt1 = Nothing

            'Grupo A Horosazonal
            lstrSql = ""
            lstrSql = lstrSql & "  Select " & vbCrLf
            lstrSql = lstrSql & "  `ord_classe`,  " & vbCrLf
            lstrSql = lstrSql & "  `ds_classe`, " & vbCrLf
            lstrSql = lstrSql & "  `refaturado`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(If((qkwh_fat_p+qkwh_fat_fp+qkw_fat_p+qkw_fat_fp)<0,0,1)) `ncons`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(qkwh_fat_p) `qkwh_fat_p`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(qkwh_fat_fp) `qkwh_fat_fp`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(vkwh_fat_p) `vkwh_fat_p`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(vkwh_fat_fp) `vkwh_fat_fp`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(vkwh_fat_p_ban) `vkwh_fat_p_ban`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(vkwh_fat_fp_ban) `vkwh_fat_fp_ban`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(qkw_fat_p) `qkw_fat_p`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(qkw_fat_fp) `qkw_fat_fp`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(vkw_fat_p) `vkw_fat_p`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(vkw_fat_fp) `vkw_fat_fp`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(qkw_fatu_p) `qkw_fatu_p`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(qkw_fatu_fp) `qkw_fatu_fp`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(vkw_fatu_p) `vkw_fatu_p`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(vkw_fatu_fp) `vkw_fatu_fp`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(vconreat_p) `vconreat_p`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(vconreat_fp) `vconreat_fp`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(vdemreat_p) `vdemreat_p`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(vdemreat_fp) `vdemreat_fp`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(`receita_p`) `vreceita_p`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(`receita_fp`) `vreceita_fp`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(icms) `icms`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(pis) `pis`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(cofins) `cofins`, " & vbCrLf


            lstrSql = lstrSql & "   SUM(qkwh_cons_p) `qkwh_cons_p`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(qkwh_cons_fp) `qkwh_cons_fp`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(`qkwh_reverso_p`) `qkwh_reverso_p`," & vbCrLf
            lstrSql = lstrSql & "   SUM(`qkwh_reverso_fp`) `qkwh_reverso_fp`," & vbCrLf

            lstrSql = lstrSql & "   SUM(qkwhCompensada_p) `qkwh_comp_p`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(qkwhCompensada_fp) `qkwh_comp_fp`, " & vbCrLf

            lstrSql = lstrSql & "   SUM(vkwhCompensada_p) `vkwh_comp_p`, " & vbCrLf
            lstrSql = lstrSql & "   SUM(vkwhCompensada_fp) `vkwh_comp_fp`, " & vbCrLf

            lstrSql = lstrSql & "   `vDescPercCompensada_p`, " & vbCrLf
            lstrSql = lstrSql & "   `vDescPercCompensada_fp`, " & vbCrLf

            lstrSql = lstrSql & "   `grpGD` " & vbCrLf


            lstrSql = lstrSql & "   FROM " & istrTabela & " " & vbCrLf
            lstrSql = lstrSql & "   WHERE `grupo` In( 'AA','AV','AX') " & vbCrLf
            lstrSql = lstrSql & "   GROUP BY `ord_classe`, `ds_classe`, `refaturado`, `grpGD` " & vbCrLf
            lstrSql = lstrSql & "   ORDER BY  `ord_classe` ASC, `ds_classe` ASC, `refaturado` ASC, `tarifa_branca`, `irrigacao_aquicultura`, `grpGD` "
            'Call Conectar(lstrSql, lrstAux)
            objAtualizaBancoDados.QUERY_NAME = lstrSql
            dt2 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            If dt2.Rows.Count > 0 Then
                'lintSampMod = GetModalidade("AV")
                Call BarraProgresso((Me.ProgressBarPre), (Me.lblPercPre), (Me.lblInfoPre), 0, dt2.Rows.Count, " Grupo A Horosazonal ")
                For Each row2 As DataRow In dt2.Rows

                    lintRef = Convert.ToInt32(IIf(IsDBNull(row2.Item("refaturado")), 0, row2.Item("refaturado")))
                    lstrCla = CStr(IIf(IsDBNull(row2.Item("ds_classe")), "", row2.Item("ds_classe")))
                    lintOrd = CShort(IIf(IsDBNull(row2.Item("ord_classe")), 0, row2.Item("ord_classe")))
                    ldblQtd = CDbl(IIf(IsDBNull(row2.Item("ncons")), 0, row2.Item("ncons")))
                    ldblQtdkWhp = CDbl(IIf(IsDBNull(row2.Item("qkwh_fat_p")), 0, row2.Item("qkwh_fat_p")))
                    ldblQtdkWhfp = CDbl(IIf(IsDBNull(row2.Item("qkwh_fat_fp")), 0, row2.Item("qkwh_fat_fp")))
                    ldblVlrkWhp = CDbl(IIf(IsDBNull(row2.Item("vkwh_fat_p")), 0, row2.Item("vkwh_fat_p")))
                    ldblVlrkWhfp = CDbl(IIf(IsDBNull(row2.Item("vkwh_fat_fp")), 0, row2.Item("vkwh_fat_fp")))
                    ldblVlrBanp = CDbl(IIf(IsDBNull(row2.Item("vkwh_fat_p_ban")), 0, row2.Item("vkwh_fat_p_ban")))
                    ldblVlrBanfp = CDbl(IIf(IsDBNull(row2.Item("vkwh_fat_fp_ban")), 0, row2.Item("vkwh_fat_fp_ban")))
                    ldblQtdkWp = CDbl(IIf(IsDBNull(row2.Item("qkw_fat_p")), 0, row2.Item("qkw_fat_p")))
                    ldblQtdkWfp = CDbl(IIf(IsDBNull(row2.Item("qkw_fat_fp")), 0, row2.Item("qkw_fat_fp")))
                    ldblVlrkWp = CDbl(IIf(IsDBNull(row2.Item("vkw_fat_p")), 0, row2.Item("vkw_fat_p")))
                    ldblVlrkWfp = CDbl(IIf(IsDBNull(row2.Item("vkw_fat_fp")), 0, row2.Item("vkw_fat_fp")))
                    ldblQtdkWup = CDbl(IIf(IsDBNull(row2.Item("qkw_fatu_p")), 0, row2.Item("qkw_fatu_p")))
                    ldblQtdkWufp = CDbl(IIf(IsDBNull(row2.Item("qkw_fatu_fp")), 0, row2.Item("qkw_fatu_fp")))
                    ldblVlrkWup = CDbl(IIf(IsDBNull(row2.Item("vkw_fatu_p")), 0, row2.Item("vkw_fatu_p")))
                    ldblVlrkWufp = CDbl(IIf(IsDBNull(row2.Item("vkw_fatu_fp")), 0, row2.Item("vkw_fatu_fp")))
                    ldblVlrCRep = CDbl(IIf(IsDBNull(row2.Item("vconreat_p")), 0, row2.Item("vconreat_p")))
                    ldblVlrCRefp = CDbl(IIf(IsDBNull(row2.Item("vconreat_fp")), 0, row2.Item("vconreat_fp")))
                    ldblVlrDRep = CDbl(IIf(IsDBNull(row2.Item("vdemreat_p")), 0, row2.Item("vdemreat_p")))
                    ldblVlrDRefp = CDbl(IIf(IsDBNull(row2.Item("vdemreat_fp")), 0, row2.Item("vdemreat_fp")))
                    ldblVlrRecp = CDbl(IIf(IsDBNull(row2.Item("vreceita_p")), 0, row2.Item("vreceita_p")))
                    ldblVlrRecfp = CDbl(IIf(IsDBNull(row2.Item("vreceita_fp")), 0, row2.Item("vreceita_fp")))
                    ldblIcm = CDbl(IIf(IsDBNull(row2.Item("icms")), 0, row2.Item("icms")))
                    ldblPis = CDbl(IIf(IsDBNull(row2.Item("pis")), 0, row2.Item("pis")))
                    ldblCof = CDbl(IIf(IsDBNull(row2.Item("cofins")), 0, row2.Item("cofins")))


                    ldblQtdKwReversop = IIf(IsDBNull(row2.Item("qkwh_reverso_p")), 0, row2.Item("qkwh_reverso_p"))
                    ldblQtdKwReversofp = IIf(IsDBNull(row2.Item("qkwh_reverso_fp")), 0, row2.Item("qkwh_reverso_fp"))

                    ldblQtdKwConsumidop = IIf(IsDBNull(row2.Item("qkwh_cons_p")), 0, row2.Item("qkwh_cons_p"))
                    ldblQtdKwConsumidofp = IIf(IsDBNull(row2.Item("qkwh_cons_fp")), 0, row2.Item("qkwh_cons_fp"))

                    ldblQtdkWComp_p = IIf(IsDBNull(row2.Item("qkwh_comp_p")), 0, row2.Item("qkwh_comp_p"))
                    ldblQtdkWComp_fp = IIf(IsDBNull(row2.Item("qkwh_comp_fp")), 0, row2.Item("qkwh_comp_fp"))

                    ldblVlrkWComp_p = IIf(IsDBNull(row2.Item("vkwh_comp_p")), 0, row2.Item("vkwh_comp_p"))
                    ldblVlrkWComp_fp = IIf(IsDBNull(row2.Item("vkwh_comp_fp")), 0, row2.Item("vkwh_comp_fp"))

                    ldblVDescPercComp_p = IIf(IsDBNull(row2.Item("vDescPercCompensada_p")), 0, row2.Item("vDescPercCompensada_p"))
                    ldblVDescPercComp_fp = IIf(IsDBNull(row2.Item("vDescPercCompensada_fp")), 0, row2.Item("vDescPercCompensada_fp"))

                    lintgrpGD = IIf(IsDBNull(row2.Item("grpGD")), 0, row2.Item("grpGD"))

                    If lstrCla.Contains("AZUL") Then
                        lintSampMod = GetModalidade("AA")
                    Else
                        lintSampMod = GetModalidade("AV")
                    End If


                    If lintgrpGD = 0 Then
                        'Quantidade Consumidores
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("QC"), GetPostoTarifario("ND"), ldblQtd, lintgrpGD)
                        'Quantidade kWh ponta
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("CF"), GetPostoTarifario("P"), ldblQtdkWhp, lintgrpGD)
                        'Quantidade kWh fora ponta
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("CF"), GetPostoTarifario("FP"), ldblQtdkWhfp, lintgrpGD)
                        'Receita kWh R$ ponta
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("RC"), GetPostoTarifario("P"), IIf(Me.chkReceitakWhBan.CheckState = System.Windows.Forms.CheckState.Checked, ldblVlrRecp, ldblVlrkWhp), lintgrpGD)
                        'Receita kWh R$ fora ponta
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("RC"), GetPostoTarifario("FP"), IIf(Me.chkReceitakWhBan.CheckState = System.Windows.Forms.CheckState.Checked, ldblVlrRecfp, ldblVlrkWhfp), lintgrpGD)
                        'Receita Bandeira R$ ponta
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("RB"), GetPostoTarifario("P"), ldblVlrBanp, lintgrpGD)
                        'Receita Bandeira R$ ponta
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("RB"), GetPostoTarifario("FP"), ldblVlrBanfp, lintgrpGD)

                        If ldblQtdkWp > 0 Then
                            'Quantidade kW ponta
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("DF"), GetPostoTarifario("P"), ldblQtdkWp, lintgrpGD)
                        End If
                        If ldblQtdkWfp > 0 Then
                            'Quantidade kW fora ponta
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("DF"), GetPostoTarifario(IIf(lintSampMod = 5, "ND", "FP")), ldblQtdkWfp, lintgrpGD)
                        End If

                        If ldblVlrkWp > 0 Then
                            'Receita kW R$ ponta
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("RD"), GetPostoTarifario("P"), ldblVlrkWp, lintgrpGD)
                        End If
                        If ldblVlrkWfp > 0 Then
                            'Receita kW R$ fora ponta
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("RD"), GetPostoTarifario(IIf(lintSampMod = 5, "ND", "FP")), ldblVlrkWfp, lintgrpGD)
                        End If

                        If ldblQtdkWup > 0 Then
                            'Quantidade kW ponta
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("UD"), GetPostoTarifario("P"), ldblQtdkWup, lintgrpGD)
                        End If
                        If ldblQtdkWufp > 0 Then
                            'Quantidade kW fora ponta
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("UD"), GetPostoTarifario(IIf(lintSampMod = 5, "ND", "FP")), ldblQtdkWufp, lintgrpGD)
                        End If
                        If ldblVlrkWup > 0 Then
                            'Receita kW R$ ponta
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("RU"), GetPostoTarifario("P"), ldblVlrkWup, lintgrpGD)
                        End If
                        If ldblVlrkWufp > 0 Then
                            'Receita kW R$ fora ponta
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("RU"), GetPostoTarifario(IIf(lintSampMod = 5, "ND", "FP")), ldblVlrkWufp, lintgrpGD)
                        End If
                        If ldblIcm > 0 Then
                            'ICMS R$
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("I"), lintSampPos, ldblIcm, lintgrpGD)
                        End If
                        If ldblPis > 0 Then
                            'PIS R$
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("P"), lintSampPos, ldblPis, lintgrpGD)
                        End If
                        If ldblCof > 0 Then
                            'COFINS R$
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("C"), lintSampPos, ldblCof, lintgrpGD)
                        End If

                    Else

                        '03 - Consumo Faturado **

                        '21 - Energia Injetada (kWh) - (antigo Energia Gerada)
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("EG"), GetPostoTarifario("P"), ldblQtdKwReversop, lintgrpGD)
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("EG"), GetPostoTarifario("FP"), ldblQtdKwReversofp, lintgrpGD)
                        '22 - Energia Faturada (kWh)
                        If lintSampCla = 9 Then

                        Else
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("EF"), GetPostoTarifario("P"), ldblQtdkWhp, lintgrpGD)
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("EF"), GetPostoTarifario("FP"), ldblQtdkWhfp, lintgrpGD)
                        End If

                        '23 - Energia debitada do SCEE (kWh)
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("ENERG_DEB_SCEE"), GetPostoTarifario("P"), 0, lintgrpGD)
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("ENERG_DEB_SCEE"), GetPostoTarifario("FP"), 0, lintgrpGD)

                        '24 - Energia Consumida (kWh)
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("EC"), GetPostoTarifario("P"), ldblQtdKwConsumidop, lintgrpGD)
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("EC"), GetPostoTarifario("FP"), ldblQtdKwConsumidofp, lintgrpGD)

                        '01 - Numero de Consumidores
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("QC"), GetPostoTarifario("ND"), ldblQtd, lintgrpGD)

                        '02 - Demanda Faturada (kW)
                        If ldblQtdkWp > 0 Then
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("DF"), GetPostoTarifario("P"), ldblQtdkWp, lintgrpGD)
                        End If
                        If ldblQtdkWfp > 0 Then
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("DF"), GetPostoTarifario(IIf(lintSampMod = 5, "ND", "FP")), ldblQtdkWfp, lintgrpGD)
                        End If

                        '05 - Receita Demanda (R$) • Vinculado ao 2 (Demanda Faturada(kW))
                        If ldblVlrkWp > 0 Then
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("RD"), GetPostoTarifario("P"), ldblVlrkWp, lintgrpGD)
                        End If
                        If ldblVlrkWfp > 0 Then
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("RD"), GetPostoTarifario(IIf(lintSampMod = 5, "ND", "FP")), ldblVlrkWfp, lintgrpGD)
                        End If

                        '06 - Receita Consumo (R$) • Vinculado ao 22 (Energia Faturada (kWh))
                        If lintSampCla = 9 Then
                        Else
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("RC"), GetPostoTarifario("P"), IIf(Me.chkReceitakWhBan.CheckState = System.Windows.Forms.CheckState.Checked, ldblVlrRecp, ldblVlrkWhp), lintgrpGD)
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("RC"), GetPostoTarifario("FP"), IIf(Me.chkReceitakWhBan.CheckState = System.Windows.Forms.CheckState.Checked, ldblVlrRecfp, ldblVlrkWhfp), lintgrpGD)
                        End If

                        '07 - Ultrapassagem Demanda (kW) • Vinculado ao 2 (Demanda Faturada(kW))
                        If ldblQtdkWup > 0 Then
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("UD"), GetPostoTarifario("P"), ldblQtdkWup, lintgrpGD)
                        End If
                        If ldblQtdkWufp > 0 Then
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("UD"), GetPostoTarifario(IIf(lintSampMod = 5, "ND", "FP")), ldblQtdkWufp, lintgrpGD)
                        End If

                        '08 - Receita Ultrapassagem Demanda (R$) • Vinculado ao 7 (Ultrapassagem Demanda (kW))
                        If ldblVlrkWup > 0 Then
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("RU"), GetPostoTarifario("P"), ldblVlrkWup, lintgrpGD)
                        End If
                        If ldblVlrkWufp > 0 Then
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("RU"), GetPostoTarifario(IIf(lintSampMod = 5, "ND", "FP")), ldblVlrkWufp, lintgrpGD)
                        End If

                        '09 - ICMS (R$)
                        If ldblIcm > 0 Then
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("I"), lintSampPos, ldblIcm, lintgrpGD)
                        End If

                        '26 - Energia compensada (kWh)
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("E_COMP"), GetPostoTarifario("P"), ldblQtdkWComp_p, lintgrpGD)
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("E_COMP"), GetPostoTarifario("FP"), ldblQtdkWComp_fp, lintgrpGD)

                        '27 - Receita energia compensada (R$)
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("RC_COMP"), GetPostoTarifario("P"), IIf(lintgrpGD = 1, 0, ldblVlrkWComp_p), lintgrpGD)
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("RC_COMP"), GetPostoTarifario("FP"), IIf(lintgrpGD = 1, 0, ldblVlrkWComp_fp), lintgrpGD)

                        '28 - Desconto de energia compensada %
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("DESC_COMP"), GetPostoTarifario("P"), IIf(lintgrpGD = 1, 100, ldblVDescPercComp_p), lintgrpGD)
                        Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("DESC_COMP"), GetPostoTarifario("FP"), IIf(lintgrpGD = 1, 100, ldblVDescPercComp_fp), lintgrpGD)


                        '20 - Desconto Consumo % **
                        '19 - Desconto Demanda % **
                        '25 - Receita Bandeiras (R$)
                        If lintSampCla = 9 Then
                        Else
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("RB"), GetPostoTarifario("P"), ldblVlrBanp, lintgrpGD)
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("RB"), GetPostoTarifario("FP"), ldblVlrBanfp, lintgrpGD)
                        End If

                        '17 - PIS/PASEP (R$)
                        If ldblPis > 0 Then
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("P"), lintSampPos, ldblPis, lintgrpGD)
                        End If

                        '18 - COFINS (R$)
                        If ldblCof > 0 Then
                            Call InsertSampExportacao(CInt(VB6.Format(Me.dtpMesAno.Value, "yyyyMM")), lintSeq, lintRef, 1, GetSampClasses(lintOrd), lintSampMod, GetSubGrupo("A", lintOrd, False), GetDetalheFornecimento("C"), lintSampPos, ldblCof, lintgrpGD)
                        End If

                    End If

                    Call BarraProgresso((Me.ProgressBarPre), (Me.lblPercPre), (Me.lblInfoPre), 1,  , " Grupo A Horosazonal")
                Next
            End If

            dt2 = Nothing

            iblnExporta = True

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

    Private Sub InsertSampExportacao(ByVal Ano_Mes As Integer, ByRef Sequencia As Short, ByVal Refaturado As Short, ByVal tipo As Short, ByVal classe As Short, ByVal Modalidade As Short, ByVal SubGrupo As Short, ByVal Detalhe As Short, ByVal Posto As Short, Optional ByVal adblValor As Double = 0, Optional ByVal grpGD As Integer = 0)
        Dim lstrSql As String

        If adblValor = 0 And Detalhe <> 27 Then
            Exit Sub
        End If

        Try
            lstrSql = ""
            lstrSql = lstrSql & "INSERT INTO `samp_exportacao` SET "
            lstrSql = lstrSql & " `ano_mes` = " & Ano_Mes & ", "
            lstrSql = lstrSql & " `sequencia` = " & Sequencia & ", "
            lstrSql = lstrSql & " `refaturado` = " & Refaturado & ", "
            lstrSql = lstrSql & " `tipo` = " & tipo & ", "
            lstrSql = lstrSql & " `classe` = " & classe & ", "
            lstrSql = lstrSql & " `modalidade` = " & Modalidade & ", "
            lstrSql = lstrSql & " `sub_grupo` = " & SubGrupo & ", "
            lstrSql = lstrSql & " `detalhe` = " & Detalhe & ", "
            lstrSql = lstrSql & " `posto` = " & Posto & ", "
            lstrSql = lstrSql & " `valor` = " & Replace(CStr(adblValor), ",", ".") & ", "
            lstrSql = lstrSql & " `grpGD` =" & grpGD & "; "
            objExecAtualizaBancoDados.QUERY_NAME = lstrSql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
            Sequencia = Sequencia + 1
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: InsertSampExportacao" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub dtpMesAno_ValueChanged(sender As Object, e As EventArgs) Handles dtpMesAno.ValueChanged
        Call MontaNomeArquivo()
    End Sub

    Private Sub MontaNomeArquivo()
        Dim lstrNomeArq As String
        Try
            lstrNomeArq = "SAMP_" & VB6.Format(Me.dtpMesAno.Value, "yyyyMM") & "_Faturamento"
            Me.txtArquivoRegular.Text = lstrNomeArq & "_Regular.TXT"
            Me.txtArquivoRefaturado.Text = lstrNomeArq & "_Refaturamento.TXT"
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: MontaNomeArquivo" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

    Private Sub txtDir_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtDir.TextChanged
        istrCaminho = Me.txtDir.Text
    End Sub

    Private Function VerificaSeExisteExportacao() As Boolean
        Dim lblnRet As Boolean
        Dim lstrsql As String
        Try
            lblnRet = False
            lstrsql = "SELECT * FROM `samp_exportacao` WHERE `ano_mes` = " & VB6.Format(Me.dtpMesAno.Value, "yyyyMM") & " LIMIT 0, 1"
            objAtualizaBancoDados.QUERY_NAME = lstrsql
            dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            If dt.Rows.Count > 0 Then
                lblnRet = True
            End If
            dt = Nothing
            VerificaSeExisteExportacao = lblnRet
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: VerificaSeExisteExportacao" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function
        End Try
    End Function

    Private Sub dtpMesAno_Leave(sender As Object, e As EventArgs) Handles dtpMesAno.Leave
        iblnExporta = VerificaSeExisteExportacao()
        Me.cmdExporta.Enabled = iblnExporta
    End Sub

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

        Try

            Dim lstrsql As String

            lstrsql = ""
            lstrsql = lstrsql & "DROP TABLE IF EXISTS AUX_SAMP_NOVO_BAIXA_RENDA"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "DROP TABLE IF EXISTS AUX_SAMP_NOVO_IMPRESSAOB"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "DROP TABLE IF EXISTS AUX_SAMP_NOVO_IMPRESSAOB_MG"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "DROP TABLE IF EXISTS AUX_SAMP_NOVO_IMPRESSAOB_IA"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "DROP TABLE IF EXISTS AUX_SAMP_NOVO_IMPRESSAOB_TB"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "DROP TABLE IF EXISTS AUX_SAMP_NOVO_IMPRESSAO_USINAS"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "DROP TABLE IF EXISTS AUX_SAMP_NOVO_IMPRESSAOA_CONVENCIONAL"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "DROP TABLE IF EXISTS AUX_SAMP_NOVO_IMPRESSAOA_HOROSSAZONAL"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "DROP TABLE IF EXISTS AUX_SAMP_NOVO_IMPRESSAOA_LIVRE"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)


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

    End Sub


    Private Sub lPrint(ByVal vNomeRPT As String)

        Dim dt As DataTable
        Dim dt_1 As DataTable
        Dim dt_2 As DataTable
        Dim dt_3 As DataTable
        Dim dc As DataColumn
        Dim ds As DataSet
        Dim lstrsql As String

        'Dim bytBLOBData() As Byte

        'Dim objEmpresa As New entidadeEmpresa
        'Dim repositorioEmpresa As New repositorioEmpresa

        'Dim vlist


        Dim bytBLOBData() As Byte

        Dim vlist

        Dim objEmpresa As New entidadeEmpresa
        Dim repositorioEmpresa As New repositorioEmpresa


        vlist = repositorioEmpresa.Select(objEmpresa)

        If vlist.Count <> 0 Then

            For Each objEmpresaFor As entidadeEmpresa In vlist

                bytBLOBData = objEmpresaFor.LOGOTIPO

            Next

        End If

        Try
            Dim rpt As New ReportDocument()

            'rptRelSampNovoBaixaRenda
            If vNomeRPT = "rptRelSampNovoBaixaRenda" Then

                dt_2 = New DataTable()
                dt_2.Columns.Add("CLASSE", GetType(String))
                dt_2.Columns.Add("NCONS", GetType(String))
                dt_2.Columns.Add("KWH", GetType(String))
                dt_2.Columns.Add("KWHRS", GetType(String))
                dt_2.Columns.Add("BANDEIRARS", GetType(String))
                dt_2.Columns.Add("RECEITA", GetType(String))
                dt_2.Columns.Add("REVERSO", GetType(String))
                dt_2.Columns.Add("REVRS", GetType(String))
                dt_2.Columns.Add("PIS", GetType(String))
                dt_2.Columns.Add("ICMS", GetType(String))
                dt_2.Columns.Add("COFINS", GetType(String))
                dt_2.Columns.Add("REFATURAMENTO", GetType(String))
                dt_2.Columns.Add("IMAGEM", GetType(Byte()))

                ds = New DataSet

                lstrsql = ""
                lstrsql = "SELECT * FROM AUX_SAMP_NOVO_BAIXA_RENDA " 'WHERE nm_usuario = '" & My.Computer.Name & "'"
                objAtualizaBancoDados.QUERY_NAME = lstrsql
                dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

                If dt.Rows.Count > 0 Then
                    For Each dr_1 As DataRow In dt.Rows

                        dt_2.Rows.Add(dr_1("CLASSE"),
                                    dr_1("NCONS"),
                                    dr_1("KWH"),
                                    dr_1("KWHRS"),
                                    dr_1("BANDEIRARS"),
                                    dr_1("RECEITA"),
                                    dr_1("REVERSO"),
                                    dr_1("REVRS"),
                                    dr_1("PIS"),
                                    dr_1("ICMS"),
                                    dr_1("COFINS"),
                                    dr_1("REFATURAMENTO"),
                                    bytBLOBData)
                    Next dr_1
                End If

                ds.Tables.Add(dt)
                ds.Tables(0).TableName = "REL_SAMP_NOVO_BAIXA_RENDA"

                If ds.Tables(0).Rows.Count = 0 Then
                    'MessageBox.Show("Não há registros para o relatório escolhido! ", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Exit Sub
                Else

                    rpt = New rptRelSampNovoBaixaRenda

                    rpt.SetDataSource(ds)
                    rpt.SetParameterValue("nomeempresa", sNomeEmpresa)
                    rpt.SetParameterValue("enderecoempresa", sEnderecoEmpresa)
                    rpt.SetParameterValue("cepempresa", sCepEmpresa)
                    rpt.SetParameterValue("ufempresa", sUFempresa)
                    rpt.SetParameterValue("cidadeempresa", sCidadeEmpresa)
                    rpt.SetParameterValue("bairroempresa", sBairroEmpresa)
                    rpt.SetParameterValue("foneempresa", sFoneEmpresa)
                    rpt.SetParameterValue("websiteempresa", sWebEmpresa)
                    rpt.SetParameterValue("MesAnoRef", Month(dtpMesAno.Value) & "/" & Year(dtpMesAno.Value))
                    rpt.SetParameterValue("NomeRel", "Grupo B - Baixa Renda 945")

                    frmViewRelatorios.rpt = rpt

                    frmViewRelatorios.CRViewer.Zoom((100))
                    frmViewRelatorios.Text = "Relatório de Baixa Renda"
                    frmViewRelatorios.ShowDialog()

                End If

                'rptRelSampNovoGrupoB
            ElseIf vNomeRPT = "rptRelSampNovoGrupoB" Then

                dt_2 = New DataTable()
                dt_2.Columns.Add("CLASSE", GetType(String))
                dt_2.Columns.Add("NCONS", GetType(String))
                dt_2.Columns.Add("KWH", GetType(String))
                dt_2.Columns.Add("KWHRS", GetType(String))
                dt_2.Columns.Add("BANDEIRARS", GetType(String))
                dt_2.Columns.Add("RECEITA", GetType(String))
                dt_2.Columns.Add("REVERSO", GetType(String))
                dt_2.Columns.Add("REVRS", GetType(String))
                dt_2.Columns.Add("PIS", GetType(String))
                dt_2.Columns.Add("ICMS", GetType(String))
                dt_2.Columns.Add("COFINS", GetType(String))
                dt_2.Columns.Add("REFATURAMENTO", GetType(String))
                dt_2.Columns.Add("IMAGEM", GetType(Byte()))

                ds = New DataSet

                lstrsql = ""
                lstrsql = "SELECT * FROM AUX_SAMP_NOVO_IMPRESSAOB " 'WHERE nm_usuario = '" & My.Computer.Name & "'"
                objAtualizaBancoDados.QUERY_NAME = lstrsql
                dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

                If dt.Rows.Count > 0 Then
                    For Each dr_1 As DataRow In dt.Rows
                        dt_2.Rows.Add(dr_1("CLASSE"),
                                    dr_1("NCONS"),
                                    dr_1("KWH"),
                                    dr_1("KWHRS"),
                                    dr_1("BANDEIRARS"),
                                    dr_1("RECEITA"),
                                    dr_1("REVERSO"),
                                    dr_1("REVRS"),
                                    dr_1("PIS"),
                                    dr_1("ICMS"),
                                    dr_1("COFINS"),
                                    dr_1("REFATURAMENTO"),
                                    bytBLOBData)
                    Next dr_1
                End If

                ds.Tables.Add(dt)
                ds.Tables(0).TableName = "REL_SAMP_NOVO_IMPRESSAOB"

                If ds.Tables(0).Rows.Count = 0 Then
                    'MessageBox.Show("Não há registros para o relatório escolhido! ", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Exit Sub
                Else

                    rpt = New rptRelSampNovoGrupoB

                    rpt.SetDataSource(ds)
                    rpt.SetParameterValue("nomeempresa", sNomeEmpresa)
                    rpt.SetParameterValue("enderecoempresa", sEnderecoEmpresa)
                    rpt.SetParameterValue("cepempresa", sCepEmpresa)
                    rpt.SetParameterValue("ufempresa", sUFempresa)
                    rpt.SetParameterValue("cidadeempresa", sCidadeEmpresa)
                    rpt.SetParameterValue("bairroempresa", sBairroEmpresa)
                    rpt.SetParameterValue("foneempresa", sFoneEmpresa)
                    rpt.SetParameterValue("websiteempresa", sWebEmpresa)
                    rpt.SetParameterValue("MesAnoRef", Month(dtpMesAno.Value) & "/" & Year(dtpMesAno.Value))
                    rpt.SetParameterValue("NomeRel", "Grupo B")

                    frmViewRelatorios.rpt = rpt

                    frmViewRelatorios.CRViewer.Zoom((100))
                    frmViewRelatorios.Text = "Relatório Grupo B"
                    frmViewRelatorios.ShowDialog()

                End If

                'rptRelSampNovoGrupoBMicroGeracao
            ElseIf vNomeRPT = "rptRelSampNovoGrupoBMicroGeracao" Then


                dt_2 = New DataTable()
                dt_2.Columns.Add("CLASSE", GetType(String))
                dt_2.Columns.Add("NCONS", GetType(String))
                dt_2.Columns.Add("KWHLIDO", GetType(String))
                dt_2.Columns.Add("KWHFAT", GetType(String))
                dt_2.Columns.Add("KWHRS", GetType(String))
                dt_2.Columns.Add("BANDEIRARS", GetType(String))
                dt_2.Columns.Add("RECEITA", GetType(String))
                dt_2.Columns.Add("REVERSO", GetType(String))
                dt_2.Columns.Add("REVERSOKWH", GetType(String))
                dt_2.Columns.Add("RECEITAREV", GetType(String))
                dt_2.Columns.Add("PIS", GetType(String))
                dt_2.Columns.Add("ICMS", GetType(String))
                dt_2.Columns.Add("COFINS", GetType(String))
                dt_2.Columns.Add("REFATURAMENTO", GetType(String))
                dt_2.Columns.Add("IMAGEM", GetType(Byte()))
                ds = New DataSet

                lstrsql = ""
                lstrsql = "SELECT * FROM AUX_SAMP_NOVO_IMPRESSAOB_MG " 'WHERE nm_usuario = '" & My.Computer.Name & "'"
                objAtualizaBancoDados.QUERY_NAME = lstrsql
                dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

                If dt.Rows.Count > 0 Then
                    For Each dr_1 As DataRow In dt.Rows
                        dt_2.Rows.Add(dr_1("CLASSE"),
                                    dr_1("NCONS"),
                                    dr_1("KWHLIDO"),
                                    dr_1("KWHFAT"),
                                    dr_1("KWHRS"),
                                    dr_1("BANDEIRARS"),
                                    dr_1("RECEITA"),
                                    dr_1("REVERSO"),
                                    dr_1("REVERSOKWH"),
                                    dr_1("RECEITAREV"),
                                    dr_1("PIS"),
                                    dr_1("ICMS"),
                                    dr_1("COFINS"),
                                    dr_1("REFATURAMENTO"),
                                    bytBLOBData)
                    Next dr_1
                End If

                ds.Tables.Add(dt)
                ds.Tables(0).TableName = "REL_SAMP_NOVO_IMPRESSAOB_MG"

                If ds.Tables(0).Rows.Count = 0 Then
                    'MessageBox.Show("Não há registros para o relatório escolhido! ", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Exit Sub
                Else

                    rpt = New rptRelSampNovoGrupoBMicroGeracao

                    rpt.SetDataSource(ds)
                    rpt.SetParameterValue("nomeempresa", sNomeEmpresa)
                    rpt.SetParameterValue("enderecoempresa", sEnderecoEmpresa)
                    rpt.SetParameterValue("cepempresa", sCepEmpresa)
                    rpt.SetParameterValue("ufempresa", sUFempresa)
                    rpt.SetParameterValue("cidadeempresa", sCidadeEmpresa)
                    rpt.SetParameterValue("bairroempresa", sBairroEmpresa)
                    rpt.SetParameterValue("foneempresa", sFoneEmpresa)
                    rpt.SetParameterValue("websiteempresa", sWebEmpresa)
                    rpt.SetParameterValue("MesAnoRef", Month(dtpMesAno.Value) & "/" & Year(dtpMesAno.Value))
                    rpt.SetParameterValue("NomeRel", "Grupo B - Micro Geração")

                    frmViewRelatorios.rpt = rpt

                    frmViewRelatorios.CRViewer.Zoom((100))
                    frmViewRelatorios.Text = "Relatório Grupo B Micro Geração"
                    frmViewRelatorios.ShowDialog()

                End If

                'rptRelSampNovoGrupoBIrrigacaoAquicultura
            ElseIf vNomeRPT = "rptRelSampNovoGrupoBIrrigacaoAquicultura" Then

                dt_2 = New DataTable()
                dt_2.Columns.Add("CLASSE", GetType(String))
                dt_2.Columns.Add("NCONS", GetType(String))
                dt_2.Columns.Add("KWHFAT", GetType(String))
                dt_2.Columns.Add("KWHRS", GetType(String))
                dt_2.Columns.Add("BANRS", GetType(String))
                dt_2.Columns.Add("KWHPNORMAL", GetType(String))
                dt_2.Columns.Add("BANPNORMAL", GetType(String))
                dt_2.Columns.Add("KWHRSPNORMAL", GetType(String))
                dt_2.Columns.Add("RECEITAPNORMAL", GetType(String))
                dt_2.Columns.Add("KWHPRESERVA", GetType(String))
                dt_2.Columns.Add("RECEITAPRESERVA", GetType(String))
                dt_2.Columns.Add("BANPRESERVA", GetType(String))
                dt_2.Columns.Add("KWHRSPRESERVA", GetType(String))
                dt_2.Columns.Add("PIS", GetType(String))
                dt_2.Columns.Add("ICMS", GetType(String))
                dt_2.Columns.Add("COFINS", GetType(String))
                dt_2.Columns.Add("REFATURAMENTO", GetType(String))
                dt_2.Columns.Add("IMAGEM", GetType(Byte()))
                ds = New DataSet


                lstrsql = ""
                lstrsql = "SELECT * FROM AUX_SAMP_NOVO_IMPRESSAOB_IA " 'WHERE nm_usuario = '" & My.Computer.Name & "'"
                objAtualizaBancoDados.QUERY_NAME = lstrsql
                dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

                If dt.Rows.Count > 0 Then
                    For Each dr_1 As DataRow In dt.Rows
                        dt_2.Rows.Add(dr_1("CLASSE"),
                                    dr_1("NCONS"),
                                    dr_1("KWHFAT"),
                                    dr_1("KWHRS"),
                                    dr_1("BANRS"),
                                    dr_1("KWHPNORMAL"),
                                    dr_1("BANPNORMAL"),
                                    dr_1("KWHRSPNORMAL"),
                                    dr_1("RECEITAPNORMAL"),
                                    dr_1("KWHPRESERVA"),
                                    dr_1("RECEITAPRESERVA"),
                                    dr_1("BANPRESERVA"),
                                    dr_1("KWHRSPRESERVA"),
                                    dr_1("PIS"),
                                    dr_1("ICMS"),
                                    dr_1("COFINS"),
                                    dr_1("REFATURAMENTO"),
                                    bytBLOBData)
                    Next dr_1
                End If

                ds.Tables.Add(dt)
                ds.Tables(0).TableName = "REL_SAMP_NOVO_IMPRESSAOB_IA"

                If ds.Tables(0).Rows.Count = 0 Then
                    'MessageBox.Show("Não há registros para o relatório escolhido! ", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Exit Sub
                Else

                    rpt = New rptRelSampNovoGrupoBIrrigacaoAquicultura

                    rpt.SetDataSource(ds)
                    rpt.SetParameterValue("nomeempresa", sNomeEmpresa)
                    rpt.SetParameterValue("enderecoempresa", sEnderecoEmpresa)
                    rpt.SetParameterValue("cepempresa", sCepEmpresa)
                    rpt.SetParameterValue("ufempresa", sUFempresa)
                    rpt.SetParameterValue("cidadeempresa", sCidadeEmpresa)
                    rpt.SetParameterValue("bairroempresa", sBairroEmpresa)
                    rpt.SetParameterValue("foneempresa", sFoneEmpresa)
                    rpt.SetParameterValue("websiteempresa", sWebEmpresa)
                    rpt.SetParameterValue("MesAnoRef", Month(dtpMesAno.Value) & "/" & Year(dtpMesAno.Value))
                    rpt.SetParameterValue("NomeRel", "Grupo B - Irrigação de Aquicultura")

                    frmViewRelatorios.rpt = rpt

                    frmViewRelatorios.CRViewer.Zoom((100))
                    frmViewRelatorios.Text = "Relatório Grupo B Micro Geração"
                    frmViewRelatorios.ShowDialog()

                End If

                'rptRelSampNovoGrupoBTarifaBranca
            ElseIf vNomeRPT = "rptRelSampNovoGrupoBTarifaBranca" Then

                dt_2 = New DataTable()
                dt_2.Columns.Add("CLASSE", GetType(String))
                dt_2.Columns.Add("NCONS", GetType(String))
                dt_2.Columns.Add("KWHFAT", GetType(String))
                dt_2.Columns.Add("KWHRS", GetType(String))
                dt_2.Columns.Add("BANRS", GetType(String))
                dt_2.Columns.Add("KWHPONTA", GetType(String))
                dt_2.Columns.Add("BANPONTA", GetType(String))
                dt_2.Columns.Add("KWHRSPONTA", GetType(String))
                dt_2.Columns.Add("RECEITAPONTA", GetType(String))
                dt_2.Columns.Add("KWHINTER", GetType(String))
                dt_2.Columns.Add("BANINTER", GetType(String))
                dt_2.Columns.Add("KWHRSINTER", GetType(String))
                dt_2.Columns.Add("RECEITAINTER", GetType(String))
                dt_2.Columns.Add("KWHFORAP", GetType(String))
                dt_2.Columns.Add("BANFORAP", GetType(String))
                dt_2.Columns.Add("KWHRSFORAP", GetType(String))
                dt_2.Columns.Add("RECEITAFORAP", GetType(String))
                dt_2.Columns.Add("PIS", GetType(String))
                dt_2.Columns.Add("ICMS", GetType(String))
                dt_2.Columns.Add("COFINS", GetType(String))
                dt_2.Columns.Add("REFATURAMENTO", GetType(String))
                dt_2.Columns.Add("IMAGEM", GetType(Byte()))
                ds = New DataSet

                lstrsql = ""
                lstrsql = "SELECT * FROM AUX_SAMP_NOVO_IMPRESSAOB_TB " 'WHERE nm_usuario = '" & My.Computer.Name & "'"
                objAtualizaBancoDados.QUERY_NAME = lstrsql
                dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

                If dt.Rows.Count > 0 Then
                    For Each dr_1 As DataRow In dt.Rows
                        dt_2.Rows.Add(dr_1("CLASSE"),
                                    dr_1("NCONS"),
                                    dr_1("KWHFAT"),
                                    dr_1("KWHRS"),
                                    dr_1("BANRS"),
                                    dr_1("KWHPONTA"),
                                    dr_1("BANPONTA"),
                                    dr_1("KWHRSPONTA"),
                                    dr_1("RECEITAPONTA"),
                                    dr_1("KWHINTER"),
                                    dr_1("BANINTER"),
                                    dr_1("KWHRSINTER"),
                                    dr_1("RECEITAINTER"),
                                    dr_1("KWHFORAP"),
                                    dr_1("BANFORAP"),
                                    dr_1("KWHRSFORAP"),
                                    dr_1("RECEITAFORAP"),
                                    dr_1("PIS"),
                                    dr_1("ICMS"),
                                    dr_1("COFINS"),
                                    dr_1("REFATURAMENTO"),
                                    bytBLOBData)
                    Next dr_1
                End If


                ds.Tables.Add(dt)
                ds.Tables(0).TableName = "REL_SAMP_NOVO_IMPRESSAOB_TB"

                If ds.Tables(0).Rows.Count = 0 Then
                    'MessageBox.Show("Não há registros para o relatório escolhido! ", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Exit Sub
                Else

                    rpt = New rptRelSampNovoGrupoBTarifaBranca

                    rpt.SetDataSource(ds)
                    rpt.SetParameterValue("nomeempresa", sNomeEmpresa)
                    rpt.SetParameterValue("enderecoempresa", sEnderecoEmpresa)
                    rpt.SetParameterValue("cepempresa", sCepEmpresa)
                    rpt.SetParameterValue("ufempresa", sUFempresa)
                    rpt.SetParameterValue("cidadeempresa", sCidadeEmpresa)
                    rpt.SetParameterValue("bairroempresa", sBairroEmpresa)
                    rpt.SetParameterValue("foneempresa", sFoneEmpresa)
                    rpt.SetParameterValue("websiteempresa", sWebEmpresa)
                    rpt.SetParameterValue("MesAnoRef", Month(dtpMesAno.Value) & "/" & Year(dtpMesAno.Value))
                    rpt.SetParameterValue("NomeRel", "Grupo B - Tarifa Branca")

                    frmViewRelatorios.rpt = rpt

                    frmViewRelatorios.CRViewer.Zoom((100))
                    frmViewRelatorios.Text = "Relatório Grupo B Micro Geração"
                    frmViewRelatorios.ShowDialog()

                End If

                'rptRelSampNovoUsinas
            ElseIf vNomeRPT = "rptRelSampNovoUsinas" Then

                dt_2 = New DataTable()
                dt_2.Columns.Add("CLASSE", GetType(String))
                dt_2.Columns.Add("NCONS", GetType(String))
                dt_2.Columns.Add("CONTRATADO", GetType(String))
                dt_2.Columns.Add("MUSTRS", GetType(String))
                dt_2.Columns.Add("DESCONTORS", GetType(String))
                dt_2.Columns.Add("RECEITARS", GetType(String))
                dt_2.Columns.Add("ENCARGORS", GetType(String))
                dt_2.Columns.Add("REFATURAMENTO", GetType(String))
                dt_2.Columns.Add("IMAGEM", GetType(Byte()))
                ds = New DataSet

                lstrsql = ""
                lstrsql = "SELECT * FROM AUX_SAMP_NOVO_IMPRESSAO_USINAS " 'WHERE nm_usuario = '" & My.Computer.Name & "'"
                objAtualizaBancoDados.QUERY_NAME = lstrsql
                dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

                If dt.Rows.Count > 0 Then
                    For Each dr_1 As DataRow In dt.Rows
                        dt_2.Rows.Add(dr_1("CLASSE"),
                                    dr_1("NCONS"),
                                    dr_1("CONTRATADO"),
                                    dr_1("DESCONTORS"),
                                    dr_1("RECEITARS"),
                                    dr_1("ENCARGORS"),
                                    dr_1("REFATURAMENTO"),
                                    bytBLOBData)
                    Next dr_1
                End If

                ds.Tables.Add(dt)
                ds.Tables(0).TableName = "REL_SAMP_NOVO_IMPRESSAO_USINAS"

                If ds.Tables(0).Rows.Count = 0 Then
                    'MessageBox.Show("Não há registros para o relatório escolhido! ", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Exit Sub
                Else

                    rpt = New rptRelSampNovoUsinas

                    rpt.SetDataSource(ds)
                    rpt.SetParameterValue("nomeempresa", sNomeEmpresa)
                    rpt.SetParameterValue("enderecoempresa", sEnderecoEmpresa)
                    rpt.SetParameterValue("cepempresa", sCepEmpresa)
                    rpt.SetParameterValue("ufempresa", sUFempresa)
                    rpt.SetParameterValue("cidadeempresa", sCidadeEmpresa)
                    rpt.SetParameterValue("bairroempresa", sBairroEmpresa)
                    rpt.SetParameterValue("foneempresa", sFoneEmpresa)
                    rpt.SetParameterValue("websiteempresa", sWebEmpresa)
                    rpt.SetParameterValue("MesAnoRef", Month(dtpMesAno.Value) & "/" & Year(dtpMesAno.Value))
                    rpt.SetParameterValue("NomeRel", "Usinas")

                    frmViewRelatorios.rpt = rpt

                    frmViewRelatorios.CRViewer.Zoom((100))
                    frmViewRelatorios.Text = "Relatório Grupo B Usinas"
                    frmViewRelatorios.ShowDialog()

                End If

                'rptRelSampNovoGrupoAConvencional
            ElseIf vNomeRPT = "rptRelSampNovoGrupoAConvencional" Then

                dt_2 = New DataTable()
                dt_2.Columns.Add("CLASSE", GetType(String))
                dt_2.Columns.Add("NCONS", GetType(String))
                dt_2.Columns.Add("KWH", GetType(String))
                dt_2.Columns.Add("KW", GetType(String))
                dt_2.Columns.Add("KWULT", GetType(String))
                dt_2.Columns.Add("KWHRS", GetType(String))
                dt_2.Columns.Add("BANDEIRARS", GetType(String))
                dt_2.Columns.Add("RECEITA", GetType(String))
                dt_2.Columns.Add("KWRS", GetType(String))
                dt_2.Columns.Add("KWULTRS", GetType(String))
                dt_2.Columns.Add("ICMS", GetType(String))
                dt_2.Columns.Add("PIS", GetType(String))
                dt_2.Columns.Add("COFINS", GetType(String))
                dt_2.Columns.Add("REFATURAMENTO", GetType(String))
                dt_2.Columns.Add("IMAGEM", GetType(Byte()))
                ds = New DataSet

                lstrsql = ""
                lstrsql = "SELECT * FROM AUX_SAMP_NOVO_IMPRESSAOA_CONVENCIONAL " 'WHERE nm_usuario = '" & My.Computer.Name & "'"
                objAtualizaBancoDados.QUERY_NAME = lstrsql
                dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

                If dt.Rows.Count > 0 Then
                    For Each dr_1 As DataRow In dt.Rows
                        dt_2.Rows.Add(dr_1("CLASSE"),
                                    dr_1("NCONS"),
                                    dr_1("KWH"),
                                    dr_1("KW"),
                                    dr_1("KWULT"),
                                    dr_1("KWHRS"),
                                    dr_1("BANDEIRARS"),
                                    dr_1("RECEITA"),
                                    dr_1("KWRS"),
                                    dr_1("KWULTRS"),
                                    dr_1("ICMS"),
                                    dr_1("PIS"),
                                    dr_1("COFINS"),
                                    dr_1("REFATURAMENTO"),
                                    bytBLOBData)
                    Next dr_1
                End If

                ds.Tables.Add(dt)
                ds.Tables(0).TableName = "REL_SAMP_NOVO_IMPRESSAOA_CONVENCIONAL"

                If ds.Tables(0).Rows.Count = 0 Then
                    'MessageBox.Show("Não há registros para o relatório escolhido! ", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Exit Sub
                Else

                    rpt = New rptRelSampNovoGrupoAConvencional

                    rpt.SetDataSource(ds)
                    rpt.SetParameterValue("nomeempresa", sNomeEmpresa)
                    rpt.SetParameterValue("enderecoempresa", sEnderecoEmpresa)
                    rpt.SetParameterValue("cepempresa", sCepEmpresa)
                    rpt.SetParameterValue("ufempresa", sUFempresa)
                    rpt.SetParameterValue("cidadeempresa", sCidadeEmpresa)
                    rpt.SetParameterValue("bairroempresa", sBairroEmpresa)
                    rpt.SetParameterValue("foneempresa", sFoneEmpresa)
                    rpt.SetParameterValue("websiteempresa", sWebEmpresa)
                    rpt.SetParameterValue("MesAnoRef", Month(dtpMesAno.Value) & "/" & Year(dtpMesAno.Value))
                    rpt.SetParameterValue("NomeRel", "Grupo A - Convencional")

                    frmViewRelatorios.rpt = rpt

                    frmViewRelatorios.CRViewer.Zoom((100))
                    frmViewRelatorios.Text = "Relatório Grupo A Convencional"
                    frmViewRelatorios.ShowDialog()

                End If

                'rptRelSampNovoGrupoAHorossazonal
            ElseIf vNomeRPT = "rptRelSampNovoGrupoAHorossazonal" Then

                dt_2 = New DataTable()
                dt_2.Columns.Add("CLASSE", GetType(String))
                dt_2.Columns.Add("NCONS", GetType(String))
                dt_2.Columns.Add("KWHPONTA", GetType(String))
                dt_2.Columns.Add("KWHFORAP", GetType(String))
                dt_2.Columns.Add("KWPONTA", GetType(String))
                dt_2.Columns.Add("KWFORAP", GetType(String))
                dt_2.Columns.Add("KWULTPONTA", GetType(String))
                dt_2.Columns.Add("KWULTFORAP", GetType(String))
                dt_2.Columns.Add("KWHRSPONTA", GetType(String))
                dt_2.Columns.Add("KWHRSFORAP", GetType(String))
                dt_2.Columns.Add("BANDEIRAPONTA", GetType(String))
                dt_2.Columns.Add("BANDEIRAFORAP", GetType(String))
                dt_2.Columns.Add("RECEITAPONTA", GetType(String))
                dt_2.Columns.Add("RECEITAFORAP", GetType(String))
                dt_2.Columns.Add("KWRSPONTA", GetType(String))
                dt_2.Columns.Add("KWRSFORAP", GetType(String))
                dt_2.Columns.Add("KWRSULTPONTA", GetType(String))
                dt_2.Columns.Add("KWRSULTFORAP", GetType(String))
                dt_2.Columns.Add("ICMS", GetType(String))
                dt_2.Columns.Add("PIS", GetType(String))
                dt_2.Columns.Add("COFINS", GetType(String))
                dt_2.Columns.Add("REFATURAMENTO", GetType(String))
                dt_2.Columns.Add("IMAGEM", GetType(Byte()))
                ds = New DataSet

                lstrsql = ""
                lstrsql = "SELECT * FROM AUX_SAMP_NOVO_IMPRESSAOA_HOROSSAZONAL " 'WHERE nm_usuario = '" & My.Computer.Name & "'"
                objAtualizaBancoDados.QUERY_NAME = lstrsql
                dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

                If dt.Rows.Count > 0 Then
                    For Each dr_1 As DataRow In dt.Rows
                        dt_2.Rows.Add(dr_1("CLASSE"),
                                    dr_1("NCONS"),
                                    dr_1("KWHPONTA"),
                                    dr_1("KWHFORAP"),
                                    dr_1("KWPONTA"),
                                    dr_1("KWFORAP"),
                                    dr_1("KWULTPONTA"),
                                    dr_1("KWULTFORAP"),
                                    dr_1("KWHRSPONTA"),
                                    dr_1("KWHRSFORAP"),
                                    dr_1("BANDEIRAPONTA"),
                                    dr_1("BANDEIRAFORAP"),
                                    dr_1("RECEITAPONTA"),
                                    dr_1("RECEITAFORAP"),
                                    dr_1("KWRSPONTA"),
                                    dr_1("KWRSFORAP"),
                                    dr_1("KWRSULTPONTA"),
                                    dr_1("KWRSULTFORAP"),
                                    dr_1("ICMS"),
                                    dr_1("PIS"),
                                    dr_1("COFINS"),
                                    dr_1("REFATURAMENTO"),
                                    bytBLOBData)


                    Next dr_1
                End If

                ds.Tables.Add(dt)
                ds.Tables(0).TableName = "REL_SAMP_NOVO_IMPRESSAOA_HOROSSAZONAL"

                If ds.Tables(0).Rows.Count = 0 Then
                    'MessageBox.Show("Não há registros para o relatório escolhido! ", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Exit Sub
                Else

                    rpt = New rptRelSampNovoGrupoAHorossazonal

                    rpt.SetDataSource(ds)
                    rpt.SetParameterValue("nomeempresa", sNomeEmpresa)
                    rpt.SetParameterValue("enderecoempresa", sEnderecoEmpresa)
                    rpt.SetParameterValue("cepempresa", sCepEmpresa)
                    rpt.SetParameterValue("ufempresa", sUFempresa)
                    rpt.SetParameterValue("cidadeempresa", sCidadeEmpresa)
                    rpt.SetParameterValue("bairroempresa", sBairroEmpresa)
                    rpt.SetParameterValue("foneempresa", sFoneEmpresa)
                    rpt.SetParameterValue("websiteempresa", sWebEmpresa)
                    rpt.SetParameterValue("MesAnoRef", Month(dtpMesAno.Value) & "/" & Year(dtpMesAno.Value))
                    rpt.SetParameterValue("NomeRel", "Grupo A - Horo Sazonal")

                    frmViewRelatorios.rpt = rpt

                    frmViewRelatorios.CRViewer.Zoom((100))
                    frmViewRelatorios.Text = "Relatório Grupo A Horossazonal"
                    frmViewRelatorios.ShowDialog()

                End If

                'rptRelSampNovoGrupoALivre
            ElseIf vNomeRPT = "rptRelSampNovoGrupoALivre" Then

                dt_2 = New DataTable()
                dt_2.Columns.Add("CLASSE", GetType(String))
                dt_2.Columns.Add("NCONS", GetType(String))
                dt_2.Columns.Add("DESCPROV", GetType(String))
                dt_2.Columns.Add("DESCREAL", GetType(String))
                dt_2.Columns.Add("DEVCCEE", GetType(String))
                dt_2.Columns.Add("COBCCEE", GetType(String))
                dt_2.Columns.Add("DESCPROV_KWH", GetType(String))
                dt_2.Columns.Add("DESCREAL_KWH", GetType(String))
                dt_2.Columns.Add("DEVCCEE_KWH", GetType(String))
                dt_2.Columns.Add("COBCCEE_KWH", GetType(String))
                dt_2.Columns.Add("KWHPONTA", GetType(String))
                dt_2.Columns.Add("KWHFORAP", GetType(String))
                dt_2.Columns.Add("KWPONTA", GetType(String))
                dt_2.Columns.Add("KWFORAP", GetType(String))
                dt_2.Columns.Add("KWULTPONTA", GetType(String))
                dt_2.Columns.Add("KWULTFORAP", GetType(String))
                dt_2.Columns.Add("KWHRSPONTA", GetType(String))
                dt_2.Columns.Add("KWHRSFORAP", GetType(String))
                dt_2.Columns.Add("BANDEIRAPONTA", GetType(String))
                dt_2.Columns.Add("BANDEIRAFORAP", GetType(String))
                dt_2.Columns.Add("RECEITAPONTA", GetType(String))
                dt_2.Columns.Add("RECEITAFORAP", GetType(String))
                dt_2.Columns.Add("KWRSPONTA", GetType(String))
                dt_2.Columns.Add("KWRSFORAP", GetType(String))
                dt_2.Columns.Add("KWRSULTPONTA", GetType(String))
                dt_2.Columns.Add("KWRSULTFORAP", GetType(String))
                dt_2.Columns.Add("ENCARGOPONTA", GetType(String))
                dt_2.Columns.Add("ENCARGOFORAP", GetType(String))
                dt_2.Columns.Add("ENCARGORSPONTA", GetType(String))
                dt_2.Columns.Add("ENCARGORSFORAP", GetType(String))
                dt_2.Columns.Add("ICMS", GetType(String))
                dt_2.Columns.Add("PIS", GetType(String))
                dt_2.Columns.Add("COFINS", GetType(String))
                dt_2.Columns.Add("REFATURAMENTO", GetType(String))
                dt_2.Columns.Add("IMAGEM", GetType(Byte()))
                ds = New DataSet


                lstrsql = ""
                lstrsql = "SELECT * FROM AUX_SAMP_NOVO_IMPRESSAOA_LIVRE " 'WHERE nm_usuario = '" & My.Computer.Name & "'"
                objAtualizaBancoDados.QUERY_NAME = lstrsql
                dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

                If dt.Rows.Count > 0 Then
                    For Each dr_1 As DataRow In dt.Rows

                        dt_2.Rows.Add(dr_1("CLASSE"),
                                    dr_1("NCONS"),
                                    dr_1("DESCPROV"),
                                    dr_1("DESCREAL"),
                                    dr_1("DEVCCEE"),
                                    dr_1("COBCCEE"),
                                    dr_1("DESCPROV_KWH"),
                                    dr_1("DESCREAL_KWH"),
                                    dr_1("DEVCCEE_KWH"),
                                    dr_1("COBCCEE_KWH"),
                                    dr_1("KWHPONTA"),
                                    dr_1("KWHFORAP"),
                                    dr_1("KWPONTA"),
                                    dr_1("KWFORAP"),
                                    dr_1("KWULTPONTA"),
                                    dr_1("KWULTFORAP"),
                                    dr_1("KWHRSPONTA"),
                                    dr_1("KWHRSFORAP"),
                                    dr_1("BANDEIRAPONTA"),
                                    dr_1("BANDEIRAFORAP"),
                                    dr_1("RECEITAPONTA"),
                                    dr_1("RECEITAFORAP"),
                                    dr_1("KWRSPONTA"),
                                    dr_1("KWRSFORAP"),
                                    dr_1("KWRSULTPONTA"),
                                    dr_1("KWRSULTFORAP"),
                                    dr_1("ENCARGOPONTA"),
                                    dr_1("ENCARGOFORAP"),
                                    dr_1("ENCARGORSPONTA"),
                                    dr_1("ENCARGORSFORAP"),
                                    dr_1("ICMS"),
                                    dr_1("PIS"),
                                    dr_1("COFINS"),
                                    dr_1("REFATURAMENTO"),
                                    bytBLOBData)

                    Next dr_1
                End If

                ds.Tables.Add(dt)
                ds.Tables(0).TableName = "REL_SAMP_NOVO_IMPRESSAOA_LIVRE"

                If ds.Tables(0).Rows.Count = 0 Then
                    'MessageBox.Show("Não há registros para o relatório escolhido! ", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Exit Sub
                Else

                    rpt = New rptRelSampNovoGrupoALivre

                    rpt.SetDataSource(ds)
                    rpt.SetParameterValue("nomeempresa", sNomeEmpresa)
                    rpt.SetParameterValue("enderecoempresa", sEnderecoEmpresa)
                    rpt.SetParameterValue("cepempresa", sCepEmpresa)
                    rpt.SetParameterValue("ufempresa", sUFempresa)
                    rpt.SetParameterValue("cidadeempresa", sCidadeEmpresa)
                    rpt.SetParameterValue("bairroempresa", sBairroEmpresa)
                    rpt.SetParameterValue("foneempresa", sFoneEmpresa)
                    rpt.SetParameterValue("websiteempresa", sWebEmpresa)
                    rpt.SetParameterValue("MesAnoRef", Month(dtpMesAno.Value) & "/" & Year(dtpMesAno.Value))
                    rpt.SetParameterValue("NomeRel", "Grupo A - Livre\Parcial.Cativo")

                    frmViewRelatorios.rpt = rpt

                    frmViewRelatorios.CRViewer.Zoom((100))
                    frmViewRelatorios.Text = "Relatório Grupo A - Livre\Parcial.Cativo"
                    frmViewRelatorios.ShowDialog()

                End If


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

    End Sub

    Private Sub sCriaTabelaAuxiliares()

        Dim lstrsql As String

        Try

            lstrsql = ""
            lstrsql = lstrsql & "DROP TABLE IF EXISTS AUX_SAMP_NOVO_BAIXA_RENDA"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "create table IF NOT EXISTS AUX_SAMP_NOVO_BAIXA_RENDA("
            lstrsql = lstrsql & "CLASSE VARCHAR(50),"
            lstrsql = lstrsql & "NCONS  VARCHAR(20),"
            lstrsql = lstrsql & "KWH    VARCHAR(20),"
            lstrsql = lstrsql & "KWHRS  VARCHAR(20),"
            lstrsql = lstrsql & "BANDEIRARS VARCHAR(20),"
            lstrsql = lstrsql & "RECEITA VARCHAR(20),"
            lstrsql = lstrsql & "REVERSO VARCHAR(20),"
            lstrsql = lstrsql & "REVRS   VARCHAR(20),"
            lstrsql = lstrsql & "PIS     VARCHAR(20),"
            lstrsql = lstrsql & "ICMS    VARCHAR(20),"
            lstrsql = lstrsql & "COFINS VARCHAR(20),"
            lstrsql = lstrsql & "REFATURAMENTO CHAR(1))"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)


            lstrsql = ""
            lstrsql = lstrsql & "DROP TABLE IF EXISTS AUX_SAMP_NOVO_IMPRESSAOB"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "create table IF NOT EXISTS AUX_SAMP_NOVO_IMPRESSAOB("
            lstrsql = lstrsql & "CLASSE VARCHAR(50),"
            lstrsql = lstrsql & "NCONS  VARCHAR(20),"
            lstrsql = lstrsql & "KWH    VARCHAR(20),"
            lstrsql = lstrsql & "KWHRS  VARCHAR(20),"
            lstrsql = lstrsql & "BANDEIRARS VARCHAR(20),"
            lstrsql = lstrsql & "RECEITA VARCHAR(20),"
            lstrsql = lstrsql & "REVERSO VARCHAR(20),"
            lstrsql = lstrsql & "REVRS   VARCHAR(20),"
            lstrsql = lstrsql & "PIS     VARCHAR(20),"
            lstrsql = lstrsql & "ICMS    VARCHAR(20),"
            lstrsql = lstrsql & "COFINS VARCHAR(20),"
            lstrsql = lstrsql & "REFATURAMENTO CHAR(1))"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "DROP TABLE IF EXISTS AUX_SAMP_NOVO_IMPRESSAOB_MG"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "create table IF NOT EXISTS AUX_SAMP_NOVO_IMPRESSAOB_MG("
            lstrsql = lstrsql & "CLASSE     VARCHAR(50), "
            lstrsql = lstrsql & "NCONS      VARCHAR(20), "
            lstrsql = lstrsql & "KWHLIDO    VARCHAR(20), "
            lstrsql = lstrsql & "KWHFAT     VARCHAR(20), "
            lstrsql = lstrsql & "KWHRS      VARCHAR(20), "
            lstrsql = lstrsql & "BANDEIRARS VARCHAR(20), "
            lstrsql = lstrsql & "RECEITA    VARCHAR(20), "
            lstrsql = lstrsql & "REVERSO    VARCHAR(20), "
            lstrsql = lstrsql & "REVERSOKWH   VARCHAR(20), "
            lstrsql = lstrsql & "RECEITAREV VARCHAR(20), "
            lstrsql = lstrsql & "PIS        VARCHAR(20), "
            lstrsql = lstrsql & "ICMS       VARCHAR(20), "
            lstrsql = lstrsql & "COFINS    VARCHAR(20), "
            lstrsql = lstrsql & "REFATURAMENTO CHAR(1))"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "DROP TABLE IF EXISTS AUX_SAMP_NOVO_IMPRESSAOB_IA"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "create table IF NOT EXISTS AUX_SAMP_NOVO_IMPRESSAOB_IA("
            lstrsql = lstrsql & "CLASSE VARCHAR(50), "
            lstrsql = lstrsql & "NCONS  VARCHAR(20), "
            lstrsql = lstrsql & "KWHFAT   VARCHAR(20), "
            lstrsql = lstrsql & "KWHRS  VARCHAR(20), "
            lstrsql = lstrsql & "BANRS  VARCHAR(20), "
            lstrsql = lstrsql & "KWHPNORMAL  VARCHAR(20), "
            lstrsql = lstrsql & "BANPNORMAL  VARCHAR(20), "
            lstrsql = lstrsql & "KWHRSPNORMAL  VARCHAR(20), "
            lstrsql = lstrsql & "RECEITAPNORMAL VARCHAR(20), "
            lstrsql = lstrsql & "BANPRESERVA VARCHAR(20), "
            lstrsql = lstrsql & "KWHRSPRESERVA VARCHAR(20), "
            lstrsql = lstrsql & "KWHPRESERVA VARCHAR(20), "
            lstrsql = lstrsql & "RECEITAPRESERVA VARCHAR(20), "
            lstrsql = lstrsql & "PIS     VARCHAR(20), "
            lstrsql = lstrsql & "ICMS    VARCHAR(20), "
            lstrsql = lstrsql & "COFINS VARCHAR(20), "
            lstrsql = lstrsql & "REFATURAMENTO CHAR(1))"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)


            lstrsql = ""
            lstrsql = lstrsql & "DROP TABLE IF EXISTS AUX_SAMP_NOVO_IMPRESSAOB_TB"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "create table IF NOT EXISTS AUX_SAMP_NOVO_IMPRESSAOB_TB("
            lstrsql = lstrsql & "CLASSE VARCHAR(50),"
            lstrsql = lstrsql & "NCONS  VARCHAR(20),"
            lstrsql = lstrsql & "KWHFAT    VARCHAR(20),"
            lstrsql = lstrsql & "KWHRS  VARCHAR(20),"
            lstrsql = lstrsql & "BANRS    VARCHAR(20),"
            lstrsql = lstrsql & "KWHPONTA  VARCHAR(20),"
            lstrsql = lstrsql & "BANPONTA  VARCHAR(20),"
            lstrsql = lstrsql & "KWHRSPONTA  VARCHAR(20),"
            lstrsql = lstrsql & "RECEITAPONTA  VARCHAR(20),"
            lstrsql = lstrsql & "KWHINTER  VARCHAR(20),"
            lstrsql = lstrsql & "BANINTER  VARCHAR(20),"
            lstrsql = lstrsql & "KWHRSINTER  VARCHAR(20),"
            lstrsql = lstrsql & "RECEITAINTER  VARCHAR(20),"
            lstrsql = lstrsql & "KWHFORAP  VARCHAR(20),"
            lstrsql = lstrsql & "BANFORAP  VARCHAR(20),"
            lstrsql = lstrsql & "KWHRSFORAP  VARCHAR(20),"
            lstrsql = lstrsql & "RECEITAFORAP  VARCHAR(20),"
            lstrsql = lstrsql & "PIS     VARCHAR(20),"
            lstrsql = lstrsql & "ICMS    VARCHAR(20),"
            lstrsql = lstrsql & "COFINS VARCHAR(20),"
            lstrsql = lstrsql & "REFATURAMENTO CHAR(1))"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)


            lstrsql = ""
            lstrsql = lstrsql & "DROP TABLE IF EXISTS AUX_SAMP_NOVO_IMPRESSAO_USINAS"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "create table IF NOT EXISTS AUX_SAMP_NOVO_IMPRESSAO_USINAS("
            lstrsql = lstrsql & "CLASSE VARCHAR(50), "
            lstrsql = lstrsql & "NCONS  VARCHAR(20), "
            lstrsql = lstrsql & "CONTRATADO    VARCHAR(20), "
            lstrsql = lstrsql & "MUSTRS     VARCHAR(20), "
            lstrsql = lstrsql & "DESCONTORS VARCHAR(20), "
            lstrsql = lstrsql & "RECEITARS  VARCHAR(20), "
            lstrsql = lstrsql & "ENCARGORS VARCHAR(20), "
            lstrsql = lstrsql & "REFATURAMENTO CHAR(1))"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "DROP TABLE IF EXISTS AUX_SAMP_NOVO_IMPRESSAOA_CONVENCIONAL"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "create table IF NOT EXISTS AUX_SAMP_NOVO_IMPRESSAOA_CONVENCIONAL("
            lstrsql = lstrsql & "CLASSE  VARCHAR(50), "
            lstrsql = lstrsql & "NCONS   VARCHAR(20), "
            lstrsql = lstrsql & "KWH     VARCHAR(20), "
            lstrsql = lstrsql & "KW      VARCHAR(20), "
            lstrsql = lstrsql & "KWULT   VARCHAR(20), "
            lstrsql = lstrsql & "KWHRS   VARCHAR(20), "
            lstrsql = lstrsql & "BANDEIRARS VARCHAR(20), "
            lstrsql = lstrsql & "RECEITA VARCHAR(20), "
            lstrsql = lstrsql & "KWRS    VARCHAR(20), "
            lstrsql = lstrsql & "KWULTRS VARCHAR(20), "
            lstrsql = lstrsql & "PIS     VARCHAR(20), "
            lstrsql = lstrsql & "ICMS    VARCHAR(20), "
            lstrsql = lstrsql & "COFINS  VARCHAR(20), "
            lstrsql = lstrsql & "REFATURAMENTO CHAR(1))"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "DROP TABLE IF EXISTS AUX_SAMP_NOVO_IMPRESSAOA_HOROSSAZONAL"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "create table IF NOT EXISTS AUX_SAMP_NOVO_IMPRESSAOA_HOROSSAZONAL("
            lstrsql = lstrsql & "CLASSE VARCHAR(50),"
            lstrsql = lstrsql & "NCONS  VARCHAR(20),"
            lstrsql = lstrsql & "KWHPONTA    VARCHAR(20),"
            lstrsql = lstrsql & "KWHFORAP  VARCHAR(20),"
            lstrsql = lstrsql & "KWPONTA VARCHAR(20),"
            lstrsql = lstrsql & "KWFORAP VARCHAR(20),"
            lstrsql = lstrsql & "KWULTPONTA VARCHAR(20),"
            lstrsql = lstrsql & "KWULTFORAP   VARCHAR(20),"
            lstrsql = lstrsql & "KWHRSPONTA     VARCHAR(20),"
            lstrsql = lstrsql & "KWHRSFORAP    VARCHAR(20),"
            lstrsql = lstrsql & "BANDEIRAPONTA VARCHAR(20),"
            lstrsql = lstrsql & "BANDEIRAFORAP VARCHAR(20),"
            lstrsql = lstrsql & "RECEITAPONTA VARCHAR(20),"
            lstrsql = lstrsql & "RECEITAFORAP VARCHAR(20),"
            lstrsql = lstrsql & "KWRSPONTA VARCHAR(20),"
            lstrsql = lstrsql & "KWRSFORAP VARCHAR(20),"
            lstrsql = lstrsql & "KWRSULTPONTA VARCHAR(20),"
            lstrsql = lstrsql & "KWRSULTFORAP VARCHAR(20),"
            lstrsql = lstrsql & "ICMS VARCHAR(20),"
            lstrsql = lstrsql & "PIS VARCHAR(20),"
            lstrsql = lstrsql & "COFINS VARCHAR(20),"
            lstrsql = lstrsql & "REFATURAMENTO CHAR(1))"

            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)


            lstrsql = ""
            lstrsql = lstrsql & "DROP TABLE IF EXISTS AUX_SAMP_NOVO_IMPRESSAOA_LIVRE"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "create table IF NOT EXISTS AUX_SAMP_NOVO_IMPRESSAOA_LIVRE("
            lstrsql = lstrsql & "CLASSE VARCHAR(50),"
            lstrsql = lstrsql & "NCONS  VARCHAR(20),"
            lstrsql = lstrsql & "DESCPROV  VARCHAR(20),"
            lstrsql = lstrsql & "DESCREAL  VARCHAR(20),"
            lstrsql = lstrsql & "DEVCCEE  VARCHAR(20),"
            lstrsql = lstrsql & "COBCCEE  VARCHAR(20),"
            lstrsql = lstrsql & "DESCPROV_KWH  VARCHAR(20),"
            lstrsql = lstrsql & "DESCREAL_KWH  VARCHAR(20),"
            lstrsql = lstrsql & "DEVCCEE_KWH  VARCHAR(20),"
            lstrsql = lstrsql & "COBCCEE_KWH  VARCHAR(20),"
            lstrsql = lstrsql & "KWHPONTA    VARCHAR(20),"
            lstrsql = lstrsql & "KWHFORAP  VARCHAR(20),"
            lstrsql = lstrsql & "KWPONTA VARCHAR(20),"
            lstrsql = lstrsql & "KWFORAP VARCHAR(20),"
            lstrsql = lstrsql & "KWULTPONTA VARCHAR(20),"
            lstrsql = lstrsql & "KWULTFORAP   VARCHAR(20),"
            lstrsql = lstrsql & "KWHRSPONTA     VARCHAR(20),"
            lstrsql = lstrsql & "KWHRSFORAP    VARCHAR(20),"
            lstrsql = lstrsql & "BANDEIRAPONTA VARCHAR(20),"
            lstrsql = lstrsql & "BANDEIRAFORAP VARCHAR(20),"
            lstrsql = lstrsql & "RECEITAPONTA VARCHAR(20),"
            lstrsql = lstrsql & "RECEITAFORAP VARCHAR(20),"
            lstrsql = lstrsql & "KWRSPONTA VARCHAR(20),"
            lstrsql = lstrsql & "KWRSFORAP VARCHAR(20),"
            lstrsql = lstrsql & "KWRSULTPONTA VARCHAR(20),"
            lstrsql = lstrsql & "KWRSULTFORAP VARCHAR(20),"
            lstrsql = lstrsql & "ENCARGOPONTA VARCHAR(20),"
            lstrsql = lstrsql & "ENCARGOFORAP VARCHAR(20),"
            lstrsql = lstrsql & "ENCARGORSPONTA VARCHAR(20),"
            lstrsql = lstrsql & "ENCARGORSFORAP VARCHAR(20),"
            lstrsql = lstrsql & "ICMS VARCHAR(20),"
            lstrsql = lstrsql & "PIS VARCHAR(20),"
            lstrsql = lstrsql & "COFINS VARCHAR(20),"
            lstrsql = lstrsql & "REFATURAMENTO CHAR(1))"

            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

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

    End Sub

    Private Sub sDeletaTabelasAuxiliares()

        'AUX_SAMP_NOVO_IMPRESSAOA_CONVENCIONAL
        'AUX_SAMP_NOVO_IMPRESSAO_USINAS
        'AUX_SAMP_NOVO_IMPRESSAOB_TB
        'AUX_SAMP_NOVO_IMPRESSAOB_IA
        'AUX_SAMP_NOVO_IMPRESSAOB_MG
        'AUX_SAMP_NOVO_IMPRESSAOB
        'AUX_SAMP_NOVO_BAIXA_RENDA
        'AUX_SAMP_NOVO_IMPRESSAOA_HOROSSAZONAL
        'AUX_SAMP_NOVO_IMPRESSAOA_LIVRE

        Try

            objExecAtualizaBancoDados.QUERY_NAME = "delete from AUX_SAMP_NOVO_BAIXA_RENDA"
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            objExecAtualizaBancoDados.QUERY_NAME = "delete from AUX_SAMP_NOVO_IMPRESSAOB"
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            objExecAtualizaBancoDados.QUERY_NAME = "delete from AUX_SAMP_NOVO_IMPRESSAOB_MG"
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            objExecAtualizaBancoDados.QUERY_NAME = "delete from AUX_SAMP_NOVO_IMPRESSAOB_IA"
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            objExecAtualizaBancoDados.QUERY_NAME = "delete from AUX_SAMP_NOVO_IMPRESSAOB_TB"
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            objExecAtualizaBancoDados.QUERY_NAME = "delete from AUX_SAMP_NOVO_IMPRESSAOA_CONVENCIONAL"
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            objExecAtualizaBancoDados.QUERY_NAME = "delete from AUX_SAMP_NOVO_IMPRESSAO_USINAS"
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            objExecAtualizaBancoDados.QUERY_NAME = "delete from AUX_SAMP_NOVO_IMPRESSAOA_HOROSSAZONAL"
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            objExecAtualizaBancoDados.QUERY_NAME = "delete from AUX_SAMP_NOVO_IMPRESSAOA_LIVRE"
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

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

    End Sub

End Class