Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE
Imports CrystalDecisions.CrystalReports.Engine
Friend Class frmRelAbertos
	Inherits System.Windows.Forms.Form

	Private objAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados

	Private objExecAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioExecAtualizaBancoDados As New repositorioAtualizaBancoDados
	Private dt, dt1, dt2, dt3, dt4 As DataTable
	Private LImagem() As Byte
	Private sNomeEmpresa As String
	Private sEnderecoEmpresa As String
	Private sCepEmpresa As String
	Private sCidadeEmpresa As String
	Private sUFempresa As String
	Private sBairroEmpresa As String
	Private sFoneEmpresa As String
	Private sWebEmpresa As String

	'UPGRADE_WARNING: Event chkCortados.CheckStateChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub chkCortados_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkCortados.CheckStateChanged
		Try
			If Me.chkLigados.CheckState = 0 And Me.chkDesligados.CheckState = 0 And Me.chkCortados.CheckState = 0 And Me.chkDesativados.CheckState = 0 Then
				Me.chkCortados.CheckState = System.Windows.Forms.CheckState.Checked
			End If

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

	'UPGRADE_WARNING: Event chkDesativados.CheckStateChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub chkDesativados_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkDesativados.CheckStateChanged
		Try
			If Me.chkLigados.CheckState = 0 And Me.chkDesligados.CheckState = 0 And Me.chkCortados.CheckState = 0 And Me.chkDesativados.CheckState = 0 Then
				Me.chkDesativados.CheckState = System.Windows.Forms.CheckState.Checked
			End If

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

	'UPGRADE_WARNING: Event chkDesligados.CheckStateChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub chkDesligados_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkDesligados.CheckStateChanged
		Try
			If Me.chkLigados.CheckState = 0 And Me.chkDesligados.CheckState = 0 And Me.chkCortados.CheckState = 0 And Me.chkDesativados.CheckState = 0 Then
				Me.chkDesligados.CheckState = System.Windows.Forms.CheckState.Checked
			End If

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

	'UPGRADE_WARNING: Event chkGrupoA.CheckStateChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub chkGrupoA_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkGrupoA.CheckStateChanged
		Try
			If Me.chkGrupoA.CheckState = 0 Then
				Me.chkGrupoB.CheckState = System.Windows.Forms.CheckState.Checked
			End If

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

	'UPGRADE_WARNING: Event chkGrupoB.CheckStateChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub chkGrupoB_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkGrupoB.CheckStateChanged
		Try
			If Me.chkGrupoB.CheckState = 0 Then
				Me.chkGrupoA.CheckState = System.Windows.Forms.CheckState.Checked
			End If

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

	'UPGRADE_WARNING: Event chkLigados.CheckStateChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub chkLigados_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkLigados.CheckStateChanged
		Try
			If Me.chkLigados.CheckState = 0 And Me.chkDesligados.CheckState = 0 And Me.chkCortados.CheckState = 0 And Me.chkDesativados.CheckState = 0 Then
				Me.chkLigados.CheckState = System.Windows.Forms.CheckState.Checked
			End If

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

	'UPGRADE_WARNING: Event chkSomenteTranTit.CheckStateChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub chkSomenteTranTit_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkSomenteTranTit.CheckStateChanged
		Try
			If Me.chkSomenteTranTit.CheckState = System.Windows.Forms.CheckState.Checked Then
				Me.chkTransfTit.CheckState = System.Windows.Forms.CheckState.Checked
				Me.chkTransfTit.Enabled = False
			Else
				Me.chkTransfTit.CheckState = System.Windows.Forms.CheckState.Unchecked
				Me.chkTransfTit.Enabled = True
			End If

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

	Private Sub cmdEmitir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEmitir.Click
		Dim lblnGravou As Boolean
		Dim lblnContinua As Boolean
		Dim lintConta As Short
		Dim lstrFiltro As String
		Dim lstrFiltro2 As String
		Dim lstrSql As String
		'Dim lrstFaturas As ADODB.Recordset
		'Dim lrstConsulta As ADODB.Recordset
		Dim lstrZona As String
		Dim lstrLivro As String
		Dim lstrConta As String
		Dim lstrRota As String
		Dim lstrCdClasse As String
		Dim lstrNmClasse As String
		Dim lstrFase As String
		Dim ldblNumNf As Double
		Dim lstrMesAno As String
		Dim lstrGrTarifa As String
		Dim ldblValorTot As Double
		Dim ldblValorLiq As Double
		Dim llngConsId As Integer
		Dim llngConsId2 As Integer
		Dim llngUnidId As Integer
		Dim lstrNome As String
		Dim lstrLogradouro As String
		Dim lstrNumero As String
		Dim lstrBairro As String
		Dim lstrQuadra As String
		Dim lstrLote As String
		Dim lstrComplemento As String
		Dim lstrCidade As String
		Dim lstrDistrito As String
		Dim lstrCep As String
		Dim lstrUf As String
		Dim lstrSituacao As String
		Dim lstrMedidor As String
		Dim ldteVencimento As Date
		Dim lstrCriterio As String
		Dim lstrAuxiliar As String
		Dim lstrFlgSit As String
		Try

			Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
			lblnGravou = False
			Me.fmeAux(0).Enabled = False
			Me.cmdEmitir.Enabled = False
			Me.cmdFechar.Enabled = False
			lstrSql = ""
			lstrSql = lstrSql & "DELETE FROM AUX_ABERTOS "
			lstrSql = lstrSql & " WHERE nomemaquina = """ & My.Computer.Name & """ "
			objExecAtualizaBancoDados.QUERY_NAME = lstrSql
			repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

			Me.lblInfo.Text = "Selecionando as faturas, aguarde....!"
			Call Me.lblInfo.Refresh()

			lstrSql = ""
			lstrSql = lstrSql & "SELECT `nf`.`zona`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`livro`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`conta`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`cons_id`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`unid_id`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`rota`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`codclas`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`fase`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`numnf`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`mesano`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`mesano_ref`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`valortot`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`valorliq`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`ta`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`datavenc`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`flgst` " & vbCrLf
			lstrSql = lstrSql & "  FROM `nf` " & vbCrLf
			'Vencidos
			If Me.opbVencidos.Checked Then
				lstrSql = lstrSql & " WHERE `nf`.`datavenc` BETWEEN '" & VB6.Format(Me.dtpInicio.Value, "yyyy-MM-dd") & "' AND '" & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & "' " & vbCrLf
				lstrSql = lstrSql & "   AND (`nf`.`flgst` <> 'C' OR (`nf`.`flgst` = 'C' AND `nf`.`datast` > '" & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & "')) "
				If Me.chkTipo.CheckState = 1 Then
					lstrSql = lstrSql & "   AND `nf`.`flgst` IN(''"
					If Me.chkJudice.CheckState = 1 Then
						lstrSql = lstrSql & ",'J'"
					End If
					If Me.chkReserva.CheckState = 1 Then
						lstrSql = lstrSql & ",'R'"
					End If
					lstrSql = lstrSql & ") " & vbCrLf
				Else
					lstrSql = lstrSql & " AND (`nf`.`datapag` > '" & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & "' OR `nf`.`datapag` = '0000-00-00') " & vbCrLf
				End If
			ElseIf Me.opbArrecadados.Checked Then
				lstrSql = lstrSql & " WHERE (`nf`.`datapag` BETWEEN '" & VB6.Format(Me.dtpInicio.Value, "yyyy-MM-dd") & "' AND '" & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & "') " & vbCrLf
				lstrSql = lstrSql & "   AND (`nf`.`flgst` <> 'C' OR (`nf`.`flgst` = 'C' AND `nf`.`datast` > '" & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & "')) "
				lstrSql = lstrSql & "   AND `nf`.`flgst` IN('P') " & vbCrLf
				If Me.chkMesAno.CheckState = 1 Then
					lstrSql = lstrSql & "   AND `nf`.`mesano_ref` = " & CInt(VB6.Format(Me.dtpMesAno.Value, "yyyymm")) & " " & vbCrLf
				End If
			ElseIf Me.opbAbertos.Checked Then
				lstrSql = lstrSql & " WHERE mesano_ref <= " & CInt(VB6.Format(Me.dtpFinal.Value, "yyyymm")) & " " & vbCrLf
				lstrSql = lstrSql & "   AND (`nf`.`flgst` <> 'C' OR (`nf`.`flgst` = 'C' AND `nf`.`datast` > '" & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & "')) "
				lstrSql = lstrSql & "   AND NOT EXISTS(SELECT * FROM `reserva` "
				lstrSql = lstrSql & "                   WHERE `reserva`.`numnf`    = `nf`.`numnf` "
				lstrSql = lstrSql & "                     AND `reserva`.`mesano`   = `nf`.`mesano` "
				lstrSql = lstrSql & "                     AND `reserva`.`zona`     = `nf`.`zona` "
				lstrSql = lstrSql & "                     AND `reserva`.`livro`    = `nf`.`livro` "
				lstrSql = lstrSql & "                     AND `reserva`.`conta`    = `nf`.`conta` "
				lstrSql = lstrSql & "                     AND `reserva`.`datares` <= '" & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & "')"
				lstrSql = lstrSql & " AND (`nf`.`datapag` > '" & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & "' OR `nf`.`datapag` = '0000-00-00') " & vbCrLf
				If Me.chkMesAno.CheckState = 1 Then
					lstrSql = lstrSql & "   AND `nf`.`mesano_ref` <> " & CInt(VB6.Format(Me.dtpMesAno.Value, "yyyymm")) & " " & vbCrLf
				End If
			End If
			If Me.chkGrupoA.CheckState = 1 Or Me.chkGrupoB.CheckState = 1 Then
				lstrSql = lstrSql & "   AND `nf`.`grupo` IN("
				If Me.chkGrupoA.CheckState = 1 Then
					lstrSql = lstrSql & "'A'"
				End If
				If Me.chkGrupoA.CheckState = 1 And Me.chkGrupoB.CheckState = 1 Then
					lstrSql = lstrSql & ","
				End If
				If Me.chkGrupoB.CheckState = 1 Then
					lstrSql = lstrSql & "'B'"
				End If
				lstrSql = lstrSql & ") " & vbCrLf
			End If
			If Me.chkJudice.CheckState = 0 And Me.chkReserva.CheckState = 0 Then
				lstrSql = lstrSql & "   AND `nf`.`flgst` NOT IN("
				If Me.chkJudice.CheckState = 0 Then
					lstrSql = lstrSql & "'J'"
				End If
				If Me.chkJudice.CheckState = 0 And Me.chkReserva.CheckState = 0 Then
					lstrSql = lstrSql & ","
				End If
				If Me.chkReserva.CheckState = 0 Then
					lstrSql = lstrSql & "'R'"
				End If
				lstrSql = lstrSql & ") " & vbCrLf
			End If

			If IsNumeric(txtValor.Text) Then
				lstrSql = lstrSql & " and nf.valorliq >= " & Replace(txtValor.Text, ",", ".")
			End If

			With Me.lvsClasses
				'If .Items.Item(1).Checked = System.Windows.Forms.CheckState.Unchecked Then
				lstrFiltro = "   AND ("
				For lintConta = 0 To .Items.Count - 1
					If .Items.Item(lintConta).Checked = True Then
						lstrFiltro = lstrFiltro & " LEFT(`nf`.`codclas`," & Len(Trim(Str_Retira(.Items.Item(lintConta).SubItems(1).Text, "<="))) & ")" & .Items.Item(lintConta).SubItems(1).Text & " OR "
						If Trim(.Items.Item(lintConta).Text) = "Industrial" And .Items.Item(4).Checked = False Then
							lstrFiltro2 = " AND `nf`.`codclas` <> ""0100"" AND"
						Else
							lstrFiltro2 = "    "
						End If
					End If
				Next lintConta
				If lstrFiltro = " (" Then
						lstrFiltro = ""
						lstrFiltro2 = "    "
					End If
					lstrFiltro = VB.Left(lstrFiltro, Len(lstrFiltro) - 3) & ") "
				'Else
				'lstrFiltro = ""
				'lstrFiltro2 = "    "
				'End If
			End With


			lstrSql = lstrSql & lstrFiltro & lstrFiltro2
			lstrSql = VB.Left(lstrSql, Len(lstrSql) - 4)
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)


			System.Windows.Forms.Application.DoEvents()
			If dt.Rows.Count > 0 Then
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, dt.Rows.Count)
				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")))
					llngConsId = IIf(IsDBNull(row.Item("cons_id")), 0, row.Item("cons_id"))
					llngUnidId = IIf(IsDBNull(row.Item("unid_id")), 0, row.Item("unid_id"))
					lstrRota = Trim(IIf(IsDBNull(row.Item("rota")), "", row.Item("rota")))
					lstrCdClasse = Trim(IIf(IsDBNull(row.Item("codclas")), "", row.Item("codclas")))
					lstrFase = Trim(IIf(IsDBNull(row.Item("fase")), "", row.Item("fase")))
					ldblNumNf = IIf(IsDBNull(row.Item("numnf")), 0, row.Item("numnf"))
					lstrMesAno = Trim(IIf(IsDBNull(row.Item("mesano")), "", row.Item("mesano")))
					ldblValorTot = IIf(IsDBNull(row.Item("valortot")), 0, row.Item("valortot"))
					ldblValorLiq = IIf(IsDBNull(row.Item("valorliq")), 0, row.Item("valorliq"))
					lstrGrTarifa = Trim(IIf(IsDBNull(row.Item("ta")), "", row.Item("ta")))
					ldteVencimento = IIf(IsDBNull(row.Item("datavenc")), "", row.Item("datavenc"))
					lstrFlgSit = Trim(IIf(IsDBNull(row.Item("flgst")), "", row.Item("flgst")))


					If Len(lstrRota) > 0 Then
						lstrRota = VB.Left(lstrRota, 4) & "." & VB.Right(lstrRota, 4)
					End If
					If Len(lstrGrTarifa) > 0 Then
						lstrGrTarifa = Mid(lstrGrTarifa, 3, 2)
					End If
					Select Case VB.Left(lstrCdClasse, 1)
						Case Is < CStr(4)
							lstrNmClasse = "04 - Industrial"
						Case Is = CStr(4)
							lstrNmClasse = "03 - Comercial"
						Case Is = CStr(5)
							lstrNmClasse = "05 - Rural"
						Case Is = CStr(6)
							If CDbl(VB.Left(lstrCdClasse, 2)) = 64 Then
								lstrNmClasse = "07 - Ilum. Pública"
							Else
								lstrNmClasse = "06 - Poder Público"
							End If
						Case Is = CStr(7)
							lstrNmClasse = "08 - Serviço Público"
						Case Is = CStr(8)
							lstrNmClasse = "09 - Consumo Próprio"
						Case Is = CStr(9)
							If CDbl(VB.Right(lstrCdClasse, 1)) = 1 Then
								lstrNmClasse = "02 - Res. Normal"
							Else
								lstrNmClasse = "01 - Res. Bx.Renda"
							End If
					End Select
					llngConsId2 = 0

					lstrSql = "SELECT " & "       nome, " & "       end logradouro, " & "       numero, " & "       bairro, " & "       quadra, " & "       lote, " & "       complement complemento, " & "       cidade, " & "       distrito, " & "       cep, " & "       uf, " & "       situacao, " & "       cons_id, " & "       num medidor " & "  FROM uc_faturamento WHERE id = " & llngUnidId & " LIMIT 1 "
					objAtualizaBancoDados.QUERY_NAME = lstrSql
					dt1 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)


					If dt1.Rows.Count > 0 Then
						For Each row1 As DataRow In dt1.Rows
							llngConsId2 = IIf(IsDBNull(row1.Item("cons_id")), 0, row1.Item("cons_id"))
							lstrNome = Trim(IIf(IsDBNull(row1.Item("nome")), "", row1.Item("nome")))
							lstrLogradouro = Trim(IIf(IsDBNull(row1.Item("logradouro")), "", row1.Item("logradouro")))
							lstrNumero = Trim(IIf(IsDBNull(row1.Item("numero")), "", row1.Item("numero")))
							lstrBairro = Trim(IIf(IsDBNull(row1.Item("bairro")), "", row1.Item("bairro")))
							lstrQuadra = Trim(IIf(IsDBNull(row1.Item("quadra")), "", row1.Item("quadra")))
							lstrLote = Trim(IIf(IsDBNull(row1.Item("lote")), "", row1.Item("lote")))
							lstrComplemento = Trim(IIf(IsDBNull(row1.Item("complemento")), "", row1.Item("complemento")))
							lstrCidade = Trim(IIf(IsDBNull(row1.Item("cidade")), "", row1.Item("cidade")))
							lstrDistrito = Trim(IIf(IsDBNull(row1.Item("distrito")), "", row1.Item("distrito")))
							lstrCep = Trim(IIf(IsDBNull(row1.Item("cep")), "", row1.Item("cep")))
							lstrUf = Trim(IIf(IsDBNull(row1.Item("uf")), "", row1.Item("uf")))
							lstrSituacao = Trim(IIf(IsDBNull(row1.Item("situacao")), "", row1.Item("situacao")))
							lstrMedidor = Trim(IIf(IsDBNull(row1.Item("medidor")), "", row1.Item("medidor")))
						Next
					End If
					If Me.chkSomenteTranTit.CheckState = System.Windows.Forms.CheckState.Checked Then
						If llngConsId <> llngConsId2 Then
							lblnContinua = True
						Else
							lblnContinua = False
						End If
					Else
						lblnContinua = True
					End If
					If llngConsId <> llngConsId2 Then
						'If .State = 1 Then Call .Close()
						lstrSql = "SELECT " & "       nome, " & "       end logradouro, " & "       numero, " & "       bairro, " & "       quadra, " & "       lote, " & "       complement complemento, " & "       cidade, " & "       distrito, " & "       cep, " & "       uf " & "  FROM consum " & " WHERE consreg = " & llngConsId & " LIMIT 1 "
						objAtualizaBancoDados.QUERY_NAME = lstrSql
						dt2 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

						If dt2.Rows.Count > 0 Then
							For Each row2 As DataRow In dt2.Rows

								lstrNome = Trim(IIf(IsDBNull(row2.Item("nome")), "", row2.Item("nome")))
								lstrLogradouro = Trim(IIf(IsDBNull(row2.Item("logradouro")), "", row2.Item("logradouro")))
								lstrNumero = Trim(IIf(IsDBNull(row2.Item("numero")), "", row2.Item("numero")))
								lstrBairro = Trim(IIf(IsDBNull(row2.Item("bairro")), "", row2.Item("bairro")))
								lstrQuadra = Trim(IIf(IsDBNull(row2.Item("quadra")), "", row2.Item("quadra")))
								lstrLote = Trim(IIf(IsDBNull(row2.Item("lote")), "", row2.Item("lote")))
								lstrComplemento = Trim(IIf(IsDBNull(row2.Item("complemento")), "", row2.Item("complemento")))
								lstrCidade = Trim(IIf(IsDBNull(row2.Item("cidade")), "", row2.Item("cidade")))
								lstrDistrito = Trim(IIf(IsDBNull(row2.Item("distrito")), "", row2.Item("distrito")))
								lstrCep = Trim(IIf(IsDBNull(row2.Item("cep")), "", row2.Item("cep")))
								lstrUf = Trim(IIf(IsDBNull(row2.Item("uf")), "", row2.Item("uf")))
								'lstrSituacao = ""
								lstrMedidor = "TRANS.TIT."
								If Me.chkTransfTit.CheckState = System.Windows.Forms.CheckState.Checked Then
									lblnContinua = True
								Else
									lblnContinua = False
								End If
							Next
						Else
							lstrNome = ""
							lstrLogradouro = ""
							lstrNumero = ""
							lstrBairro = ""
							lstrQuadra = ""
							lstrLote = ""
							lstrComplemento = ""
							lstrCidade = ""
							lstrDistrito = ""
							lstrCep = ""
							lstrUf = ""
							lstrSituacao = "D"
							lstrMedidor = ""
						End If
					End If


					If lblnContinua Then
						lstrSql = ""
						lstrSql = lstrSql & "INSERT INTO AUX_ABERTOS SET "
						lstrSql = lstrSql & "             nomemaquina = """ & My.Computer.Name & """, "
						lstrSql = lstrSql & "             zona        = """ & lstrZona & """, "
						lstrSql = lstrSql & "             livro       = """ & lstrLivro & """, "
						lstrSql = lstrSql & "             conta       = """ & lstrConta & """, "
						lstrSql = lstrSql & "             nome        = """ & lstrNome & """, "
						lstrSql = lstrSql & "             logradouro  = """ & lstrLogradouro & """, "
						lstrSql = lstrSql & "             numero      = """ & lstrNumero & """, "
						lstrSql = lstrSql & "             bairro      = """ & lstrBairro & """, "
						lstrSql = lstrSql & "             quadra      = """ & lstrQuadra & """, "
						lstrSql = lstrSql & "             lote        = """ & lstrLote & """, "
						lstrSql = lstrSql & "             complemento = """ & lstrComplemento & """, "
						lstrSql = lstrSql & "             cidade      = """ & lstrCidade & """, "
						lstrSql = lstrSql & "             distrito    = """ & lstrDistrito & """, "
						lstrSql = lstrSql & "             cep         = """ & lstrCep & """, "
						lstrSql = lstrSql & "             uf          = """ & lstrUf & """, "
						lstrSql = lstrSql & "             rota        = """ & lstrRota & """, "
						lstrSql = lstrSql & "             situacao    = """ & lstrSituacao & """, "
						lstrSql = lstrSql & "             medidor     = """ & lstrMedidor & """, "
						lstrSql = lstrSql & "             classe      = """ & lstrNmClasse & """, "
						lstrSql = lstrSql & "             fase        = """ & lstrFase & """, "
						lstrSql = lstrSql & "             numnf       = " & ldblNumNf & ", "
						lstrSql = lstrSql & "             mesano      = """ & lstrFlgSit & " " & lstrMesAno & """, "
						lstrSql = lstrSql & "             valortot    = " & Replace(CStr(ldblValorTot), ",", ".") & ", "
						lstrSql = lstrSql & "             valorliq    = " & Replace(CStr(ldblValorLiq), ",", ".") & ", "
						lstrSql = lstrSql & "             datavenc    = """ & VB6.Format(ldteVencimento, "yyyy-MM-dd") & """, "
						lstrSql = lstrSql & "             datasele    = """ & VB6.Format(Me.dtpInicio.Value, "yyyy-MM-dd") & """, "
						lstrSql = lstrSql & "             dataselef   = """ & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & """, "
						lstrSql = lstrSql & "             grupo       = """ & lstrGrTarifa & """; "
						objExecAtualizaBancoDados.QUERY_NAME = lstrSql
						repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
					End If
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)

					System.Windows.Forms.Application.DoEvents()
				Next
			End If


			lstrCriterio = "{temp_abertos.nomemaquina} = """ & My.Computer.Name & """ "
			lstrAuxiliar = ""
			If Me.chkLigados.CheckState = 1 Then
				lstrAuxiliar = lstrAuxiliar & " {temp_abertos.situacao} = ""L"" OR"
			End If
			If Me.chkDesligados.CheckState = 1 Then
				lstrAuxiliar = lstrAuxiliar & " {temp_abertos.situacao} = ""D"" OR"
			End If
			If Me.chkCortados.CheckState = 1 Then
				lstrAuxiliar = lstrAuxiliar & " {temp_abertos.situacao} = ""C"" OR"
			End If
			If Me.chkDesativados.CheckState = 1 Then
				lstrAuxiliar = lstrAuxiliar & " {temp_abertos.situacao} = ""I"" OR"
			End If
			If Len(Trim(lstrAuxiliar)) > 0 Then
				lstrAuxiliar = " AND  (" & VB.Left(lstrAuxiliar, Len(lstrAuxiliar) - 2) & ") "
			Else
				lstrAuxiliar = ""
			End If
			lstrCriterio = lstrCriterio & lstrAuxiliar


			Call sDadosEmpresa(LImagem, sNomeEmpresa, sEnderecoEmpresa, sCepEmpresa, sCidadeEmpresa, sUFempresa, sBairroEmpresa, sFoneEmpresa, sWebEmpresa)
			Call lPrint("rptAbertos")


			'With Me.crpAux
			'	.ReportFileName = My.Application.Info.DirectoryPath & "\" & LeConfig("DIR_RPT") & "Abertos.rpt"
			'	.DiscardSavedData = True
			'	.WindowState = Crystal.WindowStateConstants.crptMaximized
			'	.WindowControlBox = True
			'	.WindowControls = True
			'	.WindowShowPrintSetupBtn = True
			'	.WindowShowCloseBtn = True
			'	.WindowShowSearchBtn = True
			'	.SelectionFormula = lstrCriterio
			'	.WindowTitle = "Relatório dos em Aberto"
			'	.Destination = Crystal.DestinationConstants.crptToWindow
			'	.set_Formulas(1, "SinteticoAnalitico = " & IIf(Me.opbSintetico.Checked, 1, 0))
			'	lstrAuxiliar = " Grupo ("
			'	If Me.chkGrupoA.CheckState = 1 Then
			'		lstrAuxiliar = lstrAuxiliar & "A"
			'	End If
			'	If Me.chkGrupoB.CheckState = 1 Then
			'		lstrAuxiliar = lstrAuxiliar & "B"
			'	End If
			'	lstrAuxiliar = lstrAuxiliar & ") - Situação ("

			'	If Me.chkLigados.CheckState = 1 Then
			'		lstrAuxiliar = lstrAuxiliar & "L,"
			'	End If
			'	If Me.chkCortados.CheckState = 1 Then
			'		lstrAuxiliar = lstrAuxiliar & "C,"
			'	End If
			'	If Me.chkDesligados.CheckState = 1 Then
			'		lstrAuxiliar = lstrAuxiliar & "D,"
			'	End If
			'	If Me.chkDesativados.CheckState = 1 Then
			'		lstrAuxiliar = lstrAuxiliar & "I,"
			'	End If
			'	lstrAuxiliar = VB.Left(lstrAuxiliar, Len(lstrAuxiliar) - 1) & ")"
			'	If Me.opbVencidos.Checked = True Then
			'		lstrAuxiliar = lstrAuxiliar & " - Desconsiderar pagas após o perído ("
			'		If Me.chkTipo.CheckState = 1 Then
			'			lstrAuxiliar = lstrAuxiliar & "Sim"
			'		Else
			'			lstrAuxiliar = lstrAuxiliar & "Não"
			'		End If
			'		lstrAuxiliar = lstrAuxiliar & ") "
			'	ElseIf Me.opbArrecadados.Checked = True Then 
			'		If Me.chkMesAno.CheckState = 1 Then
			'			lstrAuxiliar = lstrAuxiliar & " - Considerando somente faturamento ("
			'			lstrAuxiliar = lstrAuxiliar & VB6.Format(Me.dtpMesAno.value, "mm/yyyy")
			'			lstrAuxiliar = lstrAuxiliar & ") "
			'		End If
			'	ElseIf Me.opbAbertos.Checked = True Then 
			'		If Me.chkMesAno.CheckState = 1 Then
			'			lstrAuxiliar = lstrAuxiliar & " - Desconsiderando o faturamento ("
			'			lstrAuxiliar = lstrAuxiliar & VB6.Format(Me.dtpMesAno.value, "mm/yyyy")
			'			lstrAuxiliar = lstrAuxiliar & ") "
			'		End If
			'	End If
			'	If Me.chkJudice.CheckState = 1 Then
			'		lstrAuxiliar = lstrAuxiliar & " - em Júdice (Sim)"
			'	End If
			'	If Me.chkReserva.CheckState = 1 Then
			'		lstrAuxiliar = lstrAuxiliar & " - em Reserva (Sim)"
			'	End If
			'	If Me.opbVencidos.Checked Then
			'		.set_Formulas(2, "titulo = """ & "CONSUMIDORES VENCIDOS DE " & VB6.Format(Me.dtpInicio.value, "dd/mm/yyyy") & " ATÉ " & VB6.Format(Me.dtpFinal.value, "dd/mm/yyyy") & """ ")
			'	ElseIf Me.opbArrecadados.Checked Then 
			'		.set_Formulas(2, "titulo = """ & "CONSUMIDORES ARRECADADOS DE " & VB6.Format(Me.dtpInicio.value, "dd/mm/yyyy") & " ATÉ " & VB6.Format(Me.dtpFinal.value, "dd/mm/yyyy") & """ ")
			'	ElseIf Me.opbAbertos.Checked Then 
			'		.set_Formulas(2, "titulo = """ & "CONSUMIDORES EM ABERTOS ATÉ " & VB6.Format(Me.dtpFinal.value, "dd/mm/yyyy") & """ ")
			'	End If
			'	.set_Formulas(3, "titulo2 = """ & lstrAuxiliar & """ ")
			'	.Action = 0
			'End With
			Me.cmdEmitir.Enabled = True
			Me.cmdFechar.Enabled = True
			Me.fmeAux(0).Enabled = True
			Me.Cursor = System.Windows.Forms.Cursors.Default


		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdEmitir_Click" & "", "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 <= 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")
			''UPGRADE_WARNING: Lower bound of collection lsiItens 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"'
			'If lsiItens.SubItems.Count - 1 > alsvAuxiliar.Items.Count - 1 Then
			'	lsiItens.SubItems(alsvAuxiliar.Items.Count - 1).Text = " = 0"
			'Else
			'	lsiItens.SubItems.Insert(alsvAuxiliar.Items.Count - 1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, " = 0"))
			'End If

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

			'alsvAuxiliar.Items.Item(1).Font = VB6.FontChangeBold(alsvAuxiliar.Items.Item(1).Font, True)

			lsiItens = alsvAuxiliar.Items.Add("Residencial")

			If lsiItens.SubItems.Count - 1 > 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 - 1).Checked = True

			lsiItens = alsvAuxiliar.Items.Add("Industrial")

			If lsiItens.SubItems.Count - 1 > 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("Livre")

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

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

			If lsiItens.SubItems.Count - 1 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		Call Me.Close()
	End Sub
	Private Sub frmRelAbertos_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Try

			Me.dtpInicio.Value = gDtBcoDados
			Me.dtpFinal.Value = Me.dtpInicio.Value
			Me.dtpMesAno.Value = Me.dtpFinal.Value
			Call CarregaClasses((Me.lvsClasses))
			Me.chkMesAno.Enabled = False
			Me.dtpMesAno.Enabled = Me.chkMesAno.Enabled
			Call sCriaAuxiliar()

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

	'Private Sub frmRelAbertos_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
	'	Try
	'		Dim lstrSql As String
	'		lstrSql = "DELETE FROM temp_abertos WHERE nomemaquina = """ & My.Computer.Name & """ "
	'		'Call Executar(lstrSql)

	'	Catch ex As Exception
	'		MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmRelAbertos_FormClosed" & "", "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
	'	Dim aItem As System.Windows.Forms.ListViewItem = lvsClasses.Items(eventArgs.Index)
	'	Dim intConta As Short

	'	Try

	'		'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
	'		'			'UPGRADE_WARNING: Lower bound of collection Me.lvsClasses.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.lvsClasses.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: lvsClasses_ItemCheck" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
	'		Exit Sub
	'	End Try
	'End Sub

	'UPGRADE_WARNING: Event opbAbertos.CheckedChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub opbAbertos_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles opbAbertos.CheckedChanged

		Try
			If eventSender.Checked Then
				Me.dtpInicio.Enabled = False
				Me.dtpFinal.Enabled = True
				Me.chkMesAno.Enabled = True
				Me.chkTipo.Enabled = False
				Me.dtpMesAno.Enabled = Me.chkMesAno.Enabled
				Me.chkMesAno.Text = "Desconsiderar somente faturamento:"
			End If

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

	'UPGRADE_WARNING: Event opbArrecadados.CheckedChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub opbArrecadados_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles opbArrecadados.CheckedChanged
		Try
			If eventSender.Checked Then
				Me.dtpInicio.Enabled = True
				Me.dtpFinal.Enabled = True
				Me.chkMesAno.Enabled = True
				Me.chkTipo.Enabled = False
				Me.dtpMesAno.Enabled = Me.chkMesAno.Enabled
				Me.chkMesAno.Text = "Considerar somente faturamento:"
			End If

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

	'UPGRADE_WARNING: Event opbVencidos.CheckedChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub opbVencidos_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles opbVencidos.CheckedChanged
		Try
			If eventSender.Checked Then
				Me.dtpInicio.Enabled = True
				Me.dtpFinal.Enabled = True
				Me.chkMesAno.Enabled = False
				Me.chkTipo.Enabled = True
				Me.dtpMesAno.Enabled = Me.chkMesAno.Enabled
			End If

		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: opbVencidos_CheckedChanged" & "", "Aviso de 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 o Erro: " & Err.Description & " - Rotina: txtValor_Enter" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub dtpInicio_ValueChanged(sender As Object, e As EventArgs) Handles dtpInicio.ValueChanged
		Me.dtpFinal.Value = CDate(UltimoDiaMes(Month(Me.dtpInicio.Value), Year(Me.dtpInicio.Value)) & "/" & Month(Me.dtpInicio.Value) & "/" & Year(Me.dtpInicio.Value))
		Me.dtpMesAno.Value = CDate("01/" & Month(Me.dtpFinal.Value) & "/" & Year(Me.dtpFinal.Value))
	End Sub

	Private Sub dtpFinal_ValueChanged(sender As Object, e As EventArgs) Handles dtpFinal.ValueChanged
		Me.dtpMesAno.Value = CDate("01/" & Month(Me.dtpFinal.Value) & "/" & Year(Me.dtpFinal.Value))
	End Sub

	Private Sub frmRelAbertos_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
		Try
			Dim lstrsql As String

			lstrsql = ""
			lstrsql = lstrsql & "DROP TABLE IF EXISTS AUX_ABERTOS"
			objExecAtualizaBancoDados.QUERY_NAME = lstrsql
			repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

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

	End Sub

	Private Sub txtValor_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtValor.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		Try
			If KeyAscii = System.Windows.Forms.Keys.Return Then
				'Call SendKeysA(System.Windows.Forms.Keys.Tab, True)
			ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then
				If KeyAscii = System.Windows.Forms.Keys.Delete Then
					KeyAscii = System.Windows.Forms.Keys.Snapshot
				End If
				If KeyAscii <> System.Windows.Forms.Keys.Snapshot Then
					KeyAscii = 0
				Else
					If InStr(1, Me.txtValor.Text, ",") > 0 Then
						KeyAscii = 0
					End If
				End If
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If


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

	Private Sub chktodos_CheckedChanged(sender As Object, e As EventArgs) Handles chktodos.CheckedChanged
		Dim intConta As Short

		Try
			If chktodos.Checked = True Then
				For intConta = 0 To lvsClasses.Items.Count - 1

					lvsClasses.Items.Item(intConta).Checked = True
				Next

			ElseIf chktodos.Checked = False Then
				For intConta = 0 To lvsClasses.Items.Count - 1

					lvsClasses.Items.Item(intConta).Checked = False
				Next intConta
			End If

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

	Private Sub sCriaAuxiliar()
		Try
			Dim lstrsql As String

			lstrsql = ""
			lstrsql = lstrsql & "DROP TABLE IF EXISTS AUX_ABERTOS"
			objExecAtualizaBancoDados.QUERY_NAME = lstrsql
			repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

			lstrsql = ""
			lstrsql = lstrsql & "create table IF NOT EXISTS AUX_ABERTOS("
			lstrsql = lstrsql & "NOMEMAQUINA varchar(50), "
			lstrsql = lstrsql & "             ZONA varchar(20), "
			lstrsql = lstrsql & "             LIVRO varchar(20), "
			lstrsql = lstrsql & "             CONTA varchar(20), "
			lstrsql = lstrsql & "             NOME varchar(50),"
			lstrsql = lstrsql & "             LOGRADOURO varchar(50), "
			lstrsql = lstrsql & "             NUMERO varchar(20), "
			lstrsql = lstrsql & "             BAIRRO varchar(50), "
			lstrsql = lstrsql & "             QUADRA varchar(20), "
			lstrsql = lstrsql & "             LOTE varchar(20), "
			lstrsql = lstrsql & "             COMPLEMENTO varchar(20), "
			lstrsql = lstrsql & "             CIDADE varchar(50), "
			lstrsql = lstrsql & "             DISTRITO  varchar(50),"
			lstrsql = lstrsql & "             CEP varchar(10),"
			lstrsql = lstrsql & "             UF varchar(10),"
			lstrsql = lstrsql & "             ROTA varchar(10),"
			lstrsql = lstrsql & "             SITUACAO varchar(20),"
			lstrsql = lstrsql & "             MEDIDOR varchar(20),"
			lstrsql = lstrsql & "             CLASSE varchar(20),"
			lstrsql = lstrsql & "             FASE varchar(20),"
			lstrsql = lstrsql & "             NUMNF varchar(10),"
			lstrsql = lstrsql & "             MESANO varchar(10),"
			lstrsql = lstrsql & "             VALORTOT varchar(20),"
			lstrsql = lstrsql & "             VALORLIQ varchar(20),"
			lstrsql = lstrsql & "             DATAVENC varchar(20),"
			lstrsql = lstrsql & "             DATASELE varchar(20),"
			lstrsql = lstrsql & "             DATASELEF varchar(20),"
			lstrsql = lstrsql & "             GRUPO  varchar(10))"
			objExecAtualizaBancoDados.QUERY_NAME = lstrsql
			repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)


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

	Private Sub lPrint(ByVal vNomeRPT As String)

		Dim dt As DataTable
		Dim dt_1 As DataTable
		Dim dt_2 As DataTable
		Dim dt_3 As DataTable
		Dim dc As DataColumn
		Dim ds As DataSet
		Dim lstrsql As String

		Dim bytBLOBData() As Byte

		Dim objEmpresa As New entidadeEmpresa
		Dim repositorioEmpresa As New repositorioEmpresa

		Dim vlist


		'Dim vlist

		'Dim objEmpresa As New entidadeEmpresa
		'Dim repositorioEmpresa As New repositorioEmpresa


		'vlist = repositorioEmpresa.Select(objEmpresa)

		'If vlist.Count <> 0 Then

		'	For Each objEmpresaFor As entidadeEmpresa In vlist

		'		bytBLOBData = objEmpresaFor.LOGOTIPO

		'	Next

		'End If

		Try
			Dim rpt As New ReportDocument()

			'rptRelSampNovoBaixaRenda
			If vNomeRPT = "rptAbertos" Then

				dt_2 = New DataTable()
				dt_2.Columns.Add("NOMEMAQUINA", GetType(String))
				dt_2.Columns.Add("ZONA", GetType(String))
				dt_2.Columns.Add("LIVRO", GetType(String))
				dt_2.Columns.Add("CONTA", GetType(String))
				dt_2.Columns.Add("NOME", GetType(String))
				dt_2.Columns.Add("LOGRADOURO", GetType(String))
				dt_2.Columns.Add("NUMERO", GetType(String))
				dt_2.Columns.Add("BAIRRO", GetType(String))
				dt_2.Columns.Add("QUADRA", GetType(String))
				dt_2.Columns.Add("LOTE", GetType(String))
				dt_2.Columns.Add("COMPLEMENTO", GetType(String))
				dt_2.Columns.Add("CIDADE", GetType(String))
				dt_2.Columns.Add("DISTRITO", GetType(String))
				dt_2.Columns.Add("CEP", GetType(String))
				dt_2.Columns.Add("UF", GetType(String))
				dt_2.Columns.Add("ROTA", GetType(String))
				dt_2.Columns.Add("SITUACAO", GetType(String))
				dt_2.Columns.Add("MEDIDOR", GetType(String))
				dt_2.Columns.Add("CLASSE", GetType(String))
				dt_2.Columns.Add("FASE", GetType(String))
				dt_2.Columns.Add("NUMNF", GetType(String))
				dt_2.Columns.Add("MESANO", GetType(String))
				dt_2.Columns.Add("VALORTOT", GetType(String))
				dt_2.Columns.Add("VALORLIQ", GetType(String))
				dt_2.Columns.Add("DATAVENC", GetType(String))
				dt_2.Columns.Add("DATASELE", GetType(String))
				dt_2.Columns.Add("DATASELEF", GetType(String))
				dt_2.Columns.Add("GRUPO", GetType(String))
				dt_2.Columns.Add("IMAGEM", GetType(Byte()))
				ds = New DataSet

				Dim sSituacao As String
				Dim lstrAuxiliar As String

				lstrAuxiliar = ""
				If Me.chkLigados.CheckState = 1 Then
					lstrAuxiliar = lstrAuxiliar & " Situacao = ""L"" OR"
				End If
				If Me.chkDesligados.CheckState = 1 Then
					lstrAuxiliar = lstrAuxiliar & " Situacao= ""D"" OR"
				End If
				If Me.chkCortados.CheckState = 1 Then
					lstrAuxiliar = lstrAuxiliar & " Situacao = ""C"" OR"
				End If
				If Me.chkDesativados.CheckState = 1 Then
					lstrAuxiliar = lstrAuxiliar & " Situacao = ""I"" OR"
				End If
				If Len(Trim(lstrAuxiliar)) > 0 Then
					lstrAuxiliar = " AND  (" & VB.Left(lstrAuxiliar, Len(lstrAuxiliar) - 2) & ") "
				Else
					lstrAuxiliar = ""
				End If


				lstrsql = ""
				lstrsql = "SELECT * FROM AUX_ABERTOS WHERE NOMEMAQUINA = '" & My.Computer.Name & "'" & lstrAuxiliar
				objAtualizaBancoDados.QUERY_NAME = lstrsql
				dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

				If dt.Rows.Count > 0 Then
					For Each dr_1 As DataRow In dt.Rows

						dt_2.Rows.Add(dr_1("NOMEMAQUINA"),
										  dr_1("ZONA"),
										  dr_1("LIVRO"),
										  dr_1("CONTA"),
										  dr_1("NOME"),
										  dr_1("LOGRADOURO"),
										  dr_1("NUMERO"),
										  dr_1("BAIRRO"),
										  dr_1("QUADRA"),
										  dr_1("LOTE"),
										  dr_1("COMPLEMENTO"),
										  dr_1("CIDADE"),
										  dr_1("DISTRITO"),
										  dr_1("CEP"),
										  dr_1("UF"),
										  dr_1("ROTA"),
										  dr_1("SITUACAO"),
										  dr_1("CLASSE"),
										  dr_1("FASE"),
										  dr_1("NUMNF"),
										  dr_1("MESANO"),
										  dr_1("VALORTOT"),
										  dr_1("VALORLIQ"),
										  dr_1("DATAVENC"),
										  dr_1("DATASELE"),
										  dr_1("DATASELEF"),
										  dr_1("GRUPO"),
										  bytBLOBData)
					Next dr_1
				End If

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

				If ds.Tables(0).Rows.Count = 0 Then
					MessageBox.Show("Não há registros para o relatório escolhido! ", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
					Exit Sub
				Else

					Dim strSelectionfrm As String
					Dim strTitulo As String
					Dim strTitulo2 As String

					'strSelectionfrm = "({REL_ABERTOS.SinteticoAnalitico}=1)"


					lstrAuxiliar = " Grupo ("
					If Me.chkGrupoA.CheckState = 1 Then
						lstrAuxiliar = lstrAuxiliar & "A"
					End If
					If Me.chkGrupoB.CheckState = 1 Then
						lstrAuxiliar = lstrAuxiliar & "B"
					End If
					lstrAuxiliar = lstrAuxiliar & ") - Situação ("

					If Me.chkLigados.CheckState = 1 Then
						lstrAuxiliar = lstrAuxiliar & "L,"
					End If
					If Me.chkCortados.CheckState = 1 Then
						lstrAuxiliar = lstrAuxiliar & "C,"
					End If
					If Me.chkDesligados.CheckState = 1 Then
						lstrAuxiliar = lstrAuxiliar & "D,"
					End If
					If Me.chkDesativados.CheckState = 1 Then
						lstrAuxiliar = lstrAuxiliar & "I,"
					End If
					lstrAuxiliar = VB.Left(lstrAuxiliar, Len(lstrAuxiliar) - 1) & ")"
					If Me.opbVencidos.Checked = True Then
						lstrAuxiliar = lstrAuxiliar & " - Desconsiderar pagas após o perído ("
						If Me.chkTipo.CheckState = 1 Then
							lstrAuxiliar = lstrAuxiliar & "Sim"
						Else
							lstrAuxiliar = lstrAuxiliar & "Não"
						End If
						lstrAuxiliar = lstrAuxiliar & ") "
					ElseIf Me.opbArrecadados.Checked = True Then
						If Me.chkMesAno.CheckState = 1 Then
							lstrAuxiliar = lstrAuxiliar & " - Considerando somente faturamento ("
							lstrAuxiliar = lstrAuxiliar & VB6.Format(Me.dtpMesAno.Value, "mm/yyyy")
							lstrAuxiliar = lstrAuxiliar & ") "
						End If
					ElseIf Me.opbAbertos.Checked = True Then
						If Me.chkMesAno.CheckState = 1 Then
							lstrAuxiliar = lstrAuxiliar & " - Desconsiderando o faturamento ("
							lstrAuxiliar = lstrAuxiliar & VB6.Format(Me.dtpMesAno.Value, "mm/yyyy")
							lstrAuxiliar = lstrAuxiliar & ") "
						End If
					End If
					If Me.chkJudice.CheckState = 1 Then
						lstrAuxiliar = lstrAuxiliar & " - em Júdice (Sim)"
					End If
					If Me.chkReserva.CheckState = 1 Then
						lstrAuxiliar = lstrAuxiliar & " - em Reserva (Sim)"
					End If

					If Me.opbVencidos.Checked Then
						strTitulo = "CONSUMIDORES VENCIDOS DE " & VB6.Format(Me.dtpInicio.Value, "dd/mm/yyyy") & " ATÉ " & VB6.Format(Me.dtpFinal.Value, "dd/mm/yyyy")
					ElseIf Me.opbArrecadados.Checked Then
						strTitulo = "CONSUMIDORES ARRECADADOS DE " & VB6.Format(Me.dtpInicio.Value, "dd/mm/yyyy") & " ATÉ " & VB6.Format(Me.dtpFinal.Value, "dd/mm/yyyy")
					ElseIf Me.opbAbertos.Checked Then
						strTitulo = "CONSUMIDORES EM ABERTOS ATÉ " & VB6.Format(Me.dtpFinal.Value, "dd/mm/yyyy")
					End If

					'strTitulo = "({REL_ABERTOS.titulo2}='" & lstrAuxiliar & "')"
					'.set_Formulas(3, "titulo2 = """ & lstrAuxiliar & """ ")
					'.Action = 0

					'End With

					rpt = New rptAbertos

					rpt.SetDataSource(ds)
					rpt.SetParameterValue("nomeempresa", sNomeEmpresa)
					rpt.SetParameterValue("enderecoempresa", sEnderecoEmpresa)
					rpt.SetParameterValue("cepempresa", sCepEmpresa)
					rpt.SetParameterValue("ufempresa", sUFempresa)
					rpt.SetParameterValue("cidadeempresa", sCidadeEmpresa)
					rpt.SetParameterValue("bairroempresa", sBairroEmpresa)
					rpt.SetParameterValue("foneempresa", sFoneEmpresa)
					rpt.SetParameterValue("websiteempresa", sWebEmpresa)
					rpt.SetParameterValue("Titulo", strTitulo)
					rpt.SetParameterValue("Titulo2", lstrAuxiliar)
					rpt.SetParameterValue("SinteticoAnalitico", IIf(opbSintetico.Checked, "1", "0"))
					'rpt.RecordSelectionFormula = strSelectionfrm
					'rpt.RecordSelectionFormula = strTitulo
					'rpt.SetParameterValue("MesAnoRef", Month(dtpMesAno.Value) & "/" & Year(dtpMesAno.Value))
					'rpt.SetParameterValue("NomeRel", "Grupo A - Período de: " & dtpIni.Value & " Até: " & dtpFim.Value)

					frmViewRelatorios.rpt = rpt

					frmViewRelatorios.CRViewer.Zoom((100))
					frmViewRelatorios.Text = "Relatório Consumidor em Aberto"
					frmViewRelatorios.ShowDialog()

				End If

			End If

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

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

	End Sub
End Class