154 lines
5.5 KiB
PHP
154 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Manajemen;
|
|
|
|
use App\Helpers\AturanPerhitunganHelper;
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class AturanPerhitunganController extends Controller
|
|
{
|
|
private function checkAccess(): void
|
|
{
|
|
$canAccess = auth()->user()->canAny(['Baca Periode', 'Ubah Periode']) || auth()->user()->hasRole('Superuser');
|
|
abort_if(! $canAccess, 403);
|
|
}
|
|
|
|
public function index(): View
|
|
{
|
|
$this->checkAccess();
|
|
|
|
$title = 'Aturan Perhitungan';
|
|
$canUbah = auth()->user()->can('Ubah Periode') || auth()->user()->hasRole('Superuser');
|
|
|
|
return view('manajemen.aturan-perhitungan.index', compact('title', 'canUbah'));
|
|
}
|
|
|
|
public function data(): JsonResponse
|
|
{
|
|
$this->checkAccess();
|
|
|
|
$canUbah = auth()->user()->can('Ubah Periode') || auth()->user()->hasRole('Superuser');
|
|
$rules = collect(AturanPerhitunganHelper::getAll());
|
|
|
|
return DataTables::of($rules)
|
|
->addIndexColumn()
|
|
->addColumn('periode_badge', function ($row) {
|
|
if (! empty($row['periode'])) {
|
|
return '<span class="badge bg-info">Periode ' . $row['periode'] . '</span>';
|
|
}
|
|
return '<span class="badge bg-secondary">Semua Periode < 401</span>';
|
|
})
|
|
->addColumn('status_badge', function ($row) {
|
|
$status = (bool) ($row['status'] ?? false);
|
|
$badgeClass = $status ? 'badge bg-success' : 'badge bg-secondary';
|
|
$statusText = $status ? 'Aktif' : 'Non Aktif';
|
|
|
|
return '<span class="' . $badgeClass . '">' . $statusText . '</span>';
|
|
})
|
|
->addColumn('switch', function ($row) use ($canUbah) {
|
|
if (! $canUbah) {
|
|
return '-';
|
|
}
|
|
$checked = !empty($row['status']) ? 'checked' : '';
|
|
return '
|
|
<div class="form-check form-switch d-inline-block">
|
|
<input class="form-check-input switch-status-btn" type="checkbox" role="switch"
|
|
data-id="' . $row['id'] . '" ' . $checked . ' style="cursor: pointer; width: 40px; height: 20px;">
|
|
</div>
|
|
';
|
|
})
|
|
->addColumn('action', function ($row) use ($canUbah) {
|
|
if (! $canUbah) {
|
|
return '-';
|
|
}
|
|
|
|
return '
|
|
<div class="dropdown">
|
|
<span id="dropdownMenuButton" data-bs-toggle="dropdown" style="cursor: pointer;">
|
|
<i class="uil-ellipsis-v"></i>
|
|
</span>
|
|
<ul class="dropdown-menu">
|
|
<li>
|
|
<a href="javascript:void(0);" class="dropdown-item edit-btn" data-id="' . $row['id'] . '">
|
|
<i class="uil-edit" style="margin-right: 5px"></i> Edit
|
|
</a>
|
|
</li>
|
|
<li>
|
|
<a href="javascript:void(0);" class="dropdown-item toggle-btn" data-id="' . $row['id'] . '">
|
|
<i class="uil-sync" style="margin-right: 5px"></i> Switch Status
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
';
|
|
})
|
|
->rawColumns(['periode_badge', 'status_badge', 'switch', 'action'])
|
|
->make(true);
|
|
}
|
|
|
|
public function show(int|string $id): JsonResponse
|
|
{
|
|
$this->checkAccess();
|
|
|
|
$rule = AturanPerhitunganHelper::find($id);
|
|
|
|
if (! $rule) {
|
|
return response()->json(['success' => false, 'message' => 'Aturan tidak ditemukan'], 404);
|
|
}
|
|
|
|
return response()->json($rule);
|
|
}
|
|
|
|
public function update(Request $request, int|string $id): JsonResponse
|
|
{
|
|
$this->checkAccess();
|
|
|
|
$request->validate([
|
|
'nama' => 'required|string|max:255',
|
|
'deskripsi' => 'nullable|string',
|
|
'status' => 'required|in:0,1,"0","1",true,false',
|
|
]);
|
|
|
|
$status = in_array($request->status, [1, '1', true, 'true'], true);
|
|
|
|
$success = AturanPerhitunganHelper::update($id, [
|
|
'nama' => $request->nama,
|
|
'deskripsi' => $request->deskripsi,
|
|
'status' => $status,
|
|
]);
|
|
|
|
if (! $success) {
|
|
return response()->json(['success' => false, 'message' => 'Gagal mengupdate aturan'], 500);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Aturan perhitungan berhasil diupdate',
|
|
]);
|
|
}
|
|
|
|
public function toggle(int|string $id): JsonResponse
|
|
{
|
|
$this->checkAccess();
|
|
|
|
$success = AturanPerhitunganHelper::toggleStatus($id);
|
|
|
|
if (! $success) {
|
|
return response()->json(['success' => false, 'message' => 'Gagal mengubah status aturan'], 500);
|
|
}
|
|
|
|
$rule = AturanPerhitunganHelper::find($id);
|
|
$statusText = !empty($rule['status']) ? 'diaktifkan' : 'dinonaktifkan';
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => "Aturan perhitungan berhasil {$statusText}",
|
|
'status' => (bool) ($rule['status'] ?? false),
|
|
]);
|
|
}
|
|
}
|