diff --git a/app/Http/Livewire/ShowServicios.php b/app/Http/Livewire/ShowServicios.php index fa29768..c13d27e 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,28 +174,27 @@ 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) - ->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(); - + $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::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 @@ -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) @@ -269,30 +270,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(); @@ -309,42 +310,41 @@ class ShowServicios extends Component //dd('soy este'); $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]) + ->withCount('historiales') + ->orderBy('inicio', 'ASC'); + + $cuenta = Cuentas::fromSub($baseQuery, 'alias') + ->where('historiales_count', '<=', $suma) ->orderBy('inicio', 'ASC') - ->lockForUpdate() ->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,44 +389,20 @@ 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; + $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(); - 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 . ' '; - } + 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; @@ -476,6 +453,7 @@ class ShowServicios extends Component 'position' => 'top' ]); } + } else { $eliminar = Historial_cuenta::where('historial_id', $historial->id)->delete();