127 lines
4.0 KiB
PHP
127 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Jobs\Concerns\ResolvesJanaNotisJumlah;
|
|
use App\Models\Cawangan;
|
|
use App\Models\Customer;
|
|
use App\Models\Gadai;
|
|
use App\Models\Lelong;
|
|
use App\Models\LelongBid;
|
|
use App\Services\WhatsAppCloudService;
|
|
use Carbon\Carbon;
|
|
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\Log;
|
|
|
|
class WhatsappNotisLelongFasa2 implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, ResolvesJanaNotisJumlah, SerializesModels;
|
|
|
|
public $timeout = 600;
|
|
|
|
public $tries = 1;
|
|
|
|
public $user_id;
|
|
public $user_kodcaw;
|
|
protected $lelong_id;
|
|
|
|
public function __construct($user_id, $user_kodcaw, $lelong_id)
|
|
{
|
|
$this->user_id = $user_id;
|
|
$this->user_kodcaw = $user_kodcaw;
|
|
$this->lelong_id = $lelong_id;
|
|
$this->onQueue('default');
|
|
}
|
|
|
|
public function handle(WhatsAppCloudService $whatsapp)
|
|
{
|
|
if (!$whatsapp->isEnabled()) {
|
|
Log::warning('WhatsappNotisLelongFasa2 skipped: WhatsApp Cloud API not enabled.');
|
|
return;
|
|
}
|
|
|
|
$caw = Cawangan::where('caw_code', $this->user_kodcaw)->first();
|
|
if (!$caw) {
|
|
Log::error('WhatsappNotisLelongFasa2: cawangan not found', ['kodcaw' => $this->user_kodcaw]);
|
|
return;
|
|
}
|
|
|
|
$lel = Lelong::find($this->lelong_id);
|
|
if (!$lel) {
|
|
Log::error('WhatsappNotisLelongFasa2: lelong not found', ['lelong_id' => $this->lelong_id]);
|
|
return;
|
|
}
|
|
|
|
$arraynorujukan = LelongBid::where('lelong_id', $this->lelong_id)
|
|
->where('kodcaw', $caw->caw_code)
|
|
->pluck('norujukan');
|
|
|
|
$gadaiAll = Gadai::on($caw->caw_code)
|
|
->whereIn('norujukan', $arraynorujukan)
|
|
->orderBy('nokp', 'asc')
|
|
->orderBy('norujukan', 'asc')
|
|
->get();
|
|
|
|
$customers = Customer::whereIn('kpbaru', $gadaiAll->pluck('nokp'))
|
|
->orWhereIn('kplama', $gadaiAll->pluck('nokp'))
|
|
->select('id', 'kpbaru', 'kplama', 'nama', 'telefon')
|
|
->get();
|
|
|
|
$customersByKp = $customers->keyBy(function ($item) {
|
|
if ($item->kpbaru != '') {
|
|
return $item->kpbaru;
|
|
}
|
|
if ($item->kplama != '') {
|
|
return $item->kplama;
|
|
}
|
|
});
|
|
|
|
$tarikhLelong = Carbon::parse($lel->tarikh_lelong)->format('d-m-Y');
|
|
$tarikhSebelum = Carbon::parse($lel->tarikh_lelong)->subDays(3)->format('d-m-Y');
|
|
$template = config('whatsapp.jana_notis_template');
|
|
$lang = config('whatsapp.jana_notis_template_lang');
|
|
|
|
foreach ($gadaiAll as $suratnotis) {
|
|
if (!isset($customersByKp[$suratnotis->nokp])) {
|
|
continue;
|
|
}
|
|
|
|
$customer = $customersByKp[$suratnotis->nokp];
|
|
$recipient = $whatsapp->resolveRecipient($customer->telefon);
|
|
if (!$recipient) {
|
|
Log::warning('WhatsappNotisLelongFasa2: invalid phone', [
|
|
'nokp' => $suratnotis->nokp,
|
|
'norujukan' => $suratnotis->norujukan,
|
|
]);
|
|
continue;
|
|
}
|
|
|
|
$jumlah = $this->resolveJanaNotisJumlah($caw->caw_code, $this->lelong_id, $suratnotis);
|
|
if ($jumlah === null) {
|
|
Log::warning('WhatsappNotisLelongFasa2: skip send, jumlah unavailable', [
|
|
'norujukan' => $suratnotis->norujukan,
|
|
]);
|
|
continue;
|
|
}
|
|
|
|
$bodyParams = [
|
|
$customer->nama,
|
|
$suratnotis->nokp,
|
|
$suratnotis->norujukan,
|
|
$caw->caw_name,
|
|
$tarikhLelong,
|
|
$tarikhSebelum,
|
|
$jumlah,
|
|
];
|
|
|
|
$whatsapp->sendTemplate($recipient, $template, $bodyParams, $lang);
|
|
|
|
usleep(300000);
|
|
}
|
|
}
|
|
}
|