ldc-mesbr-app/app/Jobs/ArchiveStatsJob.php
2026-10-28 12:27:46 +01:00

67 lines
1.8 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\ArchiveStat;
use App\Models\Statistique;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class ArchiveStatsJob implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
// Construct method.
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
try {
DB::transaction(function (): void {
Statistique::query()
->where('date', '<', now())
->each(function ($stat): void {
$archiveStat = new ArchiveStat();
$archiveStat->quantity = $stat->stat_couponPrinted;
$archiveStat->date = $stat->date;
$archiveStat->archive_coupon_id = $stat->stat_couponId;
$archiveStat->archive_campaign_id = $stat->stat_campId;
$archiveStat->saveOrFail();
});
Statistique::query()
->where('date', '<', now())
->delete();
});
} catch (\Exception $e) {
Log::error(
sprintf(
"ArchiveStatsJob failed - Code : %s - %s",
$e->getCode(),
$e->getMessage(),
)
);
\report($e);
} //end try
}
}