218 lines
7.1 KiB
PHP
218 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Exception;
|
|
|
|
class PadronEstatalService
|
|
{
|
|
private string $soapUrl;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->soapUrl = config('services.padron_estatal.url');
|
|
}
|
|
|
|
public function getVehiculoByNiv(string $niv): array
|
|
{
|
|
return $this->consultarPadron('niv', $niv);
|
|
}
|
|
|
|
public function getVehiculoByPlaca(string $placa): array
|
|
{
|
|
return $this->consultarPadron('placa', $placa);
|
|
}
|
|
|
|
public function getVehiculoByFolio(string $folio): array
|
|
{
|
|
return $this->consultarPadron('folio', $folio);
|
|
}
|
|
|
|
/**
|
|
* Consulta el padrón vehicular estatal
|
|
*/
|
|
private function consultarPadron(string $tipo, string $valor): array
|
|
{
|
|
// Construir el Data en formato JSON
|
|
$data = json_encode([
|
|
'tipo' => $tipo,
|
|
'valor' => $valor
|
|
]);
|
|
|
|
// Construir el cuerpo SOAP
|
|
$soapBody = <<<XML
|
|
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="http://mx/tgc/ConsultaPadronVehicular.wsdl">
|
|
<soapenv:Header/>
|
|
<soapenv:Body>
|
|
<wsdl:getVehiculosRepuve>
|
|
<Data>{$data}</Data>
|
|
</wsdl:getVehiculosRepuve>
|
|
</soapenv:Body>
|
|
</soapenv:Envelope>
|
|
XML;
|
|
|
|
// Configurar cURL
|
|
$ch = curl_init($this->soapUrl);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $soapBody);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
'Content-Type: text/xml; charset=utf-8',
|
|
'SOAPAction: ""',
|
|
'Content-Length: ' . strlen($soapBody)
|
|
]);
|
|
|
|
// Ejecutar la petición
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$error = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
if ($error) {
|
|
throw new Exception("Error en la petición al padrón estatal: {$error}");
|
|
}
|
|
|
|
if ($httpCode !== 200) {
|
|
throw new Exception("Error HTTP {$httpCode} al consultar padrón estatal");
|
|
}
|
|
|
|
// Parsear la respuesta
|
|
return $this->parsearRespuesta($response);
|
|
}
|
|
|
|
/**
|
|
* Parsea la respuesta del padrón estatal
|
|
*/
|
|
private function parsearRespuesta(string $soapResponse): array
|
|
{
|
|
// Extraer el contenido del tag
|
|
preg_match('/<result>(.*?)<\/result>/s', $soapResponse, $matches);
|
|
|
|
if (!isset($matches[1])) {
|
|
throw new Exception("No se pudo extraer el resultado del padrón estatal");
|
|
}
|
|
|
|
$jsonContent = trim($matches[1]);
|
|
|
|
// Decodificar el JSON
|
|
$result = json_decode($jsonContent, true);
|
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
throw new Exception("Error al decodificar JSON del padrón estatal: " . json_last_error_msg());
|
|
}
|
|
|
|
// La respuesta es un array con un objeto que tiene error y datos
|
|
if (!isset($result[0])) {
|
|
throw new Exception("Formato de respuesta inesperado del padrón estatal");
|
|
}
|
|
|
|
$data = $result[0];
|
|
|
|
// Verificar si hay error
|
|
if ($data['error'] !== 0) {
|
|
throw new Exception("Error en consulta al padrón estatal: código {$data['error']}");
|
|
}
|
|
|
|
// Verificar si hay datos
|
|
if (!isset($data['datos'][0])) {
|
|
throw new Exception("No se encontraron datos del vehículo en el padrón estatal");
|
|
}
|
|
|
|
return $data['datos'][0];
|
|
}
|
|
|
|
/**
|
|
* Extrae los datos del vehículo del resultado
|
|
*/
|
|
public function extraerDatosVehiculo(array $datos): array
|
|
{
|
|
// Convertir fecha de DD/MM/YYYY a YYYY-MM-DD
|
|
$fechaexpedicion = null;
|
|
if (isset($datos['fechaexp']) && $datos['fechaexp']) {
|
|
$fechaexpedicion = $this->convertirFecha($datos['fechaexp']);
|
|
}
|
|
|
|
return [
|
|
'placa' => $datos['placa'] ?? null,
|
|
'niv' => $datos['niv'] ?? null,
|
|
'marca' => $datos['marca'] ?? null,
|
|
'linea' => $datos['submarca'] ?? null,
|
|
'sublinea' => $datos['version'] ?? null,
|
|
'modelo' => $datos['modelo'] ?? null,
|
|
'color' => $datos['color'] ?? null,
|
|
'numero_motor' => $datos['motor'] ?? null,
|
|
'clase_veh' => $datos['clase_veh'] ?? null,
|
|
'tipo_servicio' => $datos['tipo_uso'] ?? null,
|
|
'rfv' => $datos['rfv'] ?? null,
|
|
'ofcexpedicion' => $datos['ofcexp'] ?? null,
|
|
'fechaexpedicion' => $fechaexpedicion,
|
|
'tipo_veh' => $datos['tipo_veh'] ?? null,
|
|
'numptas' => $datos['numptas'] ?? null,
|
|
'observac' => $datos['observac'] ?? null,
|
|
'cve_vehi' => $datos['cve_vehi'] ?? null,
|
|
'tipo_mov' => $datos['tipo_mov'] ?? null,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Convierte fecha de DD/MM/YYYY a YYYY-MM-DD
|
|
*/
|
|
private function convertirFecha(?string $fecha): ?string
|
|
{
|
|
if (!$fecha) {
|
|
return null;
|
|
}
|
|
|
|
// Si ya está en formato YYYY-MM-DD, retornar tal cual
|
|
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $fecha)) {
|
|
return $fecha;
|
|
}
|
|
|
|
// Convertir de DD/MM/YYYY a YYYY-MM-DD
|
|
if (preg_match('/^(\d{2})\/(\d{2})\/(\d{4})$/', $fecha, $matches)) {
|
|
return "{$matches[3]}-{$matches[2]}-{$matches[1]}";
|
|
}
|
|
|
|
// Si no coincide con ningún formato esperado, retornar null
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Extrae los datos del propietario del resultado
|
|
*/
|
|
public function extraerDatosPropietario(array $datos): array
|
|
{
|
|
// Construir dirección completa
|
|
$addressParts = array_filter([
|
|
$datos['callep'] ?? null,
|
|
isset($datos['num_ext']) && $datos['num_ext'] ? "Num {$datos['num_ext']}" : null,
|
|
isset($datos['num_int']) && $datos['num_int'] ? "Int {$datos['num_int']}" : null,
|
|
$datos['colonia'] ?? null,
|
|
isset($datos['cp']) && $datos['cp'] ? "CP {$datos['cp']}" : null,
|
|
isset($datos['munic']) && $datos['munic'] ? "Mun {$datos['munic']}" : null,
|
|
isset($datos['ent_fed']) && $datos['ent_fed'] ? "Edo {$datos['ent_fed']}" : null,
|
|
]);
|
|
|
|
$address = implode(', ', $addressParts);
|
|
|
|
return [
|
|
'name' => $datos['nombre'] ?? null,
|
|
'paternal' => $datos['ap_paterno'] ?? null,
|
|
'maternal' => $datos['ap_materno'] ?? null,
|
|
'rfc' => $datos['rfc'] ?? null,
|
|
'curp' => $datos['curp'] ?? null,
|
|
'address' => $address,
|
|
'tipopers' => $datos['tipopers'] ?? null,
|
|
'pasaporte' => $datos['pasaporte'] ?? null,
|
|
'licencia' => $datos['licencia'] ?? null,
|
|
'ent_fed' => $datos['ent_fed'] ?? null,
|
|
'munic' => $datos['munic'] ?? null,
|
|
'callep' => $datos['callep'] ?? null,
|
|
'num_ext' => $datos['num_ext'] ?? null,
|
|
'num_int' => $datos['num_int'] ?? null,
|
|
'colonia' => $datos['colonia'] ?? null,
|
|
'cp' => $datos['cp'] ?? null,
|
|
];
|
|
}
|
|
}
|