(lpj/nilai-plafond): Tambah field biaya, validasi, transaksi DB, ekspor, dan tampilan

- Menambahkan kolom biaya ke seluruh alur Nilai Plafond (model, request, controller, views, export, dan migrasi)
- Update model NilaiPlafond agar field biaya bisa di-mass assign ($fillable)
- Tambah validasi baru 'biaya' (nullable|numeric|min:0) di NilaiPlafondRequest
- Terapkan transaksi DB (beginTransaction, commit, rollback) pada store/update/destroy di controller
- Tambahkan kolom biaya ke view create, edit, dan datatable index dengan format Rupiah dan tooltip nilai mentah
- Tambah header & mapping kolom biaya di NilaiPlafondExport agar muncul di hasil export Excel
- Tambah migrasi kolom biaya bertipe decimal(15,2) nullable dengan rollback support
- Tambahkan logging detail (Log::info & Log::error) di setiap proses utama controller
- Pastikan pencarian kolom biaya pada datatables menggunakan CAST ke TEXT untuk kompatibilitas PostgreSQL
This commit is contained in:
Daeng Deni Mardaeni
2025-10-03 10:23:21 +07:00
parent 04ee3a0c48
commit e773b82218
7 changed files with 221 additions and 62 deletions

View File

@@ -24,6 +24,7 @@
$row->id,
$row->code,
$row->name,
$row->biaya,
$row->created_at
];
}
@@ -35,6 +36,7 @@
'ID',
'Code',
'Name',
'Biaya',
'Created At'
];
}
@@ -44,7 +46,8 @@
{
return [
'A' => NumberFormat::FORMAT_NUMBER,
'D' => NumberFormat::FORMAT_DATE_DATETIME
'D' => NumberFormat::FORMAT_NUMBER_00,
'E' => NumberFormat::FORMAT_DATE_DATETIME
];
}
}

View File

@@ -5,6 +5,8 @@
use App\Http\Controllers\Controller;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Facades\Excel;
use Modules\Lpj\Exports\NilaiPlafondExport;
use Modules\Lpj\Http\Requests\NilaiPlafondRequest;
@@ -14,74 +16,147 @@
{
public $user;
/**
* Tampilkan halaman daftar Nilai Plafond.
*
* @return \Illuminate\Contracts\View\View
*/
public function index()
{
Log::info('[NilaiPlafondController@index] Return view index nilai_plafond');
return view('lpj::nilai_plafond.index');
}
/**
* Simpan data Nilai Plafond baru ke database.
* Menggunakan transaksi untuk menjamin konsistensi data.
*
* @param NilaiPlafondRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(NilaiPlafondRequest $request)
{
$validate = $request->validated();
if ($validate) {
DB::beginTransaction();
try {
// Save to database
NilaiPlafond::create($validate);
$created = NilaiPlafond::create($validate);
DB::commit();
Log::info('[NilaiPlafondController@store] NilaiPlafond created', ['id' => $created->id, 'payload' => $validate]);
return redirect()
->route('basicdata.nilai-plafond.index')
->with('success', 'Jenis Aset created successfully');
->with('success', 'Nilai Plafond berhasil dibuat');
} catch (Exception $e) {
DB::rollBack();
Log::error('[NilaiPlafondController@store] Failed to create nilai plafond', ['error' => $e->getMessage(), 'payload' => $validate]);
return redirect()
->route('basicdata.nilai-plafond.create')
->with('error', 'Failed to create nilai plafond');
->with('error', 'Gagal membuat Nilai Plafond');
}
}
Log::warning('[NilaiPlafondController@store] Validation failed');
return redirect()
->route('basicdata.nilai-plafond.create')
->with('error', 'Validasi gagal');
}
/**
* Tampilkan form pembuatan Nilai Plafond.
*
* @return \Illuminate\Contracts\View\View
*/
public function create()
{
Log::info('[NilaiPlafondController@create] Return view create nilai_plafond');
return view('lpj::nilai_plafond.create');
}
/**
* Tampilkan form edit Nilai Plafond berdasarkan ID.
*
* @param int $id
* @return \Illuminate\Contracts\View\View
*/
public function edit($id)
{
$nilaiPlafond = NilaiPlafond::find($id);
Log::info('[NilaiPlafondController@edit] Return view edit nilai_plafond', ['id' => $id]);
return view('lpj::nilai_plafond.create', compact('nilaiPlafond'));
}
/**
* Update data Nilai Plafond pada database.
* Menggunakan transaksi untuk menjamin konsistensi data.
*
* @param NilaiPlafondRequest $request
* @param int $id
* @return \Illuminate\Http\RedirectResponse
*/
public function update(NilaiPlafondRequest $request, $id)
{
$validate = $request->validated();
if ($validate) {
DB::beginTransaction();
try {
// Update in database
$nilaiPlafond = NilaiPlafond::find($id);
$nilaiPlafond->update($validate);
DB::commit();
Log::info('[NilaiPlafondController@update] NilaiPlafond updated', ['id' => $id, 'payload' => $validate]);
return redirect()
->route('basicdata.nilai-plafond.index')
->with('success', 'Jenis Aset updated successfully');
->with('success', 'Nilai Plafond berhasil diperbarui');
} catch (Exception $e) {
DB::rollBack();
Log::error('[NilaiPlafondController@update] Failed to update nilai plafond', ['id' => $id, 'error' => $e->getMessage(), 'payload' => $validate]);
return redirect()
->route('basicdata.nilai-plafond.edit', $id)
->with('error', 'Failed to update nilai plafond');
->with('error', 'Gagal memperbarui Nilai Plafond');
}
}
Log::warning('[NilaiPlafondController@update] Validation failed', ['id' => $id]);
return redirect()
->route('basicdata.nilai-plafond.edit', $id)
->with('error', 'Validasi gagal');
}
/**
* Hapus data Nilai Plafond dari database.
* Menggunakan transaksi untuk menjamin konsistensi data.
*
* @param int $id
* @return void
*/
public function destroy($id)
{
DB::beginTransaction();
try {
// Delete from database
$nilaiPlafond = NilaiPlafond::find($id);
$nilaiPlafond->delete();
DB::commit();
Log::info('[NilaiPlafondController@destroy] NilaiPlafond deleted', ['id' => $id]);
echo json_encode(['success' => true, 'message' => 'Jenis Aset deleted successfully']);
echo json_encode(['success' => true, 'message' => 'Nilai Plafond berhasil dihapus']);
} catch (Exception $e) {
echo json_encode(['success' => false, 'message' => 'Failed to delete nilai plafond']);
DB::rollBack();
Log::error('[NilaiPlafondController@destroy] Failed to delete nilai plafond', ['id' => $id, 'error' => $e->getMessage()]);
echo json_encode(['success' => false, 'message' => 'Gagal menghapus Nilai Plafond']);
}
}
/**
* Endpoint data untuk DataTables custom.
* Menyediakan pencarian, sorting, dan pagination.
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function dataForDatatables(Request $request)
{
if (is_null($this->user) || !$this->user->can('nilai_plafond.view')) {
@@ -97,6 +172,8 @@
$query->where(function ($q) use ($search) {
$q->where('code', 'LIKE', "%$search%");
$q->orWhere('name', 'LIKE', "%$search%");
// CAST ke TEXT agar LIKE bekerja di PostgreSQL
$q->orWhereRaw('CAST(biaya AS TEXT) LIKE ?', ["%$search%"]);
});
}
@@ -131,6 +208,13 @@
// Calculate the current page number
$currentPage = 0 + 1;
Log::info('[NilaiPlafondController@dataForDatatables] Return datatables payload', [
'recordsTotal' => $totalRecords,
'recordsFiltered' => $filteredRecords,
'pageCount' => $pageCount,
'page' => $currentPage,
]);
// Return the response data as a JSON object
return response()->json([
'draw' => $request->get('draw'),
@@ -143,8 +227,14 @@
]);
}
/**
* Export data Nilai Plafond ke file Excel.
*
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function export()
{
Log::info('[NilaiPlafondController@export] Export nilai_plafond to Excel');
return Excel::download(new NilaiPlafondExport, 'nilai_plafond.xlsx');
}
}

View File

@@ -14,6 +14,7 @@
{
$rules = [
'name' => 'required|max:255',
'biaya' => 'nullable|numeric|min:0',
];
if ($this->method() == 'PUT') {

View File

@@ -6,5 +6,5 @@
class NilaiPlafond extends Base
{
protected $table = 'nilai_plafond';
protected $fillable = ['code', 'name'];
protected $fillable = ['code', 'name', 'biaya'];
}

View File

@@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Tambahkan kolom biaya pada tabel nilai_plafond
*
* Catatan:
* - Menggunakan tipe decimal(15,2) agar kompatibel dengan PostgreSQL (numeric)
*/
public function up(): void
{
Schema::table('nilai_plafond', function (Blueprint $table) {
// Tambahkan kolom biaya sebagai decimal dengan 2 pecahan
$table->decimal('biaya', 15, 2)->nullable();
});
}
/**
* Rollback perubahan: hapus kolom biaya dari tabel nilai_plafond
*/
public function down(): void
{
if (Schema::hasColumn('nilai_plafond', 'biaya')) {
Schema::table('nilai_plafond', function (Blueprint $table) {
$table->dropColumn('biaya');
});
}
}
};

View File

@@ -5,54 +5,68 @@
@endsection
@section('content')
<div class="w-full grid gap-5 lg:gap-7.5 mx-auto">
@if(isset($nilaiPlafond->id))
<div class="grid gap-5 mx-auto w-full lg:gap-7.5">
@if (isset($nilaiPlafond->id))
<form action="{{ route('basicdata.nilai-plafond.update', $nilaiPlafond->id) }}" method="POST">
<input type="hidden" name="id" value="{{ $nilaiPlafond->id }}">
@method('PUT')
@else
<form method="POST" action="{{ route('basicdata.nilai-plafond.store') }}">
@endif
@csrf
<div class="card border border-agi-100 pb-2.5">
<div class="card-header bg-agi-50" id="basic_settings">
<h3 class="card-title">
{{ isset($nilaiPlafond->id) ? 'Edit' : 'Tambah' }} Nilai Plafond
</h3>
<div class="flex items-center gap-2">
<a href="{{ route('basicdata.nilai-plafond.index') }}" class="btn btn-xs btn-info"><i class="ki-filled ki-exit-left"></i> Back</a>
</div>
</div>
<div class="card-body grid gap-5">
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
<label class="form-label max-w-56">
Code
</label>
<div class="flex flex-wrap items-baseline w-full">
<input class="input @error('code') border-danger bg-danger-light @enderror" type="text" name="code" value="{{ $nilaiPlafond->code ?? '' }}">
@error('code')
<em class="alert text-danger text-sm">{{ $message }}</em>
@enderror
</div>
</div>
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
<label class="form-label max-w-56">
Name
</label>
<div class="flex flex-wrap items-baseline w-full">
<input class="input @error('name') border-danger bg-danger-light @enderror" type="text" name="name" value="{{ $nilaiPlafond->name ?? '' }}">
@error('name')
<em class="alert text-danger text-sm">{{ $message }}</em>
@enderror
</div>
</div>
<div class="flex justify-end">
<button type="submit" class="btn btn-primary">
Save
</button>
</div>
</div>
</div>
</form>
@else
<form method="POST" action="{{ route('basicdata.nilai-plafond.store') }}">
@endif
@csrf
<div class="pb-2.5 border card border-agi-100">
<div class="card-header bg-agi-50" id="basic_settings">
<h3 class="card-title">
{{ isset($nilaiPlafond->id) ? 'Edit' : 'Tambah' }} Nilai Plafond
</h3>
<div class="flex gap-2 items-center">
<a href="{{ route('basicdata.nilai-plafond.index') }}" class="btn btn-xs btn-info"><i
class="ki-filled ki-exit-left"></i> Back</a>
</div>
</div>
<div class="grid gap-5 card-body">
<div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
<label class="form-label max-w-56">
Code
</label>
<div class="flex flex-wrap items-baseline w-full">
<input class="input @error('code') border-danger bg-danger-light @enderror" type="text"
name="code" value="{{ $nilaiPlafond->code ?? '' }}">
@error('code')
<em class="text-sm alert text-danger">{{ $message }}</em>
@enderror
</div>
</div>
<div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
<label class="form-label max-w-56">
Name
</label>
<div class="flex flex-wrap items-baseline w-full">
<input class="input @error('name') border-danger bg-danger-light @enderror" type="text"
name="name" value="{{ $nilaiPlafond->name ?? '' }}">
@error('name')
<em class="text-sm alert text-danger">{{ $message }}</em>
@enderror
</div>
</div>
{{-- Field Biaya --}}
<div class="mt-3">
<label class="text-gray-900 form-label">Biaya</label>
<input class="input @error('biaya') border-danger bg-danger-light @enderror" type="number"
step="0.01" name="biaya" value="{{ $nilaiPlafond->biaya ?? old('biaya') }}" placeholder="0">
@error('biaya')
<div class="text-danger">{{ $message }}</div>
@enderror
<div class="mt-1 text-xs text-muted">Contoh: 1500000 untuk satu juta lima ratus ribu</div>
</div>
</div>
<div class="flex justify-end">
<button type="submit" class="btn btn-primary">
Save
</button>
</div>
</div>
</div>
</form>
</div>
@endsection

View File

@@ -6,8 +6,8 @@
@section('content')
<div class="grid">
<div class="card border border-agi-100 card-grid min-w-full" data-datatable="false" data-datatable-page-size="10" data-datatable-state-save="false" id="nilai-plafond-table" data-api-url="{{ route('basicdata.nilai-plafond.datatables') }}">
<div class="card-header bg-agi-50 py-5 flex-wrap">
<div class="min-w-full border card border-agi-100 card-grid" data-datatable="false" data-datatable-page-size="10" data-datatable-state-save="false" id="nilai-plafond-table" data-api-url="{{ route('basicdata.nilai-plafond.datatables') }}">
<div class="flex-wrap py-5 card-header bg-agi-50">
<h3 class="card-title">
Daftar Nilai Plafond
</h3>
@@ -26,7 +26,7 @@
</div>
<div class="card-body">
<div class="scrollable-x-auto">
<table class="table table-auto table-border align-middle text-gray-700 font-medium text-sm" data-datatable-table="true">
<table class="table text-sm font-medium text-gray-700 align-middle table-auto table-border" data-datatable-table="true">
<thead>
<tr>
<th class="w-14">
@@ -40,17 +40,21 @@
<span class="sort"> <span class="sort-label"> Nilai Plafond </span>
<span class="sort-icon"> </span> </span>
</th>
<th class="min-w-[250px]" data-datatable-column="biaya">
<span class="sort"> <span class="sort-label"> Biaya </span>
<span class="sort-icon"> </span> </span>
</th>
<th class="min-w-[50px] text-center" data-datatable-column="actions">Action</th>
</tr>
</thead>
</table>
</div>
<div class="card-footer justify-center md:justify-between flex-col md:flex-row gap-3 text-gray-600 text-2sm font-medium">
<div class="flex items-center gap-2">
<div class="flex-col gap-3 justify-center font-medium text-gray-600 card-footer md:justify-between md:flex-row text-2sm">
<div class="flex gap-2 items-center">
Show
<select class="select select-sm w-16" data-datatable-size="true" name="perpage"> </select> per page
<select class="w-16 select select-sm" data-datatable-size="true" name="perpage"> </select> per page
</div>
<div class="flex items-center gap-4">
<div class="flex gap-4 items-center">
<span data-datatable-info="true"> </span>
<div class="pagination" data-datatable-pagination="true">
</div>
@@ -119,6 +123,19 @@
name: {
title: 'Nilai Plafond',
},
biaya: {
title: 'Biaya',
// formatter: format rupiah
render: (data) => {
try {
const raw = Number(data.biaya ?? 0);
const formatted = new Intl.NumberFormat('id-ID', { style: 'currency', currency: 'IDR', maximumFractionDigits: 0 }).format(raw);
return `<span title="${raw}">${formatted}</span>`;
} catch (e) {
return data?.biaya ?? '-';
}
}
},
actions: {
title: 'Status',
render: (item, data) => {