Option Strict Off
Option Explicit On

Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE
Imports CrystalDecisions.CrystalReports.Engine
Friend Class frmLeituraManual
    Inherits System.Windows.Forms.Form

    Private unid_id As String

    Dim ilngRow As Integer
    Dim idblPercentual As Double
    Dim iblnExiste As Boolean
    Dim istrMesAno As String
    Dim iblnCarga As Boolean
    Dim ilngLinha As Integer
    Dim istrCor As String
    Dim iblnConfirmaForaMedia As Boolean
    Dim iblnMostrarCortado As Boolean
    Dim vResp As Short
    Dim bAcimaPercentual As Boolean
    Dim bAbaixoPercentual As Boolean
    Const strChecked = "þ"
    Const strUnChecked = "q"

    Protected Overrides ReadOnly Property CreateParams() As CreateParams
        Get
            Dim param As CreateParams = MyBase.CreateParams
            param.ClassStyle = param.ClassStyle Or &H200
            Return param
        End Get
    End Property


#Region "Propriedades"

    Private Sub FrmLeituraManual_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load


        Try

            Me.Visible = True

            Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
            System.Windows.Forms.Application.DoEvents()

            HabilitaCampo(False)

            System.Windows.Forms.Application.DoEvents()

            Me.lblInfo.Text = " Aguarde buscando as informações"
            Me.lblInfo.Refresh()

            With Me.fmeOcorrencias
                .Visible = False
                .Enabled = False
            End With

            With Me.fmeReverso
                .Visible = False
                .Enabled = False
            End With

            With Me.fmeIrrigante
                .Visible = False
                .Enabled = False
            End With

            With Me.fmeTarifaBranca
                .Visible = False
                .Enabled = False
            End With

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

            istrMesAno = Trim(GetConfig("mes.ano.faturamento"))
            istrMesAno = IIf(istrMesAno = "-1", VB6.Format(Month(gDtBcoDados), "00") & "/" & VB6.Format(Year(gDtBcoDados), "0000"), istrMesAno)

            Me.dtpLeitura.Value = "01/" & istrMesAno
            Me.dtpLeituraAnt.Value = "01/" & VB6.Format(Me.dtpLeitura.Value.ToShortDateString(), "MM/yyyy")

            idblPercentual = CDbl(GetConfig("percentual.media")) / 100
            iblnMostrarCortado = IIf(GetConfig("caderno.leitura.cortado") = "S", True, False)

            '--- Monta o Cabeçalho da grid
            MontarGrid()

            '--- Preenche Combo Box
            PreencheCombobox()

            If Me.cmbRota.Items.Count > 1 Then
                Me.cmbRota.SelectedIndex = 1
                Me.cmbRotaIni.SelectedIndex = 1
                Me.cmbRotaFim.SelectedIndex = 1
            End If

            Me.cmbOcorrencia.SelectedIndex = 0
            Me.cmbOcorMedia.SelectedIndex = 0
            Me.cmbDiaVenc.SelectedIndex = 0

            iblnConfirmaForaMedia = (GetConfig("leitura.confirma.foramedia") = "S")

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

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

            HabilitaCampo(True)

            chkpercentual.Text = "Acima dos " & idblPercentual * 100 & "%"
            chkpercentualbaixo.Text = "Abaixo dos " & idblPercentual * 100 & "%"

            chkLeituraZerada.Text = "Leitura Anterior ou" & vbCrLf & "Leitura Atual = 0"

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub chkCortados_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles chkCortados.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub chkDesligados_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles chkDesligados.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub chkInativos_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles chkInativos.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmbDiaVenc_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles cmbDiaVenc.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmbRota_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles cmbRota.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmbRotaFim_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles cmbRotaFim.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmbRotaIni_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles cmbRotaIni.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmbOcorrencia_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles cmbOcorrencia.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
        Dim lintGeraMed As Short

        Try

            lintGeraMed = CShort(cmbOcorrencia.SelectedValue)

            With Me.mfgLeitura

                If KeyAscii = System.Windows.Forms.Keys.Return Then
                    If CShort(cmbOcorrencia.SelectedValue) = 0 Then 'se for opcao (nenhuma)
                        '.set_TextMatrix(ilngLinha, 9, "")
                        .set_TextMatrix(ilngLinha, 16, 0)
                        .set_TextMatrix(ilngLinha, 17, 0)
                        .set_TextMatrix(ilngLinha, 18, "")
                    Else
                        .set_TextMatrix(ilngLinha, 16, CShort(cmbOcorrencia.SelectedValue)) 'Gera Média
                        .set_TextMatrix(ilngLinha, 17, CShort(cmbOcorrencia.SelectedValue)) 'Código da ocorrência
                        .set_TextMatrix(ilngLinha, 18, cmbOcorrencia.Text) 'Descrição da ocorrência
                        If lintGeraMed = 1 Then
                            .set_TextMatrix(ilngLinha, 9, Val(.get_TextMatrix(ilngLinha, 8)) + Arredondar(CDbl(Me.mfgConsumos.get_TextMatrix(1, 14)), 0))
                            .set_TextMatrix(ilngLinha, 11, CalcConsumoLido(Val(.get_TextMatrix(ilngLinha, 8)), Val(.get_TextMatrix(ilngLinha, 9)), Val(.get_TextMatrix(ilngLinha, 12)), Val(.get_TextMatrix(ilngLinha, 16)), Val(.get_TextMatrix(ilngLinha, 10)), Arredondar(CDbl(Me.mfgConsumos.get_TextMatrix(1, 14)), 0)))
                            .Row = ilngLinha
                            .Col = 11
                            .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HFF)
                        ElseIf lintGeraMed = 0 Then
                            .set_TextMatrix(ilngLinha, 9, "")
                            .set_TextMatrix(ilngLinha, 16, 0)
                            .set_TextMatrix(ilngLinha, 17, CShort(cmbOcorrencia.SelectedValue)) 'Código da ocorrência
                            .set_TextMatrix(ilngLinha, 18, cmbOcorrencia.Text) 'Descrição da ocorrência
                            .Row = ilngLinha
                            .Col = 11
                            .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HFF00)
                        End If
                    End If

                    AtualizaLabels()

                End If

                With Me.fmeOcorrencias
                    .Visible = False
                    .Enabled = False
                End With

                .Focus()

                .Row = ilngLinha
                .Col = 9

            End With

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub dtpLeitura_ValueChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles dtpLeitura.ValueChanged

        Dim ldteAux As Date

        Try

            ldteAux = CDate(VB6.Format("01/" & VB6.Format(Me.dtpLeitura.Value, "MM/yyyy"), "dd/MM/yyyy"))
            ldteAux = System.DateTime.FromOADate(ldteAux.ToOADate - 1)

            If Me.dtpLeitura.Value.Day > Microsoft.VisualBasic.Day(ldteAux) Then
                Me.dtpLeituraAnt.Value = CDate(VB6.Format(Microsoft.VisualBasic.Day(ldteAux) & "/" & VB6.Format(ldteAux, "mm/yyyy"), "dd/MM/yyyy"))
            Else
                Me.dtpLeituraAnt.Value = CDate(VB6.Format(Me.dtpLeitura.Value.Day & "/" & VB6.Format(Me.dtpLeitura.Value, "MM/yyyy"), "dd/MM/yyyy"))
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub dtpLeitura_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles dtpLeitura.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try

            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub mfgConsumos_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mfgConsumos.ClickEvent


        Try


            If iblnCarga = False Then
                mfgConsumos_EnterCell(mfgConsumos, New System.EventArgs())
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub mfgConsumos_EnterCell(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mfgConsumos.EnterCell


        Try

            If iblnCarga = False Then
                With Me.mfgConsumos
                    Me.lblOcorrencia.Text = .get_TextMatrix(3, .ColSel)
                    Me.lblOcorrencia.Refresh()
                End With
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub mfgConsumos_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mfgConsumos.Leave


        Try

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

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub mfgLeitura_KeyDownEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSFlexGridLib.DMSFlexGridEvents_KeyDownEvent) Handles mfgLeitura.KeyDownEvent

        Try

            If iblnCarga = False Then
                With Me.mfgLeitura
                    If .Rows > 1 Then
                        If eventArgs.keyCode = System.Windows.Forms.Keys.F12 Then
                            With Me.fmeOcorrencias
                                .Visible = Not .Visible
                                .Enabled = Not .Enabled
                            End With
                            Me.cmbOcorrencia.Focus()
                            ilngLinha = .Row
                        ElseIf eventArgs.keyCode = System.Windows.Forms.Keys.F11 And ((.get_TextMatrix(.Row, 70) = "1") Or (.get_TextMatrix(.Row, 96) = "1")) Then
                            Me.cmdReverso.Enabled = True
                            With Me.fmeReverso
                                .Visible = Not .Visible
                                .Enabled = Not .Enabled
                            End With
                            Me.txtReversoLAnt.Text = .get_TextMatrix(.Row, 71)
                            Me.txtReversoLAtual.Text = .get_TextMatrix(.Row, 72)
                            Me.txtReversokWh.Text = .get_TextMatrix(.Row, 73)
                            Me.txtReversoResiduo.Text = .get_TextMatrix(.Row, 74)
                            If (.get_TextMatrix(.Row, 96) = "1") Then
                                Me.txtReversoLAnt.Enabled = False
                                Me.txtReversoLAtual.Enabled = False
                                Me.txtReversokWh.Enabled = False
                                Me.txtReversoResiduo.Enabled = True
                                Me.txtReversoResiduo.Focus()
                            Else
                                Me.txtReversoLAnt.Enabled = True
                                Me.txtReversoLAtual.Enabled = True
                                Me.txtReversokWh.Enabled = True
                                Me.txtReversoResiduo.Enabled = True
                                Me.txtReversoLAnt.Focus()
                            End If
                            ilngLinha = .Row
                        ElseIf eventArgs.keyCode = System.Windows.Forms.Keys.F10 And (.get_TextMatrix(.Row, 75) = "1") Then
                            Me.cmdIrrigante.Enabled = True
                            With Me.fmeIrrigante
                                .Visible = Not .Visible
                                .Enabled = Not .Enabled
                            End With
                            Me.txtIrriganteLAnt.Text = .get_TextMatrix(.Row, 76)
                            Me.txtIrriganteLAtual.Text = .get_TextMatrix(.Row, 77)
                            Me.txtIrrigantekWh.Text = .get_TextMatrix(.Row, 78)
                            Me.txtIrriganteResiduo.Text = .get_TextMatrix(.Row, 79)
                            Me.txtIrriganteLAnt.Focus()
                            ilngLinha = .Row
                        ElseIf eventArgs.keyCode = System.Windows.Forms.Keys.F9 And (.get_TextMatrix(.Row, 80) = "1") Then
                            Me.cmdTarifaBranca.Enabled = True
                            With Me.fmeTarifaBranca
                                .Top = VB6.TwipsToPixelsY(Me.fmeReverso.Top)
                                .Visible = Not .Visible
                                .Enabled = Not .Enabled
                            End With
                            Me.txtLeitAnterPonta.Text = .get_TextMatrix(.Row, 81)
                            Me.txtLeitAtualPonta.Text = .get_TextMatrix(.Row, 82)
                            Me.txtLeitAnterIntermediario.Text = .get_TextMatrix(.Row, 83)
                            Me.txtLeitAtualIntermediario.Text = .get_TextMatrix(.Row, 84)
                            Me.txtLeitAnterForaPonta.Text = .get_TextMatrix(.Row, 85)
                            Me.txtLeitAtualForaPonta.Text = .get_TextMatrix(.Row, 86)
                            Me.txtkWhPonta.Text = .get_TextMatrix(.Row, 87)
                            Me.txtResPonta.Text = .get_TextMatrix(.Row, 88)
                            Me.txtkWhIntermediario.Text = .get_TextMatrix(.Row, 89)
                            Me.txtResIntermediario.Text = .get_TextMatrix(.Row, 90)
                            Me.txtkWhForaPonta.Text = .get_TextMatrix(.Row, 91)
                            Me.txtResForaPonta.Text = .get_TextMatrix(.Row, 92)
                            Me.txtLeitAnterPonta.Focus()
                            ilngLinha = .Row
                        End If
                    End If
                End With
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub mfgLeitura_EnterCell(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mfgLeitura.EnterCell

        Try

            If mfgLeitura.Row = 0 Then
                Exit Sub
            End If

            If iblnCarga = False Then
                With Me.mfgLeitura
                    If .Rows > 1 Then
                        If .Col = 9 Or .Col = 10 Or .Col = 15 Then
                            istrCor = System.Drawing.ColorTranslator.ToOle(.CellBackColor).ToString
                            .CellFontBold = True
                            .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HFF8080)
                        End If
                        AtualizaLabels()
                        If .get_TextMatrix(.Row, 70) = "1" Or .get_TextMatrix(.Row, 96) = "1" Then
                            Me.cmdReverso.Enabled = True
                            Me.txtReversoLAnt.Text = .get_TextMatrix(.Row, 71)
                            Me.txtReversoLAtual.Text = .get_TextMatrix(.Row, 72)
                            Me.txtReversokWh.Text = .get_TextMatrix(.Row, 73)
                            Me.txtReversoResiduo.Text = .get_TextMatrix(.Row, 74)
                        Else
                            Me.cmdReverso.Enabled = False
                            Me.txtReversoLAnt.Text = ""
                            Me.txtReversoLAtual.Text = ""
                            Me.txtReversokWh.Text = ""
                            Me.txtReversoResiduo.Text = ""
                        End If

                        If .get_TextMatrix(.Row, 75) = "1" Then
                            Me.cmdIrrigante.Enabled = True
                            Me.txtIrriganteLAnt.Text = .get_TextMatrix(.Row, 76)
                            Me.txtIrriganteLAtual.Text = .get_TextMatrix(.Row, 77)
                            Me.txtIrrigantekWh.Text = .get_TextMatrix(.Row, 78)
                            Me.txtIrriganteResiduo.Text = .get_TextMatrix(.Row, 79)
                        Else
                            Me.cmdIrrigante.Enabled = False
                            Me.txtIrriganteLAnt.Text = ""
                            Me.txtIrriganteLAtual.Text = ""
                            Me.txtIrrigantekWh.Text = ""
                            Me.txtIrriganteResiduo.Text = ""
                        End If
                        If .get_TextMatrix(.Row, 80) = "1" Then
                            Me.cmdTarifaBranca.Enabled = True
                            Me.txtLeitAnterPonta.Text = .get_TextMatrix(.Row, 81)
                            Me.txtLeitAtualPonta.Text = .get_TextMatrix(.Row, 82)
                            Me.txtLeitAnterIntermediario.Text = .get_TextMatrix(.Row, 83)
                            Me.txtLeitAtualIntermediario.Text = .get_TextMatrix(.Row, 84)
                            Me.txtLeitAnterForaPonta.Text = .get_TextMatrix(.Row, 85)
                            Me.txtLeitAtualForaPonta.Text = .get_TextMatrix(.Row, 86)
                            Me.txtkWhPonta.Text = .get_TextMatrix(.Row, 87)
                            Me.txtResPonta.Text = .get_TextMatrix(.Row, 88)
                            Me.txtkWhIntermediario.Text = .get_TextMatrix(.Row, 89)
                            Me.txtResIntermediario.Text = .get_TextMatrix(.Row, 90)
                            Me.txtkWhForaPonta.Text = .get_TextMatrix(.Row, 91)
                            Me.txtResForaPonta.Text = .get_TextMatrix(.Row, 92)
                        Else
                            Me.cmdTarifaBranca.Enabled = False
                            Me.txtLeitAnterPonta.Text = ""
                            Me.txtLeitAtualPonta.Text = ""
                            Me.txtLeitAnterIntermediario.Text = ""
                            Me.txtLeitAtualIntermediario.Text = ""
                            Me.txtLeitAnterForaPonta.Text = ""
                            Me.txtLeitAtualForaPonta.Text = ""
                            Me.txtkWhPonta.Text = ""
                            Me.txtResPonta.Text = ""
                            Me.txtkWhIntermediario.Text = ""
                            Me.txtResIntermediario.Text = ""
                            Me.txtkWhForaPonta.Text = ""
                            Me.txtResForaPonta.Text = ""
                        End If

                        If .get_TextMatrix(.Row, 99) > 0 Then
                            cmdFotoEvidencia.Enabled = True
                            unid_id = .get_TextMatrix(.Row, 97)
                        Else
                            cmdFotoEvidencia.Enabled = False
                            unid_id = .get_TextMatrix(.Row, 97)
                        End If


                    Else
                        Me.cmdReverso.Enabled = False
                        Me.cmdIrrigante.Enabled = False
                        Me.cmdTarifaBranca.Enabled = False
                        cmdFotoEvidencia.Enabled = False
                    End If
                End With
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub mfgLeitura_LeaveCell(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mfgLeitura.LeaveCell

        Try

            If iblnCarga = False Then
                With Me.mfgLeitura
                    If .Rows > 1 Then
                        If .Col = 9 Or .Col = 10 Or .Col = 15 Then
                            .CellFontBold = False
                            .CellBackColor = System.Drawing.ColorTranslator.FromOle(CInt(istrCor))
                            Me.lblLeitOcorrencias.Text = ""
                            Me.lblLeitOcorrencias.Refresh()
                        End If
                    End If
                End With
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub mfgLeitura_KeyPressEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSFlexGridLib.DMSFlexGridEvents_KeyPressEvent) Handles mfgLeitura.KeyPressEvent

        Try

            If mfgLeitura.Row = 0 Then
                Exit Sub
            End If

            If iblnCarga = False Then
                With Me.mfgLeitura
                    If .Rows > 1 Then

                        If .Col = 0 Or .Col = 98 Then
                            If eventArgs.keyAscii = 13 Or eventArgs.keyAscii = 32 Then 'Enter/Space
                                AtualizaCheckBoxGrid(.Row, .Col)
                            End If
                        End If

                        If .Col = 9 Or .Col = 10 Or .Col = 15 Then
                            Select Case eventArgs.keyAscii
                                Case System.Windows.Forms.Keys.Return, System.Windows.Forms.Keys.Tab

                                    ilngRow = .Row
                                    GravaParcial()

                                    If .get_TextMatrix(.Row, 9) <> "" Then
                                        .set_TextMatrix(.Row, 11, CalcConsumoLido(Val(.get_TextMatrix(.Row, 8)), Val(.get_TextMatrix(.Row, 9)), Val(.get_TextMatrix(.Row, 12)), Val(.get_TextMatrix(.Row, 16)), Val(.get_TextMatrix(.Row, 10)), Arredondar(CDbl(Me.mfgConsumos.get_TextMatrix(1, 14)), 0)))
                                        AtualizaLabels()
                                        If iblnConfirmaForaMedia Then
                                            If InStr(Me.lblLeitOcorrencias.Text, "ACIMA") > 0 Or InStr(Me.lblLeitOcorrencias.Text, "ABAIXO") > 0 Then
                                                Beep()
                                                MessageBox.Show(Me.lblLeitOcorrencias.Text, "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                                            End If
                                        End If
                                        If Len(.get_TextMatrix(.Row, 9)) > 9 Then
                                            Beep()
                                            MessageBox.Show("A leitura digitada tem mais de 9 digitos!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                                        End If
                                    End If
                                Case System.Windows.Forms.Keys.Back
                                    'remove o ultimo caractere
                                    If Len(.Text) Then
                                        .Text = Microsoft.VisualBasic.Left(.Text, Len(.Text) - 1)
                                    End If
                                Case Else
                                    If IsNumeric(Chr(eventArgs.keyAscii)) = True Then
                                        .Text = .Text & Chr(eventArgs.keyAscii)
                                    End If
                            End Select
                        End If
                        Me.cmdReverso.Enabled = (.get_TextMatrix(.Row, 70) = "1") Or (.get_TextMatrix(.Row, 96) = "1")
                        Me.cmdIrrigante.Enabled = (.get_TextMatrix(.Row, 75) = "1")
                        Me.cmdTarifaBranca.Enabled = (.get_TextMatrix(.Row, 80) = "1")
                        cmdFotoEvidencia.Enabled = (.get_TextMatrix(.Row, 99) > 0)
                        .Focus()
                    Else
                        Me.cmdReverso.Enabled = False
                        Me.txtReversoLAnt.Text = ""
                        Me.txtReversoLAtual.Text = ""
                        Me.txtReversokWh.Text = ""
                        Me.txtReversoResiduo.Text = ""

                        Me.cmdIrrigante.Enabled = False
                        Me.txtIrriganteLAnt.Text = ""
                        Me.txtIrriganteLAtual.Text = ""
                        Me.txtIrrigantekWh.Text = ""
                        Me.txtIrriganteResiduo.Text = ""

                        Me.cmdTarifaBranca.Enabled = False
                        Me.txtLeitAnterPonta.Text = ""
                        Me.txtLeitAtualPonta.Text = ""
                        Me.txtLeitAnterIntermediario.Text = ""
                        Me.txtLeitAtualIntermediario.Text = ""
                        Me.txtLeitAnterForaPonta.Text = ""
                        Me.txtLeitAtualForaPonta.Text = ""
                        Me.txtkWhPonta.Text = ""
                        Me.txtResPonta.Text = ""
                        Me.txtkWhIntermediario.Text = ""
                        Me.txtResIntermediario.Text = ""
                        Me.txtkWhForaPonta.Text = ""
                        Me.txtResForaPonta.Text = ""

                        cmdFotoEvidencia.Enabled = False
                    End If
                End With
            End If

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

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtIrriganteLAtual_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtIrriganteLAtual.Leave

        Try


            CalcIrriganteConsumo()


        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtIrriganteResiduo_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtIrriganteResiduo.Enter

        Try


            With Me.txtIrriganteResiduo
                .SelectionStart = 0
                .SelectionLength = CInt(Len(.Text))
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtIrriganteResiduo_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtIrriganteResiduo.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)


        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then
                If KeyAscii = System.Windows.Forms.Keys.Delete Then
                    KeyAscii = System.Windows.Forms.Keys.Snapshot
                End If
                If KeyAscii <> System.Windows.Forms.Keys.Snapshot Then
                    KeyAscii = 0
                Else
                    If InStr(1, Me.txtIrriganteResiduo.Text, ",") > 0 Then
                        KeyAscii = 0
                    End If
                End If
            End If

            eventArgs.KeyChar = Chr(KeyAscii)
            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtIrriganteResiduo_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtIrriganteResiduo.Leave

        Try


            CalcIrriganteConsumo()

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtkWhForaPonta_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtkWhForaPonta.Enter

        Try


            With Me.txtkWhForaPonta
                .SelectionStart = 0
                .SelectionLength = CInt(Len(.Text))
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtkWhIntermediario_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtkWhIntermediario.Enter

        Try


            With Me.txtkWhIntermediario
                .SelectionStart = 0
                .SelectionLength = CInt(Len(.Text))
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtkWhPonta_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtkWhPonta.Enter

        Try

            With Me.txtkWhPonta
                .SelectionStart = 0
                .SelectionLength = CInt(Len(.Text))
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtLeitAnterForaPonta_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLeitAnterForaPonta.Enter

        Try


            With Me.txtLeitAnterForaPonta
                .SelectionStart = 0
                .SelectionLength = CInt(Len(.Text))
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtLeitAnterForaPonta_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtLeitAnterForaPonta.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then
                If KeyAscii = System.Windows.Forms.Keys.Delete Then
                    KeyAscii = System.Windows.Forms.Keys.Snapshot
                End If
                If KeyAscii <> System.Windows.Forms.Keys.Snapshot Then
                    KeyAscii = 0
                Else
                    If InStr(1, Me.txtLeitAnterForaPonta.Text, ",") > 0 Then
                        KeyAscii = 0
                    End If
                End If
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtLeitAnterForaPonta_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLeitAnterForaPonta.Leave

        Try

            CalcBrancaConsumo()

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtLeitAnterIntermediario_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLeitAnterIntermediario.Enter

        Try


            With Me.txtLeitAnterIntermediario
                .SelectionStart = 0
                .SelectionLength = CInt(Len(.Text))
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtLeitAnterIntermediario_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtLeitAnterIntermediario.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try

            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then
                If KeyAscii = System.Windows.Forms.Keys.Delete Then
                    KeyAscii = System.Windows.Forms.Keys.Snapshot
                End If
                If KeyAscii <> System.Windows.Forms.Keys.Snapshot Then
                    KeyAscii = 0
                Else
                    If InStr(1, Me.txtLeitAnterIntermediario.Text, ",") > 0 Then
                        KeyAscii = 0
                    End If
                End If
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtLeitAnterIntermediario_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLeitAnterIntermediario.Leave

        Try


            CalcBrancaConsumo()

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtLeitAnterPonta_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLeitAnterPonta.Enter

        Try


            With Me.txtLeitAnterPonta
                .SelectionStart = 0
                .SelectionLength = CInt(Len(.Text))
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtLeitAnterPonta_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtLeitAnterPonta.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then
                If KeyAscii = System.Windows.Forms.Keys.Delete Then
                    KeyAscii = System.Windows.Forms.Keys.Snapshot
                End If
                If KeyAscii <> System.Windows.Forms.Keys.Snapshot Then
                    KeyAscii = 0
                Else
                    If InStr(1, Me.txtLeitAnterPonta.Text, ",") > 0 Then
                        KeyAscii = 0
                    End If
                End If
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If


        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtLeitAnterPonta_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLeitAnterPonta.Leave

        Try


            CalcBrancaConsumo()


        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtLeitAtualForaPonta_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLeitAtualForaPonta.Enter

        Try

            With Me.txtLeitAtualForaPonta
                .SelectionStart = 0
                .SelectionLength = CInt(Len(.Text))
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtLeitAtualForaPonta_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtLeitAtualForaPonta.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try

            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then
                If KeyAscii = System.Windows.Forms.Keys.Delete Then
                    KeyAscii = System.Windows.Forms.Keys.Snapshot
                End If
                If KeyAscii <> System.Windows.Forms.Keys.Snapshot Then
                    KeyAscii = 0
                Else
                    If InStr(1, Me.txtLeitAtualForaPonta.Text, ",") > 0 Then
                        KeyAscii = 0
                    End If
                End If
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtLeitAtualForaPonta_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLeitAtualForaPonta.Leave

        Try

            CalcBrancaConsumo()


        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtLeitAtualIntermediario_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLeitAtualIntermediario.Enter

        Try

            With Me.txtLeitAtualIntermediario
                .SelectionStart = 0
                .SelectionLength = CInt(Len(.Text))
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtLeitAtualIntermediario_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtLeitAtualIntermediario.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then
                If KeyAscii = System.Windows.Forms.Keys.Delete Then
                    KeyAscii = System.Windows.Forms.Keys.Snapshot
                End If
                If KeyAscii <> System.Windows.Forms.Keys.Snapshot Then
                    KeyAscii = 0
                Else
                    If InStr(1, Me.txtLeitAtualIntermediario.Text, ",") > 0 Then
                        KeyAscii = 0
                    End If
                End If
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If


        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtLeitAtualIntermediario_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLeitAtualIntermediario.Leave

        Try

            CalcBrancaConsumo()


        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtLeitAtualPonta_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLeitAtualPonta.Enter

        Try

            With Me.txtLeitAtualPonta
                .SelectionStart = 0
                .SelectionLength = CInt(Len(.Text))
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtLeitAtualPonta_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtLeitAtualPonta.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then
                If KeyAscii = System.Windows.Forms.Keys.Delete Then
                    KeyAscii = System.Windows.Forms.Keys.Snapshot
                End If
                If KeyAscii <> System.Windows.Forms.Keys.Snapshot Then
                    KeyAscii = 0
                Else
                    If InStr(1, Me.txtLeitAtualPonta.Text, ",") > 0 Then
                        KeyAscii = 0
                    End If
                End If
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtLeitAtualPonta_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLeitAtualPonta.Leave

        Try

            CalcBrancaConsumo()

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtResForaPonta_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtResForaPonta.Enter

        Try


            With Me.txtResForaPonta
                .SelectionStart = 0
                .SelectionLength = CInt(Len(.Text))
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtResForaPonta_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtResForaPonta.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then
                If KeyAscii = System.Windows.Forms.Keys.Delete Then
                    KeyAscii = System.Windows.Forms.Keys.Snapshot
                End If
                If KeyAscii <> System.Windows.Forms.Keys.Snapshot Then
                    KeyAscii = 0
                Else
                    If InStr(1, Me.txtResForaPonta.Text, ",") > 0 Then
                        KeyAscii = 0
                    End If
                End If
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If


        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtResForaPonta_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtResForaPonta.Leave


        Try


            CalcBrancaConsumo()

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtResIntermediario_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtResIntermediario.Enter


        Try


            With Me.txtResIntermediario
                .SelectionStart = 0
                .SelectionLength = CInt(Len(.Text))
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtResIntermediario_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtResIntermediario.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)


        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then
                If KeyAscii = System.Windows.Forms.Keys.Delete Then
                    KeyAscii = System.Windows.Forms.Keys.Snapshot
                End If
                If KeyAscii <> System.Windows.Forms.Keys.Snapshot Then
                    KeyAscii = 0
                Else
                    If InStr(1, Me.txtResIntermediario.Text, ",") > 0 Then
                        KeyAscii = 0
                    End If
                End If
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtResIntermediario_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtResIntermediario.Leave


        Try



            CalcBrancaConsumo()

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtResPonta_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtResPonta.Enter


        Try


            With Me.txtResPonta
                .SelectionStart = 0
                .SelectionLength = CInt(Len(.Text))
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtResPonta_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtResPonta.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)


        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then
                If KeyAscii = System.Windows.Forms.Keys.Delete Then
                    KeyAscii = System.Windows.Forms.Keys.Snapshot
                End If
                If KeyAscii <> System.Windows.Forms.Keys.Snapshot Then
                    KeyAscii = 0
                Else
                    If InStr(1, Me.txtResPonta.Text, ",") > 0 Then
                        KeyAscii = 0
                    End If
                End If
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If


        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtResPonta_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtResPonta.Leave


        Try


            CalcBrancaConsumo()


        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtReversokWh_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtReversokWh.Enter


        Try

            With Me.txtReversokWh
                .SelectionStart = 0
                .SelectionLength = CInt(Len(.Text))
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtReversoLAnt_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtReversoLAnt.Enter


        Try

            With Me.txtReversoLAnt
                .SelectionStart = 0
                .SelectionLength = CInt(Len(.Text))
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtReversoLAnt_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtReversoLAnt.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)


        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then
                If KeyAscii = System.Windows.Forms.Keys.Delete Then
                    KeyAscii = System.Windows.Forms.Keys.Snapshot
                End If
                If KeyAscii <> System.Windows.Forms.Keys.Snapshot Then
                    KeyAscii = 0
                Else
                    If InStr(1, Me.txtReversoLAnt.Text, ",") > 0 Then
                        KeyAscii = 0
                    End If
                End If
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtReversoLAnt_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtReversoLAnt.Leave


        Try


            CalcReversoConsumo()

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtReversoLAtual_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtReversoLAtual.Enter


        Try

            With Me.txtReversoLAtual
                .SelectionStart = 0
                .SelectionLength = CInt(Len(.Text))
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtReversoLAtual_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtReversoLAtual.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)


        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then
                If KeyAscii = System.Windows.Forms.Keys.Delete Then
                    KeyAscii = System.Windows.Forms.Keys.Snapshot
                End If
                If KeyAscii <> System.Windows.Forms.Keys.Snapshot Then
                    KeyAscii = 0
                Else
                    If InStr(1, Me.txtReversoLAtual.Text, ",") > 0 Then
                        KeyAscii = 0
                    End If
                End If
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtReversoLAtual_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtReversoLAtual.Leave

        Try


            CalcReversoConsumo()

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtReversoResiduo_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtReversoResiduo.Enter

        Try


            With Me.txtReversoResiduo
                .SelectionStart = 0
                .SelectionLength = CInt(Len(.Text))
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtReversoResiduo_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtReversoResiduo.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then
                If KeyAscii = System.Windows.Forms.Keys.Delete Then
                    KeyAscii = System.Windows.Forms.Keys.Snapshot
                End If
                If KeyAscii <> System.Windows.Forms.Keys.Snapshot Then
                    KeyAscii = 0
                Else
                    If InStr(1, Me.txtReversoResiduo.Text, ",") > 0 Then
                        KeyAscii = 0
                    End If
                End If
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtReversoResiduo_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtReversoResiduo.Leave

        Try


            CalcReversoConsumo()

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtIrrigantekWh_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtIrrigantekWh.Enter

        Try


            With Me.txtIrrigantekWh
                .SelectionStart = 0
                .SelectionLength = CInt(Len(.Text))
            End With


        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtIrriganteLAnt_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtIrriganteLAnt.Enter

        Try

            With Me.txtIrriganteLAnt
                .SelectionStart = 0
                .SelectionLength = CInt(Len(.Text))
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtIrriganteLAnt_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtIrriganteLAnt.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try

            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then
                If KeyAscii = System.Windows.Forms.Keys.Delete Then
                    KeyAscii = System.Windows.Forms.Keys.Snapshot
                End If
                If KeyAscii <> System.Windows.Forms.Keys.Snapshot Then
                    KeyAscii = 0
                Else
                    If InStr(1, Me.txtIrriganteLAnt.Text, ",") > 0 Then
                        KeyAscii = 0
                    End If
                End If
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtIrriganteLAnt_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtIrriganteLAnt.Leave

        Try


            CalcIrriganteConsumo()


        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtIrriganteLAtual_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtIrriganteLAtual.Enter

        Try


            With Me.txtIrriganteLAtual
                .SelectionStart = 0
                .SelectionLength = CInt(Len(.Text))
            End With


        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtIrriganteLAtual_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtIrriganteLAtual.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then
                If KeyAscii = System.Windows.Forms.Keys.Delete Then
                    KeyAscii = System.Windows.Forms.Keys.Snapshot
                End If
                If KeyAscii <> System.Windows.Forms.Keys.Snapshot Then
                    KeyAscii = 0
                Else
                    If InStr(1, Me.txtIrriganteLAtual.Text, ",") > 0 Then
                        KeyAscii = 0
                    End If
                End If
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If


        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmdOkBranca_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdOkBranca.Click


        Try

            With Me.mfgLeitura
                .set_TextMatrix(ilngLinha, 81, Me.txtLeitAnterPonta.Text)
                .set_TextMatrix(ilngLinha, 82, Me.txtLeitAtualPonta.Text)
                .set_TextMatrix(ilngLinha, 83, Me.txtLeitAnterIntermediario.Text)
                .set_TextMatrix(ilngLinha, 84, Me.txtLeitAtualIntermediario.Text)
                .set_TextMatrix(ilngLinha, 85, Me.txtLeitAnterForaPonta.Text)
                .set_TextMatrix(ilngLinha, 86, Me.txtLeitAtualForaPonta.Text)
                .set_TextMatrix(ilngLinha, 87, Me.txtkWhPonta.Text)
                .set_TextMatrix(ilngLinha, 88, Me.txtResPonta.Text)
                .set_TextMatrix(ilngLinha, 89, Me.txtkWhIntermediario.Text)
                .set_TextMatrix(ilngLinha, 90, Me.txtResIntermediario.Text)
                .set_TextMatrix(ilngLinha, 91, Me.txtkWhForaPonta.Text)
                .set_TextMatrix(ilngLinha, 92, Me.txtResForaPonta.Text)
                'Zera Leitura anterior e soma os kWh ponta+intermediario+foraponta
                .set_TextMatrix(ilngLinha, 8, 0)
                .set_TextMatrix(ilngLinha, 9, CDbl(IIf(IsNumeric(Me.txtkWhPonta.Text), Me.txtkWhPonta.Text, 0)) + CDbl(IIf(IsNumeric(Me.txtkWhIntermediario.Text), Me.txtkWhIntermediario.Text, 0)) + CDbl(IIf(IsNumeric(Me.txtkWhForaPonta.Text), Me.txtkWhForaPonta.Text, 0)))
                cmdFecharBranca_Click(cmdFecharBranca, New System.EventArgs())
                .Focus()
                .Row = ilngLinha
                .Col = 9
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmdOkIrrigante_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdOkIrrigante.Click


        Try

            With Me.mfgLeitura
                .set_TextMatrix(ilngLinha, 76, Me.txtIrriganteLAnt.Text)
                .set_TextMatrix(ilngLinha, 77, Me.txtIrriganteLAtual.Text)
                .set_TextMatrix(ilngLinha, 78, Me.txtIrrigantekWh.Text)
                .set_TextMatrix(ilngLinha, 79, Me.txtIrriganteResiduo.Text)
                cmdFecharIrrigante_Click(cmdFecharIrrigante, New System.EventArgs())
                .Focus()
                .Row = ilngLinha
                .Col = 9
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmdReverso_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdReverso.Click

        Try

            mfgLeitura_KeyDownEvent(mfgLeitura, New AxMSFlexGridLib.DMSFlexGridEvents_KeyDownEvent(System.Windows.Forms.Keys.F11, 0))


        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmdReversoFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdReversoFechar.Click

        Try

            With Me.fmeReverso
                .Visible = False
                .Enabled = False
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmdReversoOk_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdReversoOk.Click

        Try


            With Me.mfgLeitura
                .set_TextMatrix(ilngLinha, 71, Me.txtReversoLAnt.Text)
                .set_TextMatrix(ilngLinha, 72, Me.txtReversoLAtual.Text)
                .set_TextMatrix(ilngLinha, 73, Me.txtReversokWh.Text)
                .set_TextMatrix(ilngLinha, 74, Me.txtReversoResiduo.Text)
                Call cmdReversoFechar_Click(cmdReversoFechar, New System.EventArgs())
                Call .Focus()
                .Row = ilngLinha
                .Col = 9
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmdTarifaBranca_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdTarifaBranca.Click

        Try

            mfgLeitura_KeyDownEvent(mfgLeitura, New AxMSFlexGridLib.DMSFlexGridEvents_KeyDownEvent(System.Windows.Forms.Keys.F9, 0))


        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmdFecharBranca_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFecharBranca.Click

        Try

            With Me.fmeTarifaBranca
                .Visible = False
                .Enabled = False
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmdFecharIrrigante_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFecharIrrigante.Click

        Try

            With Me.fmeIrrigante
                .Visible = False
                .Enabled = False
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmdIrrigante_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdIrrigante.Click

        Try

            mfgLeitura_KeyDownEvent(mfgLeitura, New AxMSFlexGridLib.DMSFlexGridEvents_KeyDownEvent(System.Windows.Forms.Keys.F10, 0))

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmdOcorrencia_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdOcorrencia.Click

        Try


            mfgLeitura_KeyDownEvent(mfgLeitura, New AxMSFlexGridLib.DMSFlexGridEvents_KeyDownEvent(System.Windows.Forms.Keys.F12, 0))


        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmdFecharOcorrencias_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFecharOcorrencias.Click

        Try
            cmbOcorrencia_KeyPress(cmbOcorrencia, New System.Windows.Forms.KeyPressEventArgs(Chr(System.Windows.Forms.Keys.Return)))
        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmdOkOcorrencias_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdOkOcorrencias.Click


        Try


            cmdFecharOcorrencias_Click(cmdFecharOcorrencias, New System.EventArgs())


        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmdBusca_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdBusca.Click

        Dim lblnResposta As Boolean

        Try

            lblnResposta = True

            If istrMesAno <> VB6.Format(Me.dtpLeitura.Value, "mm/yyyy") Then

                vResp = MessageBox.Show("Mês e ano requisitado é diferente de " & istrMesAno & ". Deseja continuar digitando?", "Informação", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)

                If vResp = DialogResult.Yes Then
                    lblnResposta = True
                Else
                    lblnResposta = False
                End If

            End If

            If lblnResposta Then

                HabilitaCampo(False)
                PreencherGrid()
                HabilitaCampo(True)

                With mfgLeitura
                    If .Rows > 1 Then
                        .Focus()
                        .Row = 1
                        .Col = 9
                        BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, 1)
                    End If
                End With

            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - 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

        Try

            lClose()

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub cmdGravar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdGravar.Click

        Dim lboolVerificado As Boolean
        Dim lboolLiberaMediaCritica As Boolean
        Dim llngLinha As Integer
        Dim llngUnidId As Integer
        Dim llngConsId As Integer
        Dim lstrSql As String
        Dim lstrZona As String
        Dim lstrLivro As String
        Dim lstrConta As String
        Dim lstrRota As String
        Dim lstrNome As String
        Dim lstrEndereco As String
        Dim lstrCidade As String
        Dim lstrSituacao As String
        Dim lstrMedidor As String
        Dim lstrLeiAnter As String
        Dim lstrLeiAtual As String
        Dim lstrMedidorKvarh As String
        Dim lstrLeitantkvarh As String
        Dim lstrLeitatualkvarh As String
        Dim lstrSequencia As String
        Dim ldblResiduo As Double
        Dim lstrOcorrencia As String
        Dim lblnResposta As Boolean
        Dim lintGradeMedia As Short
        Dim ldblConstante As Double

        'Microgeração
        Dim lintMicroGeracao As Short
        Dim lstrReversoLAnter As String
        Dim lstrReversoLAtual As String
        Dim ldblReversoConsumo As Double
        Dim ldblReversoResiduo As Double
        'Irrigante
        Dim lstrIrriganteLAnter As String
        Dim lstrIrriganteLAtual As String
        Dim ldblIrriganteConsumo As Double
        Dim ldblIrriganteResiduo As Double
        'Tarifa Branca
        Dim lintTarifaBranca As Short
        Dim lstrLeitAtualPonta As String
        Dim lstrLeitAtualInter As String
        Dim lstrLeitAtualForaP As String
        Dim lstrLeitAnterPonta As String
        Dim lstrLeitAnterInter As String
        Dim lstrLeitAnterForaP As String
        Dim ldblkWhPonta As Double
        Dim ldblkWhInter As Double
        Dim ldblkWhForaP As Double
        Dim ldblResPonta As Double
        Dim ldblResInter As Double
        Dim ldblResForaP As Double
        Dim ldblConstPonta As Double
        Dim ldblConstInter As Double
        Dim ldblConstForaP As Double


        Dim objUCFaturamento As New entidadeUCFaturamento
        Dim repositorioUCFaturamento As New repositorioUCFaturamento

        Dim vListaLeitMensalOld
        Dim objLeitmensal As New entidadeLeitmensal
        Dim objLeitmensal_Old As New entidadeLeitmensal
        Dim repositorioLeitmensal As New repositorioLeitmensal

        Dim dt As DataTable

        Dim vlist

        Try

            HabilitaCampo(False)

            If iblnExiste Then
                vResp = MessageBox.Show("Já existe esta rota no arquivo de leituras! Deseja substituir as leituras?", "Informação", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
            End If

            If vResp = DialogResult.Yes Or Not iblnExiste Then

                With Me.mfgLeitura

                    BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .Rows - 1)

                    For llngLinha = 1 To .Rows - 1


                        If lblInfo.Text.Contains("48") Then
                            Dim entrou = ""
                        End If

                        lboolVerificado = IIf(.get_TextMatrix(llngLinha, 0) = strChecked, 1, 0)
                        lboolLiberaMediaCritica = IIf(.get_TextMatrix(llngLinha, 98) = strChecked, 1, 0)
                        llngUnidId = CInt(.get_TextMatrix(llngLinha, 97)) 'Antes era a coluna 0
                        lstrZona = .get_TextMatrix(llngLinha, 1)
                        lstrLivro = .get_TextMatrix(llngLinha, 2)
                        lstrConta = .get_TextMatrix(llngLinha, 3)
                        lstrNome = .get_TextMatrix(llngLinha, 4)
                        lstrSituacao = .get_TextMatrix(llngLinha, 5)
                        lstrMedidor = .get_TextMatrix(llngLinha, 6)
                        lstrSequencia = .get_TextMatrix(llngLinha, 7)
                        lstrLeiAnter = .get_TextMatrix(llngLinha, 8)
                        lstrLeiAtual = .get_TextMatrix(llngLinha, 9)
                        ldblResiduo = IIf(Len(Trim(.get_TextMatrix(llngLinha, 10))) = 0, 0, .get_TextMatrix(llngLinha, 10))
                        ldblConstante = IIf(Len(Trim(.get_TextMatrix(llngLinha, 12))) = 0, 0, .get_TextMatrix(llngLinha, 12))
                        lstrMedidorKvarh = .get_TextMatrix(llngLinha, 13)
                        lstrLeitantkvarh = .get_TextMatrix(llngLinha, 14)
                        lstrLeitatualkvarh = CStr(Val(.get_TextMatrix(llngLinha, 15)))
                        lintGradeMedia = Val(.get_TextMatrix(llngLinha, 16))
                        lstrOcorrencia = CStr(Val(.get_TextMatrix(llngLinha, 17)))

                        'Microgeração
                        lintMicroGeracao = CShort(.get_TextMatrix(llngLinha, 70))
                        lstrReversoLAnter = .get_TextMatrix(llngLinha, 71)
                        lstrReversoLAtual = .get_TextMatrix(llngLinha, 72)
                        ldblReversoConsumo = Val(.get_TextMatrix(llngLinha, 73))
                        ldblReversoResiduo = Val(.get_TextMatrix(llngLinha, 74))

                        If ldblReversoConsumo > 0 Then
                            ldblReversoConsumo = ldblReversoConsumo
                        End If


                        'Irrigante
                        lstrIrriganteLAnter = .get_TextMatrix(llngLinha, 76)
                        lstrIrriganteLAtual = .get_TextMatrix(llngLinha, 77)
                        ldblIrriganteConsumo = Val(.get_TextMatrix(llngLinha, 78))
                        ldblIrriganteResiduo = Val(.get_TextMatrix(llngLinha, 79))

                        'Tarifa Branca
                        lintTarifaBranca = CShort(.get_TextMatrix(llngLinha, 80))
                        lstrLeitAnterPonta = .get_TextMatrix(llngLinha, 81)
                        lstrLeitAtualPonta = .get_TextMatrix(llngLinha, 82)
                        lstrLeitAnterInter = .get_TextMatrix(llngLinha, 83)
                        lstrLeitAtualInter = .get_TextMatrix(llngLinha, 84)
                        lstrLeitAnterForaP = .get_TextMatrix(llngLinha, 85)
                        lstrLeitAtualForaP = .get_TextMatrix(llngLinha, 86)
                        ldblkWhPonta = CDbl(IIf(IsNumeric(.get_TextMatrix(llngLinha, 87)), .get_TextMatrix(llngLinha, 87), 0))
                        ldblkWhInter = CDbl(IIf(IsNumeric(.get_TextMatrix(llngLinha, 89)), .get_TextMatrix(llngLinha, 89), 0))
                        ldblkWhForaP = CDbl(IIf(IsNumeric(.get_TextMatrix(llngLinha, 91)), .get_TextMatrix(llngLinha, 91), 0))
                        ldblResPonta = CDbl(IIf(IsNumeric(.get_TextMatrix(llngLinha, 88)), .get_TextMatrix(llngLinha, 88), 0))
                        ldblResInter = CDbl(IIf(IsNumeric(.get_TextMatrix(llngLinha, 90)), .get_TextMatrix(llngLinha, 90), 0))
                        ldblResForaP = CDbl(IIf(IsNumeric(.get_TextMatrix(llngLinha, 92)), .get_TextMatrix(llngLinha, 92), 0))
                        ldblConstPonta = CDbl(.get_TextMatrix(llngLinha, 93))
                        ldblConstInter = CDbl(.get_TextMatrix(llngLinha, 94))
                        ldblConstForaP = CDbl(.get_TextMatrix(llngLinha, 95))

                        'Achar classificacão unidade consumidora
                        objUCFaturamento.ZONA = lstrZona
                        objUCFaturamento.LIVRO = lstrLivro
                        objUCFaturamento.CONTA = lstrConta
                        objUCFaturamento.ID = llngUnidId
                        'objUCFaturamento.UNID_ID = llngUnidId

                        'vlist = repositorioUCFaturamento.Select_5(objUCFaturamento)
                        vlist = repositorioUCFaturamento.Select_22(objUCFaturamento)
                        Dim i = 1
                        For Each objUCFaturamentoFor As entidadeUCFaturamento In vlist

                            lstrRota = IIf(IsDBNull(objUCFaturamentoFor.ROTA), "00000000", objUCFaturamentoFor.ROTA)
                            lstrEndereco = IIf(IsDBNull(objUCFaturamentoFor.END), "", objUCFaturamentoFor.END)
                            lstrCidade = IIf(IsDBNull(objUCFaturamentoFor.CIDADE), "", objUCFaturamentoFor.CIDADE)
                            llngConsId = IIf(IsDBNull(objUCFaturamentoFor.CONS_ID), 0, objUCFaturamentoFor.CONS_ID)

                            objLeitmensal_Old.ZONA = lstrZona
                            objLeitmensal_Old.LIVRO = lstrLivro
                            objLeitmensal_Old.CONTA = lstrConta
                            objLeitmensal_Old.UNID_ID = llngUnidId
                            objLeitmensal_Old.DT_LEIT = Me.dtpLeitura.Value

                            vListaLeitMensalOld = repositorioLeitmensal.Select_1(objLeitmensal_Old)

                            objLeitmensal.ZONA = lstrZona
                            objLeitmensal.LIVRO = lstrLivro
                            objLeitmensal.CONTA = lstrConta
                            objLeitmensal.UNID_ID = llngUnidId
                            objLeitmensal.DT_LEIT = Me.dtpLeitura.Value

                            dt = repositorioLeitmensal.SelectCount(objLeitmensal)

                            If Convert.ToInt32(dt.Rows(0).ItemArray(0).ToString()) > 0 Then

                                objLeitmensal_Old = vListaLeitMensalOld(0)

                                objLeitmensal.ROTA = lstrRota
                                objLeitmensal.SEQ = lstrSequencia
                                objLeitmensal.CONS_ID = llngConsId

                                objLeitmensal.MEDIDOR = lstrMedidor

                                objLeitmensal.LEIT_ANT = Replace(Replace(IIf(lstrLeiAnter = "", 0, lstrLeiAnter), ",", ""), ".", "")
                                objLeitmensal.LEIT_ATUAL = Replace(Replace(IIf(lstrLeiAtual = "", 0, lstrLeiAtual), ",", ""), ".", "") 'Replace(Replace(lstrLeiAtual, ",", ""), ".", "")
                                objLeitmensal.MEDKVARH = lstrMedidorKvarh
                                objLeitmensal.KVARH = IIf(Trim(lstrLeitantkvarh) = "", 0, lstrLeitantkvarh)
                                objLeitmensal.KVARHATUAL = IIf(Trim(lstrLeitatualkvarh) = "", 0, lstrLeitatualkvarh)
                                objLeitmensal.OCORRENC = lstrOcorrencia
                                objLeitmensal.RESIDUO = Replace(CStr(ldblResiduo), ",", ".")
                                objLeitmensal.ENDERECO = IIf(lstrEndereco = "", lstrEndereco, Replace(lstrEndereco, "'", "\'"))
                                objLeitmensal.NOME = Replace(lstrNome, "'", "\'")
                                objLeitmensal.MUNICIP = Replace(lstrCidade, "'", "\'")
                                objLeitmensal.DT_LEIT = VB6.Format(Me.dtpLeitura.Value, "yyyy-MM-dd")
                                objLeitmensal.DT_LEITANT = VB6.Format(Me.dtpLeituraAnt.Value, "yyyy-MM-dd")

                                'Reverso
                                objLeitmensal.LEIT_REVERSO_ANT = Replace(Replace(IIf(lstrReversoLAnter = "", 0, lstrReversoLAnter), ",", ""), ".", "") 'Replace(Replace(lstrReversoLAnter, ",", ""), ".", "")
                                objLeitmensal.LEIT_REVERSO_ATUAL = Replace(Replace(IIf(lstrReversoLAtual = "", 0, lstrReversoLAtual), ",", ""), ".", "") 'Replace(Replace(lstrReversoLAtual, ",", ""), ".", "")
                                objLeitmensal.KWH_REVERSO = Replace(CStr(ldblReversoConsumo), ",", ".")
                                objLeitmensal.KWH_REVERSO_RES = Replace(CStr(ldblReversoResiduo), ",", ".")

                                'Irrigante
                                objLeitmensal.LEIT_IRRIGANTE_ANT = Replace(Replace(IIf(lstrIrriganteLAnter = "", 0, lstrIrriganteLAnter), ",", ""), ".", "") 'Replace(Replace(lstrIrriganteLAnter, ",", ""), ".", "")
                                objLeitmensal.LEIT_IRRIGANTE_ATUAL = Replace(Replace(IIf(lstrIrriganteLAtual = "", 0, lstrIrriganteLAtual), ",", ""), ".", "") 'Replace(Replace(lstrIrriganteLAtual, ",", ""), ".", "")
                                objLeitmensal.KWH_IRRIGANTE = Replace(CStr(ldblIrriganteConsumo), ",", ".")
                                objLeitmensal.KWH_IRRIGANTE_RES = Replace(CStr(ldblIrriganteResiduo), ",", ".")

                                'Tarifa Branca
                                objLeitmensal.LEITURA_ANT_PONTA = Replace(Replace(IIf(lstrLeitAnterPonta = "", 0, lstrLeitAnterPonta), ",", ""), ".", "") 'Replace(Replace(lstrLeitAnterPonta, ",", ""), ".", "")
                                objLeitmensal.LEITURA_ATUAL_PONTA = Replace(Replace(IIf(lstrLeitAtualPonta = "", 0, lstrLeitAtualPonta), ",", ""), ".", "") 'Replace(Replace(lstrLeitAtualPonta, ",", ""), ".", "")
                                objLeitmensal.LEITURA_ANT_INTERMEDIARIO = Replace(Replace(IIf(lstrLeitAnterInter = "", 0, lstrLeitAnterInter), ",", ""), ".", "") 'Replace(Replace(lstrLeitAnterInter, ",", ""), ".", "")
                                objLeitmensal.LEITURA_ATUAL_INTERMEDIARIO = Replace(Replace(IIf(lstrLeitAtualInter = "", 0, lstrLeitAtualInter), ",", ""), ".", "") 'Replace(Replace(lstrLeitAtualInter, ",", ""), ".", "")
                                objLeitmensal.LEITURA_ANT_FORA_PONTA = Replace(Replace(IIf(lstrLeitAnterForaP = "", 0, lstrLeitAnterForaP), ",", ""), ".", "") 'Replace(Replace(lstrLeitAnterForaP, ",", ""), ".", "")
                                objLeitmensal.LEITURA_ATUAL_FORA_PONTA = Replace(Replace(IIf(lstrLeitAtualForaP = "", 0, lstrLeitAtualForaP), ",", ""), ".", "") 'Replace(Replace(lstrLeitAtualForaP, ",", ""), ".", "")

                                objLeitmensal.KWH_PONTA = Replace(Replace(CStr(ldblkWhPonta), ".", ""), ",", ".")
                                objLeitmensal.KWH_INTERMEDIARIO = Replace(Replace(CStr(ldblkWhInter), ".", ""), ",", ".")
                                objLeitmensal.KWH_FORA_PONTA = Replace(Replace(CStr(ldblkWhForaP), ".", ""), ",", ".")
                                objLeitmensal.RESIDUO_PONTA = Replace(Replace(CStr(ldblResPonta), ".", ""), ",", ".")
                                objLeitmensal.RESIDUO_INTERMEDIARIO = Replace(Replace(CStr(ldblResInter), ".", ""), ",", ".")
                                objLeitmensal.RESIDUO_FORA_PONTA = Replace(Replace(CStr(ldblResForaP), ".", ""), ",", ".")
                                '
                                objLeitmensal.MEDIA = lintGradeMedia
                                '
                                objLeitmensal.ZONA = lstrZona
                                objLeitmensal.LIVRO = lstrLivro
                                objLeitmensal.CONTA = lstrConta
                                objLeitmensal.UNID_ID = llngUnidId
                                objLeitmensal.VERIFICADO = lboolVerificado
                                objLeitmensal.MEDIA_CRITICA = lboolLiberaMediaCritica
                                '
                                objLeitmensal.INT_MICRO_GERACAO = lintMicroGeracao

                                lUpdate_2(objLeitmensal)
                                SetLogLeimensal(objLeitmensal_Old, objLeitmensal)

                            Else

                                objLeitmensal.STATUS = 3
                                objLeitmensal.ROTA = lstrRota
                                objLeitmensal.SEQ = lstrSequencia
                                objLeitmensal.ZONA = lstrZona
                                objLeitmensal.LIVRO = lstrLivro
                                objLeitmensal.CONTA = lstrConta
                                objLeitmensal.LEIT_ANT = Replace(Replace(IIf(lstrLeiAnter = "", 0, lstrLeiAnter), ",", ""), ".", "") 'Replace(Replace(lstrLeiAnter, ",", ""), ".", "")
                                objLeitmensal.LEIT_ATUAL = Replace(Replace(IIf(lstrLeiAtual = "", 0, lstrLeiAtual), ",", ""), ".", "") 'Replace(Replace(lstrLeiAtual, ",", ""), ".", "")
                                objLeitmensal.MEDIDOR = lstrMedidor
                                objLeitmensal.ENDERECO = Replace(lstrEndereco, "'", "\'")
                                objLeitmensal.NOME = Replace(lstrNome, "'", "\'")
                                objLeitmensal.MUNICIP = Replace(lstrCidade, "'", "\'")
                                objLeitmensal.MEDIA = lintGradeMedia
                                objLeitmensal.OCORRENC = lstrOcorrencia
                                objLeitmensal.RESIDUO = Replace(CStr(ldblResiduo), ",", ".")
                                objLeitmensal.MEDKVARH = lstrMedidorKvarh
                                objLeitmensal.KVARH = IIf(Trim(lstrLeitantkvarh) = "", 0, lstrLeitantkvarh)
                                objLeitmensal.KVARHATUAL = IIf(Trim(lstrLeitatualkvarh) = "", 0, lstrLeitatualkvarh)
                                objLeitmensal.DT_LEIT = VB6.Format(Me.dtpLeitura.Value, "yyyy-MM-dd")
                                objLeitmensal.DT_LEITANT = VB6.Format(Me.dtpLeituraAnt.Value, "yyyy-MM-dd")
                                objLeitmensal.CONS_ID = llngConsId
                                objLeitmensal.UNID_ID = llngUnidId

                                'Reverso
                                objLeitmensal.LEIT_REVERSO_ANT = Replace(Replace(IIf(lstrReversoLAnter = "", 0, lstrReversoLAnter), ",", ""), ".", "") 'Replace(Replace(lstrReversoLAnter, ",", ""), ".", "")
                                objLeitmensal.LEIT_REVERSO_ATUAL = Replace(Replace(IIf(lstrReversoLAtual = "", 0, lstrReversoLAtual), ",", ""), ".", "") 'Replace(Replace(lstrReversoLAtual, ",", ""), ".", "")
                                objLeitmensal.KWH_REVERSO = Replace(CStr(ldblReversoConsumo), ",", ".")
                                objLeitmensal.KWH_REVERSO_RES = Replace(CStr(ldblReversoResiduo), ",", ".")

                                'Irrigante
                                objLeitmensal.LEIT_IRRIGANTE_ANT = Replace(Replace(IIf(lstrIrriganteLAnter = "", 0, lstrIrriganteLAnter), ",", ""), ".", "") 'Replace(Replace(lstrIrriganteLAnter, ",", ""), ".", "")
                                objLeitmensal.LEIT_IRRIGANTE_ATUAL = Replace(Replace(IIf(lstrIrriganteLAtual = "", 0, lstrIrriganteLAtual), ",", ""), ".", "") 'Replace(Replace(lstrIrriganteLAtual, ",", ""), ".", "")
                                objLeitmensal.KWH_IRRIGANTE = Replace(CStr(ldblIrriganteConsumo), ",", ".")
                                objLeitmensal.KWH_IRRIGANTE_RES = Replace(CStr(ldblIrriganteResiduo), ",", ".")

                                'Tarifa Branca
                                objLeitmensal.LEITURA_ANT_PONTA = Replace(Replace(IIf(lstrLeitAnterPonta = "", 0, lstrLeitAnterPonta), ",", ""), ".", "") 'Replace(Replace(lstrLeitAnterPonta, ",", ""), ".", "") ' 
                                objLeitmensal.LEITURA_ATUAL_PONTA = Replace(Replace(IIf(lstrLeitAtualPonta = "", 0, lstrLeitAtualPonta), ",", ""), ".", "") 'Replace(Replace(lstrLeitAtualPonta, ",", ""), ".", "")
                                objLeitmensal.LEITURA_ANT_INTERMEDIARIO = Replace(Replace(IIf(lstrLeitAnterInter = "", 0, lstrLeitAnterInter), ",", ""), ".", "") 'Replace(Replace(lstrLeitAnterInter, ",", ""), ".", "")
                                objLeitmensal.LEITURA_ATUAL_INTERMEDIARIO = Replace(Replace(IIf(lstrLeitAtualInter = "", 0, lstrLeitAtualInter), ",", ""), ".", "") 'Replace(Replace(lstrLeitAtualInter, ",", ""), ".", "")
                                objLeitmensal.LEITURA_ANT_FORA_PONTA = Replace(Replace(IIf(lstrLeitAnterForaP = "", 0, lstrLeitAnterForaP), ",", ""), ".", "") 'Replace(Replace(lstrLeitAnterForaP, ",", ""), ".", "")
                                objLeitmensal.LEITURA_ATUAL_FORA_PONTA = Replace(Replace(IIf(lstrLeitAtualForaP = "", 0, lstrLeitAtualForaP), ",", ""), ".", "") 'Replace(Replace(lstrLeitAtualForaP, ",", ""), ".", "")
                                objLeitmensal.KWH_PONTA = Replace(Replace(CStr(ldblkWhPonta), ".", ""), ",", ".") 'Replace(Replace(CStr(ldblkWhPonta), ".", ""), ",", ".")
                                objLeitmensal.KWH_INTERMEDIARIO = Replace(Replace(CStr(ldblkWhInter), ".", ""), ",", ".")
                                objLeitmensal.KWH_FORA_PONTA = Replace(Replace(CStr(ldblkWhForaP), ".", ""), ",", ".")
                                objLeitmensal.RESIDUO_PONTA = Replace(Replace(CStr(ldblResPonta), ".", ""), ",", ".")
                                objLeitmensal.RESIDUO_INTERMEDIARIO = Replace(Replace(CStr(ldblResInter), ".", ""), ",", ".")
                                objLeitmensal.RESIDUO_FORA_PONTA = Replace(Replace(CStr(ldblResForaP), ".", ""), ",", ".")
                                '
                                objLeitmensal.SIT = lstrSituacao
                                objLeitmensal.VERIFICADO = lboolVerificado
                                objLeitmensal.MEDIA_CRITICA = lboolLiberaMediaCritica
                                '
                                objLeitmensal.INT_MICRO_GERACAO = lintMicroGeracao

                                lInsert(objLeitmensal)

                            End If

                        Next

                        BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
                        System.Windows.Forms.Application.DoEvents()

                    Next llngLinha

                    MessageBox.Show("Gravação completada!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)

                    .Rows = 1

                End With

            End If


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

            HabilitaCampo(True)

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: EncheGradeUnidadeConsumidora" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            'MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

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

        Dim lintMicroGeracao As Short
        Dim lintIrrigacaoAquicultura As Short
        Dim lintTarifaBranca As Short
        Dim lstrZona As String
        Dim lstrLivro As String
        Dim lstrConta As String
        Dim llngUnidId As Integer
        Dim lstrNome As String
        Dim lstrEndereco As String
        Dim lstrBairro As String
        Dim lstrNumero As String
        Dim lstrQuadra As String
        Dim lstrLote As String
        Dim lstrComplemento As String
        Dim lstrRota As String
        Dim lstrSituacao As String
        Dim lstrUltimaLeit As String
        Dim lstrSequencia As String

        Dim lstrTarBrancaULei_P As String
        Dim lstrTarBrancaULei_I As String
        Dim lstrTarBrancaULei_FP As String

        Dim lstrIrriganteULei_N As String
        Dim lstrIrriganteULei_R As String

        Dim lstrMicroGerULei_R As String
        Dim lstrMicroGerULei_N As String

        Dim lstrMedidor As String
        Dim lstrClasse As String
        Dim lstrFase As String
        Dim lstrRegiao As String
        Dim ldblConstante As String
        Dim lstrLeitAtual As String
        Dim lstrLeitAjust As String
        Dim lstrRotaAux As String
        Dim lstrAux As String
        Dim lstrAux1 As String
        Dim lstrAux2 As String
        Dim llngTotCons As Integer
        Dim llngTotCDes As Integer
        Dim lstrTemp As String


        Dim nome_empresa As String
        Dim endereco_empresa As String
        Dim telefone_empresa As String
        Dim site_empresa As String
        Dim titulo_relatorio As String
        Dim nome_endereco As String
        Dim sequencia_identificacao As String
        Dim leitura_atual As String
        Dim leitura_anterior As String
        Dim medidior As String
        Dim kvarh As String
        Dim classe_fase As String
        Dim constt As String
        Dim ult_3_cons As String
        Dim rota As String

        Dim objUCFaturamento As New entidadeUCFaturamento
        Dim repositorioUCFaturamento As New repositorioUCFaturamento

        Dim objLeitmensal As New entidadeLeitmensal
        Dim repositorioLeitmensal As New repositorioLeitmensal

        Dim dt As DataTable

        Dim dc As DataColumn

        Dim vlist
        Dim vlist_1

        Try

            HabilitaCampo(False)

            If Len(Trim(Trim(Me.cmbRotaIni.Text))) > 0 Then

                'objUCFaturamento.ROTA = (Trim(Me.cmbRotaIni.Text) & "0001")
                'objUCFaturamento.AGRUPA = (Trim(Me.cmbRotaFim.Text) & "9999")
                objUCFaturamento.ROTA = Me.cmbRotaIni.Text
                objUCFaturamento.AGRUPA = cmbRotaFim.Text

                If Me.chkDesligados.CheckState = 0 Then
                    objUCFaturamento.SITUACAO = objUCFaturamento.SITUACAO & "'D',"
                End If
                If Me.chkCortados.CheckState = 0 Then
                    objUCFaturamento.SITUACAO = objUCFaturamento.SITUACAO & "'C',"
                End If
                If Me.chkInativos.CheckState = 0 Then
                    objUCFaturamento.SITUACAO = objUCFaturamento.SITUACAO & "'I',"
                End If
                If Me.chkLigados.CheckState = 0 Then
                    objUCFaturamento.SITUACAO = objUCFaturamento.SITUACAO & "'L',"
                End If

                If objUCFaturamento.SITUACAO <> "" Then
                    objUCFaturamento.SITUACAO = Mid(objUCFaturamento.SITUACAO, 1, Len(objUCFaturamento.SITUACAO) - 1)
                End If

                vlist = repositorioUCFaturamento.Select_21(objUCFaturamento)

                If vlist.Count > 0 Then

                    vResp = MessageBox.Show("Deseja imprimir o relatório?", "Confirmação de Impressão", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)

                    If vResp = DialogResult.Yes Then

                        dt = New DataTable()
                        dt.Columns.Add("NOME_EMPRESA", GetType(String))
                        dt.Columns.Add("ENDERECO_EMPRESA", GetType(String))
                        dt.Columns.Add("TELEFONE_EMPRESA", GetType(String))
                        dt.Columns.Add("SITE_EMPRESA", GetType(String))
                        dt.Columns.Add("TITULO_RELATORIO", GetType(String))
                        dt.Columns.Add("NOME_ENDERECO", GetType(String))
                        dt.Columns.Add("SEQUENCIA_IDENTIFICACAO", GetType(String))
                        dt.Columns.Add("LEITURA_ATUAL", GetType(String))
                        dt.Columns.Add("LEITURA_ANTERIOR", GetType(String))
                        dt.Columns.Add("MEDIDIOR", GetType(String))
                        dt.Columns.Add("KVARH", GetType(String))
                        dt.Columns.Add("CLASSE_FASE", GetType(String))
                        dt.Columns.Add("CONST", GetType(String))
                        dt.Columns.Add("ULT_3_CONS", GetType(String))
                        dt.Columns.Add("ROTA", GetType(String))

                    Else
                        Exit Sub
                    End If

                    BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, vlist.Count)

                    For Each objUCFaturamentoFor As entidadeUCFaturamento In vlist


                        llngTotCons = 0
                        llngTotCDes = 0

                        lstrRotaAux = Microsoft.VisualBasic.Left(IIf(IsDBNull(objUCFaturamentoFor.ROTA), "0000", objUCFaturamentoFor.ROTA), 4)

                        lstrRota = Trim(IIf(IsDBNull(objUCFaturamentoFor.ROTA), "00000000", objUCFaturamentoFor.ROTA))

                        lstrSequencia = Trim(IIf(IsDBNull(objUCFaturamentoFor.SEQUENCIA), "00000000", objUCFaturamentoFor.SEQUENCIA))

                        llngUnidId = IIf(IsDBNull(objUCFaturamentoFor.UNID_ID), 0, objUCFaturamentoFor.UNID_ID)

                        lstrZona = IIf(IsDBNull(objUCFaturamentoFor.ZONA), "000", objUCFaturamentoFor.ZONA)

                        lstrLivro = IIf(IsDBNull(objUCFaturamentoFor.LIVRO), "00", objUCFaturamentoFor.LIVRO)

                        lstrConta = IIf(IsDBNull(objUCFaturamentoFor.CONTA), "000000", objUCFaturamentoFor.CONTA)

                        lstrNome = Microsoft.VisualBasic.Left(Trim(IIf(IsDBNull(objUCFaturamentoFor.NOME), "", objUCFaturamentoFor.NOME)), 44)

                        lstrEndereco = IIf(IsDBNull(objUCFaturamentoFor.END), "", objUCFaturamentoFor.END)

                        lstrNumero = IIf(IsDBNull(objUCFaturamentoFor.NUMERO), "", objUCFaturamentoFor.NUMERO)

                        lstrBairro = IIf(IsDBNull(objUCFaturamentoFor.BAIRRO), "", objUCFaturamentoFor.BAIRRO)

                        lstrQuadra = IIf(IsDBNull(objUCFaturamentoFor.QUADRA), "", objUCFaturamentoFor.QUADRA)

                        lstrLote = IIf(IsDBNull(objUCFaturamentoFor.LOTE), "", objUCFaturamentoFor.LOTE)

                        lstrComplemento = IIf(IsDBNull(objUCFaturamentoFor.COMPLEMENT), "", objUCFaturamentoFor.COMPLEMENT)

                        lstrSituacao = UCase(IIf(IsDBNull(objUCFaturamentoFor.SITUACAO), "L", objUCFaturamentoFor.SITUACAO))

                        lstrSituacao = Microsoft.VisualBasic.Switch(lstrSituacao = "D", "DESLIGADO", lstrSituacao = "C", "CORTADO", lstrSituacao = "I", "DESATIVADO", lstrSituacao = "N", "NÃO LIGADO", lstrSituacao = "L", "LIGADO")

                        lstrMedidor = IIf(IsDBNull(objUCFaturamentoFor.NUM), "L", objUCFaturamentoFor.NUM)

                        lstrUltimaLeit = IIf(IsDBNull(objUCFaturamentoFor.ULTLEIT), "0", objUCFaturamentoFor.ULTLEIT)

                        lstrTarBrancaULei_P = IIf(IsDBNull(objUCFaturamentoFor.ULT_LEIT_KWH_P), "0", objUCFaturamentoFor.ULT_LEIT_KWH_P)

                        lstrTarBrancaULei_I = IIf(IsDBNull(objUCFaturamentoFor.ULT_LEIT_KWH_I), "0", objUCFaturamentoFor.ULT_LEIT_KWH_I)

                        lstrTarBrancaULei_FP = IIf(IsDBNull(objUCFaturamentoFor.ULT_LEIT_KWH_FP), "0", objUCFaturamentoFor.ULT_LEIT_KWH_FP)

                        lstrIrriganteULei_N = IIf(IsDBNull(objUCFaturamentoFor.ULT_LEIT_KWH_FP), "0", objUCFaturamentoFor.ULT_LEIT_KWH_FP)

                        lstrIrriganteULei_R = IIf(IsDBNull(objUCFaturamentoFor.ULT_LEIT_IRRIGANTE_FP), "0", objUCFaturamentoFor.ULT_LEIT_IRRIGANTE_FP)

                        lstrMicroGerULei_N = IIf(IsDBNull(objUCFaturamentoFor.ULTLEIT), "0", objUCFaturamentoFor.ULTLEIT)

                        lstrMicroGerULei_R = IIf(IsDBNull(objUCFaturamentoFor.ULTLEIT_REVERSO_FP), "0", objUCFaturamentoFor.ULTLEIT_REVERSO_FP)

                        lstrClasse = IIf(IsDBNull(objUCFaturamentoFor.CLAS), "", objUCFaturamentoFor.CLAS)

                        lstrFase = IIf(IsDBNull(objUCFaturamentoFor.FASE), "M", objUCFaturamentoFor.FASE)

                        lstrRegiao = IIf(IsDBNull(objUCFaturamentoFor.REGIAO), "U", objUCFaturamentoFor.REGIAO)

                        ldblConstante = IIf(IsDBNull(objUCFaturamentoFor.CONST), 0, objUCFaturamentoFor.CONST)

                        lintMicroGeracao = IIf(IsDBNull(objUCFaturamentoFor.MICRO_GERACAO), 0, objUCFaturamentoFor.MICRO_GERACAO)

                        lintIrrigacaoAquicultura = IIf(IsDBNull(objUCFaturamentoFor.IRRIGACAO_AQUICULTURA), 0, objUCFaturamentoFor.IRRIGACAO_AQUICULTURA)

                        lintTarifaBranca = IIf(IsDBNull(objUCFaturamentoFor.TARIFA_BRANCA), 0, objUCFaturamentoFor.TARIFA_BRANCA)

                        objLeitmensal.ZONA = lstrZona
                        objLeitmensal.LIVRO = lstrLivro
                        objLeitmensal.CONTA = lstrConta

                        vlist_1 = repositorioLeitmensal.Select_2(objLeitmensal)

                        If vlist_1.Count > 0 Then

                            For Each objLeitmensalFor As entidadeLeitmensal In vlist_1

                                lstrLeitAtual = IIf(IsDBNull(objLeitmensalFor.LEIT_ATUAL), "", objLeitmensalFor.LEIT_ATUAL)

                                lstrLeitAjust = IIf(IsDBNull(objLeitmensalFor.LEIT_AJUST), "", objLeitmensalFor.LEIT_AJUST)

                            Next

                        Else
                            lstrLeitAtual = ""
                            lstrLeitAjust = ""
                        End If


                        lstrTemp = String.Empty

                        lstrAux = Trim(lstrEndereco)

                        If Len(Trim(lstrNumero)) > 0 Then
                            lstrAux = lstrAux & " N:" & Trim(lstrNumero)
                        End If
                        If Len(Trim(lstrQuadra)) > 0 Then
                            lstrAux = lstrAux & " QD:" & Trim(lstrQuadra)
                        End If
                        If Len(Trim(lstrLote)) > 0 Then
                            lstrAux = lstrAux & " LT:" & Trim(lstrLote)
                        End If
                        If Len(Trim(lstrComplemento)) > 0 Then
                            lstrAux = lstrAux & " - " & Trim(lstrComplemento)
                        End If

                        lstrAux1 = Mid(lstrAux, 1, 45)
                        lstrAux2 = Mid(lstrAux, 46)

                        If Len(Trim(lstrBairro)) > 0 Then
                            If Len(Trim(lstrAux2)) > 0 Then
                                lstrAux2 = lstrAux2 & " - "
                            End If
                            lstrAux2 = lstrAux2 & Trim(lstrBairro)
                        End If


                        lstrTemp = lstrNome & Chr(13)

                        lstrTemp = lstrTemp & lstrAux1 & Chr(13) & IIf(Len(lstrAux2) > 0, lstrAux2, " ")

                        If lintMicroGeracao = 1 Or lintIrrigacaoAquicultura = 1 Or lintTarifaBranca = 1 Then
                            lstrTemp = lstrTemp & Chr(13) & IIf(lintMicroGeracao = 1, " (Micro Geração)", IIf(lintIrrigacaoAquicultura = 1, " (Irrig/Aquic.)", IIf(lintTarifaBranca = 1, "(Tarifa Branca)", "")))
                        End If

                        nome_endereco = lstrNome & Chr(13) & lstrAux1 & Chr(13) & IIf(Len(lstrAux2) > 0, lstrAux2, " ")

                        'lstrTemp = lstrTemp & "|" & "(" & Microsoft.VisualBasic.Right(VB6.Format(lstrRota, "00000000"), 4) & ")" & Chr(13)
                        lstrTemp = lstrTemp & "|" & "(" & VB6.Format(lstrSequencia, "00000000") & ")" & Chr(13)
                        lstrTemp = lstrTemp & VB6.Format(lstrZona, "000") & "." & VB6.Format(lstrLivro, "00") & "." & VB6.Format(lstrConta, "000000")

                        If lintMicroGeracao = 1 Then
                            lstrTemp = lstrTemp & "|" & Chr(13) & "003:___________" & Chr(13) & Chr(13)
                            lstrTemp = lstrTemp & "" & "103:___________"
                        ElseIf lintIrrigacaoAquicultura = 1 Then
                            lstrTemp = lstrTemp & "|" & Chr(13) & "003:___________" & Chr(13) & Chr(13)
                            lstrTemp = lstrTemp & "" & "004:__________" & Chr(13) & Chr(13)
                            lstrTemp = lstrTemp & "" & "006:__________" & Chr(13) & Chr(13)
                            lstrTemp = lstrTemp & "" & "008:__________"
                        ElseIf lintTarifaBranca = 1 Then
                            lstrTemp = lstrTemp & "|" & Chr(13) & "004:___________" & Chr(13) & Chr(13)
                            lstrTemp = lstrTemp & "" & "006:___________" & Chr(13) & Chr(13)
                            lstrTemp = lstrTemp & "" & "008:___________"
                        Else
                            If Microsoft.VisualBasic.Left(lstrSituacao, 1) = "C" Then
                                If iblnMostrarCortado Then
                                    lstrTemp = lstrTemp & "|" & Chr(13) & lstrSituacao
                                Else
                                    lstrTemp = lstrTemp & "|" & Chr(13) & lstrLeitAtual
                                End If
                            ElseIf Microsoft.VisualBasic.Left(lstrSituacao, 1) = "D" Then
                                lstrTemp = lstrTemp & "|" & Chr(13) & lstrSituacao
                            Else
                                lstrTemp = lstrTemp & "|" & Chr(13) & lstrLeitAtual
                            End If
                        End If

                        If Me.chkLeituraAnt.CheckState = System.Windows.Forms.CheckState.Checked Then
                            If lintMicroGeracao = 1 Then
                                lstrTemp = lstrTemp & "|" & "Normal" & Chr(13) & lstrMicroGerULei_N & Chr(13)
                                lstrTemp = lstrTemp & "Injetado" & Chr(13) & lstrMicroGerULei_R
                            ElseIf lintTarifaBranca = 1 Then
                                lstrTemp = lstrTemp & "|" & "Ponta" & Chr(13) & lstrTarBrancaULei_P & Chr(13)
                                lstrTemp = lstrTemp & "Interm." & Chr(13) & lstrTarBrancaULei_I & Chr(13)
                                lstrTemp = lstrTemp & "F.Ponta" & Chr(13) & lstrTarBrancaULei_FP
                            ElseIf lintIrrigacaoAquicultura = 1 Then
                                lstrTemp = lstrTemp & "|" & Chr(13) & "Normal" & Chr(13) & lstrIrriganteULei_N & Chr(13)
                                lstrTemp = lstrTemp & "Reservado" & Chr(13) & lstrIrriganteULei_R
                            Else
                                lstrTemp = lstrTemp & "|" & Chr(13) & lstrUltimaLeit
                            End If
                        Else
                            lstrTemp = lstrTemp & "|" & Chr(13) & "000000"
                        End If

                        lstrTemp = lstrTemp & "|" & Chr(13) & lstrMedidor & Chr(13) & IIf(lstrRegiao = "R", "Rural", IIf(lstrRegiao = "U", "Urbana", "-----"))
                        lstrTemp = lstrTemp & "||" & lstrClasse & Chr(13) & " (" & lstrFase & ") "
                        lstrTemp = lstrTemp & "|" & Chr(13) & Replace(VB6.Format(ldblConstante, "#,###0.000"), ",", ".")
                        lstrTemp = lstrTemp & "|" & GetUlt3Consumos(lstrZona, lstrLivro, lstrConta, llngUnidId)


                        If Microsoft.VisualBasic.Left(lstrSituacao, 1) = "L" Then
                            llngTotCons = llngTotCons + 1
                        Else
                            llngTotCDes = llngTotCDes + 1
                        End If

                        Dim resultado_relatorio As String() = lstrTemp.Split("|")

                        'Inicio gravação dados da impressão

                        nome_empresa = GetConfig("empresa.nome")
                        endereco_empresa = GetConfig("empresa.endereco") & " - " & GetConfig("empresa.municipio") & " - " & GetConfig("empresa.uf")
                        telefone_empresa = GetConfig("empresa.fone") & "     -    "
                        site_empresa = GetConfig("empresa.site")
                        titulo_relatorio = "Mês/Ano: " & Retorna_Mes((Me.dtpLeitura.Value.Month)) & "/" & Me.dtpLeitura.Value.Year & " - Rota: " & lstrRotaAux
                        nome_endereco = resultado_relatorio(0)
                        sequencia_identificacao = resultado_relatorio(1)
                        leitura_atual = resultado_relatorio(2)
                        leitura_anterior = resultado_relatorio(3)
                        medidior = resultado_relatorio(4)
                        kvarh = resultado_relatorio(5)
                        classe_fase = resultado_relatorio(6)
                        constt = resultado_relatorio(7)
                        ult_3_cons = resultado_relatorio(8)
                        rota = lstrRotaAux

                        dt.Rows.Add(nome_empresa, endereco_empresa, telefone_empresa, site_empresa, titulo_relatorio, nome_endereco, sequencia_identificacao, leitura_atual, leitura_anterior, medidior, kvarh, classe_fase, constt, ult_3_cons, rota)

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

                    Next


                    If dt.Rows.Count > 0 Then

                        dc = New DataColumn("NUMERO_CONSUMIDORES", GetType(String))
                        dc.DefaultValue = Convert.ToString(llngTotCons)
                        dt.Columns.Add(dc)

                    End If


                    If dt.Rows.Count > 0 Then

                        dc = New DataColumn("NUEMRO_DESLIGADOS", GetType(String))
                        dc.DefaultValue = Convert.ToString(llngTotCDes)
                        dt.Columns.Add(dc)

                    End If

                    If dt.Rows.Count > 0 Then

                        dc = New DataColumn("TOTAL_GERAL", GetType(String))
                        dc.DefaultValue = Convert.ToString(llngTotCons + llngTotCDes)
                        dt.Columns.Add(dc)

                    End If

                    lPrint(dt)

                Else
                    MessageBox.Show("Não foi possível localizar nenhum registro com a rota de leitura selecionada!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If

            Else
                MessageBox.Show("Faltou informar a rota de leitura !", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If

            HabilitaCampo(True)
            BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, 1)

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try


    End Sub

    Private Sub FrmLeituraManual_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try

            Select Case KeyAscii
                Case System.Windows.Forms.Keys.Escape
                    cmdFechar_Click(cmdFechar, New System.EventArgs())
            End Select

            eventArgs.KeyChar = Chr(KeyAscii)

            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

#End Region

#Region "Métodos"


    Private Sub lClose()

        Try

            If Me.mfgLeitura.Rows > 1 Then

                vResp = MessageBox.Show("Tem certeza de que deseja fechar a tela de leituras?", "Informação", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)

                If vResp = DialogResult.Yes Then
                    Me.Close()
                Else
                    With Me.fmeOcorrencias
                        .Visible = False
                        .Enabled = False
                    End With
                    With Me.fmeIrrigante
                        .Visible = False
                        .Enabled = False
                    End With
                    With Me.fmeOcorrencias
                        .Visible = False
                        .Enabled = False
                    End With
                End If
            Else
                Me.Close()
            End If

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub MontarGrid()

        Try

            With mfgLeitura

                .Rows = 1
                .Clear()
                .set_ColWidth(0, 500)
                .set_ColWidth(1, 500)
                .set_ColWidth(2, 450)
                .set_ColWidth(3, 700)
                .set_ColWidth(4, 2330)
                .set_ColWidth(5, 350)
                .set_ColWidth(6, 1100)
                .set_ColWidth(7, 1000)
                .set_ColWidth(8, 1200)
                .set_ColWidth(9, 1200)
                .set_ColWidth(10, 1150)
                .set_ColWidth(11, 1150)
                .set_ColWidth(12, 1000)
                .set_ColWidth(13, 0)
                .set_ColWidth(14, 1150)
                .set_ColWidth(15, 1150)
                .set_ColWidth(16, 0)
                .set_ColWidth(17, 870)
                .set_ColWidth(18, 1150)
                .set_ColWidth(19, 1150)
                .set_ColWidth(20, 1150)
                .set_ColWidth(21, 0)
                .set_ColWidth(22, 0)
                .set_ColWidth(23, 0)
                .set_ColWidth(24, 0)
                .set_ColWidth(25, 0)
                .set_ColWidth(26, 0)
                .set_ColWidth(27, 0)
                .set_ColWidth(28, 0)
                .set_ColWidth(29, 0)
                .set_ColWidth(30, 0)
                .set_ColWidth(31, 0)
                .set_ColWidth(32, 0)
                .set_ColWidth(33, 0)
                .set_ColWidth(34, 0)
                .set_ColWidth(35, 0)
                .set_ColWidth(36, 0)
                .set_ColWidth(37, 0)
                .set_ColWidth(38, 0)
                .set_ColWidth(39, 0)
                .set_ColWidth(40, 0)
                .set_ColWidth(41, 0)
                .set_ColWidth(42, 0)
                .set_ColWidth(43, 0)
                .set_ColWidth(44, 0)
                .set_ColWidth(45, 0)
                .set_ColWidth(46, 0)
                .set_ColWidth(47, 0)
                .set_ColWidth(48, 0)
                .set_ColWidth(49, 0)
                .set_ColWidth(50, 0)
                .set_ColWidth(51, 0)
                .set_ColWidth(52, 0)
                .set_ColWidth(53, 0)
                .set_ColWidth(54, 0)
                .set_ColWidth(55, 0)
                .set_ColWidth(56, 0)
                .set_ColWidth(57, 0)
                .set_ColWidth(58, 0)
                .set_ColWidth(59, 0)
                .set_ColWidth(60, 0)
                .set_ColWidth(61, 0)
                .set_ColWidth(62, 0)
                .set_ColWidth(63, 0)
                .set_ColWidth(64, 0)
                .set_ColWidth(65, 0)
                .set_ColWidth(66, 0)
                .set_ColWidth(67, 0)
                .set_ColWidth(68, 0)
                .set_ColWidth(69, 0)
                .set_ColWidth(70, 0)
                .set_ColWidth(71, 0)
                .set_ColWidth(72, 0)
                .set_ColWidth(73, 0)
                .set_ColWidth(74, 0)
                .set_ColWidth(75, 0)
                .set_ColWidth(76, 0)
                .set_ColWidth(77, 0)
                .set_ColWidth(78, 0)
                .set_ColWidth(79, 0)
                'Branca
                .set_ColWidth(80, 0)
                .set_ColWidth(81, 0)
                .set_ColWidth(82, 0)
                .set_ColWidth(83, 0)
                .set_ColWidth(84, 0)
                .set_ColWidth(85, 0)
                .set_ColWidth(86, 0)
                .set_ColWidth(87, 0)
                .set_ColWidth(88, 0)
                .set_ColWidth(89, 0)
                .set_ColWidth(90, 0)
                .set_ColWidth(91, 0)
                .set_ColWidth(92, 0)
                .set_ColWidth(96, 0)
                .set_ColWidth(97, 0)
                .set_ColWidth(98, 500)


                .set_TextMatrix(0, 0, "Conf.")
                .set_TextMatrix(0, 1, "Zona")
                .set_TextMatrix(0, 2, "Livro")
                .set_TextMatrix(0, 3, "Conta")
                .set_TextMatrix(0, 4, "Nome")
                .set_TextMatrix(0, 5, "Sit.")
                .set_TextMatrix(0, 6, "Medidor")
                .set_TextMatrix(0, 7, "Seq.")
                .set_TextMatrix(0, 8, "Leit. Anterior")
                .set_TextMatrix(0, 9, "Leit. Atual") 'Edita
                .set_TextMatrix(0, 10, "Residuo") 'Edita
                .set_TextMatrix(0, 11, "Consumo")
                .set_TextMatrix(0, 12, "Constante")
                .set_TextMatrix(0, 13, "Med. kVArh")
                .set_TextMatrix(0, 14, "L. Ant. kVArh")
                .set_TextMatrix(0, 15, "L. Atual kVArh") 'Edita
                .set_TextMatrix(0, 16, "Gera Média")
                .set_TextMatrix(0, 17, "Ocorrência")
                .set_TextMatrix(0, 18, "Descrição Ocorrência")
                .set_TextMatrix(0, 19, "Média Mês 1")
                .set_TextMatrix(0, 20, "Média Mês 2")
                .set_TextMatrix(0, 21, "Média Mês 3")
                .set_TextMatrix(0, 22, "Média Mês 4")
                .set_TextMatrix(0, 23, "Média Mês 5")
                .set_TextMatrix(0, 24, "Média Mês 6")
                .set_TextMatrix(0, 25, "Média Mês 7")
                .set_TextMatrix(0, 26, "Média Mês 8")
                .set_TextMatrix(0, 27, "Média Mês 9")
                .set_TextMatrix(0, 28, "Média Mês 10")
                .set_TextMatrix(0, 29, "Média Mês 11")
                .set_TextMatrix(0, 30, "Média Mês 12")                '
                .set_TextMatrix(0, 31, "Ref Mês 1")
                .set_TextMatrix(0, 32, "Ref Mês 2")
                .set_TextMatrix(0, 33, "Ref Mês 3")
                .set_TextMatrix(0, 34, "Ref Mês 4")
                .set_TextMatrix(0, 35, "Ref Mês 5")
                .set_TextMatrix(0, 36, "Ref Mês 6")
                .set_TextMatrix(0, 37, "Ref Mês 7")
                .set_TextMatrix(0, 38, "Ref Mês 8")
                .set_TextMatrix(0, 39, "Ref Mês 9")
                .set_TextMatrix(0, 40, "Ref Mês 10")
                .set_TextMatrix(0, 41, "Ref Mês 11")
                .set_TextMatrix(0, 42, "Ref Mês 12")                '
                .set_TextMatrix(0, 43, "Ocorrencia 1")
                .set_TextMatrix(0, 44, "Ocorrencia 2")
                .set_TextMatrix(0, 45, "Ocorrencia 3")
                .set_TextMatrix(0, 46, "Ocorrencia 4")
                .set_TextMatrix(0, 47, "Ocorrencia 5")
                .set_TextMatrix(0, 48, "Ocorrencia 6")
                .set_TextMatrix(0, 49, "Ocorrencia 7")
                .set_TextMatrix(0, 50, "Ocorrencia 8")
                .set_TextMatrix(0, 51, "Ocorrencia 9")
                .set_TextMatrix(0, 52, "Ocorrencia 10")
                .set_TextMatrix(0, 53, "Ocorrencia 11")
                .set_TextMatrix(0, 54, "Ocorrencia 12")                '
                .set_TextMatrix(0, 55, "Ocorrencia 1")
                .set_TextMatrix(0, 56, "Ocorrencia 2")
                .set_TextMatrix(0, 57, "Ocorrencia 3")
                .set_TextMatrix(0, 58, "Ocorrencia 4")
                .set_TextMatrix(0, 59, "Ocorrencia 5")
                .set_TextMatrix(0, 60, "Ocorrencia 6")
                .set_TextMatrix(0, 61, "Ocorrencia 7")
                .set_TextMatrix(0, 62, "Ocorrencia 8")
                .set_TextMatrix(0, 63, "Ocorrencia 9")
                .set_TextMatrix(0, 64, "Ocorrencia 10")
                .set_TextMatrix(0, 65, "Ocorrencia 11")
                .set_TextMatrix(0, 66, "Ocorrencia 12")                '
                .set_TextMatrix(0, 67, "Qtde Meses")
                .set_TextMatrix(0, 68, "Média")
                .set_TextMatrix(0, 69, "Qtde Médias")
                .set_TextMatrix(0, 70, "Micro Geração")
                .set_TextMatrix(0, 71, "L. Ant. Reverso")
                .set_TextMatrix(0, 72, "L. Atual Reverso")
                .set_TextMatrix(0, 73, "kWh Reverso")
                .set_TextMatrix(0, 74, "kWh Reverso Residuo")
                .set_TextMatrix(0, 75, "Irrigação/Aquicultura")
                .set_TextMatrix(0, 76, "L. Ant. Irrigante")
                .set_TextMatrix(0, 77, "L. Atual Irrigante")
                .set_TextMatrix(0, 78, "kWh Irrigante")
                .set_TextMatrix(0, 79, "kWh Irrigante Residuo")
                .set_TextMatrix(0, 80, "Tarifa Branca")
                .set_TextMatrix(0, 81, "L. Ant. Ponta")
                .set_TextMatrix(0, 82, "L. Atual Ponta")
                .set_TextMatrix(0, 83, "L. Ant. Intermediario")
                .set_TextMatrix(0, 84, "L. Atual Intermediario")
                .set_TextMatrix(0, 85, "L. Ant. Fora Ponta")
                .set_TextMatrix(0, 86, "L. Atual Fora Ponta")
                .set_TextMatrix(0, 87, "kWh Ponta")
                .set_TextMatrix(0, 88, "kWh Residuo Ponta")
                .set_TextMatrix(0, 89, "kWh Intermediario")
                .set_TextMatrix(0, 90, "kWh Residuo Intermediario")
                .set_TextMatrix(0, 91, "kWh Fora Ponta")
                .set_TextMatrix(0, 92, "kWh Residuo Fora Ponta")
                .set_TextMatrix(0, 93, "Constante Ponta")
                .set_TextMatrix(0, 94, "Constante Intermediario")
                .set_TextMatrix(0, 95, "Constante Fora Ponta")
                .set_TextMatrix(0, 96, "Micro Geração Distribuida")
                .set_TextMatrix(0, 97, "Id Unid")
                .set_TextMatrix(0, 98, "Libera Media Critica")
                .set_TextMatrix(0, 99, "FotoEvidencia")


                .set_ColAlignment(0, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(1, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(2, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(3, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(5, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(7, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
                .set_ColAlignment(98, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)


            End With

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub


    Private Sub PreencherGrid()

        Dim lboolVerificado As Boolean
        Dim lboolLiberaMediaCritica As Boolean
        'Uc_Faturamento
        Dim llngUnidIdMG As Integer
        Dim llngUnidId As Integer
        Dim llngConsId As Integer
        Dim lstrZona As String
        Dim lstrLivro As String
        Dim lstrConta As String
        Dim lstrNome As String
        Dim lstrSituacao As String
        Dim lstrMedidor As String
        Dim ldblConstante As Double
        Dim lstrRota As String
        Dim lstrSequencia As String
        Dim ldblUltleit As Double
        Dim ldblUltleitCorte As Double
        Dim ldblLeit As Double
        Dim lstrMedKVarh As String
        Dim ldblUltkvarh As Double
        Dim ldblKVArh As Double
        Dim ldblResCons As Double
        Dim ldteLigacao As Date
        Dim ldteCorte As Date
        Dim ldteDesligado As Date
        Dim llngDiasLig As Integer

        'Micro Geração
        Dim lintMicroGerDis As Short
        Dim lintMicroGer As Short
        Dim ldblReversoUltLeit As Double
        Dim ldblReversoResiduo As Double
        Dim ldblReversoResiduoCons As Double

        'Irrigante
        Dim lintIrrigante As Short
        Dim ldblIrriganteUltLeit As Double
        Dim ldblIrriganteResiduo As Double
        Dim ldblIrriganteResiduoCons As Double

        'Tarifa Branca
        Dim lintTarifaBranca As Short
        Dim ldblUltLeitPonta As Double
        Dim ldblUltLeitInter As Double
        Dim ldblUltLeitForaP As Double
        Dim ldblResPonta As Double
        Dim ldblResInter As Double
        Dim ldblResForaP As Double
        Dim ldblConstPonta As Double
        Dim ldblConstInter As Double
        Dim ldblConstForaP As Double
        Dim llngQtMedia As Integer

        '--- leitmensal
        Dim lstrLeit_kWh_Atual As String
        Dim lstrLeit_kWh_Anter As String
        Dim ldblLeit_kWh_Atual As Double
        Dim ldblLeit_kWh_Anter As Double
        Dim lstrLeit_kVarh_Atual As String
        Dim lstrLeit_kVarh_Anter As String
        Dim llngOcorrencia As Integer
        Dim lstrOcorrencia As String
        Dim lintGeraMedia As Short
        Dim ldblMedia As Double
        Dim ldblResiduo As Double
        Dim ldblConsumo As Double
        Dim ldblConsAux As Double

        'Reverso
        Dim ldblReversoConsumo As Double
        Dim lstrReversoLeitAtual As String
        Dim lstrReversoLeitAnter As String

        'Irrigante
        Dim ldblIrriganteConsumo As Double
        Dim lstrIrriganteLeitAtual As String
        Dim lstrIrriganteLeitAnter As String

        'Tarifa Branca
        Dim ldblkWhPonta As Double
        Dim ldblkWhInter As Double
        Dim ldblkWhForaP As Double

        Dim fotoevidencia As Integer

        Dim lstrLeitAtualPonta As String
        Dim lstrLeitAtualInter As String
        Dim lstrLeitAtualForaP As String
        Dim lstrLeitAnterPonta As String
        Dim lstrLeitAnterInter As String
        Dim lstrLeitAnterForaP As String

        Dim lintQtdMedidor As Integer = 100000
        Dim lintQtdMedidor99 As Integer = 90000

        Dim objUCFaturamento As New entidadeUCFaturamento
        Dim repositorioUCFaturamento As New repositorioUCFaturamento

        Dim objLeitmensal As New entidadeLeitmensal
        Dim repositorioLeitmensal As New repositorioLeitmensal

        Dim vlist

        Dim vlist_1
        Dim i As Integer

        Try

            iblnCarga = True

            objLeitmensal.ROTA = Me.cmbRota.Text
            objLeitmensal.DT_LEIT = Me.dtpLeitura.Value

            If Me.cmbDiaVenc.SelectedIndex > 0 Then
                objLeitmensal.DIAVENC = Me.cmbDiaVenc.Text
            Else
                objLeitmensal.DIAVENC = " "
            End If

            objLeitmensal.ZONA = "999"
            objLeitmensal.SIT = "'L'"

            If Me.chkDesligados.CheckState Then
                objLeitmensal.SIT = objLeitmensal.SIT & ",'D'"
            End If
            If Me.chkCortados.CheckState Then
                objLeitmensal.SIT = objLeitmensal.SIT & ",'C'"
            End If
            If Me.chkInativos.CheckState Then
                objLeitmensal.SIT = objLeitmensal.SIT & ",'I'"
            End If
            If Me.chkLigados.CheckState Then
                objLeitmensal.SIT = objLeitmensal.SIT & ",'L'"
            End If

            objLeitmensal.MICRO_GERACAO = Me.chkMicroGeracao.Checked


            vlist = repositorioLeitmensal.SelectOthersTables_1(objLeitmensal)

            If vlist.Count > 0 Then
                For Each objLeitmensalFor As entidadeLeitmensal In vlist
                    iblnExiste = True
                    Me.dtpLeituraAnt.Value = CDate(IIf(IsDBNull(objLeitmensalFor.DT_LEITANT), "01/" & istrMesAno, IIf(objLeitmensalFor.DT_LEITANT = "1/1/0001", "01/" & istrMesAno, objLeitmensalFor.DT_LEITANT)))
                    Me.dtpLeitura.Value = CDate(IIf(IsDBNull(objLeitmensalFor.DT_LEIT), "01/" & istrMesAno, IIf(objLeitmensalFor.DT_LEIT = "1/1/0001", "01/" & istrMesAno, objLeitmensalFor.DT_LEIT)))
                Next
            End If

            MontarGrid()
            Me.mfgLeitura.Redraw = False

            Me.mfgConsumos.Clear()
            Me.mfgConsumos.Redraw = False

            objUCFaturamento.ROTA = Me.cmbRota.Text

            If Me.cmbDiaVenc.SelectedIndex > 0 Then
                objUCFaturamento.DIAVENC = Me.cmbDiaVenc.Text
            Else
                objUCFaturamento.DIAVENC = " "
            End If

            objUCFaturamento.ZONA = "999"
            objUCFaturamento.SITUACAO = "'L'"

            If Me.chkDesligados.CheckState Then
                objUCFaturamento.SITUACAO = objUCFaturamento.SITUACAO & ",'D'"
            End If
            If Me.chkCortados.CheckState Then
                objUCFaturamento.SITUACAO = objUCFaturamento.SITUACAO & ",'C'"
            End If
            If Me.chkInativos.CheckState Then
                objUCFaturamento.SITUACAO = objUCFaturamento.SITUACAO & ",'I'"
            End If
            If Me.chkLigados.CheckState Then
                objUCFaturamento.SITUACAO = objUCFaturamento.SITUACAO & ",'L'"
            End If
            objUCFaturamento.MICRO_GERACAO = If(chkMicroGeracao.Checked, 1, 0)

            vlist = repositorioUCFaturamento.Select_6(objUCFaturamento)
            'vlist = repositorioUCFaturamento.Select_23(objUCFaturamento)

            If vlist.Count <> 0 Then

                BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, vlist.Count)

                For Each objUCFaturamentoFor As entidadeUCFaturamento In vlist

                    llngUnidId = CInt(IIf(IsDBNull(objUCFaturamentoFor.UNID_ID), 0, objUCFaturamentoFor.ID))

                    llngConsId = CInt(IIf(IsDBNull(objUCFaturamentoFor.CONS_ID), 0, objUCFaturamentoFor.CONS_ID))

                    lstrZona = Trim(IIf(IsDBNull(objUCFaturamentoFor.ZONA), "000", objUCFaturamentoFor.ZONA))

                    lstrLivro = Trim(IIf(IsDBNull(objUCFaturamentoFor.LIVRO), "00", objUCFaturamentoFor.LIVRO))

                    lstrConta = Trim(IIf(IsDBNull(objUCFaturamentoFor.CONTA), "000000", objUCFaturamentoFor.CONTA))

                    'If lstrConta = "000721" Then
                    'lstrConta = lstrConta
                    'End If

                    lstrNome = Trim(IIf(IsDBNull(objUCFaturamentoFor.NOME), "", objUCFaturamentoFor.NOME))

                    lstrSituacao = Trim(IIf(IsDBNull(objUCFaturamentoFor.SITUACAO), "L", objUCFaturamentoFor.SITUACAO))

                    lstrMedidor = Trim(IIf(IsDBNull(objUCFaturamentoFor.NUM), "", objUCFaturamentoFor.NUM))

                    ldblConstante = CDbl(IIf(IsDBNull(objUCFaturamentoFor.CONST), 0, objUCFaturamentoFor.CONST))

                    lstrRota = Trim(IIf(IsDBNull(objUCFaturamentoFor.ROTA), "", objUCFaturamentoFor.ROTA))

                    lstrSequencia = Trim(IIf(IsDBNull(objUCFaturamentoFor.SEQUENCIA), "", objUCFaturamentoFor.SEQUENCIA))

                    ldblUltleit = CDbl(IIf(IsDBNull(objUCFaturamentoFor.ULTLEIT), 0, objUCFaturamentoFor.ULTLEIT))

                    ldblUltleitCorte = CDbl(IIf(IsDBNull(objUCFaturamentoFor.ULT_LEIT_CORTE), 0, objUCFaturamentoFor.ULT_LEIT_CORTE))

                    ldblLeit = CDbl(IIf(IsDBNull(objUCFaturamentoFor.LEIT), 0, objUCFaturamentoFor.LEIT))

                    lstrMedKVarh = Trim(IIf(IsDBNull(objUCFaturamentoFor.MEDKVARH), "", objUCFaturamentoFor.MEDKVARH))

                    ldblUltkvarh = CDbl(IIf(IsDBNull(objUCFaturamentoFor.ULTKVARH), 0, objUCFaturamentoFor.ULTKVARH))

                    ldblKVArh = CDbl(IIf(IsDBNull(objUCFaturamentoFor.KVARH), 0, objUCFaturamentoFor.KVARH))

                    ldblResCons = CDbl(IIf(IsDBNull(objUCFaturamentoFor.RESIDUO_CONSUMO), 0, objUCFaturamentoFor.RESIDUO_CONSUMO))

                    ldteLigacao = CDate(IIf(IsDBNull(objUCFaturamentoFor.DATA_LIGACAO), "1900-01-01", objUCFaturamentoFor.DATA_LIGACAO))

                    ldteCorte = CDate(IIf(IsDBNull(objUCFaturamentoFor.DATA_CORTE), "1900-01-01", objUCFaturamentoFor.DATA_CORTE))

                    ldteDesligado = CDate(IIf(IsDBNull(objUCFaturamentoFor.DATA_DESLIGAMENTO), "1900-01-01", objUCFaturamentoFor.DATA_DESLIGAMENTO))

                    'Microgeração
                    lintMicroGerDis = 0

                    lintMicroGer = IIf(IsDBNull(objUCFaturamentoFor.MICRO_GERACAO), 0, objUCFaturamentoFor.MICRO_GERACAO)
                    If lintMicroGer = 0 Then
                        llngUnidIdMG = 0
                        llngUnidIdMG = CInt(GetValorTabela("microgeracao_distribuida", "cons_id", "unid_id_d=" & llngUnidId & " AND grupo = 'B'", CStr(0)))
                        If llngUnidIdMG > 0 Then lintMicroGerDis = 1
                    End If

                    ldblReversoUltLeit = CDbl(IIf(IsDBNull(objUCFaturamentoFor.ULTLEIT_REVERSO), 0, objUCFaturamentoFor.ULTLEIT_REVERSO))

                    ldblReversoResiduoCons = CDbl(IIf(IsDBNull(objUCFaturamentoFor.KWH_REVERSO_RES), 0, objUCFaturamentoFor.KWH_REVERSO_RES))

                    'Irrigante

                    lintIrrigante = IIf(IsDBNull(objUCFaturamentoFor.IRRIGACAO_AQUICULTURA), 0, objUCFaturamentoFor.IRRIGACAO_AQUICULTURA)

                    ldblIrriganteUltLeit = CDbl(IIf(IsDBNull(objUCFaturamentoFor.ULTLEIT_IRRIGANTE), 0, objUCFaturamentoFor.ULTLEIT_IRRIGANTE))

                    ldblIrriganteResiduoCons = CDbl(IIf(IsDBNull(objUCFaturamentoFor.KWH_IRRIGANTE_RES), 0, objUCFaturamentoFor.KWH_IRRIGANTE_RES))

                    'Tarifa Branca

                    lintTarifaBranca = IIf(IsDBNull(objUCFaturamentoFor.TARIFA_BRANCA), 0, objUCFaturamentoFor.TARIFA_BRANCA)

                    ldblUltLeitPonta = CDbl(IIf(IsDBNull(objUCFaturamentoFor.ULT_LEIT_KWH_P), 0, objUCFaturamentoFor.ULT_LEIT_KWH_P))

                    ldblUltLeitInter = CDbl(IIf(IsDBNull(objUCFaturamentoFor.ULT_LEIT_KWH_I), 0, objUCFaturamentoFor.ULT_LEIT_KWH_I))

                    ldblUltLeitForaP = CDbl(IIf(IsDBNull(objUCFaturamentoFor.ULT_LEIT_KWH_FP), 0, objUCFaturamentoFor.ULT_LEIT_KWH_FP))

                    ldblResPonta = CDbl(IIf(IsDBNull(objUCFaturamentoFor.KWH_RES_P), 0, objUCFaturamentoFor.KWH_RES_P))

                    ldblResInter = CDbl(IIf(IsDBNull(objUCFaturamentoFor.KWH_RES_I), 0, objUCFaturamentoFor.KWH_RES_I))

                    ldblResForaP = CDbl(IIf(IsDBNull(objUCFaturamentoFor.KWH_RES_FP), 0, objUCFaturamentoFor.KWH_RES_FP))

                    ldblConstPonta = CDbl(IIf(IsDBNull(objUCFaturamentoFor.CONST_KWH_P), 0, objUCFaturamentoFor.CONST_KWH_P))

                    ldblConstInter = CDbl(IIf(IsDBNull(objUCFaturamentoFor.CONST_KWH_I), 0, objUCFaturamentoFor.CONST_KWH_I))

                    ldblConstForaP = CDbl(IIf(IsDBNull(objUCFaturamentoFor.CONST_KWH_FP), 0, objUCFaturamentoFor.CONST_KWH_FP))

                    llngDiasLig = DateDiff(Microsoft.VisualBasic.DateInterval.Day, ldteLigacao, Me.dtpLeitura.Value)

                    objLeitmensal.ZONA = lstrZona
                    objLeitmensal.LIVRO = lstrLivro
                    objLeitmensal.CONTA = lstrConta
                    objLeitmensal.UNID_ID = llngUnidId
                    objLeitmensal.DT_LEIT = Me.dtpLeitura.Value

                    vlist_1 = repositorioLeitmensal.Select_1(objLeitmensal)

                    If vlist_1.Count > 0 Then

                        For Each objLeitmensalFor As entidadeLeitmensal In vlist_1

                            fotoevidencia = objLeitmensalFor.FOTO_EVIDENCIA

                            lboolVerificado = IIf(IsDBNull(objLeitmensalFor.VERIFICADO), 0, objLeitmensalFor.VERIFICADO)
                            lboolLiberaMediaCritica = IIf(IsDBNull(objLeitmensalFor.MEDIA_CRITICA), 0, objLeitmensalFor.MEDIA_CRITICA)

                            lstrLeit_kWh_Anter = Trim(IIf(IsDBNull(objLeitmensalFor.LEIT_ANT), "", objLeitmensalFor.LEIT_ANT))

                            lstrLeit_kWh_Atual = Trim(IIf(IsDBNull(objLeitmensalFor.LEIT_ATUAL), "", objLeitmensalFor.LEIT_ATUAL))

                            lstrLeit_kVarh_Anter = Trim(IIf(IsDBNull(objLeitmensalFor.KVARH), "0", objLeitmensalFor.KVARH))

                            lstrLeit_kVarh_Atual = Trim(IIf(IsDBNull(objLeitmensalFor.KVARHATUAL), "0", objLeitmensalFor.KVARHATUAL))

                            llngOcorrencia = CInt(IIf(IsDBNull(objLeitmensalFor.OCORRENC), 0, objLeitmensalFor.OCORRENC))

                            lintGeraMedia = CDbl(IIf(IsDBNull(objLeitmensalFor.MEDIA), 0, objLeitmensalFor.MEDIA))

                            ldblResiduo = CDbl(IIf(IsDBNull(objLeitmensalFor.RESIDUO), 0, objLeitmensalFor.RESIDUO))

                            ldblConsumo = CDbl(IIf(IsDBNull(objLeitmensalFor.CONSUMO), 0, objLeitmensalFor.CONSUMO))

                            lstrOcorrencia = ""
                            If llngOcorrencia > 0 Then lstrOcorrencia = BuscaOcorrencia(llngOcorrencia)

                            lstrReversoLeitAnter = Trim(IIf(IsDBNull(objLeitmensalFor.LEIT_REVERSO_ANT), "", objLeitmensalFor.LEIT_REVERSO_ANT))

                            lstrReversoLeitAtual = Trim(IIf(IsDBNull(objLeitmensalFor.LEIT_REVERSO_ATUAL), "", objLeitmensalFor.LEIT_REVERSO_ATUAL))

                            ldblReversoConsumo = CDbl(IIf(IsDBNull(objLeitmensalFor.KWH_REVERSO), 0, objLeitmensalFor.KWH_REVERSO))

                            ldblReversoResiduo = CDbl(IIf(IsDBNull(objLeitmensalFor.KWH_REVERSO_RES), 0, objLeitmensalFor.KWH_REVERSO_RES))

                            lstrIrriganteLeitAnter = Trim(IIf(IsDBNull(objLeitmensalFor.LEIT_IRRIGANTE_ANT), "", objLeitmensalFor.LEIT_IRRIGANTE_ANT))

                            lstrIrriganteLeitAtual = Trim(IIf(IsDBNull(objLeitmensalFor.LEIT_IRRIGANTE_ATUAL), "", objLeitmensalFor.LEIT_IRRIGANTE_ATUAL))

                            ldblIrriganteConsumo = CDbl(IIf(IsDBNull(objLeitmensalFor.KWH_IRRIGANTE), 0, objLeitmensalFor.KWH_IRRIGANTE))

                            ldblIrriganteResiduo = CDbl(IIf(IsDBNull(objLeitmensalFor.KWH_IRRIGANTE_RES), 0, objLeitmensalFor.KWH_IRRIGANTE_RES))

                            lstrLeitAnterPonta = Trim(IIf(IsDBNull(objLeitmensalFor.LEITURA_ANT_PONTA), "", objLeitmensalFor.LEITURA_ANT_PONTA))

                            lstrLeitAtualPonta = Trim(IIf(IsDBNull(objLeitmensalFor.LEITURA_ATUAL_PONTA), "", objLeitmensalFor.LEITURA_ATUAL_PONTA))

                            lstrLeitAnterInter = Trim(IIf(IsDBNull(objLeitmensalFor.LEITURA_ANT_INTERMEDIARIO), "", objLeitmensalFor.LEITURA_ANT_INTERMEDIARIO))

                            lstrLeitAtualInter = Trim(IIf(IsDBNull(objLeitmensalFor.LEITURA_ATUAL_INTERMEDIARIO), "", objLeitmensalFor.LEITURA_ATUAL_INTERMEDIARIO))

                            lstrLeitAnterForaP = Trim(IIf(IsDBNull(objLeitmensalFor.LEITURA_ANT_FORA_PONTA), "", objLeitmensalFor.LEITURA_ANT_FORA_PONTA))

                            lstrLeitAtualForaP = Trim(IIf(IsDBNull(objLeitmensalFor.LEITURA_ATUAL_FORA_PONTA), "", objLeitmensalFor.LEITURA_ATUAL_FORA_PONTA))

                            ldblkWhPonta = CDbl(IIf(IsDBNull(objLeitmensalFor.KWH_PONTA), 0, objLeitmensalFor.KWH_PONTA))

                            ldblkWhInter = CDbl(IIf(IsDBNull(objLeitmensalFor.KWH_INTERMEDIARIO), 0, objLeitmensalFor.KWH_INTERMEDIARIO))

                            ldblkWhForaP = CDbl(IIf(IsDBNull(objLeitmensalFor.KWH_FORA_PONTA), 0, objLeitmensalFor.KWH_FORA_PONTA))

                            ldblResPonta = CDbl(IIf(IsDBNull(objLeitmensalFor.RESIDUO_PONTA), 0, objLeitmensalFor.RESIDUO_PONTA))

                            ldblResInter = CDbl(IIf(IsDBNull(objLeitmensalFor.RESIDUO_INTERMEDIARIO), 0, objLeitmensalFor.RESIDUO_INTERMEDIARIO))
                            If ldblResForaP = 0 Then ldblResForaP = CDbl(IIf(IsDBNull(objLeitmensalFor.RESIDUO_FORA_PONTA), 0, objLeitmensalFor.RESIDUO_FORA_PONTA))

                            'If ldblResiduo >= 0 Then
                            'ldblResiduo = IIf(ldblResCons > 0, ldblResCons, ldblResForaP)
                            'If ldblResCons > 0 Then ldblResiduo = ldblResCons - lstrLeit_kWh_Anter
                            'End If
                        Next

                    Else

                        If DateDiff(DateInterval.Day, ldteLigacao, objLeitmensal.DT_LEIT) < 15 Then
                            Continue For
                        End If

                        lstrLeit_kWh_Anter = CStr(ldblUltleit)
                        lstrLeit_kWh_Atual = CStr(ldblLeit)
                        'If lstrLeit_kWh_Atual = "0" Then lstrLeit_kWh_Atual = ""
                        lstrLeit_kVarh_Anter = CStr(ldblKVArh)
                        lstrLeit_kVarh_Atual = CStr(ldblUltkvarh)
                        llngOcorrencia = 0
                        lintGeraMedia = 0
                        ldblResiduo = IIf(ldblResCons > 0, ldblResCons, ldblResForaP)
                        lstrOcorrencia = ""
                        lstrReversoLeitAnter = CStr(ldblReversoUltLeit)
                        lstrReversoLeitAtual = lstrReversoLeitAnter
                        ldblReversoConsumo = 0
                        ldblReversoResiduo = ldblReversoResiduoCons
                        lstrIrriganteLeitAnter = CStr(ldblIrriganteUltLeit)
                        lstrIrriganteLeitAtual = lstrIrriganteLeitAnter
                        ldblIrriganteConsumo = 0
                        ldblIrriganteResiduo = ldblIrriganteResiduoCons
                        lstrLeitAnterPonta = CStr(ldblUltLeitPonta)
                        lstrLeitAtualPonta = ""
                        lstrLeitAnterInter = CStr(ldblUltLeitInter)
                        lstrLeitAtualInter = ""
                        lstrLeitAnterForaP = CStr(ldblUltLeitForaP)
                        lstrLeitAtualForaP = ""
                        ldblkWhPonta = 0
                        ldblkWhInter = 0
                        ldblkWhForaP = 0

                    End If

                    If lstrSituacao = "C" And ldteCorte < dtpLeituraAnt.Text Then
                        GoTo pula_C
                    End If

                    If lstrSituacao = "D" And ldteDesligado < dtpLeituraAnt.Text Then
                        GoTo pula_D
                    End If

                    ldblMedia = CalcMediaMensal(lstrZona, lstrLivro, lstrConta, 12, llngConsId, llngUnidId, llngQtMedia, True, Me.mfgConsumos)

                    If lstrLeit_kWh_Atual <> "" Then ldblLeit_kWh_Atual = lstrLeit_kWh_Atual
                    If lstrLeit_kWh_Anter <> "" Then ldblLeit_kWh_Anter = lstrLeit_kWh_Anter

                    If ldblLeit_kWh_Atual >= ldblLeit_kWh_Anter Then
                        If Trim(lstrLeit_kWh_Anter) <> "" And Trim(lstrLeit_kWh_Atual) <> "" Then
                            ldblConsAux = CalcConsumoLido_Caderno(CDbl(IIf(IsNumeric(lstrLeit_kWh_Anter), lstrLeit_kWh_Anter, 0)), CDbl(IIf(IsNumeric(lstrLeit_kWh_Atual), lstrLeit_kWh_Atual, 0)), ldblConstante, lintGeraMedia, ldblResiduo, ldblMedia)
                            If ldblConsAux <> ldblConsumo Then ldblConsumo = ldblConsAux
                        End If
                    Else
                        If (ldblLeit_kWh_Atual + ldblLeit_kWh_Anter) > lintQtdMedidor Then
                            ldblConsAux = CalcConsumoLido_Caderno(CDbl(IIf(IsNumeric(lstrLeit_kWh_Anter), lstrLeit_kWh_Anter, 0)), CDbl(IIf(IsNumeric(lstrLeit_kWh_Atual), lstrLeit_kWh_Atual, 0)), ldblConstante, lintGeraMedia, ldblResiduo, ldblMedia)
                            If ldblConsAux <> ldblConsumo Then ldblConsumo = ldblConsAux
                        ElseIf (lintQtdMedidor - ldblLeit_kWh_Anter) > ldblLeit_kWh_Atual And ldblLeit_kWh_Anter > lintQtdMedidor99 Then
                            If Trim(lstrLeit_kWh_Anter) <> "" And Trim(lstrLeit_kWh_Atual) <> "" Then
                                ldblConsAux = CalcConsumoLido_Caderno(CDbl(IIf(IsNumeric(lstrLeit_kWh_Anter), lstrLeit_kWh_Anter, 0)), CDbl(IIf(IsNumeric(lstrLeit_kWh_Atual), lstrLeit_kWh_Atual, 0)), ldblConstante, lintGeraMedia, ldblResiduo, ldblMedia)
                                If ldblConsAux <> ldblConsumo Then ldblConsumo = ldblConsAux
                            End If
                        Else
                            ldblConsumo = ldblLeit_kWh_Atual + ldblResiduo
                        End If
                    End If

                    Dim bAdiciona As Boolean

                    bAcimaPercentual = False
                    bAbaixoPercentual = False
                    bAdiciona = False

                    If lstrLeit_kWh_Anter > "0" And lstrLeit_kWh_Atual > "0" Then
                        iblnCarga = False
                        TrataMediaFiltro(ldblMedia, ldblConsumo)
                        iblnCarga = True
                    End If

                    If ((bAcimaPercentual And chkpercentual.Checked = True)) Then
                        bAdiciona = True
                    End If

                    If ((bAbaixoPercentual And chkpercentualbaixo.Checked = True)) Then
                        bAdiciona = True
                    End If

                    If chkpercentual.Checked = False And chkpercentualbaixo.Checked = False Then
                        bAdiciona = True
                    End If

                    'If (bAcimaPercentual And chkpercentual.Checked = True) Or chkpercentual.Checked = False Then

                    If Me.chkAntMaiorAtual.Checked Then
                        If lstrLeit_kWh_Anter = "" Then
                            lstrLeit_kWh_Anter = 0
                        End If
                        If lstrLeit_kWh_Atual = "" Then
                            lstrLeit_kWh_Atual = 0
                        End If
                        If CDbl(IIf(IsNumeric(lstrLeit_kWh_Anter), lstrLeit_kWh_Anter, 0)) > CDbl(IIf(IsNumeric(lstrLeit_kWh_Atual), lstrLeit_kWh_Atual, 0)) Then
                            bAdiciona = True
                        Else
                            bAdiciona = False
                        End If
                    End If

                    If Me.chkLeituraZerada.Checked Then
                        If lstrLeit_kWh_Anter = "" Then
                            lstrLeit_kWh_Anter = 0
                        End If
                        If lstrLeit_kWh_Atual = "" Then
                            lstrLeit_kWh_Atual = 0
                        End If
                        If (CDbl(IIf(IsNumeric(lstrLeit_kWh_Anter), lstrLeit_kWh_Anter, 0)) = 0) Or (CDbl(IIf(IsNumeric(lstrLeit_kWh_Atual), lstrLeit_kWh_Atual, 0)) = 0) Then
                            bAdiciona = True
                        Else
                            bAdiciona = False
                        End If
                    End If

                    If bAdiciona Then
                        With Me.mfgLeitura
                            .Rows = .Rows + 1

                            .FillStyle = MSFlexGridLib.FillStyleSettings.flexFillRepeat
                            If lstrSituacao = "D" Then
                                .Row = .Rows - 1
                                .Col = 0
                                .ColSel = .Cols - 1
                                .CellForeColor = System.Drawing.ColorTranslator.FromOle(&HFF) 'Vermelho
                            ElseIf lstrSituacao = "C" Then
                                .Row = .Rows - 1
                                .Col = 0
                                .ColSel = .Cols - 1
                                .CellForeColor = System.Drawing.ColorTranslator.FromOle(&H80FF) 'Laranja
                            ElseIf lstrSituacao = "I" Then
                                .Row = .Rows - 1
                                .Col = 0
                                .ColSel = .Cols - 1
                                .CellForeColor = System.Drawing.ColorTranslator.FromOle(&H808080) 'Cinza
                            ElseIf lstrSituacao = "N" Then
                                .Row = .Rows - 1
                                .Col = 0
                                .ColSel = .Cols - 1
                                .CellForeColor = System.Drawing.ColorTranslator.FromOle(&HFF00) 'Verde
                            Else
                                If llngDiasLig < 15 Then
                                    .Row = .Rows - 1
                                    .Col = 0
                                    .ColSel = .Cols - 1
                                    .CellForeColor = System.Drawing.ColorTranslator.FromOle(&HFF0000) 'Azul
                                Else
                                    .Row = .Rows - 1
                                    .Col = 0
                                    .ColSel = .Cols - 1
                                    .CellForeColor = System.Drawing.ColorTranslator.FromOle(&H80000008) 'Normal
                                End If
                            End If

                            .Row = .Rows - 1
                            .Col = 0
                            .CellFontName = "Wingdings"
                            .CellFontSize = 14
                            .Text = IIf(lboolVerificado = True, strChecked, strUnChecked)
                            .CellForeColor = Color.Green

                            .Row = .Rows - 1
                            .Col = 98
                            .CellFontName = "Wingdings"
                            .CellFontSize = 14
                            .CellForeColor = Color.Red
                            .Text = IIf(lboolLiberaMediaCritica = True, strChecked, strUnChecked)

                            .FillStyle = MSFlexGridLib.FillStyleSettings.flexFillSingle
                            '.set_TextMatrix(.Rows - 1, 0, IIf(lboolVerificado = True, strChecked, strUnChecked))
                            .set_TextMatrix(.Rows - 1, 97, llngUnidId)
                            .set_TextMatrix(.Rows - 1, 1, lstrZona)
                            .set_TextMatrix(.Rows - 1, 2, lstrLivro)
                            .set_TextMatrix(.Rows - 1, 3, lstrConta)
                            .set_TextMatrix(.Rows - 1, 4, lstrNome)
                            .set_TextMatrix(.Rows - 1, 5, lstrSituacao)
                            .set_TextMatrix(.Rows - 1, 6, lstrMedidor)
                            '.set_TextMatrix(.Rows - 1, 7, Microsoft.VisualBasic.Right(lstrRota, 4))
                            .set_TextMatrix(.Rows - 1, 7, lstrSequencia)
                            .Col = 7
                            .CellFontBold = True
                            .set_TextMatrix(.Rows - 1, 8, lstrLeit_kWh_Anter)

                            If lstrSituacao = "C" Then
                                If ldblUltleitCorte > lstrLeit_kWh_Atual Then
                                    .set_TextMatrix(.Rows - 1, 9, ldblUltleitCorte)
                                Else
                                    .set_TextMatrix(.Rows - 1, 9, lstrLeit_kWh_Atual)
                                End If
                                '.set_TextMatrix(.Rows - 1, 9, ldblUltleitCorte)
                            ElseIf lstrSituacao = "D" Then
                                If ldblUltleitCorte > lstrLeit_kWh_Atual Then
                                    .set_TextMatrix(.Rows - 1, 9, ldblUltleitCorte)
                                Else
                                    .set_TextMatrix(.Rows - 1, 9, lstrLeit_kWh_Atual)
                                End If
                                '.set_TextMatrix(.Rows - 1, 9, ldblUltleitCorte)
                            Else
                                .set_TextMatrix(.Rows - 1, 9, lstrLeit_kWh_Atual)
                            End If

                            .set_TextMatrix(.Rows - 1, 10, ldblResiduo)
                            .set_TextMatrix(.Rows - 1, 11, ldblConsumo)
                            .set_TextMatrix(.Rows - 1, 12, ldblConstante)
                            .set_TextMatrix(.Rows - 1, 13, lstrMedKVarh)
                            .set_TextMatrix(.Rows - 1, 14, lstrLeit_kVarh_Anter)
                            .set_TextMatrix(.Rows - 1, 15, lstrLeit_kVarh_Atual)
                            .set_TextMatrix(.Rows - 1, 16, lintGeraMedia)
                            .set_TextMatrix(.Rows - 1, 17, llngOcorrencia)
                            .set_TextMatrix(.Rows - 1, 18, lstrOcorrencia)
                            '
                            '--- Consumo
                            .set_TextMatrix(.Rows - 1, 19, Me.mfgConsumos.get_TextMatrix(1, 1))
                            .set_TextMatrix(.Rows - 1, 20, Me.mfgConsumos.get_TextMatrix(1, 2))
                            .set_TextMatrix(.Rows - 1, 21, Me.mfgConsumos.get_TextMatrix(1, 3))
                            .set_TextMatrix(.Rows - 1, 22, Me.mfgConsumos.get_TextMatrix(1, 4))
                            .set_TextMatrix(.Rows - 1, 23, Me.mfgConsumos.get_TextMatrix(1, 5))
                            .set_TextMatrix(.Rows - 1, 24, Me.mfgConsumos.get_TextMatrix(1, 6))
                            .set_TextMatrix(.Rows - 1, 25, Me.mfgConsumos.get_TextMatrix(1, 7))
                            .set_TextMatrix(.Rows - 1, 26, Me.mfgConsumos.get_TextMatrix(1, 8))
                            .set_TextMatrix(.Rows - 1, 27, Me.mfgConsumos.get_TextMatrix(1, 9))
                            .set_TextMatrix(.Rows - 1, 28, Me.mfgConsumos.get_TextMatrix(1, 10))
                            .set_TextMatrix(.Rows - 1, 29, Me.mfgConsumos.get_TextMatrix(1, 11))
                            .set_TextMatrix(.Rows - 1, 30, Me.mfgConsumos.get_TextMatrix(1, 12))

                            '--- Cabeçalho
                            .set_TextMatrix(.Rows - 1, 31, Me.mfgConsumos.get_TextMatrix(0, 1))
                            .set_TextMatrix(.Rows - 1, 32, Me.mfgConsumos.get_TextMatrix(0, 2))
                            .set_TextMatrix(.Rows - 1, 33, Me.mfgConsumos.get_TextMatrix(0, 3))
                            .set_TextMatrix(.Rows - 1, 34, Me.mfgConsumos.get_TextMatrix(0, 4))
                            .set_TextMatrix(.Rows - 1, 35, Me.mfgConsumos.get_TextMatrix(0, 5))
                            .set_TextMatrix(.Rows - 1, 36, Me.mfgConsumos.get_TextMatrix(0, 6))
                            .set_TextMatrix(.Rows - 1, 37, Me.mfgConsumos.get_TextMatrix(0, 7))
                            .set_TextMatrix(.Rows - 1, 38, Me.mfgConsumos.get_TextMatrix(0, 8))
                            .set_TextMatrix(.Rows - 1, 39, Me.mfgConsumos.get_TextMatrix(0, 9))
                            .set_TextMatrix(.Rows - 1, 40, Me.mfgConsumos.get_TextMatrix(0, 10))
                            .set_TextMatrix(.Rows - 1, 41, Me.mfgConsumos.get_TextMatrix(0, 11))
                            .set_TextMatrix(.Rows - 1, 42, Me.mfgConsumos.get_TextMatrix(0, 12))
                            '---- Código de Ocorrencia
                            .set_TextMatrix(.Rows - 1, 43, Me.mfgConsumos.get_TextMatrix(2, 1))
                            .set_TextMatrix(.Rows - 1, 44, Me.mfgConsumos.get_TextMatrix(2, 2))
                            .set_TextMatrix(.Rows - 1, 45, Me.mfgConsumos.get_TextMatrix(2, 3))
                            .set_TextMatrix(.Rows - 1, 46, Me.mfgConsumos.get_TextMatrix(2, 4))
                            .set_TextMatrix(.Rows - 1, 47, Me.mfgConsumos.get_TextMatrix(2, 5))
                            .set_TextMatrix(.Rows - 1, 48, Me.mfgConsumos.get_TextMatrix(2, 6))
                            .set_TextMatrix(.Rows - 1, 49, Me.mfgConsumos.get_TextMatrix(2, 7))
                            .set_TextMatrix(.Rows - 1, 50, Me.mfgConsumos.get_TextMatrix(2, 8))
                            .set_TextMatrix(.Rows - 1, 51, Me.mfgConsumos.get_TextMatrix(2, 9))
                            .set_TextMatrix(.Rows - 1, 52, Me.mfgConsumos.get_TextMatrix(2, 10))
                            .set_TextMatrix(.Rows - 1, 53, Me.mfgConsumos.get_TextMatrix(2, 11))
                            .set_TextMatrix(.Rows - 1, 54, Me.mfgConsumos.get_TextMatrix(2, 12))
                            '--- Descrição da ocorrencia
                            .set_TextMatrix(.Rows - 1, 55, Me.mfgConsumos.get_TextMatrix(3, 1))
                            .set_TextMatrix(.Rows - 1, 56, Me.mfgConsumos.get_TextMatrix(3, 2))
                            .set_TextMatrix(.Rows - 1, 57, Me.mfgConsumos.get_TextMatrix(3, 3))
                            .set_TextMatrix(.Rows - 1, 58, Me.mfgConsumos.get_TextMatrix(3, 4))
                            .set_TextMatrix(.Rows - 1, 59, Me.mfgConsumos.get_TextMatrix(3, 5))
                            .set_TextMatrix(.Rows - 1, 60, Me.mfgConsumos.get_TextMatrix(3, 6))
                            .set_TextMatrix(.Rows - 1, 61, Me.mfgConsumos.get_TextMatrix(3, 7))
                            .set_TextMatrix(.Rows - 1, 62, Me.mfgConsumos.get_TextMatrix(3, 8))
                            .set_TextMatrix(.Rows - 1, 63, Me.mfgConsumos.get_TextMatrix(3, 9))
                            .set_TextMatrix(.Rows - 1, 64, Me.mfgConsumos.get_TextMatrix(3, 10))
                            .set_TextMatrix(.Rows - 1, 65, Me.mfgConsumos.get_TextMatrix(3, 11))
                            .set_TextMatrix(.Rows - 1, 66, Me.mfgConsumos.get_TextMatrix(3, 12))
                            '--- Meses e Média
                            .set_TextMatrix(.Rows - 1, 67, Me.mfgConsumos.get_TextMatrix(1, 13))
                            .set_TextMatrix(.Rows - 1, 68, Me.mfgConsumos.get_TextMatrix(1, 14))
                            .set_TextMatrix(.Rows - 1, 69, Me.mfgConsumos.get_TextMatrix(2, 13))


                            'Micro Geração
                            .set_TextMatrix(.Rows - 1, 70, lintMicroGer)
                            .set_TextMatrix(.Rows - 1, 71, lstrReversoLeitAnter)
                            .set_TextMatrix(.Rows - 1, 72, lstrReversoLeitAtual)
                            .set_TextMatrix(.Rows - 1, 73, ldblReversoConsumo)
                            .set_TextMatrix(.Rows - 1, 74, ldblReversoResiduo)
                            'Irrigante
                            .set_TextMatrix(.Rows - 1, 75, lintIrrigante)
                            .set_TextMatrix(.Rows - 1, 76, lstrIrriganteLeitAnter)
                            .set_TextMatrix(.Rows - 1, 77, lstrIrriganteLeitAtual)
                            .set_TextMatrix(.Rows - 1, 78, ldblIrriganteConsumo)
                            .set_TextMatrix(.Rows - 1, 79, ldblIrriganteResiduo)
                            'Tarifa Branca
                            .set_TextMatrix(.Rows - 1, 80, lintTarifaBranca)
                            .set_TextMatrix(.Rows - 1, 81, lstrLeitAnterPonta)
                            .set_TextMatrix(.Rows - 1, 82, lstrLeitAtualPonta)
                            .set_TextMatrix(.Rows - 1, 83, lstrLeitAnterInter)
                            .set_TextMatrix(.Rows - 1, 84, lstrLeitAtualInter)
                            .set_TextMatrix(.Rows - 1, 85, lstrLeitAnterForaP)
                            .set_TextMatrix(.Rows - 1, 86, lstrLeitAtualForaP)
                            .set_TextMatrix(.Rows - 1, 87, ldblkWhPonta)
                            .set_TextMatrix(.Rows - 1, 88, ldblResPonta)
                            .set_TextMatrix(.Rows - 1, 89, ldblkWhInter)
                            .set_TextMatrix(.Rows - 1, 90, ldblResInter)
                            .set_TextMatrix(.Rows - 1, 91, ldblkWhForaP)
                            .set_TextMatrix(.Rows - 1, 92, ldblResForaP)
                            .set_TextMatrix(.Rows - 1, 93, ldblConstPonta)
                            .set_TextMatrix(.Rows - 1, 94, ldblConstInter)
                            .set_TextMatrix(.Rows - 1, 95, ldblConstForaP)
                            .set_TextMatrix(.Rows - 1, 96, lintMicroGerDis)
                            .set_TextMatrix(.Rows - 1, 97, llngUnidId)
                            .set_TextMatrix(.Rows - 1, 99, fotoevidencia)

                        End With
                    End If
pula_C:
pula_D:

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

                Next

            End If

            With Me.mfgLeitura
                .Redraw = True
                If .Rows > 1 Then
                    FlexCores(&H80000005, &H8000000F)
                    .Row = 1
                    Me.lblInfo.Text = " Consumidores: [ " & .Row & " ] de [ " & .Rows - 1 & " ]."
                    Me.lblInfo.Refresh()
                End If
            End With

            Me.mfgConsumos.Redraw = True

            BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, 1)
            iblnCarga = False

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


    End Sub

    Public Function CalcConsumoLido_Caderno(ByVal adblLeiAnt As Double, ByVal adblLeitAtual As Double, ByVal adblConstante As Double, ByVal aintGeraMedia As Short, ByVal adblResiduo As Double, ByVal adblMedia As Double) As Double

        Dim ldblLeitAtual As Double

        Try

            If aintGeraMedia <> 1 Then
                If adblLeitAtual >= 0 Then
                    'verificar de consumo menor que zero pois nescaso pode haver virada de medidor


                    If (adblLeitAtual - adblLeiAnt) < 0 Then
                        Select Case Len(CStr(Truncate(adblLeiAnt, 0)))
                            Case 3
                                ldblLeitAtual = (999 + adblLeitAtual) + 1
                            Case 4
                                ldblLeitAtual = (9999 + adblLeitAtual) + 1
                            Case 5
                                ldblLeitAtual = (99999 + adblLeitAtual) + 1
                            Case 6
                                ldblLeitAtual = (999999 + adblLeitAtual) + 1
                            Case 7
                                ldblLeitAtual = (9999999 + adblLeitAtual) + 1
                            Case 8
                                ldblLeitAtual = (99999999 + adblLeitAtual) + 1
                            Case 9
                                ldblLeitAtual = (999999999 + adblLeitAtual) + 1
                        End Select
                    Else
                        If adblLeitAtual = 0 Then
                            adblLeitAtual = ldblLeitAtual
                        Else
                            ldblLeitAtual = adblLeitAtual
                        End If
                    End If


                    If adblLeiAnt > ldblLeitAtual Then
                        CalcConsumoLido_Caderno = Arredondar((ldblLeitAtual * adblConstante) + adblResiduo, 0)
                    Else
                        CalcConsumoLido_Caderno = Arredondar((((ldblLeitAtual - adblLeiAnt) * adblConstante) + adblResiduo), 0)
                    End If

                Else
                    CalcConsumoLido_Caderno = 0
                End If
            Else
                'Quando tem Ocorrência que calcula a média, então faz pelo os ultimos 12 meses conforme art 87 res 414
                CalcConsumoLido_Caderno = adblMedia

                'Refazer os calculo de consumo para apurar consumo real
                'If adblResiduo > 0 Then
                '''CalcConsumoLido = Arredondar((ldblLeitAtual + adblResiduo) * adblConstante, 0)
                'CalcConsumoLido_Caderno = Arredondar((adblLeitAtual + adblResiduo) * adblConstante, 0)
                'Else
                'If (adblLeitAtual - adblLeiAnt) < 0 Then
                'CalcConsumoLido_Caderno = Arredondar((adblLeitAtual + adblResiduo) * adblConstante, 0)
                'Else
                'CalcConsumoLido_Caderno = Arredondar(((ldblLeitAtual - adblLeiAnt) + adblResiduo) * adblConstante, 0)
                'End If
                'End If

                'Else
                'CalcConsumoLido_Caderno = 0
                'End If
                'Else
                'Quando tem Ocorrência que calcula a média, então faz pelo os ultimos 12 meses conforme art 87 res 414
                'CalcConsumoLido_Caderno = adblMedia

            End If


        Catch ex As Exception
            Exit Function
        End Try


    End Function

    Private Sub HabilitaCampo(ByVal ablnTipo As Boolean)

        Try

            Me.Cursor = IIf(ablnTipo, System.Windows.Forms.Cursors.Default, System.Windows.Forms.Cursors.WaitCursor)
            Me.cmdBusca.Enabled = ablnTipo
            Me.cmdOcorrencia.Enabled = IIf(Me.mfgLeitura.Rows > 1, ablnTipo, False)
            Me.cmdReverso.Enabled = False
            Me.cmdIrrigante.Enabled = False
            Me.cmdTarifaBranca.Enabled = False
            cmdFotoEvidencia.Enabled = False
            Me.cmdImprimir.Enabled = ablnTipo
            Me.cmdGravar.Enabled = ablnTipo
            Me.cmdFechar.Enabled = ablnTipo
            Me.cmbRota.Enabled = ablnTipo
            Me.cmbDiaVenc.Enabled = ablnTipo
            Me.dtpLeitura.Enabled = ablnTipo
            Me.dtpLeituraAnt.Enabled = ablnTipo
            Me.cmbRotaIni.Enabled = ablnTipo
            Me.cmbRotaFim.Enabled = ablnTipo
            Me.chkCortados.Enabled = ablnTipo
            Me.chkDesligados.Enabled = ablnTipo
            Me.chkInativos.Enabled = ablnTipo
            Me.chkLigados.Enabled = ablnTipo
            Me.mfgLeitura.Enabled = ablnTipo
            Me.chkLeituraAnt.Enabled = ablnTipo
            Me.chkAntMaiorAtual.Enabled = ablnTipo
            Me.chkLeituraZerada.Enabled = ablnTipo
            Me.chkpercentualbaixo.Enabled = ablnTipo
            Me.chkpercentual.Enabled = ablnTipo
            System.Windows.Forms.Application.DoEvents()

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub FlexCores(ByVal alngCorPar As Integer, ByVal alngCorImpar As Integer)

        Dim llngLinha As Integer

        Try

            With Me.mfgLeitura
                .FillStyle = MSFlexGridLib.FillStyleSettings.flexFillRepeat
                .Redraw = False
                For llngLinha = 1 To .Rows - 1
                    .Row = llngLinha
                    If EImpar(llngLinha) Then 'Se a linha for impar:
                        'Seleciona a partir da primeira coluna
                        .Col = 1
                        'Seleciona até a última coluna
                        .ColSel = .Cols - 1
                        'Aplica a cor
                        .CellBackColor = System.Drawing.ColorTranslator.FromOle(alngCorImpar)
                    Else 'Se a linha for par:
                        'Seleciona a partir da primeira coluna
                        .Col = 1
                        'Seleciona até a última coluna
                        .ColSel = .Cols - 1
                        'Aplica a cor
                        .CellBackColor = System.Drawing.ColorTranslator.FromOle(alngCorPar)
                    End If

                    If Val(IIf(IsNumeric(.get_TextMatrix(llngLinha, 17)), .get_TextMatrix(llngLinha, 17), 0)) > 0 Then
                        .FillStyle = MSFlexGridLib.FillStyleSettings.flexFillSingle
                        If CDbl(.get_TextMatrix(llngLinha, 16)) = 1 Then
                            .Row = llngLinha
                            .Col = 11
                            .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HFF)
                        Else
                            .Row = llngLinha
                            .Col = 11
                            .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HFF00)
                        End If
                        .FillStyle = MSFlexGridLib.FillStyleSettings.flexFillRepeat
                    End If
                    If Val(.get_TextMatrix(llngLinha, 70)) = 1 Or Val(.get_TextMatrix(llngLinha, 96)) = 1 Then
                        .Col = 1
                        .ColSel = .Cols - 1
                        .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HFF8080)
                    End If
                Next llngLinha
                .FillStyle = MSFlexGridLib.FillStyleSettings.flexFillSingle
                .Redraw = True
            End With
            System.Windows.Forms.Application.DoEvents()

        Catch ex As Exception
            Exit Sub
        End Try
    End Sub

    Private Sub AtualizaLabels()

        Dim lstrAux As String

        Try

            If iblnCarga = False Then
                PreenncherGridConsumos()
                With Me.mfgLeitura
                    If .Rows > 1 Then
                        Me.lblLeitOcorrencias.Text = ""
                        If Trim(.get_TextMatrix(.Row, 17)) <> "0" Then
                            Me.lblLeitOcorrencias.Text = .get_TextMatrix(.Row, 18) & " " & IIf(CDbl(.get_TextMatrix(.Row, 16)) = 0, "( LIDO )", IIf(CDbl(.get_TextMatrix(.Row, 16)) = 1, "( MÉDIA )", ""))
                        End If
                        lstrAux = TrataMedia()

                        If Trim(Me.lblLeitOcorrencias.Text) <> "" And Len(lstrAux) > 0 Then
                            Me.lblLeitOcorrencias.Text = Me.lblLeitOcorrencias.Text & " - "
                        End If
                        Me.lblLeitOcorrencias.Text = Me.lblLeitOcorrencias.Text & lstrAux
                        Me.lblLeitOcorrencias.Refresh()
                        Me.lblInfo.Text = " Consumidores: [ " & .Row & " ] de [ " & .Rows - 1 & " ]."
                        Me.lblInfo.Refresh()
                    End If
                End With
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Function TrataMedia() As String

        Dim ldblMedia As Double
        Dim ldblMaior As Double
        Dim ldblMenor As Double

        Try

            If iblnCarga = False Then
                With Me.mfgLeitura
                    If .Rows > 1 Then

                        ldblMedia = CDbl(Me.mfgConsumos.get_TextMatrix(1, 14))
                        ldblMaior = ldblMedia + (ldblMedia * idblPercentual)
                        ldblMenor = ldblMedia - (ldblMedia * idblPercentual)

                        If CDbl(.get_TextMatrix(.Row, 11)) > 0 And CDbl(.get_TextMatrix(.Row, 11)) > ldblMaior Then
                            TrataMedia = "ATENÇÃO! O consumo esta ACIMA dos " & (idblPercentual * 100) & "% da média."
                        ElseIf CDbl(.get_TextMatrix(.Row, 11)) > 0 And CDbl(.get_TextMatrix(.Row, 11)) < ldblMenor Then
                            TrataMedia = "ATENÇÃO! O consumo esta ABAIXO dos " & (idblPercentual * 100) & "% da média."
                        Else
                            TrataMedia = ""
                        End If
                    End If
                End With
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function
        End Try

    End Function

    Private Sub CalcIrriganteConsumo()

        Try
            With Me.mfgLeitura
                If Trim(Me.txtIrriganteLAtual.Text) <> "" And Trim(Me.txtIrriganteLAnt.Text) <> "" Then
                    Me.txtIrrigantekWh.Text = VB6.Format(((CDbl(Me.txtIrriganteLAtual.Text) - CDbl(Me.txtIrriganteLAnt.Text))) * CDbl(IIf(IsNumeric(.get_TextMatrix(ilngLinha, 12)), .get_TextMatrix(ilngLinha, 12), 0)), "#,##0.00")
                End If
            End With
        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub CalcBrancaConsumo()

        Try
            With Me.mfgLeitura
                If Trim(Me.txtLeitAtualPonta.Text) <> "" And Trim(Me.txtLeitAnterPonta.Text) <> "" Then
                    Me.txtkWhPonta.Text = VB6.Format(((CDbl(Me.txtLeitAtualPonta.Text) - CDbl(Me.txtLeitAnterPonta.Text) + CDbl(Me.txtResPonta.Text))) * CDbl(IIf(IsNumeric(.get_TextMatrix(ilngLinha, 93)), .get_TextMatrix(ilngLinha, 93), 0)), "#,##0.00")
                End If
                If Trim(Me.txtLeitAtualIntermediario.Text) <> "" And Trim(Me.txtLeitAnterIntermediario.Text) <> "" Then
                    Me.txtkWhIntermediario.Text = VB6.Format(((CDbl(Me.txtLeitAtualIntermediario.Text) - CDbl(Me.txtLeitAnterIntermediario.Text) + CDbl(Me.txtResIntermediario.Text))) * CDbl(IIf(IsNumeric(.get_TextMatrix(ilngLinha, 94)), .get_TextMatrix(ilngLinha, 94), 0)), "#,##0.00")
                End If
                If Trim(Me.txtLeitAtualForaPonta.Text) <> "" And Trim(Me.txtLeitAnterForaPonta.Text) <> "" Then
                    Me.txtkWhForaPonta.Text = VB6.Format(((CDbl(Me.txtLeitAtualForaPonta.Text) - CDbl(Me.txtLeitAnterForaPonta.Text) + CDbl(Me.txtResForaPonta.Text))) * CDbl(IIf(IsNumeric(.get_TextMatrix(ilngLinha, 95)), .get_TextMatrix(ilngLinha, 95), 0)), "#,##0.00")
                End If
            End With
        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub PreenncherGridConsumos()

        Dim lstrCor As String
        Dim lintCol As Short


        Try

            With Me.mfgConsumos
                .Cols = 15
                Call .Clear()
                .Redraw = False
                '--- Consumos
                lstrCor = System.Drawing.ColorTranslator.ToOle(.CellBackColor).ToString
                .set_TextMatrix(1, 0, "kWH")
                .set_TextMatrix(1, 1, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 19))
                .set_TextMatrix(1, 2, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 20))
                .set_TextMatrix(1, 3, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 21))
                .set_TextMatrix(1, 4, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 22))
                .set_TextMatrix(1, 5, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 23))
                .set_TextMatrix(1, 6, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 24))
                .set_TextMatrix(1, 7, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 25))
                .set_TextMatrix(1, 8, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 26))
                .set_TextMatrix(1, 9, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 27))
                .set_TextMatrix(1, 10, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 28))
                .set_TextMatrix(1, 11, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 29))
                .set_TextMatrix(1, 12, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 30))
                '--- Cabeçalho
                .set_TextMatrix(0, 0, "Meses")
                .set_TextMatrix(0, 1, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 31))
                .set_TextMatrix(0, 2, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 32))
                .set_TextMatrix(0, 3, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 33))
                .set_TextMatrix(0, 4, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 34))
                .set_TextMatrix(0, 5, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 35))
                .set_TextMatrix(0, 6, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 36))
                .set_TextMatrix(0, 7, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 37))
                .set_TextMatrix(0, 8, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 38))
                .set_TextMatrix(0, 9, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 39))
                .set_TextMatrix(0, 10, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 40))
                .set_TextMatrix(0, 11, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 41))
                .set_TextMatrix(0, 12, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 42))
                '--- Ocorrência
                .set_TextMatrix(2, 0, "Ocor.")
                .set_TextMatrix(2, 1, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 43))
                .set_TextMatrix(2, 2, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 44))
                .set_TextMatrix(2, 3, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 45))
                .set_TextMatrix(2, 4, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 46))
                .set_TextMatrix(2, 5, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 47))
                .set_TextMatrix(2, 6, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 48))
                .set_TextMatrix(2, 7, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 49))
                .set_TextMatrix(2, 8, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 50))
                .set_TextMatrix(2, 9, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 51))
                .set_TextMatrix(2, 10, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 52))
                .set_TextMatrix(2, 11, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 53))
                .set_TextMatrix(2, 12, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 54))
                '--- Descrição da ocorrencia
                .set_TextMatrix(3, 0, "Desc.")
                .set_TextMatrix(3, 1, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 55))
                .set_TextMatrix(3, 2, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 56))
                .set_TextMatrix(3, 3, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 57))
                .set_TextMatrix(3, 4, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 58))
                .set_TextMatrix(3, 5, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 59))
                .set_TextMatrix(3, 6, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 60))
                .set_TextMatrix(3, 7, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 61))
                .set_TextMatrix(3, 8, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 62))
                .set_TextMatrix(3, 9, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 63))
                .set_TextMatrix(3, 10, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 64))
                .set_TextMatrix(3, 11, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 65))
                .set_TextMatrix(3, 12, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 66))
                '--- Meses e Média
                .set_TextMatrix(1, 13, Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 67))
                .set_TextMatrix(1, 14, FormatNumber(IIf(IsNumeric(Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 68)), Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 68), 0), 2))
                .set_TextMatrix(2, 13, FormatNumber(IIf(IsNumeric(Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 69)), Me.mfgLeitura.get_TextMatrix(Me.mfgLeitura.RowSel, 69), 0), 2))


                .set_TextMatrix(0, 13, "Qtde")
                .set_TextMatrix(0, 14, "Média")

                .set_ColWidth(0, 0)
                .set_ColWidth(1, 0)
                .set_ColWidth(2, 0)
                .set_ColWidth(3, 0)
                .set_ColWidth(4, 0)
                .set_ColWidth(5, 0)
                .set_ColWidth(6, 0)
                .set_ColWidth(7, 0)
                .set_ColWidth(8, 0)
                .set_ColWidth(9, 0)
                .set_ColWidth(10, 0)
                .set_ColWidth(11, 0)
                .set_ColWidth(12, 0)
                .set_ColWidth(13, 0)
                .set_ColWidth(14, 0)

                .set_ColWidth(0, 700)
                .set_ColWidth(1, 700) 'Len(.TextMatrix(1, 1)) * 230
                .set_ColWidth(2, 700) 'Len(.TextMatrix(1, 2)) * 230
                .set_ColWidth(3, 700) 'Len(.TextMatrix(1, 3)) * 230
                .set_ColWidth(4, 700) 'Len(.TextMatrix(1, 4)) * 230
                .set_ColWidth(5, 700) 'Len(.TextMatrix(1, 5)) * 230
                .set_ColWidth(6, 700) 'Len(.TextMatrix(1, 6)) * 230
                .set_ColWidth(7, 700) 'Len(.TextMatrix(1, 7)) * 230
                .set_ColWidth(8, 700) 'Len(.TextMatrix(1, 8)) * 230
                .set_ColWidth(9, 700) 'Len(.TextMatrix(1, 9)) * 230
                .set_ColWidth(10, 700) 'Len(.TextMatrix(1, 10)) * 230
                .set_ColWidth(11, 700) 'Len(.TextMatrix(1, 11)) * 230
                .set_ColWidth(12, 700) 'Len(.TextMatrix(1, 12)) * 230
                .set_ColWidth(13, 550) 'Len(.TextMatrix(1, 12)) * 230
                .set_ColWidth(14, IIf(Len(.get_TextMatrix(1, 14)) <= 6, 700, Len(.get_TextMatrix(1, 14)) * 124))


                For lintCol = 1 To 12
                    If .get_TextMatrix(2, lintCol) = "MÉDIA" Then
                        .Row = 2
                        .Col = lintCol
                        .CellFontBold = True
                        .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HFF)
                    ElseIf .get_TextMatrix(3, lintCol) <> "" Then
                        .Row = 2
                        .Col = lintCol
                        .CellFontBold = True
                        .CellBackColor = System.Drawing.ColorTranslator.FromOle(&HFF00)

                    End If
                Next lintCol
                .Redraw = True
            End With

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub CalcReversoConsumo()

        Try


            With Me.mfgLeitura
                If Trim(Me.txtReversoLAtual.Text) <> "" And Trim(Me.txtReversoLAnt.Text) <> "" Then
                    Me.txtReversokWh.Text = VB6.Format(((CDbl(Me.txtReversoLAtual.Text) - CDbl(Me.txtReversoLAnt.Text))) * CDbl(IIf(IsNumeric(.get_TextMatrix(ilngLinha, 12)), .get_TextMatrix(ilngLinha, 12), 0)), "#,##0.00")
                End If
            End With

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Function BuscaOcorrencia(ByVal alngCodigo As Integer) As String

        Dim lstrRet As String

        Dim vlist

        Dim objOcorrencia As New entidadeOcorrencia
        Dim repositorioOcorrencia As New repositorioOcorrencia

        Try

            If alngCodigo > 0 Then
                alngCodigo = alngCodigo
            End If

            objOcorrencia.COD = alngCodigo

            vlist = repositorioOcorrencia.Select_1(objOcorrencia)

            If vlist.Count <> 0 Then

                For Each objOcorrenciaFor As entidadeOcorrencia In vlist
                    lstrRet = Trim(IIf(IsDBNull(objOcorrenciaFor.DESCRICAO), "Ocorrência não encontrada!", objOcorrenciaFor.DESCRICAO))
                Next

            Else
                lstrRet = "Ocorrência não encontrada!"
            End If

            BuscaOcorrencia = lstrRet

        Catch ex As Exception
            Exit Function
        End Try

    End Function

    Private Sub PreencheCombobox()

        Try

            '--- Vencimentos
            Me.lblInfo.Text = " Aguarde carregando os vencimentos..."
            Me.lblInfo.Refresh()

            PreencherDatasVencimento(cmbDiaVenc)

            '--- Rotas
            Me.lblInfo.Text = " Aguarde carregando as rotas..."
            Me.lblInfo.Refresh()

            PreencherRotasUCFaturamento(cmbRota)
            PreencherRotasUCFaturamento(cmbRotaIni)
            PreencherRotasUCFaturamento(cmbRotaFim)

            '--- Ocorrências
            Me.lblInfo.Text = " Aguarde carregando as ocorrência..."
            Me.lblInfo.Refresh()

            PreencherOcorrencia(cmbOcorrencia)
            PreencherOcorrencia_1(cmbOcorMedia)


        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub GravaParcial()

        Dim lboolVerificado As Boolean
        Dim lstrZona As String
        Dim lstrLivro As String
        Dim lstrConta As String
        Dim llngUnidId As Integer
        Dim lstrLeiAtual As String
        Dim lstrOcorrencia As String
        Dim ldblResiduo As Double
        Dim lintGradeMedia As Short
        'Microgeração
        Dim lintMicroGeracao As Short
        Dim lstrReversoLAnter As String
        Dim lstrReversoLAtual As String
        Dim ldblReversoConsumo As Double
        Dim ldblReversoResiduo As Double
        'Irrigante
        Dim lstrIrriganteLAnter As String
        Dim lstrIrriganteLAtual As String
        Dim ldblIrriganteConsumo As Double
        Dim ldblIrriganteResiduo As Double
        'Tarifa Branca
        Dim lintTarifaBranca As Short
        Dim lstrLeitAtualPonta As String
        Dim lstrLeitAtualInter As String
        Dim lstrLeitAtualForaP As String
        Dim lstrLeitAnterPonta As String
        Dim lstrLeitAnterInter As String
        Dim lstrLeitAnterForaP As String
        Dim ldblkWhPonta As Double
        Dim ldblkWhInter As Double
        Dim ldblkWhForaP As Double
        Dim ldblResPonta As Double
        Dim ldblResInter As Double
        Dim ldblResForaP As Double

        Dim objLeitmensal As New entidadeLeitmensal

        Try

            With Me.mfgLeitura
                llngUnidId = CInt(.get_TextMatrix(ilngRow, 97))
                lboolVerificado = .get_TextMatrix(ilngRow, 0)
                lstrZona = .get_TextMatrix(ilngRow, 1)
                lstrLivro = .get_TextMatrix(ilngRow, 2)
                lstrConta = .get_TextMatrix(ilngRow, 3)
                lstrLeiAtual = .get_TextMatrix(ilngRow, 9)
                ldblResiduo = IIf(Len(Trim(.get_TextMatrix(ilngRow, 10))) = 0, 0, .get_TextMatrix(ilngRow, 10))
                lintGradeMedia = Val(.get_TextMatrix(ilngRow, 16))
                lstrOcorrencia = CStr(Val(.get_TextMatrix(ilngRow, 17)))
                'MIcrogeração
                lintMicroGeracao = CShort(.get_TextMatrix(ilngRow, 70))
                lstrReversoLAnter = .get_TextMatrix(ilngRow, 71)
                lstrReversoLAtual = .get_TextMatrix(ilngRow, 72)
                ldblReversoConsumo = Val(.get_TextMatrix(ilngRow, 73))
                ldblReversoResiduo = Val(.get_TextMatrix(ilngRow, 74))
                'Irrigante
                lstrIrriganteLAnter = .get_TextMatrix(ilngRow, 76)
                lstrIrriganteLAtual = .get_TextMatrix(ilngRow, 77)
                ldblIrriganteConsumo = Val(.get_TextMatrix(ilngRow, 78))
                ldblIrriganteResiduo = Val(.get_TextMatrix(ilngRow, 79))
                'Tarifa Branca
                lintTarifaBranca = CShort(.get_TextMatrix(ilngRow, 80))
                lstrLeitAnterPonta = .get_TextMatrix(ilngRow, 81)
                lstrLeitAtualPonta = .get_TextMatrix(ilngRow, 82)
                lstrLeitAnterInter = .get_TextMatrix(ilngRow, 83)
                lstrLeitAtualInter = .get_TextMatrix(ilngRow, 84)
                lstrLeitAnterForaP = .get_TextMatrix(ilngRow, 85)
                lstrLeitAtualForaP = .get_TextMatrix(ilngRow, 86)
                ldblkWhPonta = CDbl(IIf(IsNumeric(.get_TextMatrix(ilngRow, 87)), .get_TextMatrix(ilngRow, 87), 0))
                ldblkWhInter = CDbl(IIf(IsNumeric(.get_TextMatrix(ilngRow, 89)), .get_TextMatrix(ilngRow, 89), 0))
                ldblkWhForaP = CDbl(IIf(IsNumeric(.get_TextMatrix(ilngRow, 91)), .get_TextMatrix(ilngRow, 91), 0))
                ldblResPonta = CDbl(IIf(IsNumeric(.get_TextMatrix(ilngRow, 88)), .get_TextMatrix(ilngRow, 88), 0))
                ldblResInter = CDbl(IIf(IsNumeric(.get_TextMatrix(ilngRow, 90)), .get_TextMatrix(ilngRow, 90), 0))
                ldblResForaP = CDbl(IIf(IsNumeric(.get_TextMatrix(ilngRow, 92)), .get_TextMatrix(ilngRow, 92), 0))
            End With

            objLeitmensal.LEIT_ATUAL = lstrLeiAtual
            objLeitmensal.OCORRENC = lstrOcorrencia
            objLeitmensal.RESIDUO = ldblResiduo
            'Reverso
            objLeitmensal.LEIT_REVERSO_ANT = lstrReversoLAnter
            objLeitmensal.LEIT_REVERSO_ATUAL = lstrReversoLAtual
            objLeitmensal.KWH_REVERSO = ldblReversoConsumo
            objLeitmensal.KWH_REVERSO_RES = ldblReversoResiduo
            'Irrigante
            objLeitmensal.LEIT_IRRIGANTE_ANT = lstrIrriganteLAnter
            objLeitmensal.LEIT_IRRIGANTE_ATUAL = lstrIrriganteLAtual
            objLeitmensal.KWH_IRRIGANTE = ldblIrriganteConsumo
            objLeitmensal.KWH_IRRIGANTE_RES = ldblIrriganteResiduo
            'Tarifa Branca
            objLeitmensal.LEITURA_ANT_PONTA = lstrLeitAnterPonta
            objLeitmensal.LEITURA_ATUAL_PONTA = lstrLeitAtualPonta
            objLeitmensal.LEITURA_ANT_INTERMEDIARIO = lstrLeitAnterInter
            objLeitmensal.LEITURA_ATUAL_INTERMEDIARIO = lstrLeitAtualInter
            objLeitmensal.LEITURA_ANT_FORA_PONTA = lstrLeitAnterForaP
            objLeitmensal.LEITURA_ATUAL_FORA_PONTA = lstrLeitAtualForaP
            objLeitmensal.KWH_PONTA = ldblkWhPonta
            objLeitmensal.KWH_INTERMEDIARIO = ldblkWhInter
            objLeitmensal.KWH_FORA_PONTA = ldblkWhForaP
            objLeitmensal.RESIDUO_PONTA = ldblResPonta
            objLeitmensal.RESIDUO_INTERMEDIARIO = ldblResInter
            objLeitmensal.RESIDUO_FORA_PONTA = ldblResForaP

            objLeitmensal.MEDIA = lintGradeMedia
            objLeitmensal.ZONA = lstrZona
            objLeitmensal.LIVRO = lstrLivro
            objLeitmensal.CONTA = lstrConta
            objLeitmensal.UNID_ID = llngUnidId

            lUpdate(objLeitmensal)

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Function GetUlt3Consumos(ByVal astrZona As String, ByVal astrLivro As String, ByVal astrConta As String, ByVal alngUnidId As Integer) As String

        Dim lstrConsumos(3) As String
        Dim llngCount As Integer

        Dim objNf As New entidadeNf
        Dim repositorioNf As New repositorioNf

        Dim vlist

        Try
            lstrConsumos(1) = "0"
            lstrConsumos(2) = "0"
            lstrConsumos(3) = "0"

            objNf.MESANO_REF = 3
            objNf.ITENSSERV = "030201008"
            objNf.GRUPO = "B"
            objNf.UNID_ID = alngUnidId
            objNf.FLGST = "C"


            vlist = repositorioNf.Select_10(objNf)

            If vlist.Count > 0 Then

                llngCount = 0

                For Each objNfFor As entidadeNf In vlist

                    llngCount = llngCount + 1
                    lstrConsumos(llngCount) = IIf(IsDBNull(objNfFor.ITENSSERV), "0", objNfFor.ITENSSERV)

                Next

            End If

            GetUlt3Consumos = lstrConsumos(1) & Chr(13) & lstrConsumos(2) & Chr(13) & lstrConsumos(3)

        Catch ex As Exception
            Exit Function
        End Try


    End Function

    Private Sub lPrint(ByRef dt As DataTable)

        Dim dc As DataColumn
        Dim ds As DataSet

        Dim bytBLOBData() As Byte

        Dim rpt As New ReportDocument()

        Dim vlist

        Dim objEmpresa As New entidadeEmpresa
        Dim repositorioEmpresa As New repositorioEmpresa

        Try

            ds = New DataSet

            vlist = repositorioEmpresa.Select(objEmpresa)

            If vlist.Count <> 0 Then

                For Each objEmpresaFor As entidadeEmpresa In vlist

                    bytBLOBData = objEmpresaFor.LOGOTIPO

                Next

            End If

            If dt.Rows.Count > 0 Then

                dc = New DataColumn("IMAGE", GetType(Byte()))
                dc.DefaultValue = bytBLOBData
                dt.Columns.Add(dc)

            End If

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


            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 rptLeituraManual

                rpt.SetDataSource(ds)

                frmViewRelatorios.rpt = rpt

                frmViewRelatorios.CRViewer.Zoom((100))
                frmViewRelatorios.Text = "Relatório de Leitura Manual"
                frmViewRelatorios.ShowDialog()

            End If

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Function lUpdate(ByRef objLeitmensal As entidadeLeitmensal) As Boolean

        Dim repositorioLeitmensal As New repositorioLeitmensal

        Try

            repositorioLeitmensal.Update_1(objLeitmensal)

            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function

    Private Function lUpdate_2(ByRef objLeitmensal As entidadeLeitmensal) As Boolean

        Dim repositorioLeitmensal As New repositorioLeitmensal

        Try

            repositorioLeitmensal.Update_2(objLeitmensal)
            'repositorioLeitmensal.Update_1(objLeitmensal)

            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function

    Private Function lInsert(ByRef objLeitmensal As entidadeLeitmensal) As Boolean

        Dim repositorioLeitmensal As New repositorioLeitmensal

        Try

            repositorioLeitmensal.Insert(objLeitmensal)

            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function

    Private Function TrataMediaFiltro(Optional dblMediaOpcional As Double = 0, Optional dblConsumoOpcional As Double = 0) As String

        Dim ldblMedia As Double
        Dim ldblMaior As Double
        Dim ldblMenor As Double

        Try

            If dblMediaOpcional > 0 Then
                ldblMedia = dblMediaOpcional
            Else
                ldblMedia = CDbl(IIf(Me.mfgConsumos.get_TextMatrix(1, 14) = "", 0, Me.mfgConsumos.get_TextMatrix(1, 14)))
            End If

            ldblMaior = ldblMedia + (ldblMedia * idblPercentual)
            ldblMenor = ldblMedia - (ldblMedia * idblPercentual)

            If dblConsumoOpcional > 0 And dblConsumoOpcional > ldblMaior Then
                TrataMediaFiltro = "ATENÇÃO! O consumo esta ACIMA dos " & (idblPercentual * 100) & "% da média."
                bAcimaPercentual = True
            ElseIf dblConsumoOpcional > 0 And dblConsumoOpcional < ldblMenor Then
                TrataMediaFiltro = "ATENÇÃO! O consumo esta ABAIXO dos " & (idblPercentual * 100) & "% da média."
                bAbaixoPercentual = True
            Else
                TrataMediaFiltro = ""
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function
        End Try

    End Function

    Private Sub chkpercentual_CheckedChanged(sender As Object, e As EventArgs) Handles chkpercentual.CheckedChanged
        Try

            If chkpercentual.Checked = True Then
                chkpercentualbaixo.Checked = False
            End If

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

    Private Sub chkpercentualbaixo_CheckedChanged(sender As Object, e As EventArgs) Handles chkpercentualbaixo.CheckedChanged
        Try

            If chkpercentualbaixo.Checked = True Then
                chkpercentual.Checked = False
            End If

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

    Private Sub chkAntMaiorAtual_CheckedChanged(sender As Object, e As EventArgs) Handles chkAntMaiorAtual.CheckedChanged
        If Me.chkAntMaiorAtual.Checked Then
            Me.chkLeituraAnt.Checked = False
            Me.chkLeituraZerada.Checked = False
        End If
    End Sub

    Private Sub chkLeituraAnt_CheckedChanged(sender As Object, e As EventArgs) Handles chkLeituraAnt.CheckedChanged
        If Me.chkLeituraAnt.Checked Then
            Me.chkAntMaiorAtual.Checked = False
            Me.chkLeituraZerada.Checked = False
        End If
    End Sub

    Private Sub chkLeituraZerada_CheckedChanged(sender As Object, e As EventArgs) Handles chkLeituraZerada.CheckedChanged
        If Me.chkLeituraZerada.Checked Then
            Me.chkAntMaiorAtual.Checked = False
            Me.chkLeituraAnt.Checked = False
        End If
    End Sub

    Private Sub mfgLeitura_MouseDownEvent(sender As Object, e As DMSFlexGridEvents_MouseDownEvent) Handles mfgLeitura.MouseDownEvent
        If mfgLeitura.Row = 0 Then
            Exit Sub
        End If

        If e.button = 1 Then
            With mfgLeitura
                If .MouseRow <> 0 And (.MouseCol = 0 Or .MouseCol = 98) Then
                    AtualizaCheckBoxGrid(.MouseRow, .MouseCol)
                End If
            End With
        End If
    End Sub

    Private Sub AtualizaCheckBoxGrid(iRow As Integer, iCol As Integer)
        With mfgLeitura
            If .get_TextMatrix(iRow, iCol) = strUnChecked Then
                .set_TextMatrix(iRow, iCol, strChecked)
            Else
                .set_TextMatrix(iRow, iCol, strUnChecked)
            End If
        End With
    End Sub

    Private Sub cmdFotoEvidencia_Click(sender As Object, e As EventArgs) Handles cmdFotoEvidencia.Click

        Dim vlist
        Dim repositorioLeituraMensalFoto As New repositorioLeituraMensalFoto

        Try
            'unid_id = "9458, 13130, 11187, 12443, 7232"
            vlist = repositorioLeituraMensalFoto.Select_Fotos_UC(unid_id)

            Dim ffotos As New frmGaleriaFotos(vlist) : ffotos.Show(Me)
        Catch ex As Exception
            Exit Sub
        End Try
    End Sub

    Private Sub mfgLeitura_Enter(sender As Object, e As EventArgs) Handles mfgLeitura.Enter

    End Sub


#End Region



End Class