Files
sirpremiumv2/app/Http/Livewire/ShowDiagnostico.php
T
2026-04-18 14:08:14 -05:00

102 lines
3.7 KiB
PHP

<?php
namespace App\Http\Livewire;
use App\Models\Cuentas;
use App\Models\Historial_cuenta;
use App\Models\Historiale;
use App\Models\Log_general;
use Livewire\Component;
class ShowDiagnostico extends Component
{
public $buscar_cuenta = '';
public $cuenta = null;
public $historial_perfiles = [];
public $logs_relacionados = [];
public $resumen = [];
public function buscar()
{
$this->cuenta = null;
$this->historial_perfiles = [];
$this->logs_relacionados = [];
$this->resumen = [];
if (empty(trim($this->buscar_cuenta))) {
return;
}
$this->cuenta = Cuentas::with('servicio')
->where('correo', 'ILIKE', '%' . trim($this->buscar_cuenta) . '%')
->first();
if (!$this->cuenta) {
return;
}
// Todos los historial_cuenta de esta cuenta, con su historial padre
$this->historial_perfiles = Historial_cuenta::with(['historial.vendedor', 'historial.cliente'])
->where('cuenta_id', $this->cuenta->id)
->orderBy('perfil')
->orderBy('created_at', 'desc')
->get()
->map(function ($hc) {
return [
'id' => $hc->id,
'perfil' => $hc->perfil,
'historial_id' => $hc->historial_id,
'estado_historial' => $hc->historial->estado ?? 'N/A',
'vendedor' => $hc->historial->vendedor->name ?? 'N/A',
'cliente' => $hc->historial->nombre_cliente ?? ($hc->historial->cliente->name ?? 'N/A'),
'fecha_inicio' => $hc->historial->fecha_inicio ?? 'N/A',
'fecha_final' => $hc->historial->fecha_final ?? 'N/A',
'created_at' => $hc->created_at,
];
})
->toArray();
// Logs relacionados con todos los historiales de esta cuenta
$historial_ids = Historial_cuenta::where('cuenta_id', $this->cuenta->id)
->whereNotNull('historial_id')
->pluck('historial_id')
->unique();
$this->logs_relacionados = Log_general::with('user')
->whereIn('historial_id', $historial_ids)
->orderBy('created_at', 'desc')
->take(50)
->get()
->map(function ($log) {
return [
'id' => $log->id,
'tipo' => $log->tipo,
'usuario' => $log->user->name ?? 'N/A',
'detalle' => $log->detalle,
'historial_id' => $log->historial_id,
'created_at' => $log->created_at,
];
})
->toArray();
// Resumen por perfil
$perfiles_activos = collect($this->historial_perfiles)->where('estado_historial', 'activo');
$perfiles_cancelados = collect($this->historial_perfiles)->where('estado_historial', 'cancelado');
$this->resumen = [
'total_registros' => count($this->historial_perfiles),
'activos' => $perfiles_activos->count(),
'cancelados' => $perfiles_cancelados->count(),
'max_perfil_activo' => $perfiles_activos->max('perfil') ?? 0,
'perfiles_unicos_activos' => $perfiles_activos->pluck('perfil')->unique()->sort()->values()->toArray(),
'perfil_6_activos' => $perfiles_activos->where('perfil', 6)->count(),
'perfil_6_cancelados' => $perfiles_cancelados->where('perfil', 6)->count(),
];
}
public function render()
{
return view('livewire.show-diagnostico');
}
}