ldc-mesbr-app/app/Console/Kernel.php

120 lines
4.8 KiB
PHP
Raw Normal View History

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\Site;
use App\Campagne;
use App\Jobs\CleanOldDatasJob;
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;
2019-01-15 14:17:15 +01:00
class Kernel extends ConsoleKernel
{
2019-02-20 16:26:32 +01:00
use CouponTrait;
2021-04-27 17:29:47 +02:00
2019-01-15 14:17:15 +01:00
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [];
2019-01-15 14:17:15 +01:00
/**
* Define the application's command schedule.
*
2025-04-03 09:30:16 +02:00
* @param \Illuminate\Console\Scheduling\Schedule $schedule
2019-01-15 14:17:15 +01:00
* @return void
*/
protected function schedule(Schedule $schedule)
{
// Clean datas older than 3 years every day at midnight.
$schedule->job(CleanOldDatasJob::class)->daily();
2025-04-03 09:30:16 +02:00
// ---- Commande d'alerte appelé tt les jours de la semaine a 7h00
2019-02-13 17:04:37 +01:00
$schedule->call(function () {
2025-04-03 09:30:16 +02:00
$sites = Site::all();
$couponSeuilGlobal = array();
2019-02-27 16:11:28 +01:00
$couponDiffusionGlobal = array();
2025-04-03 09:30:16 +02:00
$sendGlobal = false;
foreach ($sites as $site) {
// ---- Pour chaque site
2019-03-13 13:49:13 +01:00
$campagnes = Campagne::where(['camp_siteId' => $site->site_id])->get();
foreach ($campagnes as $campagne) {
2025-04-03 09:30:16 +02:00
$sendMail = false;
$users = array();
$couponSeuil = array();
$couponDiffusion = array();
2025-04-03 09:30:16 +02:00
$coupons = $campagne->coupons()->get();
foreach ($coupons as $coupon) {
$couponAlert = false;
2021-04-27 17:29:47 +02:00
if ($coupon->coupon_published) {
2019-03-20 09:47:22 +01:00
$coupon->coupon_left = $this->getFullQuantity($coupon) - $this->getUsedQuantity($coupon);
2021-04-27 17:29:47 +02:00
if ($coupon->coupon_left < $coupon->coupon_seuil_alert) {
2019-03-20 09:47:22 +01:00
array_push($couponSeuil, $coupon);
array_push($couponSeuilGlobal, [$site->site_nom, $coupon]);
2025-04-03 09:30:16 +02:00
$sendGlobal = true;
2019-03-20 09:47:22 +01:00
$couponAlert = true;
}
// --- Check MaxDateDiffusion
2025-04-03 09:30:16 +02:00
if ($coupon->coupon_maxdatediffusion != null && strtotime($coupon->coupon_maxdatediffusion) < (time() + (30 * 24 * 60 * 60))) {
// Fin de diffusion dans moins de 30jours
2019-03-20 09:47:22 +01:00
array_push($couponDiffusion, $coupon);
array_push($couponDiffusionGlobal, [$site->site_nom, $coupon]);
2025-04-03 09:30:16 +02:00
$sendGlobal = true;
2019-03-20 09:47:22 +01:00
$couponAlert = true;
}
2019-02-20 16:26:32 +01:00
2021-04-27 17:29:47 +02:00
if ($couponAlert) {
2019-03-20 09:47:22 +01:00
$notifications = Notification::where(['notif_campId' => $campagne->camp_id])->get();
foreach ($notifications as $notification) {
2021-04-27 17:29:47 +02:00
if (!array_key_exists($notification->notif_id, $users)) {
2025-04-03 09:30:16 +02:00
$userObject = [];
$userObject['email'] = $notification->notif_email;
$userObject['name'] = $notification->notif_name;
2019-03-20 09:47:22 +01:00
$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
}
} //end if
} //end foreach
2021-04-27 17:29:47 +02:00
if ($sendMail) {
Mail::to($users)->send(new AlertBr($couponSeuil, $couponDiffusion, $site->site_nom));
}
} //end foreach
} //end foreach
2021-04-27 17:29:47 +02:00
if ($sendGlobal) {
2019-02-27 16:11:28 +01:00
$superadmins = array();
2025-04-03 09:30:16 +02:00
$admins = User::where(['roles' => 'superadmin'])->get();
2019-02-27 16:11:28 +01:00
foreach ($admins as $admin) {
2025-04-03 09:30:16 +02:00
$adminObject = [];
2019-02-27 16:11:28 +01:00
$adminObject['email'] = $admin->email;
2025-04-03 09:30:16 +02:00
$adminObject['name'] = $admin->name;
2021-04-27 17:29:47 +02:00
array_push($superadmins, (object)$adminObject);
2019-02-27 16:11:28 +01:00
}
Mail::to($superadmins)->send(new AlertBrAdmin($couponSeuilGlobal, $couponDiffusionGlobal));
2021-04-27 17:29:47 +02:00
}
2019-02-27 16:31:52 +01:00
})->weekdays()->at('7:00');
2019-01-15 14:17:15 +01:00
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
2021-04-27 17:29:47 +02:00
$this->load(__DIR__ . '/Commands');
2019-01-15 14:17:15 +01:00
require base_path('routes/console.php');
}
}