Files
lelong/app/Jobs/GenerateJanaNotisPdf.php
T

180 lines
6.6 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\Cawangan;
use App\Models\Customer;
use App\Models\Gadai;
use App\Models\JanaNotisExport;
use App\Models\Lelong;
use App\Models\LelongBid;
use App\Models\User;
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\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use PDF;
use Throwable;
class GenerateJanaNotisPdf implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $timeout = 600;
public $tries = 1;
protected $exportId;
public function __construct($exportId)
{
$this->exportId = $exportId;
$this->onQueue('jana-notis');
}
public function handle()
{
ini_set('memory_limit', '2048M');
$export = JanaNotisExport::find($this->exportId);
if (! $export) {
return;
}
$export->update([
'status' => JanaNotisExport::STATUS_PROCESSING,
'error_message' => null,
]);
$user = User::find($export->user_id);
$caw = Cawangan::where('caw_code', $export->kodcaw)->first();
$lelong_id = $export->lelong_id;
$fasa = (int) $export->fasa;
Log::info("Jana notis fasa {$fasa} (queued): {$export->kodcaw}, ".($user->name ?? 'unknown'));
$tarikh_cetak = Carbon::now()->toDateString();
$lelongid = Lelong::find($lelong_id);
$arraynorujukan = LelongBid::where('lelong_id', $lelong_id)
->where('kodcaw', $caw->caw_code)
->pluck('norujukan');
$marhuns = Gadai::on($caw->caw_code)
->whereIn('norujukan', $arraynorujukan)
->select('nokp', 'norujukan', 'bakipjm', 'tagbaru')
->orderBy('nokp', 'asc')
->get();
$customers = Customer::whereIn('kpbaru', $marhuns->pluck('nokp'))
->orWhereIn('kplama', $marhuns->pluck('nokp'))
->join('poskod', 'customer.poskod', '=', 'poskod.poskod')
->join('daerah', 'poskod.koddaerah', '=', 'daerah.koddaerah')
->join('negeri', 'poskod.kodnegeri', '=', 'negeri.kodnegeri')
->get();
$customers_keyBy_noKp = $customers->keyBy(function ($item) {
if ($item->kpbaru != '') {
return $item->kpbaru;
}
if ($item->kplama != '') {
return $item->kplama;
}
});
foreach ($marhuns as $index => $marhun) {
if (isset($customers_keyBy_noKp[$marhun->nokp])) {
$marhuns[$index]['nama'] = $customers_keyBy_noKp[$marhun->nokp];
}
$getdatalelongbid = LelongBid::where('lelong_id', $lelong_id)
->where('norujukan', $marhun->norujukan)
->first();
$tagbaru = $marhun->tagbaru;
if ($tagbaru == '0') {
$getkeuntunganhinggatarikhlelong = Http::get(config('app.api_ujrah_url').'get/upahtawarruqbytarikh', [
'kodcaw' => $export->kodcaw,
'norujukan' => $marhun->norujukan,
'tarikh' => $getdatalelongbid['t_lelong'],
'jeniskeuntungan' => 'asal',
])->json();
$nilaitebus = (float) $getkeuntunganhinggatarikhlelong['harga_tebus'];
$nilaipembiayaan = (float) $getdatalelongbid['pinjaman'];
$nilaikeuntungan = (float) $getkeuntunganhinggatarikhlelong['upahsemasa'];
$jumlah = $nilaipembiayaan + $nilaikeuntungan;
$marhuns[$index]['nilaitebus'] = $nilaitebus;
$marhuns[$index]['nilaipembiayaan'] = $nilaipembiayaan;
$marhuns[$index]['nilaikeuntungan'] = $nilaikeuntungan;
$marhuns[$index]['jumlah'] = $jumlah;
} elseif ($tagbaru == '1') {
$getkeuntunganhinggatarikhlelong = Http::get(config('app.api_ujrah_url').'get/upahtawarruqbytarikh', [
'kodcaw' => $export->kodcaw,
'norujukan' => $marhun->norujukan,
'tarikh' => $getdatalelongbid['t_lelong'],
'jeniskeuntungan' => 'onepercent',
])->json();
$nilaitebus = (float) $getkeuntunganhinggatarikhlelong['harga_tebus'];
$nilaipembiayaan = (float) $getdatalelongbid['pinjaman'];
$nilaipendapatan = (float) $getkeuntunganhinggatarikhlelong['pendapatan'];
$nilaikeuntungan = (float) $getkeuntunganhinggatarikhlelong['keuntungan'];
$nilaiujrah = (float) $getkeuntunganhinggatarikhlelong['ujrah'];
$jumlah = $nilaipembiayaan + $nilaipendapatan;
$marhuns[$index]['nilaitebus'] = $nilaitebus;
$marhuns[$index]['nilaipembiayaan'] = $nilaipembiayaan;
$marhuns[$index]['nilaikeuntungan'] = $nilaikeuntungan;
$marhuns[$index]['nilaiujrah'] = $nilaiujrah;
$marhuns[$index]['jumlah'] = $jumlah;
}
}
$view = $fasa === 2 ? 'pdf.jananotisfasa2_new' : 'pdf.jananotis_new';
$fileName = 'Penjanaan Notis Fasa '.$fasa.':Batch Lelong '.$lelong_id.'.pdf';
$relativePath = 'jana_notis/'.$export->kodcaw.'/'.$lelong_id.'_fasa'.$fasa.'_'.$export->id.'.pdf';
$pdf = PDF::loadView($view, compact('marhuns', 'lelongid', 'caw', 'tarikh_cetak'))
->setPaper('a4', 'potrait');
Storage::disk('local')->put($relativePath, $pdf->output());
$export->update([
'status' => JanaNotisExport::STATUS_COMPLETED,
'file_path' => $relativePath,
'file_name' => $fileName,
'error_message' => null,
]);
if ($fasa === 2) {
WhatsappNotisLelongFasa2::dispatch($export->user_id, $export->kodcaw, $export->lelong_id);
} else {
WhatsappNotisLelongFasa1::dispatch($export->user_id, $export->kodcaw, $export->lelong_id);
}
Log::info("Queued WhatsApp Jana Notis fasa {$fasa} for lelong {$export->lelong_id}");
}
public function failed(Throwable $exception)
{
$export = JanaNotisExport::find($this->exportId);
if ($export) {
$export->update([
'status' => JanaNotisExport::STATUS_FAILED,
'error_message' => $exception->getMessage(),
]);
}
Log::error('GenerateJanaNotisPdf failed: '.$exception->getMessage(), [
'export_id' => $this->exportId,
]);
}
}