Update Log aktivitas
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Web\Konfigurasi;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Konfigurasi\FilterLogAktivitasRequest;
|
||||
use App\Models\Pertanian\Petani;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Spatie\Activitylog\Models\Activity;
|
||||
|
||||
class LogAktivitasController extends Controller
|
||||
{
|
||||
public const JENIS = [
|
||||
'auth' => ['label' => 'Login', 'warna' => 'info'],
|
||||
'petani' => ['label' => 'Profil Petani', 'warna' => 'primary'],
|
||||
'verifikasi' => ['label' => 'Status Petani', 'warna' => 'success'],
|
||||
'pertanian' => ['label' => 'Data Pertanian', 'warna' => 'primary'],
|
||||
'map' => ['label' => 'Peta & Panen', 'warna' => 'primary'],
|
||||
'user' => ['label' => 'Pengguna', 'warna' => 'warning'],
|
||||
'profile' => ['label' => 'Profil Pengguna', 'warna' => 'warning'],
|
||||
'konfigurasi' => ['label' => 'Konfigurasi', 'warna' => 'warning'],
|
||||
'program' => ['label' => 'Program', 'warna' => 'danger'],
|
||||
'core' => ['label' => 'Program (Inti)', 'warna' => 'danger'],
|
||||
'bumdes' => ['label' => 'BUMDes', 'warna' => 'dark'],
|
||||
'koperasi' => ['label' => 'Koperasi', 'warna' => 'dark'],
|
||||
'ekosistem' => ['label' => 'Ekosistem', 'warna' => 'dark'],
|
||||
'master' => ['label' => 'Master Data', 'warna' => 'secondary'],
|
||||
'wilayah' => ['label' => 'Wilayah', 'warna' => 'secondary'],
|
||||
'penduduk' => ['label' => 'Penduduk', 'warna' => 'secondary'],
|
||||
'distribusi' => ['label' => 'Distribusi', 'warna' => 'secondary'],
|
||||
'facilitator' => ['label' => 'Fasilitator', 'warna' => 'secondary'],
|
||||
];
|
||||
|
||||
public const PER_PAGE = [25, 50, 100];
|
||||
|
||||
public function index(FilterLogAktivitasRequest $request)
|
||||
{
|
||||
$filter = $request->validated();
|
||||
|
||||
// Ambil log sesuai filter
|
||||
$aktivitas = Activity::query()
|
||||
->with(['causer', 'subject'])
|
||||
->when($filter['jenis'] ?? null, fn ($q, $jenis) => $q->where('log_name', $jenis))
|
||||
->when($filter['dari'] ?? null, fn ($q, $tgl) => $q->where('created_at', '>=', $tgl))
|
||||
->when($filter['sampai'] ?? null, fn ($q, $tgl) => $q->where('created_at', '<', Carbon::parse($tgl)->addDay()))
|
||||
->when($filter['search'] ?? null, fn ($q, $cari) => $this->cari($q, '%' . mb_strtolower($cari) . '%'))
|
||||
->latest('id')
|
||||
->paginate((int) ($filter['per_page'] ?? self::PER_PAGE[0]))
|
||||
->withQueryString();
|
||||
|
||||
// Nama petani untuk log tab petani (satu query)
|
||||
$namaPetani = Petani::withoutGlobalScopes()->withTrashed()
|
||||
->whereIn('id', $aktivitas->getCollection()->pluck('properties.petani_id')->filter()->unique())
|
||||
->pluck('nama', 'id');
|
||||
|
||||
return view('konfigurasi.log-aktivitas.index', [
|
||||
'aktivitas' => $aktivitas,
|
||||
'jenis' => self::JENIS,
|
||||
'perPage' => self::PER_PAGE,
|
||||
'namaPetani' => $namaPetani,
|
||||
]);
|
||||
}
|
||||
|
||||
// Cari di aktivitas, nama pengguna, dan nama petani
|
||||
private function cari($query, string $cari): void
|
||||
{
|
||||
$query->where(fn ($q) => $q
|
||||
->whereRaw('lower(description) like ?', [$cari])
|
||||
->orWhereHasMorph('causer', [User::class], fn ($q) => $q->whereRaw('lower(name) like ?', [$cari]))
|
||||
->orWhereRaw(
|
||||
"coalesce((properties->>'petani_id')::bigint, case when subject_type = ? then subject_id end) in (select id from petani where lower(nama) like ?)",
|
||||
[Petani::class, $cari]
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -47,10 +47,15 @@ class PetaniStatusController extends Controller
|
||||
return back()->with('status_petani_error', 'Data petani masih berstatus draf.');
|
||||
}
|
||||
|
||||
$dari = $petani->status;
|
||||
$petani->status = $tujuan;
|
||||
$petani->updated_by = $request->user()->id;
|
||||
$petani->save();
|
||||
|
||||
$petani->catatPerubahanStatus($dari, $tujuan === StatusDataPetani::LENGKAP
|
||||
? 'Ditandai lengkap oleh enumerator'
|
||||
: 'Tanda lengkap dibatalkan oleh enumerator');
|
||||
|
||||
return back()->with('status_petani_success', $tujuan === StatusDataPetani::LENGKAP
|
||||
? 'Data petani berhasil ditandai lengkap.'
|
||||
: 'Tanda lengkap berhasil dibatalkan. Status kembali ke draf.');
|
||||
|
||||
@@ -96,6 +96,8 @@ class VerifikasiPetaniController extends Controller
|
||||
$petani->updated_by = auth()->id();
|
||||
$petani->save();
|
||||
|
||||
$petani->catatPerubahanStatus(StatusDataPetani::LENGKAP, 'Diverifikasi');
|
||||
|
||||
return $kembali->with('verifikasi_success', "Data petani {$petani->nama} berhasil diverifikasi.");
|
||||
}
|
||||
|
||||
@@ -120,11 +122,14 @@ class VerifikasiPetaniController extends Controller
|
||||
return $kembali->with('verifikasi_error', "Data petani {$petani->nama} sudah berstatus Draf.");
|
||||
}
|
||||
|
||||
$dari = $petani->status;
|
||||
$petani->status = StatusDataPetani::DRAFT;
|
||||
$petani->catatan = trim($data['catatan']);
|
||||
$petani->updated_by = auth()->id();
|
||||
$petani->save();
|
||||
|
||||
$petani->catatPerubahanStatus($dari, 'Dikembalikan ke draf oleh verifikator', ['catatan' => $petani->catatan]);
|
||||
|
||||
return $kembali->with('verifikasi_success', "Data petani {$petani->nama} dikembalikan ke Draf dengan catatan untuk enumerator.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Konfigurasi;
|
||||
|
||||
use App\Http\Controllers\Web\Konfigurasi\LogAktivitasController;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class FilterLogAktivitasRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return Gate::any(['super_admin', 'admin']);
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'jenis' => ['nullable', Rule::in(array_keys(LogAktivitasController::JENIS))],
|
||||
'search' => ['nullable', 'string', 'max:100'],
|
||||
'dari' => ['nullable', 'date'],
|
||||
'sampai' => ['nullable', 'date', 'after_or_equal:dari'],
|
||||
'per_page' => ['nullable', 'integer', Rule::in(LogAktivitasController::PER_PAGE)],
|
||||
];
|
||||
}
|
||||
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
'sampai.after_or_equal' => 'Tanggal akhir tidak boleh sebelum tanggal awal.',
|
||||
'per_page.in' => 'Jumlah data per halaman tidak valid.',
|
||||
];
|
||||
}
|
||||
|
||||
public function attributes(): array
|
||||
{
|
||||
return [
|
||||
'jenis' => 'jenis aktivitas',
|
||||
'search' => 'kata kunci',
|
||||
'dari' => 'tanggal awal',
|
||||
'sampai' => 'tanggal akhir',
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user