ldc-mesbr-app/app/Traits/GraphTrait.php
Vincent BRIDIER 203922849c
fix: PHPCBF
2025-04-03 09:30:16 +02:00

76 lines
2.4 KiB
PHP

<?php
namespace App\Traits;
use App\Statistique;
use DateTime;
trait GraphTrait
{
// ------ Make Labels
public function getLabelsForStats($startdate, $enddate)
{
// ---- Calcul de l'intervalle en jour
$datetime1 = new DateTime($startdate);
$datetime2 = new DateTime($enddate);
$interval = $datetime1->diff($datetime2);
$days = $interval->format('%a');
// ---- Labels des graphiques
$labels = [];
for ($i = 0; $i <= $days; $i++) {
$date = date('d/m/Y', strtotime("+" . $i . " days", strtotime($startdate)));
array_push($labels, $date);
}
return $labels;
}
// ---- Data for global graph
public function getDataForCamp($labels, $campid)
{
$dataGlobal = [];
foreach ($labels as $label) {
$date = date('Y-m-d', strtotime(str_replace('/', '-', $label)));
$stat = Statistique::where(['stat_campId' => $campid])->whereDate('created_at', '=', $date)->sum('stat_couponPrinted');
if (isset($stat)) {
$dataGlobal[] = $stat;
} else {
$dataGlobal[] = 0;
}
}
return $dataGlobal;
}
// ---- Data for one coupon on one camp
public function getDataForCoupon($labels, $campid, $coupon)
{
$dataCoupon = [];
foreach ($labels as $label) {
$date = date('Y-m-d', strtotime(str_replace('/', '-', $label)));
$stat = Statistique::where(['stat_campId' => $campid, 'stat_couponId' => $coupon->coupon_id])->whereDate('created_at', '=', $date)->sum('stat_couponPrinted');
if (isset($stat)) {
$dataCoupon[] = $stat;
} else {
$dataCoupon[] = 0;
}
}
return $dataCoupon;
}
// ---- Data for one coupon all camp
public function getDataForCouponAllCamp($labels, $coupon)
{
$dataCoupon = [];
foreach ($labels as $label) {
$date = date('Y-m-d', strtotime(str_replace('/', '-', $label)));
$stat = Statistique::where(['stat_couponId' => $coupon->coupon_id])->whereDate('created_at', '=', $date)->sum('stat_couponPrinted');
if (isset($stat)) {
$dataCoupon[] = $stat;
} else {
$dataCoupon[] = 0;
}
}
return $dataCoupon;
}
}