From acd1aa09f7ca5c64d9bda91bc339ffab7fdfe1a9 Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Tue, 13 Aug 2024 15:09:26 +0700 Subject: [PATCH] Feature #9 : Nilai Plafond --- app/Exports/NilaiPlafondExport.php | 50 ++++++ .../Controllers/NilaiPlafondController.php | 150 ++++++++++++++++++ app/Http/Requests/NilaiPlafondRequest.php | 35 ++++ app/Models/NilaiPlafond.php | 10 ++ ...8_13_075620_create_nilai_plafond_table.php | 38 +++++ module.json | 8 + .../views/nilai_plafond/create.blade.php | 58 +++++++ resources/views/nilai_plafond/index.blade.php | 148 +++++++++++++++++ routes/breadcrumbs.php | 16 ++ routes/web.php | 11 ++ 10 files changed, 524 insertions(+) create mode 100644 app/Exports/NilaiPlafondExport.php create mode 100644 app/Http/Controllers/NilaiPlafondController.php create mode 100644 app/Http/Requests/NilaiPlafondRequest.php create mode 100644 app/Models/NilaiPlafond.php create mode 100644 database/migrations/2024_08_13_075620_create_nilai_plafond_table.php create mode 100644 resources/views/nilai_plafond/create.blade.php create mode 100644 resources/views/nilai_plafond/index.blade.php diff --git a/app/Exports/NilaiPlafondExport.php b/app/Exports/NilaiPlafondExport.php new file mode 100644 index 0000000..a937c50 --- /dev/null +++ b/app/Exports/NilaiPlafondExport.php @@ -0,0 +1,50 @@ +id, + $row->code, + $row->name, + $row->created_at + ]; + } + + public function headings() + : array + { + return [ + 'ID', + 'Code', + 'Name', + 'Created At' + ]; + } + + public function columnFormats() + : array + { + return [ + 'A' => NumberFormat::FORMAT_NUMBER, + 'D' => NumberFormat::FORMAT_DATE_DATETIME + ]; + } + } diff --git a/app/Http/Controllers/NilaiPlafondController.php b/app/Http/Controllers/NilaiPlafondController.php new file mode 100644 index 0000000..6a2f41f --- /dev/null +++ b/app/Http/Controllers/NilaiPlafondController.php @@ -0,0 +1,150 @@ +validated(); + + if ($validate) { + try { + // Save to database + NilaiPlafond::create($validate); + return redirect() + ->route('basicdata.nilai-plafond.index') + ->with('success', 'Jenis Aset created successfully'); + } catch (Exception $e) { + return redirect() + ->route('basicdata.nilai-plafond.create') + ->with('error', 'Failed to create nilai plafond'); + } + } + } + + public function create() + { + return view('lpj::nilai_plafond.create'); + } + + public function edit($id) + { + $nilaiPlafond = NilaiPlafond::find($id); + return view('lpj::nilai_plafond.create', compact('nilaiPlafond')); + } + + public function update(NilaiPlafondRequest $request, $id) + { + $validate = $request->validated(); + + if ($validate) { + try { + // Update in database + $nilaiPlafond = NilaiPlafond::find($id); + $nilaiPlafond->update($validate); + return redirect() + ->route('basicdata.nilai-plafond.index') + ->with('success', 'Jenis Aset updated successfully'); + } catch (Exception $e) { + return redirect() + ->route('basicdata.nilai-plafond.edit', $id) + ->with('error', 'Failed to update nilai plafond'); + } + } + } + + public function destroy($id) + { + try { + // Delete from database + $nilaiPlafond = NilaiPlafond::find($id); + $nilaiPlafond->delete(); + + echo json_encode(['success' => true, 'message' => 'Jenis Aset deleted successfully']); + } catch (Exception $e) { + echo json_encode(['success' => false, 'message' => 'Failed to delete nilai plafond']); + } + } + + public function dataForDatatables(Request $request) + { + if (is_null($this->user) || !$this->user->can('nilai_plafond.view')) { + //abort(403, 'Sorry! You are not allowed to view users.'); + } + + // Retrieve data from the database + $query = NilaiPlafond::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('code', 'LIKE', "%$search%"); + $q->orWhere('name', '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->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 export() + { + return Excel::download(new NilaiPlafondExport, 'nilai_plafond.xlsx'); + } + } diff --git a/app/Http/Requests/NilaiPlafondRequest.php b/app/Http/Requests/NilaiPlafondRequest.php new file mode 100644 index 0000000..3890ed7 --- /dev/null +++ b/app/Http/Requests/NilaiPlafondRequest.php @@ -0,0 +1,35 @@ + 'required|max:255', + ]; + + if ($this->method() == 'PUT') { + $rules['code'] = 'required|max:50|unique:nilai_plafond,code,' . $this->id; + } else { + $rules['code'] = 'required|max:50|unique:nilai_plafond,code'; + } + return $rules; + } + + /** + * Determine if the user is authorized to make this request. + */ + public function authorize() + : bool + { + return true; + } + } diff --git a/app/Models/NilaiPlafond.php b/app/Models/NilaiPlafond.php new file mode 100644 index 0000000..3b0bcaf --- /dev/null +++ b/app/Models/NilaiPlafond.php @@ -0,0 +1,10 @@ +id(); + $table->string('code')->unique()->index(); + $table->string('name'); + $table->boolean('status')->default(true)->nullable(); + $table->timestamps(); + $table->timestamp('authorized_at')->nullable(); + $table->char('authorized_status', 1)->nullable(); + $table->softDeletes(); + + $table->unsignedBigInteger('created_by')->nullable(); + $table->unsignedBigInteger('updated_by')->nullable(); + $table->unsignedBigInteger('deleted_by')->nullable(); + $table->unsignedBigInteger('authorized_by')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('nilai_plafond'); + } +}; diff --git a/module.json b/module.json index 16d83e4..12bf09c 100644 --- a/module.json +++ b/module.json @@ -134,6 +134,14 @@ "attributes": [], "permission": "", "roles": [] + }, + { + "title": "Nilai Plafond", + "path": "basicdata.nilai-plafond", + "classes": "", + "attributes": [], + "permission": "", + "roles": [] } ] } diff --git a/resources/views/nilai_plafond/create.blade.php b/resources/views/nilai_plafond/create.blade.php new file mode 100644 index 0000000..3a8aaf5 --- /dev/null +++ b/resources/views/nilai_plafond/create.blade.php @@ -0,0 +1,58 @@ +@extends('layouts.main') + +@section('breadcrumbs') + {{ Breadcrumbs::render(request()->route()->getName()) }} +@endsection + +@section('content') +
+ @if(isset($nilaiPlafond->id)) +
+ + @method('PUT') + @else + + @endif + @csrf +
+
+

+ {{ isset($nilaiPlafond->id) ? 'Edit' : 'Tambah' }} Nilai Plafond +

+
+ Back +
+
+
+
+ +
+ + @error('code') + {{ $message }} + @enderror +
+
+
+ +
+ + @error('name') + {{ $message }} + @enderror +
+
+
+ +
+
+
+
+
+@endsection diff --git a/resources/views/nilai_plafond/index.blade.php b/resources/views/nilai_plafond/index.blade.php new file mode 100644 index 0000000..32607e2 --- /dev/null +++ b/resources/views/nilai_plafond/index.blade.php @@ -0,0 +1,148 @@ +@extends('layouts.main') + +@section('breadcrumbs') + {{ Breadcrumbs::render('basicdata.nilai-plafond') }} +@endsection + +@section('content') +
+
+
+

+ Daftar Nilai Plafond +

+ +
+
+
+ + + + + + + + + +
+ + + Code + + + Nilai Plafond + + Action
+
+ +
+
+
+@endsection + +@push('scripts') + + + +@endpush + diff --git a/routes/breadcrumbs.php b/routes/breadcrumbs.php index b9cb703..9fff01a 100644 --- a/routes/breadcrumbs.php +++ b/routes/breadcrumbs.php @@ -113,6 +113,22 @@ $trail->push('Edit Cabang'); }); + Breadcrumbs::for('basicdata.nilai-plafond', function (BreadcrumbTrail $trail) { + $trail->parent('basicdata'); + $trail->push('Nilai Plafond', route('basicdata.nilai-plafond.index')); + }); + + Breadcrumbs::for('basicdata.nilai-plafond.create', function (BreadcrumbTrail $trail) { + $trail->parent('basicdata.nilai-plafond'); + $trail->push('Tambah Nilai Plafond', route('basicdata.nilai-plafond.create')); + }); + + Breadcrumbs::for('basicdata.nilai-plafond.edit', function (BreadcrumbTrail $trail) { + $trail->parent('basicdata.nilai-plafond'); + $trail->push('Edit Nilai Plafond'); + }); + + Breadcrumbs::for('debitur', function (BreadcrumbTrail $trail) { $trail->push('Debitur', route('debitur.index')); }); diff --git a/routes/web.php b/routes/web.php index 107f8cc..8b46e67 100644 --- a/routes/web.php +++ b/routes/web.php @@ -8,6 +8,7 @@ use Modules\Lpj\Http\Controllers\JenisFasilitasKreditController; use Modules\Lpj\Http\Controllers\JenisJaminanController; use Modules\Lpj\Http\Controllers\JenisLegalitasJaminanController; + use Modules\Lpj\Http\Controllers\NilaiPlafondController; use Modules\Lpj\Http\Controllers\TujuanPenilaianController; /* @@ -100,8 +101,18 @@ 'destroy' => 'branch.destroy', ] ]); + + Route::name('nilai-plafond.')->prefix('nilai-plafond')->group(function () { + Route::get('restore/{id}', [NilaiPlafondController::class, 'restore'])->name('restore'); + Route::get('datatables', [NilaiPlafondController::class, 'dataForDatatables']) + ->name('datatables'); + Route::get('export', [NilaiPlafondController::class, 'export'])->name('export'); + }); + Route::resource('nilai-plafond', NilaiPlafondController::class); }); + + Route::name('debitur.')->prefix('debitur')->group(function () { Route::get('restore/{id}', [DebitureController::class, 'restore'])->name('restore'); Route::get('datatables', [DebitureController::class, 'dataForDatatables'])