fechaActual = date('Y-m-d'); $consulta_tarifas = Tarifas::take(1); if (empty($this->fecha_inicial)) { $this->fecha_inicial = Carbon::now()->format('Y-m-d'); $this->fecha_final = Carbon::now()->addDay(30)->format('Y-m-d'); } if ($this->servicio_modTarifas) { $this->tarifas = Tarifas::with('rol') ->where('servicio_id', $this->servicio_modTarifas) // ->where('estado', 'activo') ->get(); } // Empezamos definiendo la consulta básica $consulta_cuentas = Cuentas::where('correo', 'LIKE', '%' . $this->buscar . '%') ->withCount('historiales') ->with('historiales') ->orderBy('id', 'DESC'); $consulta_cuentas = $consulta_cuentas->when(!empty($this->servicios_id), function ($query) { return $query->where('servicio_id', $this->servicios_id); }) ->when(!empty($this->completa), function ($query) { return $query->where('estado', 'pendiente'); }) ->when(!empty($this->pantallas), function ($query) { return $query->where('estado', 'activo'); }) ->when(!empty($this->activoHoy), function ($query) { $fechaHoy = Carbon::now()->subDay(1); return $query->whereDate('vencimiento', '>', $fechaHoy); }) ->when($this->venceHoy, function ($query) { $fechaHoy = Carbon::now()->addDay(1); return $query->whereDate('vencimiento', '<', $fechaHoy); }) ->when($this->libres, function ($query) { return $query->whereDoesntHave('historiales'); }); if (!empty($this->repetidas)) { $consulta_cuentas = $consulta_cuentas->whereIn('correo', function ($subquery) { $subquery->select('correo') ->from('cuentas') ->groupBy('correo') ->havingRaw('COUNT(*) > 1'); }); } $consulta_cuentas = $consulta_cuentas->paginate($this->entradas); if ($this->searchTerm) { $this->usuarios = User::where('email', 'LIKE', '%' . $this->searchTerm . '%')->get(); if ($this->selectedOption) { $this->searchTerm = $this->selectedOption; } } else { $this->selectedOption = null; } return view('livewire.show-planes', [ 'cuentas' => $consulta_cuentas, 'servicios' => Servicio::where('estado', 'activo')->get(), 'roles' => Role::get() ]); } //modal Ver public function ver($id, $nom) { $this->id_cuentaVer = $id; $this->consulta_verUsuarios = Cuentas::with('historiales', 'servicio')->where('id', $id)->first(); //dd($this->consulta_verUsuarios); $this->nombre_servicios = $nom; } public function openRenovar() { $this->renovar = true; $id = $this->id_cuentaVer; $renovarCuenta = Cuentas::findOrFail($id); $this->usuario_renovar = $renovarCuenta->correo; $this->password_renovar = $renovarCuenta->password; $this->fecha_inicial_renovar_old = $renovarCuenta->inicio; $this->fecha_final_renovar_old = $renovarCuenta->vencimiento; $this->fecha_inicial_renovar = $renovarCuenta->inicio; $this->fecha_final_renovar = $renovarCuenta->vencimiento; $this->estado = $renovarCuenta->estado; } public function saveRenovar() { if($this->fecha_inicial_renovar_old !== $this->fecha_inicial_renovar || $this->fecha_final_renovar_old !== $this->fecha_final_renovar){ $this->alert('warning', '¿Desea también desvincular todos los usuarios de esta cuenta?', [ 'position' => 'center', 'timer' => '60000', 'toast' => false, 'text' => 'Esta seguro', 'showConfirmButton' => true, 'onConfirmed' => 'siRenovar', 'showDenyButton' => false, 'onDenied' => 'noRenovar', 'showCancelButton' => true, 'allowOutsideClick' => false, 'allowEscapeKey' => false, 'cancelButtonText' => 'No, solo renovar', 'confirmButtonText' => 'Si, liberar cuenta', ]); }else{ $cuenta_renovar = Cuentas::find($this->id_cuentaVer); Cuentas::where('id', $this->id_cuentaVer)->update([ 'correo' => $this->usuario_renovar, 'password' => $this->password_renovar, 'inicio' => date('Y-m-d', strtotime(strval($this->fecha_inicial_renovar))), 'vencimiento' => date('Y-m-d', strtotime(strval($this->fecha_final_renovar))), 'estado' => $this->estado, ]); Log_historial::create([ 'user_id' => Auth::user()->id, 'detalle' => 'El usuario ha editado la cuenta ' . $cuenta_renovar->correo ]); $this->alert('success', 'Cuenta actualizada correctamente!', [ 'position' => 'top' ]); $this->renovar = false; $this->ver = false; } } public function siRenovar() { $id = $this->id_cuentaVer; Historial_cuenta::where('cuenta_id', $id)->delete(); $cuenta_renovar = Cuentas::find($id); Cuentas::where('id', $id)->update([ 'correo' => $this->usuario_renovar, 'password' => $this->password_renovar, 'inicio' => date('Y-m-d', strtotime(strval($this->fecha_inicial_renovar))), 'vencimiento' => date('Y-m-d', strtotime(strval($this->fecha_final_renovar))), 'estado' => $this->estado, ]); Log_historial::create([ 'user_id' => Auth::user()->id, 'detalle' => 'El usuario ha editado, renovado y liberado la cuenta ' . $cuenta_renovar->correo ]); $this->alert('success', 'Cuenta renovada y liberada correctamente!', [ 'position' => 'top' ]); $this->renovar = false; $this->ver = false; } public function noRenovar() { $id = $this->id_cuentaVer; Cuentas::where('id', $id)->update([ 'correo' => $this->usuario_renovar, 'password' => $this->password_renovar, 'inicio' => date('Y-m-d', strtotime(strval($this->fecha_inicial_renovar))), 'vencimiento' => date('Y-m-d', strtotime(strval($this->fecha_final_renovar))), 'estado' => $this->estado, ]); $cuenta_renovar = Cuentas::find($id); Log_historial::create([ 'user_id' => Auth::user()->id, 'detalle' => 'El usuario ha editado y renovado la cuenta ' . $cuenta_renovar->correo ]); $this->alert('success', 'Cuenta renovada correctamente!', [ 'position' => 'top' ]); $this->renovar = false; $this->ver = false; } /* ------------- CREAR ------------- */ public function createNewCuenta() { if ($consulta_cuenta = Cuentas::where('correo', $this->usuario_newCuenta)->exists()) { $this->alert('warning', 'La cuenta ya existe', [ 'position' => 'top' ]); } else { $cuenta = new Cuentas; $cuenta->servicio_id = $this->servicio_newCuenta; $cuenta->correo = $this->usuario_newCuenta; $cuenta->password = $this->password_newCuenta; $cuenta->inicio = date('Y-m-d', strtotime(strval($this->fecha_inicial))); $cuenta->vencimiento = date('Y-m-d', strtotime(strval($this->fecha_final))); $cuenta->estado = $this->estado; $cuenta->save(); Log_historial::create([ 'user_id' => Auth::user()->id, 'detalle' => 'El usuario ha creado la cuenta ' . $cuenta->correo ]); if ($this->estado === 'activo') { $servicioPantallas = Servicio::find($this->servicio_newCuenta)->pantallas; $cuentasPendientes = Pendientes::where('estado', 'pendiente')->where('servicio_id', $this->servicio_newCuenta)->take($servicioPantallas)->get(); foreach ($cuentasPendientes as $i => $cuentaPendiente) { $servicioName = $cuentaPendiente->servicio->nombre; $historial_cuenta = new Historial_cuenta(); $historial_cuenta->historial_id = $cuentaPendiente->historial_id; $historial_cuenta->cuenta_id = $cuenta->id; $historial_cuenta->perfil = $i + 1; $historial_cuenta->save(); $cuentaPendiente->update(['estado' => 'cerrado']); $cuentaPendiente->historial->vendedor->notify(new NotificacionPendiente($servicioName)); $consulta = new Notificacion(); $consulta->remitente_id = Auth::user()->id; $consulta->destinatario_id = $historial_cuenta->historial->vendedor_id; $consulta->titulo = "Licencia asignada para $servicioName"; $consulta->descripcion = 'La licencia de ' . $servicioName . ' ha sido activada y ahora puedes utilizar tu cuenta sin problemas. Tus credenciales son: correo: ' . $historial_cuenta->cuenta->correo . ' contraseña: ' . $historial_cuenta->cuenta->password; $consulta->estado = true; $consulta->todos = false; $consulta->save(); } } $this->alert('success', 'Cuenta creada correctamente!', [ 'position' => 'top' ]); $this->limpiarInputNewCuenta(); $this->newCuenta = false; } } public function createServicio() { $url_foto = $this->photo->store('photos'); $url_foto_inicio = $this->photo_inicio->store('photos'); $servicio = new Servicio; $servicio->nombre = $this->nombre_servicio; $servicio->descripcion = $this->descripcion_servicio; $servicio->estado = $this->estado_servicio; $servicio->color_fondo = $this->color_servicio; $servicio->pantallas = '6'; $servicio->img = $url_foto; $servicio->img_inicio = $url_foto_inicio; $servicio->save(); $this->alert('success', 'Servicio creado correctamente!', [ 'position' => 'top' ]); $this->serviciosModal = false; } public function verObservacion($id) { $verObservacion = Historiale::findOrFail($id); $this->id_observacion = $id; $this->observaciones = $verObservacion->observaciones; } public function update() { Historiale::where('id', $this->id_observacion)->update(['observaciones' => $this->observaciones,]); $this->alert('success', 'Observacion añadida correctamente!', [ 'position' => 'top' ]); $this->observacionesm = false; } public function verObservacion1($id) { $verObservacion = Cuentas::findOrFail($id); $this->id_observacion1 = $id; $this->observaciones1 = $verObservacion->observaciones; } public function update_observaciones1() { Cuentas::where('id', $this->id_observacion1)->update(['observaciones' => $this->observaciones1,]); $this->alert('success', 'Observacion añadida correctamente!', [ 'position' => 'top' ]); $this->observacionesm1 = false; } public function delete_cuenta($id) { $this->delete_cuenta = $id; $this->alert('warning', '¿Eliminar cuenta?', [ 'position' => 'center', 'timer' => '10000', 'toast' => false, 'text' => 'Esta seguro', 'showConfirmButton' => true, 'onConfirmed' => 'confirmed', 'showDenyButton' => false, 'onDenied' => '', 'showCancelButton' => true, 'onDismissed' => '', 'cancelButtonText' => 'Salir', 'confirmButtonText' => 'Si', ]); } public function confirmed() { Cuentas::where('id', $this->delete_cuenta)->delete(); $this->alert('success', 'Cuenta eliminada correctamente!', [ 'position' => 'top' ]); } /* ------------- ACTUALIZAR ------------- */ public function updateTarifas() { $consulta_existe = Tarifas::where('pantallas', $this->pantallas_modTarifas) ->where('servicio_id', $this->servicio_modTarifas) ->where('rol_id', $this->roles_modTarifas) ->where('dias', $this->diasTarifa) ->exists(); if ($consulta_existe) { $this->alert('warning', '¡Esta tarifa ya existe!', [ 'position' => 'top' ]); } elseif (empty($this->servicio_modTarifas)) { $this->alert('warning', '¡Seleccione un servicio!', [ 'position' => 'top' ]); } elseif (empty($this->precio_modTarifas)) { $this->alert('warning', '¡Coloque un precio!', [ 'position' => 'top' ]); } elseif (empty($this->utilidad)) { $this->alert('warning', '¡Coloque la utilidad!', [ 'position' => 'top' ]); } else { $tarifa = new Tarifas; $tarifa->pantallas = $this->pantallas_modTarifas; $tarifa->dias = $this->diasTarifa; $tarifa->valor = $this->precio_modTarifas; $tarifa->utilidad = $this->utilidad; $tarifa->estado = $this->estadoTarifa ? 'activo' : 'inactivo'; $tarifa->servicio_id = $this->servicio_modTarifas; $tarifa->rol_id = $this->roles_modTarifas; $tarifa->save(); $this->alert('success', '¡Tarifas actualizadas correctamente!', [ 'position' => 'top' ]); } } /* ------------- ELIMINAR ------------- */ public function eliminarModTarifa($id) { Tarifas::where('id', $id)->delete(); $this->alert('success', 'Tarifas eliminada correctamente!', [ 'position' => 'top' ]); } /* ------------- LIMPIAR CAMPOS ------------- */ public function limpiarInputObservaciones() { $this->observaciones = ''; } public function limpiarInputObservaciones1() { $this->observaciones1 = ''; $this->observacionesm1 = false; } public function limpiarInputNewCuenta() { $this->servicio_newCuenta = null; $this->usuario_newCuenta = ''; $this->password_newCuenta = ''; $this->fecha_inicial = null; $this->fecha_final = null; $this->newCuenta = false; } public function limpiarInputServiciosModal() { $this->nombre_servicio = ''; $this->descripcion_servicio = ''; $this->estado_servicio = null; $this->color_servicio = null; } public function eliminarCuentas() { $cuentas = Cuentas::whereIn('id', $this->delete_1)->get(); if ($cuentas) { forEach ($cuentas as $cuenta) { Log_historial::create([ 'user_id' => Auth::user()->id, 'detalle' => 'El usuario ha eliminado la cuenta ' . $cuenta->correo ]); } } $consulta_cuentas = Cuentas::whereIn('id', $this->delete_1)->delete(); $this->alert('success', 'Cuentas eliminadas correctamente!', [ 'position' => 'top' ]); $this->delete_1 = []; } public function openAdd() { $this->addUser = true; } public function saveUser() { $this->validate([ 'tarifa' => 'required', ]); $tarifa = json_decode($this->tarifa, true); // dd($tarifa) //dd($this->tarifa['dias']); $usuario_existe = User::firstWhere('email', $this->searchTerm)->id ?? null; $usuario_id = $usuario_existe; if (is_null($usuario_existe)) { $rol_id = Role::where('nombre', 'cliente')->value('id'); $usuario = new User; $usuario->name = 'AssignedUser'; $usuario->email = $this->searchTerm; $usuario->password = '123456'; $usuario->celular = '+57'; $usuario->rol_id = $rol_id; $usuario->save(); $usuario_id = $usuario->id; } // dd($this->tarifa); $historial = new Historiale; $historial->fecha_inicio = Carbon::now(); $historial->fecha_final = Carbon::now()->addDay($tarifa['dias']); $historial->valor = '0'; $historial->tipo_pago = 'manual'; $historial->estado = 'activo'; $historial->vendedor_id = Auth()->user()->id; $historial->tarifa_id = $tarifa['id']; $historial->cliente_id = $usuario_id; $historial->cantidad = $tarifa['pantallas']; $historial->save(); $consultaHC = Historial_cuenta::where('cuenta_id', $this->consulta_verUsuarios->id)->orderBy('created_at', 'desc')->first()->perfil ?? 0; if ($this->consulta_verUsuarios->estado == 'pendiente') { // completa $perfiles = $this->consulta_verUsuarios->servicio->completa; for ($i = 0; $i < $perfiles; $i++) { $historial_cuenta = new Historial_cuenta; $historial_cuenta->cuenta_id = $this->consulta_verUsuarios->id; $historial_cuenta->historial_id = $historial->id; $historial_cuenta->perfil = $i + 1; $historial_cuenta->save(); } $this->alert('success', 'Usuario añadido correctamente!', [ 'position' => 'top' ]); $this->addUser = false; $this->consulta_verUsuarios = Cuentas::with('historiales', 'servicio')->where('id', $this->id_cuentaVer)->first(); } if ($this->consulta_verUsuarios->estado == 'activo') { // pantallas $perfiles = $this->consulta_verUsuarios->servicio->pantallas; if ($consultaHC) { if ($consultaHC < $perfiles) { $historial_cuenta = new Historial_cuenta; $historial_cuenta->cuenta_id = $this->consulta_verUsuarios->id; $historial_cuenta->historial_id = $historial->id; $historial_cuenta->perfil = $consultaHC + 1; $historial_cuenta->save(); $this->alert('success', 'Usuario añadido correctamente!', [ 'position' => 'top' ]); $this->addUser = false; $this->consulta_verUsuarios = Cuentas::with('historiales', 'servicio')->where('id', $this->id_cuentaVer)->first(); } else { $historial->delete(); $this->alert('warning', 'Ya haz alcanzado el maximo de perfiles!', [ 'position' => 'top' ]); } } else { $historial_cuenta = new Historial_cuenta; $historial_cuenta->cuenta_id = $this->consulta_verUsuarios->id; $historial_cuenta->historial_id = $historial->id; $historial_cuenta->perfil = $consultaHC + 1; $historial_cuenta->save(); $this->alert('success', 'Usuario añadido correctamente!', [ 'position' => 'top' ]); } $this->addUser = false; $this->consulta_verUsuarios = Cuentas::with('historiales', 'servicio')->where('id', $this->id_cuentaVer)->first(); } } public function editar($tarifa) { //dd($tarifa); $this->editarTarifaRol = true; $this->informacionTarifaRol = $tarifa; $this->roles_modTarifas = $tarifa['rol_id']; $this->pantallas_modTarifas = $tarifa['pantallas']; $this->precio_modTarifas = $tarifa['valor']; $this->utilidad = $tarifa['utilidad']; $this->diasTarifa = $tarifa['dias']; $this->estadoTarifa = $tarifa['estado'] == 'activo' ? 1 : 0; $this->servicio_modTarifas = $tarifa['servicio_id']; } public function actualizarTarifaRol($id) { //dd($id); $validateData = $this->validate([ 'roles_modTarifas' => 'required', 'pantallas_modTarifas' => 'required', 'precio_modTarifas' => 'required', 'utilidad' => 'required', 'diasTarifa' => 'required', 'estadoTarifa' => 'required' ]); $consulta = Tarifas::find($id)->update([ 'pantallas' => $this->pantallas_modTarifas, 'dias' => $this->diasTarifa, 'valor' => $this->precio_modTarifas, 'utilidad' => $this->utilidad, 'estado' => $this->estadoTarifa == 1 ? 'activo' : 'inactivo', 'rol_id' => $this->roles_modTarifas, 'servicio_id' => $this->servicio_modTarifas ]); $this->alert('success', 'Tarifa actualizada correctamente!', [ 'position' => 'top' ]); $this->editarTarifaRol = false; } }