2020-10-21 15:26:21 +02:00
|
|
|
<?php
|
|
|
|
|
|
2026-10-02 12:09:46 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2022-01-10 14:02:25 +01:00
|
|
|
namespace App\Http\Controllers\Bo;
|
2020-10-21 15:26:21 +02:00
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2020-11-20 15:18:31 +01:00
|
|
|
use App\Models\Participation;
|
|
|
|
|
use App\Models\Settings;
|
2026-10-02 12:09:46 +02:00
|
|
|
use Illuminate\Contracts\View\View;
|
2023-12-13 12:45:15 +01:00
|
|
|
use Illuminate\Support\Arr;
|
2026-10-02 12:09:46 +02:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
use Illuminate\Support\Facades\Date;
|
2020-11-20 15:18:31 +01:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2020-10-21 15:26:21 +02:00
|
|
|
|
|
|
|
|
class DashboardController extends Controller
|
|
|
|
|
{
|
2023-08-23 13:29:54 +00:00
|
|
|
/**
|
|
|
|
|
* Dashboard Page
|
|
|
|
|
*/
|
2026-10-02 12:09:46 +02:00
|
|
|
public function index(): View
|
2020-10-21 15:26:21 +02:00
|
|
|
{
|
2023-11-30 11:37:17 +01:00
|
|
|
// * Pour les stats.
|
2026-10-02 12:09:46 +02:00
|
|
|
/** @var Settings */
|
|
|
|
|
$settings = resolve(Settings::class);
|
2023-12-13 12:45:15 +01:00
|
|
|
$avgParticipUser = 0;
|
|
|
|
|
$statsUnique = [];
|
2026-10-02 12:09:46 +02:00
|
|
|
$query = DB::table(new Participation()->getTable());
|
|
|
|
|
$dateFrmtr = function (object $row): object {
|
|
|
|
|
$row->date = Date::parse($row->date)->isoFormat('DD/MM/YYYY');
|
|
|
|
|
|
2023-12-13 12:45:15 +01:00
|
|
|
return $row;
|
|
|
|
|
};
|
2020-11-20 15:18:31 +01:00
|
|
|
|
2026-10-02 12:09:46 +02:00
|
|
|
/** @var Collection<string, object> */
|
2023-12-13 12:45:15 +01:00
|
|
|
$participationsPerDay = (clone $query)
|
|
|
|
|
->selectRaw('COUNT(*) AS count, DATE(created_at) AS date')
|
|
|
|
|
->groupByRaw('DATE(created_at)')->get()->map($dateFrmtr)->keyBy('date');
|
2020-11-20 15:18:31 +01:00
|
|
|
|
2026-10-02 12:09:46 +02:00
|
|
|
/** @var Collection<object> */
|
2023-12-13 12:45:15 +01:00
|
|
|
$participationsUniquePerDay = (clone $query)
|
|
|
|
|
->selectRaw('DISTINCT customer_id AS user, COUNT(*) AS count, DATE(created_at) AS date')
|
|
|
|
|
->groupByRaw('DATE(created_at), user')->get()->map($dateFrmtr);
|
2020-11-20 15:18:31 +01:00
|
|
|
|
2026-10-02 12:09:46 +02:00
|
|
|
/** @var Collection<object> */
|
2023-12-13 12:45:15 +01:00
|
|
|
$participationsUniquePerCustomer = (clone $query)
|
2026-10-02 12:09:46 +02:00
|
|
|
->selectRaw('DISTINCT customer_id AS user, COUNT(*) AS count')
|
2023-12-13 12:45:15 +01:00
|
|
|
->groupBy('user')->orderBy('user')->get();
|
2020-11-20 15:18:31 +01:00
|
|
|
|
2023-12-13 12:45:15 +01:00
|
|
|
$participationsTotal = Participation::query()->count();
|
2020-11-20 15:18:31 +01:00
|
|
|
|
2026-10-02 12:09:46 +02:00
|
|
|
/** @var Collection<object> 3 codes EAN les plus utilisés */
|
2023-12-13 12:45:15 +01:00
|
|
|
$codesEan = (clone $query)
|
2024-05-17 10:53:39 +02:00
|
|
|
->selectRaw('COUNT(product_id) AS nb_usage, product_id, ean_code')
|
|
|
|
|
->join('products', 'product_id', '=', 'products.id')
|
|
|
|
|
->groupBy('product_id', 'ean_code')
|
2023-12-13 12:45:15 +01:00
|
|
|
->orderByDesc('nb_usage')->limit(3)->get();
|
2023-08-23 13:29:54 +00:00
|
|
|
|
2026-10-02 12:09:46 +02:00
|
|
|
$participationsUniquePerDay->each(function (object $row) use (&$statsUnique): void {
|
2023-12-13 12:45:15 +01:00
|
|
|
Arr::set($statsUnique, $row->date, (Arr::get($statsUnique, $row->date) ?? 0) + 1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// * Calcul participation moyenne par utilisateur.
|
|
|
|
|
if ($participationsUniquePerCustomer->count() > 0) {
|
|
|
|
|
$avgParticipUser = round($participationsTotal / $participationsUniquePerCustomer->count(), 1);
|
2020-11-20 15:18:31 +01:00
|
|
|
}
|
2026-10-02 12:09:46 +02:00
|
|
|
now()->lt($settings->end_date) ? now() : $settings->end_date;
|
2023-12-13 12:45:15 +01:00
|
|
|
$dates = $participationsPerDay->keys();
|
|
|
|
|
$count = $participationsPerDay->pluck('count');
|
|
|
|
|
$countUnique = $dates->map(fn (string $formated_date) => Arr::get($statsUnique, $formated_date, 0));
|
|
|
|
|
|
2026-10-02 12:09:46 +02:00
|
|
|
/** @var view-string $view */
|
|
|
|
|
$view = 'back::pages.dashboard.index';
|
|
|
|
|
|
|
|
|
|
return view($view, [
|
|
|
|
|
'dates' => $dates,
|
|
|
|
|
'count' => $count,
|
|
|
|
|
'countUnique' => $countUnique,
|
|
|
|
|
'participationsTotal' => $participationsTotal,
|
|
|
|
|
'avgParticipUser' => $avgParticipUser,
|
|
|
|
|
'codesEan' => $codesEan,
|
|
|
|
|
'settings' => $settings,
|
|
|
|
|
]);
|
2020-10-21 15:26:21 +02:00
|
|
|
}
|
|
|
|
|
}
|