diff --git a/app/Http/Controllers/Web/Ekosistem/PetaniCPMController.php b/app/Http/Controllers/Web/Ekosistem/PetaniCPMController.php index e67b130..b5d25e2 100644 --- a/app/Http/Controllers/Web/Ekosistem/PetaniCPMController.php +++ b/app/Http/Controllers/Web/Ekosistem/PetaniCPMController.php @@ -8,6 +8,7 @@ use Illuminate\View\View; use Illuminate\Support\Facades\Gate; use Illuminate\Http\RedirectResponse; use Illuminate\Validation\Rule; +use Illuminate\Http\JsonResponse; use App\Models\Ekosistem\PetaniCPM; use App\Models\Wilayah\Provinsi; @@ -184,4 +185,96 @@ class PetaniCPMController extends Controller return redirect()->route('petani-cpm.index')->with('success', 'Data petani CPM berhasil dihapus.'); } + + public function summary(string $tipe): JsonResponse + { + switch ($tipe) { + case 'jenis_kelamin': + return $this->calculateJenisKelamin(); + case 'usia': + return $this->calculateUsia(); + case 'pendidikan': + return $this->calculatePendidikan(); + } + + return response()->json([ + 'petani' => PetaniCPM::count(), + 'pengumpul' => PetaniCPM::where('petani_pengumpul', 'pengumpul')->count(), + 'petani' => PetaniCPM::where('petani_pengumpul', 'petani')->count(), + ]); + } + + private function calculateJenisKelamin() + { + $petaniBase = PetaniCPM::query(); + + return response()->json([ + 'total' => (clone $petaniBase)->count(), + 'laki_laki' => (clone $petaniBase)->where('jenis_kelamin', 1)->count(), + 'perempuan' => (clone $petaniBase)->where('jenis_kelamin', 0)->count(), + ]); + } + + private function calculateUsia() + { + $petaniBase = PetaniCPM::query(); + + $usiaRataRata = (clone $petaniBase) + ->selectRaw('AVG(EXTRACT(YEAR FROM AGE(CURRENT_DATE, tanggal_lahir))) as avg') + ->value('avg'); + $usiaMayoritas = (clone $petaniBase) + ->selectRaw(" + CASE + WHEN EXTRACT(YEAR FROM AGE(CURRENT_DATE, tanggal_lahir)) BETWEEN 0 AND 4 THEN '0-4' + WHEN EXTRACT(YEAR FROM AGE(CURRENT_DATE, tanggal_lahir)) BETWEEN 5 AND 9 THEN '5-9' + WHEN EXTRACT(YEAR FROM AGE(CURRENT_DATE, tanggal_lahir)) BETWEEN 10 AND 14 THEN '10-14' + WHEN EXTRACT(YEAR FROM AGE(CURRENT_DATE, tanggal_lahir)) BETWEEN 15 AND 19 THEN '15-19' + WHEN EXTRACT(YEAR FROM AGE(CURRENT_DATE, tanggal_lahir)) BETWEEN 20 AND 24 THEN '20-24' + WHEN EXTRACT(YEAR FROM AGE(CURRENT_DATE, tanggal_lahir)) BETWEEN 25 AND 29 THEN '25-29' + WHEN EXTRACT(YEAR FROM AGE(CURRENT_DATE, tanggal_lahir)) BETWEEN 30 AND 34 THEN '30-34' + WHEN EXTRACT(YEAR FROM AGE(CURRENT_DATE, tanggal_lahir)) BETWEEN 35 AND 39 THEN '35-39' + WHEN EXTRACT(YEAR FROM AGE(CURRENT_DATE, tanggal_lahir)) BETWEEN 40 AND 44 THEN '40-44' + WHEN EXTRACT(YEAR FROM AGE(CURRENT_DATE, tanggal_lahir)) BETWEEN 45 AND 49 THEN '45-49' + WHEN EXTRACT(YEAR FROM AGE(CURRENT_DATE, tanggal_lahir)) BETWEEN 50 AND 54 THEN '50-54' + WHEN EXTRACT(YEAR FROM AGE(CURRENT_DATE, tanggal_lahir)) BETWEEN 55 AND 59 THEN '55-59' + WHEN EXTRACT(YEAR FROM AGE(CURRENT_DATE, tanggal_lahir)) BETWEEN 60 AND 64 THEN '60-64' + WHEN EXTRACT(YEAR FROM AGE(CURRENT_DATE, tanggal_lahir)) BETWEEN 65 AND 69 THEN '65-69' + WHEN EXTRACT(YEAR FROM AGE(CURRENT_DATE, tanggal_lahir)) BETWEEN 70 AND 74 THEN '70-74' + ELSE '75+' + END as kelompok_usia + ") + ->groupBy('kelompok_usia') + ->orderByRaw('COUNT(*) DESC') + ->value('kelompok_usia'); + + return response()->json([ + 'usia_rata_rata' => $usiaRataRata ? round($usiaRataRata, 1) : 0, + 'usia_mayoritas' => $usiaMayoritas ?? '-', + ]); + } + + private function calculatePendidikan() + { + $petaniBase = PetaniCPM::query(); + + $mayoritasValue = (clone $petaniBase) + ->selectRaw('pendidikan_terakhir, COUNT(*) as total') + ->groupBy('pendidikan_terakhir') + ->orderByDesc('total') + ->orderByDesc('pendidikan_terakhir') + ->value('pendidikan_terakhir'); + + $tertinggiValue = (clone $petaniBase) + ->whereNotNull('pendidikan_terakhir') + ->orderByDesc('pendidikan_terakhir') // karena ada prefix angka + ->value('pendidikan_terakhir'); + + $mayoritasLabel = $mayoritasValue?->label() ?? '-'; + $tertinggiLabel = $tertinggiValue?->label() ?? '-'; + + return response()->json([ + 'pendidikan_mayoritas' => $mayoritasLabel, + 'pendidikan_tertinggi' => $tertinggiLabel, + ]); + } } diff --git a/public/icons/agromonev_192.png b/public/icons/agromonev_192.png new file mode 100644 index 0000000..a233804 Binary files /dev/null and b/public/icons/agromonev_192.png differ diff --git a/public/icons/agromonev_512.png b/public/icons/agromonev_512.png new file mode 100644 index 0000000..811379c Binary files /dev/null and b/public/icons/agromonev_512.png differ diff --git a/public/manifest.json b/public/manifest.json new file mode 100644 index 0000000..dd5cf48 --- /dev/null +++ b/public/manifest.json @@ -0,0 +1,20 @@ +{ + "name": "AgroMonev", + "short_name": "AgroMonev", + "start_url": "/", + "display": "standalone", + "background_color": "#ffffff", + "theme_color": "#0d6efd", + "icons": [ + { + "src": "/icons/agromonev_192.png", + "sizes": "192x192", + "type": "image/png" + }, + { + "src": "/icons/agromonev_512.png", + "sizes": "512x512", + "type": "image/png" + } + ] +} \ No newline at end of file diff --git a/public/service-worker.js b/public/service-worker.js new file mode 100644 index 0000000..cae4b14 --- /dev/null +++ b/public/service-worker.js @@ -0,0 +1,73 @@ +const CACHE_NAME = 'agromonev-v1'; + +// asset statis saja +const STATIC_ASSETS = [ + '/', + '/offline', +]; + +// install +self.addEventListener('install', event => { + self.skipWaiting(); + event.waitUntil( + caches.open(CACHE_NAME) + .then(cache => cache.addAll(STATIC_ASSETS)) + ); +}); + +// activate (hapus cache lama) +self.addEventListener('activate', event => { + event.waitUntil( + caches.keys().then(keys => { + return Promise.all( + keys.map(key => { + if (key !== CACHE_NAME) { + return caches.delete(key); + } + }) + ); + }) + ); + self.clients.claim(); +}); + +self.addEventListener('fetch', event => { + const request = event.request; + + if (!request.url.startsWith('http')) return; + if (request.method !== 'GET') return; + + const url = new URL(request.url); + + // ❗ SKIP halaman auth & dashboard + if ( + url.pathname.startsWith('/login') || + url.pathname.startsWith('/logout') || + url.pathname.startsWith('/register') || + url.pathname.startsWith('/dashboard') || + url.pathname.startsWith('/admin') + ) { + event.respondWith(fetch(request)); + return; + } + + // ❗ SKIP API + if (url.pathname.startsWith('/api')) { + event.respondWith(fetch(request)); + return; + } + + // 🔹 HTML → network first + if (request.headers.get('accept')?.includes('text/html')) { + event.respondWith( + fetch(request) + .catch(() => caches.match('/offline')) + ); + return; + } + + // 🔹 asset → cache + event.respondWith( + caches.match(request).then(res => res || fetch(request)) + ); +}); \ No newline at end of file diff --git a/resources/views/layout/header.blade.php b/resources/views/layout/header.blade.php index 636279b..b0f3656 100644 --- a/resources/views/layout/header.blade.php +++ b/resources/views/layout/header.blade.php @@ -15,10 +15,17 @@ name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0" /> - Admin | {{ $title ?? 'Database Petani' }} + AgroMonev | {{ $title ?? 'Database Petani' }} + + + + + + + diff --git a/resources/views/layout/main.blade.php b/resources/views/layout/main.blade.php index 5e59cbe..036e944 100644 --- a/resources/views/layout/main.blade.php +++ b/resources/views/layout/main.blade.php @@ -112,5 +112,15 @@ } --> @stack('scripts') + + \ No newline at end of file diff --git a/resources/views/offline.blade.php b/resources/views/offline.blade.php new file mode 100644 index 0000000..35703f0 --- /dev/null +++ b/resources/views/offline.blade.php @@ -0,0 +1,10 @@ + + + + Offline + + +

Koneksi terputus

+

Silakan cek internet Anda.

+ + \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index 1111e25..6fb26ec 100644 --- a/routes/web.php +++ b/routes/web.php @@ -9,6 +9,8 @@ Route::post('/create-account', [App\Http\Controllers\Web\Konfigurasi\AuthControl Route::post('/authenticate', [App\Http\Controllers\Web\Konfigurasi\AuthController::class, 'authenticate'])->name('auth.authenticate'); Route::post('/logout', [App\Http\Controllers\Web\Konfigurasi\AuthController::class, 'logout'])->name('logout'); +Route::view('/offline', 'offline')->name('offline'); + Route::middleware('auth') ->group(function () {