forked from ivansabik/estafeta-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Estafeta.php
247 lines (224 loc) · 10.1 KB
/
Estafeta.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
require 'vendor/autoload.php';
define('URL_RASTREAR', 'http://rastreo3.estafeta.com/RastreoWebInternet/consultaEnvio.do');
define('URL_COTIZAR', 'http://herramientascs.estafeta.com/Cotizador/Cotizar');
define('URL_FIRMA', 'http://rastreo3.estafeta.com');
define('URL_COMPROBANTE', 'http://rastreo3.estafeta.com/RastreoWebInternet/consultaEnvio.do?dispatch=doComprobanteEntrega&guiaEst=');
use Ivansabik\DomHunter\DomHunter;
use Ivansabik\DomHunter\Tabla;
use Ivansabik\DomHunter\KeyValue;
use Ivansabik\DomHunter\IdUnico;
use Ivansabik\DomHunter\NodoDom;
class Estafeta {
# Endpoint de /rastreo?numero=<NUMERO DE GUIA O CODIGO DE RASTREO>
public function rastreo($numero = NULL) {
if (!$numero) {
return array('mensaje_error' => 'Falta el parametro "numero"', 'error' => 1);
}
$tipo_numero = $this->_valida_numero($numero);
# Params iniciales
$params_peticion = array(
'idioma' => 'es',
'dispatch' => 'doRastreoInternet',
'guias' => $numero
);
if ($tipo_numero == 'guia') {
$params_peticion['tipoGuia'] = 'ESTAFETA';
} elseif ($tipo_numero == 'rastreo') {
$params_peticion['tipoGuia'] = 'REFERENCE';
} elseif ($tipo_numero == 'invalido') {
return array('mensaje_error' => 'No es un numero de guia o codigo de rastreo valido', 'error' => 2);
}
# Busca dom
$hunter = new DomHunter(URL_RASTREAR, 1);
$hunter->arrParamsPeticion = $params_peticion;
$presas = array();
$presas[] = array('numero_guia', new KeyValue('mero de gu'));
$presas[] = array('codigo_rastreo', new KeyValue('digo de rastreo'));
$presas[] = array('origen', new KeyValue('origen'));
$presas[] = array('destino', new KeyValue('destino'));
$presas[] = array('cp_destino', new IdUnico(5, 'num'));
$presas[] = array('servicio', new KeyValue('entrega garantizada', FALSE));
$presas[] = array('estatus', new KeyValue('status del servicio'));
$presas[] = array('fecha_recoleccion', new KeyValue('fecha de recoleccion'));
$presas[] = array('fecha_programada', new KeyValue('de entrega', TRUE, TRUE));
$presas[] = array('fecha_entrega', new KeyValue('Fecha y hora de entrega'));
$presas[] = array('tipo_envio', new KeyValue('tipo de envio'));
$presas[] = array('peso', new KeyValue('Peso kg'));
$presas[] = array('peso_volumetrico', new KeyValue('Peso volumétrico kg'));
$presas[] = array('recibio', new KeyValue('recibi'));
$presas[] = array('dimensiones', new KeyValue('Dimensiones cm'));
$columnas = array('fecha', 'lugar_movimiento', 'comentarios');
$presas[] = array('movimientos', new Tabla(array('ocurrencia' => -1), $columnas, 3));
$hunter->arrPresas = $presas;
$hunted = $hunter->hunt();
if (strpos($hunted['estatus'], 'No hay informaci') !== false) {
return array('estatus' => $hunted['estatus']);
} else {
# Busca coordenadas de origen
try {
$ciudad = $hunted['origen'];
$direccion = $ciudad . ', Mexico';
$geocoder = new GoogleMapsGeocoder($direccion);
$response = $geocoder->geocode();
$latitud = $response['results'][0]['geometry']['location']['lat'];
$longitud = $response['results'][0]['geometry']['location']['lng'];
$hunted['origen'] = array(
'nombre' => $ciudad,
'latitud' => $latitud,
'longitud' => $longitud
);
} catch (Exception $e) {
}
# Busca coordenadas de destino
try {
$ciudad = $hunted['destino'];
$cp = $hunted['cp_destino'];
$direccion = $ciudad . ', Mexico';
$geocoder = new GoogleMapsGeocoder($direccion);
$response = $geocoder->geocode();
$latitud = $response['results'][0]['geometry']['location']['lat'];
$longitud = $response['results'][0]['geometry']['location']['lng'];
unset($hunted['cp_destino']);
$hunted['destino'] = array(
'nombre' => $ciudad,
'latitud' => $latitud,
'longitud' => $longitud,
'cp_destino' => $cp
);
} catch (Exception $e) {
}
# Descomposicion de dimensiones, de string a ancho, alto, largo
$dimensiones = $hunted['dimensiones'];
$piezas_dimensiones = explode('x', $dimensiones);
$alto = $piezas_dimensiones[0];
$largo = $piezas_dimensiones[1];
$ancho = $piezas_dimensiones[2];
$hunted['dimensiones'] = array('alto' => $alto, 'largo' => $largo, 'ancho' => $ancho);
# Construye historial de movimientos
$historial = $hunted['movimientos'];
$movimientos = array();
foreach ($historial as $evento) {
$movimiento = array();
$movimiento['descripcion'] = $evento['lugar_movimiento'];
$movimiento['fecha'] = $evento['fecha'];
# Comentado por buggiento https://github.com/ivansabik/dom-hunter/issues/4
# movimiento['comentarios'] = $evento['comentarios'];
$movimiento['id'] = self::id_movimiento($movimiento['descripcion']);
$movimientos[] = $movimiento;
}
$hunted['movimientos'] = $movimientos;
# Firma de recibido
$presas = array();
$opciones_navegacion = array('getElementById' => $hunted['numero_guia'] . 'FIR', 'nextSibling' => '', 'find' => 'img');
$presas[] = array('firma_recibido', new NodoDom(array('navegacion' => $opciones_navegacion), 'src'));
$hunter->arrPresas = $presas;
$hunted_firma = $hunter->hunt();
if (isset($hunted['firma_recibido'])) {
$hunted['firma_recibido'] = URL_FIRMA . $hunted_firma['firma_recibido'];
} else {
$hunted['firma_recibido'] = '';
}
# Comprobante de entrega
$hunted['comprobante_entrega'] = URL_COMPROBANTE . $hunted['numero_guia'];
return $hunted;
}
}
# Endpoint de /cotizacion?cp_origen=01210&cp_destino=86035
public function cotizacion($cp_origen = NULL, $cp_destino = NULL, $tipo = 'sobre', $peso = NULL, $alto = NULL, $largo = NULL, $ancho = NULL) {
if (!$cp_origen) {
return array('mensaje_error' => 'Falta el parametro "cp_origen"', 'error' => 3);
}
if (!$cp_destino) {
return array('mensaje_error' => 'Falta el parametro "cp_destino"', 'error' => 4);
}
if (!preg_match("/^[0-9]{5}$/", $cp_origen) || !preg_match("/^[0-9]{5}$/", $cp_destino)) {
return array('mensaje_error' => 'No es un codigo postal de origen o destino valido', 'error' => 5);
}
$params_peticion = array(
'CPOrigen' => $cp_origen,
'CPDestino' => $cp_destino,
'Tipo' => $tipo,
'cTipoEnvio' => $tipo
);
# Paquetes
if ($tipo == 'paquete') {
if (!$peso) {
return array('mensaje_error' => 'Falta el parametro "peso" para cotizar paquetes', 'error' => 6);
}
if (!$alto) {
return array('mensaje_error' => 'Falta el parametro "alto" para cotizar paquetes', 'error' => 7);
}
if (!$largo) {
return array('mensaje_error' => 'Falta el parametro "largo" para cotizar paquetes', 'error' => 8);
}
if (!$ancho) {
return array('mensaje_error' => 'Falta el parametro "ancho" para cotizar paquetes', 'error' => 9);
}
$params_peticion['Peso'] = $peso;
$params_peticion['Alto'] = $alto;
$params_peticion['Largo'] = $largo;
$params_peticion['Ancho'] = $ancho;
}
# Busca dom
$hunter = new DomHunter(URL_COTIZAR, 1);
$hunter->arrParamsPeticion = $params_peticion;
$presas = array();
$columnas = array('producto', 'peso_kg', 'tarifa_guia', 'tarifa_combustible', 'cargos_extra', 'sobrepeso_costo', 'sobrepeso_combustible', 'costo_total');
$presas[] = array('costos', new Tabla(array('ocurrencia' => -1), $columnas, 10, true));
$hunter->arrPresas = $presas;
$hunted = $hunter->hunt();
return $hunted;
}
private function _valida_numero($numero) {
# Numero de guia (22 y alfanumérico)
if (strlen($numero) == 22 && ctype_alnum($numero)) {
return 'guia';
}
# Codigo de rastreo (10 y numérico)
if (strlen($_GET['numero']) == 10 && is_numeric($numero)) {
return 'rastreo';
}
return 'invalido';
}
public function id_movimiento($texto) {
# En proceso de entrega
if (preg_match('/EN PROCESO DE ENTREGA/i', $texto)) {
return 1;
}
# Llegada a CEDI
if (preg_match('/LLEGADA A CENTRO DE DISTRIBUCI/i', $texto)) {
return 2;
}
# En ruta foránea hacia un destino
if (preg_match('/EN RUTA FOR/i', $texto)) {
return 3;
}
# Recolección en oficina por ruta local
if (preg_match('/N EN OFICINA/i', $texto)) {
return 4;
}
# Recibido en oficina
if (preg_match('/RECIBIDO EN OFICINA/i', $texto)) {
return 5;
}
# Movimiento en CEDI
if (preg_match('/MOVIMIENTO EN CENTRO DE DISTRIBUCI/i', $texto)) {
return 6;
}
# Aclaracion en proceso
if (preg_match('/N EN PROCESO/i', $texto)) {
return 7;
}
# En ruta local
if (preg_match('/EN RUTA LOCAL/i', $texto)) {
return 8;
}
# Movimiento local
if (preg_match('/MOVIMIENTO LOCAL/i', $texto)) {
return 9;
}
return -1;
}
}
?>