ldc-mesbr-app/app/Http/Controllers/StatistiqueController.php

122 lines
4.4 KiB
PHP
Raw Normal View History

2019-02-20 16:26:32 +01:00
<?php
namespace App\Http\Controllers;
use App\Statistique;
2019-03-12 11:43:50 +01:00
use App\Campagne;
2019-02-20 16:26:32 +01:00
use Illuminate\Http\Request;
2019-03-07 08:43:51 +01:00
use Illuminate\Support\Facades\Auth;
use App\Traits\CouponTrait;
2019-03-18 08:58:41 +01:00
use App\Traits\GraphTrait;
2019-02-20 16:26:32 +01:00
class StatistiqueController extends Controller
{
2019-03-07 08:43:51 +01:00
use CouponTrait;
2019-03-18 08:58:41 +01:00
use GraphTrait;
2021-04-27 17:29:47 +02:00
2019-02-20 16:26:32 +01:00
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
2021-04-27 17:29:47 +02:00
{
2019-03-07 08:43:51 +01:00
$sites = Auth::user()->sites()->get();
foreach ($sites as $site) {
2021-04-27 17:29:47 +02:00
if (isset($site->campagnes)) {
2019-03-07 08:43:51 +01:00
$campagnes = $site->campagnes;
foreach ($campagnes as $campagne) {
switch ($campagne->camp_type) {
case 0:
$campagne->camp_type = "Mur";
break;
case 1:
$campagne->camp_type = "Panier";
break;
case 2:
$campagne->camp_type = "Mur sans sélection";
break;
case 3:
$campagne->camp_type = "Panier Sélectionnable";
break;
}
$coupons = $campagne->coupons;
foreach ($coupons as $coupon) {
$coupon->quantity = $this->getFullQuantity($coupon);
$coupon->remain = $this->getFullQuantity($coupon) - $this->getUsedQuantity($coupon);
2019-05-07 09:43:55 +02:00
$coupon->diffused = $this->getDiffusedQuantity($coupon);
2019-03-07 08:43:51 +01:00
$coupon->isPrintable = $this->isAvailableToPrint($coupon, $campagne);
2019-03-18 09:52:43 +01:00
$coupon->isPaused = $this->isPausedToPrint($coupon, $campagne);
2019-03-07 08:43:51 +01:00
}
}
}
}
return view('stats.index')->with(['sites' => $sites]);
2019-02-20 16:26:32 +01:00
}
/**
2019-03-12 11:43:50 +01:00
* Show the stat detail of a campaign.
2019-02-20 16:26:32 +01:00
*
* @return \Illuminate\Http\Response
*/
2021-04-27 17:29:47 +02:00
public function statdetail(Campagne $campagne)
{
2019-03-12 11:43:50 +01:00
$count = 0;
$colors = config('constants.colors');
switch ($campagne->camp_type) {
case 0:
$campagne->camp_type = "Mur";
break;
case 1:
$campagne->camp_type = "Panier";
break;
case 2:
$campagne->camp_type = "Mur sans sélection";
break;
case 3:
$campagne->camp_type = "Panier Sélectionnable";
break;
}
2019-02-20 16:26:32 +01:00
2019-03-12 11:43:50 +01:00
$coupons = $campagne->coupons;
foreach ($coupons as $coupon) {
$coupon->quantity = $this->getFullQuantity($coupon);
2019-07-10 14:36:55 +02:00
$coupon->remain = $coupon->quantity - $this->getUsedQuantity($coupon);
2019-05-07 09:43:55 +02:00
$coupon->diffused = $this->getDiffusedQuantity($coupon);
2019-03-12 11:43:50 +01:00
$coupon->isPrintable = $this->isAvailableToPrint($coupon, $campagne);
2019-03-18 09:52:43 +01:00
$coupon->isPaused = $this->isPausedToPrint($coupon, $campagne);
2021-06-30 11:11:05 +02:00
$coupon->color = $coupon->color = "#" . str_pad( dechex( mt_rand( 0, 255 ) ), 2, '0', STR_PAD_LEFT) . str_pad( dechex( mt_rand( 0, 255 ) ), 2, '0', STR_PAD_LEFT) . str_pad( dechex( mt_rand( 0, 255 ) ), 2, '0', STR_PAD_LEFT);
2021-04-27 17:29:47 +02:00
$count++;
2019-03-12 11:43:50 +01:00
}
$enddate = date('Y-m-d');
$startdate = date('Y-m-d', strtotime("-30 days"));
2021-04-27 17:29:47 +02:00
return view('stats.detail')->with(['campagne' => $campagne, 'startdate' => $startdate, 'enddate' => $enddate]);
2019-02-20 16:26:32 +01:00
}
2021-04-27 17:29:47 +02:00
public function load_stats(Request $request)
{
2019-03-12 11:43:50 +01:00
$campid = $request->campid;
$startdate = $request->startdate;
$enddate = $request->enddate;
2019-02-20 16:26:32 +01:00
2019-03-18 08:58:41 +01:00
// ---- Labels pour la période
$labels = $this->getLabelsForStats($startdate, $enddate);
2019-03-12 11:43:50 +01:00
// ---- Data pour le graphique global
2019-03-18 08:58:41 +01:00
$dataGlobal = $this->getDataForCamp($labels, $campid);
2021-04-27 17:29:47 +02:00
// ---- Data pour le graphique par coupons
2019-03-12 11:43:50 +01:00
$campagne = Campagne::where(['camp_id' => $campid])->first();
$coupons = $campagne->coupons;
$dataSpec = [];
foreach ($coupons as $coupon) {
2019-03-18 08:58:41 +01:00
$dataCoupon = $this->getDataForCoupon($labels, $campid, $coupon);
2019-03-12 11:43:50 +01:00
$dataSpec[$coupon->coupon_code] = $dataCoupon;
}
$data = array('labels' => $labels, 'dataGlobal' => $dataGlobal, 'dataSpec' => $dataSpec);
return json_encode($data);
2019-02-20 16:26:32 +01:00
}
}