Option Strict Off
Option Explicit On


Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE
Friend Class frmLocalizaCnae
    Inherits System.Windows.Forms.Form

#Region "Propriedades"

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

        Try
            Me.tevCnae.Nodes.Clear()
            CarregaCnae()
        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 tevCnae_AfterCollapse(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.TreeViewEventArgs) Handles tevCnae.AfterCollapse

        Dim Node As System.Windows.Forms.TreeNode = eventArgs.Node

        Try

            Node.ImageKey = "fechado"

        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 tevCnae_DoubleClick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles tevCnae.DoubleClick

        Try

            With Me.tevCnae
                If .Nodes.Item(.SelectedNode.Index).GetNodeCount(False) = 0 Then
                    vfrmOld.SetaCnae(CInt(IIf(IsNumeric(Me.txtIdCnae.Text), Me.txtIdCnae.Text, 0)), "")
                    cmdFechar_Click(cmdFechar, New System.EventArgs())
                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 tevCnae_AfterExpand(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.TreeViewEventArgs) Handles tevCnae.AfterExpand

        Dim Node As System.Windows.Forms.TreeNode = eventArgs.Node

        Try

            Node.ImageKey = "aberto"

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

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)


        Try


            If KeyAscii = System.Windows.Forms.Keys.Return Then
                tevCnae_DoubleClick(tevCnae, 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

    Private Sub tevCnae_NodeClick(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles tevCnae.NodeMouseClick

        Dim Node As System.Windows.Forms.TreeNode = eventArgs.Node

        Try

            With Node
                'Me.txtIdCnae.Text = Strings.Left$(.Key, Strings.InStr(1, .Key, "[") - 1)
                Me.txtIdCnae.Text = .Tag
            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 txtLocalizaCodigoDivisao_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLocalizaCodigoDivisao.Enter

        Try


            With Me.txtLocalizaCodigoDivisao
                .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 txtLocalizaCodigoDivisao_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtLocalizaCodigoDivisao.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
                KeyAscii = 0
            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 txtLocalizaCodigoDivisao_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLocalizaCodigoDivisao.Leave

        Try

            With Me.txtLocalizaCodigoDivisao
                .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 txtLocalizaCodigoGrupo_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLocalizaCodigoGrupo.Enter

        Try

            With Me.txtLocalizaCodigoGrupo
                .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 txtLocalizaCodigoGrupo_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtLocalizaCodigoGrupo.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
                KeyAscii = 0
            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 txtLocalizaCodigoGrupo_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLocalizaCodigoGrupo.Leave

        Try

            With Me.txtLocalizaCodigoGrupo
                .Text = VB6.Format(.Text, "0")
            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 txtLocalizaCodigoClasse_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLocalizaCodigoClasse.Enter


        Try

            With Me.txtLocalizaCodigoClasse
                .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 txtLocalizaCodigoClasse_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtLocalizaCodigoClasse.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
                KeyAscii = 0
            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 txtLocalizaCodigoClasse_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLocalizaCodigoClasse.Leave

        Try
            With Me.txtLocalizaCodigoClasse
                .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 txtLocalizaCodigoSubClasse_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLocalizaCodigoSubClasse.Enter

        Try

            With Me.txtLocalizaCodigoSubClasse
                .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 txtLocalizaCodigoSubClasse_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtLocalizaCodigoSubClasse.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
                KeyAscii = 0
            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 txtLocalizaCodigoSubClasse_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLocalizaCodigoSubClasse.Leave

        Try

            With Me.txtLocalizaCodigoSubClasse
                .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 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 cmdLocalizar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdLocalizar.Click

        Dim llngIdx As Integer
        Dim lstrCod As String
        Dim llngAux As Integer
        Dim lstrAux As String

        Try

            lstrAux = Me.txtLocalizaCodigoDivisao.Text & Me.txtLocalizaCodigoGrupo.Text & Me.txtLocalizaCodigoClasse.Text & Me.txtLocalizaCodigoSubClasse.Text

            With Me.tevCnae
                For llngIdx = 1 To .Nodes.Count - 1

                    lstrCod = Mid(.Nodes.Item(llngIdx).Name, InStr(1, .Nodes.Item(llngIdx).Name, "]") + 1)

                    If lstrCod = lstrAux Then

                        .Nodes.Item(llngIdx).Expand()
                        llngAux = llngIdx

                        While Not .Nodes.Item(llngAux).Parent Is Nothing

                            .Nodes.Item(llngAux).Parent.Expand()

                            If Not .Nodes.Item(llngAux).Parent Is Nothing Then
                                llngAux = .Nodes.Item(llngAux).Parent.Index
                            End If

                        End While

                        '.Nodes.Item(llngIdx).Selected = True
                        Call tevCnae_NodeClick(tevCnae, New System.Windows.Forms.TreeNodeMouseClickEventArgs(tevCnae.SelectedNode, System.Windows.Forms.MouseButtons.None, 0, 0, 0))
                        Call .Focus()
                    End If
                Next llngIdx
            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 frmLocalizaCnae_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
        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

    Public Sub SetaLocalizaCnae(ByRef frmOld As System.Windows.Forms.Form)
        Try

            vfrmOld = frmOld
            vfrmOld.Enabled = False

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub CarregaCnae()

        Dim lblnPriVez As Boolean
        Dim llngIdGrup As Integer
        Dim llngIdCnae As Integer
        Dim lstrCdCnae As String
        Dim lstrDsCnae As String
        Dim lstrCtCnae As String
        Dim lstrSeCnae As String
        Dim lstrDiCnae As String
        Dim lstrGrCnae As String
        Dim lstrClCnae As String
        Dim lstrSuCnae As String
        Dim lstrNoPai As String
        Dim lstrFilho As String
        Dim lstrDesAux As String

        Dim vlist

        Dim objCnae As New entidadeCnae
        Dim repositorioCnae As New repositorioCnae

        Try

            vlist = repositorioCnae.Select(objCnae)

            If vlist.Count > 0 Then

                For Each objCnaeFor As entidadeCnae In vlist

                    llngIdCnae = Convert.ToInt32(IIf(IsDBNull(objCnaeFor.ID), 0, objCnaeFor.ID))
                    lstrCdCnae = Trim(IIf(IsDBNull(objCnaeFor.CODIGO), "", objCnaeFor.CODIGO))
                    lstrDsCnae = Trim(IIf(IsDBNull(objCnaeFor.DESCRICAO), "", objCnaeFor.DESCRICAO))
                    lstrCtCnae = Trim(IIf(IsDBNull(objCnaeFor.CONTROLE), "Interno", objCnaeFor.CONTROLE))
                    lstrSeCnae = Trim(IIf(IsDBNull(objCnaeFor.SECAO), "A", objCnaeFor.SECAO))
                    lstrDiCnae = Trim(IIf(IsDBNull(objCnaeFor.DIVISAO), "00", objCnaeFor.DIVISAO))
                    lstrGrCnae = Trim(IIf(IsDBNull(objCnaeFor.GRUPO), "0", objCnaeFor.GRUPO))
                    lstrClCnae = Trim(IIf(IsDBNull(objCnaeFor.CLASSE), "00", objCnaeFor.CLASSE))
                    lstrSuCnae = Trim(IIf(IsDBNull(objCnaeFor.SUBCLASSE), "00", objCnaeFor.SUBCLASSE))

                    If lstrSuCnae <> "" Then
                        lstrNoPai = llngIdGrup & "[" & lstrSeCnae & "]" & lstrDiCnae & lstrGrCnae & lstrClCnae
                        lstrFilho = lstrNoPai & lstrSuCnae
                    ElseIf lstrClCnae <> "" Then
                        lstrNoPai = llngIdGrup & "[" & lstrSeCnae & "]" & lstrDiCnae & lstrGrCnae
                        lstrFilho = lstrNoPai & lstrClCnae
                    ElseIf lstrGrCnae <> "" Then
                        lstrNoPai = llngIdGrup & "[" & lstrSeCnae & "]" & lstrDiCnae
                        lstrFilho = lstrNoPai & lstrGrCnae
                    ElseIf lstrDiCnae <> "" Then
                        lstrNoPai = llngIdGrup & "[" & lstrSeCnae & "]"
                        lstrFilho = lstrNoPai & lstrDiCnae
                    Else
                        lstrNoPai = llngIdCnae & "[" & lstrSeCnae & "]"
                        llngIdGrup = llngIdCnae
                        lblnPriVez = True
                    End If

                    With Me.tevCnae.Nodes
                        If lblnPriVez Then
                            Call .Add(lstrNoPai, lstrDsCnae & "-" & Mid(lstrCdCnae, 2), "fechado")
                            .Item(.Count - 1).Collapse()
                            .Item(.Count - 1).Tag = CStr(llngIdCnae)
                            lblnPriVez = False
                        Else
                            Call .Find(lstrNoPai, True)(0).Nodes.Add(lstrFilho, lstrDsCnae & "-" & Mid(lstrCdCnae, 2), IIf(lstrCtCnae = "Interno", "fechado", "ponto"))
                            .Item(.Count - 1).Collapse()
                            .Item(.Count - 1).Tag = CStr(llngIdCnae)
                        End If
                    End With

                Next

            End If


        Catch ex As Exception
            Exit Sub
        End Try

    End Sub


#End Region


End Class