Fix: actualización auto
This commit is contained in:
parent
e0649e85ef
commit
2db309a203
@ -20,6 +20,7 @@
|
|||||||
use Exception;
|
use Exception;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
use App\Jobs\ProcessRepuveResponse;
|
use App\Jobs\ProcessRepuveResponse;
|
||||||
|
use App\Models\CatalogTagStatus;
|
||||||
|
|
||||||
class UpdateController extends Controller
|
class UpdateController extends Controller
|
||||||
{
|
{
|
||||||
@ -235,9 +236,164 @@ public function vehicleUpdate(Request $request)
|
|||||||
->first();
|
->first();
|
||||||
|
|
||||||
if (!$vehicle) {
|
if (!$vehicle) {
|
||||||
return ApiResponse::NOT_FOUND->response([
|
DB::beginTransaction();
|
||||||
'message' => 'No se encontró un vehículo con esa placa en el sistema',
|
|
||||||
|
// Consultar Padrón Estatal para datos del vehículo y propietario
|
||||||
|
try {
|
||||||
|
$datosCompletosRaw = $this->padronEstatalService->getVehiculoByPlaca($placa);
|
||||||
|
$vehicleDataEstatal = $this->padronEstatalService->extraerDatosVehiculo($datosCompletosRaw);
|
||||||
|
$ownerDataEstatal = $this->padronEstatalService->extraerDatosPropietario($datosCompletosRaw);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
DB::rollBack();
|
||||||
|
return ApiResponse::BAD_REQUEST->response([
|
||||||
|
'message' => 'Error al consultar el Padrón Estatal',
|
||||||
|
'placa' => $placa,
|
||||||
|
'error' => $e->getMessage(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Consultar REPUVE Federal para obtener folio y tag_number
|
||||||
|
try {
|
||||||
|
$repuveResponse = $this->repuveService->consultarVehiculo(null, $placa);
|
||||||
|
|
||||||
|
if ($repuveResponse['has_error']) {
|
||||||
|
DB::rollBack();
|
||||||
|
return ApiResponse::BAD_REQUEST->response([
|
||||||
|
'message' => 'Error al consultar REPUVE Federal',
|
||||||
|
'placa' => $placa,
|
||||||
|
'error' => $repuveResponse['error_message'] ?? 'Error desconocido',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parsear folio y tag_number de la respuesta
|
||||||
|
$rawResponse = $repuveResponse['raw_response'];
|
||||||
|
$campos = explode('|', $rawResponse);
|
||||||
|
|
||||||
|
$folio = $campos[29] ?? null;
|
||||||
|
$tagNumber = $campos[30] ?? null;
|
||||||
|
|
||||||
|
if (!$folio || !$tagNumber) {
|
||||||
|
DB::rollBack();
|
||||||
|
return ApiResponse::BAD_REQUEST->response([
|
||||||
|
'message' => 'No se pudo obtener el folio o tag_number del REPUVE Federal',
|
||||||
|
'placa' => $placa,
|
||||||
|
'folio_obtenido' => $folio,
|
||||||
|
'tag_number_obtenido' => $tagNumber,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
} catch (Exception $e) {
|
||||||
|
DB::rollBack();
|
||||||
|
return ApiResponse::BAD_REQUEST->response([
|
||||||
|
'message' => 'Error al consultar REPUVE Federal',
|
||||||
|
'placa' => $placa,
|
||||||
|
'error' => $e->getMessage(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validar NIV
|
||||||
|
$nivEstatal = $vehicleDataEstatal['niv'];
|
||||||
|
if (!$nivEstatal) {
|
||||||
|
DB::rollBack();
|
||||||
|
return ApiResponse::BAD_REQUEST->response([
|
||||||
|
'message' => 'El Padrón Estatal no retornó un NIV válido',
|
||||||
|
'placa' => $placa,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validar RFC del propietario
|
||||||
|
if (!$ownerDataEstatal['rfc']) {
|
||||||
|
DB::rollBack();
|
||||||
|
return ApiResponse::BAD_REQUEST->response([
|
||||||
|
'message' => 'El Padrón Estatal no retornó un RFC válido para el propietario',
|
||||||
|
'placa' => $placa,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Crear o actualizar Owner
|
||||||
|
$owner = Owner::updateOrCreate(
|
||||||
|
['rfc' => $ownerDataEstatal['rfc']],
|
||||||
|
$ownerDataEstatal
|
||||||
|
);
|
||||||
|
|
||||||
|
// Crear Vehicle
|
||||||
|
$vehicle = Vehicle::create(array_merge(
|
||||||
|
$vehicleDataEstatal,
|
||||||
|
['owner_id' => $owner->id]
|
||||||
|
));
|
||||||
|
|
||||||
|
// Verificar si el Tag ya existe por folio
|
||||||
|
$tag = Tag::where('folio', $folio)->first();
|
||||||
|
|
||||||
|
if ($tag) {
|
||||||
|
// Si el tag existe, actualizar con el nuevo vehículo
|
||||||
|
$statusAssigned = CatalogTagStatus::where('code', Tag::STATUS_ASSIGNED)->first();
|
||||||
|
|
||||||
|
if (!$statusAssigned) {
|
||||||
|
DB::rollBack();
|
||||||
|
return ApiResponse::INTERNAL_ERROR->response([
|
||||||
|
'message' => 'No se encontró el estado "disponible" en el catálogo de estados de TAG',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$tag->update([
|
||||||
|
'vehicle_id' => $vehicle->id,
|
||||||
|
'tag_number' => $tagNumber,
|
||||||
|
'status_id' => $statusAssigned->id,
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
// Si no existe, crear nuevo Tag
|
||||||
|
$statusAssigned = CatalogTagStatus::where('code', Tag::STATUS_ASSIGNED)->first();
|
||||||
|
|
||||||
|
if (!$statusAssigned) {
|
||||||
|
DB::rollBack();
|
||||||
|
return ApiResponse::INTERNAL_ERROR->response([
|
||||||
|
'message' => 'No se encontró el estado "disponible" en el catálogo de estados de TAG',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$tag = Tag::create([
|
||||||
|
'folio' => $folio,
|
||||||
|
'tag_number' => $tagNumber,
|
||||||
|
'vehicle_id' => $vehicle->id,
|
||||||
|
'status_id' => $statusAssigned->id,
|
||||||
|
'module_id' => Auth::user()->module_id ?? null,
|
||||||
|
'package_id' => null,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Crear Record
|
||||||
|
$record = Record::create([
|
||||||
|
'folio' => $folio,
|
||||||
|
'vehicle_id' => $vehicle->id,
|
||||||
|
'user_id' => Auth::id(),
|
||||||
|
'module_id' => Auth::user()->module_id ?? null,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// REPUVE Nacional
|
||||||
|
/* ProcessRepuveResponse::dispatch($record->id, $datosCompletosRaw); */
|
||||||
|
|
||||||
|
DB::commit();
|
||||||
|
|
||||||
|
// Recargar relaciones para la respuesta
|
||||||
|
$record->load(['vehicle.owner', 'vehicle.tag.status', 'files']);
|
||||||
|
|
||||||
|
return ApiResponse::OK->response([
|
||||||
|
'message' => 'Vehículo inscrito exitosamente en el sistema',
|
||||||
'placa' => $placa,
|
'placa' => $placa,
|
||||||
|
'is_new' => true,
|
||||||
|
'folio' => $folio,
|
||||||
|
'tag_number' => $tagNumber,
|
||||||
|
'vehicle_info' => [
|
||||||
|
'niv' => $vehicle->niv,
|
||||||
|
'marca' => $vehicle->marca,
|
||||||
|
'linea' => $vehicle->linea,
|
||||||
|
'modelo' => $vehicle->modelo,
|
||||||
|
],
|
||||||
|
'owner_info' => [
|
||||||
|
'rfc' => $owner->rfc,
|
||||||
|
'full_name' => $owner->full_name,
|
||||||
|
],
|
||||||
|
'record' => $record,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -272,7 +428,7 @@ public function vehicleUpdate(Request $request)
|
|||||||
|
|
||||||
if (!$record) {
|
if (!$record) {
|
||||||
return ApiResponse::BAD_REQUEST->response([
|
return ApiResponse::BAD_REQUEST->response([
|
||||||
'message' => 'El vehículo no tiene un expediente (record) asociado',
|
'message' => 'El vehículo no tiene un expediente asociado',
|
||||||
'placa' => $placa,
|
'placa' => $placa,
|
||||||
'vehicle_id' => $vehicle->id,
|
'vehicle_id' => $vehicle->id,
|
||||||
]);
|
]);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user