2020-10-21 15:26:21 +02:00
|
|
|
<?php
|
|
|
|
|
|
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;
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
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
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
|
*/
|
2023-08-24 13:00:43 +02:00
|
|
|
public function index(): \Illuminate\Contracts\View\View
|
2020-10-21 15:26:21 +02:00
|
|
|
{
|
2023-11-30 11:37:17 +01:00
|
|
|
// FIXME: refactor and simply this
|
|
|
|
|
// * Pour les stats.
|
2022-01-07 19:00:30 +01:00
|
|
|
$settings = app(Settings::class);
|
2023-11-30 11:37:17 +01:00
|
|
|
$participations = Participation::query()->selectRaw("COUNT(*) AS count, DATE(created_at) AS date")
|
2022-01-11 12:39:28 +01:00
|
|
|
->groupByRaw("DATE(created_at)")->get();
|
2023-11-30 11:37:17 +01:00
|
|
|
$participationsUniquePerDay = Participation::query()->selectRaw(
|
2023-08-24 12:59:51 +00:00
|
|
|
"DISTINCT customer_id AS user, COUNT(*) AS count, DATE(created_at) AS date"
|
|
|
|
|
)->groupByRaw("DATE(created_at), user")->get();
|
2021-08-18 17:08:02 +02:00
|
|
|
$statsUnique = [];
|
|
|
|
|
foreach ($participationsUniquePerDay as $participationPerDay) {
|
2023-08-24 13:00:43 +02:00
|
|
|
$dateUnique = Carbon::parse($participationPerDay->date)->format('d/m/Y');
|
2020-11-20 15:18:31 +01:00
|
|
|
$statsUnique[$dateUnique] = isset($statsUnique[$dateUnique]) ? $statsUnique[$dateUnique] + 1 : 1;
|
|
|
|
|
}
|
2023-11-30 11:37:17 +01:00
|
|
|
$participationsUnique = Participation::query()->selectRaw("DISTINCT customer_id AS user, COUNT(*) AS count")
|
2023-11-20 17:22:24 +01:00
|
|
|
->groupBy("user")->orderBy("user")->get();
|
2020-11-20 15:18:31 +01:00
|
|
|
|
2023-11-30 11:37:17 +01:00
|
|
|
// * Données calculées à afficher.
|
|
|
|
|
$totalParticipation = Participation::query()->count();
|
2021-08-18 17:08:02 +02:00
|
|
|
if (count($participationsUnique) > 0) {
|
2020-12-01 12:46:56 +01:00
|
|
|
$avgParticipUser = round($totalParticipation / count($participationsUnique), 1);
|
|
|
|
|
} else {
|
|
|
|
|
$avgParticipUser = 0;
|
|
|
|
|
}
|
2020-11-20 15:18:31 +01:00
|
|
|
|
2023-11-30 11:37:17 +01:00
|
|
|
// * Tableau des dates comprises dans la table participation.
|
2021-08-18 17:08:02 +02:00
|
|
|
foreach ($participations as $participation) {
|
2023-08-24 13:00:43 +02:00
|
|
|
$datesParticipations[Carbon::parse($participation->date)->format('d/m/Y')] = $participation->count;
|
2021-08-18 17:08:02 +02:00
|
|
|
}
|
2020-11-20 15:18:31 +01:00
|
|
|
|
2023-11-30 11:37:17 +01:00
|
|
|
// * Création des tableaux avec toutes les dates depuis le début du jeu et le nombre de participations associées.
|
2023-08-23 13:29:54 +00:00
|
|
|
$date = $settings->start_date;
|
2023-11-30 11:37:17 +01:00
|
|
|
$dateFin = now()->lt($settings->end_date) ? now() : $settings->end_date;
|
2020-11-20 15:18:31 +01:00
|
|
|
|
2022-01-11 12:39:28 +01:00
|
|
|
// Récupération des 3 codes EAN les plus utilisés.
|
2023-11-30 11:37:17 +01:00
|
|
|
$codesEan = Participation::query()->select(
|
2023-08-24 13:00:43 +02:00
|
|
|
DB::raw('COUNT(product_id) AS nb_usage, product_id')
|
|
|
|
|
)->groupBy('product_id')->orderByDesc('nb_usage')->limit(3)->get();
|
2020-11-20 15:18:31 +01:00
|
|
|
|
2023-08-23 13:29:54 +00:00
|
|
|
|
|
|
|
|
// TODO : Carbon Interval.
|
2023-11-30 11:37:17 +01:00
|
|
|
// * Génération des tableaux de date depuis le début du jeu.
|
2021-08-18 17:08:02 +02:00
|
|
|
$dates = [];
|
|
|
|
|
$count = [];
|
2020-12-01 12:55:34 +01:00
|
|
|
$countUnique = [];
|
2020-11-20 15:18:31 +01:00
|
|
|
while ($dateFin > $date) {
|
2021-08-18 17:08:02 +02:00
|
|
|
$dates[] = $date->format('d/m/Y');
|
2022-01-11 12:39:28 +01:00
|
|
|
$count[] = isset($datesParticipations[$date->format('d/m/Y')]) ?
|
2023-08-23 13:29:54 +00:00
|
|
|
$datesParticipations[$date->format('d/m/Y')] : 0;
|
2020-11-20 15:18:31 +01:00
|
|
|
$countUnique[] = isset($statsUnique[$date->format('d/m/Y')]) ? $statsUnique[$date->format('d/m/Y')] : 0;
|
|
|
|
|
$date->addDay();
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-20 17:22:24 +01:00
|
|
|
return view('back::pages.dashboard.index', compact(
|
2022-01-11 12:39:28 +01:00
|
|
|
'dates',
|
|
|
|
|
'count',
|
|
|
|
|
'countUnique',
|
|
|
|
|
'totalParticipation',
|
|
|
|
|
'avgParticipUser',
|
|
|
|
|
'codesEan',
|
|
|
|
|
'settings'
|
|
|
|
|
));
|
2020-10-21 15:26:21 +02:00
|
|
|
}
|
|
|
|
|
}
|