Update ShowServicios.php

This commit is contained in:
lizandrogd
2025-07-07 17:27:51 -05:00
parent 3ce5d3384e
commit 2c68201f3b
+63 -41
View File
@@ -174,27 +174,28 @@ class ShowServicios extends Component
}); });
}) })
->count(); ->count();
} else { } else {
$cantidad_solicitada = (int) $this->cantidad_tarjetaPlan * (int) $this->pantallas_tarjetaPlan; $cantidad_solicitada = (int) $this->cantidad_tarjetaPlan * (int) $this->pantallas_tarjetaPlan;
$cantidad_restante = $servicio_tarifa->pantallas - $cantidad_solicitada; $cantidad_restante = $servicio_tarifa->pantallas - $cantidad_solicitada;
//Traer todas las cuentas por pantalla 'activo' de los ultimos 15 dias. solo disponibles //Traer todas las cuentas por pantalla 'activo' de los ultimos 15 dias. solo disponibles
$baseQuery = Cuentas::select('*')->whereDate('inicio', '>=', $this->fecha) $cuentas_pantallas = Cuentas::whereDate('inicio', '>=', $this->fecha)
->whereDate('vencimiento', '>=', $this->fecha_final_1) ->whereDate('vencimiento', '>=', $this->fecha_final_1)
->whereDate('vencimiento', '<=', $this->fecha_final_2) ->whereDate('vencimiento', '<=', $this->fecha_final_2)
->where('servicio_id', $servicio_tarifa->id) ->where('servicio_id', $servicio_tarifa->id)
->where('estado', 'activo') ->where('estado', 'activo')
->withCount('historiales') ->whereRaw('(
->where('estado', 'activo'); SELECT COUNT(*)
FROM historiales_cuenta
$cuentas_pantallas = Cuentas::fromSub($baseQuery, 'alias') WHERE historiales_cuenta.cuenta_id = cuentas.id
->where('historiales_count', '<=', $cantidad_restante) ) <= ?', [$cantidad_restante])
->orderBy('inicio', 'ASC') ->orderBy('inicio', 'ASC')
->lockForUpdate()
->get(); ->get();
//dd($cuentas_pantallas); // con count da un numero entero //dd($cuentas_pantallas); // con count da un numero entero
$cuentas_pantallas_tomadas = Historial_cuenta::whereIn('cuenta_id', $cuentas_pantallas $cuentas_pantallas_tomadas = Historial_cuenta::whereIn('cuenta_id', $cuentas_pantallas
@@ -210,7 +211,6 @@ class ShowServicios extends Component
} else { } else {
$this->disponibles = $cuentas_pantallas; $this->disponibles = $cuentas_pantallas;
} }
} }
//dd($this->tarjetaPlan); //dd($this->tarjetaPlan);
@@ -222,7 +222,6 @@ class ShowServicios extends Component
$this->cantidad_tarjetaPlan = 1; $this->cantidad_tarjetaPlan = 1;
$this->tarjetaPlan = false; $this->tarjetaPlan = false;
$this->tarifa_id = ''; $this->tarifa_id = '';
} }
public function notificacionPocas($servicio, $restantes) public function notificacionPocas($servicio, $restantes)
@@ -311,40 +310,41 @@ class ShowServicios extends Component
$suma = $pantalla_servicio - $cantidad_solicitada; $suma = $pantalla_servicio - $cantidad_solicitada;
$cantidad_necesaria = $this->cantidad_tarjetaPlan; $cantidad_necesaria = $this->cantidad_tarjetaPlan;
$baseQuery = Cuentas::select('*') $cuenta = Cuentas::where('servicio_id', $this->servicio_id)
->where('servicio_id', $this->servicio_id)
->whereDate('inicio', '>=', $this->fecha) ->whereDate('inicio', '>=', $this->fecha)
->whereDate('vencimiento', '>=', $this->fecha_final_1) ->whereDate('vencimiento', '>=', $this->fecha_final_1)
->whereDate('vencimiento', '<=', $this->fecha_final_2) ->whereDate('vencimiento', '<=', $this->fecha_final_2)
->where('estado', 'activo') ->where('estado', 'activo')
->withCount('historiales') ->whereRaw('(
->orderBy('inicio', 'ASC'); SELECT COUNT(*)
FROM historiales_cuenta
$cuenta = Cuentas::fromSub($baseQuery, 'alias') WHERE historiales_cuenta.cuenta_id = cuentas.id
->where('historiales_count', '<=', $suma) ) <= ?', [$suma])
->orderBy('inicio', 'ASC') ->orderBy('inicio', 'ASC')
->lockForUpdate()
->get(); ->get();
if ($cuenta->isEmpty()) { if ($cuenta->isEmpty()) {
$suma3 = $pantalla_servicio - $pantallas; $suma3 = $pantalla_servicio - $pantallas;
$cantidad_necesaria = $this->cantidad_tarjetaPlan; $cantidad_necesaria = $this->cantidad_tarjetaPlan;
$baseQuery = Cuentas::select('*')->where('servicio_id', $this->servicio_id)
$cuenta = Cuentas::where('servicio_id', $this->servicio_id)
->whereDate('inicio', '>=', $this->fecha) ->whereDate('inicio', '>=', $this->fecha)
->whereDate('vencimiento', '>=', $this->fecha_final_1) ->whereDate('vencimiento', '>=', $this->fecha_final_1)
->whereDate('vencimiento', '<=', $this->fecha_final_2) ->whereDate('vencimiento', '<=', $this->fecha_final_2)
->withCount('historiales')
->where('estado', 'activo') ->where('estado', 'activo')
->Orderby('inicio', 'ASC'); ->whereRaw('(
SELECT COUNT(*)
FROM historiales_cuenta
$cuenta = Cuentas::fromSub($baseQuery, 'alias') WHERE historiales_cuenta.cuenta_id = cuentas.id
->where('historiales_count', '<=', $suma3) ) <= ?', [$suma3])
->orderBy('inicio', 'ASC') ->orderBy('inicio', 'ASC')
->lockForUpdate()
->get(); ->get();
} }
} elseif ((int) $pantalla_servicio_completa == (int) $this->pantallas_tarjetaPlan) { } elseif ((int) $pantalla_servicio_completa == (int) $this->pantallas_tarjetaPlan) {
//Cuentas completas //Cuentas completas
@@ -357,26 +357,25 @@ class ShowServicios extends Component
->where('estado', 'pendiente') ->where('estado', 'pendiente')
->doesntHave('perfil') ->doesntHave('perfil')
->get(); ->get();
} elseif ((int) $pantalla_servicio < (int) $cantidad_solicitada) { } elseif ((int) $pantalla_servicio < (int) $cantidad_solicitada) {
$suma2 = $pantalla_servicio - $pantallas; $suma2 = $pantalla_servicio - $pantallas;
$cantidad_necesaria = $this->cantidad_tarjetaPlan; $cantidad_necesaria = $this->cantidad_tarjetaPlan;
$baseQuery = Cuentas::select('*')->where('servicio_id', $this->servicio_id)
$cuenta = Cuentas::where('servicio_id', $this->servicio_id)
->whereDate('inicio', '>=', $this->fecha) ->whereDate('inicio', '>=', $this->fecha)
->whereDate('vencimiento', '>=', $this->fecha_final_1) ->whereDate('vencimiento', '>=', $this->fecha_final_1)
->whereDate('vencimiento', '<=', $this->fecha_final_2) ->whereDate('vencimiento', '<=', $this->fecha_final_2)
->Orderby('inicio', 'ASC') ->where('estado', 'activo')
->withCount('historiales') ->whereRaw('(
->where('estado', 'activo'); SELECT COUNT(*)
FROM historiales_cuenta
$cuenta = Cuentas::fromSub($baseQuery, 'alias') WHERE historiales_cuenta.cuenta_id = cuentas.id
->where('historiales_count', '<=', $suma2) ) <= ?', [$suma2])
->orderBy('inicio', 'ASC') ->orderBy('inicio', 'ASC')
->lockForUpdate()
->get(); ->get();
} else { } else {
$cuenta = null; $cuenta = null;
$cantidad_necesaria = null; $cantidad_necesaria = null;
@@ -389,21 +388,45 @@ class ShowServicios extends Component
if (!empty($cuenta) && isset($cuenta[$h])) { if (!empty($cuenta) && isset($cuenta[$h])) {
$esta_error = false; $esta_error = false;
$cuentasMostar = ''; $cuentasMostar = '';
// 🔐 Bloqueo de cuenta para evitar perfiles duplicados
$cuentaBloqueada = Cuentas::where('id', $cuenta[$h]->id)
->lockForUpdate()
->first();
$perfil = $cuenta[$h]->historiales_count; // 🧠 Obtener perfiles ya asignados
$perfilesUsados = Historial_cuenta::where('cuenta_id', $cuentaBloqueada->id)
->pluck('perfil')
->toArray();
for ($y = 0; $y < (int) $pantallas; $y++) {
$perfilAsignado = 1;
$perfilesAsignados = 0;
while ($perfilesAsignados < (int) $pantallas) {
if (!in_array($perfilAsignado, $perfilesUsados)) {
$historial_cuenta = new Historial_cuenta; $historial_cuenta = new Historial_cuenta;
$historial_cuenta->cuenta_id = $cuenta[$h]->id; $historial_cuenta->cuenta_id = $cuentaBloqueada->id;
$historial_cuenta->historial_id = $historial->id; $historial_cuenta->historial_id = $historial->id;
$historial_cuenta->perfil = (int) $perfil + (int) $y + 1; $historial_cuenta->perfil = $perfilAsignado;
$historial_cuenta->save(); $historial_cuenta->save();
if ($cuenta[$h]->correo != $cuentasMostar) { $perfilesUsados[] = $perfilAsignado;
$cuentasMostar .= $cuenta[$h]->correo . ' '; $perfilesAsignados++;
if ($cuentaBloqueada->correo != $cuentasMostar) {
$cuentasMostar .= $cuentaBloqueada->correo . ' ';
} }
} }
$perfilAsignado++;
// Evita bucles infinitos por error
if ($perfilAsignado > $pantallas * 2) { // margen de seguridad mayor
$esta_error = true;
break;
}
}
} else { } else {
$esta_error = true; $esta_error = true;
$historial->update(['estado' => 'cancelado']); $historial->update(['estado' => 'cancelado']);
@@ -453,7 +476,6 @@ class ShowServicios extends Component
'position' => 'top' 'position' => 'top'
]); ]);
} }
} else { } else {
$eliminar = Historial_cuenta::where('historial_id', $historial->id)->delete(); $eliminar = Historial_cuenta::where('historial_id', $historial->id)->delete();