ldc-mesbr-app/app/Console/Commands/CheckAlert.php
Vincent BRIDIER d1495826a4 - yarn.lock
2021-10-10 17:30:35 +02:00

119 lines
4.1 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Campagne;
use App\Mail\AlertBr;
use App\Mail\AlertBrAdmin;
use App\Notification;
use App\Site;
use App\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
use App\Traits\CouponTrait;
class CheckAlert extends Command
{
use CouponTrait;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'check:alert';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->info('Démarrage du traitement');
$sites = Site::all();
$couponSeuilGlobal = array();
$couponDiffusionGlobal = array();
$sendGlobal = false;
foreach ($sites as $site) { // ---- Pour chaque site
$campagnes = Campagne::where(['camp_siteId' => $site->site_id])->get();
foreach ($campagnes as $campagne) {
$sendMail = false;
$users = array();
$couponSeuil = array();
$couponDiffusion = array();
$coupons = $campagne->coupons()->get();
foreach ($coupons as $coupon) {
$couponAlert = false;
if ($coupon->coupon_published) {
$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;
}
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;
}
}
$sendMail = true;
}
}
}
if ($sendMail) {
Mail::to($users)->send(new AlertBr($couponSeuil, $couponDiffusion, $site->site_nom));
}
}
}
if ($sendGlobal) {
$this->line("Envoi mail superadmin");
$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));
}
$this->line("");
$this->info('Fin du traitement');
}
}