ldc-boutique-cnc/app/Jobs/DiscountsStatusUpdate.php
2023-05-16 11:32:11 +02:00

39 lines
1.3 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\Shop\Discount;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;
/**
* Update status using
* date_start and date_end attributes.
*/
class DiscountsStatusUpdate
{
/**
* Called when the job will be executed.
*
* @return void
*/
public function handle()
{
/** @var \Illuminate\Database\Eloquent\Collection<array-key,\App\Models\Shop\Discount> $discountsToPublish */
$discountsToPublish = Discount::query()->where('date_start', '<=', Carbon::now()->toDateTime())
->whereNotNull('date_start')->get();
Log::debug(__CLASS__ . " discounts to publish " . $discountsToPublish->count());
foreach ($discountsToPublish as $discount) {
$discount->status = true;
$discount->saveOrFail();
}
/** @var \Illuminate\Database\Eloquent\Collection<array-key,\App\Models\Shop\Discount> $discountsToUnPublish */
$discountsToUnPublish = Discount::query()->where('date_end', '<=', Carbon::now()->toDateTime())
->whereNotNull('date_end')->get();
Log::debug(__CLASS__ . " discounts to unpublish " . $discountsToUnPublish->count());
foreach ($discountsToUnPublish as $discount) {
$discount->status = false;
$discount->saveOrFail();
}
}
}