From 2c68201f3b518530e7979592334e4bad3463c572 Mon Sep 17 00:00:00 2001 From: lizandrogd <77708265+lizandrogd@users.noreply.github.com> Date: Mon, 7 Jul 2025 17:27:51 -0500 Subject: [PATCH] Update ShowServicios.php --- app/Http/Livewire/ShowServicios.php | 176 ++++++++++++++++------------ 1 file changed, 99 insertions(+), 77 deletions(-) diff --git a/app/Http/Livewire/ShowServicios.php b/app/Http/Livewire/ShowServicios.php index c13d27e..fa29768 100755 --- a/app/Http/Livewire/ShowServicios.php +++ b/app/Http/Livewire/ShowServicios.php @@ -110,7 +110,7 @@ class ShowServicios extends Component ->orderBy('ubicacion') ->get(); - //dd($consulta_servicios); + //dd($consulta_servicios); return view('livewire.show-servicios', [ @@ -174,27 +174,28 @@ 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 - $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') - ->withCount('historiales') - ->where('estado', 'activo'); + $cuentas_pantallas = Cuentas::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(); + - $cuentas_pantallas = Cuentas::fromSub($baseQuery, 'alias') - ->where('historiales_count', '<=', $cantidad_restante) - ->orderBy('inicio', 'ASC') - ->get(); - //dd($cuentas_pantallas); // con count da un numero entero $cuentas_pantallas_tomadas = Historial_cuenta::whereIn('cuenta_id', $cuentas_pantallas @@ -210,7 +211,6 @@ class ShowServicios extends Component } else { $this->disponibles = $cuentas_pantallas; } - } //dd($this->tarjetaPlan); @@ -222,7 +222,6 @@ class ShowServicios extends Component $this->cantidad_tarjetaPlan = 1; $this->tarjetaPlan = false; $this->tarifa_id = ''; - } public function notificacionPocas($servicio, $restantes) @@ -270,30 +269,30 @@ class ShowServicios extends Component $usuario_existe = User::firstWhere('email', $this->email_tarjetaPlan)->id ?? null; $usuario_id = $usuario_existe; - if (is_null($usuario_existe)) { - $usuario = new User; - $usuario->name = $this->nombre_tarjetaPlan; - $usuario->email = $this->email_tarjetaPlan; - $usuario->password = '123'; - $usuario->celular = '+57' . $this->celular_tarjetaPlan; - $usuario->save(); + if (is_null($usuario_existe)) { + $usuario = new User; + $usuario->name = $this->nombre_tarjetaPlan; + $usuario->email = $this->email_tarjetaPlan; + $usuario->password = '123'; + $usuario->celular = '+57' . $this->celular_tarjetaPlan; + $usuario->save(); - $usuario_id = $usuario->id; - } - //dd($this->fechaFinal); - $historial = new Historiale; - $historial->fecha_inicio = Carbon::now(); - $historial->fecha_final = $this->fechaFinal; - $historial->valor = $total_valor; - $historial->tipo_pago = 'manual'; - $historial->estado = 'pendiente'; - $historial->vendedor_id = Auth()->user()->id; - $historial->tarifa_id = $this->tarifa_id; - $historial->cliente_id = $usuario_id; - $historial->cantidad = $this->cantidad_tarjetaPlan; - $historial->nombre_cliente = $this->nombre_tarjetaPlan; - $historial->utilidad = $utilidad_total; - $historial->save(); + $usuario_id = $usuario->id; + } + //dd($this->fechaFinal); + $historial = new Historiale; + $historial->fecha_inicio = Carbon::now(); + $historial->fecha_final = $this->fechaFinal; + $historial->valor = $total_valor; + $historial->tipo_pago = 'manual'; + $historial->estado = 'pendiente'; + $historial->vendedor_id = Auth()->user()->id; + $historial->tarifa_id = $this->tarifa_id; + $historial->cliente_id = $usuario_id; + $historial->cantidad = $this->cantidad_tarjetaPlan; + $historial->nombre_cliente = $this->nombre_tarjetaPlan; + $historial->utilidad = $utilidad_total; + $historial->save(); $pantallas = Tarifas::where('id', $this->tarifa_id)->with('servicio')->first(); @@ -310,41 +309,42 @@ class ShowServicios extends Component //dd('soy este'); $suma = $pantalla_servicio - $cantidad_solicitada; $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('vencimiento', '>=', $this->fecha_final_1) ->whereDate('vencimiento', '<=', $this->fecha_final_2) ->where('estado', 'activo') - ->withCount('historiales') - ->orderBy('inicio', 'ASC'); - - $cuenta = Cuentas::fromSub($baseQuery, 'alias') - ->where('historiales_count', '<=', $suma) + ->whereRaw('( + SELECT COUNT(*) + FROM historiales_cuenta + WHERE historiales_cuenta.cuenta_id = cuentas.id + ) <= ?', [$suma]) ->orderBy('inicio', 'ASC') + ->lockForUpdate() ->get(); + + - if ($cuenta->isEmpty()) { $suma3 = $pantalla_servicio - $pantallas; $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('vencimiento', '>=', $this->fecha_final_1) ->whereDate('vencimiento', '<=', $this->fecha_final_2) - ->withCount('historiales') ->where('estado', 'activo') - ->Orderby('inicio', 'ASC'); - - - $cuenta = Cuentas::fromSub($baseQuery, 'alias') - ->where('historiales_count', '<=', $suma3) + ->whereRaw('( + SELECT COUNT(*) + FROM historiales_cuenta + WHERE historiales_cuenta.cuenta_id = cuentas.id + ) <= ?', [$suma3]) ->orderBy('inicio', 'ASC') + ->lockForUpdate() ->get(); } - } elseif ((int) $pantalla_servicio_completa == (int) $this->pantallas_tarjetaPlan) { //Cuentas completas @@ -357,26 +357,25 @@ 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; - $baseQuery = Cuentas::select('*')->where('servicio_id', $this->servicio_id) + + $cuenta = Cuentas::where('servicio_id', $this->servicio_id) ->whereDate('inicio', '>=', $this->fecha) ->whereDate('vencimiento', '>=', $this->fecha_final_1) ->whereDate('vencimiento', '<=', $this->fecha_final_2) - ->Orderby('inicio', 'ASC') - ->withCount('historiales') - ->where('estado', 'activo'); - - $cuenta = Cuentas::fromSub($baseQuery, 'alias') - ->where('historiales_count', '<=', $suma2) + ->where('estado', 'activo') + ->whereRaw('( + SELECT COUNT(*) + FROM historiales_cuenta + WHERE historiales_cuenta.cuenta_id = cuentas.id + ) <= ?', [$suma2]) ->orderBy('inicio', 'ASC') + ->lockForUpdate() ->get(); - - + } else { $cuenta = null; $cantidad_necesaria = null; @@ -389,20 +388,44 @@ 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(); - $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++) { - $historial_cuenta = new Historial_cuenta; - $historial_cuenta->cuenta_id = $cuenta[$h]->id; - $historial_cuenta->historial_id = $historial->id; - $historial_cuenta->perfil = (int) $perfil + (int) $y + 1; - $historial_cuenta->save(); + $perfilAsignado = 1; + $perfilesAsignados = 0; - if ($cuenta[$h]->correo != $cuentasMostar) { - $cuentasMostar .= $cuenta[$h]->correo . ' '; + while ($perfilesAsignados < (int) $pantallas) { + if (!in_array($perfilAsignado, $perfilesUsados)) { + $historial_cuenta = new Historial_cuenta; + $historial_cuenta->cuenta_id = $cuentaBloqueada->id; + $historial_cuenta->historial_id = $historial->id; + $historial_cuenta->perfil = $perfilAsignado; + $historial_cuenta->save(); + + $perfilesUsados[] = $perfilAsignado; + $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 { $esta_error = true; @@ -453,7 +476,6 @@ class ShowServicios extends Component 'position' => 'top' ]); } - } else { $eliminar = Historial_cuenta::where('historial_id', $historial->id)->delete();