This commit is contained in:
Lizandro Guarnizo
2026-04-18 14:13:24 -05:00
parent 03207775af
commit d11423c379
2 changed files with 51 additions and 7 deletions
+19 -2
View File
@@ -329,11 +329,28 @@ class ShowPromociones extends Component
}
if ($cuenta) {
$perfil = Historial_cuenta::where('cuenta_id', $cuenta->id)->max('perfil') ?? 0;
// Bloquear la fila y re-verificar conteo real antes de insertar (protección race condition)
$cuentaLocked = Cuentas::where('id', $cuenta->id)->lockForUpdate()->first();
$perfil = Historial_cuenta::where('cuenta_id', $cuentaLocked->id)->max('perfil') ?? 0;
$perfilesOcupados = Historial_cuenta::where('cuenta_id', $cuentaLocked->id)
->whereHas('historial', fn($q) => $q->where('estado', 'activo'))
->count();
$maxPerfiles = ($cuentaLocked->estado === 'pendiente')
? $pantalla_servicio_completa
: $pantalla_servicio;
if (($perfilesOcupados + (int) $pantallas_tarifa) > $maxPerfiles) {
$this->f[] = $servicio_id;
$this->alert('warning', 'No hay perfiles disponibles de ' . $tarifa->servicio->nombre . ', intenta nuevamente.', ['position' => 'top']);
continue;
}
for ($y = 0; $y < (int) $pantallas_tarifa; $y++) {
$historial_cuenta = new Historial_cuenta();
$historial_cuenta->historial_id = $historial->id;
$historial_cuenta->cuenta_id = $cuenta->id;
$historial_cuenta->cuenta_id = $cuentaLocked->id;
$historial_cuenta->perfil = (int) $perfil + (int) $y + 1;
$historial_cuenta->save();