Telah dilakukan pembersihan pada kode dengan menghapus logika pengolahan file dan pembuatan histori setelah proses update permohonan. Perubahan ini diharapkan menyederhanakan fungsi update di dalam PermohonanController serta mengurangi beban pemrosesan yang tidak diperlukan.
357 lines
14 KiB
PHP
357 lines
14 KiB
PHP
<?php
|
|
|
|
namespace Modules\Lpj\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Barryvdh\DomPDF\Facade\Pdf;
|
|
use Exception;
|
|
use Illuminate\Http\Request;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use Modules\Location\Models\City;
|
|
use Modules\Location\Models\District;
|
|
use Modules\Location\Models\Province;
|
|
use Modules\Location\Models\Village;
|
|
use Modules\Lpj\Exports\PermohonanExport;
|
|
use Modules\Lpj\Http\Requests\PermohonanRequest;
|
|
use Modules\Lpj\Models\Branch;
|
|
use Modules\Lpj\Models\Debiture;
|
|
use Modules\Lpj\Models\DokumenJaminan;
|
|
use Modules\Lpj\Models\JenisFasilitasKredit;
|
|
use Modules\Lpj\Models\NilaiPlafond;
|
|
use Modules\Lpj\Models\Permohonan;
|
|
use Modules\Lpj\Models\StatusPermohonan;
|
|
use Modules\Lpj\Models\TujuanPenilaian;
|
|
use Modules\Lpj\Services\PermohonanHistoryService;
|
|
|
|
class PermohonanController extends Controller
|
|
{
|
|
public $user;
|
|
protected $historyService;
|
|
|
|
public function __construct(PermohonanHistoryService $historyService)
|
|
{
|
|
$this->historyService = $historyService;
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
return view('lpj::permohonan.index');
|
|
}
|
|
|
|
public function store(PermohonanRequest $request)
|
|
{
|
|
$validate = $request->validated();
|
|
if ($validate) {
|
|
try {
|
|
// Process file upload
|
|
$filePath = null;
|
|
if ($request->hasFile('attachment')) {
|
|
$file = $request->file('attachment');
|
|
$fileName = time() . '_' . $file->getClientOriginalName();
|
|
$filePath = $file->storeAs('permohonan_attachments', $fileName, 'public');
|
|
}
|
|
|
|
// Get keterangan if provided
|
|
$keterangan = $request->input('keterangan') ?? null;
|
|
|
|
|
|
// Save to database
|
|
$permohonan = Permohonan::create($validate);
|
|
|
|
// Create history
|
|
$this->historyService->createHistory(
|
|
$permohonan,
|
|
$validate['status'],
|
|
$keterangan,
|
|
[], // beforeRequest is empty for new permohonan
|
|
$permohonan->toArray(),
|
|
$filePath
|
|
);
|
|
return redirect()
|
|
->route('permohonan.index')->with('success', 'Permohonan created successfully');
|
|
} catch (Exception $e) {
|
|
return redirect()
|
|
->route('permohonan.create')->with('error', 'Failed to create permohonan' . $e->getMessage());
|
|
}
|
|
} else {
|
|
return redirect()
|
|
->route('permohonan.create')->with('success', 'error naon iye')->withInput();
|
|
}
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return view('lpj::permohonan.create');
|
|
}
|
|
|
|
public function createPermohonan($debitur)
|
|
{
|
|
$branches = Branch::all();
|
|
$debitur = Debiture::find($debitur);
|
|
$tujuanPenilaian = TujuanPenilaian::all();
|
|
$status = StatusPermohonan::all();
|
|
$fasilitasKredit = JenisFasilitasKredit::all();
|
|
$plafond = NilaiPlafond::all();
|
|
|
|
return view(
|
|
'lpj::permohonan.form',
|
|
compact('branches', 'debitur', 'tujuanPenilaian', 'status', 'fasilitasKredit', 'plafond'),
|
|
);
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$permohonan = Permohonan::find($id);
|
|
$branches = Branch::all();
|
|
$debitur = Debiture::find($permohonan->debiture_id);
|
|
$tujuanPenilaian = TujuanPenilaian::all();
|
|
$status = StatusPermohonan::all();
|
|
$provinces = Province::all();
|
|
$cities = City::where('province_code', $debitur->province_code)->get();
|
|
$districts = District::where('city_code', $debitur->city_code)->get();
|
|
$villages = Village::where('district_code', $debitur->district_code)->get();
|
|
$documents = DokumenJaminan::with('pemilik', 'detail')->where('debiture_id', $id)->get();
|
|
|
|
$fasilitasKredit = JenisFasilitasKredit::all();
|
|
$plafond = NilaiPlafond::all();
|
|
|
|
return view(
|
|
'lpj::permohonan.form',
|
|
compact(
|
|
'permohonan',
|
|
'branches',
|
|
'debitur',
|
|
'tujuanPenilaian',
|
|
'status',
|
|
'provinces',
|
|
'cities',
|
|
'districts',
|
|
'villages',
|
|
'documents',
|
|
'fasilitasKredit',
|
|
'plafond',
|
|
),
|
|
);
|
|
}
|
|
|
|
public function update(PermohonanRequest $request, $id)
|
|
{
|
|
$permohonan = Permohonan::findOrFail($id);
|
|
$beforeRequest = $permohonan->toArray();
|
|
|
|
$validate = $request->validated();
|
|
if ($validate) {
|
|
try {
|
|
// Update in database
|
|
|
|
if ($permohonan->status == 'revisi') {
|
|
$validate['status'] = 'order';
|
|
}
|
|
$permohonan->update($validate);
|
|
|
|
return redirect()
|
|
->route('permohonan.index')->with('success', 'Permohonan updated successfully');
|
|
} catch (Exception $e) {
|
|
return redirect()
|
|
->route('permohonan.edit', $id)->with('error', 'Failed to update permohonan');
|
|
}
|
|
}
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
// Delete from database
|
|
$permohonan = Permohonan::find($id);
|
|
$permohonan->delete();
|
|
|
|
echo json_encode(['success' => true, 'message' => 'Permohonan deleted successfully']);
|
|
} catch (Exception $e) {
|
|
echo json_encode(['success' => false, 'message' => 'Failed to delete permohonan']);
|
|
}
|
|
}
|
|
|
|
public function dataForDatatables(Request $request)
|
|
{
|
|
if (is_null($this->user) || !$this->user->can('debitur.view')) {
|
|
//abort(403, 'Sorry! You are not allowed to view users.');
|
|
}
|
|
|
|
// Retrieve data from the database
|
|
$query = Permohonan::query();
|
|
|
|
// Apply search filter if provided
|
|
if ($request->has('search') && !empty($request->get('search'))) {
|
|
$search = $request->get('search');
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('nomor_registrasi', 'LIKE', '%' . $search . '%');
|
|
$q->orWhere('tanggal_permohonan', 'LIKE', '%' . $search . '%');
|
|
$q->orWhereRelation('user', 'name', 'LIKE', '%' . $search . '%');
|
|
$q->orWhereRelation('debiture', 'name', 'LIKE', '%' . $search . '%');
|
|
$q->orWhereRelation('tujuanPenilaian', 'name', 'LIKE', '%' . $search . '%');
|
|
$q->orWhereRelation('branch', 'name', 'LIKE', '%' . $search . '%');
|
|
$q->orWhere('status', 'LIKE', '%' . $search . '%');
|
|
});
|
|
}
|
|
|
|
// Apply sorting if provided
|
|
if ($request->has('sortOrder') && !empty($request->get('sortOrder'))) {
|
|
$order = $request->get('sortOrder');
|
|
$column = $request->get('sortField');
|
|
$query->orderBy($column, $order);
|
|
}
|
|
|
|
// Get the total count of records
|
|
$totalRecords = $query->count();
|
|
$size = $request->get('size', 10);
|
|
if ($size == 0) {
|
|
$size = 10;
|
|
}
|
|
|
|
// Apply pagination if provided
|
|
if ($request->has('page') && $request->has('size')) {
|
|
$page = $request->get('page');
|
|
$size = $request->get('size');
|
|
$offset = ($page - 1) * $size; // Calculate the offset
|
|
|
|
$query->skip($offset)->take($size);
|
|
}
|
|
|
|
// Get the filtered count of records
|
|
$filteredRecords = $query->count();
|
|
|
|
// Get the data for the current page
|
|
$data = $query->with(['user', 'debiture', 'branch', 'tujuanPenilaian','penilaian'])->get();
|
|
|
|
// Calculate the page count
|
|
$pageCount = ceil($totalRecords / $size);
|
|
|
|
// Calculate the current page number
|
|
$currentPage = max(1, $request->get('page', 1));
|
|
|
|
// Return the response data as a JSON object
|
|
return response()->json([
|
|
'draw' => $request->get('draw'),
|
|
'recordsTotal' => $totalRecords,
|
|
'recordsFiltered' => $filteredRecords,
|
|
'pageCount' => $pageCount,
|
|
'page' => $currentPage,
|
|
'totalCount' => $totalRecords,
|
|
'data' => $data,
|
|
]);
|
|
}
|
|
|
|
public function export()
|
|
{
|
|
return Excel::download(new PermohonanExport, 'permohonan.xlsx');
|
|
}
|
|
|
|
public function authorization()
|
|
{
|
|
return view('lpj::permohonan.authorization.index');
|
|
}
|
|
|
|
public function dataForAuthorization(Request $request)
|
|
{
|
|
if (is_null($this->user) || !$this->user->can('debitur.view')) {
|
|
//abort(403, 'Sorry! You are not allowed to view users.');
|
|
}
|
|
|
|
// Retrieve data from the database
|
|
$query = Permohonan::query();
|
|
|
|
// Apply search filter if provided
|
|
if ($request->has('search') && !empty($request->get('search'))) {
|
|
$search = $request->get('search');
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('nomor_registrasi', 'LIKE', '%' . $search . '%');
|
|
$q->orWhere('tanggal_permohonan', 'LIKE', '%' . $search . '%');
|
|
$q->orWhereRelation('user', 'name', 'LIKE', '%' . $search . '%');
|
|
$q->orWhereRelation('debiture', 'name', 'LIKE', '%' . $search . '%');
|
|
$q->orWhereRelation('tujuanPenilaian', 'name', 'LIKE', '%' . $search . '%');
|
|
$q->orWhereRelation('branch', 'name', 'LIKE', '%' . $search . '%');
|
|
$q->orWhere('status', 'LIKE', '%' . $search . '%');
|
|
});
|
|
}
|
|
|
|
// Apply sorting if provided
|
|
if ($request->has('sortOrder') && !empty($request->get('sortOrder'))) {
|
|
$order = $request->get('sortOrder');
|
|
$column = $request->get('sortField');
|
|
$query->orderBy($column, $order);
|
|
}
|
|
|
|
// Get the total count of records
|
|
$totalRecords = $query->count();
|
|
|
|
// Apply pagination if provided
|
|
if ($request->has('page') && $request->has('size')) {
|
|
$page = $request->get('page');
|
|
$size = $request->get('size');
|
|
$offset = ($page - 1) * $size; // Calculate the offset
|
|
|
|
$query->skip($offset)->take($size);
|
|
}
|
|
|
|
// Get the filtered count of records
|
|
$filteredRecords = $query->count();
|
|
|
|
// Get the data for the current page
|
|
$data = $query->with(['user', 'debiture', 'branch', 'tujuanPenilaian'])->where('status', '=', 'order')->get(
|
|
);
|
|
|
|
// Calculate the page count
|
|
$pageCount = ceil($totalRecords / $request->get('size'));
|
|
|
|
// Calculate the current page number
|
|
$currentPage = 0 + 1;
|
|
|
|
// Return the response data as a JSON object
|
|
return response()->json([
|
|
'draw' => $request->get('draw'),
|
|
'recordsTotal' => $totalRecords,
|
|
'recordsFiltered' => $filteredRecords,
|
|
'pageCount' => $pageCount,
|
|
'page' => $currentPage,
|
|
'totalCount' => $totalRecords,
|
|
'data' => $data,
|
|
]);
|
|
}
|
|
|
|
public function showAuthorization($id)
|
|
{
|
|
$permohonan = Permohonan::find($id);
|
|
return view('lpj::permohonan.authorization.show', compact('permohonan'));
|
|
}
|
|
|
|
public function updateAuthorization(Request $request, $id)
|
|
{
|
|
try {
|
|
$permohonan = Permohonan::find($id);
|
|
$permohonan->status = $request->status;
|
|
$permohonan->keterangan = $request->keterangan;
|
|
$permohonan->save();
|
|
} catch (Exception $e) {
|
|
return redirect()->route('authorization.show', $id)->with('error', 'Failed to update permohonan');
|
|
}
|
|
|
|
return redirect()->route('authorization.index')->with('success', 'Permohonan updated successfully');
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$permohonan = Permohonan::find($id);
|
|
|
|
return view('lpj::permohonan.show', compact('permohonan'));
|
|
}
|
|
|
|
public function print($id)
|
|
{
|
|
$permohonan = Permohonan::find($id);
|
|
return view('lpj::permohonan.print', compact('permohonan'));
|
|
|
|
// $pdf = Pdf::loadView('lpj::permohonan.print', compact('permohonan'));
|
|
// return $pdf->stream();
|
|
}
|
|
}
|