143 lines
5 KiB
PHP
143 lines
5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Statistique;
|
|
use App\Campagne;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use App\Traits\CouponTrait;
|
|
use DateTime;
|
|
|
|
class StatistiqueController extends Controller
|
|
{
|
|
use CouponTrait;
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index()
|
|
{
|
|
$sites = Auth::user()->sites()->get();
|
|
foreach ($sites as $site) {
|
|
if(isset($site->campagnes)){
|
|
$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);
|
|
$coupon->isPrintable = $this->isAvailableToPrint($coupon, $campagne);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return view('stats.index')->with(['sites' => $sites]);
|
|
}
|
|
|
|
/**
|
|
* Show the stat detail of a campaign.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function statdetail(Campagne $campagne) {
|
|
$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;
|
|
}
|
|
|
|
$coupons = $campagne->coupons;
|
|
foreach ($coupons as $coupon) {
|
|
$coupon->quantity = $this->getFullQuantity($coupon);
|
|
$coupon->remain = $this->getFullQuantity($coupon) - $this->getUsedQuantity($coupon);
|
|
$coupon->isPrintable = $this->isAvailableToPrint($coupon, $campagne);
|
|
$coupon->color = $colors[$count];
|
|
$count++;
|
|
}
|
|
$enddate = date('Y-m-d');
|
|
$startdate = date('Y-m-d', strtotime("-30 days"));
|
|
return view('stats.detail')->with(['campagne' => $campagne, 'startdate' => $startdate, 'enddate'=> $enddate]);
|
|
}
|
|
|
|
public function load_stats(Request $request){
|
|
$campid = $request->campid;
|
|
$startdate = $request->startdate;
|
|
$enddate = $request->enddate;
|
|
|
|
$datetime1 = new DateTime($request->startdate);
|
|
$datetime2 = new DateTime($request->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);
|
|
}
|
|
//array_reverse($labels);
|
|
|
|
// ---- Data pour le graphique global
|
|
$dataGlobal = [];
|
|
$test = [];
|
|
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');
|
|
$test[] = $date;
|
|
if(isset($stat)){
|
|
$dataGlobal[] = $stat;
|
|
} else {
|
|
$dataGlobal[] = 0;
|
|
}
|
|
}
|
|
|
|
// ---- Data pour le graphique par coupons
|
|
$campagne = Campagne::where(['camp_id' => $campid])->first();
|
|
$coupons = $campagne->coupons;
|
|
$dataSpec = [];
|
|
foreach ($coupons as $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)->first();
|
|
if(isset($stat->stat_couponPrinted)){
|
|
$dataCoupon[] = $stat->stat_couponPrinted;
|
|
} else {
|
|
$dataCoupon[] = 0;
|
|
}
|
|
}
|
|
$dataSpec[$coupon->coupon_code] = $dataCoupon;
|
|
}
|
|
|
|
|
|
$data = array('labels' => $labels, 'dataGlobal' => $dataGlobal, 'dataSpec' => $dataSpec);
|
|
|
|
return json_encode($data);
|
|
}
|
|
}
|