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 frmRelItaipuRepasse
	Inherits System.Windows.Forms.Form
	'Private ifrmRelItaipu As FrmRelatorio
	Private istrClasseCabec As String
	Private istrCol As String
	Private istrExtensaoArq As String
	Private istrNmArq As String
    Private istrPath As String
    Private dt, dt1, dt2, dt3 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 Function BuscakWh(ByVal astrZona As String, ByVal astrLivro As String, ByVal astrConta As String, ByVal alngConsId As Integer, ByVal alngUnidId As Integer, ByVal alngMesAno As Integer, ByRef astrGrupo As String) As Double
        Dim ldblRet As Double
        Dim lstrTa As String
        Dim lstrIte As String
        Dim lstrSql As String
        'Dim lrstAux As ADODB.Recordset
        'lrstAux = New ADODB.Recordset
        Try
            ldblRet = 0
            lstrSql = ""
            lstrSql = lstrSql & "SELECT zona,livro,conta,mesano,numnf,mesano_ref,cons_id,unid_id,grupo,itensserv,flgst,ta "
            lstrSql = lstrSql & "  FROM `" & gstrDataBase & "`.`nf` "
            lstrSql = lstrSql & " WHERE `nf`.`mesano_ref` = " & alngMesAno & " "
            lstrSql = lstrSql & "   AND `nf`.`zona` = '" & astrZona & "' "
            lstrSql = lstrSql & "   AND `nf`.`livro` = '" & astrLivro & "' "
            lstrSql = lstrSql & "   AND `nf`.`conta` = '" & astrConta & "' "
            If alngConsId > 0 Then lstrSql = lstrSql & "   AND `nf`.`cons_id` = " & alngConsId & " "
            If alngUnidId > 0 Then lstrSql = lstrSql & "   AND `nf`.`unid_id` = " & alngUnidId & " "
            lstrSql = lstrSql & "   AND `nf`.`grupo` = '" & astrGrupo & "' "
            lstrSql = lstrSql & "   AND `nf`.`flgst` <> 'C' "
            lstrSql = lstrSql & "   LIMIT 0, 1 "
            '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
                    lstrIte = Trim(IIf(IsDBNull(row.Item("itensserv")), "", row.Item("itensserv")))
                    lstrTa = Mid(Trim(IIf(IsDBNull(row.Item("ta")), "01B1", row.Item("ta"))), 3, 2)
                    If astrGrupo = "B" Then
                        ldblRet = BuscaVlrItem(lstrIte, "030201009")
                    Else
                        If lstrTa = "AV" Or lstrTa = "AA" Or lstrTa = "AL" Or lstrTa = "AX" Then
                            ldblRet = BuscaVlrItem(lstrIte, "0302010010") + BuscaVlrItem(lstrIte, "0302010011")
                        ElseIf lstrTa = "A4" Or lstrTa = "AS" Then
                            ldblRet = BuscaVlrItem(lstrIte, "0302010009")
                        Else
                            ldblRet = BuscaVlrItem(lstrIte, "0302010009")
                        End If
                    End If
                Next
            End If

            BuscakWh = ldblRet

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

    Private Sub cmdArquivo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdArquivo.Click
        Dim lstrAux As String
        Call NomeArquivo()
        Try

            Me.dialArqOpen.Filter = "|*." & Me.dialArqOpen.DefaultExt
            Me.dialArqOpen.FileName = StrReverse(Mid(StrReverse(Me.txtArquivo.Text), CInt(InStr(StrReverse(Me.txtArquivo.Text), "\")))) & istrNmArq & "." & Me.dialArqOpen.DefaultExt
            Me.dialArqOpen.Title = "Arquivo de exportação bonus itaipu"
            Me.dialArqOpen.ShowDialog()
            lstrAux = StrReverse(Me.dialArqOpen.FileName)
            Me.txtArquivo.Text = StrReverse(Mid(lstrAux, CInt(InStr(lstrAux, "\")))) & istrNmArq & "." & Me.dialArqOpen.DefaultExt

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AddLancto" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
        End Try
    End Sub
    Private Sub cmdCarregar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCarregar.Click
        Dim lintQtdUcRes As Short 'Quantidade de residencial
        Dim lintQtdUcRur As Short 'Quantidade de rural
        Dim ldblQtdkWhRes As Double
        Dim ldblQtdkWhRur As Double
        Dim ldblTotCons As Double
        Dim lstrClasse As String
        Dim lstrZona As String
        Dim lstrLivro As String
        Dim lstrConta As String
        Dim ldblJan As Double
        Dim ldblFev As Double
        Dim ldblMar As Double
        Dim ldblAbr As Double
        Dim ldblMai As Double
        Dim ldblJun As Double
        Dim ldblJul As Double
        Dim ldblAgo As Double
        Dim ldblSet As Double
        Dim ldblOut As Double
        Dim ldblNov As Double
        Dim ldblDez As Double
        Dim lintMeses As Short
        Dim ldblTotal As Double
        Dim ldblMedia As Double
        Dim ldblPerc As Double
        Dim ldblRateio As Double
        Dim lstrGrupo As String
        Dim llngConsId As Integer
        Dim llngUnidId As Integer
        Dim lstrSql As String

        Try
            ' Dim lrstAux As ADODB.Recordset
            ' lrstAux = New ADODB.Recordset
            Me.lblQtdUC.Text = CStr(0)
            Me.lblQtdUcRes.Text = CStr(0)
            Me.lblQtdUcRur.Text = CStr(0)
            Me.lblQtdKWh.Text = CStr(0)
            Me.lblQtdKWhRes.Text = CStr(0)
            Me.lblQtdKWhRur.Text = CStr(0)
            Me.lblInfo.Text = " Carregando os dados aguarde..."
            Call Me.lblInfo.Refresh()
            Call MontaGrid()
            Me.mfgDados.Redraw = False
            Call LigDes(False)
            lstrSql = ""
            lstrSql = lstrSql & "SELECT "
            lstrSql = lstrSql & "  `ano`,`classe`,`zona`,`livro`,`conta`,`cons_id`,`unid_id`,`grupo`," & vbCrLf
            lstrSql = lstrSql & "  `mes_01`,`mes_02`,`mes_03`,`mes_04`,`mes_05`,`mes_06`,`mes_07`,`mes_08`,`mes_09`,`mes_10`,`mes_11`,`mes_12`,`meses`," & vbCrLf
            lstrSql = lstrSql & "  `total`,`media`,`percentual`,`rateio`" & vbCrLf
            lstrSql = lstrSql & " FROM `bonusitaipu` " & vbCrLf
            lstrSql = lstrSql & "WHERE `ano` =  " & CInt(Me.txtAno.Text) & " "
            'Call Conectar(lstrSql, lrstAux)
            objAtualizaBancoDados.QUERY_NAME = lstrSql
            dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            If dt.Rows.Count > 0 Then
                lintQtdUcRes = 0
                lintQtdUcRur = 0
                ldblQtdkWhRes = 0
                ldblQtdkWhRur = 0
                Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, dt.Rows.Count)
                For Each row As DataRow In dt.Rows

                    lstrClasse = Trim(IIf(IsDBNull(row.Item("classe")), "", row.Item("classe")))
                    lstrZona = Trim(IIf(IsDBNull(row.Item("zona")), "", row.Item("zona")))
                    lstrLivro = Trim(IIf(IsDBNull(row.Item("livro")), "", row.Item("livro")))
                    lstrConta = Trim(IIf(IsDBNull(row.Item("conta")), "", row.Item("conta")))
                    ldblJan = CDbl(IIf(IsDBNull(row.Item("mes_01")), 0, row.Item("mes_01")))
                    ldblFev = CDbl(IIf(IsDBNull(row.Item("mes_02")), 0, row.Item("mes_02")))
                    ldblMar = CDbl(IIf(IsDBNull(row.Item("mes_03")), 0, row.Item("mes_03")))
                    ldblAbr = CDbl(IIf(IsDBNull(row.Item("mes_04")), 0, row.Item("mes_04")))
                    ldblMai = CDbl(IIf(IsDBNull(row.Item("mes_05")), 0, row.Item("mes_05")))
                    ldblJun = CDbl(IIf(IsDBNull(row.Item("mes_06")), 0, row.Item("mes_06")))
                    ldblJul = CDbl(IIf(IsDBNull(row.Item("mes_07")), 0, row.Item("mes_07")))
                    ldblAgo = CDbl(IIf(IsDBNull(row.Item("mes_08")), 0, row.Item("mes_08")))
                    ldblSet = CDbl(IIf(IsDBNull(row.Item("mes_09")), 0, row.Item("mes_09")))
                    ldblOut = CDbl(IIf(IsDBNull(row.Item("mes_10")), 0, row.Item("mes_10")))
                    ldblNov = CDbl(IIf(IsDBNull(row.Item("mes_11")), 0, row.Item("mes_11")))
                    ldblDez = CDbl(IIf(IsDBNull(row.Item("mes_12")), 0, row.Item("mes_12")))
                    lintMeses = CShort(IIf(IsDBNull(row.Item("meses")), 0, row.Item("meses")))
                    ldblTotal = CDbl(IIf(IsDBNull(row.Item("total")), 0, row.Item("total")))
                    ldblMedia = CDbl(IIf(IsDBNull(row.Item("media")), 0, row.Item("media")))
                    ldblPerc = CDbl(IIf(IsDBNull(row.Item("percentual")), 0, row.Item("percentual")))
                    ldblRateio = CDbl(IIf(IsDBNull(row.Item("rateio")), 0, row.Item("rateio")))
                    lstrGrupo = Trim(IIf(IsDBNull(row.Item("grupo")), "", row.Item("grupo")))
                    llngConsId = CInt(IIf(IsDBNull(row.Item("cons_id")), 0, row.Item("cons_id")))
                    llngUnidId = CInt(IIf(IsDBNull(row.Item("unid_id")), 0, row.Item("unid_id")))
                    With Me.mfgDados
                        .Rows = .Rows + 1
                        .set_TextMatrix(.Rows - 1, 0, lstrClasse)
                        .set_TextMatrix(.Rows - 1, 1, lstrZona)
                        .set_TextMatrix(.Rows - 1, 2, lstrLivro)
                        .set_TextMatrix(.Rows - 1, 3, lstrConta)
                        .set_TextMatrix(.Rows - 1, 4, IIf(ldblJan > 0, ldblJan, ""))
                        .set_TextMatrix(.Rows - 1, 5, IIf(ldblFev > 0, ldblFev, ""))
                        .set_TextMatrix(.Rows - 1, 6, IIf(ldblMar > 0, ldblMar, ""))
                        .set_TextMatrix(.Rows - 1, 7, IIf(ldblAbr > 0, ldblAbr, ""))
                        .set_TextMatrix(.Rows - 1, 8, IIf(ldblMai > 0, ldblMai, ""))
                        .set_TextMatrix(.Rows - 1, 9, IIf(ldblJun > 0, ldblJun, ""))
                        .set_TextMatrix(.Rows - 1, 10, IIf(ldblJul > 0, ldblJul, ""))
                        .set_TextMatrix(.Rows - 1, 11, IIf(ldblAgo > 0, ldblAgo, ""))
                        .set_TextMatrix(.Rows - 1, 12, IIf(ldblSet > 0, ldblSet, ""))
                        .set_TextMatrix(.Rows - 1, 13, IIf(ldblOut > 0, ldblOut, ""))
                        .set_TextMatrix(.Rows - 1, 14, IIf(ldblNov > 0, ldblNov, ""))
                        .set_TextMatrix(.Rows - 1, 15, IIf(ldblDez > 0, ldblDez, ""))
                        .set_TextMatrix(.Rows - 1, 16, "Meses")
                        .set_TextMatrix(.Rows - 1, 17, ldblTotal)
                        .set_TextMatrix(.Rows - 1, 18, ldblMedia)
                        .set_TextMatrix(.Rows - 1, 19, ldblPerc)
                        .set_TextMatrix(.Rows - 1, 20, ldblRateio)
                        .set_TextMatrix(.Rows - 1, 21, lstrGrupo)
                        .set_TextMatrix(.Rows - 1, 22, llngConsId)
                        .set_TextMatrix(.Rows - 1, 23, llngUnidId)
                    End With
                    ldblTotCons = ldblJan + ldblFev + ldblMar + ldblAbr + ldblMai + ldblJun + ldblJul + ldblAgo + ldblSet + ldblOut + ldblNov + ldblDez
                    If CDbl(VB.Left(lstrClasse, 1)) = 9 Then
                        lintQtdUcRes = lintQtdUcRes + 1
                        ldblQtdkWhRes = ldblQtdkWhRes + ldblTotCons
                    ElseIf CDbl(VB.Left(lstrClasse, 1)) = 5 Then
                        lintQtdUcRur = lintQtdUcRur + 1
                        ldblQtdkWhRur = ldblQtdkWhRur + ldblTotCons
                    End If
                    Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
                Next
            End If


            Me.lblQtdUcRes.Text = FormatNumber(lintQtdUcRes, 0)
            Me.lblQtdUcRur.Text = FormatNumber(lintQtdUcRur, 0)
            Me.lblQtdKWhRes.Text = FormatNumber(ldblQtdkWhRes, 0)
            Me.lblQtdKWhRur.Text = FormatNumber(ldblQtdkWhRur, 0)
            Me.lblQtdUC.Text = FormatNumber(lintQtdUcRes + lintQtdUcRur, 0)
            Me.lblQtdKWh.Text = FormatNumber(ldblQtdkWhRes + ldblQtdkWhRur, 0)

            With Me.mfgDados
                If .Rows > 1 Then
                    .Col = 18
                    .Sort = MSFlexGridLib.SortSettings.flexSortNumericAscending
                End If
                .Redraw = True
            End With
            'UPGRADE_NOTE: Object lrstAux 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"'
            'lrstAux = Nothing
            Call TotalizaGrade()
            Call LigDes(True)
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AddLancto" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

    Private Sub cmdExporta_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExporta.Click
        Dim lstrLin As String
        Dim lintARQ As Short
        Dim llngRow As Integer
        Dim llngCol As Integer
        Try
            'Call LigDesCampos(False)
            If Trim(Me.txtArquivo.Text) <> "" Then
                lintARQ = FreeFile()
                FileOpen(lintARQ, Me.txtArquivo.Text, OpenMode.Output)
                With Me.mfgDados
                    For llngRow = 0 To .Rows - 1
                        lstrLin = ""
                        For llngCol = 0 To .Cols - 1
                            If .get_ColWidth(llngCol) <> 0 Then
                                If llngCol = 4 Then
                                    lstrLin = lstrLin & """" & .get_TextMatrix(llngRow, llngCol) & """;"
                                Else
                                    lstrLin = lstrLin & .get_TextMatrix(llngRow, llngCol) & ";"
                                End If
                            End If
                        Next llngCol
                        PrintLine(lintARQ, lstrLin)
                    Next llngRow
                End With
                FileClose(lintARQ)
                Call MsgBox("Arquivo gerado com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
            Else
                Call MsgBox("Você precisa selecionar o caminho e o arquivo para exportação!", MsgBoxStyle.Information, gstrTituloMensagem)
            End If
            'Call LigDesCampos(True)
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AddLancto" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
        End Try
    End Sub

    Private Sub 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
        'Dim lstrTab As String
        'Dim llngRow As Integer
        'Dim llngCol As Integer
        'Call LigDes(False)
        ''With ifrmRelItaipu
        ''    .quem_chama = Me
        ''    .VS.Font = VB6.FontChangeName(.VS.Font, "Arial")
        ''    .VS.Font = VB6.FontChangeSize(.VS.Font, 7)
        ''    .VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
        ''    .VS.PageBorder = VSVIEW6.PageBorderSettings.pbNone
        ''    .VS.ShowGuides = VSVIEW6.ShowGuideSettings.gdShow
        ''    .VS.Orientation = VSVIEW6.OrientationSettings.orPortrait
        ''    .VS.PhysicalPage = True
        ''    .VS.PaperSize = VSVIEW6.PaperSizeSettings.pprA4
        ''    .VS.MarginTop = "7mm"
        ''    .VS.MarginRight = "7mm"
        ''    .VS.MarginLeft = "7mm"
        ''    .VS.MarginBottom = "5mm"
        ''    .VS.MarginFooter = "0mm"
        ''    .VS.MarginHeader = "0mm"
        ''    .VS.TextColor = System.Drawing.Color.Black
        ''    .VS.BrushStyle = VSVIEW6.BrushStyleSettings.bsSolid
        ''    .VS.Font = VB6.FontChangeBold(.VS.Font, False)
        ''    .VS.StartDoc()
        ''    .VS.Font = VB6.FontChangeSize(.VS.Font, 12)
        ''    .VS.Font = VB6.FontChangeBold(.VS.Font, True)
        ''    .VS.Table = ">60mm|>30mm|>30mm;Classe|Qtde U.C.|KWh Qtde"
        ''    .VS.Font = VB6.FontChangeBold(.VS.Font, False)
        ''    .VS.Table = ">60mm|>30mm|>30mm;Rural|" & FormatNumber(Me.lblQtdUcRur.Text, 0) & "|" & FormatNumber(Me.lblQtdKWhRur.Text, 0)
        ''    .VS.Table = ">60mm|>30mm|>30mm;Residencial|" & FormatNumber(Me.lblQtdUcRes.Text, 0) & "|" & FormatNumber(Me.lblQtdKWhRes.Text, 0)
        ''    Call .LineFeed(1)
        ''    .VS.Font = VB6.FontChangeBold(.VS.Font, True)
        ''    .VS.Table = ">60mm|>30mm|>30mm;Total|" & FormatNumber(Me.lblQtdUC.Text, 0) & "|" & FormatNumber(Me.lblQtdKWh.Text, 0)
        ''    .VS.Font = VB6.FontChangeBold(.VS.Font, False)
        ''    .VS.Font = VB6.FontChangeSize(.VS.Font, 7)
        ''    Call .LineFeed(2)
        ''    .VS.Font = VB6.FontChangeSize(.VS.Font, 12)
        ''    With Me.mfgResumo
        ''        For llngRow = 0 To .Rows - 1
        ''            lstrTab = ""
        ''            For llngCol = 0 To .Cols - 1
        ''                lstrTab = lstrTab & .get_TextMatrix(llngRow, llngCol) & IIf(llngCol < 4, "|", "")
        ''            Next llngCol
        ''            If llngRow <= 1 Then
        ''                ifrmRelItaipu.VS.Font = VB6.FontChangeBold(ifrmRelItaipu.VS.Font, True)
        ''            Else
        ''                ifrmRelItaipu.VS.Font = VB6.FontChangeBold(ifrmRelItaipu.VS.Font, False)
        ''            End If
        ''            ifrmRelItaipu.VS.Table = ">10mm|<30mm|>35mm|>35mm|>35mm|>35mm;|" & lstrTab
        ''        Next llngRow
        ''    End With
        ''    Call .VS.EndDoc()
        ''    Call .Show()
        ''End With
        'Call LigDes(True)

        Dim lstrTab As String
        Dim llngRow As Integer
        Dim llngCol As Integer
        Call LigDes(False)
        Dim lstrsql As String

        Try

            lstrsql = ""
            lstrsql = "DELETE FROM AUX_BONUSITAIPU"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = "DELETE FROM AUX_BONUSITAIPU_RESUMO"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)


            With Me.mfgResumo

                lstrsql = "INSERT INTO AUX_BONUSITAIPU_RESUMO SET "
                lstrsql = lstrsql & "CLASSE = 'Residêncial' ,"
                lstrsql = lstrsql & "QTDE = '" & lblQtdUcRes.Text & "' ,"
                lstrsql = lstrsql & "KWH = '" & lblQtdKWhRes.Text & "'"
                objExecAtualizaBancoDados.QUERY_NAME = lstrsql
                repositorioAtualizaBancoDados.Execute(objExecAtualizaBancoDados)


                lstrsql = "INSERT INTO AUX_BONUSITAIPU_RESUMO SET "
                lstrsql = lstrsql & "CLASSE = 'Rural' ,"
                lstrsql = lstrsql & "QTDE = '" & lblQtdUcRur.Text & "' ,"
                lstrsql = lstrsql & "KWH = '" & lblQtdKWhRur.Text & "'"
                objExecAtualizaBancoDados.QUERY_NAME = lstrsql
                repositorioAtualizaBancoDados.Execute(objExecAtualizaBancoDados)


                For llngRow = 1 To .Rows - 1
                    lstrsql = "INSERT INTO AUX_BONUSITAIPU SET "
                    lstrsql = lstrsql & "REF = '" & .get_TextMatrix(llngRow, 0) & "' ,"
                    lstrsql = lstrsql & "QTDERES = '" & .get_TextMatrix(llngRow, 1) & "' ,"
                    lstrsql = lstrsql & "KWHRES = '" & .get_TextMatrix(llngRow, 2) & "' ,"
                    lstrsql = lstrsql & "QTDERURAL = '" & .get_TextMatrix(llngRow, 3) & "',"
                    lstrsql = lstrsql & "KWHRURAL = '" & .get_TextMatrix(llngRow, 4) & "'"
                    objExecAtualizaBancoDados.QUERY_NAME = lstrsql
                    repositorioAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
                Next llngRow

            End With


            Call sDadosEmpresa(LImagem, sNomeEmpresa, sEnderecoEmpresa, sCepEmpresa, sCidadeEmpresa, sUFempresa, sBairroEmpresa, sFoneEmpresa, sWebEmpresa)
            Call lPrint("rptRelBonusItaipu")
            Call LigDes(True)

        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 cmdRateio_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdRateio.Click

        Dim ldblRateio(12) As Double
        Dim ldblValor(12) As Double
        Dim lintMes As Short
        Dim ldblValorRateio As String

        Dim lstrSql As String

        Try

            ldblValorRateio = Replace(Me.txtValorTotal.Text, ",", ".")
            Me.lblInfo.Text = " Calculando os valores por mês aguarde..."
            Call Me.lblInfo.Refresh()
            Call LigDes(False)

            ''lstrSql = ""
            ''lstrSql = lstrSql & "SELECT * "
            ''lstrSql = lstrSql & " FROM `bonusitaipu` "
            ''lstrSql = lstrSql & " WHERE `ano` =  " & Conversion.CLng(Me.txtAno.Text) & " "
            ''lstrSql = lstrSql & " Order By `bonusitaipu`.`unid_id`"
            ''Call Conectar(lstrSql, lrstConsulta)
            ''With lrstConsulta
            ''If .State = adodb.ObjectStateEnum.adStateOpen Then
            ''If .RecordCount > 0 Then
            ''Call BarraProgresso(Me.ProgressBar, Me.lblPerc, Me.lblInfo, 0, .RecordCount)
            ''While Not .EOF

            For lintMes = 1 To 12
                lstrSql = ""
                lstrSql = lstrSql & "SELECT zona,livro,conta, mes_" & VB6.Format(lintMes, "00") & ",round(((mes_" & VB6.Format(lintMes, "00") & "/(select sum(IF(mes_01<350,mes_01,0))+sum(IF(mes_02<350,mes_02,0))+sum(IF(mes_03<350,mes_03,0))+sum(IF(mes_04<350,mes_04,0))+sum(IF(mes_05<350,mes_05,0))+sum(IF(mes_06<350,mes_06,0))+ sum(IF(mes_07<350,mes_07,0))+sum(IF(mes_08<350,mes_08,0))+sum(IF(mes_09<350,mes_09,0))+sum(IF(mes_10<350,mes_10,0))+sum(IF(mes_11<350,mes_11,0))+sum(IF(mes_12<350,mes_12,0))from bonusitaipu where ano = " & txtAno.Text & ")) * 100 ), 5) rateio,((" & ldblValorRateio & " * round(((mes_" & VB6.Format(lintMes, "00") & "/(select sum(IF(mes_01<350,mes_01,0))+sum(IF(mes_02<350,mes_02,0))+sum(IF(mes_03<350,mes_03,0))+sum(IF(mes_04<350,mes_04,0))+sum(IF(mes_05<350,mes_05,0))+sum(IF(mes_06<350,mes_06,0))+ sum(IF(mes_07<350,mes_07,0))+sum(IF(mes_08<350,mes_08,0))+sum(IF(mes_09<350,mes_09,0))+sum(IF(mes_10<350,mes_10,0))+sum(IF(mes_11<350,mes_11,0))+sum(IF(mes_12<350,mes_12,0))"
                lstrSql = lstrSql & " FROM bonusitaipu where ano = " & txtAno.Text & ")) * 100 ), 5))/100) valor from bonusitaipu where ano = " & txtAno.Text & " and mes_" & VB6.Format(lintMes, "00") & " < 350"
                'lstrSql = lstrSql & " AND Unid_id = " & .Fields("unid_id") & ""
                'lstrSql = lstrSql & " AND cons_id = " & .Fields("cons_id").value & ""
                ''lstrSql = lstrSql & " AND zona = " & .Fields("zona").value & ""
                ''lstrSql = lstrSql & " AND livro = " & .Fields("livro").value & ""
                ''lstrSql = lstrSql & " AND conta = " & .Fields("conta").value & ""
                lstrSql = lstrSql & " Order By bonusitaipu.unid_id"
                'Call Conectar(lstrSql, lrstConsulta1)
                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)
                    For Each row As DataRow In dt.Rows

                        lstrSql = ""
                        lstrSql = lstrSql & "UPDATE `bonusitaipu` SET "
                        lstrSql = lstrSql & "       `rateio_" & VB6.Format(lintMes, "00") & "`  = " & Replace(IIf(IsDBNull(row.Item("rateio")), 0, row.Item("rateio")), ",", ".") & ","
                        lstrSql = lstrSql & "       `valor_" & VB6.Format(lintMes, "00") & "` = " & Replace(IIf(IsDBNull(row.Item("valor")), 0, row.Item("valor")), ",", ".") & ""
                        'lstrSql = lstrSql & " WHERE `unid_id` = " & row.item("unid_id") & ""
                        'lstrSql = lstrSql & " AND `cons_id` = " & row.item("cons_id") & ""
                        lstrSql = lstrSql & " WHERE `zona` = " & row.Item("zona") & ""
                        lstrSql = lstrSql & " AND `livro` = " & row.Item("livro") & ""
                        lstrSql = lstrSql & " AND `conta` = " & row.Item("conta") & ""
                        objExecAtualizaBancoDados.QUERY_NAME = lstrSql
                        repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

                        ''ldblRateio(lintMes) = row.item("rateio").value
                        ''ldblValor(lintMes) = row.item("valor").value
                        Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
                    Next
                End If

            Next lintMes

            ''For lintMes = 1 To 12
            ''  lstrSql = ""
            ''  lstrSql = lstrSql & "UPDATE `bonusitaipu` SET "
            ''  lstrSql = lstrSql & "       `rateio_" & Format(lintMes, "00") & "`  = " & Replace(ldblRateio(lintMes), ",", ".") & ", "
            ''  lstrSql = lstrSql & "       `valor_" & Format(lintMes, "00") & "` = " & Replace(ldblValor(lintMes), ",", ".") & " "
            'lstrSql = lstrSql & " WHERE `unid_id` = " & .Fields("unid_id").value & ""
            'lstrSql = lstrSql & " AND `cons_id` = " & .Fields("cons_id").value & ""
            ''  lstrSql = lstrSql & " WHERE `zona` = " & .Fields("zona").value & ""
            ''  lstrSql = lstrSql & " AND `livro` = " & .Fields("livro").value & ""
            ''  lstrSql = lstrSql & " AND `conta` = " & .Fields("conta").value & ""
            ''  Call Executar(lstrSql)
            ''Next lintMes

            ''Call BarraProgresso(Me.ProgressBar, Me.lblPerc, Me.lblInfo, 1)
            ''Call .MoveNext
            ''Wend
            ''Call .Close
            ''End If
            ''End If
            ''End With


            ''With Me.mfgDados
            ''  .Col = 18
            ''  .Sort = MSFlexGridLib.SortSettings.flexSortNumericAscending
            ''  Call BarraProgresso(Me.ProgressBar, Me.lblPerc, Me.lblInfo, 0, .Rows - 1)
            ''  For llngRow = 1 To .Rows - 1
            ''    ldblMedia = CDbl(.TextMatrix(llngRow, 18))
            ''    ldblPerc = ldblMedia / CDbl(Me.lblQtdKWh.Caption)
            'ldblPerc = Arredondar(ldblMedia / CDbl(Me.lblQtdKWh.Caption), 15)
            ''    .TextMatrix(llngRow, 19) = ldblPerc
            'Grava o processo do bonus
            ''    lstrSql = ""
            ''    lstrSql = lstrSql & "INSERT INTO `bonusitaipu` SET " & vbCrLf
            ''    lstrSql = lstrSql & "  `ano`        =  " & Conversion.CLng(Me.txtAno.Text) & ", " & vbCrLf
            ''    lstrSql = lstrSql & "  `classe`     = '" & .TextMatrix(llngRow, 0) & "', " & vbCrLf
            ''    lstrSql = lstrSql & "  `zona`       = '" & .TextMatrix(llngRow, 1) & "', " & vbCrLf
            ''    lstrSql = lstrSql & "  `livro`      = '" & .TextMatrix(llngRow, 2) & "', " & vbCrLf
            ''    lstrSql = lstrSql & "  `conta`      = '" & .TextMatrix(llngRow, 3) & "', " & vbCrLf
            ''    lstrSql = lstrSql & "  `cons_id`    = '" & .TextMatrix(llngRow, 22) & "', " & vbCrLf
            ''    lstrSql = lstrSql & "  `unid_id`    = '" & .TextMatrix(llngRow, 23) & "', " & vbCrLf
            ''    lstrSql = lstrSql & "  `grupo`      = '" & .TextMatrix(llngRow, 21) & "', " & vbCrLf
            ''   If Strings.Trim(.TextMatrix(llngRow, 4)) <> "" Then lstrSql = lstrSql & "  `mes_01`     =  " & Strings.Replace(Conversion.CDbl(.TextMatrix(llngRow, 4)), ",", ".") & ", " & vbCrLf
            ''   If Strings.Trim(.TextMatrix(llngRow, 5)) <> "" Then lstrSql = lstrSql & "  `mes_02`     =  " & Strings.Replace(Conversion.CDbl(.TextMatrix(llngRow, 5)), ",", ".") & ", " & vbCrLf
            ''    If Strings.Trim(.TextMatrix(llngRow, 6)) <> "" Then lstrSql = lstrSql & "  `mes_03`     =  " & Strings.Replace(Conversion.CDbl(.TextMatrix(llngRow, 6)), ",", ".") & ", " & vbCrLf
            ''    If Strings.Trim(.TextMatrix(llngRow, 7)) <> "" Then lstrSql = lstrSql & "  `mes_04`     =  " & Strings.Replace(Conversion.CDbl(.TextMatrix(llngRow, 7)), ",", ".") & ", " & vbCrLf
            ''    If Strings.Trim(.TextMatrix(llngRow, 8)) <> "" Then lstrSql = lstrSql & "  `mes_05`     =  " & Strings.Replace(Conversion.CDbl(.TextMatrix(llngRow, 8)), ",", ".") & ", " & vbCrLf
            ''    If Strings.Trim(.TextMatrix(llngRow, 9)) <> "" Then lstrSql = lstrSql & "  `mes_06`     =  " & Strings.Replace(Conversion.CDbl(.TextMatrix(llngRow, 9)), ",", ".") & ", " & vbCrLf
            ''    If Strings.Trim(.TextMatrix(llngRow, 10)) <> "" Then lstrSql = lstrSql & "  `mes_07`     =  " & Strings.Replace(Conversion.CDbl(.TextMatrix(llngRow, 10)), ",", ".") & ", " & vbCrLf
            ''    If Strings.Trim(.TextMatrix(llngRow, 11)) <> "" Then lstrSql = lstrSql & "  `mes_08`     =  " & Strings.Replace(Conversion.CDbl(.TextMatrix(llngRow, 11)), ",", ".") & ", " & vbCrLf
            ''    If Strings.Trim(.TextMatrix(llngRow, 12)) <> "" Then lstrSql = lstrSql & "  `mes_09`     =  " & Strings.Replace(Conversion.CDbl(.TextMatrix(llngRow, 12)), ",", ".") & ", " & vbCrLf
            ''    If Strings.Trim(.TextMatrix(llngRow, 13)) <> "" Then lstrSql = lstrSql & "  `mes_10`     =  " & Strings.Replace(Conversion.CDbl(.TextMatrix(llngRow, 13)), ",", ".") & ", " & vbCrLf
            ''    If Strings.Trim(.TextMatrix(llngRow, 14)) <> "" Then lstrSql = lstrSql & "  `mes_11`     =  " & Strings.Replace(Conversion.CDbl(.TextMatrix(llngRow, 14)), ",", ".") & ", " & vbCrLf
            ''    If Strings.Trim(.TextMatrix(llngRow, 15)) <> "" Then lstrSql = lstrSql & "  `mes_12`     =  " & Strings.Replace(Conversion.CDbl(.TextMatrix(llngRow, 15)), ",", ".") & ", " & vbCrLf
            ''    lstrSql = lstrSql & "  `meses`      =  " & Conversion.CLng(.TextMatrix(llngRow, 16)) & ", " & vbCrLf
            ''    lstrSql = lstrSql & "  `total`      =  " & Strings.Replace(Conversion.CDbl(.TextMatrix(llngRow, 17)), ",", ".") & ", " & vbCrLf
            ''    lstrSql = lstrSql & "  `media`      =  " & Strings.Replace(Conversion.CDbl(.TextMatrix(llngRow, 18)), ",", ".") & ", " & vbCrLf
            ''    lstrSql = lstrSql & "  `percentual` =  " & Strings.Replace(Conversion.CDbl(.TextMatrix(llngRow, 19)), ",", ".") & ", " & vbCrLf
            ''    lstrSql = lstrSql & "  `rateio`     =  " & Strings.Replace(Conversion.CDbl(IIf(IsNumeric(.TextMatrix(llngRow, 20)), .TextMatrix(llngRow, 20), 0)), ",", ".") & " " & vbCrLf
            ''    Call Executar(lstrSql)
            ''    Call BarraProgresso(Me.ProgressBar, Me.lblPerc, Me.lblInfo, 1)
            ''  Next llngRow
            ''.Redraw = True
            ''End With
            'UPGRADE_NOTE: Object lrstConsulta 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"'
            ' lrstConsulta = Nothing
            'UPGRADE_NOTE: Object lrstConsulta1 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"'
            'lrstConsulta1 = Nothing
            Call LigDes(True)
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AddLancto" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

    Private Sub frmRelItaipuRepasse_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
        Try
            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
                'Call SendKeysA(System.Windows.Forms.Keys.Tab, True)
            ElseIf KeyAscii = System.Windows.Forms.Keys.Escape Then
                Call cmdFechar_Click(cmdFechar, New System.EventArgs())
            End If
            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: AddLancto" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
        End Try
    End Sub

    Private Sub frmRelItaipuRepasse_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        Dim lstrSql As String

        Try

            Call MontaGrid()
            Me.txtAno.Text = CStr(Year(Date.Now))
            Call SCriaTabelaAuxiliar()

            Me.txtArquivo.Text = My.Application.Info.DirectoryPath & "\" & istrNmArq & "." & Me.dialArqOpen.DefaultExt
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AddLancto" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
        End Try
    End Sub

    Private Sub MontaGrid()
        Dim lintAux As Short
        Try
            With Me.mfgDados
                .Rows = 1
                .Clear()
                .set_ColAlignment(0, MSFlexGridLib.AlignmentSettings.flexAlignLeftCenter)
                .set_ColAlignment(1, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(2, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(3, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColWidth(0, 1500)
                .set_ColWidth(1, 450)
                .set_ColWidth(2, 450)
                .set_ColWidth(3, 700)
                .set_ColWidth(4, 600)
                .set_ColWidth(5, 600)
                .set_ColWidth(6, 600)
                .set_ColWidth(7, 600)
                .set_ColWidth(8, 600)
                .set_ColWidth(9, 600)
                .set_ColWidth(10, 600)
                .set_ColWidth(11, 600)
                .set_ColWidth(12, 600)
                .set_ColWidth(13, 600)
                .set_ColWidth(14, 600)
                .set_ColWidth(15, 600)
                .set_ColWidth(16, 550)
                .set_ColWidth(17, 700)
                .set_ColWidth(18, 700)
                .set_ColWidth(19, 1000)
                .set_ColWidth(20, 900)
                .set_ColWidth(21, 0)
                .set_ColWidth(22, 0)
                .set_ColWidth(23, 0)
                .set_TextMatrix(0, 0, "Classe")
                .set_TextMatrix(0, 1, "Zona")
                .set_TextMatrix(0, 2, "Livro")
                .set_TextMatrix(0, 3, "Conta")
                .set_TextMatrix(0, 4, "Jan")
                .set_TextMatrix(0, 5, "Fev")
                .set_TextMatrix(0, 6, "Mar")
                .set_TextMatrix(0, 7, "Abr")
                .set_TextMatrix(0, 8, "Mai")
                .set_TextMatrix(0, 9, "Jun")
                .set_TextMatrix(0, 10, "Jul")
                .set_TextMatrix(0, 11, "Ago")
                .set_TextMatrix(0, 12, "Set")
                .set_TextMatrix(0, 13, "Out")
                .set_TextMatrix(0, 14, "Nov")
                .set_TextMatrix(0, 15, "Dez")
                .set_TextMatrix(0, 16, "Meses")
                .set_TextMatrix(0, 17, "Total")
                .set_TextMatrix(0, 18, "Média")
                .set_TextMatrix(0, 19, "%")
                .set_TextMatrix(0, 20, "Rateio")
                .set_TextMatrix(0, 21, "grupo")
                .set_TextMatrix(0, 22, "cons_id")
                .set_TextMatrix(0, 23, "unid_id")
            End With
            With Me.mfgResumo
                .Cols = 5
                .Rows = 14
                .Clear()
                .FixedRows = 2
                .MergeCells = MSFlexGridLib.MergeCellsSettings.flexMergeRestrictRows
                '.SelectionMode = flexSelectionByRow
                '.FixedCols = 4
                'Primeira Linha
                .Row = 0
                .Col = 0
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 1
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 2
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 3
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 4
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                'Segunda Linha
                .Row = 1
                .Col = 0
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 1
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 2
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 3
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                .Col = 4
                .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                .CellFontBold = True
                For lintAux = 2 To 13
                    .Row = lintAux
                    .Col = 0
                    .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HC0C0C0)
                    .CellFontBold = True
                Next lintAux
                .set_ColAlignment(0, MSFlexGridLib.AlignmentSettings.flexAlignLeftCenter)
                .set_TextMatrix(0, 0, "Referência")
                .set_TextMatrix(1, 0, Me.txtAno.Text)
                .set_TextMatrix(2, 0, "Janeiro")
                .set_TextMatrix(3, 0, "Fevereiro")
                .set_TextMatrix(4, 0, "Março")
                .set_TextMatrix(5, 0, "Abril")
                .set_TextMatrix(6, 0, "Maio")
                .set_TextMatrix(7, 0, "Junho")
                .set_TextMatrix(8, 0, "Julho")
                .set_TextMatrix(9, 0, "Agosto")
                .set_TextMatrix(10, 0, "Setembro")
                .set_TextMatrix(11, 0, "Outubro")
                .set_TextMatrix(12, 0, "Novembro")
                .set_TextMatrix(13, 0, "Dezembro")
                .set_ColAlignment(1, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(2, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_TextMatrix(0, 1, "Residencial")
                .set_TextMatrix(0, 2, "Residencial")
                .set_TextMatrix(1, 1, "Qtde")
                .set_TextMatrix(1, 2, "kWh")
                .set_ColAlignment(3, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(4, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_TextMatrix(0, 3, "Rural")
                .set_TextMatrix(0, 4, "Rural")
                .set_TextMatrix(1, 3, "Qtde")
                .set_TextMatrix(1, 4, "kWh")
                .set_ColWidth(0, 2000)
                .set_ColWidth(1, 2000)
                .set_ColWidth(2, 2000)
                .set_ColWidth(3, 2000)
                .set_ColWidth(4, 2000)
                .set_MergeCol(1, True)
                .set_MergeCol(2, True)
                .set_MergeCol(3, True)
                .set_MergeCol(4, True)
                .set_MergeRow(0, True)
                .Row = 1
                .Col = 0
            End With
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AddLancto" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
        End Try
    End Sub
    Private Sub txtAno_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtAno.Enter
        With Me.txtAno
            .SelectionStart = 0
            .SelectionLength = Len(.Text)
        End With
    End Sub
    Private Sub txtAno_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtAno.KeyPress
        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
        If KeyAscii < 48 Or KeyAscii > 57 Then
            KeyAscii = 0
        End If
        eventArgs.KeyChar = Chr(KeyAscii)
        If KeyAscii = 0 Then
            eventArgs.Handled = True
        End If
    End Sub
    Private Sub LigDes(ByVal ablnModo As Boolean)
        Try
            Me.txtAno.Enabled = ablnModo
            Me.cmdCarregar.Enabled = ablnModo
            Me.cmdProcessar.Enabled = ablnModo
            Me.cmdImprimir.Enabled = ablnModo
            Me.cmdFechar.Enabled = ablnModo
            Me.cmdRateio.Enabled = ablnModo
            Me.txtValorTotal.Enabled = ablnModo
            'UPGRADE_ISSUE: Form property frmRelItaipuRepasse.MousePointer does not support custom mousepointers. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="45116EAB-7060-405E-8ABE-9DBB40DC2E86"'
            Me.Cursor = IIf(ablnModo, System.Windows.Forms.Cursors.Default, System.Windows.Forms.Cursors.WaitCursor)
            System.Windows.Forms.Application.DoEvents()

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AddLancto" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
        End Try
    End Sub
    Private Sub NomeArquivo()
        Dim lstrAux As String
        Dim lstrGrupo As String
        Dim lstrTipo As String
        Dim lstrPeriodo As String
        Try
            Me.dialArqOpen.DefaultExt = "CSV"
            istrNmArq = "BonusItaipu_" & Me.txtAno.Text
            lstrAux = StrReverse(Me.txtArquivo.Text)
            Me.txtArquivo.Text = StrReverse(Mid(lstrAux, CInt(InStr(lstrAux, "\")))) & istrNmArq & "." & Me.dialArqOpen.DefaultExt
            System.Windows.Forms.Application.DoEvents()
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AddLancto" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
        End Try
    End Sub
    Private Sub txtAno_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtAno.Leave
        Call NomeArquivo()
        Me.mfgResumo.set_TextMatrix(1, 0, Me.txtAno.Text)
    End Sub
    Private Sub txtValorTotal_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtValorTotal.Enter
        With Me.txtValorTotal
            .SelectionStart = 0
            .SelectionLength = CInt(Len(.Text))
        End With
    End Sub

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

        Dim lintQtdUcRes As Short 'Quantidade de residencial
        Dim lintQtdUcRur As Short 'Quantidade de rural
        Dim ldblQtdkWhRes As Double
        Dim ldblQtdkWhRur As Double
        Dim lblnExiste As Boolean
        Dim llngRow As Integer
        Dim lintMes As Short
        Dim lintQtdMeses As Short
        Dim lstrCdClas As String
        Dim lstrNmClas As String
        Dim lstrZona As String
        Dim lstrLivro As String
        Dim lstrConta As String
        Dim llngConsId As Integer
        Dim llngUnidId As Integer
        Dim lstrGrupo As String
        'UPGRADE_WARNING: Lower bound of array ldblConsumo was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
        Dim ldblConsumo(12) As Double
        Dim ldblTotCons As Double
        Dim ldblMedia As Double
        Dim ldblPerc As Double
        Dim lblnContinua As Boolean
        Dim lstrItens As String
        Dim lstrSql As String
        'Dim lrstConsulta As ADODB.Recordset
        'lrstConsulta = New ADODB.Recordset

        Try
            Me.lblQtdUC.Text = CStr(0)
            Me.lblQtdUcRes.Text = CStr(0)
            Me.lblQtdUcRur.Text = CStr(0)
            Me.lblQtdKWh.Text = CStr(0)
            Me.lblQtdKWhRes.Text = CStr(0)
            Me.lblQtdKWhRur.Text = CStr(0)
            Me.lblInfo.Text = " Carregando os dados aguarde..."
            Call Me.lblInfo.Refresh()
            Call LigDes(False)
            lblnExiste = False

            lstrSql = ""
            lstrSql = "SELECT * FROM bonusitaipu WHERE ano =  " & CInt(Me.txtAno.Text) & " LIMIT 0, 1"
            objAtualizaBancoDados.QUERY_NAME = lstrSql
            dt1 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            If dt1.Rows.Count > 0 Then
                lblnExiste = True
            End If

            dt1 = Nothing

            If lblnExiste Then
                lblnExiste = (MsgBox("Já existe um processo do bonus itaipu feito no ano de " & Me.txtAno.Text & ". Deseja continuar mesmo assim?", MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton1, gstrTituloMensagem) = MsgBoxResult.No)
            End If
            If Not lblnExiste Then

                lstrSql = ""
                lstrSql = "DELETE FROM bonusitaipu WHERE ano =  " & CInt(Me.txtAno.Text) & " "
                objExecAtualizaBancoDados.QUERY_NAME = lstrSql
                repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

                Call MontaGrid()
                Me.mfgDados.Redraw = False
                lintQtdUcRes = 0
                lintQtdUcRur = 0
                ldblQtdkWhRes = 0
                ldblQtdkWhRur = 0
                lstrSql = ""
                lstrSql = lstrSql & "SELECT `nf`.`codclas`, "
                lstrSql = lstrSql & "       `nf`.`mesano`, "
                lstrSql = lstrSql & "       `nf`.`mesano_ref`, "
                lstrSql = lstrSql & "       `nf`.`numnf`, "
                lstrSql = lstrSql & "       `nf`.`zona`, "
                lstrSql = lstrSql & "       `nf`.`livro`, "
                lstrSql = lstrSql & "       `nf`.`conta`, "
                lstrSql = lstrSql & "       `nf`.`grupo`, "
                lstrSql = lstrSql & "       `nf`.`cons_id`, "
                lstrSql = lstrSql & "       `nf`.`unid_id` "
                lstrSql = lstrSql & "  FROM `" & gstrDataBase & "`.`nf` "
                lstrSql = lstrSql & " WHERE ((`nf`.`codclas` BETWEEN 9000 AND 9999) OR (`nf`.`codclas` BETWEEN 5000 AND 5999)) "
                lstrSql = lstrSql & "   AND `nf`.`mesano` = ""12/" & Me.txtAno.Text & """ "
                'Call Conectar(lstrSql, lrstConsulta)
                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)
                    For Each row As DataRow In dt.Rows
                        lintQtdMeses = 0
                        ldblTotCons = 0

                        lstrCdClas = VB6.Format(IIf(IsDBNull(row.Item("codclas")), "0", row.Item("codclas")), "0000")
                        Select Case VB.Left(lstrCdClas, 1)
                            Case Is < CStr(4)
                                lstrNmClas = "Industrial"
                            Case Is = CStr(4)
                                lstrNmClas = "Comercial"
                            Case Is = CStr(5)
                                lstrNmClas = "Rural"
                            Case Is = CStr(6)
                                If CDbl(VB.Left(lstrCdClas, 2)) = 64 Then
                                    lstrNmClas = "Iluminação Pública"
                                Else
                                    lstrNmClas = "Poder Público"
                                End If
                            Case Is = CStr(7)
                                lstrNmClas = "Serviço Público"
                            Case Is = CStr(8)
                                lstrNmClas = "Consumo Próprio"
                            Case Is = CStr(9)
                                lstrNmClas = "Residencial"
                        End Select

                        lstrZona = VB6.Format(IIf(IsDBNull(row.Item("zona")), "0", row.Item("zona")), "000")
                        lstrLivro = VB6.Format(IIf(IsDBNull(row.Item("livro")), "0", row.Item("livro")), "00")
                        lstrConta = VB6.Format(IIf(IsDBNull(row.Item("conta")), "0", row.Item("conta")), "000000")
                        lstrGrupo = Trim(IIf(IsDBNull(row.Item("grupo")), "B", row.Item("grupo")))
                        llngConsId = CInt(IIf(IsDBNull(row.Item("cons_id")), "0", row.Item("cons_id")))
                        llngUnidId = CInt(IIf(IsDBNull(row.Item("unid_id")), "0", row.Item("unid_id")))
                        For lintMes = 1 To 12
                            ldblConsumo(lintMes) = BuscakWh(lstrZona, lstrLivro, lstrConta, llngConsId, llngUnidId, CInt(Me.txtAno.Text & VB6.Format(lintMes, "00")), lstrGrupo)
                            If ldblConsumo(lintMes) > 0 Then
                                lintQtdMeses = lintQtdMeses + 1
                            End If
                            ldblTotCons = ldblTotCons + ldblConsumo(lintMes)
                        Next lintMes
                        ldblMedia = Arredondar(ldblTotCons / IIf(lintQtdMeses > 0, lintQtdMeses, 1), 0)

                        lblnContinua = False

                        If Me.opbMedia.Checked Then
                            If ldblMedia < 350 Then
                                lblnContinua = True
                            End If
                        ElseIf Me.opbDez.Checked Then
                            If ldblConsumo(12) < 350 Then
                                lblnContinua = True
                            End If
                        End If
                        If lblnContinua Then
                            With Me.mfgDados
                                .Rows = .Rows + 1
                                .set_TextMatrix(.Rows - 1, 0, lstrCdClas & " - " & lstrNmClas)
                                .set_TextMatrix(.Rows - 1, 1, lstrZona)
                                .set_TextMatrix(.Rows - 1, 2, lstrLivro)
                                .set_TextMatrix(.Rows - 1, 3, lstrConta)
                                For lintMes = 1 To 12
                                    .set_TextMatrix(.Rows - 1, lintMes + 3, IIf(ldblConsumo(lintMes) = 0, "", ldblConsumo(lintMes)))
                                Next lintMes
                                .set_TextMatrix(.Rows - 1, 16, lintQtdMeses)
                                .set_TextMatrix(.Rows - 1, 17, ldblTotCons)
                                .set_TextMatrix(.Rows - 1, 18, ldblMedia)
                                .set_TextMatrix(.Rows - 1, 21, lstrGrupo)
                                .set_TextMatrix(.Rows - 1, 22, llngConsId)
                                .set_TextMatrix(.Rows - 1, 23, llngUnidId)
                            End With
                            If CDbl(VB.Left(lstrCdClas, 1)) = 9 Then
                                lintQtdUcRes = lintQtdUcRes + 1
                                ldblQtdkWhRes = ldblQtdkWhRes + ldblTotCons
                            ElseIf CDbl(VB.Left(lstrCdClas, 1)) = 5 Then
                                lintQtdUcRur = lintQtdUcRur + 1
                                ldblQtdkWhRur = ldblQtdkWhRur + ldblTotCons
                            End If
                        End If

                        Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
                    Next
                End If

                dt = Nothing

                Me.lblQtdUcRes.Text = FormatNumber(lintQtdUcRes, 0)
                Me.lblQtdUcRur.Text = FormatNumber(lintQtdUcRur, 0)
                Me.lblQtdKWhRes.Text = FormatNumber(ldblQtdkWhRes, 0)
                Me.lblQtdKWhRur.Text = FormatNumber(ldblQtdkWhRur, 0)
                Me.lblQtdUC.Text = FormatNumber(lintQtdUcRes + lintQtdUcRur, 0)
                Me.lblQtdKWh.Text = FormatNumber(ldblQtdkWhRes + ldblQtdkWhRur, 0)

                With Me.mfgDados
                    .Col = 18
                    .Sort = MSFlexGridLib.SortSettings.flexSortNumericAscending
                    Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .Rows - 1)
                    For llngRow = 1 To .Rows - 1
                        ldblMedia = CDbl(.get_TextMatrix(llngRow, 18))
                        ldblPerc = ldblMedia / CDbl(Me.lblQtdKWh.Text)
                        'ldblPerc = Arredondar(ldblMedia / CDbl(Me.lblQtdKWh.Caption), 15)
                        .set_TextMatrix(llngRow, 19, ldblPerc)
                        'Grava o processo do bonus
                        lstrSql = ""
                        lstrSql = lstrSql & "INSERT INTO `bonusitaipu` SET " & vbCrLf
                        lstrSql = lstrSql & "  `ano`        =  " & CInt(Me.txtAno.Text) & ", " & vbCrLf
                        lstrSql = lstrSql & "  `classe`     = '" & .get_TextMatrix(llngRow, 0) & "', " & vbCrLf
                        lstrSql = lstrSql & "  `zona`       = '" & .get_TextMatrix(llngRow, 1) & "', " & vbCrLf
                        lstrSql = lstrSql & "  `livro`      = '" & .get_TextMatrix(llngRow, 2) & "', " & vbCrLf
                        lstrSql = lstrSql & "  `conta`      = '" & .get_TextMatrix(llngRow, 3) & "', " & vbCrLf
                        lstrSql = lstrSql & "  `cons_id`    = '" & .get_TextMatrix(llngRow, 22) & "', " & vbCrLf
                        lstrSql = lstrSql & "  `unid_id`    = '" & .get_TextMatrix(llngRow, 23) & "', " & vbCrLf
                        lstrSql = lstrSql & "  `grupo`      = '" & .get_TextMatrix(llngRow, 21) & "', " & vbCrLf
                        If Trim(.get_TextMatrix(llngRow, 4)) <> "" Then lstrSql = lstrSql & "  `mes_01`     =  " & Replace(CStr(CDbl(.get_TextMatrix(llngRow, 4))), ",", ".") & ", " & vbCrLf
                        If Trim(.get_TextMatrix(llngRow, 5)) <> "" Then lstrSql = lstrSql & "  `mes_02`     =  " & Replace(CStr(CDbl(.get_TextMatrix(llngRow, 5))), ",", ".") & ", " & vbCrLf
                        If Trim(.get_TextMatrix(llngRow, 6)) <> "" Then lstrSql = lstrSql & "  `mes_03`     =  " & Replace(CStr(CDbl(.get_TextMatrix(llngRow, 6))), ",", ".") & ", " & vbCrLf
                        If Trim(.get_TextMatrix(llngRow, 7)) <> "" Then lstrSql = lstrSql & "  `mes_04`     =  " & Replace(CStr(CDbl(.get_TextMatrix(llngRow, 7))), ",", ".") & ", " & vbCrLf
                        If Trim(.get_TextMatrix(llngRow, 8)) <> "" Then lstrSql = lstrSql & "  `mes_05`     =  " & Replace(CStr(CDbl(.get_TextMatrix(llngRow, 8))), ",", ".") & ", " & vbCrLf
                        If Trim(.get_TextMatrix(llngRow, 9)) <> "" Then lstrSql = lstrSql & "  `mes_06`     =  " & Replace(CStr(CDbl(.get_TextMatrix(llngRow, 9))), ",", ".") & ", " & vbCrLf
                        If Trim(.get_TextMatrix(llngRow, 10)) <> "" Then lstrSql = lstrSql & "  `mes_07`     =  " & Replace(CStr(CDbl(.get_TextMatrix(llngRow, 10))), ",", ".") & ", " & vbCrLf
                        If Trim(.get_TextMatrix(llngRow, 11)) <> "" Then lstrSql = lstrSql & "  `mes_08`     =  " & Replace(CStr(CDbl(.get_TextMatrix(llngRow, 11))), ",", ".") & ", " & vbCrLf
                        If Trim(.get_TextMatrix(llngRow, 12)) <> "" Then lstrSql = lstrSql & "  `mes_09`     =  " & Replace(CStr(CDbl(.get_TextMatrix(llngRow, 12))), ",", ".") & ", " & vbCrLf
                        If Trim(.get_TextMatrix(llngRow, 13)) <> "" Then lstrSql = lstrSql & "  `mes_10`     =  " & Replace(CStr(CDbl(.get_TextMatrix(llngRow, 13))), ",", ".") & ", " & vbCrLf
                        If Trim(.get_TextMatrix(llngRow, 14)) <> "" Then lstrSql = lstrSql & "  `mes_11`     =  " & Replace(CStr(CDbl(.get_TextMatrix(llngRow, 14))), ",", ".") & ", " & vbCrLf
                        If Trim(.get_TextMatrix(llngRow, 15)) <> "" Then lstrSql = lstrSql & "  `mes_12`     =  " & Replace(CStr(CDbl(.get_TextMatrix(llngRow, 15))), ",", ".") & ", " & vbCrLf
                        lstrSql = lstrSql & "  `meses`      =  " & CInt(.get_TextMatrix(llngRow, 16)) & ", " & vbCrLf
                        lstrSql = lstrSql & "  `total`      =  " & Replace(CStr(CDbl(.get_TextMatrix(llngRow, 17))), ",", ".") & ", " & vbCrLf
                        lstrSql = lstrSql & "  `media`      =  " & Replace(CStr(CDbl(.get_TextMatrix(llngRow, 18))), ",", ".") & ", " & vbCrLf
                        lstrSql = lstrSql & "  `percentual` =  " & Replace(CStr(CDbl(.get_TextMatrix(llngRow, 19))), ",", ".") & ", " & vbCrLf
                        lstrSql = lstrSql & "  `rateio`     =  " & Replace(CStr(CDbl(IIf(IsNumeric(.get_TextMatrix(llngRow, 20)), .get_TextMatrix(llngRow, 20), 0))), ",", ".") & " " & vbCrLf
                        ' Call Executar(lstrSql)
                        objExecAtualizaBancoDados.QUERY_NAME = lstrSql
                        repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

                        Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
                    Next llngRow
                    .Redraw = True
                End With
            End If

            Call TotalizaGrade()
            Call LigDes(True)

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AddLancto" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub
    Private Sub txtValorTotal_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtValorTotal.Leave
        With Me.txtValorTotal
            .Text = VB6.Format(CDbl(IIf(IsNumeric(.Text), .Text, 0)), "##.00")
        End With
    End Sub
    Private Sub TotalizaGrade()
        Dim llngQtde(12) As Integer
        Dim ldblKwh As Integer
        Dim llngRow As Integer
        Dim llngCol As Integer
        Try
            Me.mfgResumo.Redraw = False
            With Me.mfgDados
                Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .Rows - 1)
                For llngRow = 1 To .Rows - 1
                    For llngCol = 4 To 15
                        If IsNumeric(.get_TextMatrix(llngRow, llngCol)) Then
                            ldblKwh = CDbl(.get_TextMatrix(llngRow, llngCol))
                            If ldblKwh < 350 Then
                                Call AdicionaGradeResumo(IIf(VB.Left(.get_TextMatrix(llngRow, 0), 1) = "9", "Residencial", "Rural"), (llngCol - 3), ldblKwh)
                            End If
                        End If
                    Next llngCol
                    Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
                Next llngRow
            End With
            Me.mfgResumo.Redraw = True
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AddLancto" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

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

        Try

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


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


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

    Private Sub AdicionaGradeResumo(ByVal astrClasse As String, ByVal aintMes As Short, ByVal adblConsumo As Double)
        Try
            With Me.mfgResumo
                If astrClasse = "Residencial" Then
                    .set_TextMatrix(aintMes + 1, 1, VB6.Format(CInt(IIf(IsNumeric(.get_TextMatrix(aintMes + 1, 1)), .get_TextMatrix(aintMes + 1, 1), 0)) + 1, "#0")) 'qtde
                    .set_TextMatrix(aintMes + 1, 2, VB6.Format(CDbl(IIf(IsNumeric(.get_TextMatrix(aintMes + 1, 2)), .get_TextMatrix(aintMes + 1, 2), 0)) + adblConsumo, "#,##0.00")) 'Consumo
                ElseIf astrClasse = "Rural" Then
                    .set_TextMatrix(aintMes + 1, 3, VB6.Format(CInt(IIf(IsNumeric(.get_TextMatrix(aintMes + 1, 3)), .get_TextMatrix(aintMes + 1, 3), 0)) + 1, "#0")) 'qtde
                    .set_TextMatrix(aintMes + 1, 4, VB6.Format(CDbl(IIf(IsNumeric(.get_TextMatrix(aintMes + 1, 4)), .get_TextMatrix(aintMes + 1, 4), 0)) + adblConsumo, "#,##0.00")) 'Consumo
                End If
            End With
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AddLancto" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

    Private Sub SCriaTabelaAuxiliar()
        Dim lstrsql As String

        Try

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

            lstrsql = ""
            lstrsql = lstrsql & "CREATE TABLE IF NOT EXISTS `AUX_BONUSITAIPU` (" & vbCrLf
            lstrsql = lstrsql & "  `REF`        varchar(10) DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `QTDERES`     varchar(20) DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `KWHRES`       varchar(20) DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `QTDERURAL`      varchar(20) DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `KWHRURAL`      varchar(20) DEFAULT NULL" & vbCrLf
            lstrsql = lstrsql & ")"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)


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

            lstrsql = ""
            lstrsql = lstrsql & "CREATE TABLE IF NOT EXISTS `AUX_BONUSITAIPU_RESUMO` (" & vbCrLf
            lstrsql = lstrsql & "  `CLASSE`        varchar(20) DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `QTDE`     varchar(20) DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `KWH`       varchar(20) DEFAULT NULL" & vbCrLf
            lstrsql = lstrsql & ")"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "CREATE TABLE IF NOT EXISTS `bonusitaipu` (" & vbCrLf
            lstrsql = lstrsql & "  `ano`        int(11) DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `classe`     varchar(50) DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `zona`       varchar(3) DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `livro`      varchar(2) DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `conta`      varchar(8) DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `cons_id`    int(11) DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `unid_id`    int(11) DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `grupo`      char(1) DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `mes_01`     double DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `mes_02`     double DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `mes_03`     double DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `mes_04`     double DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `mes_05`     double DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `mes_06`     double DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `mes_07`     double DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `mes_08`     double DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `mes_09`     double DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `mes_10`     double DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `mes_11`     double DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `mes_12`     double DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `meses`      int(11) DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `total`      double DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `media`      double DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `percentual` double DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  `rateio`     double DEFAULT NULL," & vbCrLf
            lstrsql = lstrsql & "  PRIMARY KEY (`ano`,`zona`,`livro`,`conta`,`cons_id`,`unid_id`,`grupo`)" & vbCrLf
            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: 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()

            If vNomeRPT = "rptRelBonusItaipu" Then


                dt_3 = New DataTable()
                dt_3.Columns.Add("REF", GetType(String))
                dt_3.Columns.Add("QTDERES", GetType(String))
                dt_3.Columns.Add("KWHRES", GetType(String))
                dt_3.Columns.Add("QTDERURAL", GetType(String))
                dt_3.Columns.Add("KWHRURAL", GetType(String))


                dt_2 = New DataTable()
                dt_2.Columns.Add("CLASSE", GetType(String))
                dt_2.Columns.Add("QTDE", GetType(String))
                dt_2.Columns.Add("KWH", GetType(String))

                ds = New DataSet

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

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

                        dt_3.Rows.Add(dr_1("REF"),
                                          dr_1("QTDERES"),
                                          dr_1("KWHRES"),
                                          dr_1("QTDERURAL"),
                                          dr_1("KWHRURAL"))
                    Next dr_1
                End If


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

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

                        dt_2.Rows.Add(dr_1("CLASSE"),
                                          dr_1("QTDE"),
                                          dr_1("KWH"))
                    Next dr_1
                End If


                ds.Tables.Add(dt1)
                ds.Tables(0).TableName = "REL_BONUSITAIPU"

                ds.Tables.Add(dt2)
                ds.Tables(1).TableName = "REL_BONUSITAIPU_RESUMO"

                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 rptRelBonusItaipu

                    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("F_Titulo", "Bônus Itaipu" & txtAno.Text)

                    '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 Bônus Itaipu"
                    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