74 lines
3.1 KiB
PHP
74 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\ShorakaFinancing;
|
|
use App\Services\ShorakaFinancingService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Throwable;
|
|
|
|
class ShorakaController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$records = ShorakaFinancing::where('kodcaw', Auth::user()->cawangan)
|
|
->orderByDesc('id')->limit(100)->get();
|
|
$cawangan_info = ['details_caw' => Auth::user()->cawangan, 'kodcaw' => Auth::user()->cawangan];
|
|
return view('shoraka.index', compact('records', 'cawangan_info'));
|
|
}
|
|
|
|
public function show(string $norujukan, ShorakaFinancingService $service)
|
|
{
|
|
$record = $service->local($norujukan);
|
|
$data = null;
|
|
$error = null;
|
|
try {
|
|
$data = $service->api()->get($service->url($norujukan))->throw()->json()['data'];
|
|
} catch (Throwable $exception) {
|
|
$error = 'Maklumat Shoraka tidak dapat diambil. Bayaran dan SAG masih ditahan.';
|
|
}
|
|
$cawangan_info = ['details_caw' => Auth::user()->cawangan, 'kodcaw' => Auth::user()->cawangan];
|
|
return view('shoraka.show', compact('norujukan', 'record', 'data', 'error', 'cawangan_info'));
|
|
}
|
|
|
|
public function submit(string $norujukan, ShorakaFinancingService $service)
|
|
{
|
|
$record = $service->local($norujukan);
|
|
abort_unless($record && $record->teller === Auth::user()->name, 403);
|
|
$response = $service->api()->post($service->url($norujukan, '/submit'));
|
|
return redirect()->route('shoraka.show', compact('norujukan'))
|
|
->with('shoraka_message', $response->successful()
|
|
? 'Order dihantar. Semak status sebelum menyelesaikan pembiayaan.'
|
|
: (($response->json()['message'] ?? null) ?: 'Order belum berjaya. Semak status sebelum cuba semula.'));
|
|
}
|
|
|
|
public function refresh(string $norujukan, ShorakaFinancingService $service)
|
|
{
|
|
try {
|
|
$response = $service->api()->post($service->url($norujukan, '/refresh'));
|
|
$message = $response->successful() ? 'Status dikemas kini.'
|
|
: (($response->json()['message'] ?? null) ?: 'Status tidak dapat dikemas kini.');
|
|
} catch (Throwable $exception) {
|
|
$message = 'Sambungan terganggu. Jangan hantar order semula.';
|
|
}
|
|
return redirect()->route('shoraka.show', compact('norujukan'))->with('shoraka_message', $message);
|
|
}
|
|
|
|
public function finish(Request $request, string $norujukan, ShorakaFinancingService $service)
|
|
{
|
|
return $service->finish($request, $norujukan);
|
|
}
|
|
|
|
public function certificate(string $norujukan, ShorakaFinancingService $service)
|
|
{
|
|
$response = $service->api()->get($service->url($norujukan, '/certificate'))->throw();
|
|
abort_unless(substr($response->body(), 0, 5) === '%PDF-', 502);
|
|
return response($response->body(), 200, [
|
|
'Content-Type' => 'application/pdf',
|
|
'Content-Disposition' => 'attachment; filename="sijil-shoraka.pdf"',
|
|
'Cache-Control' => 'private, no-store',
|
|
]);
|
|
}
|
|
}
|