From 7316361c5b75e7dcc1fe2db04a54dc39e4d4e022 Mon Sep 17 00:00:00 2001 From: Thibault Richy Date: Thu, 21 Mar 2019 15:29:29 +0100 Subject: [PATCH] Maj Stats : get rid of cron & add record of stats into ajax_impression --- app/Console/Kernel.php | 80 ++++++++++++------------- app/Http/Controllers/AjaxController.php | 14 +++++ 2 files changed, 54 insertions(+), 40 deletions(-) diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 1cb7ca4..660af7f 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -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(); } diff --git a/app/Http/Controllers/AjaxController.php b/app/Http/Controllers/AjaxController.php index 6ceb1f7..657322b 100644 --- a/app/Http/Controllers/AjaxController.php +++ b/app/Http/Controllers/AjaxController.php @@ -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