Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE
Public Class frmCadastroCodigoClasse
    Private objAtualizaBancoDados As New entidadeAtualizaBancoDados
    Private repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados

    Private objExecAtualizaBancoDados As New entidadeAtualizaBancoDados
    Private repositorioExecAtualizaBancoDados As New repositorioAtualizaBancoDados
    Private dt, dt_1, dt_2, dt_3, dtinterno As DataTable
    Dim vresultDebito As Boolean
    Dim vresultCredito As Boolean
    Dim vresultCodigo As Boolean
    Dim vCodigo As String
    Dim vMascaraContaDebito As String
    Dim vMascaraCep As String

    Dim vResp As Short
    Dim vNew As Boolean

    'Private Sub txtcoddeb_KeyDown(sender As Object, e As KeyEventArgs)
    '    Try

    '        If IsNumeric(txtcoddeb.Text) And e.KeyCode = Keys.Enter Then
    '            LocalizarContaDeb(txtcoddeb.Text)
    '        ElseIf Trim(txtcoddeb.Text) = "" Then
    '            mskContaDebito.Text = String.Empty
    '        ElseIf e.KeyCode = Keys.F3 Then

    '            vresultDebito = True
    '            vresultCredito = False
    '            vresultCodigo = False
    '            frmPesquisaSimples.SetParametros("PLANO_CONTAS", "Plano Conta", Me, String.Empty, "GRAU = 7 AND TIPO = 'A'", "ID DESC", String.Empty)
    '            frmPesquisaSimples.Show()

    '        End If

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

    Private Sub cmdLocdeb_Click(sender As Object, e As EventArgs)
        Try

            vresultDebito = True
            vresultCredito = False
            vresultCodigo = False
            frmPesquisaSimples.SetParametros("PLANO_CONTAS", "Plano Conta", Me, String.Empty, "GRAU = 7 AND TIPO = 'A'", "ID DESC", String.Empty)
            frmPesquisaSimples.Show()

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

    End Sub

    Private Sub cmdFechar_Click(sender As Object, e As EventArgs) Handles cmdFechar.Click
        Me.Close()
    End Sub

    Private Sub frmCadastroCodigoClasse_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim sql As String
        Dim bd_contab As String = GetConfig("integracao.bd.contab")

        Try
            vMascaraContaDebito = GetConfig("conta.Mascara")
            mskContaDebito.Mask = vMascaraContaDebito
            mskContaCredito.Mask = vMascaraContaDebito
            mskDigitavel.Mask = vMascaraContaDebito

            cmbMunicipio.Enabled = False
            mskDigitavel.Enabled = False
            chkAtrelarClasse.Visible = False
            cmbClasseDigitavel.Visible = False

            Dim lstrsql As String = "SELECT codigo, descricao  FROM `classe` WHERE codigo IN('0100','9100','0000','4000','5000','6100','6400','7100') ORDER BY descricao"
            sCarregaComboGeral(lstrsql, cmbClasse)

            lstrsql = "SELECT cod as codigo , Nome as descricao   FROM `munic` WHERE conta_faturamento is not null"
            sCarregaComboGeral(lstrsql, cmbMunicipio)

            lstrsql = "SELECT codigo, descricao  FROM `classe` WHERE codigo IN('0100','9100','0000','4000','5000','6100','6400','7100') ORDER BY descricao"
            sCarregaComboGeral(lstrsql, cmbClasseDigitavel)

            Call MontaGrade()

            ' MontaGrid()

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

    End Sub

    'Public Sub LocalizarContaDeb(ByVal id As Double)

    '    Dim lstrdebito As String

    '    Dim vlist

    '    Dim objPlanoContas As New entidadePlanoContas
    '    Dim repositorioPlanoContas As New repositorioPlanoContas

    '    Try

    '        objPlanoContas.CONTA = String.Empty
    '        objPlanoContas.ID = id
    '        objPlanoContas.ATIVO = 1

    '        vlist = repositorioPlanoContas.Select(objPlanoContas)

    '        If vlist.Count > 0 Then
    '            For Each objPlanoContasFor As entidadePlanoContas In vlist

    '                txtcoddeb.Text = id
    '                lstrdebito = Trim(IIf(IsDBNull(objPlanoContasFor.CONTA), String.Empty, objPlanoContasFor.CONTA))
    '                lstrdebito = Replace(lstrdebito, ".", "")
    '                mskContaDebito.Text = lstrdebito

    '            Next

    '        Else
    '            mskContaDebito.Text = ""
    '        End If

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

    'End Sub
    'Private Sub txtcoddeb_KeyPress(sender As Object, e As KeyPressEventArgs)
    '    Try
    '        If e.KeyChar = Chr(Keys.Enter) Then
    '            e.KeyChar = Nothing
    '            'cmbtipo.Focus()
    '            SendKeys.Send("{TAB}")
    '        Else
    '            SomenteInteger(e.KeyChar, txtcoddeb, False)
    '        End If
    '    Catch ex As Exception
    '        MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtcoddeb_KeyPress" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
    '        Exit Sub
    '    End Try
    'End Sub
    Public Sub SetaCodigo(ByVal astrTabela As String, ByVal alngId_A As String, Optional ByVal alngId_B As Integer = 0, Optional ByVal alngId_C As Integer = 0, Optional ByVal alngId_D As Integer = 0)

        Dim lstrsql As String

        Try

            txtCodigo.Text = alngId_A

            lstrsql = "SELECT descri FROM codigos WHERE num = '" & alngId_A & "'"
            objAtualizaBancoDados.QUERY_NAME = lstrsql
            dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            If dt.Rows.Count > 0 Then
                For Each row As DataRow In dt.Rows
                    txtdesricao.Text = Trim(IIf(IsDBNull(row.Item("descri")), "", row.Item("descri")))
                Next
            End If

            Call MontaGrade()

            lstrsql = "SELECT * FROM codigo_conta WHERE codigo = '" & alngId_A & "'"
            objAtualizaBancoDados.QUERY_NAME = lstrsql
            dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            If dt.Rows.Count > 0 Then
                For Each row As DataRow In dt.Rows
                    'mskCadastrada.Text = Trim(IIf(IsDBNull(row.Item("cta_deb_fat")), "", row.Item("cta_deb_fat")))

                    With Me.mfgDados
                        .Rows = Me.mfgDados.Rows + 1
                        .set_TextMatrix(.Rows - 1, 0, Trim(IIf(IsDBNull(row.Item("codigo")), "", row.Item("codigo"))))
                        .set_TextMatrix(.Rows - 1, 1, Trim(IIf(IsDBNull(row.Item("tipo")), "", row.Item("tipo"))))
                        .set_TextMatrix(.Rows - 1, 2, Trim(IIf(IsDBNull(row.Item("descricao")), "", row.Item("descricao"))))
                        .set_TextMatrix(.Rows - 1, 3, Trim(IIf(IsDBNull(row.Item("conta")), "", row.Item("conta"))))
                        .set_TextMatrix(.Rows - 1, 4, Trim(IIf(IsDBNull(row.Item("id")), "", row.Item("id"))))
                    End With

                Next
            End If


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

    End Sub

    'Private Sub cmdLimpardeb_Click(sender As Object, e As EventArgs)
    '    Try

    '        txtcoddeb.Text = String.Empty
    '        mskContaDebito.Text = String.Empty

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

    'Private Sub cmdLimparcred_Click(sender As Object, e As EventArgs)
    '    Try

    '        txtcodcred.Text = String.Empty
    '        mskContaCredito.Text = String.Empty

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

    Private Sub cmdatualizar_Click(sender As Object, e As EventArgs) Handles cmdatualizar.Click
        Dim lstrsql As String
        Dim icodItem As Integer
        Try

            If Replace(mskContaCredito.Text, ",", "") <> "" Or Replace(mskContaDebito.Text, ",", "") <> "" Then
                If txtCodigo.Text <> "" Then

                    lstrsql = "SELECT * FROM codigos WHERE num = '" & txtCodigo.Text & "'"
                    objAtualizaBancoDados.QUERY_NAME = lstrsql
                    dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

                    If dt.Rows.Count <= 0 Then
                        MsgBox("O código informado inexistente", vbInformation + vbOKOnly, "Aviso")
                        Exit Sub
                    End If

                    lstrsql = ""
                    If opbDigitavel.Checked Then
                        If chkAtrelarClasse.Checked Then
                            lstrsql = lstrsql & "SELECT * FROM Codigo_Conta WHERE codigo = '" & txtCodigo.Text & "' AND conta = '" & Replace(mskDigitavel.Text, ",", ".") & "' AND cod_item = '" & VB6.GetItemData(Me.cmbClasseDigitavel, Me.cmbClasseDigitavel.SelectedIndex) & "'"
                        Else
                            lstrsql = lstrsql & "SELECT * FROM Codigo_Conta WHERE codigo = '" & txtCodigo.Text & "' AND conta = '" & Replace(mskDigitavel.Text, ",", ".") & "'"
                        End If
                    Else
                        lstrsql = lstrsql & "SELECT * FROM Codigo_Conta WHERE codigo = '" & txtCodigo.Text & "' AND conta = '" & IIf(opbClasse.Checked, Replace(mskContaDebito.Text, ",", "."), Replace(mskContaCredito.Text, ",", ".")) & "'"
                    End If
                    objAtualizaBancoDados.QUERY_NAME = lstrsql
                    dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

                    If dt.Rows.Count > 0 Then
                        MsgBox("Conta ja cadastrada para esse código !", vbInformation + vbOKOnly, "Aviso")
                        Exit Sub
                    End If

                    If opbClasse.Checked Then
                        If cmbClasse.Text <> "" Then
                            icodItem = VB6.GetItemData(Me.cmbClasse, Me.cmbClasse.SelectedIndex)
                        End If
                    ElseIf opbMunic.Checked Then
                        If cmbMunicipio.Text <> "" Then
                            icodItem = VB6.GetItemData(Me.cmbMunicipio, Me.cmbMunicipio.SelectedIndex)
                        End If
                    Else
                        If chkAtrelarClasse.Checked = True Then
                            icodItem = VB6.GetItemData(Me.cmbClasseDigitavel, Me.cmbClasseDigitavel.SelectedIndex)
                        Else
                            icodItem = 0
                        End If
                    End If

                        lstrsql = ""
                    lstrsql = lstrsql & "insert into Codigo_Conta(codigo,tipo,descricao,conta,cod_item)values ( "
                    lstrsql = lstrsql & " '" & txtCodigo.Text & "', "
                    lstrsql = lstrsql & " '" & IIf(opbClasse.Checked, "Classe", IIf(opbMunic.Checked, "Munic", IIf(chkAtrelarClasse.Checked, "Digitavel atrelado a" & icodItem, "Digitavel"))) & "', "
                    lstrsql = lstrsql & " '" & IIf(opbClasse.Checked, Replace(cmbClasse.Text, ",", "."), IIf(opbMunic.Checked, Replace(cmbMunicipio.Text, ",", "."), txtdesricao.Text)) & "', "
                    lstrsql = lstrsql & " '" & IIf(opbClasse.Checked, Replace(mskContaDebito.Text, ",", "."), IIf(opbMunic.Checked, Replace(mskContaCredito.Text, ",", "."), Replace(mskDigitavel.Text, ",", "."))) & "' , "
                    lstrsql = lstrsql & " " & icodItem & " ) "
                    objExecAtualizaBancoDados.QUERY_NAME = lstrsql
                        repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

                        Call SetaCodigo("", Trim(txtCodigo.Text))

                        ' MsgBox("Atualizado", vbInformation + vbOKOnly, "Aviso")

                    End If
                End If

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

    End Sub
    Private Sub cmdLocCod_Click(sender As Object, e As EventArgs) Handles cmdLocCod.Click

        Try

            vresultDebito = False
            vresultCredito = False
            vresultCodigo = True
            frmPesquisaSimples.SetParametros("CODIGOS", "Codigos", Me, String.Empty, "", "num ASC", String.Empty)
            frmPesquisaSimples.Show()

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

    End Sub

    'Private Sub txtcodcred_KeyDown(sender As Object, e As KeyEventArgs)

    '    Try

    '        If IsNumeric(txtcodcred.Text) And e.KeyCode = Keys.Enter And Trim(txtcodcred.Text) <> "" Then
    '            LocalizarContaCred(txtcodcred.Text)
    '        ElseIf Trim(txtcodcred.Text) = "" Then
    '            mskContaCredito.Text = String.Empty
    '        ElseIf e.KeyCode = Keys.F3 Then
    '            vresultDebito = False
    '            vresultCredito = True
    '            frmPesquisaSimples.SetParametros("PLANO_CONTAS", "Plano Conta", Me, String.Empty, "GRAU = 7 AND TIPO = 'A'", "ID DESC", String.Empty)
    '            frmPesquisaSimples.Show()
    '        End If

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

    'End Sub

    Private Sub txtCodigo_KeyDown(sender As Object, e As KeyEventArgs) Handles txtCodigo.KeyDown
        Try

            'If IsNumeric(txtCodigo.Text) And e.KeyCode = Keys.Enter And Trim(txtCodigo.Text) <> "" Then
            If e.KeyCode = Keys.Enter And Trim(txtCodigo.Text) <> "" Then
                LocalizarCodigos(txtCodigo.Text)
            ElseIf e.KeyCode = Keys.F3 Then
                ' vresultDebito = False
                ' vresultCredito = True
                frmPesquisaSimples.SetParametros("CODIGOS", "Códigos", Me, String.Empty, "", "ID DESC", String.Empty)
                frmPesquisaSimples.Show()
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtCodigo_KeyDown" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub
    'Private Sub txtcodcred_KeyPress(sender As Object, e As KeyPressEventArgs)
    '    Try
    '        If e.KeyChar = Chr(Keys.Enter) Then
    '            e.KeyChar = Nothing
    '            'cmbtipo.Focus()
    '            SendKeys.Send("{TAB}")
    '        Else
    '            SomenteInteger(e.KeyChar, txtcodcred, False)
    '        End If
    '    Catch ex As Exception
    '        MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtcodcred_KeyPress" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
    '        Exit Sub
    '    End Try
    'End Sub
    'Public Sub LocalizarContaCred(ByVal id As Double)

    '    Dim lstrcredito As String

    '    Dim vlist

    '    Dim objPlanoContas As New entidadePlanoContas
    '    Dim repositorioPlanoContas As New repositorioPlanoContas

    '    Try

    '        objPlanoContas.CONTA = String.Empty
    '        objPlanoContas.ID = id
    '        objPlanoContas.ATIVO = 1

    '        vlist = repositorioPlanoContas.Select(objPlanoContas)

    '        If vlist.Count > 0 Then
    '            For Each objPlanoContasFor As entidadePlanoContas In vlist

    '                txtcodcred.Text = id
    '                lstrcredito = Trim(IIf(IsDBNull(objPlanoContasFor.CONTA), String.Empty, objPlanoContasFor.CONTA))
    '                lstrcredito = Replace(lstrcredito, ".", "")
    '                mskContaCredito.Text = lstrcredito

    '            Next
    '        Else
    '            mskContaCredito.Text = ""
    '        End If

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

    'End Sub

    Private Sub cmdLocCred_Click(sender As Object, e As EventArgs)
        Try

            vresultDebito = False
            vresultCredito = True
            frmPesquisaSimples.SetParametros("PLANO_CONTAS", "Plano Conta", Me, String.Empty, "GRAU = 7 AND TIPO = 'A'", "ID DESC", String.Empty)
            frmPesquisaSimples.Show()

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

    End Sub


    Public Sub LocalizarCodigos(ByVal id As String)

        Dim lstrcredito As String

        Dim vlist

        Dim objCodigos As New entidadeCodigo
        Dim repositorioCodigos As New repositorioCodigo

        Try

            objCodigos.NUM = String.Empty
            objCodigos.NUM = id
            'objCodigos.ATIVO = 1

            vlist = repositorioCodigos.Select_1(objCodigos)

            If vlist.Count > 0 Then
                For Each objCodigos_ As entidadeCodigo In vlist
                    SetaCodigo("", Trim(txtCodigo.Text))
                Next
            Else
                txtCodigo.Text = ""
                txtdesricao.Text = ""
                MessageBox.Show("Código não cadastrado !", "Aviso do Sistema", MessageBoxButtons.OK)
            End If

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

    End Sub

    Private Sub opbClasse_Click(sender As Object, e As EventArgs) Handles opbClasse.Click
        If opbClasse.Checked Then
            cmbClasse.Enabled = True
            cmbMunicipio.SelectedIndex = -1
            mskContaCredito.Text = ""
            cmbMunicipio.Enabled = False
            mskDigitavel.Enabled = False
            chkAtrelarClasse.Visible = False
            cmbClasseDigitavel.Visible = False
        End If
    End Sub

    Private Sub opbMunic_Click(sender As Object, e As EventArgs) Handles opbMunic.Click
        If opbMunic.Checked Then
            cmbMunicipio.Enabled = True
            cmbClasse.SelectedIndex = -1
            mskContaDebito.Text = ""
            cmbClasse.Enabled = False
            mskDigitavel.Enabled = False
            chkAtrelarClasse.Visible = False
            cmbClasseDigitavel.Visible = False
        End If
    End Sub

    Private Sub cmbClasse_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbClasse.SelectedIndexChanged
        If cmbClasse.SelectedIndex > -1 Then

            sRetornaConta()

        End If
    End Sub
    Private Sub cmdExcluir_Click(sender As Object, e As EventArgs) Handles cmdExcluir.Click

        Dim lstrsql As String

        If mfgDados.Rows > 1 Then

            lstrsql = "DELETE FROM codigo_conta WHERE id = " & mfgDados.get_TextMatrix(mfgDados.RowSel, 4)
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            Call SetaCodigo("", Trim(txtCodigo.Text))

        End If

    End Sub

    Private Sub opbDigitavel_Click(sender As Object, e As EventArgs) Handles opbDigitavel.Click
        If opbDigitavel.Checked Then
            mskDigitavel.Enabled = True
            chkAtrelarClasse.Visible = True
            ' cmbClasseDigitavel.Visible = True
        End If
    End Sub

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

        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

        Try

            If KeyAscii <> 8 And (KeyAscii < 48 Or KeyAscii > 57) 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 txtCodigo_Leave(sender As Object, e As EventArgs) Handles txtCodigo.Leave
        Try

            With Me.txtCodigo
                ' .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 chkAtrelarClasse_CheckedChanged(sender As Object, e As EventArgs) Handles chkAtrelarClasse.CheckedChanged
        If chkAtrelarClasse.Checked Then
            cmbClasseDigitavel.Visible = True
        Else
            cmbClasseDigitavel.Visible = False
        End If
    End Sub

    'Private Sub cmdExcluir_Click(sender As Object, e As EventArgs) Handles cmdExcluir.Click
    '    Dim lstrsql As String
    '    Try
    '        lstrsql = "delete from codigo_conta_classe where codclasse =  " & mfgDados.get_TextMatrix(mfgDados.RowSel, 4) & " and numcod =  " & mfgDados.get_TextMatrix(mfgDados.RowSel, 0)
    '        objExecAtualizaBancoDados.QUERY_NAME = lstrsql
    '        repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

    '        ' sCarregaGrid(VB6.GetItemData(Me.cmbClasse, Me.cmbClasse.SelectedIndex))

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

    'End Sub

    Private Sub cmbMunicipio_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbMunicipio.SelectedIndexChanged
        If cmbMunicipio.SelectedIndex > -1 Then

            sRetornaConta()
            'MontaGrid()
            'sCarregaGrid(VB6.GetItemData(Me.cmbClasse, Me.cmbClasse.SelectedIndex))
        End If
    End Sub

    'Private Sub sLimpa()
    '    Try
    '        cmbClasse.SelectedIndex = -1
    '        txtcodcred.Text = ""
    '        txtcoddeb.Text = ""
    '        mskContaCredito.Text = String.Empty
    '        mskContaDebito.Text = String.Empty
    '        txtCodigo.Text = ""

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

    'End Sub
    Private Function fValida(ByVal sClasse As String, ByVal sCodigo As String) As Boolean

        Dim lstrsql As String
        Try
            lstrsql = "SELECT * FROM codigo_conta_classe where codclasse = " & sClasse & " and numcod = " & sCodigo & " order by numcod "
            objAtualizaBancoDados.QUERY_NAME = lstrsql
            dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            If dt.Rows.Count > 0 Then
                MessageBox.Show("Já existe conta cadastrada para esse código e classe", "Aviso de Sistema", MessageBoxButtons.OK)
                Return True
            Else
                Return False
            End If

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

    'Private Sub sCarregaGrid(Optional ByVal sClasse As Integer = 0)

    '    Dim lstrsql As String
    '    Try
    '        If sClasse > 0 Then
    '            lstrsql = "SELECT * FROM codigo_conta_classe where codclasse = " & sClasse & " order by numcod "
    '        Else
    '            lstrsql = "SELECT * FROM codigo_conta_classe order by descrclasse, numcod "
    '        End If
    '        objAtualizaBancoDados.QUERY_NAME = lstrsql
    '        dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

    '        If dt.Rows.Count > 0 Then
    '            For Each row As DataRow In dt.Rows
    '                With Me.mfgDados
    '                    .Rows = .Rows + 1
    '                    .set_TextMatrix(.Rows - 1, 0, Trim(IIf(IsDBNull(row.Item("numcod")), "", row.Item("numcod"))))
    '                    .set_TextMatrix(.Rows - 1, 1, Trim(IIf(IsDBNull(row.Item("descrclasse")), "", row.Item("descrclasse"))))
    '                    .set_TextMatrix(.Rows - 1, 2, Trim(IIf(IsDBNull(row.Item("conta_debito")), "", row.Item("conta_debito"))))
    '                    .set_TextMatrix(.Rows - 1, 3, Trim(IIf(IsDBNull(row.Item("conta_credito")), "", row.Item("conta_credito"))))
    '                    .set_TextMatrix(.Rows - 1, 4, Trim(IIf(IsDBNull(row.Item("codclasse")), "", row.Item("codclasse"))))
    '                End With
    '            Next
    '        End If
    '    Catch ex As Exception
    '        MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: sCarregaGrid" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
    '        Exit Sub
    '    End Try
    'End Sub

    'Private Sub MontaGrid()
    '    Dim lintAux As Short
    '    Try
    '        With Me.mfgDados
    '            .Rows = 1
    '            .Clear()
    '            .set_ColWidth(0, 1500)
    '            .set_ColWidth(1, 1500)
    '            .set_ColWidth(2, 2000)
    '            .set_ColWidth(3, 2000)
    '            .set_ColWidth(4, 0)
    '            .set_TextMatrix(0, 0, "Código")
    '            .set_TextMatrix(0, 1, "Classe")
    '            .set_TextMatrix(0, 2, "Conta Contábil Crédito")
    '            .set_TextMatrix(0, 3, "Conta Contábil Débito")
    '            .set_TextMatrix(0, 4, "codclasse")
    '        End With
    '    Catch ex As Exception
    '        MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: MontaGrid" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
    '        Exit Sub
    '    End Try
    'End Sub

    'Private Function fConsiste() As Boolean
    '    Try
    '        If txtCodigo.Text = "" Then
    '            MessageBox.Show("favor informar o código", "Aviso de Sistema", MessageBoxButtons.OK)
    '            txtCodigo.Focus()
    '            Return True
    '        End If

    '        If txtcoddeb.Text = "" Then
    '            MessageBox.Show("Já informar a Conta de Débito", "Aviso de Sistema", MessageBoxButtons.OK)
    '            txtcoddeb.Focus()
    '            Return True
    '        End If

    '        If txtcodcred.Text = "" Then
    '            MessageBox.Show("Favor informar a Conta de Crédito", "Aviso de Sistema", MessageBoxButtons.OK)
    '            txtcodcred.Focus()
    '            Return True
    '        End If

    '        If cmbClasse.SelectedIndex = -1 Then
    '            MessageBox.Show("Favor informar a Classe", "Aviso de Sistema", MessageBoxButtons.OK)
    '            cmbClasse.Focus()
    '            Return True
    '        End If

    '        Return False

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

    'End Function

    Private Sub sRetornaConta()
        Dim lstrsql As String
        Try

            If opbClasse.Checked Then

                If chkParcelamento.Checked Then
                    lstrsql = "SELECT conta_faturamento_parc as conta FROM classe WHERE codigo = " & VB6.GetItemData(Me.cmbClasse, Me.cmbClasse.SelectedIndex)
                Else
                    lstrsql = "SELECT conta_faturamento as conta FROM classe WHERE codigo = " & VB6.GetItemData(Me.cmbClasse, Me.cmbClasse.SelectedIndex)
                End If

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

                    If dt.Rows.Count > 0 Then
                        For Each row As DataRow In dt.Rows
                        mskContaDebito.Text = Trim(IIf(IsDBNull(row.Item("conta")), "", row.Item("conta")))
                    Next
                    End If

                Else

                    lstrsql = "SELECT conta_faturamento FROM munic WHERE cod = " & VB6.GetItemData(Me.cmbMunicipio, Me.cmbMunicipio.SelectedIndex)
                objAtualizaBancoDados.QUERY_NAME = lstrsql
                dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

                If dt.Rows.Count > 0 Then
                    For Each row As DataRow In dt.Rows
                        mskContaCredito.Text = Trim(IIf(IsDBNull(row.Item("conta_faturamento")), "", row.Item("conta_faturamento")))
                    Next
                End If

            End If


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

    Private Sub MontaGrade()

        Try

            With Me.mfgDados
                Call .Clear()
                .Rows = 1

                .set_ColAlignment(0, MSFlexGridLib.AlignmentSettings.flexAlignLeftCenter)
                .set_ColAlignment(1, MSFlexGridLib.AlignmentSettings.flexAlignLeftCenter)
                .set_ColAlignment(2, MSFlexGridLib.AlignmentSettings.flexAlignLeftCenter)
                .set_ColAlignment(3, MSFlexGridLib.AlignmentSettings.flexAlignLeftCenter)
                .set_ColAlignment(4, MSFlexGridLib.AlignmentSettings.flexAlignLeftCenter)
                .set_ColWidth(0, 1200)
                .set_ColWidth(1, 3000)
                .set_ColWidth(2, 3000)
                .set_ColWidth(3, 2200)
                .set_ColWidth(4, 0)
                .set_TextMatrix(0, 0, "Código")
                .set_TextMatrix(0, 1, "Tipo")
                .set_TextMatrix(0, 2, "Descrição")
                .set_TextMatrix(0, 3, "Conta")
                .set_TextMatrix(0, 4, "id")

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

    End Sub
End Class