DONE: use laravel horizon and redis for jobs queue, use queue when generate notis lelong, remove duplicated home file

This commit is contained in:
2026-10-07 12:10:50 +08:00
parent 09b1c12b21
commit 14d793e0f4
37 changed files with 2040 additions and 2252 deletions
+71 -184
View File
@@ -2,8 +2,10 @@
namespace App\Http\Controllers;
use App\Jobs\NotifyJanaNotis;
use App\Jobs\GenerateJanaNotisPdf;
use App\Models\JanaNotisExport;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Illuminate\Support\Facades\Auth;
@@ -81,7 +83,22 @@ class NotisController extends Controller
}
return view('jananotis.notislelong',compact('lelongid'));
$latestExports = JanaNotisExport::where('kodcaw', $user->kodcaw)
->whereIn('lelong_id', $lelongid->pluck('lelong_id'))
->orderByDesc('id')
->get()
->unique(function ($export) {
return $export->lelong_id . '_' . $export->fasa;
})
->keyBy(function ($export) {
return $export->lelong_id . '_' . $export->fasa;
});
$hasPendingExport = $latestExports->contains(function ($export) {
return $export->isInProgress();
});
return view('jananotis.notislelong', compact('lelongid', 'latestExports', 'hasPendingExport'));
}
public function senarainotis($lelong_id){
@@ -332,199 +349,69 @@ class NotisController extends Controller
public function jananotis2_new($lelong_id)
{
// focus only on variable: $marhuns, $lelongid, $caw, $tarikh_cetak
// variable not use: $lelong_id, $customer, $listsag
$user = Auth::user();
$caw = Cawangan::where('caw_code', '=', $user->kodcaw)->first();
$currentdate = Carbon::now();
$tarikh_cetak = $currentdate->toDateString();
$current_date_time = $currentdate->toDateTimeString();
Log::info("Jana notis fasa 1: " . $user->kodcaw . ", " . $user->name);
NotifyJanaNotis::dispatch($user->id, $user->kodcaw, $user->name, 'Jana Notis Fasa 1', $current_date_time);
$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();
$marhuns_pluck_nokp = $marhuns->pluck('nokp');
$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;
} else if ($item->kplama != '') {
return $item->kplama;
}
});
foreach ($marhuns as $index => $marhun) {
if ($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();
//jika ambil data dari api
$tagbaru = $marhun->tagbaru;
// dd($tagbaru);
if($tagbaru == '0'){
$getkeuntunganhinggatarikhlelong = Http::get(config('app.api_ujrah_url'). 'get/upahtawarruqbytarikh',[
'kodcaw' => $user->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' => $user->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;
}
// dd($getkeuntunganhinggatarikhlelong);
}
// return view('pdf.jananotis', compact('marhuns', 'lelongid', 'caw', 'tarikh_cetak'));
$pdf = PDF::loadView('pdf.jananotis_new', compact('marhuns', 'lelongid', 'caw', 'tarikh_cetak'))
->setPaper('a4', 'potrait');
return $pdf->download('Penjanaan Notis Fasa 1:Batch Lelong ' . $lelong_id . '.pdf');
return $this->queueJanaNotisExport($lelong_id, 1);
}
public function jananotis2_new2($lelong_id)
{
// focus only on variable: $marhuns, $lelongid, $caw, $tarikh_cetak
// variable not use: $lelong_id, $customer, $listsag
return $this->queueJanaNotisExport($lelong_id, 2);
}
$user = Auth::user();
protected function queueJanaNotisExport($lelong_id, $fasa)
{
$user = Auth::user();
$caw = Cawangan::where('caw_code', '=', $user->kodcaw)->first();
$existing = JanaNotisExport::where('lelong_id', $lelong_id)
->where('kodcaw', $user->kodcaw)
->where('fasa', $fasa)
->whereIn('status', [
JanaNotisExport::STATUS_PENDING,
JanaNotisExport::STATUS_PROCESSING,
JanaNotisExport::STATUS_COMPLETED,
])
->latest('id')
->first();
$currentdate = Carbon::now();
$tarikh_cetak = $currentdate->toDateString();
$current_date_time = $currentdate->toDateTimeString();
Log::info("Jana notis fasa 1: " . $user->kodcaw . ", " . $user->name);
NotifyJanaNotis::dispatch($user->id, $user->kodcaw, $user->name, 'Jana Notis Fasa 1', $current_date_time);
$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();
$marhuns_pluck_nokp = $marhuns->pluck('nokp');
$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;
} else if ($item->kplama != '') {
return $item->kplama;
}
});
foreach ($marhuns as $index => $marhun) {
if ($customers_keyBy_noKp[$marhun->nokp]) {
$marhuns[$index]['nama'] = $customers_keyBy_noKp[$marhun->nokp];
if ($existing) {
if ($existing->isReady()) {
return redirect()->route('jananotis.notislelong')
->with('success', 'Notis Fasa ' . $fasa . ' untuk batch ' . $lelong_id . ' telah dijana. Sila muat turun fail sedia ada.');
}
$getdatalelongbid = LelongBid::where('lelong_id', $lelong_id)->where('norujukan', $marhun->norujukan)->first();
//jika ambil data dari api
$tagbaru = $marhun->tagbaru;
// dd($tagbaru);
if($tagbaru == '0'){
$getkeuntunganhinggatarikhlelong = Http::get(config('app.api_ujrah_url'). 'get/upahtawarruqbytarikh',[
'kodcaw' => $user->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' => $user->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;
}
// dd($getkeuntunganhinggatarikhlelong);
return redirect()->route('jananotis.notislelong')
->with('success', 'Penjanaan Notis Fasa ' . $fasa . ' untuk batch ' . $lelong_id . ' masih dalam proses.');
}
// return view('pdf.jananotis', compact('marhuns', 'lelongid', 'caw', 'tarikh_cetak'));
$pdf = PDF::loadView('pdf.jananotisfasa2_new', compact('marhuns', 'lelongid', 'caw', 'tarikh_cetak'))
->setPaper('a4', 'potrait');
return $pdf->download('Penjanaan Notis Fasa 2:Batch Lelong ' . $lelong_id . '.pdf');
$export = JanaNotisExport::create([
'user_id' => $user->id,
'lelong_id' => $lelong_id,
'kodcaw' => $user->kodcaw,
'fasa' => $fasa,
'status' => JanaNotisExport::STATUS_PENDING,
]);
GenerateJanaNotisPdf::dispatch($export->id);
Log::info("Queued Jana notis fasa {$fasa}: {$user->kodcaw}, {$user->name}, export #{$export->id}");
return redirect()->route('jananotis.notislelong')
->with('success', 'Penjanaan Notis Fasa ' . $fasa . ' untuk batch ' . $lelong_id . ' telah dimulakan. Sila tunggu dan muat turun apabila siap.');
}
public function downloadJanaNotisExport($export_id)
{
$user = Auth::user();
$export = JanaNotisExport::where('id', $export_id)
->where('kodcaw', $user->kodcaw)
->firstOrFail();
if (!$export->isReady() || !Storage::disk('local')->exists($export->file_path)) {
return redirect()->route('jananotis.notislelong')
->with('error', 'Fail PDF belum siap atau tidak dijumpai. Sila jana semula.');
}
return Storage::disk('local')->download($export->file_path, $export->file_name);
}