Files
sirpremiumv2/app/Http/Livewire/Bell.php
T

90 lines
2.2 KiB
PHP
Executable File

<?php
namespace App\Http\Livewire;
use App\Models\Log_general;
use App\Models\Notificacion;
use App\Models\PremioEntregado;
use App\Models\Saldo;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use Jantinnerezo\LivewireAlert\LivewireAlert;
class Bell extends Component
{
use LivewireAlert;
public $numNoti = 0;
public $menuNotificaciones = false;
public $reclamandoPremio = false;
public function render()
{
$consulta = Notificacion::where('estado', true)
->where(function ($query) {
$query->where('todos', true)
->orWhere('destinatario_id', Auth::user()->id);
})->orderBy('created_at', 'desc')
->get();
$this->numNoti = count($consulta);
return view('livewire.bell', [
'consulta' => $consulta,
]);
}
public function deleteNotificacion($id)
{
$notificacion = Notificacion::find($id);
if ($notificacion) {
$notificacion->update(['estado' => false]);
}
}
public function reclamarPremio($id)
{
if ($this->reclamandoPremio) {
return;
}
$this->reclamandoPremio = true;
$notificacion = Notificacion::find($id);
if (!$notificacion || $notificacion->estado == 0) {
$this->reclamandoPremio = false;
$this->alert('error', 'No se pudo reclamar el premio', [
'position' => 'top'
]);
return;
}
$consulta_saldo = Saldo::where('usuario_id', Auth::user()->id)->first();
if ($consulta_saldo) {
$consulta_saldo->update([
'valor' => $consulta_saldo->valor + $notificacion->premio
]);
if ($notificacion) {
$notificacion->update(['estado' => false]);
}
}
Log_general::create([
'user_id' => Auth::user()->id,
'detalle' => 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' ha reclamado el premio de $' . $notificacion->premio
]);
$this->reclamandoPremio = false;
$this->alert('success', 'Haz reclamado el premio correctamente', [
'position' => 'top'
]);
}
}