Files
api_arrahn/app/Jobs/WhatsappUnsoldCommodityNotification.php
T
2024-06-11 08:41:12 +08:00

88 lines
2.2 KiB
PHP

<?php
namespace App\Jobs;
use Illuminate\Support\Facades\Http;
use App\Setting;
use Carbon\Carbon;
use App\KomuditiPurchase;
use Illuminate\Support\Facades\Log;
class WhatsappUnsoldCommodityNotification extends Job
{
public $status;
public $start_time;
public $end_time;
public $message;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$settings = Setting::on('mysql7')->where('group', 'unsold_commodity_notifications')->get();
$this->status = $settings->where('key', 'status')->first()->value;
$this->start_time = $settings->where('key', 'start_time')->first()->value;
$this->end_time = $settings->where('key', 'end_time')->first()->value;
$this->message = $settings->where('key', 'message')->first()->value;
$isKomoditiStillAvailable = KomuditiPurchase::on('mysql7')->latest()->first()->unit_komuditi === 0;
$pic_key = 'pic_' . strtolower(Carbon::now()->englishDayOfWeek);
$pic = json_decode($settings->where('key', $pic_key)->first()->value);
$phone_numbers = array_map(function ($p) {
if(isset($p->no_telefon)){
return $p->no_telefon;
}
}, $pic);
if ($phone_numbers[0] === NULL) {
return;
}
if ($this->status === '0') {
return;
}
if (Carbon::parse($this->start_time) > Carbon::now()) {
return;
}
if (Carbon::parse($this->end_time) < Carbon::now()) {
return;
}
if (!$isKomoditiStillAvailable) {
return;
}
foreach ($phone_numbers as $phone_number) {
Http::get(env('WABOT_API'), [
'number' => $phone_number,
'type' => 'text',
'message' => $this->message,
'instance_id' => env('WABOT_INSTANCES_ID'),
'access_token' => env('WABOT_TOKEN_KEYS')
]);
}
dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::now()->addMinutes(5)));
}
}