entrega de premios desde boton

This commit is contained in:
Felipe
2024-03-06 15:05:42 -05:00
parent 3d1eac3801
commit fbf1f43a09
2 changed files with 37 additions and 7 deletions
+31
View File
@@ -3,7 +3,9 @@
namespace App\Http\Livewire;
use App\Models\Configuracione;
use App\Models\Notificacion;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use Jantinnerezo\LivewireAlert\LivewireAlert;
@@ -16,6 +18,7 @@ class ShowTop extends Component
public function render()
{
$this->configuracion = Configuracione::orderby('id', 'desc')->first();
$month = $this->showLastMonth ? date('m', strtotime('last month')) : date('m');
@@ -35,4 +38,32 @@ class ShowTop extends Component
'usuarios' => $consulta_ventas,
]);
}
public function entregarPremio()
{
$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) {
$query->whereMonth('fecha_inicio', $month);
}])
->withCount(['historiales_venta' => function ($query) use ($month) {
$query->whereMonth('fecha_inicio', $month);
}])->orderBy('historiales_venta_count', 'desc')->take(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 = $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();
}
}
}