Option Strict Off
Option Explicit On

Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE

Friend Class frmCadPercentualLivres
    Inherits System.Windows.Forms.Form

    Dim ibytModo As Byte '1-> novo / '2-> editar
    Dim ilngConsId As Integer
    Dim ilngUnidId As Integer
    Dim vResp As Short

#Region "Propriedades"

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



        Try

            ilngConsId = 0
            ilngUnidId = 0
            ibytModo = 0

            Me.cmbCodigo.Items.Clear()
            Me.cmbDecricao.Items.Clear()

            Me.cmbCodigo.Items.Add("0302010051")
            Me.cmbCodigo.Items.Add("0302010052")
            Me.cmbCodigo.Items.Add("0302010055")
            Me.cmbCodigo.Items.Add("0302010056")

            Me.cmbDecricao.Items.Add("DESC.PROV.DEM.")
            Me.cmbDecricao.Items.Add("DESC.REAL.DEM.")
            Me.cmbDecricao.Items.Add("DESC.PROV.ENERG.")
            Me.cmbDecricao.Items.Add("DESC.REAL.ENERG.")

            MontaGrid()
            habDesabTudo(False)


        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 mfgDados_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mfgDados.ClickEvent

        Dim lintAux As Short


        Try


            With Me.mfgDados
                If .Rows > 1 Then
                    For lintAux = 0 To Me.cmbCodigo.Items.Count
                        If VB6.GetItemString(Me.cmbCodigo, lintAux) = .get_TextMatrix(.RowSel, 6) Then
                            Me.cmbCodigo.SelectedIndex = lintAux
                            Me.cmbDecricao.SelectedIndex = lintAux
                        End If
                    Next lintAux
                    Me.dtpFatur.Value = CDate("01/" & .get_TextMatrix(.RowSel, 8))
                    Me.dtpRefer.Value = CDate("01/" & .get_TextMatrix(.RowSel, 9))
                    Me.txtValor.Text = FormatNumber(.get_TextMatrix(.RowSel, 10), 7)
                    Me.txtTextoRef.Text = .get_TextMatrix(.RowSel, 13)
                    Call habDesabTudo(False)
                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 Sub mfgDados_KeyDownEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSFlexGridLib.DMSFlexGridEvents_KeyDownEvent) Handles mfgDados.KeyDownEvent


        Try

            mfgDados_ClickEvent(mfgDados, 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 txtConta_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtConta.Enter


        Try


            With Me.txtConta
                .SelectionStart = 0
                .SelectionLength = 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 txtConta_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtConta.Leave


        Try


            With Me.txtConta
                .Text = VB6.Format(.Text, "000000")
            End With

            If Me.txtZona.Text <> "" And Me.txtLivro.Text <> "" And Me.txtConta.Text <> "" Then
                SetaCodigo(0, Me.txtZona.Text, Me.txtLivro.Text, Me.txtConta.Text, "A")
                MontaGrid()
                PreencheGrid()
            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 txtLivro_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLivro.Enter


        Try


            With Me.txtLivro
                .SelectionStart = 0
                .SelectionLength = Len(.Text)
            End With
            'Me.txtConta.Text = ""



        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 txtLivro_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLivro.Leave


        Try


            With Me.txtLivro
                .Text = VB6.Format(.Text, "00")
            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 txtValor_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtValor.Enter


        Try


            With Me.txtValor
                .SelectionStart = 0
                .SelectionLength = 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 txtZona_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtZona.Enter


        Try


            With Me.txtZona
                .SelectionStart = 0
                .SelectionLength = Len(.Text)
            End With
            'Me.txtLivro.Text = ""
            'Me.txtConta.Text = ""



        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 txtZona_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtZona.Leave


        Try


            With Me.txtZona
                .Text = VB6.Format(.Text, "000")
            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 txtTextoRef_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtTextoRef.Enter


        Try


            With Me.txtTextoRef
                .SelectionStart = 0
                .SelectionLength = 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 cmbDecricao_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmbDecricao.TextChanged

        Try


            Me.cmbCodigo.SelectedIndex = Me.cmbDecricao.SelectedIndex



        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 cmbDecricao_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmbDecricao.SelectedIndexChanged

        Try


            cmbDecricao_TextChanged(cmbDecricao, 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 cmdEditar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEditar.Click

        Try



            ibytModo = 2
            habDesabTudo(True)
            Me.cmbDecricao.Focus()


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

        Dim lstrSql As String


        Dim objAtualizaBancoDados As New entidadeAtualizaBancoDados
        Dim repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados


        Try



            With Me.mfgDados
                If .RowSel < 1 Then
                    MessageBox.Show("É necessário selecionar um registro!", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Else
                    vResp = MessageBox.Show("Esta operação não possui retorno!" & vbCrLf & vbCrLf & "Tem certeza de que deseja excluir este registro?", "Confirmação de Exclusão ", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)

                    If vResp = DialogResult.Yes Then
                        lstrSql = "DELETE FROM `descontoprovisorio` " & vbCrLf
                        lstrSql = lstrSql & " WHERE `descontoprovisorio`.`id`    = " & CInt(.get_TextMatrix(.RowSel, 0)) & vbCrLf

                        objAtualizaBancoDados.QUERY_NAME = lstrSql
                        repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

                        MontaGrid()
                        PreencheGrid()

                    End If

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

        Try



            frmLocalizaUnidade.Show()
            frmLocalizaUnidade.SetaLocalizaUnidade("U", Me)


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

        Try


            lClear()
            habDesabTudo(True)
            ibytModo = 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 cmdSalvar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSalvar.Click

        Dim lstrSql As String


        Dim objAtualizaBancoDados As New entidadeAtualizaBancoDados
        Dim repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados


        Try


            If ibytModo = 1 Then
                lstrSql = "INSERT INTO "
            ElseIf ibytModo = 2 Then
                lstrSql = "UPDATE "
            End If

            With Me.mfgDados
                lstrSql = lstrSql & " `descontoprovisorio` SET " & vbCrLf
                lstrSql = lstrSql & "       `cons_id`    = " & ilngConsId & ", " & vbCrLf
                lstrSql = lstrSql & "       `unid_id`    = " & ilngUnidId & ", " & vbCrLf
                lstrSql = lstrSql & "       `zona`       = '" & Me.txtZona.Text & "', " & vbCrLf
                lstrSql = lstrSql & "       `livro`      = '" & Me.txtLivro.Text & "', " & vbCrLf
                lstrSql = lstrSql & "       `conta`      = '" & Me.txtConta.Text & "', " & vbCrLf
                lstrSql = lstrSql & "       `codigo`     = '" & VB6.GetItemString(Me.cmbCodigo, Me.cmbCodigo.SelectedIndex) & "', " & vbCrLf
                lstrSql = lstrSql & "       `valor`      = " & Replace(CStr(CDbl(IIf(IsNumeric(Me.txtValor.Text), Me.txtValor.Text, 0))), ",", ".") & ", " & vbCrLf
                lstrSql = lstrSql & "       `texto_ref`  = '" & Replace(Me.txtTextoRef.Text, "'", "\'") & "', "
                lstrSql = lstrSql & "       `mesano`     = " & CInt(VB6.Format(Me.dtpFatur.Value, "yyyymm")) & ", " & vbCrLf
                lstrSql = lstrSql & "       `referencia` = " & CInt(VB6.Format(Me.dtpRefer.Value, "yyyymm")) & " " & vbCrLf
                If ibytModo = 2 Then
                    lstrSql = lstrSql & " WHERE `descontoprovisorio`.`id`    = " & CInt(.get_TextMatrix(.RowSel, 0)) & vbCrLf
                End If
            End With

            objAtualizaBancoDados.QUERY_NAME = lstrSql
            repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            If ibytModo = 1 Then
                lClear()
            End If

            habDesabTudo(False)
            MontaGrid()
            PreencheGrid()



        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 frmCadPercentualLivres_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

        Try

            If Not vfrmOld Is Nothing Then
                vfrmOld.Enabled = True
            End If

            vfrmOld = Nothing

        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 frmCadPercentualLivres_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)


        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then
                TabEnter(KeyAscii, eventArgs)
            ElseIf KeyAscii = System.Windows.Forms.Keys.Escape Then
                cmdFechar_Click(cmdFechar, New System.EventArgs())
            End If

            eventArgs.KeyChar = Chr(KeyAscii)

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



        Catch ex As Exception
            MessageBox.Show("Ocorreu 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

            Close()

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub lClear()

        Try

            Me.txtTextoRef.Text = ""
            Me.txtValor.Text = "0"
            Me.cmbCodigo.SelectedIndex = 0
            Me.cmbDecricao.SelectedIndex = 0
            Me.dtpFatur.Value = CDate(VB6.Format("01/" & VB6.Format(BuscaDataBD, "mm/yyyy"), "dd/mm/yyyy"))
            Me.dtpRefer.Value = Me.dtpFatur.Value

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub MontaGrid()


        Try

            With mfgDados
                Call .Clear()
                .Rows = 1
                .set_ColWidth(0, 0)
                .set_TextMatrix(0, 0, "Id")
                .set_ColWidth(1, 0)
                .set_TextMatrix(0, 1, "Cons_id")
                .set_ColWidth(2, 0)
                .set_TextMatrix(0, 2, "Unid_id")
                .set_ColWidth(3, 0)
                .set_TextMatrix(0, 3, "Zona")
                .set_ColWidth(4, 0)
                .set_TextMatrix(0, 4, "Livro")
                .set_ColWidth(5, 0)
                .set_TextMatrix(0, 5, "Conta")
                .set_ColWidth(6, 0)
                .set_TextMatrix(0, 6, "Código")
                .set_ColWidth(7, 2000)
                .set_TextMatrix(0, 7, "Descrição")
                .set_ColWidth(8, 800)
                .set_TextMatrix(0, 8, "Fatur.") 'mesano
                .set_ColWidth(9, 800)
                .set_TextMatrix(0, 9, "Refer.")
                .set_ColWidth(10, 1200)
                .set_TextMatrix(0, 10, "Percentual")
                .set_ColWidth(11, 0)
                .set_TextMatrix(0, 11, "Demanda Ponta")
                .set_ColWidth(12, 0)
                .set_TextMatrix(0, 12, "Demanda F.Ponta")
                .set_ColWidth(13, 0)
                .set_TextMatrix(0, 13, "Texto referencia")
            End With


        Catch ex As Exception
            Exit Sub
        End Try


    End Sub

    Private Sub PreencheGrid()

        Dim llngId As Integer
        Dim lstrZona As String
        Dim lstrLivro As String
        Dim lstrConta As String
        Dim lstrCodigo As String
        Dim ldblValor As Double
        Dim llngMesano As Integer
        Dim llngRefere As Integer
        Dim ldblDemanP As Double
        Dim ldblDemaFp As Double
        Dim lstrTextoRef As String
        Dim lintIdx As Short
        Dim lstrSql As String


        Dim dt As DataTable


        Dim objAtualizaBancoDados As New entidadeAtualizaBancoDados
        Dim repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados


        Try


            lstrSql = ""
            lstrSql = lstrSql & "SELECT  " & vbCrLf
            lstrSql = lstrSql & "       `descontoprovisorio`.`id`, " & vbCrLf
            lstrSql = lstrSql & "       `descontoprovisorio`.`cons_id`, " & vbCrLf
            lstrSql = lstrSql & "       `descontoprovisorio`.`unid_id`, " & vbCrLf
            lstrSql = lstrSql & "       `descontoprovisorio`.`zona`, " & vbCrLf
            lstrSql = lstrSql & "       `descontoprovisorio`.`livro`, " & vbCrLf
            lstrSql = lstrSql & "       `descontoprovisorio`.`conta`, " & vbCrLf
            lstrSql = lstrSql & "       `descontoprovisorio`.`codigo`, " & vbCrLf
            lstrSql = lstrSql & "       `descontoprovisorio`.`valor`, " & vbCrLf
            lstrSql = lstrSql & "       `descontoprovisorio`.`mesano`, " & vbCrLf
            lstrSql = lstrSql & "       `descontoprovisorio`.`referencia`, " & vbCrLf
            lstrSql = lstrSql & "       `descontoprovisorio`.`demanda_p`, " & vbCrLf
            lstrSql = lstrSql & "       `descontoprovisorio`.`demanda_fp`, " & vbCrLf
            lstrSql = lstrSql & "       `descontoprovisorio`.`texto_ref` " & vbCrLf
            lstrSql = lstrSql & "  FROM `descontoprovisorio` " & vbCrLf
            lstrSql = lstrSql & " WHERE `descontoprovisorio`.`cons_id` = " & ilngConsId & vbCrLf
            lstrSql = lstrSql & "   AND `descontoprovisorio`.`unid_id` = " & ilngUnidId & vbCrLf
            lstrSql = lstrSql & " ORDER BY `mesano` DESC, `referencia` DESC, `codigo` ASC" & vbCrLf


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


            If dt.Rows.Count <> 0 Then

                For Each row As DataRow In dt.Rows


                    llngId = CInt(IIf(IsDBNull(row("id")), 0, row("id")))

                    lstrZona = Trim(IIf(IsDBNull(row("zona")), "", row("zona")))

                    lstrLivro = Trim(IIf(IsDBNull(row("livro")), "", row("livro")))

                    lstrConta = Trim(IIf(IsDBNull(row("conta")), "", row("conta")))

                    lstrCodigo = Trim(IIf(IsDBNull(row("codigo")), "", row("codigo")))

                    ldblValor = CDbl(IIf(IsDBNull(row("valor")), 0, row("valor")))

                    llngMesano = CInt(IIf(IsDBNull(row("mesano")), 0, row("mesano")))

                    llngRefere = CInt(IIf(IsDBNull(row("referencia")), 0, row("referencia")))

                    ldblDemanP = CDbl(IIf(IsDBNull(row("demanda_p")), 0, row("demanda_p")))

                    ldblDemaFp = CDbl(IIf(IsDBNull(row("demanda_fp")), 0, row("demanda_fp")))

                    lstrTextoRef = Trim(IIf(IsDBNull(row("texto_ref")), "", row("texto_ref")))

                    If CDbl(Microsoft.VisualBasic.Right(lstrCodigo, 1)) = 1 Then lintIdx = 0
                    If CDbl(Microsoft.VisualBasic.Right(lstrCodigo, 1)) = 2 Then lintIdx = 1
                    If CDbl(Microsoft.VisualBasic.Right(lstrCodigo, 1)) = 5 Then lintIdx = 2
                    If CDbl(Microsoft.VisualBasic.Right(lstrCodigo, 1)) = 6 Then lintIdx = 3

                    With Me.mfgDados
                        .Rows = .Rows + 1
                        .set_TextMatrix(.Rows - 1, 0, llngId)
                        .set_TextMatrix(.Rows - 1, 1, ilngConsId)
                        .set_TextMatrix(.Rows - 1, 2, ilngUnidId)
                        .set_TextMatrix(.Rows - 1, 3, lstrZona)
                        .set_TextMatrix(.Rows - 1, 4, lstrLivro)
                        .set_TextMatrix(.Rows - 1, 5, lstrConta)
                        .set_TextMatrix(.Rows - 1, 6, lstrCodigo)
                        .set_TextMatrix(.Rows - 1, 7, VB6.GetItemString(Me.cmbDecricao, lintIdx))
                        .set_TextMatrix(.Rows - 1, 8, Microsoft.VisualBasic.Right(CStr(llngMesano), 2) & "/" & Microsoft.VisualBasic.Left(CStr(llngMesano), 4))
                        .set_TextMatrix(.Rows - 1, 9, Microsoft.VisualBasic.Right(CStr(llngRefere), 2) & "/" & Microsoft.VisualBasic.Left(CStr(llngRefere), 4))
                        .set_TextMatrix(.Rows - 1, 10, FormatNumber(ldblValor, 7))
                        .set_TextMatrix(.Rows - 1, 11, ldblDemanP)
                        .set_TextMatrix(.Rows - 1, 12, ldblDemaFp)
                        .set_TextMatrix(.Rows - 1, 13, lstrTextoRef)
                    End With



                Next

            End If



        Catch ex As Exception
            Exit Sub
        End Try


    End Sub

    Private Sub habDesabTudo(ByRef forma As Boolean)



        Try


            Me.txtValor.Enabled = forma
            Me.cmbCodigo.Enabled = forma
            Me.cmbDecricao.Enabled = forma
            Me.dtpFatur.Enabled = forma
            Me.dtpRefer.Enabled = forma
            'Me.mfgDados.Enabled = forma
            Me.txtTextoRef.Enabled = forma
            If Not vfrmOld Is Nothing Then
                Me.txtZona.Enabled = False
                Me.txtLivro.Enabled = False
                Me.txtConta.Enabled = False
                Me.cmdLocalizaZlc.Enabled = False
            Else
                Me.txtZona.Enabled = Not forma
                Me.txtLivro.Enabled = Not forma
                Me.txtConta.Enabled = Not forma
                Me.cmdLocalizaZlc.Enabled = Not forma
            End If

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Public Sub SetaParametros(ByRef afrmQuemChama As System.Windows.Forms.Form, ByVal astrZona As String, ByVal astrLivro As String, ByVal astrConta As String, ByVal alngUnidId As Integer, ByVal astrGrupo As String)



        Try


            If Not vfrmOld Is Nothing Then

                vfrmOld = afrmQuemChama

                SetaCodigo(alngUnidId, astrZona, astrLivro, astrConta, astrGrupo)

                vfrmOld.Enabled = False
            End If


        Catch ex As Exception
            Exit Sub
        End Try


    End Sub

    Public Sub SetaCodigo(ByVal alngUnidId As Integer, ByVal astrZona As String, ByVal astrLivro As String, ByVal astrConta As String, ByVal astrGrupo As String)

        Dim lstrSql As String

        Dim dt As DataTable


        Dim objAtualizaBancoDados As New entidadeAtualizaBancoDados
        Dim repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados


        Try

            Me.txtNome.Text = ""

            lstrSql = ""
            lstrSql = lstrSql & "SELECT cons_id, unid_id, nome, ta " & vbCrLf
            lstrSql = lstrSql & "  FROM uc_faturamento" & vbCrLf
            lstrSql = lstrSql & " WHERE zona  = '" & astrZona & "' " & vbCrLf
            lstrSql = lstrSql & "   AND livro = '" & astrLivro & "' " & vbCrLf
            lstrSql = lstrSql & "   AND conta = '" & astrConta & "' "

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


            If dt.Rows.Count <> 0 Then

                For Each row As DataRow In dt.Rows

                    Me.txtNome.Text = IIf(IsDBNull(row("nome")), "", row("nome"))
                    ilngUnidId = IIf(IsDBNull(row("unid_id")), 0, row("unid_id"))
                    ilngConsId = IIf(IsDBNull(row("cons_id")), 0, row("cons_id"))
                    Me.txtZona.Text = astrZona
                    Me.txtLivro.Text = astrLivro
                    Me.txtConta.Text = astrConta
                    Me.txtTa.Text = IIf(IsDBNull(row("ta")), "01A400", row("ta"))

                Next

            End If



            MontaGrid()
            PreencheGrid()
            habDesabTudo(False)



        Catch ex As Exception
            Exit Sub
        End Try

    End Sub


#End Region

End Class