Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE
Imports System.IO
Imports CrystalDecisions.CrystalReports.Engine
Friend Class frmCadNis
	Inherits System.Windows.Forms.Form
	Private istrNis As String
	Private istrCodFamiliar As String
	Private idteValidade As Date
	Private idteNascimento As Date
	Private istrUFNaciment As String
	Private idteCadbf As Date
	Private idteCadastro As Date
	Private istrCodmunic As String
	Private istrNomeNis As String
	Private istrCpfNis As String
	Private istrRgNis As String
	Private istrBpc As String
	Private istrApS As String
	Private ifrmQuemChama As System.Windows.Forms.Form
	Private dt, dt2, dt3, dt4 As DataTable
	Private objAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados

	Private objExecAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioexecAtualizaBancoDados As New repositorioAtualizaBancoDados

	Private Sub frmCadNis_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		Try
			If KeyAscii = System.Windows.Forms.Keys.Escape Then
				Call Me.Close()
			ElseIf KeyAscii = System.Windows.Forms.Keys.Return Then
				'Call SendKeysA(System.Windows.Forms.Keys.Tab, True)
			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: frmCadNis_KeyPress" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub frmCadNis_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Try
			'Call CenterForm(Me)
			'Me.dtpCadastro.Value = CDate(VB6.Format(Date.Now, "dd/mm/yyyy"))
			'Me.dtpNascimento.Value = Me.dtpCadastro.Value
			'Me.dtpCadbf.Value = Me.dtpCadastro.Value
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmCadNis_Load" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub frmCadNis_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		'ifrmQuemChama.Enabled = True
	End Sub

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

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


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

	Private Sub txtCpfNis_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtCpfNis.Leave
		Dim lstrMsgAux As String
		Try
			Me.txtCpfNis.Text = FormataCPF(Me.txtCpfNis.Text)
			If ValidaNisCpfNis(lstrMsgAux) Then
				Call MsgBox(lstrMsgAux & Chr(10) & " se você continuar este registro será desativado! ")
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtCpfNis_Leave" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

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

	Private Sub txtNis_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtNis.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		Try
			If KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then
				KeyAscii = 0
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtNis_KeyPress" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	
	Private Sub txtNis_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtNis.Leave
		If Trim(Me.txtNis.Text) <> "" Then
			Call BuscaNis()
		End If
	End Sub

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

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

	Private Sub txtNomeNis_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtNomeNis.Leave
		Me.txtNomeNis.Text = UCase(Trim(Me.txtNomeNis.Text))
	End Sub

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

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

	Public Sub SetaNis(ByVal astrCpf As String, ByVal astrNome As String, ByVal astrNrRg As String, ByVal astrZona As String, ByVal astrLivro As String, ByVal astrConta As String, ByVal alngUnidId As Integer, ByVal afrmQuemChama As System.Windows.Forms.Form)
		Dim lstrTip As String
		Dim ldteAux As Date
		Dim lstrSql As String

		Try

			'ifrmQuemChama = afrmQuemChama
			'ifrmQuemChama.Enabled = False
			ldteAux = Date.Now

			'Seta as propriedades da unidade consumidora que esta chamando a tela NIS
			Me.lblNomeConsumidor.Text = astrNome
			Me.lblNumCpf.Text = astrCpf
			Me.lblNumRg.Text = astrNrRg
			Me.lblZonaUC.Text = astrZona
			Me.lblLivroUC.Text = astrLivro
			Me.lblContaUC.Text = astrConta
			Me.lblUnidId.Text = CStr(alngUnidId)

			'Busca o nis da U.C.
			lstrTip = ""
			lstrSql = ""
			lstrSql = lstrSql & "SELECT `nis`.`id` "
			lstrSql = lstrSql & "  FROM `nis`"
			lstrSql = lstrSql & " WHERE `nis`.`cpf` = """ & Str_Retira(Me.lblNumCpf.Text, ".-/ ") & """ "
			lstrSql = lstrSql & "   AND `nis`.`unid_id` = " & CInt(Me.lblUnidId.Text)
			lstrSql = lstrSql & "   AND `nis`.`evento` = 'I' "
			lstrSql = lstrSql & " ORDER BY `id` DESC LIMIT 0, 1 "
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			lstrTip = IIf(dt.Rows.Count > 0, "I", "")

			If Trim(lstrTip) = "" Then
				lstrSql = ""
				lstrSql = lstrSql & "SELECT `nis`.`id` "
				lstrSql = lstrSql & "  FROM `nis`"
				lstrSql = lstrSql & " WHERE `nis`.`cpf` = """ & Str_Retira(Me.lblNumCpf.Text, ".-/ ") & """ "
				lstrSql = lstrSql & "   AND `nis`.`unid_id` = " & CInt(Me.lblUnidId.Text)
				lstrSql = lstrSql & "   AND `nis`.`evento` = 'A' "
				lstrSql = lstrSql & " ORDER BY `id` DESC LIMIT 0, 1 "
				objAtualizaBancoDados.QUERY_NAME = lstrSql
				dt2 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

				lstrTip = IIf(dt2.Rows.Count > 0, "A", "")
			End If

			lstrSql = ""
			lstrSql = lstrSql & "SELECT "
			lstrSql = lstrSql & "       `nis`.`evento`,"
			lstrSql = lstrSql & "       `nis`.`nis`, "
			lstrSql = lstrSql & "       `nis`.`data_cadastro`, "
			lstrSql = lstrSql & "       `nis`.`data_alteracao`, "
			lstrSql = lstrSql & "       `nis`.`data_nascimento`, "
			lstrSql = lstrSql & "       `nis`.`uf_nascimento`, "
			lstrSql = lstrSql & "       `nis`.`dtcadbf`, "
			lstrSql = lstrSql & "       `nis`.`codmunic`, "
			lstrSql = lstrSql & "       `nis`.`zona`, "
			lstrSql = lstrSql & "       `nis`.`livro`, "
			lstrSql = lstrSql & "       `nis`.`conta`, "
			lstrSql = lstrSql & "       `nis`.`evento`, "
			lstrSql = lstrSql & "       `nis`.`nome_nis`, "
			lstrSql = lstrSql & "       `nis`.`cpf_nis`, "
			lstrSql = lstrSql & "       `nis`.`rg_nis`, "
			lstrSql = lstrSql & "       `nis`.`unid_id`, "
			lstrSql = lstrSql & "       `nis`.`validade`, "
			lstrSql = lstrSql & "       `nis`.`ap_saude`, "
			lstrSql = lstrSql & "       `nis`.`bpc`, "
			lstrSql = lstrSql & "       `nis`.`cod_familiar_fam` "
			lstrSql = lstrSql & "  FROM `nis` "
			lstrSql = lstrSql & " WHERE (`nis`.`cpf` = """ & Str_Retira(Me.lblNumCpf.Text, ".-/ ") & """ OR  `nis`.`cpf_nis` = """ & Str_Retira(Me.lblNumCpf.Text, ".-/ ") & """ )"
			lstrSql = lstrSql & "   AND `nis`.`unid_id` = " & CInt(Me.lblUnidId.Text)
			If Trim(lstrTip) <> "" Then
				lstrSql = lstrSql & "   AND `nis`.`evento` = '" & lstrTip & "' "
			End If
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt3 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			If dt3.Rows.Count > 0 Then
				For Each row3 As DataRow In dt3.Rows

					istrNis = VB.Left(Replace(Trim(IIf(IsDBNull(row3.Item("nis")), "", row3.Item("nis"))), "-", ""), 11)
					Me.txtNis.Text = VB.Left(istrNis, 11)
					Me.txtCodFamiliar.Text = VB.Left(Trim(IIf(IsDBNull(row3.Item("cod_familiar_fam")), "", row3.Item("cod_familiar_fam"))), 11)
					Me.dtpNascimento.Value = CDate(IIf(IsDBNull(row3.Item("data_nascimento")), ldteAux, row3.Item("data_nascimento")))
					Me.dtpCadastro.Value = CDate(IIf(IsDBNull(row3.Item("data_cadastro")), ldteAux, row3.Item("data_cadastro")))
					Me.dtpAlteracao.Value = CDate(IIf(IsDBNull(row3.Item("data_alteracao")), ldteAux, row3.Item("data_alteracao")))
					Me.dtpCadbf.Value = CDate(IIf(IsDBNull(row3.Item("dtcadbf")), ldteAux, row3.Item("dtcadbf")))
					Me.txtCodMunic.Text = Trim(IIf(IsDBNull(row3.Item("codmunic")), "", row3.Item("codmunic")))
					Me.lblZonaNis.Text = Trim(IIf(IsDBNull(row3.Item("zona")), "", row3.Item("zona")))
					Me.lblLivroNis.Text = Trim(IIf(IsDBNull(row3.Item("livro")), "", row3.Item("livro")))
					Me.lblContaNis.Text = Trim(IIf(IsDBNull(row3.Item("conta")), "", row3.Item("conta")))
					Me.lblCodEvento.Text = Trim(IIf(IsDBNull(row3.Item("evento")), "", row3.Item("evento")))
					Me.lblCodEvento.Text = IIf(Me.lblCodEvento.Text = "I", "Incluído", IIf(Me.lblCodEvento.Text = "A", "Alterado", "Desativado"))
					Call SetUf(UCase(Trim(IIf(IsDBNull(row3.Item("uf_nascimento")) Or row3.Item("uf_nascimento") = "", gstrEmpresa_UF, row3.Item("uf_nascimento")))))
					Me.txtNomeNis.Text = Trim(IIf(IsDBNull(row3.Item("nome_nis")), "", row3.Item("nome_nis")))
					Me.txtCpfNis.Text = Trim(IIf(IsDBNull(row3.Item("cpf_nis")), "", row3.Item("cpf_nis")))
					Me.txtCpfNis.Text = FormataCPF(Me.txtCpfNis.Text)
					Me.txtRgNis.Text = Trim(IIf(IsDBNull(row3.Item("rg_nis")), "", row3.Item("rg_nis")))

					'If gintUsuario_NivelCaixa <> 0 Then
					Me.dtpCadbf.Enabled = True
					Me.dtpCadastro.Enabled = True
					Me.dtpAlteracao.Enabled = True
					'End If

					Me.lblUnidIdNis.Text = CStr(CInt(IIf(IsDBNull(row3.Item("unid_id")), 0, row3.Item("unid_id"))))
					istrApS = IIf(IsDBNull(row3.Item("ap_saude")), "N", row3.Item("ap_saude"))
					Me.chkApSaude.CheckState = IIf(istrApS = "S", 1, 0)
					istrBpc = IIf(IsDBNull(row3.Item("bpc")), "N", row3.Item("bpc"))
					Me.chkBPC.CheckState = IIf(istrBpc = "S", 1, 0)
					Me.dtpValidade.Value = CDate(IIf(IsDBNull(row3.Item("validade")), ldteAux, row3.Item("validade")))
					idteValidade = Me.dtpValidade.Value
					idteNascimento = Me.dtpNascimento.Value
					istrUFNaciment = VB6.GetItemString(Me.cmbUf, Me.cmbUf.SelectedIndex)
					idteCadbf = Me.dtpCadbf.Value
					idteCadastro = Me.dtpCadastro.Value
					istrCodmunic = Me.txtCodMunic.Text
					istrNomeNis = Me.txtNomeNis.Text
					istrCpfNis = Me.txtCpfNis.Text
					istrRgNis = Me.txtRgNis.Text
					istrCodFamiliar = Me.txtCodFamiliar.Text
					Call PopulaGridHistorico()
				Next
			End If

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

	End Sub

	Private Sub cmdDesativar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDesativar.Click
		Dim lstrSql As String

		Try
			If MsgBox("Deseja desativar as informações do NIS do " & UCase(Trim(Me.txtNomeNis.Text)) & "?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, gstrTituloMensagem) = MsgBoxResult.Yes Then
				Call DesativaNis(Me.lblNumCpf.Text)
				Call cmdFechar_Click(cmdFechar, New System.EventArgs())
			End If

		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdDesativar_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	
	Private Sub DesativaNis(ByVal astrCpf As String)
		Dim lstrSql As String
		Try

			lstrSql = ""
			lstrSql = lstrSql & "UPDATE `nis` "
			lstrSql = lstrSql & "   SET `evento`          = ""D"", "
			lstrSql = lstrSql & "       `data_alteracao`  = """ & VB6.Format(Date.Now, "yyyy-MM-dd") & """, "
			lstrSql = lstrSql & "       `flgst`           = ""*"" "
			lstrSql = lstrSql & " WHERE `nis`.`cpf`     = """ & Str_Retira(astrCpf, " /.-") & """ "
			lstrSql = lstrSql & "   AND `nis`.`nis`     = """ & Trim(Me.txtNis.Text) & """ "
			lstrSql = lstrSql & "   AND `nis`.`evento` IN ('I','A') "
			objExecAtualizaBancoDados.QUERY_NAME = lstrSql
			repositorioexecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

			Call MsgBox("N.I.S. desativado com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: DesativaNis" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	
	Private Sub DesativaNisPeloCpf(ByVal astrCpf As String)
		Dim lstrSql As String
		Try
			lstrSql = ""
			lstrSql = lstrSql & "UPDATE `nis` "
			lstrSql = lstrSql & "   SET `evento`          = ""D"", "
			lstrSql = lstrSql & "       `data_alteracao`  = """ & VB6.Format(Date.Now, "yyyy-MM-dd") & """, "
			lstrSql = lstrSql & "       `flgst`           = ""*"" "
			lstrSql = lstrSql & " WHERE `nis`.`cpf_nis`     = """ & Str_Retira(astrCpf, " /.-") & """ "
			lstrSql = lstrSql & "   AND `nis`.`evento` IN ('I','A') "
			objExecAtualizaBancoDados.QUERY_NAME = lstrSql
			repositorioexecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

			Call MsgBox("N.I.S. desativado com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: DesativaNisPeloCpf" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	
	Private Sub cmdReativar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdReativar.Click
		Dim lstrSql As String
		Try

			If MsgBox("Deseja reativar as informações do NIS do consumidor " & UCase(Trim(Me.txtNomeNis.Text)) & "?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, gstrTituloMensagem) = MsgBoxResult.Yes Then
				lstrSql = ""
				lstrSql = lstrSql & "UPDATE `nis` "
				lstrSql = lstrSql & "   SET `evento`          = ""A"", "
				lstrSql = lstrSql & "       `data_alteracao`  = """ & VB6.Format(Date.Now, "yyyy-MM-dd") & """, "
				lstrSql = lstrSql & "       `data_cadastro`  = """ & VB6.Format(Me.dtpCadastro.Value, "yyyy-MM-dd") & """, "
				lstrSql = lstrSql & "       `flgst`           = ""*"" "
				lstrSql = lstrSql & " WHERE `nis`.`cpf`     = """ & Str_Retira(Me.lblNumCpf.Text, " /.-") & """ "
				lstrSql = lstrSql & "   AND `nis`.`nis`     = """ & Trim(Me.txtNis.Text) & """ "
				lstrSql = lstrSql & "   AND `nis`.`evento`  = 'D' "
				objExecAtualizaBancoDados.QUERY_NAME = lstrSql
				repositorioexecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

				Call MsgBox("N.I.S. reativado com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
				Call cmdFechar_Click(cmdFechar, New System.EventArgs())
			End If

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

	End Sub
	
	Private Sub cmdGravar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdGravar.Click
		Dim llngUnidId As Integer
		Dim lstrCpfAux As String
		'Dim lstrEvento As String
		Dim ldteAux As Date
		Dim lstrNisAux As String
		Dim lstrAuxMsg As String
		Dim lstrSql As String
		Dim lblnNovo As Boolean

		Try

			lstrAuxMsg = ""
			lblnNovo = False
			lstrNisAux = VB6.Format(Trim(Me.txtNis.Text), "00000000000")
			ldteAux = Date.Now
			If (CInt(IIf(IsNumeric(Me.lblUnidId.Text), Me.lblUnidId.Text, 0)) > 0 And CInt(IIf(IsNumeric(Me.lblUnidIdNis.Text), Me.lblUnidIdNis.Text, 0)) > 0) And (CInt(IIf(IsNumeric(Me.lblUnidId.Text), Me.lblUnidId.Text, 0)) <> CInt(IIf(IsNumeric(Me.lblUnidIdNis.Text), Me.lblUnidIdNis.Text, 0))) Then
				lblnNovo = False
			Else
				lstrSql = ""
				lstrSql = lstrSql & "SELECT * "
				lstrSql = lstrSql & "  FROM `nis` "
				lstrSql = lstrSql & " WHERE `nis`.`cpf` = """ & Str_Retira(Me.lblNumCpf.Text, ".-/ ") & """ "
				lstrSql = lstrSql & "   AND `nis`.`unid_id` = " & CInt(Me.lblUnidId.Text)
				lstrSql = lstrSql & "   AND `nis`.`nis` = '" & Me.txtNis.Text & "' "
				objAtualizaBancoDados.QUERY_NAME = lstrSql
				dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

				lblnNovo = (dt.Rows.Count = 0)
			End If

			If MsgBox("Confirma a " & IIf(lblnNovo, "inclusão", "alteração") & " do cadastro do nis ", MsgBoxStyle.Question + MsgBoxStyle.YesNo, gstrTituloMensagem) = MsgBoxResult.Yes Then
				If lblnNovo = True Then
					If ValidaNisCpfNis(lstrAuxMsg) Then
						Call MsgBox(lstrAuxMsg & Chr(10) & " este registro será desativado! ", MsgBoxStyle.Exclamation, gstrTituloMensagem)
						Call DesativaNisPeloCpf(Me.txtCpfNis.Text)
					End If
					lstrAuxMsg = ""
					If ValidaNisCpf(lstrAuxMsg, lstrNisAux) Then
						Call MsgBox(lstrAuxMsg, MsgBoxStyle.Exclamation, gstrTituloMensagem)
						Call DesativaNis(lstrCpfAux)
					End If
					lstrSql = ""
					lstrSql = lstrSql & "INSERT INTO `nis` " & vbCrLf
					lstrSql = lstrSql & "        SET `cpf`             = """ & Str_Retira(Me.lblNumCpf.Text, ".-/ ") & """, " & vbCrLf
					lstrSql = lstrSql & "            `nis`             = """ & lstrNisAux & """, " & vbCrLf
					lstrSql = lstrSql & "            `cod_familiar_fam`= " & IIf(CDbl(Me.txtCodFamiliar.Text) > 0, Me.txtCodFamiliar.Text, "0") & ", " & vbCrLf
					lstrSql = lstrSql & "            `data_cadastro`   = """ & VB6.Format(ldteAux, "yyyy-MM-dd") & """, " & vbCrLf
					lstrSql = lstrSql & "            `data_alteracao`  = """ & VB6.Format(ldteAux, "yyyy-MM-dd") & """, " & vbCrLf
					lstrSql = lstrSql & "            `data_nascimento` = """ & VB6.Format(Me.dtpNascimento.value, "yyyy-MM-dd") & """, " & vbCrLf
					lstrSql = lstrSql & "            `validade`        = """ & VB6.Format(Me.dtpValidade.value, "yyyy-MM-dd") & """, " & vbCrLf
					lstrSql = lstrSql & "            `uf_nascimento`   = """ & VB6.GetItemString(Me.cmbUf, Me.cmbUf.SelectedIndex) & """, " & vbCrLf
					lstrSql = lstrSql & "            `evento`          = ""I"", " & vbCrLf
					lstrSql = lstrSql & "            `unid_id`         = " & CInt(Me.lblUnidId.Text) & ", " & vbCrLf
					lstrSql = lstrSql & "            `zona`            = """ & Me.lblZonaUC.Text & """, " & vbCrLf
					lstrSql = lstrSql & "            `livro`           = """ & Me.lblLivroUC.Text & """, " & vbCrLf
					lstrSql = lstrSql & "            `conta`           = """ & Me.lblContaUC.Text & """, " & vbCrLf
					lstrSql = lstrSql & "            `flgst`           = ""*"", " & vbCrLf
					lstrSql = lstrSql & "            `dtcadbf`         = """ & VB6.Format(Me.dtpCadbf.value, "yyyy-MM-dd") & """, " & vbCrLf
					lstrSql = lstrSql & "            `codmunic`        = """ & Trim(Me.txtCodMunic.Text) & """, " & vbCrLf
					lstrSql = lstrSql & "            `nome_nis`        = """ & Trim(Me.txtNomeNis.Text) & """, " & vbCrLf
					lstrSql = lstrSql & "            `cpf_nis`         = """ & Str_Retira(Trim(Me.txtCpfNis.Text), ".-/ ") & """, " & vbCrLf
					lstrSql = lstrSql & "            `rg_nis`          = """ & Trim(Me.txtRgNis.Text) & """, "
					lstrSql = lstrSql & "            `bpc`             = """ & IIf(Me.chkBPC.CheckState = 1, "S", "N") & """, "
					lstrSql = lstrSql & "            `ap_saude`        = """ & IIf(Me.chkApSaude.CheckState = 1, "S", "N") & """ "
					objExecAtualizaBancoDados.QUERY_NAME = lstrSql
					repositorioexecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

					Call AtualizaHistoricoUnidade(CInt(Me.lblUnidId.Text), "B", "[" & VB6.Format(ldteAux, "dd/mm/yyyy") & "]: INCLUIDO NIS: " & lstrNisAux)
				Else
					If CInt(Me.lblUnidId.Text) <> CInt(Me.lblUnidIdNis.Text) Then
						If MsgBox("Deseja transferir o NIS para esta Unidade Consumidora " & Me.lblZonaUC.Text & "." & Me.lblLivroUC.Text & "." & Me.lblContaUC.Text & "?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, gstrTituloMensagem) = MsgBoxResult.Yes Then
							lstrSql = ""
							lstrSql = lstrSql & "UPDATE `nis` " & vbCrLf
							lstrSql = lstrSql & "   SET `unid_id`   = """ & Me.lblUnidId.Text & """, " & vbCrLf
							lstrSql = lstrSql & "       `cpf`      = """ & Str_Retira(Me.lblNumCpf.Text, ".-/ ") & """, " & vbCrLf
							lstrSql = lstrSql & "       `zona`      = """ & Me.lblZonaUC.Text & """, " & vbCrLf
							lstrSql = lstrSql & "       `livro`     = """ & Me.lblLivroUC.Text & """, " & vbCrLf
							lstrSql = lstrSql & "       `conta`     = """ & Me.lblContaUC.Text & """ " & vbCrLf
							lstrSql = lstrSql & " WHERE `nis`.`cpf_nis` = """ & Str_Retira(Me.txtCpfNis.Text, ".-/ ") & """ " & vbCrLf
							lstrSql = lstrSql & "   AND `nis`.`unid_id` = " & CInt(Me.lblUnidIdNis.Text)
							lstrSql = lstrSql & "   AND `nis`.`nis` = """ & lstrNisAux & """ " & vbCrLf
							objExecAtualizaBancoDados.QUERY_NAME = lstrSql
							repositorioexecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

							Call AtualizaHistoricoUnidade(CInt(Me.lblUnidId.Text), "B", "[" & VB6.Format(ldteAux, "dd/mm/yyyy") & "]: TRANSF. NIS: " & lstrNisAux & " DE " & Me.lblZonaNis.Text & "." & Me.lblLivroNis.Text & "." & Me.lblContaNis.Text)
							Call AtualizaHistoricoUnidade(CInt(Me.lblUnidIdNis.Text), "B", "[" & VB6.Format(ldteAux, "dd/mm/yyyy") & "]: TRANSF. NIS: " & lstrNisAux & " PARA " & Me.lblZonaUC.Text & "." & Me.lblLivroUC.Text & "." & Me.lblContaUC.Text)
						End If
					End If

					If Trim(istrNis) <> Trim(lstrNisAux) Or idteNascimento <> Me.dtpNascimento.value Or istrUFNaciment <> VB6.GetItemString(Me.cmbUf, Me.cmbUf.SelectedIndex) Or idteCadbf <> Me.dtpCadbf.value Or istrCodmunic <> Me.txtCodMunic.Text Or istrNomeNis <> Me.txtNomeNis.Text Or istrCpfNis <> Me.txtCpfNis.Text Or istrRgNis <> Me.txtRgNis.Text Or istrBpc <> IIf(Me.chkBPC.CheckState = 1, "S", "N") Or istrApS <> IIf(Me.chkApSaude.CheckState = 1, "S", "N") Or idteValidade <> Me.dtpValidade.value Or idteCadastro <> Me.dtpCadastro.value Or istrCodFamiliar <> Me.txtCodFamiliar.Text Then
						lstrSql = ""
						lstrSql = lstrSql & "UPDATE `nis` " & vbCrLf
						lstrSql = lstrSql & "   SET `nis`             = """ & lstrNisAux & """, " & vbCrLf
						lstrSql = lstrSql & "       `cod_familiar_fam`= " & IIf(CDbl(Me.txtCodFamiliar.Text) > 0, Me.txtCodFamiliar.Text, "0") & ", " & vbCrLf
						lstrSql = lstrSql & "       `data_alteracao`  = """ & VB6.Format(ldteAux, "yyyy-MM-dd") & """, " & vbCrLf
						lstrSql = lstrSql & "       `data_nascimento` = """ & VB6.Format(Me.dtpNascimento.value, "yyyy-MM-dd") & """, " & vbCrLf
						lstrSql = lstrSql & "       `data_cadastro`   = """ & VB6.Format(Me.dtpCadastro.value, "yyyy-MM-dd") & """, " & vbCrLf
						lstrSql = lstrSql & "       `validade`        = """ & VB6.Format(Me.dtpValidade.value, "yyyy-MM-dd") & """, " & vbCrLf
						lstrSql = lstrSql & "       `uf_nascimento`   = """ & VB6.GetItemString(Me.cmbUf, Me.cmbUf.SelectedIndex) & """, " & vbCrLf
						lstrSql = lstrSql & "       `evento`          = ""A"", " & vbCrLf
						lstrSql = lstrSql & "       `flgst`           = ""*"", " & vbCrLf
						lstrSql = lstrSql & "       `dtcadbf`         = """ & VB6.Format(Me.dtpCadbf.value, "yyyy-MM-dd") & """, " & vbCrLf
						lstrSql = lstrSql & "       `codmunic`        = """ & Trim(Me.txtCodMunic.Text) & """, " & vbCrLf
						lstrSql = lstrSql & "       `nome_nis`        = """ & Trim(Me.txtNomeNis.Text) & """, " & vbCrLf
						lstrSql = lstrSql & "       `cpf_nis`         = """ & Str_Retira(Trim(Me.txtCpfNis.Text), ".-/ ") & """, " & vbCrLf
						lstrSql = lstrSql & "       `rg_nis`          = """ & Trim(Me.txtRgNis.Text) & """, " & vbCrLf
						lstrSql = lstrSql & "       `bpc`             = """ & IIf(Me.chkBPC.CheckState = 1, "S", "N") & """, "
						lstrSql = lstrSql & "       `ap_saude`        = """ & IIf(Me.chkApSaude.CheckState = 1, "S", "N") & """ "
						lstrSql = lstrSql & " WHERE `nis`.`cpf` = """ & Str_Retira(Me.lblNumCpf.Text, ".-/ ") & """ " & vbCrLf
						lstrSql = lstrSql & "   AND `nis`.`unid_id` = " & CInt(Me.lblUnidIdNis.Text)
						objExecAtualizaBancoDados.QUERY_NAME = lstrSql
						repositorioexecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

						Call AtualizaHistoricoUnidade(CInt(Me.lblUnidIdNis.Text), "B", "[" & VB6.Format(ldteAux, "dd/mm/yyyy") & "]: ATUALIZADO NIS: " & lstrNisAux)
					End If
				End If
				Call MsgBox("Gravação concluída!", MsgBoxStyle.Information, gstrTituloMensagem)
				Call cmdFechar_Click(cmdFechar, New System.EventArgs())
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdGravar_Click" & "", "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 SetUf(ByVal astrUf As String)
		Dim llngIdx As Integer
		Try
			If Len(Trim(astrUf)) > 0 Then
				llngIdx = 0
				With Me.cmbUf
					While llngIdx < .Items.Count
						If VB6.GetItemString(Me.cmbUf, llngIdx) = astrUf Then
							.SelectedIndex = llngIdx
							llngIdx = .Items.Count
						End If
						llngIdx = llngIdx + 1
					End While
				End With
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: SetUf" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub PopulaGridHistorico()
		Dim lstrZona As String
		Dim lstrLivro As String
		Dim lstrConta As String
		Dim lstrEvento As String
		Dim ldteCadastro As Date
		Dim lstrSql As String

		Try
			With Me.mfgDados
				.Cols = 5
				.Rows = 1
				Call .Clear()
				.set_TextMatrix(0, 0, "Zona")
				.set_TextMatrix(0, 1, "Livro")
				.set_TextMatrix(0, 2, "Conta")
				.set_TextMatrix(0, 3, "Evento")
				.set_TextMatrix(0, 4, "Cadastro")
			End With

			lstrSql = ""
			lstrSql = lstrSql & "SELECT `nis`.`id`, "
			lstrSql = lstrSql & "       `nis`.`zona`, "
			lstrSql = lstrSql & "       `nis`.`livro`, "
			lstrSql = lstrSql & "       `nis`.`conta`, "
			lstrSql = lstrSql & "       `nis`.`evento`, "
			lstrSql = lstrSql & "       `nis`.`data_cadastro` "
			lstrSql = lstrSql & "  FROM `nis` "
			lstrSql = lstrSql & " WHERE `nis`.`nis` = '" & VB.Left(istrNis, 11) & "' "
			lstrSql = lstrSql & " ORDER BY `id` ASC "
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			If dt.Rows.Count > 0 Then
				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")))
					lstrEvento = Trim(IIf(IsDBNull(row.Item("evento")), "I", row.Item("evento")))
					ldteCadastro = CDate(IIf(IsDBNull(row.Item("data_cadastro")), Date.Now, row.Item("data_cadastro")))
					With Me.mfgDados
						.Rows = .Rows + 1
						.set_TextMatrix(.Rows - 1, 0, lstrZona)
						.set_TextMatrix(.Rows - 1, 1, lstrLivro)
						.set_TextMatrix(.Rows - 1, 2, lstrConta)
						.set_TextMatrix(.Rows - 1, 3, lstrEvento)
						.set_TextMatrix(.Rows - 1, 4, ldteCadastro)
					End With
				Next
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: PopulaGridHistorico" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub
	Private Sub BuscaNis()
		Dim lstrAux As String
		Dim lstrSql As String
		Dim ldteAux As Date

		Try

			ldteAux = Date.Now
			lstrSql = ""
			lstrSql = lstrSql & "SELECT "
			lstrSql = lstrSql & "       `nis`.`evento`,"
			lstrSql = lstrSql & "       `nis`.`nis`, "
			lstrSql = lstrSql & "       `nis`.`data_cadastro`, "
			lstrSql = lstrSql & "       `nis`.`data_alteracao`, "
			lstrSql = lstrSql & "       `nis`.`data_nascimento`, "
			lstrSql = lstrSql & "       `nis`.`uf_nascimento`, "
			lstrSql = lstrSql & "       `nis`.`dtcadbf`, "
			lstrSql = lstrSql & "       `nis`.`codmunic`, "
			lstrSql = lstrSql & "       `nis`.`zona`, "
			lstrSql = lstrSql & "       `nis`.`livro`, "
			lstrSql = lstrSql & "       `nis`.`conta`, "
			lstrSql = lstrSql & "       `nis`.`evento`, "
			lstrSql = lstrSql & "       `nis`.`nome_nis`, "
			lstrSql = lstrSql & "       `nis`.`cpf_nis`, "
			lstrSql = lstrSql & "       `nis`.`rg_nis`, "
			lstrSql = lstrSql & "       `nis`.`unid_id`, "
			lstrSql = lstrSql & "       `nis`.`cod_familiar_fam` "
			lstrSql = lstrSql & "  FROM `nis` "
			lstrSql = lstrSql & " WHERE `nis`.`nis` = '" & Me.txtNis.Text & "' "
			lstrSql = lstrSql & "   AND `nis`.`evento` NOT IN ('D') "
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

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

					Me.txtCodFamiliar.Text = VB.Left(Trim(IIf(IsDBNull(row.Item("cod_familiar_fam")), "", row.Item("cod_familiar_fam"))), 11)
					Me.dtpNascimento.Value = CDate(IIf(IsDBNull(row.Item("data_nascimento")), ldteAux, row.Item("data_nascimento")))
					Me.dtpCadastro.Value = CDate(IIf(IsDBNull(row.Item("data_cadastro")), ldteAux, row.Item("data_cadastro")))
					Me.dtpAlteracao.Value = CDate(IIf(IsDBNull(row.Item("data_alteracao")), ldteAux, row.Item("data_alteracao")))
					Me.dtpCadbf.Value = CDate(IIf(IsDBNull(row.Item("dtcadbf")), "1900-01-01", row.Item("dtcadbf")))
					Me.txtCodMunic.Text = Trim(IIf(IsDBNull(row.Item("codmunic")), "", row.Item("codmunic")))
					Me.lblZonaNis.Text = Trim(IIf(IsDBNull(row.Item("zona")), "", row.Item("zona")))
					Me.lblLivroNis.Text = Trim(IIf(IsDBNull(row.Item("livro")), "", row.Item("livro")))
					Me.lblContaNis.Text = Trim(IIf(IsDBNull(row.Item("conta")), "", row.Item("conta")))
					Me.lblCodEvento.Text = Trim(IIf(IsDBNull(row.Item("evento")), "", row.Item("evento")))
					Me.lblCodEvento.Text = IIf(Me.lblCodEvento.Text = "I", "Incluído", IIf(Me.lblCodEvento.Text = "A", "Alterado", "Desativado"))
					Call SetUf(UCase(Trim(IIf(IsDBNull(row.Item("uf_nascimento")) Or row.Item("uf_nascimento") = "", gstrEmpresa_UF, row.Item("uf_nascimento")))))
					Me.txtNomeNis.Text = Trim(IIf(IsDBNull(row.Item("nome_nis")), "", row.Item("nome_nis")))
					Me.txtCpfNis.Text = Trim(IIf(IsDBNull(row.Item("cpf_nis")), "", row.Item("cpf_nis")))
					Me.txtCpfNis.Text = FormataCPF(Me.txtCpfNis.Text)
					Me.txtRgNis.Text = Trim(IIf(IsDBNull(row.Item("rg_nis")), "", row.Item("rg_nis")))
					Me.dtpCadbf.Enabled = True
					Me.dtpCadastro.Enabled = True
					Me.dtpAlteracao.Enabled = True
					Me.lblUnidIdNis.Text = CStr(CInt(IIf(IsDBNull(row.Item("unid_id")), 0, row.Item("unid_id"))))

					idteNascimento = Me.dtpNascimento.Value
					istrUFNaciment = VB6.GetItemString(Me.cmbUf, Me.cmbUf.SelectedIndex)

					idteCadbf = Me.dtpCadbf.Value
					istrCodmunic = Me.txtCodMunic.Text
					istrNomeNis = Me.txtNomeNis.Text
					istrCpfNis = Me.txtCpfNis.Text
					istrRgNis = Me.txtRgNis.Text
					istrNis = Me.txtNis.Text
					istrCodFamiliar = Me.txtCodFamiliar.Text
					Call PopulaGridHistorico()
				Next
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: BuscaNis" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	Private Function ValidaNisCpfNis(ByRef astrMsgAux As String) As Boolean
		Dim lblnRet As Boolean
		Dim lstrSql As String

		Try

			lblnRet = False
			astrMsgAux = ""
			lstrSql = ""
			lstrSql = lstrSql & "SELECT `nis`.`cpf`, "
			lstrSql = lstrSql & "       `nis`.`zona`, "
			lstrSql = lstrSql & "       `nis`.`livro`, "
			lstrSql = lstrSql & "       `nis`.`conta`, "
			lstrSql = lstrSql & "       `nis`.`nis` "
			lstrSql = lstrSql & "  FROM `nis` "
			lstrSql = lstrSql & " WHERE `nis`.`cpf_nis` = """ & Str_Retira(Trim(Me.txtCpfNis.Text), ".-/ ") & """ "
			lstrSql = lstrSql & "   AND `nis`.`evento` IN ('I','A') "
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			lblnRet = dt.Rows.Count > 0
			If lblnRet Then
				For Each row As DataRow In dt.Rows
					astrMsgAux = "Este CPF possui o nis " & IIf(IsDBNull(row.Item("nis")), "", row.Item("nis")) & Chr(10) & " na unidade " & IIf(IsDBNull(row.Item("zona")), "", row.Item("zona")) & "." & IIf(IsDBNull(row.Item("livro")), "", row.Item("livro")) & "." & IIf(IsDBNull(row.Item("conta")), "", row.Item("conta"))
				Next
			End If
			ValidaNisCpfNis = lblnRet
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: ValidaNisCpfNis" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Function
		End Try

	End Function

	Private Function ValidaNisCpf(ByRef astrMsgAux As String, ByVal astrNisAux As String) As Boolean
		Dim lblnRet As Boolean
		Dim lstrSql As String
		Try
			lblnRet = False
			astrMsgAux = ""
			lstrSql = ""
			lstrSql = lstrSql & "SELECT `nis`.`cpf`, "
			lstrSql = lstrSql & "       `nis`.`zona`, "
			lstrSql = lstrSql & "       `nis`.`livro`, "
			lstrSql = lstrSql & "       `nis`.`conta` "
			lstrSql = lstrSql & "  FROM `nis` "
			lstrSql = lstrSql & " WHERE `nis`.`nis`  = """ & astrNisAux & """ "
			lstrSql = lstrSql & "   AND `nis`.`cpf` <> """ & Str_Retira(Me.lblNumCpf.Text, ".-/ ") & """ "
			lstrSql = lstrSql & "   AND `nis`.`evento` IN ('I','A') "
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			lblnRet = dt.Rows.Count > 0
			If lblnRet Then
				For Each row As DataRow In dt.Rows
					astrMsgAux = "Este NIS já está cadastrado!" & Chr(10) & "no CPF " & IIf(IsDBNull(row.Item("cpf")), "", row.Item("cpf")) & Chr(10) & " este registro será desativado! "
				Next
			Else
				astrMsgAux = ""
			End If
			ValidaNisCpf = lblnRet
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: ValidaNisCpf" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Function
		End Try

	End Function
End Class