diff --git a/app/Http/Controllers/PembatalanController.php b/app/Http/Controllers/PembatalanController.php index 7e735df..44f567d 100644 --- a/app/Http/Controllers/PembatalanController.php +++ b/app/Http/Controllers/PembatalanController.php @@ -1,153 +1,160 @@ historyService = $historyService; + } - public function __construct(PermohonanHistoryService $historyService) - { - $this->historyService = $historyService; - } + public function index() + { + $user = auth()->user(); + return view('lpj::pembatalan.index', compact('user')); + } - public function index() - { - return view('lpj::pembatalan.index'); - } + public function edit($id) + { + $user = auth()->user(); + $pembatalan = PermohonanPembatalan::with(['permohonan.debiture', 'permohonan.branch'])->find($id); - public function edit($id) - { - $pembatalan = PermohonanPembatalan::with(['permohonan.debiture','permohonan.branch'])->find($id); + return view( + 'lpj::pembatalan.form', + compact( + 'pembatalan', + 'user' + ), + ); + } - return view( - 'lpj::pembatalan.form', - compact( - 'pembatalan' - ), - ); - } + public function update(Request $request, $id) + { + $user = auth()->user(); + $pembatalan = PermohonanPembatalan::findOrFail($id); + $permohonan = Permohonan::find($pembatalan->permohonan_id); + $beforeRequest = $permohonan->toArray(); - public function update(Request $request, $id) - { - $pembatalan = PermohonanPembatalan::findOrFail($id); - $permohonan = Permohonan::find($pembatalan->permohonan_id); - $beforeRequest = $permohonan->toArray(); + try { + // Update Permohonan Pembatalan + if ($request->status == 'approved') { + $pembatalan->status = 'approved'; + $pembatalan->authorized_at = now(); + $pembatalan->authorized_by = $user->id; + $pembatalan->authorized_status = 1; + $pembatalan->save(); - try { - // Update Permohonan Pembatalan - if($request->status=='approved'){ - $pembatalan->status = 'approved'; - $pembatalan->authorized_at = now(); - $pembatalan->authorized_by = auth()->user()->id; - $pembatalan->authorized_status = 1; - $pembatalan->save(); - - $permohonan->status = 'batal'; - $permohonan->save(); - } else{ - $pembatalan->status = 'rejected'; - $pembatalan->authorized_at = now(); - $pembatalan->authorized_by = auth()->user()->id; - $pembatalan->authorized_status = 3; - $pembatalan->save(); - } - - return redirect() - ->route('pembatalan.index')->with('success', 'Permohonan Pembatalan updated successfully'); - } catch (Exception $e) { - return redirect() - ->route('pembatalan.edit', $id)->with('error', 'Failed to update permohonan Pembatalan'); - } - } - - 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.'); + $permohonan->status = 'batal'; + $permohonan->save(); + } else { + $pembatalan->status = 'rejected'; + $pembatalan->authorized_at = now(); + $pembatalan->authorized_by = $user->id; + $pembatalan->authorized_status = 3; + $pembatalan->save(); } - // Retrieve data from the database - $query = PermohonanPembatalan::query(); - $query = $query->orderBy('created_at', 'desc'); - // Apply search filter if provided - if ($request->has('search') && !empty($request->get('search'))) { - $search = $request->get('search'); - $query->where(function ($q) use ($search) { - $q->orWhereRelation('permohonan', 'nomor_registrasi', 'LIKE', '%' . $search . '%'); - $q->orWhereRelation('permohonan.debiture', 'name', 'LIKE', '%' . $search . '%'); - $q->orWhere('alasan_pembatalan', '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(['permohonan.debiture','permohonan.branch','creator'])->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, - ]); + return redirect() + ->route('pembatalan.index')->with('success', 'Permohonan Pembatalan updated successfully'); + } catch (Exception $e) { + return redirect() + ->route('pembatalan.edit', $id)->with('error', 'Failed to update permohonan Pembatalan'); } } + + public function dataForDatatables(Request $request) + { + $user = auth()->user(); + if (is_null($user) || !$user->can('debitur.view')) { + //abort(403, 'Sorry! You are not allowed to view users.'); + } + + // Retrieve data from the database + $query = PermohonanPembatalan::query()->with(['permohonan']); + $query = $query->orderBy('created_at', 'desc'); + // Apply search filter if provided + if ($request->has('search') && !empty($request->get('search'))) { + $search = $request->get('search'); + $query->where(function ($q) use ($search) { + $q->orWhereRelation('permohonan', 'nomor_registrasi', 'LIKE', '%' . $search . '%'); + $q->orWhereRelation('permohonan.debiture', 'name', 'LIKE', '%' . $search . '%'); + $q->orWhere('alasan_pembatalan', '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'); + // Only allow sorting by existing columns + if (in_array($column, ['created_at', 'status', 'alasan_pembatalan'])) { + $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(['permohonan.debiture', 'permohonan.branch', 'creator'])->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, + ]); + } +} diff --git a/app/Http/Controllers/PermohonanController.php b/app/Http/Controllers/PermohonanController.php index da92c4a..5b6c423 100644 --- a/app/Http/Controllers/PermohonanController.php +++ b/app/Http/Controllers/PermohonanController.php @@ -1,518 +1,543 @@ historyService = $historyService; + } - public function __construct(PermohonanHistoryService $historyService) - { - $this->historyService = $historyService; - } + public function index() + { + return view('lpj::permohonan.index'); + } - 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, - ); - - $documents = DokumenJaminan::where('permohonan_id', $permohonan->id)->get(); - if (count($documents) < 1) { - return redirect()->route( - 'debitur.jaminan.create', - array_merge(['permohonan_id' => $permohonan->id], ['id' => $permohonan->debiture->id]), - )->with('success', 'Permohonan created successfully, Lengkapi data jaminan terlebih dahulu'); - } - 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()); + 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, + ); + + $documents = DokumenJaminan::where('permohonan_id', $permohonan->id)->get(); + if (count($documents) < 1) { + return redirect()->route( + 'debitur.jaminan.create', + array_merge(['permohonan_id' => $permohonan->id], ['id' => $permohonan->debiture->id]), + )->with('success', 'Permohonan created successfully, Lengkapi data jaminan terlebih dahulu'); } - } 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 destroy($id) - { - try { - // Delete from database - $permohonan = Permohonan::find($id); - $permohonan->delete(); - - echo json_encode(['success' => true, 'message' => 'Permohonan deleted successfully']); + ->route('permohonan.index')->with('success', 'Permohonan created successfully'); } catch (Exception $e) { - echo json_encode(['success' => false, 'message' => 'Failed to delete permohonan']); + 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 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.'); } - 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(); - // Retrieve data from the database - $query = Permohonan::query(); + if (Auth::check() && !empty(Auth::user()->branch_id)) { + $query = $query->where('branch_id', Auth::user()->branch_id); + } - if (!Auth::user()->hasAnyRole(['administrator'])) { - $query = $query->where('branch_id', Auth::user()->branch_id); - } - - $query = $query->orderBy('nomor_registrasi', 'desc'); - // 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('mig_mst_lpj_nomor_jaminan', '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->get(); - - $data = $data->map(function ($item) { - return [ - 'id' => $item->id, - 'nomor_registrasi' => $item->nomor_registrasi, - 'mig_mst_lpj_nomor_jaminan' => $item->mig_mst_lpj_nomor_jaminan, - 'tanggal_permohonan' => $item->tanggal_permohonan ? dateFormat( - $item->tanggal_permohonan, - ) : null, - 'pemohon' => $item->user->name ?? 'N/A', - 'branch' => $item->branch->name ?? 'N/A', - 'debiture' => [ - 'name' => $item->debiture->name, - ], - 'tujuan_penilaian' => [ - 'code' => $item->tujuanPenilaian->code ?? null, - 'name' => $item->tujuanPenilaian->name ?? null, - ], - 'status' => $item->status, - 'documents' => count($item->documents), - 'keterangan' => $item->keterangan, - 'penilaian' => [ - 'id' => $item->penilaian->id ?? null, - 'waktu_penilaian' => $item->penilaian->waktu_penilaian ?? null, - 'rejected_note' => $item->penilaian->rejected_note ?? null, - 'authorized_status' => $item->penilaian->authorized_status ?? null, - ], - 'registrasi_catatan' => $item->registrasi_catatan, - ]; + $query = $query->orderBy('nomor_registrasi', 'desc'); + // 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('mig_mst_lpj_nomor_jaminan', '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 . '%'); }); - - // 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'); - } + // Log sorting parameters for debugging + Log::debug('Sorting parameters', [ + 'sortField' => $request->get('sortField'), + 'sortOrder' => $request->get('sortOrder') + ]); - public function authorization() - { - return view('lpj::permohonan.authorization.index'); - } + // Apply sorting if provided + if ($request->has('sortOrder') && !empty($request->get('sortOrder'))) { + $order = $request->get('sortOrder'); + $column = $request->get('sortField'); - 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.'); - } + // Define allowed columns for sorting + $allowedColumns = [ + 'nomor_registrasi', + 'mig_mst_lpj_nomor_jaminan', + 'tanggal_permohonan', + 'status', + 'user_id', + 'branch_id' + ]; - // Retrieve data from the database - $query = Permohonan::query()->with('documents')->has('documents', '>', 0)->where('status', '=', 'order'); - - if (!Auth::user()->hasAnyRole(['administrator'])) { - $query = $query->where('branch_id', Auth::user()->branch_id); - } - - // 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'); + // Handle special cases for sorting + if ($column === 'pemohon') { + $query->orderBy('user_id', $order); + } elseif ($column === 'branch') { + $query->orderBy('branch_id', $order); + } elseif (in_array($column, $allowedColumns)) { $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'])->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')); + // Get the total count of records + $totalRecords = $query->count(); + $size = $request->get('size', 10); + if ($size == 0) { + $size = 10; } - public function updateAuthorization(Request $request, $id) - { + // 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->get(); + + $data = $data->map(function ($item) { + return [ + 'id' => $item->id, + 'nomor_registrasi' => $item->nomor_registrasi, + 'mig_mst_lpj_nomor_jaminan' => $item->mig_mst_lpj_nomor_jaminan, + 'tanggal_permohonan' => $item->tanggal_permohonan ? dateFormat( + $item->tanggal_permohonan, + ) : null, + 'pemohon' => $item->user->name ?? 'N/A', + 'branch' => $item->branch->name ?? 'N/A', + 'debiture' => [ + 'name' => $item->debiture->name, + ], + 'tujuan_penilaian' => [ + 'code' => $item->tujuanPenilaian->code ?? null, + 'name' => $item->tujuanPenilaian->name ?? null, + ], + 'status' => $item->status, + 'documents' => count($item->documents), + 'keterangan' => $item->keterangan, + 'penilaian' => [ + 'id' => $item->penilaian->id ?? null, + 'waktu_penilaian' => $item->penilaian->waktu_penilaian ?? null, + 'rejected_note' => $item->penilaian->rejected_note ?? null, + 'authorized_status' => $item->penilaian->authorized_status ?? null, + ], + 'registrasi_catatan' => $item->registrasi_catatan, + ]; + }); + + // 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()->with('documents')->has('documents', '>', 0)->where('status', '=', 'order'); + + if (Auth::check() && !empty(Auth::user()->branch_id)) { + $query = $query->where('branch_id', Auth::user()->branch_id); + } + + // 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'])->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(); + } + + public function showPembatalan($id) + { + $permohonan = Permohonan::with(['pembatalan', 'debiture'])->findOrFail($id); + return view('lpj::permohonan.pembatalan-form', compact('permohonan')); + } + + public function pembatalan(Request $request) + { + // Validate the request + $validatedData = $request->validate([ + 'permohonan_id' => 'required|exists:permohonan,id', + 'alasan_pembatalan' => 'required|string', + 'file_pembatalan' => 'required|file|mimes:pdf,doc,docx|max:2048', + ]); + + // Handle file upload + if ($request->hasFile('file_pembatalan')) { + $file = $request->file('file_pembatalan'); + $filename = time() . '_' . $file->getClientOriginalName(); + $filePath = $file->storeAs('pembatalan', $filename, 'public'); + $validatedData['file_pembatalan'] = $filePath; + } + + // Add created_by + $validatedData['created_by'] = Auth::check() ? Auth::id() : null; + + // Create new PermohonanPembatalan + $pembatalan = PermohonanPembatalan::create($validatedData); + + return redirect()->route('permohonan.index')->with('success', 'Pembatalan Permohonan Menunggu Approval'); + } + + public function storeAproved(Request $request, $id): JsonResponse + { + $data = []; + if (request()->ajax()) { 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(); - } - - public function showPembatalan($id) - { - $permohonan = Permohonan::with(['pembatalan', 'debiture'])->findOrFail($id); - return view('lpj::permohonan.pembatalan-form', compact('permohonan')); - } - - public function pembatalan(Request $request) - { - // Validate the request - $validatedData = $request->validate([ - 'permohonan_id' => 'required|exists:permohonan,id', - 'alasan_pembatalan' => 'required|string', - 'file_pembatalan' => 'required|file|mimes:pdf,doc,docx|max:2048', - ]); - - // Handle file upload - if ($request->hasFile('file_pembatalan')) { - $file = $request->file('file_pembatalan'); - $filename = time() . '_' . $file->getClientOriginalName(); - $filePath = $file->storeAs('pembatalan', $filename, 'public'); - $validatedData['file_pembatalan'] = $filePath; - } - - // Add created_by - $validatedData['created_by'] = auth()->id(); - - // Create new PermohonanPembatalan - $pembatalan = PermohonanPembatalan::create($validatedData); - - return redirect()->route('permohonan.index')->with('success', 'Pembatalan Permohonan Menunggu Approval'); - } - - public function storeAproved(Request $request, $id) - : JsonResponse { - $data = []; - if (request()->ajax()) { - try { - $penilaian = Penilaian::findOrFail($id); - $penilaian->update([ - 'authorized_status' => 1, - ]); - - $permohonan = Permohonan::findOrFail($request->permohonan_id); - - $permohonan->update([ - 'status' => 'proses-survey', - ]); - - $data['status'] = 'success'; - $data['message'] = 'Jadwal ' . $request->noReg . ' berhasil di aprove'; - } catch (Exception $e) { - $data['status'] = 'error'; - $data['message'] = 'Gagal membuat jadwal: ' . $e->getMessage(); - } - } else { - $data['status'] = 'error'; - $data['message'] = "no ajax request"; - } - - return response()->json($data); - } - - 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); - - $documents = DokumenJaminan::where('permohonan_id', $permohonan->id)->get(); - if (count($documents) < 1) { - return redirect()->route( - 'debitur.jaminan.create', - array_merge(['permohonan_id' => $permohonan->id], ['id' => $permohonan->debiture->id]), - )->with('success', 'Permohonan created successfully, Lengkapi data jaminan terlebih dahulu'); - } - - 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 storeRescheduleSurvey(Request $request, $id) - { - try { - $validatedData = $request->validate([ - 'permohonan_id' => 'required|exists:permohonan,id', - 'penilaian_id' => 'nullable', - 'nomor_registrasi' => 'required', - 'reschedule_note' => 'required', - 'reschedule_date' => 'required', - 'keterangan' => 'required', + $penilaian = Penilaian::findOrFail($id); + $penilaian->update([ + 'authorized_status' => 1, ]); - DB::beginTransaction(); - $permohonan = Permohonan::findOrFail($request->permohonan_id); + $permohonan->update([ - 'status' => 'request-reschedule', + 'status' => 'proses-survey', ]); - $penilaian = Penilaian::findOrFail($id); - - $penilaian->update([ - 'reschedule_date' => $request->reschedule_date, - 'reschedule_note' => $request->reschedule_note, - ]); - - DB::commit(); - return response()->json([ - 'status' => 'success', - 'message' => 'Proses request reschedule permohonan Nomor registrasi ' . $request->nomor_registrasi . ' berhasil', - ]); + $data['status'] = 'success'; + $data['message'] = 'Jadwal ' . $request->noReg . ' berhasil di aprove'; } catch (Exception $e) { - DB::rollBack(); - return response()->json([ - 'status' => 'error', - 'message' => 'Gagal membuat request reschedule: ' . $e->getMessage(), - ]); + $data['status'] = 'error'; + $data['message'] = 'Gagal membuat jadwal: ' . $e->getMessage(); } + } else { + $data['status'] = 'error'; + $data['message'] = "no ajax request"; } + return response()->json($data); } + + 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); + + $documents = DokumenJaminan::where('permohonan_id', $permohonan->id)->get(); + if (count($documents) < 1) { + return redirect()->route( + 'debitur.jaminan.create', + array_merge(['permohonan_id' => $permohonan->id], ['id' => $permohonan->debiture->id]), + )->with('success', 'Permohonan created successfully, Lengkapi data jaminan terlebih dahulu'); + } + + 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 storeRescheduleSurvey(Request $request, $id) + { + try { + $validatedData = $request->validate([ + 'permohonan_id' => 'required|exists:permohonan,id', + 'penilaian_id' => 'nullable', + 'nomor_registrasi' => 'required', + 'reschedule_note' => 'required', + 'reschedule_date' => 'required', + 'keterangan' => 'required', + ]); + + DB::beginTransaction(); + + $permohonan = Permohonan::findOrFail($request->permohonan_id); + $permohonan->update([ + 'status' => 'request-reschedule', + ]); + + $penilaian = Penilaian::findOrFail($id); + + $penilaian->update([ + 'reschedule_date' => $request->reschedule_date, + 'reschedule_note' => $request->reschedule_note, + ]); + + DB::commit(); + return response()->json([ + 'status' => 'success', + 'message' => 'Proses request reschedule permohonan Nomor registrasi ' . $request->nomor_registrasi . ' berhasil', + ]); + } catch (Exception $e) { + DB::rollBack(); + return response()->json([ + 'status' => 'error', + 'message' => 'Gagal membuat request reschedule: ' . $e->getMessage(), + ]); + } + } +} diff --git a/database/migrations/2025_05_07_183700_add_nomor_registrasi_to_persetujuan_penawaran_table.php b/database/migrations/2025_05_07_183700_add_nomor_registrasi_to_persetujuan_penawaran_table.php new file mode 100644 index 0000000..c6d7af8 --- /dev/null +++ b/database/migrations/2025_05_07_183700_add_nomor_registrasi_to_persetujuan_penawaran_table.php @@ -0,0 +1,28 @@ +string('nomor_registrasi')->nullable()->after('penawaran_id'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('persetujuan_penawaran', function (Blueprint $table) { + $table->dropColumn('nomor_registrasi'); + }); + } +};