Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE
Imports CrystalDecisions.CrystalReports.Engine
Friend Class frmRelEstOperacao
	Inherits System.Windows.Forms.Form
	
	Private strTabela As String
	Private istrSql As String
	'Private fr As New FrmRelatorio
	Private strTamCol As String
	Private istrTitulo As String
	Private istrTitulo1 As String
	Private istrTitulo2 As String
	Private istrTitulo3 As String
	Private strClasseOld As String
	Private strClasse As String
	Private strNmClasse As String
    Private blnAnalitico As Boolean
    Private dt, dt1, dt2, dt3, dt4 As DataTable
    Private objAtualizaBancoDados As New entidadeAtualizaBancoDados
    Private repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados

    Private objExecAtualizaBancoDados As New entidadeAtualizaBancoDados
    Private repositorioExecAtualizaBancoDados As New repositorioAtualizaBancoDados
    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 bValidacao As Boolean

    Private Sub frmRelEstOperacao_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        Try
            Me.DTPInicial.Value = Today
            Me.DTPFinal.Value = Today
            Me.DTPMesAno.Value = Today
            Call CarregaClasses((Me.lvsClasses))
            Call CarregaCidades((Me.lvsCidades))
            istrTitulo1 = gstrEmpresa_Nome
            strTabela = "temprelestop_" & My.Computer.Name
            sCriaTabelaAuxiliar()

            Me.DTPMesAno.Enabled = True
            Me.DTPInicial.Enabled = False
            Me.DTPFinal.Enabled = False
            Me.Label1(0).Enabled = False

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmRelEstOperacao_Load" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub
    Private Sub cmdEmitir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEmitir.Click
        Try

            If MontaTabela() Then
                Call ImprimeRelatorio()
                Call sDadosEmpresa(LImagem, sNomeEmpresa, sEnderecoEmpresa, sCepEmpresa, sCidadeEmpresa, sUFempresa, sBairroEmpresa, sFoneEmpresa, sWebEmpresa)
                If blnAnalitico Then
                    lPrint("rptRelEstatOper_analitico")
                Else
                    lPrint("rptRelEstatOper")
                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdEmitir_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 Function MontaTabela() As Boolean
        Dim strfiltro As String
        Dim strfiltro2 As String
        Dim strFiltroCidade As String
        Dim strColuna As String
        Dim strItens As String
        Dim strZona As String
        Dim strLivro As String
        Dim strConta As String
        Dim lstrGrTar As String
        Dim strGrupo As String
        Dim strNome As String
        Dim lngUnidId As Integer
        Dim lngConsId As Integer
        Dim dblQkwhCon As Double
        Dim dblQKwhFat As Double
        Dim dblVEnergia As Double
        Dim dblVDemanda As Double
        Dim dblVReativo As Double

        Dim ldblQConsumoP As Double
        Dim ldblQConsumoFP As Double
        Dim ldblVConsumoP As Double
        Dim ldblVConsumoFP As Double

        Dim ldblQReversoP As Double
        Dim ldblQReversoFP As Double
        Dim ldblVReversoP As Double
        Dim ldblVReversoFP As Double
        Dim lintLivreEspecial As Short
        Dim lintParcialmenteCativo As Short
        Dim lintMicroGeracao As Short
        Dim lintMicroGeracaoDistribuida As Short
        Dim intConta As Short
        ' Dim rsFatura As ADODB.Recordset
        'Dim rsConsulta As ADODB.Recordset

        objExecAtualizaBancoDados.QUERY_NAME = "delete from " & strTabela
        repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

        objExecAtualizaBancoDados.QUERY_NAME = "delete from " & strTabela & "_itens"
        repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

        objExecAtualizaBancoDados.QUERY_NAME = "delete from " & strTabela & "Analitico"
        repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

        Try

            MontaTabela = True
            istrSql = ""
            istrSql = istrSql & "SELECT codclas, itensserv, zona, livro, conta, cons_id, unid_id, ta, numnf, mesano, mesano_ref, livreespecial, parcialmente_cativo, micro_geracao, micro_geracao_distribuida "
            istrSql = istrSql & "  FROM nf "
            istrSql = istrSql & " WHERE "
            'Se for soment A ou se for somente B ou se for A e B
            If Me.chkGrupoA.CheckState And Not Me.chkGrupoB.CheckState Then
                istrSql = istrSql & " grupo = 'A' AND "
            ElseIf Me.chkGrupoB.CheckState And Not Me.chkGrupoA.CheckState Then
                istrSql = istrSql & " grupo = 'B' AND "
            End If
            strfiltro = ""
            strfiltro2 = ""

            With Me.lvsClasses
                If .Items.Item(1).Checked = False Then
                    strfiltro = "("
                    For intConta = 2 To .Items.Count - 1
                        If .Items.Item(intConta).Checked = True Then
                            strfiltro = strfiltro & " LEFT(codclas," & Len(Trim(Str_Retira(.Items.Item(intConta).SubItems(1).Text, "<="))) & ")" & .Items.Item(intConta).SubItems(1).Text & " OR "
                            If Trim(.Items.Item(intConta).Text) = "Industrial" And .Items.Item(4).Checked = False Then
                                strfiltro2 = " codclas <> '0100' AND "
                            Else
                                strfiltro2 = ""
                            End If
                        End If
                    Next intConta
                    If strfiltro = "(" Then
                        Call MsgBox("Faltou marcar ao menos uma classe!", MsgBoxStyle.Information, gstrTituloMensagem)
                        Me.lvsClasses.Focus()
                        MontaTabela = False
                        Exit Function
                    End If
                    strfiltro = VB.Left(strfiltro, Len(strfiltro) - 3) & ") AND "
                Else
                    strfiltro = ""
                    strfiltro2 = ""
                End If
            End With
            istrSql = istrSql & strfiltro & strfiltro2

            If Me.opbMesAnoFat.Checked Then
                If Me.opbMesAno.Checked Then
                    istrSql = istrSql & " mesano = ('" & VB6.Format(Month(Me.DTPMesAno.Value), "00") & "/" & Year(Me.DTPMesAno.Value) & "') AND "
                Else
                    istrSql = istrSql & " (mesano_ref >= " & Year(Me.DTPMesAno.Value) & VB6.Format(Month(Me.DTPMesAno.Value), "00") & ") AND "
                End If
            Else
                If Me.opbDtVencto.Checked Then
                    strColuna = "datavenc"
                ElseIf Me.opbDtLeitAnt.Checked Then
                    strColuna = "dataleitant"
                ElseIf Me.opbDtLeitAtu.Checked Then
                    strColuna = "dataleit"
                ElseIf Me.opbDtApres.Checked Then
                    strColuna = "dataapr"
                ElseIf Me.opbDtEmissao.Checked Then
                    strColuna = "dataem"
                ElseIf Me.opbDtPagto.Checked Then
                    strColuna = "datapag"
                End If
                If Me.opbMesAno.Checked Then
                    istrSql = istrSql & " CONCAT(MONTH(" & strColuna & "),""/"",YEAR(" & strColuna & ")) = (""" & VB6.Format(Month(Me.DTPMesAno.Value), "00") & "/" & Year(Me.DTPMesAno.Value) & """) AND "
                Else
                    istrSql = istrSql & " (" & strColuna & " >= '" & VB6.Format(Me.DTPInicial.Value, "yyyy-MM-dd") & "' AND "
                    istrSql = istrSql & " " & strColuna & " <= '" & VB6.Format(Me.DTPFinal.Value, "yyyy-MM-dd") & "') AND "
                End If
            End If
            strFiltroCidade = MontaFiltro(Me.lvsCidades, 2)
            If Len(Trim(strFiltroCidade)) > 0 Then
                istrSql = istrSql & " zona IN " & strFiltroCidade & " AND "
            End If
            istrSql = VB.Left(istrSql, Len(istrSql) - 4)
            Me.lblStatus1.Text = "Buscando as faturas..."
            Me.lblStatus1.Refresh()

            objAtualizaBancoDados.QUERY_NAME = istrSql
            dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)
            'Call Conectar(istrSql, rsFatura)


            If dt.Rows.Count > 0 Then
                Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, dt.Rows.Count)
                For Each row As DataRow In dt.Rows
                    lintLivreEspecial = IIf(IsDBNull(row.Item("livreespecial")), 0, row.Item("livreespecial"))
                    lintParcialmenteCativo = IIf(IsDBNull(row.Item("parcialmente_cativo")), 0, row.Item("parcialmente_cativo"))
                    lintMicroGeracao = IIf(IsDBNull(row.Item("micro_geracao")), 0, row.Item("micro_geracao"))
                    lintMicroGeracaoDistribuida = IIf(IsDBNull(row.Item("micro_geracao_distribuida")), 0, row.Item("micro_geracao_distribuida"))
                    strClasse = IIf(IsDBNull(row.Item("codclas")), "", row.Item("codclas"))
                    strNmClasse = Replace(PegaNomeClasse(strClasse), ";", ":")
                    strItens = IIf(IsDBNull(row.Item("itensserv")), "", row.Item("itensserv"))
                    strZona = IIf(IsDBNull(row.Item("zona")), "", row.Item("zona"))
                    strLivro = IIf(IsDBNull(row.Item("livro")), "", row.Item("livro"))
                    strConta = IIf(IsDBNull(row.Item("conta")), "", row.Item("conta"))
                    lngConsId = IIf(IsDBNull(row.Item("cons_id")), 0, row.Item("cons_id"))
                    lngUnidId = IIf(IsDBNull(row.Item("unid_id")), 0, row.Item("unid_id"))
                    lstrGrTar = IIf(IsDBNull(row.Item("ta")), "", row.Item("ta"))
                    strGrupo = IIf(Mid(lstrGrTar, 3, 1) = "A", Mid(lstrGrTar, 3, 2), Mid(lstrGrTar, 3, 1))
                    dblQkwhCon = 0
                    dblQKwhFat = 0
                    dblVEnergia = 0
                    dblVDemanda = 0
                    dblVReativo = 0

                    If strGrupo = "A4" Then
                        dblQkwhCon = dblQkwhCon + BuscaVlrItem(strItens, "0302010008") 'Consumo lido
                        dblQKwhFat = dblQKwhFat + BuscaVlrItem(strItens, "0302010009") 'Consumo faturado
                        dblVEnergia = dblVEnergia + BuscaVlrItem(strItens, "0302020009") 'Valor Consumo faturado
                        dblVReativo = dblVReativo + BuscaVlrItem(strItens, "0302020012") 'Valor Consumo Reativo
                        dblVReativo = dblVReativo + BuscaVlrItem(strItens, "0302020031") 'Valor Demanda Reativo
                        dblVDemanda = dblVDemanda + BuscaVlrItem(strItens, "0302020015") 'Valor Demanda
                        dblVDemanda = dblVDemanda + BuscaVlrItem(strItens, "0302020018") 'Valor Demanda de Ultrapassagem
                    ElseIf strGrupo = "B" Then
                        dblQkwhCon = dblQkwhCon + BuscaVlrItem(strItens, "030201008") + BuscaVlrItem(strItens, "030201018") + BuscaVlrItem(strItens, "030201028") + BuscaVlrItem(strItens, "030201038") 'Consumo lido
                        dblQKwhFat = dblQKwhFat + BuscaVlrItem(strItens, "030201009") + BuscaVlrItem(strItens, "030201019") + BuscaVlrItem(strItens, "030201029") + BuscaVlrItem(strItens, "030201039") 'Consumo faturado
                        dblVEnergia = dblVEnergia + BuscaVlrItem(strItens, "030202009") + BuscaVlrItem(strItens, "030202019") + BuscaVlrItem(strItens, "030202029") + BuscaVlrItem(strItens, "030202039") 'Valor Consumo faturado
                        dblVReativo = dblVReativo + BuscaVlrItem(strItens, "0302020012") 'Valor Consumo Reativo
                    Else
                        ldblQReversoP = BuscaVlrItem(strItens, "0302012010")
                        ldblQConsumoP = BuscaVlrItem(strItens, "0302010010")
                        ldblQReversoFP = BuscaVlrItem(strItens, "0302012011")
                        ldblQConsumoFP = BuscaVlrItem(strItens, "0302010011")

                        If ldblQConsumoP <= ldblQReversoP Then
                            ldblQReversoP = ldblQConsumoP
                            ldblQConsumoP = 0
                        Else
                            ldblQConsumoP = ldblQConsumoP - ldblQReversoP
                        End If

                        If ldblQConsumoFP <= ldblQReversoFP Then
                            ldblQReversoFP = ldblQConsumoFP
                            ldblQConsumoFP = 0
                        Else
                            ldblQConsumoFP = ldblQConsumoFP - ldblQReversoFP
                        End If


                        dblQkwhCon = dblQkwhCon + ldblQConsumoP 'Consumo faturado p
                        dblQkwhCon = dblQkwhCon + ldblQConsumoFP 'Consumo faturado fp
                        dblQKwhFat = dblQKwhFat + ldblQConsumoP 'Consumo faturado p
                        dblQKwhFat = dblQKwhFat + ldblQConsumoFP 'Consumo faturado fp

                        dblVEnergia = dblVEnergia + BuscaVlrItem(strItens, "0302020010") 'Valor Consumo faturado p
                        dblVEnergia = dblVEnergia + BuscaVlrItem(strItens, "0302020011") 'Valor Consumo faturado fp
                        dblVDemanda = dblVDemanda + BuscaVlrItem(strItens, "0302020021") 'Valor Demanda p
                        dblVDemanda = dblVDemanda + BuscaVlrItem(strItens, "0302020023") '+ BuscaVlrItem(strItens, "0302020072") 'Valor Demanda fp
                        dblVDemanda = dblVDemanda + BuscaVlrItem(strItens, "0302020022") 'Valor Demanda de ultrapassagem p
                        dblVDemanda = dblVDemanda + BuscaVlrItem(strItens, "0302020026") 'Valor Demanda de ultrapassagem fp
                        dblVReativo = dblVReativo + BuscaVlrItem(strItens, "0302020013") 'Valor Consumo Reativo p
                        dblVReativo = dblVReativo + BuscaVlrItem(strItens, "0302020014") 'Valor Consumo Reativo fp
                        dblVReativo = dblVReativo + BuscaVlrItem(strItens, "0302020027") 'Valor Demanda Reativo p
                        dblVReativo = dblVReativo + BuscaVlrItem(strItens, "0302020028") 'Valor Demanda Reativo fp
                    End If

                    'Busca o nome do cabra na unidade consumidora se existir senao entao tenta pega somente o nome do consumidor
                    istrSql = "SELECT nome " & "  FROM uc_faturamento WHERE cons_id = " & lngConsId & "   AND id = " & lngUnidId
                    objAtualizaBancoDados.QUERY_NAME = istrSql
                    dt1 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)
                    ' Call Conectar(istrSql, rsConsulta)

                    If dt1.Rows.Count > 0 Then
                        For Each row1 As DataRow In dt1.Rows
                            strNome = IIf(IsDBNull(row1.Item("nome")), "NOME NÃO INFORMADO", row1.Item("nome"))
                        Next
                    Else
                        ' If rsConsulta.State = 1 Then Call rsConsulta.Close()

                        istrSql = "SELECT nome " & "  FROM consum " & " WHERE consreg = " & lngConsId
                        ' Call Conectar(istrSql, rsConsulta)
                        objAtualizaBancoDados.QUERY_NAME = istrSql
                        dt2 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

                        If dt2.Rows.Count > 0 Then
                            For Each row2 As DataRow In dt2.Rows
                                strNome = IIf(IsDBNull(row2.Item("nome")), "NOME NÃO INFORMADO", row2.Item("nome"))
                            Next
                        Else
                            strNome = "UNIDADE CONSUMIDORA EXCLUIDA "
                        End If
                    End If
                    'If rsConsulta.State = 1 Then Call rsConsulta.Close()

                    istrSql = "INSERT INTO " & strTabela & " (classe, nmclasse, qkwh_cons, qkwh_fat, vkwh, vkw, vkvarh, nome, zona, livro, conta, grupo) VALUES (""" & strClasse & """, """ & strNmClasse & """, " & Replace(CStr(dblQkwhCon), ",", ".") & ", " & Replace(CStr(dblQKwhFat), ",", ".") & ", " & Replace(CStr(dblVEnergia), ",", ".") & ", " & Replace(CStr(dblVDemanda), ",", ".") & ", " & Replace(CStr(dblVReativo), ",", ".") & ", """ & strNome & """, """ & strZona & """, """ & strLivro & """, """ & strConta & """, """ & IIf(strGrupo <> "AL", strGrupo, IIf(strGrupo = "AL", strGrupo & IIf(lintParcialmenteCativo = 1, "P", IIf(lintLivreEspecial = 1, "S", "")), "")) & """); "
                    objExecAtualizaBancoDados.QUERY_NAME = istrSql
                    repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

                    'Call rsFatura.MoveNext()
                    Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
                    System.Windows.Forms.Application.DoEvents()
                Next
            End If

            Me.lblStatus1.Text = ""
            Me.lblStatus1.Refresh()

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

    Private Sub ImprimeRelatorio()
        '   Dim rsConsulta As ADODB.Recordset
        Dim strTemp As String
        Dim lstrSql1 As String
        Dim lstrSql2 As String
        Dim lstrSql3 As String
        Dim lstrSql4 As String
        Dim lstrSql5 As String
        Dim lstrSql6 As String
        Dim lstrSql7 As String
        Dim lstrSql8 As String
        Dim strFiltro3 As String
        Dim blnStartDoc As Boolean
        Dim intConta As Short
        Dim intCidad As Short

        Try

            blnStartDoc = False
            If Me.opbMesAno.Checked Then
                If Me.opbMesAnoFat.Checked Then
                    istrTitulo3 = "Faturamento"
                ElseIf Me.opbDtVencto.Checked Then
                    istrTitulo3 = "Data de Vencimento"
                ElseIf Me.opbDtLeitAnt.Checked Then
                    istrTitulo3 = "Data da Leitura Anterior"
                ElseIf Me.opbDtLeitAtu.Checked Then
                    istrTitulo3 = "Data da Leitura Atual"
                ElseIf Me.opbDtApres.Checked Then
                    istrTitulo3 = "Data da Apresentação"
                ElseIf Me.opbDtEmissao.Checked Then
                    istrTitulo3 = "Data da Emissão"
                ElseIf Me.opbDtPagto.Checked Then
                    istrTitulo3 = "Data do Pagamento"
                End If
                istrTitulo3 = istrTitulo3 & " do Mês/Ano: " & VB6.Format(Me.DTPMesAno.Value, "MM/yyyy")
            ElseIf Me.opbPeriodo.Checked Then
                If Me.opbMesAnoFat.Checked Then
                    istrTitulo3 = "Faturamento"
                ElseIf Me.opbDtVencto.Checked Then
                    istrTitulo3 = "Data de Vencimento"
                ElseIf Me.opbDtLeitAnt.Checked Then
                    istrTitulo3 = "Data da Leitura Anterior"
                ElseIf Me.opbDtLeitAtu.Checked Then
                    istrTitulo3 = "Data da Leitura Atual"
                ElseIf Me.opbDtApres.Checked Then
                    istrTitulo3 = "Data da Apresentação"
                ElseIf Me.opbDtEmissao.Checked Then
                    istrTitulo3 = "Data da Emissão"
                ElseIf Me.opbDtPagto.Checked Then
                    istrTitulo3 = "Data do Pagamento"
                End If
                istrTitulo3 = istrTitulo3 & " do Período de: " & VB6.Format(Me.DTPInicial.Value, "dd/mm/yyyy") & " até " & VB6.Format(Me.DTPFinal.Value, "dd/mm/yyyy")
            End If
            Me.lblStatus1.Text = "Imprimindo..."
            Me.lblStatus1.Refresh()

            For intCidad = 1 To Me.lvsCidades.Items.Count - 1

                If Me.lvsCidades.Items.Item(intCidad).Checked Then
                    lstrSql7 = " WHERE zona= """ & Me.lvsCidades.Items.Item(intCidad).SubItems(1).Text & """ AND "
                    If Me.chkGrupoAB.CheckState Then
                        lstrSql8 = " WHERE "
                    End If
                    For intConta = 2 To Me.lvsClasses.Items.Count - 1

                        If Me.lvsClasses.Items.Item(intConta).Checked Then
                            istrTitulo2 = "Consumo de Energia do " & Me.lvsClasses.Items.Item(intConta).Text
                            istrSql = " LEFT(classe," & Len(Trim(Str_Retira(Me.lvsClasses.Items.Item(intConta).SubItems(1).Text, "<="))) & ") " & Me.lvsClasses.Items.Item(intConta).SubItems(1).Text & " "

                            If Trim(Me.lvsClasses.Items.Item(intConta).Text) = "Industrial" And Me.lvsClasses.Items.Item(4).Checked = False Then
                                istrSql = istrSql & " AND TRIM(classe) <> '0100' "
                            End If

                            If Me.opbSintetico.Checked Then
                                lstrSql1 = "SELECT count(*) qcons, SUM(qkwh_cons) qkwh_cons, SUM(qkwh_fat) qkwh_fat, SUM(vkwh) vkwh, SUM(vkw) vkw, SUM(vkvarh) vkvarh, classe, nmclasse, grupo "
                                lstrSql3 = " GROUP BY grupo, classe ORDER BY grupo ASC, classe ASC"

                                lstrSql2 = "SELECT count(*) qcons, SUM(qkwh_cons) qkwh_cons, SUM(qkwh_fat) qkwh_fat, SUM(vkwh) vkwh, SUM(vkw) vkw, SUM(vkvarh) vkvarh, classe, nmclasse "
                                lstrSql4 = " GROUP BY classe ORDER BY classe ASC "
                            End If

                            If Me.opbAnalitico.Checked Then
                                lstrSql5 = "SELECT 1 qcons, qkwh_cons, qkwh_fat, vkwh, vkw, vkvarh, classe, nmclasse, grupo, nome, zona, livro, conta "
                                lstrSql6 = " ORDER BY classe ASC, grupo ASC, zona ASC, livro ASC, conta ASC "
                            End If

                            Dim lstrsql As String
                            If Me.opbSintetico.Checked Then
                                'Sintético A
                                If Me.chkGrupoA.CheckState Then

                                    'Somente os Convencionais
                                    lstrsql = lstrSql1 & "  FROM " & strTabela & lstrSql7 & istrSql & " AND grupo = 'A4'" & lstrSql3
                                    istrTitulo = istrTitulo2 & " - Convencional / " & Me.lvsCidades.Items.Item(intCidad).Text
                                    blnAnalitico = False
                                    Call MontaDetalhe(lstrsql, intConta, blnStartDoc)

                                    'Somente os Horosazonais Verde
                                    lstrsql = lstrSql1 & "  FROM " & strTabela & lstrSql7 & istrSql & " AND grupo = 'AV'" & lstrSql3
                                    istrTitulo = istrTitulo2 & " - Horo Sazonal Verde / " & Me.lvsCidades.Items.Item(intCidad).Text
                                    blnAnalitico = False
                                    Call MontaDetalhe(lstrsql, intConta, blnStartDoc)

                                    'Somente os Horosazonais Azul
                                    lstrsql = lstrSql1 & "  FROM " & strTabela & lstrSql7 & istrSql & " AND grupo = 'AA'" & lstrSql3
                                    istrTitulo = istrTitulo2 & " - Horo Sazonal Azul / " & Me.lvsCidades.Items.Item(intCidad).Text
                                    blnAnalitico = False
                                    Call MontaDetalhe(lstrsql, intConta, blnStartDoc)

                                    'Somente os Auto Produtor
                                    lstrsql = lstrSql1 & "  FROM " & strTabela & lstrSql7 & istrSql & " AND grupo = 'AP'" & lstrSql3
                                    istrTitulo = istrTitulo2 & " - Produtor / " & Me.lvsCidades.Items.Item(intCidad).Text
                                    blnAnalitico = False
                                    Call MontaDetalhe(lstrsql, intConta, blnStartDoc)

                                    If Trim(Me.lvsClasses.Items.Item(intConta).Text) = "Livre" Then

                                        'Somente o Livre normal
                                        lstrsql = lstrSql1 & "  FROM " & strTabela & lstrSql7 & istrSql & " AND grupo = 'AL'" & lstrSql3
                                        istrTitulo = istrTitulo2 & " - Livre / " & Me.lvsCidades.Items.Item(intCidad).Text
                                        blnAnalitico = False
                                        Call MontaDetalhe(lstrsql, intConta, blnStartDoc)

                                        lstrsql = lstrSql1 & "  FROM " & strTabela & lstrSql7 & istrSql & " AND grupo = 'ALS'" & lstrSql3
                                        istrTitulo = istrTitulo2 & " - Livre Especial / " & Me.lvsCidades.Items.Item(intCidad).Text
                                        blnAnalitico = False
                                        Call MontaDetalhe(lstrsql, intConta, blnStartDoc)

                                        'Somente o Livre parcialmente cativo
                                        lstrsql = lstrSql1 & "  FROM " & strTabela & lstrSql7 & istrSql & " AND grupo = 'ALP'" & lstrSql3
                                        istrTitulo = istrTitulo2 & " - Livre Parc.Cat. / " & Me.lvsCidades.Items.Item(intCidad).Text
                                        blnAnalitico = False
                                        Call MontaDetalhe(lstrsql, intConta, blnStartDoc)

                                    End If
                                End If
                                '---------------
                                'Sintético B
                                If Me.chkGrupoB.CheckState Then

                                    lstrsql = lstrSql1 & "  FROM " & strTabela & lstrSql7 & istrSql & "AND grupo = 'B'" & lstrSql3
                                    istrTitulo = istrTitulo2 & " - Grupo B / " & Me.lvsCidades.Items.Item(intCidad).Text
                                    blnAnalitico = False
                                    Call MontaDetalhe(lstrsql, intConta, blnStartDoc)

                                End If
                                '--------------
                                'Sintético A+B
                                If Me.chkGrupoAB.CheckState Then

                                    lstrsql = lstrSql2 & "  FROM " & strTabela & lstrSql8 & istrSql & lstrSql4
                                    istrTitulo = istrTitulo2 & " - (A + B) " & IIf(Me.opbConsumo.Checked, Chr(13) & "pelo Consumido Lido ", Chr(13) & "pelo Consumido Faturado")
                                    blnAnalitico = False
                                    Call MontaDetalhe(lstrsql, intConta, blnStartDoc)

                                    intCidad = Me.lvsCidades.Items.Count - 1
                                End If
                                '--------------
                            End If

                            'Análitico A+B
                            If Me.opbAnalitico.Checked Then

                                lstrsql = lstrSql5 & "  FROM " & strTabela & " WHERE " & istrSql & lstrSql6
                                istrTitulo = istrTitulo2 & " - (A + B) " & IIf(Me.opbConsumo.Checked, Chr(13) & "pelo Consumido Lido", Chr(13) & "pelo Consumido Faturado")
                                blnAnalitico = True
                                Call MontaDetalhe(lstrsql, intConta, blnStartDoc)

                                intCidad = Me.lvsCidades.Items.Count - 1
                            End If
                            '--------------
                        End If
                    Next intConta
                End If
            Next intCidad

            Me.lblStatus1.Text = ""
            Me.lblStatus1.Refresh()

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

    'Private Sub MontaDetalhe(ByRef arsConsulta As ADODB.Recordset, ByVal aintConta As Short, ByRef ablnStartDoc As Boolean)
    Private Sub MontaDetalhe(ByVal SQL As String, ByVal aintConta As Short, ByRef ablnStartDoc As Boolean)
        Dim dblQCons As Double
        Dim dblQKwhCons As Double
        Dim dblQKwhFat As Double
        Dim dblVKwhFat As Double
        Dim dblVKwFat As Double
        Dim dblVKvarhFat As Double
        Dim dblAuxiliar As Double

        'Total Geral
        Dim dblTQCons As Double
        Dim dblTQKwhCons As Double
        Dim dblTQKwhFat As Double
        Dim dblTVKwhFat As Double
        Dim dblTVKwFat As Double
        Dim dblTVKvarhFat As Double

        'Sub-Total de Cada Classe
        Dim dblSQCons As Double
        Dim dblSQKwhCons As Double
        Dim dblSQKwhFat As Double
        Dim dblSVKwhFat As Double
        Dim dblSVKwFat As Double
        Dim dblSVKvarhFat As Double

        Dim strGrupo As String
        Dim strNome As String
        Dim strZona As String
        Dim strLivro As String
        Dim strConta As String
        Dim strTemp As String
        Dim strClasseAnt As String

        Dim lngCrtlPag As Integer
        Dim lstrsql As String

        Try
            'If aintConta > 2 Then
            '  fr.VS.NewPage()
            'End If
            strClasseAnt = ""
            strClasseOld = ""

            dblTQCons = 0
            dblTQKwhCons = 0
            dblTQKwhFat = 0
            dblTVKwhFat = 0
            dblTVKwFat = 0
            dblTVKvarhFat = 0

            dblSQCons = 0
            dblSQKwhCons = 0
            dblSQKwhFat = 0
            dblSVKwhFat = 0
            dblSVKwFat = 0
            dblSVKvarhFat = 0

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

            If dt.Rows.Count > 0 Then
                For Each row As DataRow In dt.Rows
                    ''' While Not arsConsulta.EOF

                    dblQCons = IIf(IsDBNull(row.Item("qcons")), 0, row.Item("qcons")) 'Quantidade de Consumidores
                    dblQKwhCons = IIf(IsDBNull(row.Item("qkwh_cons")), 0, row.Item("qkwh_cons")) 'Consumo Lido
                    dblQKwhFat = IIf(IsDBNull(row.Item("qkwh_fat")), 0, row.Item("qkwh_fat")) 'Consumo Faturado
                    dblVKwhFat = IIf(IsDBNull(row.Item("vkwh")), 0, row.Item("vkwh")) 'Valor Consumo Faturado
                    dblVKwFat = IIf(IsDBNull(row.Item("vkw")), 0, row.Item("vkw")) 'Valor Demanda
                    dblVKvarhFat = IIf(IsDBNull(row.Item("vkvarh")), 0, row.Item("vkvarh")) 'Valor Reativo
                    strClasse = IIf(IsDBNull(row.Item("classe")), 0, row.Item("classe")) 'Classe
                    strNmClasse = IIf(IsDBNull(row.Item("nmclasse")), "", row.Item("nmclasse")) 'Nome da Classe

                    dblTQCons = dblTQCons + dblQCons
                    dblTQKwhCons = dblTQKwhCons + dblQKwhCons
                    dblTQKwhFat = dblTQKwhFat + dblQKwhFat
                    dblTVKwhFat = dblTVKwhFat + dblVKwhFat
                    dblTVKwFat = dblTVKwFat + dblVKwFat
                    dblTVKvarhFat = dblTVKvarhFat + dblVKvarhFat

                    If ablnStartDoc = False Then
                        ' fr.VS.StartDoc()
                        ablnStartDoc = True
                    End If

                    If blnAnalitico Then
                        strNome = IIf(IsDBNull(row.Item("nome")), "", row.Item("nome")) 'Nome da Unidade Consumidora
                        strGrupo = IIf(IsDBNull(row.Item("grupo")), "", row.Item("grupo")) 'Grupo
                        strZona = IIf(IsDBNull(row.Item("zona")), "", row.Item("zona")) 'Zona
                        strLivro = IIf(IsDBNull(row.Item("livro")), "", row.Item("livro")) 'Livro
                        strConta = IIf(IsDBNull(row.Item("conta")), "", row.Item("conta")) 'Conta
                    End If

                    If blnAnalitico Then
                        If Trim(strClasseAnt) = "" Then strClasseAnt = strClasse
                        If strClasseAnt <> strClasse Then
                            '        fr.VS.TableBorder = tbBoxRows
                            'total de cada classe
                            dblAuxiliar = 0
                            If Me.chkKwh.CheckState Then dblAuxiliar = dblAuxiliar + dblSVKwhFat
                            If Me.chkKw.CheckState Then dblAuxiliar = dblAuxiliar + dblSVKwFat
                            If Me.chkKvarh.CheckState Then dblAuxiliar = dblAuxiliar + dblSVKvarhFat
                            'fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbBoxRows
                            'strTemp = "<44mm|>20mm|<30mm|>23mm|<38mm|>25mm;No. de Consumidores:|" & FormatNumber(dblSQCons, 0) & "|" & "Consumo Kwh:|" & FormatNumber(IIf(Me.opbConsumo.Checked, dblSQKwhCons, dblSQKwhFat), 0) & "|" & "Energia distribuida:|" & VB6.Format(dblAuxiliar, "standard")
                            ''UPGRADE_ISSUE: VBControlExtender method fr.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
                            'fr.VS.DrawLine("79mm", fr.VS.CurrentY, "79mm", fr.VS.CurrentY + 140)
                            ''UPGRADE_ISSUE: VBControlExtender method fr.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
                            'fr.VS.DrawLine("132mm", fr.VS.CurrentY, "132mm", fr.VS.CurrentY + 140)
                            'fr.VS.Table = strTemp
                            'fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
                            dblSQCons = 0
                            dblSQKwhCons = 0
                            dblSQKwhFat = 0
                            dblSVKwhFat = 0
                            dblSVKwFat = 0
                            dblSVKvarhFat = 0
                            strClasseAnt = strClasse
                            ' fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
                        End If
                        If strClasseOld <> strClasse Then
                            'fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
                            ''UPGRADE_ISSUE: VBControlExtender method fr.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
                            'fr.VS.Table = "<10mm;" & "|" & fr.VS.CurrentY
                            ''        fr.VS.TableBorder = tbAll
                            'fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, True)
                            'strTemp = VB.Left("CLASSE: (" & VB6.Format(strClasse, "0000") & ") - " & strNmClasse, 70)
                            'fr.VS.Table = "<180mm;" & strTemp
                            'fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, False)
                            'strClasseOld = strClasse
                            'fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, True)
                            'strTemp = "<30mm|<135mm|^15mm;Identificador|Nome|Grupo"
                            'fr.VS.Table = strTemp
                            'fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, False)
                        End If
                    Else
                        '      fr.VS.TableBorder = tbBoxRows
                        'fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, True)
                        'strTemp = VB.Left("CLASSE: (" & VB6.Format(strClasse, "0000") & ") - " & strNmClasse, 70)
                        'fr.VS.Table = "<180mm;" & strTemp
                        'fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, False)
                    End If

                    If Me.opbSintetico.Checked Then
                        dblAuxiliar = 0
                        If Me.chkKwh.CheckState Then dblAuxiliar = dblAuxiliar + dblVKwhFat
                        If Me.chkKw.CheckState Then dblAuxiliar = dblAuxiliar + dblVKwFat
                        If Me.chkKvarh.CheckState Then dblAuxiliar = dblAuxiliar + dblVKvarhFat
                        'fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbBoxRows
                        'strTemp = "<44mm|>20mm|<30mm|>23mm|<38mm|>25mm;No. de Consumidores:|" & FormatNumber(dblQCons, 0) & "|" & "Consumo Kwh:|" & FormatNumber(IIf(Me.opbConsumo.Checked, dblQKwhCons, dblQKwhFat), 0) & "|" & "Energia distribuida:|" & VB6.Format(dblAuxiliar, "standard")
                        ''UPGRADE_ISSUE: VBControlExtender method fr.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
                        'fr.VS.DrawLine("79mm", fr.VS.CurrentY, "79mm", fr.VS.CurrentY + 140)
                        '''UPGRADE_ISSUE: VBControlExtender method fr.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
                        'fr.VS.DrawLine("132mm", fr.VS.CurrentY, "132mm", fr.VS.CurrentY + 140)
                    End If
                    If blnAnalitico Then
                        '      fr.VS.TableBorder = tbAll
                        strTemp = "<30mm|<135mm|^15mm;"
                        strTemp = strTemp & strZona & "." & strLivro & "." & strConta & "|"
                        strTemp = strTemp & strNome & "|"
                        strTemp = strTemp & IIf(VB.Left(strGrupo, 1) = "B", "B", IIf(VB.Left(strGrupo, 2) = "AV", "Verde", IIf(VB.Left(strGrupo, 2) = "AA", "Azul", IIf(VB.Left(strGrupo, 2) = "A4", "Conv.", IIf(VB.Left(strGrupo, 2) = "AP", "Prod.", IIf(VB.Left(strGrupo, 2) = "AL", "Livre", ""))))))
                    End If
                    'fr.VS.Table = strTemp
                    'fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
                    If Me.opbSintetico.Checked Then
                        '      If fr.VS.CurrentLine <= 159 Then
                        'UPGRADE_ISSUE: VBControlExtender method fr.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
                        '  fr.VS.Table = "<10mm;" & "|" & fr.VS.CurrentY
                        '      End If
                    End If
                    ''' Call arsConsulta.MoveNext()
                    dblSQCons = dblSQCons + dblQCons
                    dblSQKwhCons = dblSQKwhCons + dblQKwhCons
                    dblSQKwhFat = dblSQKwhFat + dblQKwhFat
                    dblSVKwhFat = dblSVKwhFat + dblVKwhFat
                    dblSVKwFat = dblSVKwFat + dblVKwFat
                    dblSVKvarhFat = dblSVKvarhFat + dblVKvarhFat


                    If blnAnalitico Then

                        Dim sIdentificacao As String
                        sIdentificacao = strZona & "." & strLivro & "." & strConta

                        lstrsql = "INSERT INTO " & strTabela & "Analitico (TIPO, CLASSE, NMCLASSE, NCONS, QKWH,ZLC,NOME,GRUPO, ENERGIA_DISTRIBUIDA) VALUES (""" & istrTitulo & """,""" & strClasse & """, """ & strNmClasse & """, " & Replace(CStr(dblTQCons), ",", ".") & ", " & Replace(CStr(IIf(Me.opbConsumo.Checked, dblTQKwhCons, dblTQKwhFat)), ",", ".") & ", '" & sIdentificacao & "','" & strNome & "' ,'" & IIf(VB.Left(strGrupo, 1) = "B", "B", IIf(VB.Left(strGrupo, 2) = "AV", "Verde", IIf(VB.Left(strGrupo, 2) = "AA", "Azul", IIf(VB.Left(strGrupo, 2) = "A4", "Conv.", IIf(VB.Left(strGrupo, 2) = "AP", "Prod.", IIf(VB.Left(strGrupo, 2) = "AL", "Livre", "")))))) & "' ," & Replace(CStr(dblQKwhFat), ",", ".") & ") "
                        objExecAtualizaBancoDados.QUERY_NAME = lstrsql
                        repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
                    Else
                        lstrsql = "INSERT INTO " & strTabela & "_Itens (TIPO, CLASSE, NMCLASSE, NCONS, QKWH, ENERGIA_DISTRIBUIDA) VALUES (""" & istrTitulo & """,""" & strClasse & """, """ & strNmClasse & """, " & Replace(CStr(dblTQCons), ",", ".") & ", " & Replace(CStr(IIf(Me.opbConsumo.Checked, dblTQKwhCons, dblTQKwhFat)), ",", ".") & ", " & Replace(CStr(dblQKwhFat), ",", ".") & ") "
                        objExecAtualizaBancoDados.QUERY_NAME = lstrsql
                        repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
                    End If

                Next
                '''End While

            End If

            If blnAnalitico Then
                '    fr.VS.TableBorder = tbBoxRows
                dblAuxiliar = 0
                If Me.chkKwh.CheckState Then dblAuxiliar = dblAuxiliar + dblSVKwhFat
                If Me.chkKw.CheckState Then dblAuxiliar = dblAuxiliar + dblSVKwFat
                If Me.chkKvarh.CheckState Then dblAuxiliar = dblAuxiliar + dblSVKvarhFat
                'fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbBoxRows
                'strTemp = "<44mm|>20mm|<30mm|>23mm|<38mm|>25mm;No. de Consumidores:|" & FormatNumber(dblSQCons, 0) & "|" & "Consumo Kwh:|" & FormatNumber(IIf(Me.opbConsumo.Checked, dblSQKwhCons, dblSQKwhFat), 0) & "|" & "Energia distribuida:|" & VB6.Format(dblAuxiliar, "standard")
                ''UPGRADE_ISSUE: VBControlExtender method fr.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
                'fr.VS.DrawLine("79mm", fr.VS.CurrentY, "79mm", fr.VS.CurrentY + 140)
                ''UPGRADE_ISSUE: VBControlExtender method fr.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
                'fr.VS.DrawLine("132mm", fr.VS.CurrentY, "132mm", fr.VS.CurrentY + 140)
                'fr.VS.Table = strTemp
                'fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
                'dblSQCons = 0
                'dblSQKwhCons = 0
                'dblSQKwhFat = 0
                'dblSVKwhFat = 0
                'fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
            End If
            'fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
            'fr.VS.Table = "<10mm;" & "|"
            ''  fr.VS.TableBorder = tbBoxRows
            'fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, True)
            'strTemp = "TOTAL GERAL"
            'fr.VS.Table = "<180mm;" & strTemp
            'fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, False)
            '  fr.VS.TableBorder = tbBoxRows
            dblAuxiliar = 0
            If Me.chkKwh.CheckState Then dblAuxiliar = dblAuxiliar + dblTVKwhFat
            If Me.chkKw.CheckState Then dblAuxiliar = dblAuxiliar + dblTVKwFat
            If Me.chkKvarh.CheckState Then dblAuxiliar = dblAuxiliar + dblTVKvarhFat
            'fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbBoxRows
            'strTemp = "<44mm|>20mm|<30mm|>23mm|<38mm|>25mm;No. de Consumidores:|" & FormatNumber(dblTQCons, 0) & "|" & "Consumo Kwh:|" & FormatNumber(IIf(Me.opbConsumo.Checked, dblTQKwhCons, dblTQKwhFat), 0) & "|" & "Energia distribuida:|" & VB6.Format(dblAuxiliar, "standard")
            ''UPGRADE_ISSUE: VBControlExtender method fr.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
            'fr.VS.DrawLine("79mm", fr.VS.CurrentY, "79mm", fr.VS.CurrentY + 140)
            ''UPGRADE_ISSUE: VBControlExtender method fr.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
            'fr.VS.DrawLine("132mm", fr.VS.CurrentY, "132mm", fr.VS.CurrentY + 140)
            'fr.VS.Table = strTemp
            'fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone


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

    'Public Function CabecaRel() As Object
    '	fr.VS.CurrentLine = 1
    '	fr.VS.MarginLeft = "7mm"
    '	Call Cabecalho_Relatorio((fr.VS), istrTitulo1, istrTitulo3, "4", True, istrTitulo)
    '	'UPGRADE_ISSUE: VBControlExtender method fr.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
    '	fr.VS.CurrentY = 1900
    '	'  fr.VS.Table = "<10mm;" & "|"
    '	'  fr.VS.Table = "<10mm;" & "|"
    '	'  fr.VS.Table = "<10mm;" & "|"
    '	fr.VS.MarginLeft = "15mm"
    '	fr.VS.MarginRight = "15mm"
    '	fr.VS.Font = VB6.FontChangeSize(fr.VS.Font, 6)

    '	If blnAnalitico Then
    '		fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
    '		'UPGRADE_ISSUE: VBControlExtender method fr.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
    '		fr.VS.Table = "<10mm;" & "|" & fr.VS.CurrentY
    '		'    fr.VS.TableBorder = tbAll
    '		fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, True)
    '		fr.VS.Table = "<180mm;" & VB.Left("CLASSE: (" & VB6.Format(strClasse, "0000") & ") - " & strNmClasse, 70)
    '		fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, False)
    '		strClasseOld = strClasse
    '		fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, True)
    '		fr.VS.Table = "<30mm|<135mm|^15mm;Identificador|Nome|Grupo"
    '		fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, False)
    '	Else
    '		'    fr.VS.TableBorder = tbBoxRows
    '		'    fr.VS.Table = "<180mm;" & Left("CLASSE: (" & Format(strClasse, "0000") & ") - " & strNmClasse, 70)
    '	End If

    'End Function

    Private Function PegaNomeClasse(ByVal astrClasse As String) As String
        Dim lstrsql As String
        Try
            lstrsql = "SELECT descricao FROM classe WHERE codigo = """ & astrClasse & """ "
            objAtualizaBancoDados.QUERY_NAME = lstrsql
            dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            If dt.Rows.Count > 0 Then
                For Each row As DataRow In dt.Rows
                    PegaNomeClasse = IIf(IsDBNull(row.Item("descricao")), "NOME DA CLASSE NAO DEFINIDA", row.Item("descricao"))
                Next
            Else
                PegaNomeClasse = "CLASSE INEXISTENTE"
            End If
            '        If rsClasse.State = 1 Then Call rsClasse.Close()
            'UPGRADE_NOTE: Object rsClasse 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"'
            '        rsClasse = Nothing

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

    ''-------------------------------------------
    Private Function MontaFiltro(ByVal alsvAuxiliar As System.Windows.Forms.ListView, ByVal aintColuna As Short) As String
        Dim strfiltro As String
        Dim intConta As Short
        Try
            If alsvAuxiliar.Items.Item(1).Checked = False Then
                strfiltro = ""
                For intConta = 2 To alsvAuxiliar.Items.Count
                    If alsvAuxiliar.Items.Item(intConta).Checked = True Then
                        If aintColuna = 1 Then
                            strfiltro = strfiltro & """" & alsvAuxiliar.Items.Item(intConta).Text & ""","
                        Else
                            strfiltro = strfiltro & """" & alsvAuxiliar.Items.Item(intConta).SubItems(aintColuna - 1).Text & ""","
                        End If
                    End If
                Next intConta
                If Len(Trim(strfiltro)) > 0 Then
                    strfiltro = " (" & VB.Left(strfiltro, Len(strfiltro) - 1) & ") "
                End If
            End If
            MontaFiltro = strfiltro

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

    Private Sub CarregaClasses(ByRef alsvAuxiliar As System.Windows.Forms.ListView)
        'Versão: 001 Desenvolvedor: Ricardo Inacio Data: 03/06/2008 Descrição: Função para carregar as classe em uma listview com checkbox
        Dim lsiItens As System.Windows.Forms.ListViewItem
        Dim clhColunas As System.Windows.Forms.ColumnHeader
        Dim strNome As String
        Dim strZona As String
        Dim lngTamanho As Integer

        Try
            ' ----- Limpa a List que carrega as cidades
            alsvAuxiliar.Items.Clear()

            If alsvAuxiliar.Columns.Count <= 1 Then
                clhColunas = alsvAuxiliar.Columns.Add("", "Descrição", CInt(VB6.TwipsToPixelsX(2600)))
                clhColunas = alsvAuxiliar.Columns.Add("", "Condição", CInt(VB6.TwipsToPixelsX(0)))
            End If

            'lsiItens = alsvAuxiliar.Items.Add("Marcar Todas")
            'If lsiItens.SubItems.Count > alsvAuxiliar.Items.Count Then
            '    lsiItens.SubItems(alsvAuxiliar.Items.Count).Text = " = 0"
            'Else
            '    lsiItens.SubItems.Insert(alsvAuxiliar.Items.Count, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " = 0"))
            'End If
            'alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True

            'alsvAuxiliar.Items.Item(1).Font = VB6.FontChangeBold(alsvAuxiliar.Items.Item(1).Font, True)

            lsiItens = alsvAuxiliar.Items.Add("Residencial")
            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = " = 9"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " = 9"))
            End If
            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("Industrial")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = " < 4"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " < 4"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("Livre")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = " = 0100"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " = 0100"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("Comercial")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = " = 4"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " = 4"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("Rural")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = " = 5"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " = 5"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("Poder Público Municipal")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = " = 61"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " = 61"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("Poder Público Estadual")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = " = 62"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " = 62"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("Poder Público Federal")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = " = 63"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " = 63"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("Iluminação Pública")


            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = " = 7"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " = 7"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("Serviço Púplico")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = " = 64"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " = 64"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("Consumo Próprio")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = " = 8"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " = 8"))
            End If
            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True

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

    Private Sub chkTodasClasses_CheckedChanged(sender As Object, e As EventArgs) Handles chkTodasClasses.CheckedChanged
        Dim intConta As Short

        Try
            If chkTodasClasses.Checked = True Then
                For intConta = 0 To lvsClasses.Items.Count - 1
                    lvsClasses.Items.Item(intConta).Checked = True
                Next

            ElseIf chkTodasClasses.Checked = False Then
                For intConta = 0 To lvsClasses.Items.Count - 1
                    lvsClasses.Items.Item(intConta).Checked = False
                Next intConta
            End If

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

    Private Sub chkTodasCidades_CheckedChanged(sender As Object, e As EventArgs) Handles chkTodasCidades.CheckedChanged
        Dim intConta As Short

        Try
            If chkTodasCidades.Checked = True Then
                For intConta = 0 To lvsCidades.Items.Count - 1
                    lvsCidades.Items.Item(intConta).Checked = True
                Next

            ElseIf chkTodasCidades.Checked = False Then
                For intConta = 0 To lvsCidades.Items.Count - 1
                    lvsCidades.Items.Item(intConta).Checked = False
                Next intConta
            End If

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

    Private Sub opbMesAno_Click(sender As Object, e As EventArgs) Handles opbMesAno.Click
        Try
            If sender.Checked Then
                Me.DTPMesAno.Enabled = Me.opbMesAno.Checked
                Me.DTPInicial.Enabled = Me.opbPeriodo.Checked
                Me.DTPFinal.Enabled = Me.opbPeriodo.Checked
                Me.Label1(0).Enabled = Me.opbPeriodo.Checked
            End If
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: opbMesAno_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

    Private Sub opbPeriodo_Click(sender As Object, e As EventArgs) Handles opbPeriodo.Click
        Try
            If sender.Checked Then
                Me.DTPMesAno.Enabled = Me.opbMesAno.Checked
                Me.DTPInicial.Enabled = Me.opbPeriodo.Checked
                Me.DTPFinal.Enabled = Me.opbPeriodo.Checked
                Me.Label1(0).Enabled = Me.opbPeriodo.Checked
            End If
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: opbPeriodo_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

    Private Sub frmRelEstOperacao_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        Dim lstrsql As String

        Try

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


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


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


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

    Private Sub CarregaCidades(ByRef alsvAuxiliar As System.Windows.Forms.ListView)
        'Versão: 001 Desenvolvedor: Ricardo Inacio Data: 03/06/2008 Descrição: Função para carregar as cidades em uma listview com checkbox
        ' Dim rsConsulta As ADODB.Recordset
        Dim lsiItens As System.Windows.Forms.ListViewItem
        Dim clhColunas As System.Windows.Forms.ColumnHeader
        Dim strNome As String
        Dim strZona As String
        Dim lngTamanho As Integer
        Try

            'rsConsulta = New ADODB.Recordset
            ' ----- Limpa a List que carrega as cidades
            alsvAuxiliar.Items.Clear()

            If alsvAuxiliar.Columns.Count <= 1 Then
                clhColunas = alsvAuxiliar.Columns.Add("", "Cidades", CInt(VB6.TwipsToPixelsX(1600)))
                clhColunas = alsvAuxiliar.Columns.Add("", "Zona", CInt(VB6.TwipsToPixelsX(550)))
            End If
            ' ----- Busca as Cidades
            istrSql = "SELECT DISTINCT zona, nome " & "  FROM munic " & " ORDER BY zona ASC, nome ASC"
            objAtualizaBancoDados.QUERY_NAME = istrSql
            dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            ' ----- Se Retornou alguma coisa, monta as cidades
            If dt.Rows.Count > 0 Then
                ' lsiItens = alsvAuxiliar.Items.Add("Marcar Todas")
                'alsvAuxiliar.Items.Item(1).Checked = True
                For Each row As DataRow In dt.Rows

                    strNome = Trim(IIf(IsDBNull(row.Item("nome")), "", row.Item("nome")))
                    strZona = IIf(IsDBNull(row.Item("zona")), "", row.Item("zona"))
                    lsiItens = alsvAuxiliar.Items.Add(strNome)

                    If lsiItens.SubItems.Count > 1 Then
                        lsiItens.SubItems(1).Text = IIf(strZona = "", "000", strZona)
                    Else
                        lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, strZona))
                    End If
                    alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
                    If lngTamanho < Len(strNome) Then lngTamanho = Len(strNome)
                Next
            End If
            ''UPGRADE_WARNING: Lower bound of collection alsvAuxiliar.ColumnHeaders has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
            'alsvAuxiliar.Columns.Item(1).Width = VB6.TwipsToPixelsX(lngTamanho * 300)
            'If rsConsulta.State = 1 Then Call rsConsulta.Close()
            ''UPGRADE_NOTE: Object rsConsulta 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"'
            'rsConsulta = Nothing
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: CarregaCidades" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

    Private Sub LigDesCampos(ByVal ablnModo As Boolean)

        Try
            Me.FrameGrupo.Enabled = ablnModo
            Me.chkGrupoA.Enabled = ablnModo
            Me.chkGrupoB.Enabled = ablnModo
            Me.chkGrupoAB.Enabled = ablnModo
            Me.FrameTipo.Enabled = ablnModo
            Me.opbSintetico.Enabled = ablnModo
            Me.opbAnalitico.Enabled = ablnModo
            Me.FrameConsumo.Enabled = ablnModo
            Me.opbConsumo.Enabled = ablnModo
            Me.opbFaturado.Enabled = ablnModo
            Me.FrameValor.Enabled = ablnModo
            Me.chkKwh.Enabled = ablnModo
            Me.chkKw.Enabled = ablnModo
            Me.chkKvarh.Enabled = ablnModo
            Me.FrameSelecao.Enabled = ablnModo
            Me.opbMesAno.Enabled = ablnModo
            Me.opbPeriodo.Enabled = ablnModo
            Me.DTPMesAno.Enabled = ablnModo
            Me.DTPInicial.Enabled = ablnModo
            Me.DTPFinal.Enabled = ablnModo
            Me.FrameDatas.Enabled = ablnModo
            Me.opbMesAnoFat.Enabled = ablnModo
            Me.opbDtVencto.Enabled = ablnModo
            Me.opbDtLeitAnt.Enabled = ablnModo
            Me.opbDtLeitAtu.Enabled = ablnModo
            Me.opbDtApres.Enabled = ablnModo
            Me.opbDtEmissao.Enabled = ablnModo
            Me.opbDtPagto.Enabled = ablnModo
            Me.FrameClasses.Enabled = ablnModo
            Me.lvsClasses.Enabled = ablnModo
            Me.FrameCidades.Enabled = ablnModo
            Me.lvsCidades.Enabled = ablnModo
            Me.cmdEmitir.Enabled = ablnModo
            Me.cmdFechar.Enabled = ablnModo
            System.Windows.Forms.Application.DoEvents()

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

        Dim lstrsql As String

        Try

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

            lstrsql = ""
            lstrsql = "create table " & strTabela & "("
            lstrsql = lstrsql & "TIPO         VARCHAR(100), " 'Classe
            lstrsql = lstrsql & "CLASSE       VARCHAR(6), " 'Classe
            lstrsql = lstrsql & "NMCLASSE     VARCHAR(100), " 'NmClasse
            lstrsql = lstrsql & "QKWH_CONS    DOUBLE, " 'Qtde Kwh CONSUMIDO (ENERGIA CONSUMIDA)
            lstrsql = lstrsql & "QKWH_FAT     DOUBLE, " 'Qtde Kwh FATURADO
            lstrsql = lstrsql & "VKWH         DOUBLE, " 'Valor Kwh
            lstrsql = lstrsql & "VKW          DOUBLE, " 'Valor Kw
            lstrsql = lstrsql & "VKVARH       DOUBLE, " 'Valor Kvarh
            lstrsql = lstrsql & "NOME         VARCHAR(85), "
            lstrsql = lstrsql & "ZONA         VARCHAR(3), "
            lstrsql = lstrsql & "LIVRO        VARCHAR(2), "
            lstrsql = lstrsql & "CONTA        VARCHAR(6), "
            lstrsql = lstrsql & "GRUPO        VARCHAR(3)) "
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)


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

            lstrsql = ""
            lstrsql = "create table " & strTabela & "_Itens("
            lstrsql = lstrsql & "TIPO         VARCHAR(100), "
            lstrsql = lstrsql & "CLASSE       VARCHAR(20), "
            lstrsql = lstrsql & "NMCLASSE     VARCHAR(100), " 'NmClasse
            lstrsql = lstrsql & "NCONS    varchar(20), " 'Qtde Kwh CONSUMIDO (ENERGIA CONSUMIDA)
            lstrsql = lstrsql & "QKWH     varchar(20), " 'Qtde Kwh FATURADO
            lstrsql = lstrsql & "ENERGIA_DISTRIBUIDA varchar(20)) "
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)


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

            lstrsql = ""
            lstrsql = "create table " & strTabela & "Analitico("
            lstrsql = lstrsql & "TIPO         VARCHAR(100), " 'Classe
            lstrsql = lstrsql & "CLASSE       VARCHAR(20), " 'Classe
            lstrsql = lstrsql & "NMCLASSE     VARCHAR(100), " 'NmClasse
            lstrsql = lstrsql & "NCONS    varchar(20), " 'Qtde Kwh CONSUMIDO (ENERGIA CONSUMIDA)
            lstrsql = lstrsql & "QKWH     varchar(20), " 'Qtde Kwh FATURADO
            lstrsql = lstrsql & "ZLC     varchar(20), " 'Qtde Kwh FATURADO
            lstrsql = lstrsql & "NOME     varchar(100), " 'Qtde Kwh FATURADO
            lstrsql = lstrsql & "GRUPO     varchar(20), " 'Qtde Kwh FATURADO
            lstrsql = lstrsql & "ENERGIA_DISTRIBUIDA varchar(20)) "
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: sCriaTabelaAuxiliar" & "", "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


        Try
            Dim rpt As New ReportDocument()

            'rptConsumidorAVencer
            If vNomeRPT = "rptRelEstatOper" Then


                dt_2 = New DataTable()
                dt_2.Columns.Add("TIPO", GetType(String))
                dt_2.Columns.Add("CLASSE", GetType(String))
                dt_2.Columns.Add("NMCLASSE", GetType(String))
                dt_2.Columns.Add("NCONS", GetType(String))
                dt_2.Columns.Add("QKWH", GetType(String))
                dt_2.Columns.Add("ENERGIA_DISTRIBUIDA", GetType(String))
                dt_2.Columns.Add("IMAGEM", GetType(Byte()))

                ds = New DataSet

                lstrsql = ""
                lstrsql = "SELECT * FROM " & strTabela & "_Itens"
                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("TIPO"),
                                          dr_1("CLASSE"),
                                          dr_1("NMCLASSE"),
                                          dr_1("NCONS"),
                                          dr_1("QKWH"),
                                          dr_1("ENERGIA_DISTRIBUIDA"),
                                          bytBLOBData)
                    Next dr_1
                End If


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

                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 rptRelEstatOper

                    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("Tipo", IIf(cmbOpcao.Text = "Geral", "Geral", "Munícipio"))
                    'rpt.SetParameterValue("MesAnoRef", Month(dtpMesAno.Value) & "/" & Year(dtpMesAno.Value))
                    ' rpt.SetParameterValue("NomeRel", "Grupo A - Período de: " & dtpIni.Value & " Até: " & dtpFim.Value)

                    frmViewRelatorios.rpt = rpt

                    frmViewRelatorios.CRViewer.Zoom((100))
                    frmViewRelatorios.Text = "Relatório Consumidor a Vencer"
                    frmViewRelatorios.ShowDialog()

                End If

            Else ' ANALITICO

                dt_2 = New DataTable()
                dt_2.Columns.Add("TIPO", GetType(String))
                dt_2.Columns.Add("CLASSE", GetType(String))
                dt_2.Columns.Add("NMCLASSE", GetType(String))
                dt_2.Columns.Add("NCONS", GetType(String))
                dt_2.Columns.Add("QKWH", GetType(String))
                dt_2.Columns.Add("ZLC", GetType(String))
                dt_2.Columns.Add("NOME", GetType(String))
                dt_2.Columns.Add("GRUPO", GetType(String))
                dt_2.Columns.Add("ENERGIA_DISTRIBUIDA", GetType(String))
                dt_2.Columns.Add("IMAGEM", GetType(Byte()))

                ds = New DataSet

                lstrsql = ""
                lstrsql = "SELECT * FROM " & strTabela & "Analitico"
                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("TIPO"),
                                          dr_1("CLASSE"),
                                          dr_1("NMCLASSE"),
                                          dr_1("NCONS"),
                                          dr_1("QKWH"),
                                          dr_1("ZLC"),
                                          dr_1("NOME"),
                                          dr_1("GRUPO"),
                                          dr_1("ENERGIA_DISTRIBUIDA"),
                                          bytBLOBData)
                    Next dr_1
                End If


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

                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 rptRelEstatOper_analitico

                    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("Tipo", IIf(cmbOpcao.Text = "Geral", "Geral", "Munícipio"))
                    'rpt.SetParameterValue("MesAnoRef", Month(dtpMesAno.Value) & "/" & Year(dtpMesAno.Value))
                    ' rpt.SetParameterValue("NomeRel", "Grupo A - Período de: " & dtpIni.Value & " Até: " & dtpFim.Value)

                    frmViewRelatorios.rpt = rpt

                    frmViewRelatorios.CRViewer.Zoom((100))
                    frmViewRelatorios.Text = "Relatório Consumidor a Vencer"
                    frmViewRelatorios.ShowDialog()

                End If

            End If
            Me.Cursor = System.Windows.Forms.Cursors.Default

        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
End Class