-
Notifications
You must be signed in to change notification settings - Fork 13
/
Rif.php
183 lines (166 loc) · 5.62 KB
/
Rif.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* Rutina para obtener los datos fiscales desde el portal del seniat
*
* @author José Ayrám <[email protected]>
* @category Libs
* @since 06/12/2012
* @version 1.0
*
*/
class Rif {
/**
*[code_result] = -2: Formato de rif inválido
* -1: No hay soporte a curl
* 0: No hay conexion a internet
* 1: Consulta satisfactoria
* Otherwise:
* 450: Formato de rif invalido
* 452: Rif no existe
*
* [seniat] = nombre: [CADENA CON EL NOMBRE]
* agenteretensioniva: [SI|NO]
* contribuyenteiva: [SI|NO]
* tasa: [VACIO|ENTERO MONTO TASA]
* @var Array
*/
private $_responseJson = array(
'code_result' => '',
'message' => '',
'seniat' => array()
);
/**
*
* @var String
*/
private $_url = 'http://contribuyente.seniat.gob.ve/getContribuyente/getrif?rif=';
/**
*
* @var String
*/
private $_rif;
/**
*
* @param String $rif
*/
public function __construct($rif) {
$this->setRif($rif);
}
/**
* Dar formato inicial al RIF recibido
*
* @param String $rif
* @return String
*/
private function setRif($rif) {
$this->_rif = str_replace('-', '', strtoupper($rif));
return $this->_rif;
}
/**
* Obtener la información en formato Json
*
* @param String $rif
* @return Json
* @throws Exception
*/
public function getInfo() {
if ($this->_validar()) {
if(function_exists('curl_init')) {
$this->_url .= $this->_rif;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->_url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec ($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_status == 200) {
try {
if(substr($result,0,1)!= '<' ) {
throw new Exception($result);
}
$xml = simplexml_load_string($result);
if(!is_bool($xml)) {
$elements = $xml->children('rif');
$seniat = array();
$this->_responseJson['code_result'] = 1;
$this->_responseJson['message'] = 'Consulta satisfactoria';
foreach($elements as $key => $node) {
$index = strtolower($node->getName());
$seniat[$index] = (string)$node;
}
$this->_responseJson['seniat'] = $seniat;
}
} catch(Exception $e) {
$exception = explode(' ', $result, 2);
$this->_responseJson['code_result'] =(int) $exception[0];
}
} else {
// No hay conexión a internet
$this->_responseJson['code_result'] = 0;
$this->_responseJson['message'] = 'Recurso inaccesible';
}
} else {
// No hay soporte CURL
$this->_responseJson['code_result'] = -1;
$this->_responseJson['message'] = 'No Existe Soporte Curl';
}
} else {
// Formato de RIF inválido
$this->_responseJson['code_result'] = -2;
$this->_responseJson['message'] = 'Rif Inexistente o Inválido';
}
return json_encode($this->_responseJson);
}
/**
* Validar formato del RIF
*
* Basado en el método módulo 11 para el cálculo del dígito verificador
* y aplicando las modificaciones propias ejecutadas por el seniat
* @link http://es.wikipedia.org/wiki/C%C3%B3digo_de_control#C.C3.A1lculo_del_d.C3.ADgito_verificador
*
* @return boolean
*/
private function _validar() {
$retorno = preg_match("/^([VEJPG]{1})([0-9]{9}$)/", $this->_rif);
if ($retorno) {
$digitos = str_split($this->_rif);
$digitos[8] *= 2;
$digitos[7] *= 3;
$digitos[6] *= 4;
$digitos[5] *= 5;
$digitos[4] *= 6;
$digitos[3] *= 7;
$digitos[2] *= 2;
$digitos[1] *= 3;
// Determinar dígito especial según la inicial del RIF
// Regla introducida por el SENIAT
switch ($digitos[0]) {
case 'V':
$digitoEspecial = 1;
break;
case 'E':
$digitoEspecial = 2;
break;
case 'C':
case 'J':
$digitoEspecial = 3;
break;
case 'P':
$digitoEspecial = 4;
break;
case 'G':
$digitoEspecial = 5;
break;
}
$suma = (array_sum($digitos) - $digitos[9]) + ($digitoEspecial*4);
$residuo = $suma % 11;
$resta = 11 - $residuo;
$digitoVerificador = ($resta >= 10) ? 0 : $resta;
if ($digitoVerificador != $digitos[9]) {
$retorno = false;
}
}
return $retorno;
}
}