2019-01-15 14:17:15 +01:00
< ? php
namespace App\Console ;
use Illuminate\Console\Scheduling\Schedule ;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel ;
2019-02-20 16:26:32 +01:00
use App\Coupon ;
use App\Site ;
use App\Panier ;
use App\PanierContent ;
use App\Mur ;
use App\Campagne ;
use App\Statistique ;
2019-02-27 16:11:28 +01:00
use App\User ;
2019-02-20 16:26:32 +01:00
use App\Notification ;
2019-02-13 17:04:37 +01:00
use App\Mail\AlertBr ;
2019-02-27 16:12:26 +01:00
use App\Mail\AlertBrAdmin ;
2019-02-13 17:04:37 +01:00
use Illuminate\Support\Facades\Mail ;
2019-02-20 16:26:32 +01:00
use App\Traits\CouponTrait ;
use Carbon\Carbon ;
2019-01-15 14:17:15 +01:00
class Kernel extends ConsoleKernel
{
2019-02-20 16:26:32 +01:00
use CouponTrait ;
2019-01-15 14:17:15 +01:00
/**
* The Artisan commands provided by your application .
*
* @ var array
*/
protected $commands = [
//
];
/**
* Define the application ' s command schedule .
*
* @ param \Illuminate\Console\Scheduling\Schedule $schedule
* @ return void
*/
protected function schedule ( Schedule $schedule )
{
2019-02-20 16:26:32 +01:00
//---- Commande d'alerte appelé tt les jours de la semaine a 7h00
2019-02-13 17:04:37 +01:00
$schedule -> call ( function () {
2019-02-20 16:26:32 +01:00
$sites = Site :: all ();
2019-02-27 16:11:28 +01:00
$couponSeuilGlobal = array ();
$couponDiffusionGlobal = array ();
$sendGlobal = false ;
2019-03-13 12:02:05 +01:00
foreach ( $sites as $site ) { // ---- Pour chaque site
2019-03-13 13:49:13 +01:00
$campagnes = Campagne :: where ([ 'camp_siteId' => $site -> site_id ]) -> get ();
2019-03-13 12:02:05 +01:00
foreach ( $campagnes as $campagne ) {
$sendMail = false ;
$users = array ();
$couponSeuil = array ();
$couponDiffusion = array ();
$coupons = $campagne -> coupons () -> get ();
foreach ( $coupons as $coupon ) {
$couponAlert = false ;
2019-03-20 09:47:22 +01:00
if ( $coupon -> coupon_published ){
2019-03-12 11:43:50 +01:00
2019-03-20 09:47:22 +01:00
$coupon -> coupon_left = $this -> getFullQuantity ( $coupon ) - $this -> getUsedQuantity ( $coupon );
if ( $coupon -> coupon_left < $coupon -> coupon_seuil_alert ){
array_push ( $couponSeuil , $coupon );
array_push ( $couponSeuilGlobal , [ $site -> site_nom , $coupon ]);
$sendGlobal = true ;
$couponAlert = true ;
}
// --- Check MaxDateDiffusion
if ( $coupon -> coupon_maxdatediffusion != null && strtotime ( $coupon -> coupon_maxdatediffusion ) < ( time () + ( 30 * 24 * 60 * 60 ))){ //Fin de diffusion dans moins de 30jours
array_push ( $couponDiffusion , $coupon );
array_push ( $couponDiffusionGlobal , [ $site -> site_nom , $coupon ]);
$sendGlobal = true ;
$couponAlert = true ;
}
2019-02-20 16:26:32 +01:00
2019-03-20 09:47:22 +01:00
if ( $couponAlert ){
$notifications = Notification :: where ([ 'notif_campId' => $campagne -> camp_id ]) -> get ();
foreach ( $notifications as $notification ) {
if ( ! array_key_exists ( $notification -> notif_id , $users )){
$userObject = [];
$userObject [ 'email' ] = $notification -> notif_email ;
$userObject [ 'name' ] = $notification -> notif_name ;
$users [ $notification -> notif_id ] = ( object ) $userObject ;
}
2019-02-20 16:26:32 +01:00
}
2019-03-20 09:47:22 +01:00
$sendMail = true ;
2019-02-20 16:26:32 +01:00
}
2019-03-20 09:47:22 +01:00
2019-02-20 16:26:32 +01:00
}
}
2019-03-13 12:02:05 +01:00
if ( $sendMail ){
Mail :: to ( $users ) -> send ( new AlertBr ( $couponSeuil , $couponDiffusion , $site -> site_nom ));
}
2019-02-20 16:26:32 +01:00
}
2019-02-27 16:11:28 +01:00
}
if ( $sendGlobal ) {
$superadmins = array ();
$admins = User :: where ([ 'roles' => 'superadmin' ]) -> get ();
foreach ( $admins as $admin ) {
$adminObject = [];
$adminObject [ 'email' ] = $admin -> email ;
$adminObject [ 'name' ] = $admin -> name ;
array_push ( $superadmins , ( object ) $adminObject );
}
Mail :: to ( $superadmins ) -> send ( new AlertBrAdmin ( $couponSeuilGlobal , $couponDiffusionGlobal ));
}
2019-02-27 16:31:52 +01:00
}) -> weekdays () -> at ( '7:00' );
2019-02-20 16:26:32 +01:00
// ---- Sauvegarde des stats tous les jours a 23:45
$schedule -> call ( function () {
$last_stat = Statistique :: orderBy ( 'created_at' , 'DESC' ) -> first ();
2019-02-22 16:48:34 +01:00
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' );
}
2019-02-20 16:26:32 +01:00
$campagnes = Campagne :: all ();
foreach ( $campagnes as $campagne ) {
$coupons = $campagne -> coupons () -> get ();
foreach ( $coupons as $coupon ) {
if ( $campagne -> camp_type == 0 || $campagne -> camp_type == 2 ) { //Calcul des BR consommé pour les murs
2019-03-21 09:14:55 +01:00
$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 ();
2019-02-20 16:26:32 +01:00
}
2019-03-21 09:14:55 +01:00
}
// 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;
// }
// }
2019-02-20 16:26:32 +01:00
}
}
2019-03-20 10:56:30 +01:00
}) -> everyFifteenMinutes ();
2019-02-20 16:26:32 +01:00
2019-01-15 14:17:15 +01:00
}
/**
* Register the commands for the application .
*
* @ return void
*/
protected function commands ()
{
$this -> load ( __DIR__ . '/Commands' );
require base_path ( 'routes/console.php' );
}
}