ldc-crm/app/Jobs/SetServiceStats.php
2021-08-23 20:50:50 +02:00

48 lines
1.1 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\Service;
use App\Models\ServiceStat;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class SetServiceStats implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
/** @var \App\Models\Service */
protected $service;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Service $service)
{
$this->service = $service;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$serviceStat = ServiceStat::firstOrNew(['date' => date('Y-m-d')]);
if (!$serviceStat->exists) {
$serviceStat->service()->associate($this->service);
$serviceStat->brand()->associate($this->service->brand);
}
$serviceStat->nb_calls += 1;
$serviceStat->saveOrFail();
}
}