Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmEmissaoSubvencao
	Inherits System.Windows.Forms.Form
	Private istrPrefNmArq As String
	Private istrCodEmpresa As String
	
	'UPGRADE_WARNING: Event cmbOperacao.SelectedIndexChanged 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 cmbOperacao_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmbOperacao.SelectedIndexChanged
		Call RedefineNomeArq()
	End Sub
	
	Private Function BuscaClasse(ByVal astrClasse As String) As Object
		Select Case CShort(astrClasse)
			Case 0 To 3
				'UPGRADE_WARNING: Couldn't resolve default property of object BuscaClasse. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				BuscaClasse = "IND"
			Case 4
				'UPGRADE_WARNING: Couldn't resolve default property of object BuscaClasse. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				BuscaClasse = "COM"
			Case 5
				'UPGRADE_WARNING: Couldn't resolve default property of object BuscaClasse. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				BuscaClasse = "RUR"
			Case 9
				'UPGRADE_WARNING: Couldn't resolve default property of object BuscaClasse. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				BuscaClasse = "RES"
			Case Else
				'UPGRADE_WARNING: Couldn't resolve default property of object BuscaClasse. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				BuscaClasse = "OUT"
		End Select
	End Function
	
	Private Function RemoveCaracteres(ByVal astrString As String, ByVal astrCaracteres As String) As Object
		Dim llngCount As Integer
		Dim lstrNovaString As String
		
		lstrNovaString = astrString
		For llngCount = 1 To Len(astrCaracteres)
			lstrNovaString = Replace(lstrNovaString, Mid(astrCaracteres, llngCount, 1), "")
		Next 
		
		'UPGRADE_WARNING: Couldn't resolve default property of object RemoveCaracteres. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		RemoveCaracteres = lstrNovaString
	End Function
	
	Private Sub InsereSCS(ByVal astrTpOperacao As String, ByVal astrTpAcao As String, ByVal adteRegistro As Date, ByVal astrId As String, Optional ByVal astrMotivo As String = "")
		Dim lstrSql As String
		Dim lrstVerif As ADODB.Recordset
		Dim lblnVerif As Boolean
		
		lrstVerif = New ADODB.Recordset
		
		lblnVerif = False
		
		If astrTpAcao = "R" Then
			lstrSql = "SELECT COUNT(*) AS verif " & "  FROM scs " & " WHERE tpoperacao = '" & astrTpOperacao & "' " & "   AND tpacao = 'R' " & "   AND identificador = '" & astrId & "'"
			Call Conectar(lstrSql, lrstVerif)
			With lrstVerif
				lblnVerif = (.Fields("verif").value > 0)
				If .State = 1 Then Call .Close()
			End With
		End If
		
		If astrTpAcao = "A" Then
			lstrSql = "DELETE FROM scs " & " WHERE tpoperacao = '" & astrTpOperacao & "' " & "   AND tpacao = 'A' " & "   AND identificador = '" & astrId & "' " & "   AND DATE_FORMAT(dtregistro, '%m/%Y') = '" & VB6.Format(adteRegistro, "mm/yyyy") & "'"
			Call Executar(lstrSql)
			Call Conectar(lstrSql, lrstVerif)
		End If
		
		If Not lblnVerif Then
			lstrSql = "INSERT INTO scs SET tpoperacao    = '" & astrTpOperacao & "', " & "                    tpacao        = '" & astrTpAcao & "', " & "                    identificador = '" & astrId & "', " & "                    dtregistro    = '" & VB6.Format(adteRegistro, "yyyy-MM-dd") & "', " & "                    `desc`        = """ & astrMotivo & """"
			Call Executar(lstrSql)
		End If
		
		'UPGRADE_NOTE: Object lrstVerif may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		lrstVerif = Nothing
	End Sub
	
	Private Sub RegistrarBeneficiario()
		Dim lintTipo As Short
		
		Dim lstrSql As String
		Dim lrstBenef As ADODB.Recordset
		Dim lrstVerif As ADODB.Recordset
		
		lrstBenef = New ADODB.Recordset
		lrstVerif = New ADODB.Recordset
		
		Dim lblnBxRenda As Boolean
		Dim lblnCabec As Boolean
		
		Dim llngConsId As Integer
		Dim lstrCpf As String
		Dim lstrNisCod As String
		Dim lstrNome As String
		Dim lstrDtNascto As String
		Dim lstrDtBolsaFam As String
		Dim lstrDtAutoDecl As String
		Dim lstrCodIbge As String
		Dim lstrPaisNacto As String
		Dim lstrUfNascto As String
		Dim lstrTitEleitor As String
		Dim lstrNrId As String
		Dim lstrEmissorId As String
		Dim lstrUfId As String
		Dim lstrOutroDoc As String
		Dim lstrEmissorOutroDoc As String
		Dim lstrSexo As String
		Dim lstrDtCad As String
		
		Dim lintARQ As Short
		
		Me.lblInfo.Text = " Buscando dados..."
		Me.lblInfo.Refresh()
		
		lstrSql = "SELECT consum.consreg, " & "       consum.cgc, " & "       nis.nis, " & "       consum.nome, " & "       consum.dt_nascto, " & "       nis.data_nascimento, " & "       nis.uf_nascimento, " & "       consum.rg, " & "       nis.data_cadastro, " & "       consum.dt_cadastro " & "  FROM consum LEFT JOIN nis ON (nis.cpf = consum.cgc) "
		'          & " WHERE scs_status = ""I"" AND scs_data  "
		Call Conectar(lstrSql, lrstBenef)
		
		With lrstBenef
			If .RecordCount > 0 Then
				
				lblnCabec = True
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount)
				
				While Not .EOF
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					llngConsId = IIf(IsDbNull(.Fields("consreg").value), 0, .Fields("consreg").value)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrCpf = Trim(IIf(IsDbNull(.Fields("cgc").value), "", .Fields("cgc").value))
					If Len(lstrCpf) > 11 Then lstrCpf = ""
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					'UPGRADE_WARNING: Couldn't resolve default property of object RemoveCaracteres(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					lstrNisCod = VB.Left(RemoveCaracteres(Trim(IIf(IsDbNull(.Fields("nis").value), "", .Fields("nis").value)), "/-."), 11)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrNome = Replace(RetiraAcentuacao(VB.Left(Trim(IIf(IsDbNull(.Fields("nome").value), "", .Fields("nome").value)), 100)), "&", "E")
					If Len(lstrNisCod) > 0 Then
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrDtNascto = VB6.Format(IIf(IsDbNull(.Fields("data_nascimento").value) Or Trim(.Fields("data_nascimento").value) = "", "1900-01-01", .Fields("data_nascimento").value), "dd/mm/yyyy")
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrDtBolsaFam = VB6.Format(IIf(IsDbNull(.Fields("data_cadastro").value) Or Trim(.Fields("data_cadastro").value) = "", "1900-01-01", .Fields("data_cadastro").value), "dd/mm/yyyy")
						lstrDtAutoDecl = VB6.Format("1900-01-01", "dd/mm/yyyy")
					Else
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrDtNascto = VB6.Format(IIf(IsDbNull(.Fields("dt_nascto").value) Or Trim(.Fields("dt_nascto").value) = "", "1900-01-01", .Fields("dt_nascto").value), "dd/mm/yyyy")
						lstrDtBolsaFam = VB6.Format("1900-01-01", "dd/mm/yyyy")
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrDtAutoDecl = VB6.Format(IIf(IsDbNull(.Fields("data_cadastro").value) Or Trim(.Fields("data_cadastro").value) = "", "1900-01-01", .Fields("data_cadastro").value), "dd/mm/yyyy")
					End If
					lstrCodIbge = ""
					lstrPaisNacto = ""
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrUfNascto = Trim(IIf(IsDbNull(.Fields("uf_nascimento").value), "", .Fields("uf_nascimento").value))
					lstrTitEleitor = ""
					lstrNrId = ""
					lstrEmissorId = ""
					lstrUfId = ""
					lstrOutroDoc = ""
					lstrEmissorOutroDoc = ""
					lstrSexo = ""
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrDtCad = IIf(IsDbNull(.Fields("dt_cadastro").value), "1900-01-01", .Fields("dt_cadastro").value)
					lstrSql = "SELECT (SELECT COUNT(*) " & "  FROM nf, unidb " & " WHERE nf.cons_id = " & llngConsId & " " & "   AND nf.zona = unidb.zona " & "   AND nf.livro = unidb.livro " & "   AND nf.conta = unidb.conta " & "   AND nf.mesano = '" & VB6.Format(Me.dtpMesAno.value, "mm/yyyy") & "' " & "   AND nf.flgst <> 'C' " & "   AND (nf.codclas IN ('9104','9105','9106') " & "        OR (nf.codclas IN ('9101','9107') AND unidb.bx0402 = 'S')) " & "   AND (SELECT COUNT(*) FROM scs WHERE tpoperacao = 'B' AND tpacao = 'R' AND identificador = nf.cons_id AND DATE_FORMAT(dtregistro, '%m/%Y') <> nf.mesano) = 0" & ") AS bxrenda_ok"
					Call Conectar(lstrSql, lrstVerif)
					
					With lrstVerif
						lblnBxRenda = (.Fields("bxrenda_ok").value > 0)
						If .State = 1 Then Call .Close()
					End With
					
					If lblnBxRenda Then
						
						Call InsereSCS("B", "R", Me.dtpMesAno.value, CStr(llngConsId))
						
						If lblnCabec Then
							lblnCabec = False
							
							lintARQ = FreeFile
							FileOpen(lintARQ, Me.txtArquivo.Text, OpenMode.Output)
							
							PrintLine(lintARQ, "<?xml version=""1.0"" encoding=""iso-8859-1""?>")
							PrintLine(lintARQ, "<SCS tipo=""REGISTRARBENEFICIARIO"" " & "codigo_agente=""" & istrCodEmpresa & """ " & "mes=""" & VB6.Format(Me.dtpMesAno.value, "mm") & """ ano=""" & VB6.Format(Me.dtpMesAno.value, "yyyy") & """ " & "sequencial=""S" & Me.txtSequencia.Text & """>")
							PrintLine(lintARQ, vbTab & "<beneficiarios>")
						End If
						
						PrintLine(lintARQ, vbTab & vbTab & "<beneficiario cba=""" & llngConsId & """>")
						PrintLine(lintARQ, vbTab & vbTab & vbTab & "<cpf>" & lstrCpf & "</cpf>")
						PrintLine(lintARQ, vbTab & vbTab & vbTab & "<codigo_nis>" & lstrNisCod & "</codigo_nis>")
						'UPGRADE_WARNING: Couldn't resolve default property of object RemoveCaracteres(lstrNome, ./\-()). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						PrintLine(lintARQ, vbTab & vbTab & vbTab & "<nome>" & RemoveCaracteres(lstrNome, "./\-()") & "</nome>")
						PrintLine(lintARQ, vbTab & vbTab & vbTab & "<data_nascimento>" & IIf(lstrDtNascto = "01/01/1900", "", lstrDtNascto) & "</data_nascimento>")
						PrintLine(lintARQ, vbTab & vbTab & vbTab & "<cod_ibge_municipio>" & lstrCodIbge & "</cod_ibge_municipio>")
						PrintLine(lintARQ, vbTab & vbTab & vbTab & "<pais_nascimento>" & lstrPaisNacto & "</pais_nascimento>")
						PrintLine(lintARQ, vbTab & vbTab & vbTab & "<uf_nascimento>" & lstrUfNascto & "</uf_nascimento>")
						PrintLine(lintARQ, vbTab & vbTab & vbTab & "<titulo_eleitor>" & lstrTitEleitor & "</titulo_eleitor>")
						PrintLine(lintARQ, vbTab & vbTab & vbTab & "<identidade >")
						PrintLine(lintARQ, vbTab & vbTab & vbTab & vbTab & "<numero>" & lstrNrId & "</numero>")
						PrintLine(lintARQ, vbTab & vbTab & vbTab & vbTab & "<emissor>" & lstrEmissorId & "</emissor>")
						PrintLine(lintARQ, vbTab & vbTab & vbTab & vbTab & "<estado>" & lstrUfId & "</estado>")
						PrintLine(lintARQ, vbTab & vbTab & vbTab & "</identidade>")
						PrintLine(lintARQ, vbTab & vbTab & vbTab & "<outro_documento>")
						PrintLine(lintARQ, vbTab & vbTab & vbTab & vbTab & "<numero>" & lstrOutroDoc & "</numero>")
						PrintLine(lintARQ, vbTab & vbTab & vbTab & vbTab & "<emissor>" & lstrEmissorOutroDoc & "</emissor>")
						PrintLine(lintARQ, vbTab & vbTab & vbTab & "</outro_documento>")
						PrintLine(lintARQ, vbTab & vbTab & vbTab & "<sexo>" & lstrSexo & "</sexo>")
						PrintLine(lintARQ, vbTab & vbTab & vbTab & "<data_bolsa_familia>" & IIf(lstrDtBolsaFam = "01/01/1900", "", lstrDtBolsaFam) & "</data_bolsa_familia>")
						PrintLine(lintARQ, vbTab & vbTab & vbTab & "<data_autodeclarado>" & IIf(lstrDtAutoDecl = "01/01/1900", "", lstrDtAutoDecl) & "</data_autodeclarado>")
						PrintLine(lintARQ, vbTab & vbTab & vbTab & "<data_cadastro>" & IIf(lstrDtCad = "01/01/1900", "", lstrDtCad) & "</data_cadastro>")
						PrintLine(lintARQ, vbTab & vbTab & "</beneficiario>")
						
					End If
					
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
					Call .MoveNext()
					System.Windows.Forms.Application.DoEvents()
				End While
				
				If Not lblnCabec Then
					PrintLine(lintARQ, vbTab & "</beneficiarios>")
					PrintLine(lintARQ, "</SCS>")
					FileClose(lintARQ)
					Call MsgBox("O arquivo foi gerado com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
				Else
					Call MsgBox("Não foram encontrados registros para essa operação." & vbCrLf & "O arquivo NÃO foi gerado!", MsgBoxStyle.Information, gstrTituloMensagem)
				End If
				
			End If
			If .State = 1 Then Call .Close()
		End With
		
		Me.lblInfo.Text = ""
		
	End Sub
	
	Private Sub RegistrarUC()
		Dim lstrSql As String
		Dim lrstUC As ADODB.Recordset
		Dim lrstVerif As ADODB.Recordset
		'  Dim lrstNF As ADODB.Recordset
		
		lrstUC = New ADODB.Recordset
		lrstVerif = New ADODB.Recordset
		'  Set lrstNF = New ADODB.Recordset
		
		'  Dim lblnNfOK As Boolean
		Dim lblnCabec As Boolean
		
		Dim lintARQ As Short
		
		'  Dim llngConsIdNF As Long
		Dim llngConsId As Integer
		Dim llngUnidId As Integer
		Dim lstrZona As String
		Dim lstrLivro As String
		Dim lstrConta As String
		Dim lstrZLC As String
		Dim lstrCodEnd As String
		Dim llngCodIbge As Integer
		Dim lstrSetorCensitario As String
		Dim lstrLogradouro As String
		Dim lstrBairro As String
		Dim lstrCep As String
		Dim lstrClasse As String
		'  Dim lstrClasseNF As String
		Dim lstrClasseConsumo As String
		Dim lstrTpLigacao As String
		Dim lstrDtCadastro As String
		Dim lstrBxRenda0402 As String
		Dim lstrStatus As String
		Dim lblnVerifRegistro As Boolean
		
		Me.lblInfo.Text = " Buscando dados..."
		Me.lblInfo.Refresh()
		
		lstrSql = "SELECT unidb.unid_id, " & "       unidb.cons_id, " & "       unidb.zona, " & "       unidb.livro, " & "       unidb.conta, " & "       unidb.`end`, " & "       unidb.numero, " & "       unidb.complement, " & "       unidb.quadra, " & "       unidb.lote, " & "       unidb.bairro, " & "       unidb.cidade, " & "       unidb.cep, " & "       unidb.clas, " & "       unidb.fase, " & "       unidb.data_entrada, " & "       unidb.bx0402, " & "       unidb.codend, " & "       SCSchecaStatus(CONCAT(unidb.zona, unidb.livro, unidb.conta), 'U') AS status, " & "       (SELECT COUNT(*) FROM scs WHERE scs.tpoperacao = 'U' AND scs.tpacao = 'R' AND scs.identificador = CONCAT(unidb.zona, unidb.livro, unidb.conta) AND DATE_FORMAT(scs.dtregistro, '%m/%Y') <> '" & VB6.Format(Me.dtpMesAno.value, "mm/yyyy") & "') AS verifRegistro " & "  FROM unidb "
		Call Conectar(lstrSql, lrstUC)
		
		With lrstUC
			If .RecordCount > 0 Then
				
				lblnCabec = True
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount)
				
				While Not .EOF
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					llngUnidId = IIf(IsDbNull(.Fields("unid_id").value), 0, .Fields("unid_id").value)
					'        llngConsId = IIf(IsNull(.Fields("cons_id").Value), 0, .Fields("cons_id").Value)
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrZona = VB.Right("000" & Trim(IIf(IsDbNull(.Fields("zona").value), "", .Fields("zona").value)), 3)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrLivro = VB.Right("00" & Trim(IIf(IsDbNull(.Fields("livro").value), "", .Fields("livro").value)), 2)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrConta = VB.Right("000000" & Trim(IIf(IsDbNull(.Fields("conta").value), "", .Fields("conta").value)), 6)
					lstrZLC = lstrZona & lstrLivro & lstrConta
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrCodEnd = Trim(IIf(IsDbNull(.Fields("codend").value), "000000", .Fields("codend").value))
					llngCodIbge = CInt(BuscaIbge( , lstrCodEnd))
					lstrSetorCensitario = ""
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrLogradouro = Trim(IIf(IsDbNull(.Fields("end").value), "", .Fields("end").value))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					If Not IsDbNull(.Fields("numero").value) Then
						lstrLogradouro = lstrLogradouro & IIf(Len(Trim(.Fields("numero").value)), ", " & Trim(.Fields("numero").value), "")
					End If
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					If Not IsDbNull(.Fields("complement").value) Then
						lstrLogradouro = lstrLogradouro & IIf(Len(Trim(.Fields("complement").value)), " - " & Trim(.Fields("complement").value), "")
					End If
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					If Not IsDbNull(.Fields("quadra").value) Then
						lstrLogradouro = lstrLogradouro & IIf(Len(Trim(.Fields("quadra").value)), " - Qd: " & Trim(.Fields("quadra").value), "")
					End If
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					If Not IsDbNull(.Fields("lote").value) Then
						lstrLogradouro = lstrLogradouro & IIf(Len(Trim(.Fields("lote").value)), " - Lt: " & Trim(.Fields("lote").value), "")
					End If
					lstrLogradouro = "2|" & VB.Left(lstrLogradouro, 205)
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrBairro = Trim(IIf(IsDbNull(.Fields("bairro").value), "", .Fields("bairro").value))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					'UPGRADE_WARNING: Couldn't resolve default property of object RemoveCaracteres(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					lstrCep = RemoveCaracteres(Trim(IIf(IsDbNull(.Fields("cep").value), "", .Fields("cep").value)), "-.")
					
					'        lstrClasse = Left(Trim(IIf(IsNull(.Fields("clas").Value), "0", .Fields("clas").Value)), 1)
					'        lstrClasseConsumo = BuscaClasse(lstrClasse)
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrTpLigacao = Trim(IIf(IsDbNull(.Fields("fase").value), "", .Fields("fase").value))
					Select Case UCase(lstrTpLigacao)
						Case Is = "M"
							lstrTpLigacao = "MO"
						Case Is = "B"
							lstrTpLigacao = "BI"
						Case Is = "T"
							lstrTpLigacao = "TR"
					End Select
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrDtCadastro = IIf(IsDbNull(.Fields("data_entrada").value), "1900-01-01", .Fields("data_entrada").value)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrBxRenda0402 = IIf(IsDbNull(.Fields("bx0402").value), "N", .Fields("bx0402").value)
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrStatus = IIf(IsDbNull(.Fields("status").value), "", .Fields("status").value)
					lblnVerifRegistro = (.Fields("verifRegistro").value = 0)
					
					'Verifica se há fatura, se já não foi registrada
					lstrSql = "SELECT nf.cons_id, nf.codclas " & "  FROM nf " & " WHERE (nf.codclas IN ('9104','9105','9106') " & "        OR (nf.codclas IN ('9101','9107') AND '" & lstrBxRenda0402 & "' = 'S')) " & "   AND nf.zona = '" & lstrZona & "' " & "   AND nf.livro = '" & lstrLivro & "' " & "   AND nf.conta = '" & lstrConta & "' " & "   AND nf.mesano = '" & VB6.Format(Me.dtpMesAno.value, "mm/yyyy") & "' " & "   AND nf.flgst <> 'C' " & " LIMIT 0,1"
					Call Conectar(lstrSql, lrstVerif)
					With lrstVerif
						If .RecordCount > 0 Then
							
							'Se estiver desativado, cria um cadastro para ativar
							If lstrStatus = "D" Then
								Call InsereSCS("U", "E", Me.dtpMesAno.value, lstrZLC, "Faturado como Baixa Renda")
							End If
							
							If lblnVerifRegistro Then
								
								'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
								llngConsId = IIf(IsDbNull(.Fields("cons_id").value), 0, .Fields("cons_id").value)
								'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
								lstrClasse = IIf(IsDbNull(.Fields("codclas").value), "", .Fields("codclas").value)
								'UPGRADE_WARNING: Couldn't resolve default property of object BuscaClasse(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
								lstrClasseConsumo = BuscaClasse(lstrClasse)
								
								Call InsereSCS("U", "R", Me.dtpMesAno.value, lstrZLC)
								
								'              If llngConsId <> llngConsIdNF Then
								'                llngConsId = llngConsIdNF
								'    '            Call InsereSCS("U", "A", DateSerial(Me.dtpMesAno.Year, Me.dtpMesAno.Month + 1, 1), lstrZLC, "Alteração de Beneficiário")
								'              End If
								'              If Left(lstrClasse, 1) <> "9" Then
								'                lstrClasseConsumo = BuscaClasse(lstrClasseNF)
								'                Call InsereSCS("U", "D", DateSerial(Me.dtpMesAno.Year, Me.dtpMesAno.Month + 1, 1), lstrZLC, "Alteração de Beneficiário")
								'              End If
								
								If lblnCabec Then
									lblnCabec = False
									
									lintARQ = FreeFile
									FileOpen(lintARQ, Me.txtArquivo.Text, OpenMode.Output)
									
									PrintLine(lintARQ, "<?xml version=""1.0"" encoding=""iso-8859-1""?>")
									PrintLine(lintARQ, "<SCS tipo=""REGISTRARUC"" " & "codigo_agente=""" & istrCodEmpresa & """ " & "mes=""" & VB6.Format(Me.dtpMesAno.value, "mm") & """ ano=""" & VB6.Format(Me.dtpMesAno.value, "yyyy") & """ " & "sequencial=""S" & Me.txtSequencia.Text & """>")
									PrintLine(lintARQ, vbTab & "<unidades_consumidoras>")
								End If
								
								PrintLine(lintARQ, vbTab & vbTab & "<unidade_consumidora codigo=""" & lstrZLC & """>")
								PrintLine(lintARQ, vbTab & vbTab & vbTab & "<cba>" & llngConsId & "</cba>")
								PrintLine(lintARQ, vbTab & vbTab & vbTab & "<cod_ibge_municipio>" & IIf(llngCodIbge > 0, llngCodIbge, "") & "</cod_ibge_municipio>")
								PrintLine(lintARQ, vbTab & vbTab & vbTab & "<setor_censitario>" & lstrSetorCensitario & "</setor_censitario>")
								PrintLine(lintARQ, vbTab & vbTab & vbTab & "<logradouro>" & lstrLogradouro & "</logradouro>")
								PrintLine(lintARQ, vbTab & vbTab & vbTab & "<bairro>" & lstrBairro & "</bairro>")
								PrintLine(lintARQ, vbTab & vbTab & vbTab & "<cep>" & lstrCep & "</cep>")
								PrintLine(lintARQ, vbTab & vbTab & vbTab & "<classe_consumo>" & lstrClasseConsumo & "</classe_consumo>")
								PrintLine(lintARQ, vbTab & vbTab & vbTab & "<tipo_ligacao>" & lstrTpLigacao & "</tipo_ligacao>")
								PrintLine(lintARQ, vbTab & vbTab & vbTab & "<data_cadastro>" & IIf(lstrDtCadastro = "01/01/1900", "", lstrDtCadastro) & "</data_cadastro>")
								PrintLine(lintARQ, vbTab & vbTab & vbTab & "<baixa_renda_abril_2002>" & lstrBxRenda0402 & "</baixa_renda_abril_2002>")
								PrintLine(lintARQ, vbTab & vbTab & "</unidade_consumidora>")
								
							End If
							
						End If
						If .State = 1 Then Call .Close()
					End With
					
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
					Call .MoveNext()
					System.Windows.Forms.Application.DoEvents()
				End While
				
				If Not lblnCabec Then
					PrintLine(lintARQ, vbTab & "</unidades_consumidoras>")
					PrintLine(lintARQ, "</SCS>")
					FileClose(lintARQ)
					Call MsgBox("O arquivo foi gerado com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
				Else
					Call MsgBox("Não foram encontrados registros para essa operação." & vbCrLf & "O arquivo NÃO foi gerado!", MsgBoxStyle.Information, gstrTituloMensagem)
				End If
				
			End If
			If .State = 1 Then Call .Close()
			'UPGRADE_NOTE: Object lrstUC may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
			lrstUC = Nothing
			'UPGRADE_NOTE: Object lrstVerif may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
			lrstVerif = Nothing
		End With
		
		Me.lblInfo.Text = ""
		
	End Sub
	
	Private Sub RegistrarFatura()
		Dim lstrSql As String
		Dim lrstNF As ADODB.Recordset
		
		lrstNF = New ADODB.Recordset
		
		Dim lintARQ As Short
		
		Dim lblnProximo As Boolean
		Dim lblnPrimeira As Boolean
		
		Dim llngConsId As Integer
		Dim llngUnidId As Integer
		Dim lstrZona As String
		Dim lstrLivro As String
		Dim lstrConta As String
		Dim lstrClasse As String
		
		Dim ldblConsumoFaturado As Double
		Dim ldblConsumoMedido As Double
		Dim ldblValorConsumo As Double
		
		Dim lstrMesRef As String
		Dim lstrAnoRef As String
		Dim lstrMesComp As String
		Dim lstrAnoComp As String
		
		Dim lstrBxRenda0402 As String
		Dim lstrTpFatura As String
		Dim lstrTpConsumidor As String
		
		Dim ldblVlrT1_NR As Double
		Dim ldblVlrT1_BX As Double
		Dim ldblVlrT2_NR As Double
		Dim ldblVlrT2_BX As Double
		Dim ldblVlrT3_NR As Double
		Dim ldblVlrT3_BX As Double
		Dim ldblVlrT4_NR As Double
		Dim ldblVlrT4_BX As Double
		Dim lintQtdeConsumo120 As Short
		
		Dim ldblValorReducaoRec As Double
		Dim ldblValorAumentoRec As Double
		Dim ldblMediaConsumo As Double
		
		Dim ldteRegistro As Date
		Dim ldteLeitAnt As Date
		Dim ldteLeitAtu As Date
		
		Me.lblInfo.Text = " Buscando dados..."
		Me.lblInfo.Refresh()
		
		lstrSql = "SELECT nf.cons_id, " & "       nf.unid_id, " & "       nf.mesano, " & "       nf.zona, " & "       nf.livro, " & "       nf.conta, " & "       nf.codclas, " & "       nf.mesano, " & "       nf.dataem, " & "       nf.dataleit, " & "       nf.dataleitant, " & "       MID(nf.ta,3,1) AS grupo, " & "       BuscaVlrItem(nf.itensserv,'030201008') + 0 AS kwhlido, " & "       BuscaVlrItem(nf.itensserv,'030201009') + 0 AS kwhfatur, " & "       BuscaVlrItem(nf.itensserv,'030202009') + 0 AS kwhvalor, " & "       unidb.bx0402 " & "  FROM nf LEFT JOIN unidb ON (unidb.zona = nf.zona AND unidb.livro = nf.livro AND unidb.conta = nf.conta) " & " WHERE nf.mesano = '" & VB6.Format(Me.dtpMesAno.value, "mm/yyyy") & "' " & "   AND nf.flgst <> 'C' " & "   AND (nf.codclas IN ('9104','9105','9106') " & "        OR (nf.codclas IN ('9101','9107') AND unidb.bx0402 = 'S')) " & " ORDER BY nf.zona ASC, nf.livro ASC, nf.conta ASC, nf.cons_id ASC, nf.dataleit DESC"
		Call Conectar(lstrSql, lrstNF)
		
		With lrstNF
			If .RecordCount > 0 Then
				
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount)
				
				lintARQ = FreeFile
				FileOpen(lintARQ, Me.txtArquivo.Text, OpenMode.Output)
				
				PrintLine(lintARQ, "<?xml version=""1.0"" encoding=""iso-8859-1""?>")
				PrintLine(lintARQ, "<SCS tipo=""REGISTRARFATURA"" " & "codigo_agente=""" & istrCodEmpresa & """ " & "mes=""" & VB6.Format(Me.dtpMesAno.value, "mm") & """ ano=""" & VB6.Format(Me.dtpMesAno.value, "yyyy") & """ " & "sequencial=""S" & Me.txtSequencia.Text & """>")
				PrintLine(lintARQ, vbTab & "<faturas>")
				
				lblnPrimeira = True
				
				While Not .EOF
					
					ldblVlrT1_NR = 0
					ldblVlrT1_BX = 0
					ldblVlrT2_NR = 0
					ldblVlrT2_BX = 0
					ldblVlrT3_NR = 0
					ldblVlrT3_BX = 0
					ldblVlrT4_NR = 0
					ldblVlrT4_BX = 0
					lintQtdeConsumo120 = 0
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					llngUnidId = IIf(IsDbNull(.Fields("unid_id").value), 0, .Fields("unid_id").value)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					llngConsId = IIf(IsDbNull(.Fields("cons_id").value), 0, .Fields("cons_id").value)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrZona = IIf(IsDbNull(.Fields("zona").value), "", .Fields("zona").value)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrLivro = IIf(IsDbNull(.Fields("livro").value), "", .Fields("livro").value)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrConta = IIf(IsDbNull(.Fields("conta").value), "", .Fields("conta").value)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrClasse = IIf(IsDbNull(.Fields("codclas").value), "", .Fields("codclas").value)
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldblConsumoFaturado = IIf(IsDbNull(.Fields("kwhfatur").value), 0, .Fields("kwhfatur").value)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldblConsumoMedido = IIf(IsDbNull(.Fields("kwhlido").value), 0, .Fields("kwhlido").value)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldblValorConsumo = IIf(IsDbNull(.Fields("kwhvalor").value), 0, .Fields("kwhvalor").value)
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrBxRenda0402 = IIf(IsDbNull(.Fields("bx0402").value), "N", .Fields("bx0402").value)
					lstrTpFatura = "FAT"
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldteRegistro = IIf(IsDbNull(.Fields("dataem").value), "1951-01-01", .Fields("dataem").value)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldteLeitAnt = IIf(IsDbNull(.Fields("dataleitant").value), "1951-01-01", .Fields("dataleitant").value)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldteLeitAtu = IIf(IsDbNull(.Fields("dataleit").value), "1951-01-01", .Fields("dataleit").value)
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrMesRef = VB.Left(IIf(IsDbNull(.Fields("mesano").value), "", .Fields("mesano").value), 2)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrAnoRef = VB.Right(IIf(IsDbNull(.Fields("mesano").value), "", .Fields("mesano").value), 4)
					lstrMesComp = VB6.Format(Me.dtpMesAno.value, "mm")
					lstrAnoComp = VB6.Format(Me.dtpMesAno.value, "yyyy")
					
					lblnProximo = VerificaProximo(lstrZona, lstrLivro, lstrConta, llngConsId, lrstNF)
					
					Select Case lstrClasse
						Case "9101", "9107"
							lstrTpConsumidor = "T1"
							ldblVlrT1_NR = ldblValorConsumo
							ldblVlrT1_BX = BaixaRenda(ldblConsumoFaturado, ldteLeitAnt, ldteLeitAtu)
							
						Case "9104"
							lstrTpConsumidor = "T2"
							ldblVlrT2_NR = Residencial(ldblConsumoFaturado, ldteLeitAnt, ldteLeitAtu)
							ldblVlrT2_BX = ldblValorConsumo
							If lstrBxRenda0402 = "S" Then
								ldblVlrT4_NR = Residencial(ldblConsumoFaturado, ldteLeitAnt, ldteLeitAtu)
								ldblVlrT4_BX = ldblValorConsumo
							End If
							lintQtdeConsumo120 = ChecaPicoPelaNF(llngConsId, lstrZona, lstrLivro, lstrConta, 12)
							If ldblConsumoMedido > 120 Then
								lintQtdeConsumo120 = lintQtdeConsumo120 + 1
							End If
							
						Case "9105", "9106"
							lstrTpConsumidor = "T3"
							ldblVlrT3_NR = Residencial(ldblConsumoFaturado, ldteLeitAnt, ldteLeitAtu)
							ldblVlrT3_BX = ldblValorConsumo
							If lstrBxRenda0402 = "S" Then
								ldblVlrT4_NR = Residencial(ldblConsumoFaturado, ldteLeitAnt, ldteLeitAtu)
								ldblVlrT4_BX = ldblValorConsumo
							End If
					End Select
					
					ldblValorReducaoRec = ((ldblVlrT2_NR + ldblVlrT3_NR) - (ldblVlrT2_BX + ldblVlrT3_BX)) - (ldblVlrT4_NR - ldblVlrT4_BX)
					ldblValorAumentoRec = (ldblVlrT1_NR - ldblVlrT1_BX)
					ldblMediaConsumo = MediaMensalPelaNF(llngConsId, lstrZona, lstrLivro, lstrConta, 12, 0)
					
					If lblnPrimeira Then
						PrintLine(lintARQ, vbTab & vbTab & "<fatura unidade_consumidora=""" & lstrZona & lstrLivro & lstrConta & """ cba=""" & VB6.Format(llngConsId, New String(Chr(11), CShort("0"))) & """ ano_competencia=""" & lstrAnoComp & """ mes_competencia=""" & lstrMesComp & """>")
						PrintLine(lintARQ, vbTab & vbTab & vbTab & "<caso_especial></caso_especial>")
						PrintLine(lintARQ, vbTab & vbTab & vbTab & "<movimentos>")
					End If
					
					PrintLine(lintARQ, vbTab & vbTab & vbTab & vbTab & "<movimento ano_referencia=""" & lstrAnoRef & """ mes_referencia=""" & lstrMesRef & """ tipo_fatura=""" & lstrTpFatura & """>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & vbTab & vbTab & "<consumo_medido>" & VB6.Format(ldblConsumoMedido, "0.00") & "</consumo_medido>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & vbTab & vbTab & "<consumo_faturado>" & VB6.Format(ldblConsumoFaturado, "0.00") & "</consumo_faturado>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & vbTab & vbTab & "<valor_faturado>" & VB6.Format(ldblValorConsumo, "0.00") & "</valor_faturado>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & vbTab & vbTab & "<valor_reducao_receita>" & VB6.Format(ldblValorReducaoRec, "0.00") & "</valor_reducao_receita>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & vbTab & vbTab & "<valor_aumento_receita>" & VB6.Format(ldblValorAumentoRec, "0.00") & "</valor_aumento_receita>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & vbTab & vbTab & "<media_consumo_12_meses>" & VB6.Format(ldblMediaConsumo, "0.00") & "</media_consumo_12_meses>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & vbTab & vbTab & "<qtde_consumo_12_meses_superior_120KWh>" & lintQtdeConsumo120 & "</qtde_consumo_12_meses_superior_120KWh>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & vbTab & vbTab & "<data_registro>" & VB6.Format(ldteRegistro, "dd/mm/yyyy") & "</data_registro>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & vbTab & vbTab & "<tipo_consumidor>" & lstrTpConsumidor & "</tipo_consumidor>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & vbTab & "</movimento>")
					
					If Not lblnProximo Then
						PrintLine(lintARQ, vbTab & vbTab & vbTab & "</movimentos>")
						PrintLine(lintARQ, vbTab & vbTab & "</fatura>")
						lblnPrimeira = True
					Else
						lblnPrimeira = False
					End If
					
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
					Call .MoveNext()
					System.Windows.Forms.Application.DoEvents()
				End While
				
				PrintLine(lintARQ, vbTab & "</faturas>")
				PrintLine(lintARQ, "</SCS>")
				FileClose(lintARQ)
				Call MsgBox("O arquivo foi gerado com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
				
			Else
				Call MsgBox("Não foram encontrados registros para essa operação." & vbCrLf & "O arquivo NÃO foi gerado!", MsgBoxStyle.Information, gstrTituloMensagem)
			End If
			
			If .State = 1 Then Call .Close()
		End With
		
		Me.lblInfo.Text = ""
		
	End Sub
	
	Private Sub AlterarBeneficiario()
		Dim lstrSql As String
		Dim lrstBenef As ADODB.Recordset
		lrstBenef = New ADODB.Recordset
		
		Dim lblnBxRenda As Boolean
		
		Dim llngConsId As Integer
		Dim lstrCpf As String
		Dim lstrNisCod As String
		Dim lstrNome As String
		Dim lstrDtNascto As String
		Dim lstrDtBolsaFam As String
		Dim lstrDtAutoDecl As String
		Dim lstrCodIbge As String
		Dim lstrPaisNacto As String
		Dim lstrUfNascto As String
		Dim lstrTitEleitor As String
		Dim lstrNrId As String
		Dim lstrEmissorId As String
		Dim lstrUfId As String
		Dim lstrOutroDoc As String
		Dim lstrEmissorOutroDoc As String
		Dim lstrSexo As String
		Dim lstrDtCad As String
		
		Dim lstrMotivo As String
		Dim lstrDtAlteracao As String
		
		Dim lintARQ As Short
		
		Me.lblInfo.Text = " Buscando dados..."
		Me.lblInfo.Refresh()
		
		lstrSql = "SELECT consum.consreg, " & "       consum.cgc, " & "       nis.nis, " & "       consum.nome, " & "       consum.dt_nascto, " & "       nis.data_nascimento, " & "       nis.uf_nascimento, " & "       consum.rg, " & "       nis.data_cadastro, " & "       consum.dt_cadastro, " & "       scs.`desc`, " & "       scs.dtregistro " & "  FROM scs, consum LEFT JOIN nis ON (nis.cpf = consum.cgc) " & " WHERE scs.tpoperacao = 'B' AND scs.tpacao = 'A' AND consum.consreg = scs.identificador " & "   AND consum.consreg = scs.identificador AND DATE_FORMAT(scs.dtregistro, '%m/%Y') = '" & VB6.Format(Me.dtpMesAno.value, "mm/yyyy") & "' "
		Call Conectar(lstrSql, lrstBenef)
		
		With lrstBenef
			If .RecordCount > 0 Then
				
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount)
				
				lintARQ = FreeFile
				FileOpen(lintARQ, Me.txtArquivo.Text, OpenMode.Output)
				
				PrintLine(lintARQ, "<?xml version=""1.0"" encoding=""iso-8859-1""?>")
				PrintLine(lintARQ, "<SCS tipo=""ALTERARBENEFICIARIO"" " & "codigo_agente=""" & istrCodEmpresa & """ " & "mes=""" & VB6.Format(Me.dtpMesAno.value, "mm") & """ ano=""" & VB6.Format(Me.dtpMesAno.value, "yyyy") & """ " & "sequencial=""S" & Me.txtSequencia.Text & """>")
				PrintLine(lintARQ, vbTab & "<beneficiarios>")
				
				
				While Not .EOF
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					llngConsId = IIf(IsDbNull(.Fields("consreg").value), 0, .Fields("consreg").value)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrCpf = Trim(IIf(IsDbNull(.Fields("cgc").value), "", .Fields("cgc").value))
					If Len(lstrCpf) > 11 Then lstrCpf = ""
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					'UPGRADE_WARNING: Couldn't resolve default property of object RemoveCaracteres(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					lstrNisCod = VB.Left(RemoveCaracteres(Trim(IIf(IsDbNull(.Fields("nis").value), "", .Fields("nis").value)), "/-."), 11)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrNome = Replace(RetiraAcentuacao(VB.Left(Trim(IIf(IsDbNull(.Fields("nome").value), "", .Fields("nome").value)), 100)), "&", "E")
					If Len(lstrNisCod) > 0 Then
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrDtNascto = VB6.Format(IIf(IsDbNull(.Fields("data_nascimento").value), "1900-01-01", .Fields("data_nascimento").value), "dd/mm/yyyy")
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrDtBolsaFam = VB6.Format(IIf(IsDbNull(.Fields("data_cadastro").value), "1900-01-01", .Fields("data_cadastro").value), "dd/mm/yyyy")
						lstrDtAutoDecl = VB6.Format("1900-01-01", "dd/mm/yyyy")
					Else
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrDtNascto = VB6.Format(IIf(IsDbNull(.Fields("dt_nascto").value), "1900-01-01", .Fields("dt_nascto").value), "dd/mm/yyyy")
						lstrDtBolsaFam = VB6.Format("1900-01-01", "dd/mm/yyyy")
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrDtAutoDecl = VB6.Format(IIf(IsDbNull(.Fields("data_cadastro").value), "1900-01-01", .Fields("data_cadastro").value), "dd/mm/yyyy")
					End If
					lstrCodIbge = ""
					lstrPaisNacto = ""
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrUfNascto = Trim(IIf(IsDbNull(.Fields("uf_nascimento").value), "", .Fields("uf_nascimento").value))
					lstrTitEleitor = ""
					lstrNrId = ""
					lstrEmissorId = ""
					lstrUfId = ""
					lstrOutroDoc = ""
					lstrEmissorOutroDoc = ""
					lstrSexo = ""
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrDtCad = IIf(IsDbNull(.Fields("dt_cadastro").value), "1900-01-01", .Fields("dt_cadastro").value)
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrMotivo = VB.Left(IIf(IsDbNull(.Fields("desc").value), "", .Fields("desc").value), 100)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrDtAlteracao = VB6.Format(IIf(IsDbNull(.Fields("dtregistro").value), VB6.Format(Me.dtpMesAno.value, "yyyy-mm") & "-01", .Fields("dtregistro").value), "dd/mm/yyyy")
					
					PrintLine(lintARQ, vbTab & vbTab & "<beneficiario cba=""" & llngConsId & """>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<cpf>" & lstrCpf & "</cpf>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<codigo_nis>" & lstrNisCod & "</codigo_nis>")
					'UPGRADE_WARNING: Couldn't resolve default property of object RemoveCaracteres(lstrNome, ./\-()). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<nome>" & RemoveCaracteres(lstrNome, "./\-()") & "</nome>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<data_nascimento>" & IIf(lstrDtNascto = "01/01/1900", "", lstrDtNascto) & "</data_nascimento>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<cod_ibge_municipio>" & lstrCodIbge & "</cod_ibge_municipio>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<pais_nascimento>" & lstrPaisNacto & "</pais_nascimento>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<uf_nascimento>" & lstrUfNascto & "</uf_nascimento>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<titulo_eleitor>" & lstrTitEleitor & "</titulo_eleitor>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<identidade >")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & vbTab & "<numero>" & lstrNrId & "</numero>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & vbTab & "<emissor>" & lstrEmissorId & "</emissor>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & vbTab & "<estado>" & lstrUfId & "</estado>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "</identidade>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<outro_documento>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & vbTab & "<numero>" & lstrOutroDoc & "</numero>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & vbTab & "<emissor>" & lstrEmissorOutroDoc & "</emissor>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "</outro_documento>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<sexo>" & lstrSexo & "</sexo>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<data_bolsa_familia>" & IIf(lstrDtBolsaFam = "01/01/1900", "", lstrDtBolsaFam) & "</data_bolsa_familia>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<data_autodeclarado>" & IIf(lstrDtAutoDecl = "01/01/1900", "", lstrDtAutoDecl) & "</data_autodeclarado>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<data_cadastro>" & IIf(lstrDtCad = "01/01/1900", "", lstrDtCad) & "</data_cadastro>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<motivo>" & lstrMotivo & "</motivo>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<data_alteracao>" & lstrDtAlteracao & "</data_alteracao>")
					PrintLine(lintARQ, vbTab & vbTab & "</beneficiario>")
					
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
					Call .MoveNext()
					System.Windows.Forms.Application.DoEvents()
				End While
				
				PrintLine(lintARQ, vbTab & "</beneficiarios>")
				PrintLine(lintARQ, "</SCS>")
				FileClose(lintARQ)
				Call MsgBox("O arquivo foi gerado com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
			Else
				Call MsgBox("Não foram encontrados registros para essa operação." & vbCrLf & "O arquivo NÃO foi gerado!", MsgBoxStyle.Information, gstrTituloMensagem)
			End If
			If .State = 1 Then Call .Close()
		End With
		
		Me.lblInfo.Text = ""
		
	End Sub
	
	
	Private Sub AlterarUC()
		Dim lstrSql As String
		Dim lrstUC As ADODB.Recordset
		lrstUC = New ADODB.Recordset
		
		Dim lintARQ As Short
		
		Dim llngConsId As Integer
		Dim llngUnidId As Integer
		Dim lstrZona As String
		Dim lstrLivro As String
		Dim lstrConta As String
		Dim lstrZLC As String
		Dim lstrCodEnd As String
		Dim llngCodIbge As Integer
		Dim lstrSetorCensitario As String
		Dim lstrLogradouro As String
		Dim lstrBairro As String
		Dim lstrCep As String
		Dim lstrClasseConsumo As String
		Dim lstrTpLigacao As String
		Dim lstrDtCadastro As String
		Dim lstrBxRenda0402 As String
		
		Dim lstrMotivo As String
		Dim lstrDtAlteracao As String
		
		Me.lblInfo.Text = " Buscando dados..."
		Me.lblInfo.Refresh()
		
		lstrSql = "SELECT scs.`desc`, " & "       scs.dtregistro, " & "       unidb.unid_id, " & "       unidb.cons_id, " & "       unidb.zona, " & "       unidb.livro, " & "       unidb.conta, " & "       unidb.`end`, " & "       unidb.numero, " & "       unidb.complement, " & "       unidb.quadra, " & "       unidb.lote, " & "       unidb.bairro, " & "       unidb.cidade, " & "       unidb.cep, " & "       unidb.clas, " & "       unidb.fase, " & "       unidb.data_entrada, " & "       unidb.bx0402, " & "       unidb.codend " & "  FROM unidb, scs " & " WHERE CONCAT(unidb.zona, unidb.livro, unidb.conta) = scs.identificador " & "   AND scs.tpoperacao = 'U' AND tpacao = 'A' AND DATE_FORMAT(scs.dtregistro, '%m/%Y') = '" & VB6.Format(Me.dtpMesAno.value, "mm/yyyy") & "'"
		Call Conectar(lstrSql, lrstUC)
		
		With lrstUC
			If .RecordCount > 0 Then
				
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount)
				
				lintARQ = FreeFile
				FileOpen(lintARQ, Me.txtArquivo.Text, OpenMode.Output)
				
				PrintLine(lintARQ, "<?xml version=""1.0"" encoding=""iso-8859-1""?>")
				PrintLine(lintARQ, "<SCS tipo=""ALTERARUC"" " & "codigo_agente=""" & istrCodEmpresa & """ " & "mes=""" & VB6.Format(Me.dtpMesAno.value, "mm") & """ ano=""" & VB6.Format(Me.dtpMesAno.value, "yyyy") & """ " & "sequencial=""S" & Me.txtSequencia.Text & """>")
				PrintLine(lintARQ, vbTab & "<unidades_consumidoras>")
				
				While Not .EOF
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					llngUnidId = IIf(IsDbNull(.Fields("unid_id").value), 0, .Fields("unid_id").value)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					llngConsId = IIf(IsDbNull(.Fields("cons_id").value), 0, .Fields("cons_id").value)
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrZona = VB.Right("000" & Trim(IIf(IsDbNull(.Fields("zona").value), "", .Fields("zona").value)), 3)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrLivro = VB.Right("00" & Trim(IIf(IsDbNull(.Fields("livro").value), "", .Fields("livro").value)), 2)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrConta = VB.Right("000000" & Trim(IIf(IsDbNull(.Fields("conta").value), "", .Fields("conta").value)), 6)
					lstrZLC = lstrZona & lstrLivro & lstrConta
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrCodEnd = Trim(IIf(IsDbNull(.Fields("codend").value), "000000", .Fields("codend").value))
					llngCodIbge = CInt(BuscaIbge( , lstrCodEnd))
					lstrSetorCensitario = ""
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrLogradouro = Trim(IIf(IsDbNull(.Fields("end").value), "", .Fields("end").value))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					If Not IsDbNull(.Fields("numero").value) Then
						lstrLogradouro = lstrLogradouro & IIf(Len(Trim(.Fields("numero").value)), ", " & Trim(.Fields("numero").value), "")
					End If
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					If Not IsDbNull(.Fields("complement").value) Then
						lstrLogradouro = lstrLogradouro & IIf(Len(Trim(.Fields("complement").value)), " - " & Trim(.Fields("complement").value), "")
					End If
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					If Not IsDbNull(.Fields("quadra").value) Then
						lstrLogradouro = lstrLogradouro & IIf(Len(Trim(.Fields("quadra").value)), " - Qd: " & Trim(.Fields("quadra").value), "")
					End If
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					If Not IsDbNull(.Fields("lote").value) Then
						lstrLogradouro = lstrLogradouro & IIf(Len(Trim(.Fields("lote").value)), " - Lt: " & Trim(.Fields("lote").value), "")
					End If
					lstrLogradouro = "2|" & VB.Left(lstrLogradouro, 205)
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrBairro = Trim(IIf(IsDbNull(.Fields("bairro").value), "", .Fields("bairro").value))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					'UPGRADE_WARNING: Couldn't resolve default property of object RemoveCaracteres(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					lstrCep = RemoveCaracteres(Trim(IIf(IsDbNull(.Fields("cep").value), "", .Fields("cep").value)), "-.")
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrClasseConsumo = VB.Left(Trim(IIf(IsDbNull(.Fields("clas").value), "0", .Fields("clas").value)), 1)
					'UPGRADE_WARNING: Couldn't resolve default property of object BuscaClasse(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					lstrClasseConsumo = BuscaClasse(lstrClasseConsumo)
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrTpLigacao = Trim(IIf(IsDbNull(.Fields("fase").value), "", .Fields("fase").value))
					Select Case UCase(lstrTpLigacao)
						Case Is = "M"
							lstrTpLigacao = "MO"
						Case Is = "B"
							lstrTpLigacao = "BI"
						Case Is = "T"
							lstrTpLigacao = "TR"
					End Select
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrDtCadastro = IIf(IsDbNull(.Fields("data_entrada").value), "1900-01-01", .Fields("data_entrada").value)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrBxRenda0402 = IIf(IsDbNull(.Fields("bx0402").value), "N", .Fields("bx0402").value)
					
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrMotivo = VB.Left(IIf(IsDbNull(.Fields("desc").value), "", .Fields("desc").value), 100)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrDtAlteracao = VB6.Format(IIf(IsDbNull(.Fields("dtregistro").value), VB6.Format(Me.dtpMesAno.value, "yyyy-mm") & "-01", .Fields("dtregistro").value), "dd/mm/yyyy")
					
					PrintLine(lintARQ, vbTab & vbTab & "<unidade_consumidora codigo=""" & lstrZLC & """>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<cba>" & llngConsId & "</cba>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<cod_ibge_municipio>" & IIf(llngCodIbge > 0, llngCodIbge, "") & "</cod_ibge_municipio>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<setor_censitario>" & lstrSetorCensitario & "</setor_censitario>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<logradouro>" & lstrLogradouro & "</logradouro>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<bairro>" & lstrBairro & "</bairro>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<cep>" & lstrCep & "</cep>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<classe_consumo>" & lstrClasseConsumo & "</classe_consumo>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<tipo_ligacao>" & lstrTpLigacao & "</tipo_ligacao>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<data_cadastro>" & IIf(lstrDtCadastro = "01/01/1900", "", lstrDtCadastro) & "</data_cadastro>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<baixa_renda_abril_2002>" & lstrBxRenda0402 & "</baixa_renda_abril_2002>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<motivo>" & lstrMotivo & "</motivo>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<data_alteracao>" & lstrDtAlteracao & "</data_alteracao>")
					PrintLine(lintARQ, vbTab & vbTab & "</unidade_consumidora>")
					
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
					Call .MoveNext()
					System.Windows.Forms.Application.DoEvents()
				End While
				
				PrintLine(lintARQ, vbTab & "</unidades_consumidoras>")
				PrintLine(lintARQ, "</SCS>")
				FileClose(lintARQ)
				Call MsgBox("O arquivo foi gerado com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
			Else
				Call MsgBox("Não foram encontrados registros para essa operação." & vbCrLf & "O arquivo NÃO foi gerado!", MsgBoxStyle.Information, gstrTituloMensagem)
			End If
			If .State = 1 Then Call .Close()
		End With
		
		Me.lblInfo.Text = ""
		
	End Sub
	
	Private Sub DesativarUC()
		Dim lstrSql As String
		Dim lrstSCS As ADODB.Recordset
		lrstSCS = New ADODB.Recordset
		
		Dim lintARQ As Short
		
		Dim llngConsId As Integer
		Dim lstrZLC As String
		Dim lstrMotivo As String
		Dim lstrDtDesativacao As String
		
		Me.lblInfo.Text = " Buscando dados..."
		Me.lblInfo.Refresh()
		
		lstrSql = "SELECT scs.dtregistro, " & "       scs.identificador, " & "       scs.`desc`, " & "       unidb.cons_id " & "  FROM unidb, scs " & " WHERE CONCAT(unidb.zona, unidb.livro, unidb.conta) = scs.identificador " & "   AND scs.tpoperacao = 'U' AND tpacao = 'D' AND DATE_FORMAT(scs.dtregistro, '%m/%Y') = '" & VB6.Format(Me.dtpMesAno.value, "mm/yyyy") & "'"
		Call Conectar(lstrSql, lrstSCS)
		
		With lrstSCS
			If .RecordCount > 0 Then
				
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount)
				
				lintARQ = FreeFile
				FileOpen(lintARQ, Me.txtArquivo.Text, OpenMode.Output)
				
				PrintLine(lintARQ, "<?xml version=""1.0"" encoding=""iso-8859-1""?>")
				PrintLine(lintARQ, "<SCS tipo=""DESATIVARUC"" " & "codigo_agente=""" & istrCodEmpresa & """ " & "mes=""" & VB6.Format(Me.dtpMesAno.value, "mm") & """ ano=""" & VB6.Format(Me.dtpMesAno.value, "yyyy") & """ " & "sequencial=""S" & Me.txtSequencia.Text & """>")
				PrintLine(lintARQ, vbTab & "<unidades_consumidoras>")
				
				While Not .EOF
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					llngConsId = IIf(IsDbNull(.Fields("cons_id").value), 0, .Fields("cons_id").value)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrZLC = IIf(IsDbNull(.Fields("identificador").value), "00000000000", .Fields("identificador").value)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrMotivo = VB.Left(IIf(IsDbNull(.Fields("desc").value), "", .Fields("desc").value), 100)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrDtDesativacao = VB6.Format(IIf(IsDbNull(.Fields("dtregistro").value), VB6.Format(Me.dtpMesAno.value, "yyyy-mm") & "-01", .Fields("dtregistro").value), "dd/mm/yyyy")
					
					PrintLine(lintARQ, vbTab & vbTab & "<unidade_consumidora codigo=""" & lstrZLC & """>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<cba>" & llngConsId & "</cba>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<motivo>" & lstrMotivo & "</motivo>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<data_desativacao>" & lstrDtDesativacao & "</data_desativacao>")
					PrintLine(lintARQ, vbTab & vbTab & "</unidade_consumidora>")
					
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
					Call .MoveNext()
					System.Windows.Forms.Application.DoEvents()
				End While
				
				PrintLine(lintARQ, vbTab & "</unidades_consumidoras>")
				PrintLine(lintARQ, "</SCS>")
				FileClose(lintARQ)
				Call MsgBox("O arquivo foi gerado com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
			Else
				Call MsgBox("Não foram encontrados registros para essa operação." & vbCrLf & "O arquivo NÃO foi gerado!", MsgBoxStyle.Information, gstrTituloMensagem)
			End If
			If .State = 1 Then Call .Close()
			'UPGRADE_NOTE: Object lrstSCS may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
			lrstSCS = Nothing
		End With
		
		Me.lblInfo.Text = ""
		
	End Sub
	
	Private Sub ReativarUC()
		Dim lstrSql As String
		Dim lrstSCS As ADODB.Recordset
		lrstSCS = New ADODB.Recordset
		
		Dim lintARQ As Short
		
		Dim llngConsId As Integer
		Dim lstrZLC As String
		Dim lstrMotivo As String
		Dim lstrDtReativacao As String
		
		Me.lblInfo.Text = " Buscando dados..."
		Me.lblInfo.Refresh()
		
		lstrSql = "SELECT scs.dtregistro, " & "       scs.identificador, " & "       scs.`desc`, " & "       unidb.cons_id " & "  FROM unidb, scs " & " WHERE CONCAT(unidb.zona, unidb.livro, unidb.conta) = scs.identificador " & "   AND scs.tpoperacao = 'U' AND tpacao = 'E' AND DATE_FORMAT(scs.dtregistro, '%m/%Y') = '" & VB6.Format(Me.dtpMesAno.value, "mm/yyyy") & "'"
		Call Conectar(lstrSql, lrstSCS)
		
		With lrstSCS
			If .RecordCount > 0 Then
				
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount)
				
				lintARQ = FreeFile
				FileOpen(lintARQ, Me.txtArquivo.Text, OpenMode.Output)
				
				PrintLine(lintARQ, "<?xml version=""1.0"" encoding=""iso-8859-1""?>")
				PrintLine(lintARQ, "<SCS tipo=""REATIVARUC"" " & "codigo_agente=""" & istrCodEmpresa & """ " & "mes=""" & VB6.Format(Me.dtpMesAno.value, "mm") & """ ano=""" & VB6.Format(Me.dtpMesAno.value, "yyyy") & """ " & "sequencial=""S" & Me.txtSequencia.Text & """>")
				PrintLine(lintARQ, vbTab & "<unidades_consumidoras>")
				
				While Not .EOF
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					llngConsId = IIf(IsDbNull(.Fields("cons_id").value), 0, .Fields("cons_id").value)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrZLC = IIf(IsDbNull(.Fields("identificador").value), "00000000000", .Fields("identificador").value)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrMotivo = VB.Left(IIf(IsDbNull(.Fields("desc").value), "", .Fields("desc").value), 100)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrDtReativacao = VB6.Format(IIf(IsDbNull(.Fields("dtregistro").value), VB6.Format(Me.dtpMesAno.value, "yyyy-mm") & "-01", .Fields("dtregistro").value), "dd/mm/yyyy")
					
					PrintLine(lintARQ, vbTab & vbTab & "<unidade_consumidora codigo=""" & lstrZLC & """>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<cba>" & llngConsId & "</cba>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<motivo>" & lstrMotivo & "</motivo>")
					PrintLine(lintARQ, vbTab & vbTab & vbTab & "<data_reativacao>" & lstrDtReativacao & "</data_reativacao>")
					PrintLine(lintARQ, vbTab & vbTab & "</unidade_consumidora>")
					
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
					Call .MoveNext()
					System.Windows.Forms.Application.DoEvents()
				End While
				
				PrintLine(lintARQ, vbTab & "</unidades_consumidoras>")
				PrintLine(lintARQ, "</SCS>")
				FileClose(lintARQ)
				Call MsgBox("O arquivo foi gerado com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
			Else
				Call MsgBox("Não foram encontrados registros para essa operação." & vbCrLf & "O arquivo NÃO foi gerado!", MsgBoxStyle.Information, gstrTituloMensagem)
			End If
			If .State = 1 Then Call .Close()
			'UPGRADE_NOTE: Object lrstSCS may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
			lrstSCS = Nothing
		End With
		
		Me.lblInfo.Text = ""
		
	End Sub
	
	
	Private Function PreparaArquivo(ByRef aintArq As Short, ByVal astrOper As String) As Object
		Dim lstrNMArq As String
		On Error GoTo ErroArquivo
		lstrNMArq = Me.txtArquivo.Text
		If astrOper = "O" Then
			Me.lblInfo.Text = "Gerando o arquivo XML..."
			Me.lblInfo.Refresh()
			aintArq = FreeFile
			FileOpen(aintArq, lstrNMArq, OpenMode.Output)
			PrintLine(aintArq, "<?xml version=""1.0"" encoding=""iso-8859-1"" ?>")
			PrintLine(aintArq, "<SCS tipo=""" & "REGISTRARBENEFICIARIO" & """ " & "codigo_agente=""" & "1234" & """ " & "mes=""" & VB6.Format(Me.dtpMesAno.Month, "00") & """ " & "ano=""" & VB6.Format(Me.dtpMesAno.Year, "0000") & """ " & "sequencial=""S" & VB6.Format(Me.txtSequencia.Text, "000") & """>")
		ElseIf astrOper = "C" Then 
			PrintLine(aintArq, "</SCS>")
			FileClose(aintArq)
			Me.lblInfo.Text = " Arquivo gerado com sucesso!"
			Me.lblInfo.Refresh()
		End If
		Exit Function
ErroArquivo: 
		Select Case Err.Number
			Case 76
				Call MsgBox("Não foi possível encontrar o caminho para gravar o arquivo!", MsgBoxStyle.Exclamation, gstrTituloMensagem)
			Case Else
				Call MsgBox(ErrorToString(), MsgBoxStyle.Exclamation, gstrTituloMensagem)
		End Select
	End Function
	
	Private Sub cmdArquivo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdArquivo.Click
		Me.dialArqOpen.DefaultExt = "xml"
		'UPGRADE_WARNING: Filter has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		Me.dialArqOpen.Filter = "*." & Me.dialArqOpen.DefaultExt
		Me.dialArqOpen.FileName = istrPrefNmArq & Replace(Me.cmbOperacao.Text, " ", "") & "_" & VB6.Format(Me.dtpMesAno.value, "mmyyyy") & "_S" & Me.txtSequencia.Text & "." & Me.dialArqOpen.DefaultExt
		Me.dialArqOpen.Title = "Nome do arquivo"
		Me.dialArqOpen.ShowDialog()
		Me.txtArquivo.Text = Me.dialArqOpen.FileName
	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 cmdGerar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdGerar.Click
		'  Dim lstrLinha As String
		
		
		Call LigDes(False)
		Me.Cursor = System.Windows.Forms.Cursors.AppStarting
		
		Select Case Me.cmbOperacao.Text
			Case "REGISTRAR UC"
				Call RegistrarUC()
			Case "ALTERAR UC"
				Call AlterarUC()
			Case "DESATIVAR UC"
				Call DesativarUC()
			Case "REATIVAR UC"
				Call ReativarUC()
			Case "REGISTRAR BENEFICIARIO"
				Call RegistrarBeneficiario()
			Case "ALTERAR BENEFICIARIO"
				Call AlterarBeneficiario()
			Case "DESATIVAR BENEFICIARIO"
			Case "REATIVAR BENEFICIARIO"
			Case "TROCAR BENEFICIARIOUC"
			Case "REGISTRAR FATURA"
				Call RegistrarFatura()
			Case "ALTERAR FATURA"
			Case "REMOVER FATURA"
		End Select
		
		Me.Cursor = System.Windows.Forms.Cursors.Arrow
		Call LigDes(True)
		
		
		
		'  Dim lstrTab   As String
		'  lstrTab = "   "
		''  Call RegistrarBeneficiario(lstrTab)
		'Call RegistrarUnidades(lstrTab)
		
		'  Select Case istrOperacao
		'    Case "REGISTRARUC"
		'      Call RegistrarUnidades(lstrTab)
		'    Case "ALTERARUC"
		'    Case "DESATIVARUC"
		'    Case "REATIVARUC"
		'    Case "REGISTRARBENEFICIARIO"
		'      Call RegistrarBeneficiario(lstrTab)
		'    Case "ALTERARBENEFICIARIO"
		'    Case "DESATIVARBENEFICIARIO"
		'    Case "REATIVARBENEFICIARIO"
		'    Case "TROCARBENEFICIARIOUC"
		'    Case "REGISTRARFATURA"
		'      Call RegistrarFaturas(lstrTab)
		'    Case "ALTERARFATURA"
		'    Case "REMOVERFATURA"
		'  End Select
		'
		'  Me.MousePointer = vbDefault
	End Sub
	
	Private Sub dtpMesAno_Change(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles dtpMesAno.Change
		Call RedefineNomeArq()
	End Sub
	
	Private Sub frmEmissaoSubvencao_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Dim lstrCodEmpresa As String
		
		Me.Icon = FrmAtPrincipal.Icon
		
		istrCodEmpresa = VB.Right("0000" & Trim(LeConfig("EMPRESA_COD_RES89")), 4)
		istrPrefNmArq = "APLSCS" & istrCodEmpresa
		
		Me.txtSequencia.Text = "001"
		Me.dtpMesAno.value = VB6.Format(Now, "dd/mm/yyyy")
		
		'Adiciona os itens na combo Operação
		With Me.cmbOperacao
			Call .Items.Add("REGISTRAR UC")
			Call .Items.Add("ALTERAR UC")
			Call .Items.Add("DESATIVAR UC")
			Call .Items.Add("REATIVAR UC")
			Call .Items.Add("REGISTRAR BENEFICIARIO")
			Call .Items.Add("ALTERAR BENEFICIARIO")
			'Call .AddItem("DESATIVAR BENEFICIARIO")
			'Call .AddItem("REATIVAR BENEFICIARIO")
			'Call .AddItem("TROCAR BENEFICIARIO UC")
			Call .Items.Add("REGISTRAR FATURA")
			'Call .AddItem("ALTERAR FATURA")
			'Call .AddItem("REMOVER FATURA")
			.SelectedIndex = 0
		End With
		
		Me.txtArquivo.Text = My.Application.Info.DirectoryPath & "\" & istrPrefNmArq & Replace(Me.cmbOperacao.Text, " ", "") & "_" & VB6.Format(Me.dtpMesAno.value, "mmyyyy") & "_S" & Me.txtSequencia.Text & ".xml"
		
	End Sub
	
	Private Sub RedefineNomeArq()
		Me.txtArquivo.Text = VB.Left(Me.txtArquivo.Text, InStrRev(Me.txtArquivo.Text, "\")) & istrPrefNmArq & Replace(Me.cmbOperacao.Text, " ", "") & "_" & VB6.Format(Me.dtpMesAno.value, "mmyyyy") & "_S" & Me.txtSequencia.Text & ".xml"
	End Sub
	
	'UPGRADE_WARNING: Event txtSequencia.TextChanged 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 txtSequencia_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtSequencia.TextChanged
		Call RedefineNomeArq()
	End Sub
	
	Private Sub txtSequencia_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtSequencia.Enter
		With Me.txtSequencia
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	End Sub
	
	Private Sub txtSequencia_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtSequencia.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		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
	End Sub
	
	Private Sub txtSequencia_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtSequencia.Leave
		With Me.txtSequencia
			.Text = VB6.Format(.Text, "000")
		End With
	End Sub
	
	Private Function MediaMensalPelaNF(ByVal alngConsId As Integer, ByVal astrZona As String, ByVal astrLivro As String, ByVal astrConta As String, ByVal aintNMeses As Short, ByVal adblConstanteMedidor As Double) As Double
		Dim lblnMesmoConsId As Boolean
		Dim llngConsId As Integer
		Dim ldblMedia As Double
		Dim ldblConsumo As Double
		Dim ldblAcumulado As Double
		Dim lintDivisor As Short
		Dim lstrSql As String
		Dim llngMesano As Integer
		Dim lrstConsulta As ADODB.Recordset
		lrstConsulta = New ADODB.Recordset
		llngMesano = CInt(VB6.Format(Me.dtpMesAno._Value, "yyyymm"))
		lstrSql = "SELECT (buscavlritem(itensserv,'030201008')+0) AS consumolido, cons_id " & "  FROM nf " & " WHERE zona = '" & astrZona & "' " & "   AND livro = '" & astrLivro & "' " & "   AND conta = '" & astrConta & "' " & "   AND CONCAT(RIGHT(mesano,4),LEFT(mesano,2)) <= " & llngMesano & "   AND flgst <> 'C' " & " ORDER BY CONCAT(RIGHT(mesano,4),LEFT(mesano,2)) DESC " & " LIMIT 0, " & aintNMeses
		Call Conectar(lstrSql, lrstConsulta)
		With lrstConsulta
			ldblAcumulado = 0
			lintDivisor = 0
			lblnMesmoConsId = True
			If .RecordCount > 0 Then
				'lintDivisor = .RecordCount
				While Not .EOF And lblnMesmoConsId
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					llngConsId = IIf(IsDbNull(.Fields("cons_id").value), 0, .Fields("cons_id").value)
					If (llngConsId <> alngConsId) Then
						lblnMesmoConsId = False
					Else
						lintDivisor = lintDivisor + 1
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						ldblConsumo = IIf(IsDbNull(.Fields("consumolido").value), 0, .Fields("consumolido").value)
						ldblAcumulado = ldblAcumulado + ldblConsumo
						Call .MoveNext()
					End If
				End While
			Else
				ldblMedia = 0
			End If
			If .State = 1 Then Call .Close()
		End With
		
		If lintDivisor > 1 Then
			ldblMedia = ldblAcumulado / lintDivisor
			If adblConstanteMedidor <> 0 Then
				ldblMedia = Arredondar(ldblMedia / adblConstanteMedidor, 0)
				ldblMedia = ldblMedia * adblConstanteMedidor
			End If
		Else
			ldblMedia = 0
		End If
		MediaMensalPelaNF = Arredondar(ldblMedia, 2)
		'UPGRADE_NOTE: Object lrstConsulta may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		lrstConsulta = Nothing
	End Function
	
	Private Function ChecaPicoPelaNF(ByVal alngConsId As Object, ByVal astrZona As String, ByVal astrLivro As String, ByVal astrConta As String, ByVal aintNMeses As Short) As Integer
		Dim lblnMesmoConsId As Boolean
		Dim llngConsId As Integer
		Dim ldblConsumo As Double
		Dim llngAcumulado As Integer
		Dim lstrSql As String
		Dim llngMesano As Integer
		Dim lrstConsulta As ADODB.Recordset
		lrstConsulta = New ADODB.Recordset
		llngMesano = CInt(VB6.Format(Me.dtpMesAno._Value, "yyyymm"))
		lstrSql = "SELECT (buscavlritem(itensserv, '030201008')+0) AS consumolido, cons_id " & "  FROM nf " & " WHERE zona = '" & astrZona & "' " & "   AND livro = '" & astrLivro & "' " & "   AND conta = '" & astrConta & "' " & "   AND mesano_ref < " & llngMesano & "   AND flgst <> 'C' " & " ORDER BY mesano_ref DESC " & " LIMIT 0, " & aintNMeses
		Call Conectar(lstrSql, lrstConsulta)
		With lrstConsulta
			llngAcumulado = 0
			lblnMesmoConsId = True
			If .RecordCount > 0 Then
				While Not .EOF And lblnMesmoConsId
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					llngConsId = IIf(IsDbNull(.Fields("cons_id").value), 0, .Fields("cons_id").value)
					'UPGRADE_WARNING: Couldn't resolve default property of object alngConsId. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					If (llngConsId <> alngConsId) Then
						lblnMesmoConsId = False
					Else
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						ldblConsumo = IIf(IsDbNull(.Fields("consumolido").value), 0, .Fields("consumolido").value)
						llngAcumulado = llngAcumulado + IIf(ldblConsumo > 120, 1, 0)
						If llngAcumulado > 1 Then Call .MoveLast()
						Call .MoveNext()
					End If
				End While
			Else
				llngAcumulado = 0
			End If
			If .State = 1 Then Call .Close()
		End With
		ChecaPicoPelaNF = llngAcumulado
		'UPGRADE_NOTE: Object lrstConsulta may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		lrstConsulta = Nothing
	End Function
	
	Private Function BaixaRenda(ByVal adblConsumo As Double, ByVal adteLeitAnt As Date, ByVal adteLeitAtu As Date) As Double
		'Rotina de tarifa revisada com as novas regras de tarifa com bandeira OK
		Dim ldteAux As Date
		'
		Dim lstrCod As String
		Dim ldblTarifa As Double
		Dim ldblValorAuxil As Double
		Dim ldblValorTotal As Double
		'
		Dim ldblLimite As Double
		Dim ldblLimiteIni As Double
		Dim ldblLimiteFim As Double
		Dim ldblAuxiliar As Double
		Dim ldblConsumo As Double
		'
		Dim lstrSql As String
		Dim lrstTarifaGrB As ADODB.Recordset
		lrstTarifaGrB = New ADODB.Recordset
		ldblAuxiliar = adblConsumo
		ldblLimiteIni = 0
		ldblConsumo = 0
		ldblValorTotal = 0
		'Pega as tarifas para calculo do Baixa Renda
		ldteAux = CDate(UltimoDiaMes((Me.dtpMesAno.Month), (Me.dtpMesAno.Year)) & "/" & VB6.Format(Me.dtpMesAno.value, "MM/YYYY"))
		lstrSql = ""
		lstrSql = lstrSql & "  SELECT * "
		lstrSql = lstrSql & "    FROM `tarifa` "
		lstrSql = lstrSql & "   WHERE `referencia` <= '" & VB6.Format(ldteAux, "yyyy-MM-dd") & "' "
		lstrSql = lstrSql & "     AND `num` BETWEEN '01B1021100' AND '01B1021199' "
		lstrSql = lstrSql & "     AND `valor` <> 0 "
		lstrSql = lstrSql & " ORDER BY `referencia` DESC, `num` ASC LIMIT 0, 4"
		Call Conectar(lstrSql, lrstTarifaGrB)
		With lrstTarifaGrB
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					While Not .EOF And ldblAuxiliar > 0
						'Busca a tarifa conforme proporcionalidade entre as data de leitura e data da tarifa
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrCod = Trim(IIf(IsDbNull(.Fields("num").value), "", .Fields("num").value))
						ldblTarifa = Arredondar(TarifaProporcional(adteLeitAnt, adteLeitAtu, lstrCod, 3) / 1000, 5)
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						ldblLimiteFim = CDbl(IIf(IsDbNull(.Fields("limitkwh").value), 0, .Fields("limitkwh").value))
						ldblLimite = ldblLimiteFim - ldblLimiteIni
						If ldblAuxiliar <= ldblLimite Then
							ldblConsumo = ldblAuxiliar
							ldblValorAuxil = Arredondar(ldblAuxiliar * ldblTarifa, 2)
							ldblAuxiliar = 0
						Else
							ldblConsumo = ldblLimite
							ldblValorAuxil = Arredondar(ldblLimite * ldblTarifa, 2)
							ldblAuxiliar = ldblAuxiliar - ldblLimite
						End If
						ldblValorTotal = ldblValorTotal + ldblValorAuxil
						ldblLimiteIni = ldblLimiteFim
						Call .MoveNext()
					End While
				End If
				Call .Close()
			End If
		End With
		'UPGRADE_NOTE: Object lrstTarifaGrB may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		lrstTarifaGrB = Nothing
		BaixaRenda = ldblValorTotal
	End Function
	
	Private Function Residencial(ByVal adblConsumo As Double, ByVal adteLeitAnt As Date, ByVal adteLeitAtu As Date) As Double
		'Rotina de tarifa revisada com as novas regras de tarifa com bandeira OK
		Dim lstrCod As String
		Dim ldteAux As Date
		Dim lstrSql As String
		Dim ldblTarifa As Double
		Dim lrstTarifaGrB As ADODB.Recordset
		lrstTarifaGrB = New ADODB.Recordset
		'Pega a tarifa do Residencial Normal
		ldteAux = CDate(UltimoDiaMes((Me.dtpMesAno.Month), (Me.dtpMesAno.Year)) & "/" & VB6.Format(Me.dtpMesAno.value, "MM/YYYY"))
		lstrSql = ""
		lstrSql = lstrSql & "  SELECT * "
		lstrSql = lstrSql & "    FROM `tarifa` "
		lstrSql = lstrSql & "   WHERE `referencia` <= '" & VB6.Format(ldteAux, "yyyy-MM-dd") & "' "
		lstrSql = lstrSql & "     AND `num` BETWEEN '01B1011100' AND '01B1011199' "
		lstrSql = lstrSql & "     AND `valor` <> 0 "
		lstrSql = lstrSql & "ORDER BY `referencia` DESC, `num` ASC LIMIT 0, 1"
		Call Conectar(lstrSql, lrstTarifaGrB)
		With lrstTarifaGrB
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrCod = Trim(IIf(IsDbNull(.Fields("num").value), "", .Fields("num").value))
					ldblTarifa = Arredondar(TarifaProporcional(adteLeitAnt, adteLeitAtu, lstrCod, 3) / 1000, 5)
					Residencial = adblConsumo * ldblTarifa
				End If
				Call .Close()
			End If
		End With
		'UPGRADE_NOTE: Object lrstTarifaGrB may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		lrstTarifaGrB = Nothing
	End Function
	
	Private Sub LigDes(ByVal ablnModo As Boolean)
		Me.dtpMesAno.Enabled = ablnModo
		Me.txtSequencia.Enabled = ablnModo
		Me.txtArquivo.Enabled = ablnModo
		Me.cmbOperacao.Enabled = ablnModo
		Me.cmdArquivo.Enabled = ablnModo
		Me.cmdGerar.Enabled = ablnModo
		Me.cmdFechar.Enabled = ablnModo
	End Sub
	
	Private Function VerificaProximo(ByVal astrZona As String, ByVal astrLivro As String, ByVal astrConta As String, ByVal alngConsId As Object, ByVal arstAux As ADODB.Recordset) As Boolean
		Dim lstrZona As String
		Dim lstrLivro As String
		Dim lstrConta As String
		Dim llngConsId As Integer
		
		VerificaProximo = False
		With arstAux
			If Not .EOF Then
				Call .MoveNext()
				If Not .EOF Then
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrZona = VB.Right("000" & Trim(IIf(IsDbNull(.Fields("zona").value), "", .Fields("zona").value)), 3)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrLivro = VB.Right("00" & Trim(IIf(IsDbNull(.Fields("livro").value), "", .Fields("livro").value)), 2)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrConta = VB.Right("000000" & Trim(IIf(IsDbNull(.Fields("conta").value), "", .Fields("conta").value)), 6)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					llngConsId = IIf(IsDbNull(.Fields("cons_id").value), 0, .Fields("cons_id").value)
					'UPGRADE_WARNING: Couldn't resolve default property of object alngConsId. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					If lstrZona = astrZona And lstrLivro = astrLivro And lstrConta = astrConta And llngConsId = alngConsId Then
						VerificaProximo = True
					End If
				End If
				Call .MovePrevious()
			End If
		End With
	End Function
End Class