Revert "Update ShowServicios.php"

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