Files
Sio-Back/app/Http/Controllers/Operador/ServiciosController.php
SIO Admin de656b70a2 feat: Actualizacion sistema SIO Backend
- Nuevo modulo de historial de cambios (ServicioHistorial)
- Observer para tracking automatico de cambios en servicios
- Correccion de variables auxiliar en ServiciosController
- Actualizacion de configuraciones y migraciones
- Endpoint para consultar historial de cambios

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-17 23:01:55 +00:00

268 lines
9.4 KiB
PHP
Executable File

<?php
namespace App\Http\Controllers\Operador;
use App\Models\Parametro;
use const App\Http\Controllers\ANCLAJE_SERVICIO_HORAS;
use const App\Http\Controllers\LLAVE_ANCLAJE_SERVICIO_HORAS;
use const App\Http\Controllers\PAGADO;
use const App\Http\Controllers\PENDIENTE;
use const App\Http\Controllers\REPROGRAMADO;
use const App\Http\Controllers\VISITA;
use App\Http\Requests\Operador\RechazarSolicitudRequest;
use App\Models\CatServicio;
use App\Models\ServicioDet;
use App\Models\ServicioEnc;
use App\Models\ServicioProgreso;
use App\Models\User;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;
use Illuminate\Support\Facades\DB;
use Log;
class ServiciosController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$user = Auth::user();
if(!Carbon::parse($user->last_login)->isToday()){
return response()->json(['error'=>'Es necesario iniciar sesión el día de hoy'],420);
}
$ayer = Carbon::yesterday()->format('Y-m-d 23:00:00');
$hoy = Carbon::now()->format('Y-m-d 23:59:59');
$campos = $this->camposSolicitudesServiciosOperador();
$s = ServicioDet::select($campos)
->joinDetalleServicioDet()
->joinDetalleServicioDetMovil()
->leftJoin('servicios_progreso', function($join)
{
$join->on('servicios_enc.id', '=', 'servicios_progreso.servicio_enc_id');
$join->on('servicios_det.id', '=', 'servicios_progreso.servicio_det_id');
})
->whereNull('servicios_progreso.id')
->whereIn('servicios_det.estatus_servicio_id', [PENDIENTE, REPROGRAMADO, VISITA, PAGADO])
->where('servicios_det.operador_id', $user->id)
->where('servicios_enc.sucursal_id', $user->sucursal_id)
->whereBetween('servicios_det.fecha_solicitud', [$ayer, $hoy])
->get();
$servicios_progresos = ServicioProgreso::select('servicio_det_id')->get();
if(count($servicios_progresos) != 0){
$servicios = collect($s)->whereNotIn('id',$servicios_progresos->pluck('servicio_det_id'))->all();
}else{
$servicios = $s;
}
$tiempo_anclaje = Parametro::where('llave',LLAVE_ANCLAJE_SERVICIO_HORAS)->first();
$tiempo_anclaje = ($tiempo_anclaje)? $tiempo_anclaje->valor : ANCLAJE_SERVICIO_HORAS;
$tiempo_anclaje = (1 + $tiempo_anclaje) * 60;
$data = [];
$hoy = Carbon::now();
$hoy_dia = $hoy->format('Y-m-d');
foreach ($servicios as $s){
if(Carbon::parse($s->fecha_solicitud)->format('Y-m-d') == $hoy_dia){
array_push($data, $s);
}else{
if(Carbon::parse($s->fecha_solicitud)->diffInMinutes($hoy) <= $tiempo_anclaje){
array_push($data, $s);
}
}
}
//Comentado por rechazo del cliente
/*$fecha_actual = Carbon::now()->format('Y-m-d');
$salida = [];
foreach ($servicios as $s){
$fecha_asignacion = Carbon::createFromTimeString($s->fecha_agenda)->format('Y-m-d');
if($fecha_asignacion == $fecha_actual && $s->aceptado){
$salida[] = $s;
}elseif ($fecha_asignacion != $fecha_actual){
$salida[] = $s;
}
}*/
return response()->success($data);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$user = Auth::user();
$campos = $this->camposSolicitudesServiciosOperador();
$servicios = ServicioDet::select($campos)
->joinDetalleServicioDet()
->joinDetalleServicioDetMovil()
->where('servicios_det.id', $id)
->whereIn('servicios_det.estatus_servicio_id', [PENDIENTE, REPROGRAMADO, VISITA, PAGADO])
->where('servicios_det.operador_id', $user->id)
->where('servicios_enc.sucursal_id', $user->sucursal_id)
->whereDate('servicios_det.fecha_solicitud', Carbon::now()->format('Y-m-d'))
->first();
return response()->success($servicios);
}
public function aceptarSolicitud($id){
DB::beginTransaction();
try {
$ambiente = strtolower(config('ambiente')['ambiente']);
$url = config('ambiente')[$ambiente];
$servicio_det = ServicioDet::where('id',$id)->first();
if(!$servicio_det){
DB::rollBack();
return response()->unprocessable('Error', ['El servicio no se encontró en nuestros registros.']);
}
$servicio_det->update(['aceptado' => 1]);
$servicio_enc = ServicioEnc::where('id',$servicio_det->servicio_enc_id)->first();
$usuario = User::where('id', $servicio_enc->usuario_agenda_id)->first();
if($usuario->token_firebase) {
$authorization = config('firebase')['authorization'];
$project_id = config('firebase')['project_id'];
$endpoint = config('firebase')['endpoint'];
$headers = [
'Authorization' => 'key=' . $authorization,
'project_id' => $project_id,
'content-type' => 'application/json'
];
$cat_servicio = CatServicio::where('id', $servicio_det->servicio_id)->first();
$datos = ['registration_ids' => array($usuario->token_firebase),
'data' => ['url' => $url, 'solicitud_id' => $servicio_det->servicio_enc_id,'servicio_id' => $id, 'title' => 'Servicio Aceptado', 'body' => $cat_servicio->nombre, 'observacion' => '']];
$client = new Client();
$client->post($endpoint, [
'headers' => $headers,
'json' => $datos,
]);
}
DB::commit();
return response()->success(['result' => 'ok']);
} catch (\Exception $e) {
DB::rollBack();
return response()->unprocessable('Error', ['Error al aceptar solicitud.']);
}
}
public function rechazarSolicitud(RechazarSolicitudRequest $request, $id){
$data = $request->all();
DB::beginTransaction();
try {
$ambiente = strtolower(config('ambiente')['ambiente']);
$url = config('ambiente')[$ambiente];
$servicio_det = ServicioDet::where('id',$id)->first();
if(!$servicio_det){
DB::rollBack();
return response()->unprocessable('Error', ['El servicio no se encontró en nuestros registros.']);
}
$servicio_det->update(['aceptado' => 0, 'operador_id' => null, 'vehiculo_id' => null, 'auxiliar_1' => null, 'auxiliar_2' => null, 'observacion' => $data['observacion']]);
$servicio_enc = ServicioEnc::where('id',$servicio_det->servicio_enc_id)->first();
$usuario = User::where('id', $servicio_enc->usuario_agenda_id)->first();
if($usuario->token_firebase) {
$authorization = config('firebase')['authorization'];
$project_id = config('firebase')['project_id'];
$endpoint = config('firebase')['endpoint'];
$headers = [
'Authorization' => 'key=' . $authorization,
'project_id' => $project_id,
'content-type' => 'application/json'
];
$cat_servicio = CatServicio::where('id', $servicio_det->servicio_id)->first();
$datos = ['registration_ids' => array($usuario->token_firebase),
'data' => ['url' => $url, 'solicitud_id' => $servicio_det->servicio_enc_id, 'servicio_id' => $id, 'title' => 'Servicio Rechazado', 'body' => $cat_servicio->nombre, 'observacion' => $data['observacion']]];
$client = new Client();
$client->post($endpoint, [
'headers' => $headers,
'json' => $datos,
]);
}
DB::commit();
return response()->success(['result' => 'ok']);
} catch (\Exception $e) {
DB::rollBack();
return response()->unprocessable('Error', ['Error al rechazar solicitud.']);
}
}
public function ServiciosDiaSiguiente()
{
$user = Auth::user();
$campos = $this->camposSolicitudesServiciosOperador();
$s = ServicioDet::select($campos)
->joinDetalleServicioDet()
->joinDetalleServicioDetMovil()
->whereIn('servicios_det.estatus_servicio_id', [PENDIENTE, REPROGRAMADO, VISITA, PAGADO])
->where('servicios_det.operador_id', $user->id)
->where('servicios_enc.sucursal_id', $user->sucursal_id)
->whereDate('servicios_det.fecha_solicitud', Carbon::tomorrow()->format('Y-m-d'))
->get();
$servicios_progresos = ServicioProgreso::select('servicio_det_id')->get();
if(count($servicios_progresos) != 0){
$servicios = collect($s)->whereNotIn('id',$servicios_progresos->pluck('servicio_det_id'))->all();
}else{
$servicios = $s;
}
return response()->success($servicios);
}
}