Buscar en el Blog

miércoles, 7 de diciembre de 2011

Validar RUC - SUNAT PERU en VB.net

Basado en el ejemplo de JhofranSoft 
 Algoritmo de Validación del RUC


Lo que modifique del codigo original es que puse todo lo concerniente a la validacion del ruc en una clase, de esa manera solo agregan la clase a su proyecto  y llaman a la funcion de validar el ruc y listo!


La clase
  1. Public Class ValidarRuc
  2. Public Shared Function validarRuc(ByVal ruc As String) As Boolean
  3. If VAL_RUC(ruc) = False Then
  4. Return False
  5. End If
  6. Return True
  7. End Function
  8. 'Ejm:
  9. 'RUC = 10254824220
  10. 'FACTOR = 5432765432
  11. 'Se separa los 10 primeros digitos de la izquierda y se hace un calculo inividual
  12. '1 * 5 =5
  13. '0 * 4 = 0
  14. '2 * 3 = 6
  15. '5 * 2 = 10
  16. '4 * 7 = 28
  17. '8 * 6 = 48
  18. '2 * 5 = 10
  19. '4 * 4 = 16
  20. '2 * 3 = 6
  21. '2 * 2 = 4
  22. 'Se suma el resultado de todas las multiplicaciones
  23. 'SUMA = 133
  24. 'Se calcula el residuo de la division por 11
  25. '133/ 11 = 1
  26. 'RESIDUO = 1
  27. 'Se resta 11 menos el residuo
  28. '11 - 1
  29. 'RESTA = 10
  30. 'digito de chequeo = RESTA
  31. 'si resta = 10 entonces digito de cheque = 0
  32. 'si resta = 11 entonces digito de cheque = 1
  33. 'RUC 10254824220 es valido por que su digito numero 11 es 0 y el digito de chekeo es 0.
  34. Private Shared Function LeftC(ByVal str As String, ByVal Length As Integer) As String
  35. Dim LenT As Integer = str.Length
  36. If LenT <= Length Then
  37. Return str
  38. Else
  39. Return str.Substring(0, Length)
  40. End If
  41. End Function
  42. Private Shared Function RightC(ByVal str As String, ByVal Length As Integer) As String
  43. Dim LenT As Integer = str.Length
  44. If LenT <= Length Then
  45. Return str
  46. Else
  47. Return str.Substring(LenT - Length)
  48. End If
  49. End Function
  50. Private Shared Function VAL_RUC(ByVal ruc As String) As Boolean
  51. Dim FACTOR() As Integer = {5, 4, 3, 2, 7, 6, 5, 4, 3, 2}
  52. Dim suma As Integer = 0
  53. 'ERROR SI NO ES NUMERO
  54. If Not IsNumeric(ruc) Then
  55. Return False
  56. End If
  57. 'ERROR SI NO CUMPLE LOS 11 DIGITOS
  58. If ruc.Length <> 11 Then
  59. Return False
  60. End If
  61. 'ERROR SI NO TIENE LOS 2 PRIMEROS DIGITOS
  62. '10 persona natural.
  63. '20 persona juridica.
  64. '17 o 15 extranjeros
  65. Dim VAL_DIGIT() As String = {"20", "17", "15", "10"}
  66. Dim DIGIT As String = LeftC(ruc, 2)
  67. Array.Sort(VAL_DIGIT)
  68. If Array.BinarySearch((VAL_DIGIT), DIGIT) < 0 Then
  69. Return False
  70. End If
  71. For I = 0 To ruc.Length - 2
  72. suma += Integer.Parse(ruc.Substring(I, 1)) * FACTOR(I)
  73. Next
  74. Dim residuo As Integer = suma Mod 11
  75. Dim resta As Integer = 11 - residuo
  76. Dim digChk As Integer
  77. If resta = 10 Then
  78. digChk = 0
  79. ElseIf resta = 11 Then
  80. digChk = 1
  81. Else
  82. digChk = resta
  83. End If
  84. If digChk = RightC(ruc, 1) Then
  85. Return True
  86. Else
  87. Return False
  88. End If
  89. End Function
  90. End Class

Luego solo llamamos a la funcion para validar

  1. If ValidarRuc.validarRuc(txtruc.Text) = True Then
  2. MessageBox.Show("Valido", "Exito", MessageBoxButtons.OK, MessageBoxIcon.Information)
  3. Else
  4. MessageBox.Show("Invalido", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  5. End If





7 comentarios:

  1. BUENOS TUTORIALES (Y)!

    TENTRAS UNO DONDE INDIQUE COMO PONERLE ETIQUETA DE DATOS A EN CADA BARRA A UN GRAFICO DE BARRAS EN POWERBUILDER?

    SALUDOS ^^

    ResponderEliminar
  2. No entendi muy bien la pregunta, podrias explicar con mayor detalle el problem?

    ResponderEliminar
  3. Oe wena eeh! les felicito, me ayudo a validar mi programa k tanto necesitaba. Yo validaba de esta forma:

    If Me.TxtDni.Text.StartsWith("10") = False And Me.TxtDni.Text.StartsWith("20") = False Then 'valida nro de ruc k empieze con 10 ó 20
    MsgBox("Número de ruc no válido " & (Chr(13)) & "Debe empezar con 10 ó 20", MsgBoxStyle.Critical, "Mensaje del sistema")
    Me.TxtDni.Focus()
    Exit Sub
    End If

    Pero weno creo k no era muy necesario validar asi, de todas maneras me sirvio

    ResponderEliminar
  4. El arreglo VAL_DIGIT debe estar ordenado de lo contrario no va a funcionar para 15 y 17.
    Array.Sort(VAL_DIGIT)

    ResponderEliminar
  5. Tienes razon, es necesario ordenarlo para que puede efectuarse la busqueda correctamente

    ResponderEliminar
  6. Gracias por el dato del Array.Sort(VAL_DIGIT) modificare mi codigo.

    ResponderEliminar

Nota: solo los miembros de este blog pueden publicar comentarios.