'required', 'cantidad_tarjetaPlan' => 'required', 'email_tarjetaPlan' => 'required', 'celular_tarjetaPlan' => 'required', ]; public function render() { $this->fechaHoy = Carbon::now(); $this->fechaActual = Carbon::parse($this->fechaHoy); $date = $this->fechaHoy->subDay(30); $this->fecha = $date->format('Y-m-d'); $this->usuario_actual = Auth::user(); if (empty($this->nombre_tarjetaPlan) && empty($this->email_tarjetaPlan)) { $this->nombre_tarjetaPlan = $this->usuario_actual->name; $this->email_tarjetaPlan = $this->usuario_actual->email; $this->celular_tarjetaPlan = $this->usuario_actual->celular; } if (!empty($this->tarifa_id)) { $this->paquetesDisponibles(); } $this->msj = Configuracione::orderby('id', 'desc')->select('msj_compra', 'msj_wp', 'clave_1', 'clave_2', 'clave_3', 'clave_4', 'clave_5', 'clave_6', 'iptv')->first(); $consulta_servicios = Servicio::where('estado', 'activo') ->with([ 'tarifas' => function ($query) { $query->where('rol_id', $this->usuario_actual->rol->id) ->with([ 'usuario_tarifas' => function ($query) { $query->where('usuario_id', $this->usuario_actual->id); } ]); } ]) ->get(); return view('livewire.show-servicios', [ 'servicios' => $consulta_servicios, ]); } public function cargartarjetaPlan($id) { $this->tarifas = Tarifas::with('servicio') ->where('id', $id) ->first(); $this->servicio_t = $this->tarifas->servicio; //Servicio por pantallas $this->cuentas_creadas = Cuentas::whereDate('inicio', '>=', $this->fecha) ->where('servicio_id', $this->servicio_t->id) ->where('estado', 'activo') ->get(); $this->cuentas_tomadas = Historial_cuenta::whereIn('cuenta_id', $this->cuentas_creadas ->pluck('id')) ->count(); //$prueba1 = $this->cuentas_creadas->count() * $this->servicio_t->pantallas ; //$prueba2 = $this->cuentas_tomadas ; //dd($prueba1, $prueba2 , $prueba1-$prueba2); $this->servicio_id = $this->servicio_t->id; $this->tarifa_id = $id; $this->servicio_tarjetaPlan = $this->servicio_t->nombre; $this->tiempo_tarjetaPlan = $this->servicio_t->por_tiempo; $this->pantallas_tarjetaPlan = $this->tarifas->pantallas; $this->dias_tarjetaPlan = $this->tarifas->dias; $this->fecha_final_1 = $today = Carbon::now(); $this->fecha_final_2 = $today = Carbon::now(); $this->mensaje = $this->servicio_t->mensaje; //Servicio por cuenta completa $this->cuentas_completas = Cuentas::whereDate('inicio', '>=', $this->fecha) ->whereDate('vencimiento', '>=', $this->fecha_final_1->addDays($this->tarifas->dias - 20)) ->whereDate('vencimiento', '<=', $this->fecha_final_2->addDays($this->tarifas->dias + 20)) ->where('servicio_id', $this->tarifas->servicio->id) ->where('estado', 'pendiente') ->get(); $this->cuentas_completas_tomadas = Historial_cuenta::whereIn('cuenta_id', $this->cuentas_completas->pluck('id')) ->count(); $usuario_tarifa = Usuario_tarifa::where('usuario_id', auth()->user()->id) ->where('tarifa_id', $id) ->first(); $this->valor_tarjetaPlan = ($usuario_tarifa) ? $usuario_tarifa->precio : $this->tarifas->valor; $this->utilidad = ($usuario_tarifa) ? $usuario_tarifa->utilidad : $this->tarifas->utilidad; $this->colorFondo_tarjetaPlan = $this->servicio_t->color_fondo; $this->fechaFinal = $this->fechaActual->addDay($this->tarifas->dias); //Cantidad de paquetes disponibles $this->paquetesDisponibles(); $this->tarjetaPlan = true; } public function paquetesDisponibles() { if ($this->tarifas->pantallas == $this->servicio_t->completa) { $this->disponibles = (($this->cuentas_completas->count() * $this->servicio_t->completa) - $this->cuentas_completas_tomadas) / $this->tarifas->pantallas; $restantes_completas = $this->cuentas_completas->count() - ($this->cuentas_completas_tomadas / $this->servicio_t->completa); if ($restantes_completas <= 10) { //$this->notificacionPocas($this->servicio_t,$restantes_completas); } } else { $cantidad_solicitada = (int) $this->cantidad_tarjetaPlan * (int) $this->pantallas_tarjetaPlan; if ($this->servicio_t->pantallas >= $cantidad_solicitada) { $this->disponibles = (($this->cuentas_creadas->count() * $this->servicio_t->pantallas) - $this->cuentas_tomadas) / $this->tarifas->pantallas; $restantes = $this->cuentas_creadas->count() - ($this->cuentas_tomadas / $this->servicio_t->pantallas); } else { $this->disponibles = ($this->cuentas_creadas->count() - $this->cuentas_tomadas); $restantes = $this->cuentas_creadas->count() - ($this->cuentas_tomadas); } if ($restantes <= 10) { //$this->notificacionPocas($this->servicio_t,$restantes); } } } public function notificacionPocas($servicio, $restantes) { $roles = Role::where('nombre', 'super')->with('usuarios')->first(); $user = $roles->usuarios; $notificacion = [ 'servicio' => $servicio->nombre, 'cantidad' => $restantes, 'tipo' => 'cuentas normales', ]; Notification::send($user, (new licencias($notificacion))); } /* ---------------- Btn Comprar ---------------- */ public function comprar() { $this->validate(); $cantidad_solicitada = (int) $this->cantidad_tarjetaPlan * (int) $this->pantallas_tarjetaPlan; $total_valor = (int) $this->valor_tarjetaPlan * (int) $this->cantidad_tarjetaPlan; $utilidad_total = (int) $this->utilidad * (int) $this->cantidad_tarjetaPlan; $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(); $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(); $servicioNombre = $pantallas->servicio->nombre; $servicio_id = $pantallas->servicio_id; $pantalla_servicio_completa = $pantallas->servicio->completa; $pantalla_servicio = $pantallas->servicio->pantallas; $pantallas = $pantallas->pantallas ?? null; if ((int) $pantalla_servicio >= (int) $cantidad_solicitada && (int) $pantalla_servicio_completa != (int) $this->pantallas_tarjetaPlan) { $suma = $pantalla_servicio - $cantidad_solicitada; $cantidad_necesaria = $this->cantidad_tarjetaPlan; $cuenta = Cuentas::where('servicio_id', $this->servicio_id)->whereDate('inicio', '>=', $this->fecha)->Orderby('created_at', 'ASC')->withCount('historiales')->where('estado', 'activo')->having('historiales_count', '<=', $suma)->get(); } elseif ((int) $pantalla_servicio_completa == (int) $this->pantallas_tarjetaPlan) { //Cuentas completas $cantidad_necesaria = $this->cantidad_tarjetaPlan; $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('created_at', 'ASC')->withCount('historiales')->where('estado', 'pendiente')->having('historiales_count', '==', 0)->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)->whereDate('inicio', '>=', $this->fecha)->Orderby('created_at', 'ASC')->withCount('historiales')->where('estado', 'activo')->having('historiales_count', '<=', $suma2)->get(); } else { $cuenta = null; $cantidad_necesaria = null; } //dd($cuenta, $cantidad_necesaria); for ($h = 0; $h < (int) $cantidad_necesaria; $h++) { //dd($h); if (!empty($cuenta)) { $cuentasMostar = ''; $perfil = $cuenta[$h]->historiales_count; 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(); if ($cuenta[$h]->correo != $cuentasMostar) { $cuentasMostar .= $cuenta[$h]->correo . ' '; } } } else { $this->alert('warning', 'No hay cuentas disponibles!', [ 'position' => 'top' ]); } } //dd($historial->cuentas); $descuento = Saldo::where('usuario_id', Auth::user()->id)->first(); $saldo_anterior = $descuento->valor; //dd($saldo_anterior); $nuevo_saldo = $saldo_anterior - $total_valor; $cobro = $descuento->update(['valor' => $nuevo_saldo]); if ($cobro) { $historial->update(['estado' => 'activo']); $logsGeneral = new Log_general(); $logsGeneral->user_id = Auth::user()->id; $logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' ha comprado el servicio ' . $servicioNombre . ' - ' . $cuentasMostar . ' cantidad de ' . $this->cantidad_tarjetaPlan . ' por un valor de $' . $total_valor; $logsGeneral->save(); $this->credenciales($historial); } else { $this->alert('warning', 'Error en el cobro, comuniquese con soporte!', [ 'position' => 'top' ]); } $this->tarjetaPlan = false; $this->cantidad_tarjetaPlan = 1; } public function credenciales($historial) { $this->cuentas = $historial; $this->credenciales = true; $encryptado = Crypt::encryptString($historial->id); $enlace = '/credenciales?id=' . $encryptado; $user = $historial->cliente; $notificacion = [ 'enlace' => $enlace, 'fecha_final' => $historial->fecha_final, 'cliente' => $historial->cliente->name, 'servicio' => $historial->tarifa->servicio->nombre . '-' . $historial->tarifa->pantallas . 'pantallas', 'tipo' => 'compra', 'vendedor' => auth()->user()->username, ]; Notification::send($user, (new compra($notificacion))); $this->alert('success', 'Credenciales enviadas a su email!', [ 'position' => 'top' ]); $shortURLObject = ShortURL::destinationUrl('https://app.sirpremium.com.co' . $enlace)->make(); $shortURL = $shortURLObject->default_short_url; $message = $historial->tarifa->servicio->nombre . '-' . $historial->tarifa->pantallas . 'P. Ver credenciales: ' . $shortURL; $recipients = '+57' . $this->celular_tarjetaPlan; $this->sendMessage($message, $recipients); } public function sendMessage($message, $recipients) { $auth_basic = base64_encode('admin@jaguarws.ga:9u3xxXhHt8maeiOexg07pFKI0rpYazI0'); $curl = curl_init(); curl_setopt_array( $curl, array( CURLOPT_URL => "https://api.labsmobile.com/json/send", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => '{"message":"' . $message . '", "tpoa":"Sender","recipient":[{"msisdn":"' . $recipients . '"}]}', CURLOPT_HTTPHEADER => array( "Authorization: Basic " . $auth_basic, "Cache-Control: no-cache", "Content-Type: application/json" ), ) ); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { $this->alert('warning', 'Error enviando SMS!' . $err, [ 'position' => 'top' ]); } else { if (!empty(json_decode($response)->subid)) { $this->alert('success', 'Credenciales enviadas por Email y SMS! ' . json_decode($response)->subid, [ 'position' => 'top' ]); } else { $this->alert('success', 'Credenciales enviadas por Email! ' . json_decode($response)->code, [ 'position' => 'top' ]); } } } public function consultaTarifa($servicio1) { $this->servicio = $servicio1; $this->verServ = false; } }