115 lines
4.9 KiB
PHP
115 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Console;
|
|
|
|
use Illuminate\Console\Scheduling\Schedule;
|
|
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
|
use App\Models\Site;
|
|
use App\Models\Campagne;
|
|
use App\Jobs\ArchiveStatsJob;
|
|
use App\Jobs\CleanOldDatasJob;
|
|
use App\Models\User;
|
|
use App\Models\Notification;
|
|
use App\Mail\AlertBr;
|
|
use App\Mail\AlertBrAdmin;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use App\Traits\CouponTrait;
|
|
|
|
class Kernel extends ConsoleKernel
|
|
{
|
|
use CouponTrait;
|
|
|
|
/**
|
|
* The Artisan commands provided by your application.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $commands = [
|
|
\App\Console\Commands\CustomersClaims::class,
|
|
\App\Console\Commands\ArchiveStatsCommand::class,
|
|
\App\Console\Commands\RegenBRBase::class,
|
|
];
|
|
|
|
/**
|
|
* Define the application's command schedule.
|
|
*
|
|
* @param \Illuminate\Console\Scheduling\Schedule $schedule
|
|
* @return void
|
|
*/
|
|
protected function schedule(Schedule $schedule)
|
|
{
|
|
// Archive stats every day at 2:00 AM (to prevent time difference between the server and the reality).
|
|
$schedule->job(ArchiveStatsJob::class)->dailyAt('2:00');
|
|
|
|
// Clean datas older than 3 years every day at midnight.
|
|
$schedule->job(CleanOldDatasJob::class)->daily();
|
|
|
|
// ---- Commande d'alerte appelé tt les jours de la semaine a 7h00
|
|
$schedule->call(function (): void {
|
|
$sites = Site::all();
|
|
$couponSeuilGlobal = [];
|
|
$couponDiffusionGlobal = [];
|
|
$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 = [];
|
|
$couponSeuil = [];
|
|
$couponDiffusion = [];
|
|
$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;
|
|
}
|
|
} //end if
|
|
} //end foreach
|
|
|
|
if ($sendMail) {
|
|
Mail::to($users)->send(new AlertBr($couponSeuil, $couponDiffusion, $site->site_nom));
|
|
}
|
|
} //end foreach
|
|
} //end foreach
|
|
if ($sendGlobal) {
|
|
$superadmins = [];
|
|
$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));
|
|
}
|
|
})->weekdays()->at('7:00');
|
|
}
|
|
}
|