Ver Mensaje Individual
  #2104  
Antiguo 11-07-2024
jlmoli_67 jlmoli_67 is offline
Miembro
 
Registrado: feb 2024
Posts: 125
Reputación: 3
jlmoli_67 Va por buen camino
Cita:
Empezado por jlmoli_67 Ver Mensaje
Hola, en .net uso la funcion compruebacif() que comprueba la validez de un cof-nif haciendo una llamada a las otras funciones que he pegado. Si esta todo ok entonces hago una peticion soap firmada con los datos que tengo del cliente y me responde si dicho cliente esta censado.

Function compruebacif() As Integer
compruebacif = 0

Dim esCIF As Boolean = Verificar_CIF(TextBox3.Text)
If Len(TextBox3.Text) = 0 Then
MsgBox("El valor introducido no es correcto.Corrija el problema y vuelva a intentarlo", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "Atencion")

Exit Function
End If




Dim esNIF As Boolean = Verificar_NIF(TextBox3.Text)
If esCIF = True Or esNIF = True Then
' LabelInfo.Text &= vbCrLf & "Es un CIF: " & TextBox10.Text
compruebacif = 1

End If



End Function


Public Function Verificar_NIF(ByVal valor As String) As Boolean
Dim aux As String

valor = valor.ToUpper ' ponemos la letra en mayúscula
aux = valor.Substring(0, valor.Length - 1) ' quitamos la letra del NIF

If aux.Length >= 7 AndAlso IsNumeric(aux) Then
aux = CalculaNIF(aux) ' calculamos la letra del NIF para comparar con la que tenemos
Else
Return False
End If

If valor <> aux Then ' comparamos las letras
Return False
End If

Return True
End Function
Private Function CalculaNIF(ByVal strA As String) As String

Const cCADENA As String = "TRWAGMYFPDXBNJZSQVHLCKE"
Const cNUMEROS As String = "0123456789"
Dim a, b, c, NIF As Integer
Dim sb As New StringBuilder

strA = Trim(strA)
If Len(strA) = 0 Then Return ""

' Dejar sólo los números
For i As Integer = 0 To strA.Length - 1
If cNUMEROS.IndexOf(strA(i)) > -1 Then
sb.Append(strA(i))
End If
Next

strA = sb.ToString
a = 0
NIF = CInt(Val(strA))
Do
b = CInt(Int(NIF / 24))
c = NIF - (24 * b)
a = a + c
NIF = b
Loop While b <> 0
b = CInt(Int(a / 23))
c = a - (23 * b)

Return strA & Mid(cCADENA, CInt(c + 1), 1)
End Function



Public Function Verificar_CIF(ByVal valor As String) As Boolean
Dim strLetra As String, strNumero As String, strDigit As String
Dim strDigitAux As String
Dim auxNum As Integer
Dim i As Integer
Dim suma As Integer
Dim letras As String

letras = "ABCDEFGHJNPQRSUVW"
'KLM
valor = UCase(valor)

If Len(valor) < 9 OrElse Not IsNumeric(Mid(valor, 2, 7)) Then
Return False
End If

strLetra = Mid(valor, 1, 1) ' letra del CIF
strNumero = Mid(valor, 2, 7) ' Código de Control
strDigit = Mid(valor, 9) ' CIF menos primera y última posición

If InStr(letras, strLetra) = 0 Then ' comprobamos la letra del CIF (1ª posición)
Return False
End If

For i = 1 To 7
If i Mod 2 = 0 Then
suma = suma + CInt(Mid(strNumero, i, 1))
Else
auxNum = CInt(Mid(strNumero, i, 1)) * 2
suma = suma + (auxNum \ 10) + (auxNum Mod 10)
End If
Next
suma = (10 - (suma Mod 10)) Mod 10

Select Case strLetra
Case "K", "P", "Q", "S"
suma = suma + 64
strDigitAux = Chr(suma)
Case "X"
strDigitAux = Mid(CalculaNIF(strNumero), 8, 1)
Case Else
strDigitAux = CStr(suma)
End Select

If strDigit = strDigitAux Then
Return True
Else
Return False
End If
End Function



ejemplo peticion:
MINIF=reemplazar por cif-nif
MINOMBRE=reemplazar por el nombre o razon social


<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:vnif="http://www2.agenciatributaria.gob.es/static_files/common/internet/dep/aplicaciones/es/aeat/burt/jdit/ws/VNifV2Ent.xsd" xmlns:LocalPart="http://www2.agenciatributaria.gob.es/static_files/common/internet/dep/aplicaciones/es/aeat/burt/jdit/ws/VNifV2Ent.xsd">
<soapenv:Header/>
<soapenv:Body>
<vnif:VNifV2Ent>
<vnif:Contribuyente>
<vnif:Nif>MINIF</vnif:Nif>
<vnif:Nombre>MINOMBRE</vnif:Nombre>
</vnif:Contribuyente>
</vnif:VNifV2Ent>
</soapenv:Body>
</soapenv:Envelope>

un saludo
Se me olvido puntualizar que si el numero comienza y termina por una letra entonces se trata de un extranjero o de una administracion en cuyo caso lo doy por bueno.....por ahora, entre otras cosas porque no se aun como tratarlo.