Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Imports fatd_faturamento.ENTIDADE
Imports fatd_faturamento.BLL.Repositorio


Friend Class frmExportaMaioresDevedores
	Inherits System.Windows.Forms.Form

    'Private ifrmBarraProgresso As frmBarraProgresso

    Private Sub cmdArquivo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdArquivo.Click
        Try
            Me.dialArqOpen.DefaultExt = "CSV"
            Me.dialArqOpen.Filter = "|*." & Me.dialArqOpen.DefaultExt
            Me.dialArqOpen.FileName = My.Application.Info.DirectoryPath & "\Listagem_" & VB6.Format(BuscaDataBD, "yyyymmddHHMMSS") & "." & Me.dialArqOpen.DefaultExt
            Me.dialArqOpen.Title = "Arquivo de exportação maiores devedores"
            Me.dialArqOpen.ShowDialog()
            Me.txtArquivo.Text = Me.dialArqOpen.FileName
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdArquivo_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub
    Private Sub cmdExporta_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExporta.Click
        Dim lstrLin As String
        Dim lintARQ As Short
        Dim llngRow As Integer
        Dim llngCol As Integer
        Call LigDesCampos(False)

        Try

            If Trim(Me.txtArquivo.Text) <> "" Then
                lintARQ = FreeFile()
                FileOpen(lintARQ, Me.txtArquivo.Text, OpenMode.Output)
                With Me.mfgDados
                    For llngRow = 0 To .Rows - 1
                        If llngRow = 0 Or (llngRow >= CInt(Me.txtSeqIni.Text) And llngRow <= CInt(Me.txtSeqFim.Text)) Then
                            lstrLin = ""
                            For llngCol = 0 To .Cols - 1
                                If .get_ColWidth(llngCol) <> 0 Then
                                    If llngCol = 4 Then
                                        lstrLin = lstrLin & """" & .get_TextMatrix(llngRow, llngCol) & """;"
                                    Else
                                        lstrLin = lstrLin & .get_TextMatrix(llngRow, llngCol) & ";"
                                    End If
                                End If
                            Next llngCol
                            PrintLine(lintARQ, lstrLin)
                        End If
                    Next llngRow
                End With
                FileClose(lintARQ)
                Call MsgBox("Arquivo gerado com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
            Else
                Call MsgBox("Você precisa selecionar o caminho e o arquivo para exportação!", MsgBoxStyle.Information, gstrTituloMensagem)
            End If
            Call LigDesCampos(True)
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdExporta_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub
    Private Sub MontaGrade()
        Try
            With Me.mfgDados
                .Rows = 1
                .Cols = 15
                .Clear()
                .Redraw = False
                .set_ColWidth(0, 700)
                .set_TextMatrix(0, 0, "Seq")
                .set_ColWidth(1, 500)
                .set_TextMatrix(0, 1, "Zona")
                .set_ColWidth(2, 500)
                .set_TextMatrix(0, 2, "Livro")
                .set_ColWidth(3, 700)
                .set_TextMatrix(0, 3, "Conta")
                .set_ColWidth(4, 2000)
                .set_TextMatrix(0, 4, "Faturas")
                .set_ColWidth(5, 3500)
                .set_TextMatrix(0, 5, "Nome")
                .set_ColWidth(6, 1750)
                .set_TextMatrix(0, 6, "Logradouro")
                .set_ColWidth(7, 1500)
                .set_TextMatrix(0, 7, "Bairro")
                .set_ColWidth(8, 1200)
                .set_TextMatrix(0, 8, "Cidade")
                .set_ColWidth(9, 300)
                .set_TextMatrix(0, 9, "UF")
                .set_ColWidth(10, 950)
                .set_TextMatrix(0, 10, "CEP")
                .set_ColWidth(11, 1200)
                .set_TextMatrix(0, 11, "Valor")
                .set_ColWidth(12, 1200)
                .set_TextMatrix(0, 12, "Classe")
                .set_ColWidth(13, 1200)
                .set_TextMatrix(0, 13, "Grupo")
                .set_ColWidth(14, 0)
                .set_TextMatrix(0, 14, "Ordena")
                .Redraw = True
            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
    Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
        Call Me.Close()
    End Sub

    Private Sub cmdProcessar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdProcessar.Click
        Dim llngRow As Integer
        Dim llngPos As Integer
        Dim lblnAux As Boolean
        Dim lstrZona As String
        Dim lstrLivro As String
        Dim lstrConta As String
        Dim ldblNumNF As Double
        Dim lstrMesAno As String
        Dim ldblValor As Double
        Dim ldteVenc As Date
        Dim lstrNomeUc As String
        Dim lstrEndUc As String
        Dim lstrNumeroUc As String
        Dim lstrComplementUC As String
        Dim lstrQuadraUC As String
        Dim lstrLoteUc As String
        Dim lstrBairroUc As String
        Dim lstrCidadeUc As String
        Dim lstrUfUc As String
        Dim lstrCepUc As String
        Dim lstrClasse As String
        Dim lstrGrupo As String
        Dim lstrSql As String
        Dim lstrFilClas As String
        Dim lstrFilGru As String
        Dim lintConta As Short
        Dim objAtualizaBancoDados As New entidadeAtualizaBancoDados
        Dim repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados
        Dim dt As DataTable

        Try

            Call LigDesCampos(False)
            Call MontaGrade()
            Me.mfgDados.Redraw = False

            lstrFilClas = ""
            With Me.lvsClasses

                If .Items.Item(1).Checked = False Then
                    lstrFilClas = " AND ("
                    For lintConta = 2 To .Items.Count - 1
                        If .Items.Item(lintConta).Checked = True Then
                            lstrFilClas = lstrFilClas & " LEFT(`nf`.`codclas`," & Len(Trim(Str_Retira(.Items.Item(lintConta).SubItems(1).Text, "<="))) & ")" & .Items.Item(lintConta).SubItems(1).Text & " OR "
                        End If
                    Next lintConta
                    If lstrFilClas = " (" Then
                        lstrFilClas = ""
                    End If
                    lstrFilClas = VB.Left(lstrFilClas, Len(lstrFilClas) - 3) & ") "
                Else
                    lstrFilClas = ""
                End If
            End With
            lstrFilGru = ""
            With Me.lvsGrupoTar

                If .Items.Item(1).Checked = False Then
                    lstrFilGru = " AND MID(`nf`.`ta`, 3, 2) IN("
                    For lintConta = 2 To .Items.Count - 1
                        lstrFilGru = lstrFilGru & .Items.Item(lintConta).SubItems(1).Text & ","
                    Next lintConta
                    lstrFilGru = VB.Left(lstrFilGru, Len(lstrFilGru) - 1) & ")"
                End If
            End With

            lstrSql = ""
            lstrSql = lstrSql & "SELECT nf.zona, nf.livro, nf.conta, nf.numnf, nf.mesano, if(nf.valorliq>0,nf.valorliq,nf.valortot) AS valortotal, nf.datavenc, nf.codclas, nf.ta, nf.grupo, " & vbCrLf
            lstrSql = lstrSql & "       IFNULL(uc_faturamento.`nome`,consum.nome) AS nomeuc," & vbCrLf
            lstrSql = lstrSql & "       IFNULL(uc_faturamento.`end`,consum.end) AS enduc," & vbCrLf
            lstrSql = lstrSql & "       IFNULL(uc_faturamento.`numero`,consum.numero) AS numerouc," & vbCrLf
            lstrSql = lstrSql & "       IFNULL(uc_faturamento.`complement`,consum.complement) AS complementuc," & vbCrLf
            lstrSql = lstrSql & "       IFNULL(uc_faturamento.`quadra`,consum.quadra) AS quadrauc," & vbCrLf
            lstrSql = lstrSql & "       IFNULL(uc_faturamento.lote,consum.lote) AS loteuc," & vbCrLf
            lstrSql = lstrSql & "       IFNULL(uc_faturamento.bairro,consum.bairro) AS bairrouc," & vbCrLf
            lstrSql = lstrSql & "       IFNULL(uc_faturamento.cidade,consum.cidade) AS cidadeuc," & vbCrLf
            lstrSql = lstrSql & "       IFNULL(uc_faturamento.uf,consum.uf) AS ufuc," & vbCrLf
            lstrSql = lstrSql & "       IFNULL(uc_faturamento.`cep`,consum.cep) AS cepuc  " & vbCrLf
            lstrSql = lstrSql & "  FROM nf " & vbCrLf
            lstrSql = lstrSql & "  LEFT JOIN uc_faturamento ON(uc_faturamento.id = nf.unid_id AND uc_faturamento.cons_id = nf.cons_id AND nf.grupo = MID(uc_faturamento.ta,3,1))" & vbCrLf
            lstrSql = lstrSql & "  LEFT JOIN consum ON(consum.consreg = nf.cons_id)" & vbCrLf
            lstrSql = lstrSql & " WHERE nf.mesano_ref <= " & VB6.Format(Me.dtpBase.Value, "yyyymm") & vbCrLf
            lstrSql = lstrSql & "   AND (nf.flgst <> 'P' OR (nf.flgst = 'P' AND nf.datapag > '" & VB6.Format(Me.dtpBase.Value, "yyyy-MM-dd") & "'))" & vbCrLf
            lstrSql = lstrSql & "   AND (nf.flgst <> 'C' OR (nf.flgst = 'C' AND nf.datast > '" & VB6.Format(Me.dtpBase.Value, "yyyy-MM-dd") & "'))" & vbCrLf
            lstrSql = lstrSql & "   AND (IFNULL(uc_faturamento.`situacao`,'I') <> 'C' AND IFNULL(uc_faturamento.`data_corte`,'" & VB6.Format(Me.dtpBase.Value, "yyyy-MM-dd") & "') > '" & VB6.Format(Me.dtpBase.Value, "yyyy-MM-dd") & "') "
            lstrSql = lstrSql & "   AND (IFNULL(uc_faturamento.`situacao`,'I') <> 'D' AND IFNULL(uc_faturamento.`data_desligamento`,'" & VB6.Format(Me.dtpBase.Value, "yyyy-MM-dd") & "') > '" & VB6.Format(Me.dtpBase.Value, "yyyy-MM-dd") & "')  "
            lstrSql = lstrSql & "   AND (IFNULL(uc_faturamento.`situacao`,'I') <> 'I')"
            lstrSql = lstrSql & "   AND nf.datavenc < '" & VB6.Format(Me.dtpBase.Value, "yyyy-MM-dd") & "' " & vbCrLf
            lstrSql = lstrSql & lstrFilClas & vbCrLf
            lstrSql = lstrSql & lstrFilGru & vbCrLf
            lstrSql = lstrSql & " ORDER BY nf.zona ASC, nf.livro ASC, nf.conta ASC, valortotal DESC"
            objAtualizaBancoDados.QUERY_NAME = lstrSql
            dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            If dt.Rows.Count > 0 Then
                Call BarraProgresso((Me.ProgressBarDevedores), (Me.lblPercDevedores), (Me.lblInfoDevedores), 0, dt.Rows.Count, " Maiores Devedores ")
                For Each row As DataRow In dt.Rows

                    lstrZona = Trim(IIf(IsDBNull(row.Item("zona")), "", row.Item("zona")))
                    lstrLivro = Trim(IIf(IsDBNull(row.Item("livro")), "", row.Item("livro")))
                    lstrConta = Trim(IIf(IsDBNull(row.Item("conta")), "", row.Item("conta")))
                    ldblNumNF = CDbl(IIf(IsDBNull(row.Item("numnf")), 0, row.Item("numnf")))
                    lstrMesAno = Trim(IIf(IsDBNull(row.Item("mesano")), "", row.Item("mesano")))
                    ldblValor = CDbl(IIf(IsDBNull(row.Item("valortotal")), 0, row.Item("valortotal")))
                    ldteVenc = CDate(IIf(IsDBNull(row.Item("datavenc")), "", row.Item("datavenc")))
                    lstrNomeUc = Trim(IIf(IsDBNull(row.Item("nomeuc")), "", row.Item("nomeuc")))
                    lstrEndUc = Trim(IIf(IsDBNull(row.Item("enduc")), "", row.Item("enduc")))
                    lstrNumeroUc = Trim(IIf(IsDBNull(row.Item("numerouc")), "", row.Item("numerouc")))
                    lstrComplementUC = Trim(IIf(IsDBNull(row.Item("complementuc")), "", row.Item("complementuc")))
                    lstrQuadraUC = Trim(IIf(IsDBNull(row.Item("quadrauc")), "", row.Item("quadrauc")))
                    lstrLoteUc = Trim(IIf(IsDBNull(row.Item("loteuc")), "", row.Item("loteuc")))
                    lstrBairroUc = Trim(IIf(IsDBNull(row.Item("bairrouc")), "", row.Item("bairrouc")))
                    lstrCidadeUc = Trim(IIf(IsDBNull(row.Item("cidadeuc")), "", row.Item("cidadeuc")))
                    lstrUfUc = Trim(IIf(IsDBNull(row.Item("ufuc")), "", row.Item("ufuc")))
                    lstrCepUc = Trim(IIf(IsDBNull(row.Item("cepuc")), "", row.Item("cepuc")))
                    If Len(lstrCepUc) > 0 Then lstrCepUc = VB.Left(lstrCepUc, 2) & "." & Mid(lstrCepUc, 3, 3) & "-" & VB.Right(lstrCepUc, 3)
                    lstrClasse = Trim(IIf(IsDBNull(row.Item("codclas")), "", row.Item("codclas")))
                    lstrGrupo = Mid(Trim(IIf(IsDBNull(row.Item("ta")), "01B1", row.Item("ta"))), 3, 2)

                    If VB.Left(lstrClasse, 1) = "9" Then
                        lstrClasse = "Residencial"
                    ElseIf CDbl(VB.Left(lstrClasse, 1)) + 0 < 4 Then
                        lstrClasse = "Industrial"
                    ElseIf VB.Left(lstrClasse, 1) = "4" Then
                        lstrClasse = "Comercial"
                    ElseIf VB.Left(lstrClasse, 1) = "5" Then
                        lstrClasse = "Rural"
                    ElseIf VB.Left(lstrClasse, 2) = "61" Then
                        lstrClasse = "Poder Público Municipal"
                    ElseIf VB.Left(lstrClasse, 2) = "62" Then
                        lstrClasse = "Poder Público Estadual"
                    ElseIf VB.Left(lstrClasse, 2) = "63" Then
                        lstrClasse = "Poder Público Federal"
                    ElseIf VB.Left(lstrClasse, 2) = "64" Then
                        lstrClasse = "Iluminação Pública"
                    ElseIf VB.Left(lstrClasse, 1) = "7" Then
                        lstrClasse = "Serviço Púplico"
                    ElseIf VB.Left(lstrClasse, 1) = "8" Then
                        lstrClasse = "Consumo Próprio"
                    Else
                        lstrClasse = "Indefinido"
                    End If

                    If lstrGrupo = "A4" Then
                        lstrGrupo = "Convencional"
                    ElseIf lstrGrupo = "AS" Then
                        lstrGrupo = "Convencional Suprimento"
                    ElseIf lstrGrupo = "AA" Then
                        lstrGrupo = "Horo Sazonal Azul"
                    ElseIf lstrGrupo = "AV" Then
                        lstrGrupo = "Horo Sazonal Verde"
                    ElseIf lstrGrupo = "AL" Then
                        lstrGrupo = "Consumidor Livre"
                    ElseIf lstrGrupo = "AP" Then
                        lstrGrupo = "Auto Produtor"
                    ElseIf lstrGrupo = "B1" Then
                        lstrGrupo = "Residencial e Baixa Renda"
                    ElseIf lstrGrupo = "B2" Then
                        lstrGrupo = "Rural"
                    ElseIf lstrGrupo = "B3" Then
                        lstrGrupo = "Demais Classes"
                    ElseIf lstrGrupo = "B4" Then
                        lstrGrupo = "Iluminação Pública"
                    Else
                        lstrGrupo = "Indefinido"
                    End If

                    With Me.mfgDados
                        lblnAux = False
                        For llngRow = 0 To .Rows - 1
                            If .get_TextMatrix(llngRow, 1) = lstrZona And .get_TextMatrix(llngRow, 2) = lstrLivro And .get_TextMatrix(llngRow, 3) = lstrConta Then
                                llngPos = llngRow
                                llngRow = .Rows - 1
                                lblnAux = True
                            End If
                        Next llngRow
                        If lblnAux = False Then
                            .Rows = .Rows + 1
                            llngPos = (.Rows - 1)
                            .set_TextMatrix(llngPos, 1, lstrZona)
                            .set_TextMatrix(llngPos, 2, lstrLivro)
                            .set_TextMatrix(llngPos, 3, lstrConta)
                            .set_TextMatrix(llngPos, 4, "Nº Nota Fiscal: " & VB6.Format(ldblNumNF, "000000000") & " de " & lstrMesAno & " - Vencimento: " & VB6.Format(ldteVenc, "dd/mm/yyyy") & " - R$ " & FormatNumber(ldblValor, 2))
                            .set_TextMatrix(llngPos, 5, lstrNomeUc)
                            .set_TextMatrix(llngPos, 6, lstrEndUc & IIf(Len(lstrNumeroUc) > 0, "," & lstrNumeroUc, "") & IIf(Len(lstrComplementUC) > 0, " - " & lstrComplementUC, "") & IIf(Len(lstrQuadraUC) > 0, " Q: " & lstrQuadraUC, "") & IIf(Len(lstrLoteUc) > 0, " L: " & lstrLoteUc, ""))
                            .set_TextMatrix(llngPos, 7, lstrBairroUc)
                            .set_TextMatrix(llngPos, 8, lstrCidadeUc)
                            .set_TextMatrix(llngPos, 9, lstrUfUc)
                            .set_TextMatrix(llngPos, 10, lstrCepUc)
                            .set_TextMatrix(llngPos, 12, lstrClasse)
                            .set_TextMatrix(llngPos, 13, lstrGrupo)
                        Else
                            .set_TextMatrix(llngPos, 4, .get_TextMatrix(llngPos, 4) & vbCrLf & "Nº Nota Fiscal: " & VB6.Format(ldblNumNF, "000000000") & " de " & lstrMesAno & " - Vencimento: " & VB6.Format(ldteVenc, "dd/mm/yyyy") & " - R$ " & FormatNumber(ldblValor, 2))
                        End If
                        .set_TextMatrix(llngPos, 11, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 11) = "", 0, .get_TextMatrix(llngPos, 11))) + ldblValor, 2))
                        .set_TextMatrix(llngPos, 14, CDbl(.get_TextMatrix(llngPos, 11)) * 100)
                    End With

                    Call BarraProgresso((Me.ProgressBarDevedores), (Me.lblPercDevedores), (Me.lblInfoDevedores), 1, dt.Rows.Count, " Maiores Devedores ")
                Next
                'End If


                Me.mfgDados.Redraw = True
                Call LigDesCampos(True)
                Me.mfgDados.Redraw = True
                Call LigDesCampos(True)
                Me.mfgDados.Redraw = True
                Call LigDesCampos(True)

                'Ordena
                With Me.mfgDados
                    .Redraw = False
                    .Col = 14
                    .Sort = MSFlexGridLib.SortSettings.flexSortNumericDescending
                    .Redraw = True
                    'Sequencia
                    For llngRow = 1 To .Rows - 1
                        .set_TextMatrix(llngRow, 0, llngRow)
                    Next llngRow
                    Me.txtSeqIni.Text = .get_TextMatrix(1, 0)
                    Me.txtSeqFim.Text = .get_TextMatrix(.Rows - 1, 0)
                End With
            Else
                MessageBox.Show("Não foram encontrados Registros !", "Aviso do Sistema", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Call LigDesCampos(True)
            End If
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdProcessar_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub frmExortaMaioresDevedores_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        Try
            Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
            If KeyAscii = System.Windows.Forms.Keys.Escape Then
                Call cmdFechar_Click(cmdFechar, New System.EventArgs())
            ElseIf 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 o Erro: " & Err.Description & " - Rotina: frmExortaMaioresDevedores_KeyPress" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

    Private Sub frmExortaMaioresDevedores_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        Try
            'Call CenterForm(Me)
            Me.dtpBase.Value = Date.Now
            Call MontaGrade()
            Call CarregaClasses((Me.lvsClasses))
            Call CarregaGrupoTar((Me.lvsGrupoTar))
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmExortaMaioresDevedores_Load" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

    Private Sub txtSeqFim_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtSeqFim.Enter
        Try
            With Me.txtSeqFim
                .SelectionStart = 0
                .SelectionLength = Len(.Text)
            End With
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtSeqFim_Enter" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

    Private Sub txtSeqIni_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtSeqIni.Enter
        Try
            With Me.txtSeqIni
                .SelectionStart = 0
                .SelectionLength = Len(.Text)
            End With
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtSeqIni_Enter" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

    Private Sub txtSeqFim_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtSeqFim.Leave
        Try
            With Me.txtSeqFim
                If CInt(.Text) > CDbl(Me.mfgDados.get_TextMatrix(Me.mfgDados.Rows - 1, 0)) Then
                    .Text = Me.mfgDados.get_TextMatrix(Me.mfgDados.Rows - 1, 0)
                End If
            End With
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtSeqFim_Leave" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub
    Private Sub txtSeqIni_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtSeqIni.KeyPress
        Try
            Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

            If KeyAscii <> System.Windows.Forms.Keys.Back Then
                If KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9 Then
                    KeyAscii = 0
                End If
            End If
            eventArgs.KeyChar = Chr(KeyAscii)
            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtSeqIni_KeyPress" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

    Private Sub txtSeqFim_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtSeqFim.KeyPress
        Try
            Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
            If KeyAscii <> System.Windows.Forms.Keys.Back Then
                If KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9 Then
                    KeyAscii = 0
                End If
            End If
            eventArgs.KeyChar = Chr(KeyAscii)
            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtSeqFim_KeyPress" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub
    Private Sub CarregaClasses(ByRef alsvAuxiliar As System.Windows.Forms.ListView)

        Dim lsiItens As System.Windows.Forms.ListViewItem
        Dim clhColunas As System.Windows.Forms.ColumnHeader
        Dim strNome As String
        Dim strZona As String
        Dim lngTamanho As Integer

        Try

            ' ----- Limpa a List que carrega as cidades
            alsvAuxiliar.Items.Clear()

            If alsvAuxiliar.Columns.Count <= 1 Then
                clhColunas = alsvAuxiliar.Columns.Add("", "Classe", CInt(VB6.TwipsToPixelsX(2600)))
                clhColunas = alsvAuxiliar.Columns.Add("", "Condição", CInt(VB6.TwipsToPixelsX(0)))
            End If

            ' lsiItens = alsvAuxiliar.Items.Add("Marcar Todas")

            'If lsiItens.SubItems.Count > alsvAuxiliar.Items.Count Then
            '    lsiItens.SubItems(alsvAuxiliar.Items.Count).Text = " = 0"
            'Else
            '    lsiItens.SubItems.Insert(alsvAuxiliar.Items.Count, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " = 0"))
            'End If

            'alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            'alsvAuxiliar.Items.Item(0).Font = VB6.FontChangeBold(alsvAuxiliar.Items.Item(0).Font, True)
            lsiItens = alsvAuxiliar.Items.Add("Residencial")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = " = 9"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " = 9"))
            End If

            ' alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("Industrial")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = " < 4"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " < 4"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("Comercial")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = " = 4"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " = 4"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("Rural")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = " = 5"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " = 5"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("Poder Público Municipal")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = " = 61"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " = 61"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("Poder Público Estadual")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = " = 62"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " = 62"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("Poder Público Federal")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = " = 63"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " = 63"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("Iluminação Pública")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = " = 64"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " = 64"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("Serviço Púplico")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = " = 7"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " = 7"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("Consumo Próprio")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = " = 8"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " = 8"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True

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

    Private Sub CarregaGrupoTar(ByRef alsvAuxiliar As System.Windows.Forms.ListView)
        Dim lsiItens As System.Windows.Forms.ListViewItem
        Dim clhColunas As System.Windows.Forms.ColumnHeader
        Dim strNome As String
        Dim strZona As String
        Dim lngTamanho As Integer

        Try

            ' ----- Limpa a List que carrega as cidades
            alsvAuxiliar.Items.Clear()

            If alsvAuxiliar.Columns.Count <= 1 Then
                clhColunas = alsvAuxiliar.Columns.Add("", "Grupo", CInt(VB6.TwipsToPixelsX(2600)))
                clhColunas = alsvAuxiliar.Columns.Add("", "Condição", CInt(VB6.TwipsToPixelsX(0)))
            End If

            ' lsiItens = alsvAuxiliar.Items.Add("Marcar Todas")

            'If lsiItens.SubItems.Count > alsvAuxiliar.Items.Count Then
            '    lsiItens.SubItems(alsvAuxiliar.Items.Count).Text = " = 0"
            'Else
            '    lsiItens.SubItems.Insert(alsvAuxiliar.Items.Count, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " = 0"))
            'End If

            ' alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            ' alsvAuxiliar.Items.Item(0).Font = VB6.FontChangeBold(alsvAuxiliar.Items.Item(0).Font, True)
            lsiItens = alsvAuxiliar.Items.Add("A4 - Convencional")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = "'A4'"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "'A4'"))
            End If

            'alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("AS - Convencional Suprimento")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = "'AS'"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "'AS'"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("AA - Horo Sazonal Azul")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = "'AA'"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "'AA'"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("AV - Horo Sazonal Verde")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = "'AV'"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "'AV'"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("AL - Consumidor Livre")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = "'AL'"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "'AL'"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("AP - Auto Produtor")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = "'AP'"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "'AP'"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("B1 - Residencial e Baixa Renda")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = "'B1'"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "'B1'"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("B2 - Rural")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = "'B2'"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "'B2'"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("B3 - Demais Classes")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = "'B3'"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "'B3'"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True
            lsiItens = alsvAuxiliar.Items.Add("B4 - Iluminação Pública")

            If lsiItens.SubItems.Count > 1 Then
                lsiItens.SubItems(1).Text = "'B4'"
            Else
                lsiItens.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "'B4'"))
            End If

            alsvAuxiliar.Items.Item(alsvAuxiliar.Items.Count - 1).Checked = True

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: CarregaGrupoTar" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub
    Private Sub lvsClasses_ItemCheck(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.ItemCheckEventArgs) Handles lvsClasses.ItemCheck
        Try
            Dim aItem As System.Windows.Forms.ListViewItem = lvsClasses.Items(eventArgs.Index)
            Dim intConta As Short

            With Me.lvsClasses
                ' ----- Faz o controle para selecionar todas as cidades ou nenhuma
                If aItem.Text = "Marcar Todas" Then
                    For intConta = 1 To .Items.Count - 1
                        .Items.Item(intConta).Checked = .Items.Item(1).Checked
                    Next intConta
                Else
                    If aItem.Checked = False Then
                        .Items.Item(1).Checked = False
                    End If
                End If
            End With
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: lvsClasses_ItemCheck" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

    Private Sub lvsGrupoTar_ItemCheck(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.ItemCheckEventArgs) Handles lvsGrupoTar.ItemCheck
        Try
            Dim aItem As System.Windows.Forms.ListViewItem = lvsGrupoTar.Items(eventArgs.Index)
            Dim intConta As Short

            With Me.lvsGrupoTar
                ' ----- Faz o controle para selecionar todas as cidades ou nenhuma
                If aItem.Text = "Marcar Todas" Then
                    For intConta = 1 To .Items.Count - 1
                        'UPGRADE_WARNING: Lower bound of collection Me.lvsGrupoTar.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
                        .Items.Item(intConta).Checked = .Items.Item(1).Checked
                    Next intConta
                Else
                    If aItem.Checked = False Then
                        'UPGRADE_WARNING: Lower bound of collection Me.lvsGrupoTar.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
                        .Items.Item(1).Checked = False
                    End If
                End If
            End With
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: lvsGrupoTar_ItemCheck" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

    Private Sub LigDesCampos(ByVal ablnModo As Boolean)
        Try
            'UPGRADE_ISSUE: Form property frmExortaMaioresDevedores.MousePointer does not support custom mousepointers. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="45116EAB-7060-405E-8ABE-9DBB40DC2E86"'
            Me.Cursor = IIf(ablnModo, System.Windows.Forms.Cursors.Arrow, System.Windows.Forms.Cursors.WaitCursor)
            Me.dtpBase.Enabled = ablnModo
            Me.lvsClasses.Enabled = ablnModo
            Me.lvsGrupoTar.Enabled = ablnModo
            Me.cmdProcessar.Enabled = ablnModo
            Me.cmdArquivo.Enabled = ablnModo
            Me.cmdExporta.Enabled = ablnModo
            Me.cmdFechar.Enabled = ablnModo
            'Me.txtSeqIni.Enabled = ablnModo
            'Me.txtSeqFim.Enabled = ablnModo
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: LigDesCampos" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

End Class