Maj Stats : get rid of cron & add record of stats into ajax_impression

This commit is contained in:
Thibault Richy 2019-03-21 15:29:29 +01:00
parent ca5af9f5e7
commit 7316361c5b
2 changed files with 54 additions and 40 deletions

View file

@ -109,52 +109,52 @@ class Kernel extends ConsoleKernel
})->weekdays()->at('7:00');
// ---- Sauvegarde des stats tous les jours a 23:45
$schedule->call(function () {
$campagnes = Campagne::all();
foreach ($campagnes as $campagne) {
// $schedule->call(function () {
// $campagnes = Campagne::all();
// foreach ($campagnes as $campagne) {
$last_stat = Statistique::where(['stat_campId' => $campagne->camp_id])->orderBy('created_at', 'DESC')->first();
if(isset($last_stat->created_at)){
$dateStart = $last_stat->created_at;
} else {
$dateStart = Carbon::now()->setTime(0,0,0)->subMonth()->format('Y-m-d H:i:s');
}
// $last_stat = Statistique::where(['stat_campId' => $campagne->camp_id])->orderBy('created_at', 'DESC')->first();
// if(isset($last_stat->created_at)){
// $dateStart = $last_stat->created_at;
// } else {
// $dateStart = Carbon::now()->setTime(0,0,0)->subMonth()->format('Y-m-d H:i:s');
// }
$coupons = $campagne->coupons()->get();
foreach ($coupons as $coupon) {
if ($campagne->camp_type == 0 || $campagne->camp_type == 2) { //Calcul des BR consommé pour les murs
// $coupons = $campagne->coupons()->get();
// foreach ($coupons as $coupon) {
// if ($campagne->camp_type == 0 || $campagne->camp_type == 2) { //Calcul des BR consommé pour les murs
$counts = Mur::selectRaw('count(*) as count, DATE_FORMAT(mur_createdate, "%Y-%m-%d") as date')
->where(['mur_couponId' => $coupon->coupon_id, 'mur_campid' => $campagne->camp_id])
->whereBetween('mur_createdate', [$dateStart, Carbon::now()->setTime(23,59,59)->format('Y-m-d H:i:s')])
->groupBy(DB::raw("DATE_FORMAT(mur_createdate, '%Y-%m-%d')"))
->pluck('count', 'date');
// $counts = Mur::selectRaw('count(*) as count, DATE_FORMAT(mur_createdate, "%Y-%m-%d") as date')
// ->where(['mur_couponId' => $coupon->coupon_id, 'mur_campid' => $campagne->camp_id])
// ->whereBetween('mur_createdate', [$dateStart, Carbon::now()->setTime(23,59,59)->format('Y-m-d H:i:s')])
// ->groupBy(DB::raw("DATE_FORMAT(mur_createdate, '%Y-%m-%d')"))
// ->pluck('count', 'date');
foreach($counts as $date => $count){
$stat = new Statistique();
$stat->stat_campId = $campagne->camp_id;
$stat->stat_couponId = $coupon->coupon_id;
$stat->stat_couponPrinted = $count;
$stat->created_at = $date;
$stat->save();
}
// foreach($counts as $date => $count){
// $stat = new Statistique();
// $stat->stat_campId = $campagne->camp_id;
// $stat->stat_couponId = $coupon->coupon_id;
// $stat->stat_couponPrinted = $count;
// $stat->created_at = $date;
// $stat->save();
// }
}
// else { //Calcul des BR consommé pour les paniers
// $count = 0;
// $paniers = Panier::where('panier_campId', $campagne->camp_id)->where(function($query) use ($dateStart){
// $query->whereBetween('panier_createdate', [$dateStart, Carbon::now()->setTime(23,59,59)->format('Y-m-d H:i:s')])
// ->orWhereBetween('panier_updatedate', [$dateStart, Carbon::now()->setTime(23,59,59)->format('Y-m-d H:i:s')]);
// })->get();
// foreach ($paniers as $panier) {
// $panierContentCount = $panier->panierContents()->where(['panier_content_couponId' => $coupon->coupon_id])->sum('panier_content_printed');
// $count = $count + $panierContentCount;
// }
// }
// }
// // else { //Calcul des BR consommé pour les paniers
// // $count = 0;
// // $paniers = Panier::where('panier_campId', $campagne->camp_id)->where(function($query) use ($dateStart){
// // $query->whereBetween('panier_createdate', [$dateStart, Carbon::now()->setTime(23,59,59)->format('Y-m-d H:i:s')])
// // ->orWhereBetween('panier_updatedate', [$dateStart, Carbon::now()->setTime(23,59,59)->format('Y-m-d H:i:s')]);
// // })->get();
// // foreach ($paniers as $panier) {
// // $panierContentCount = $panier->panierContents()->where(['panier_content_couponId' => $coupon->coupon_id])->sum('panier_content_printed');
// // $count = $count + $panierContentCount;
// // }
// // }
}
}
})->everyFifteenMinutes();
// }
// }
// })->everyFifteenMinutes();
}

View file

@ -146,6 +146,13 @@ class AjaxController extends Controller
$mur->mur_contactId = $contact->contact_id;
$mur->mur_couponId = $cp->coupon_id;
$mur->save();
//Sauvegarde de stat
$stat = new Statistique();
$stat->stat_campId = $campagne->camp_id;
$stat->stat_couponId = $cp->coupon_id;
$stat->stat_couponPrinted = 1;
$stat->save();
}
}
}elseif ($campagne->camp_type == 1) { //---- POUR LES PANIERS
@ -227,6 +234,13 @@ class AjaxController extends Controller
$mur->mur_contactId = $contact->contact_id;
$mur->mur_couponId = $cp->coupon_id;
$mur->save();
//Sauvegarde de stat
$stat = new Statistique();
$stat->stat_campId = $campagne->camp_id;
$stat->stat_couponId = $cp->coupon_id;
$stat->stat_couponPrinted = 1;
$stat->save();
}
}
} elseif ($campagne->camp_type == 3) { //---- POUR LES PANIERS AVEC SELECTION