2019-03-18 08:58:41 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Traits;
|
|
|
|
|
|
|
|
|
|
use App\Statistique;
|
|
|
|
|
use DateTime;
|
|
|
|
|
|
2021-04-27 17:29:47 +02:00
|
|
|
trait GraphTrait
|
|
|
|
|
{
|
2019-03-18 08:58:41 +01:00
|
|
|
// ------ Make Labels
|
2021-04-27 17:29:47 +02:00
|
|
|
public function getLabelsForStats($startdate, $enddate)
|
|
|
|
|
{
|
2019-03-18 08:58:41 +01:00
|
|
|
// ---- Calcul de l'intervalle en jour
|
|
|
|
|
$datetime1 = new DateTime($startdate);
|
|
|
|
|
$datetime2 = new DateTime($enddate);
|
2025-04-03 09:30:16 +02:00
|
|
|
$interval = $datetime1->diff($datetime2);
|
|
|
|
|
$days = $interval->format('%a');
|
2019-03-18 08:58:41 +01:00
|
|
|
|
|
|
|
|
// ---- Labels des graphiques
|
|
|
|
|
$labels = [];
|
2021-04-27 17:29:47 +02:00
|
|
|
for ($i = 0; $i <= $days; $i++) {
|
|
|
|
|
$date = date('d/m/Y', strtotime("+" . $i . " days", strtotime($startdate)));
|
2019-03-18 08:58:41 +01:00
|
|
|
array_push($labels, $date);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $labels;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---- Data for global graph
|
2021-04-27 17:29:47 +02:00
|
|
|
public function getDataForCamp($labels, $campid)
|
|
|
|
|
{
|
2019-03-18 08:58:41 +01:00
|
|
|
$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');
|
2021-04-27 17:29:47 +02:00
|
|
|
if (isset($stat)) {
|
2019-03-18 08:58:41 +01:00
|
|
|
$dataGlobal[] = $stat;
|
|
|
|
|
} else {
|
|
|
|
|
$dataGlobal[] = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $dataGlobal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---- Data for one coupon on one camp
|
2021-04-27 17:29:47 +02:00
|
|
|
public function getDataForCoupon($labels, $campid, $coupon)
|
|
|
|
|
{
|
2019-03-18 08:58:41 +01:00
|
|
|
$dataCoupon = [];
|
|
|
|
|
foreach ($labels as $label) {
|
|
|
|
|
$date = date('Y-m-d', strtotime(str_replace('/', '-', $label)));
|
2019-03-20 14:43:38 +01:00
|
|
|
$stat = Statistique::where(['stat_campId' => $campid, 'stat_couponId' => $coupon->coupon_id])->whereDate('created_at', '=', $date)->sum('stat_couponPrinted');
|
2021-04-27 17:29:47 +02:00
|
|
|
if (isset($stat)) {
|
2019-03-20 14:43:38 +01:00
|
|
|
$dataCoupon[] = $stat;
|
2019-03-18 08:58:41 +01:00
|
|
|
} else {
|
|
|
|
|
$dataCoupon[] = 0;
|
2021-04-27 17:29:47 +02:00
|
|
|
}
|
2019-03-18 08:58:41 +01:00
|
|
|
}
|
|
|
|
|
return $dataCoupon;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---- Data for one coupon all camp
|
2021-04-27 17:29:47 +02:00
|
|
|
public function getDataForCouponAllCamp($labels, $coupon)
|
|
|
|
|
{
|
2019-03-18 08:58:41 +01:00
|
|
|
$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');
|
2021-04-27 17:29:47 +02:00
|
|
|
if (isset($stat)) {
|
2019-03-18 08:58:41 +01:00
|
|
|
$dataCoupon[] = $stat;
|
|
|
|
|
} else {
|
|
|
|
|
$dataCoupon[] = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $dataCoupon;
|
|
|
|
|
}
|
2021-04-27 17:29:47 +02:00
|
|
|
}
|