Files
sirpremiumv2/app/Http/Livewire/ShowTop.php
T
2024-07-12 10:53:59 -05:00

108 lines
3.9 KiB
PHP
Executable File

<?php
namespace App\Http\Livewire;
use App\Models\Configuracione;
use App\Models\Log_general;
use App\Models\Notificacion;
use App\Models\PremioEntregado;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use Jantinnerezo\LivewireAlert\LivewireAlert;
class ShowTop extends Component
{
public $configuracion;
public $showLastMonth = false;
use LivewireAlert;
public function render()
{
$this->configuracion = Configuracione::orderby('id', 'desc')->first();
$currentYear = date('Y');
$monthYear = date('m-Y', strtotime('last month'));
$month = $this->showLastMonth ? date('m', strtotime('last month')) : date('m');
$consulta_ventas = User::with(['historiales_venta' => function ($query) use ($month, $currentYear) {
$query->whereMonth('fecha_inicio', $month)
->whereYear('fecha_inicio', $currentYear)
->where('estado', 'activo');
}])
->withCount(['historiales_venta' => function ($query) use ($month, $currentYear) {
$query->whereMonth('fecha_inicio', $month)
->whereYear('fecha_inicio', $currentYear)
->where('estado', 'activo');
}])
->orderBy('historiales_venta_count', 'desc')
->limit(10)
->get();
$consultaPremioEntregado = PremioEntregado::where('fecha', $monthYear)
->first();
return view('livewire.show-top', [
'usuarios' => $consulta_ventas,
'consultaPremioEntregado' => $consultaPremioEntregado
]);
}
public function entregarPremio()
{
$currentYear = date('Y');
$monthYear = date('m-Y', strtotime('last month'));
$month = date('m', strtotime('last month'));
$rangos = ['primero', 'segundo', 'tercero', 'cuarto', 'quinto', 'sexto', 'séptimo', 'octavo', 'noveno', 'décimo'];
$top_vendedores = User::with(['historiales_venta' => function ($query) use ($month, $currentYear) {
$query->whereMonth('fecha_inicio', $month)
->whereYear('fecha_inicio', $currentYear)
->where('estado', 'activo');
}])
->withCount(['historiales_venta' => function ($query) use ($month, $currentYear) {
$query->whereMonth('fecha_inicio', $month)
->whereYear('fecha_inicio', $currentYear)
->where('estado', 'activo');
}])
->orderBy('historiales_venta_count', 'desc')
->limit(10)
->get();
foreach ($top_vendedores as $index => $vendedor) {
$premio = $this->configuracion->{'premio_' . ($index + 1)};
$notificacion = new Notificacion();
$notificacion->remitente_id = Auth::user()->id;
// $notificacion->destinatario_id = 410;
$notificacion->destinatario_id = $vendedor->id;
$notificacion->titulo = 'Felicidades ' . $vendedor->name ?? '' . ' 🎊';
$notificacion->descripcion = 'Quedaste ' . $rangos[$index] . ' en el #Top10MejoresDistribuidores del mes de ' . date('M', strtotime('last month'));
$notificacion->estado = 1;
$notificacion->todos = 0;
$notificacion->premio = str_replace('.', '', str_replace('$', '', $premio));
$notificacion->save();
Log_general::create([
'user_id' => Auth::user()->id,
'detalle' => 'El usuario ' . Auth::user()->name . ' ha entregado el premio ' . $premio . ' para el usuario: ' . $vendedor->name . ' - ' . $vendedor->email
]);
}
PremioEntregado::create([
'user_id' => Auth::user()->id,
'fecha' => $monthYear,
'entregado' => 1
]);
$this->alert('success', 'Premios entregados correctamente', [
'position' => 'top'
]);
}
}