diff --git a/app/Console/Commands/EmisiCalculationQueue.php.bak b/app/Console/Commands/EmisiCalculationQueue.php.bak deleted file mode 100644 index a51c06e..0000000 --- a/app/Console/Commands/EmisiCalculationQueue.php.bak +++ /dev/null @@ -1,214 +0,0 @@ -rowActive()->first(); - if ($processing) { - return; - } - - $calculation = Calculation::where('calculation_status', SigdStatus::PENDING) - ->rowActive()->orderBy('created_at', 'asc')->first(); - if (!$calculation) { - // $this->logMessage('info', 'Semua kalkulasi emisi telah berhasil diselesaikan.'); - return; - } - - $calculation->update([ - 'executed_time' => now(), - 'calculation_status' => SigdStatus::PROSES, - ]); - - $this->createLogFile($calculation); - $this->logMessage('info', 'Kalkulasi Emisi untuk Tahun Inventory: ' . $calculation->inventory_year . ', id: ' . $calculation->id); - - $isLocked = ActivityLock::isLocked($calculation->inventory_year); - if ($isLocked) { - $this->logMessage('warning', 'Kalkulasi Emisi untuk Tahun Inventory: ' . $calculation->inventory_year . ', id: ' . $calculation->id . ' sedang dikunci dan tidak dapat melakukan kalkulasi.'); - - $calculation->update([ - 'finished_time' => now(), - 'calculation_status' => SigdStatus::GAGAL, - ]); - - return; - } - - if ($calculation->energy) $this->executeService('energy', $calculation); - if ($calculation->agriculture) $this->executeService('agriculture', $calculation); - if ($calculation->folu) $this->executeService('folu', $calculation); - if ($calculation->waste) $this->executeService('waste', $calculation); - if ($calculation->ippu) $this->executeService('ippu', $calculation); - - // Execute Energy GPC emission calculation - $this->energyGPCCalc($calculation); - - // Execute mapping IPCC to GPC - $this->gpcMapping($calculation); - - $calculation->update([ - 'finished_time' => now(), - 'calculation_status' => SigdStatus::SELESAI, - ]); - - $this->logMessage('info', 'Kalkulasi Emisi untuk Tahun Inventory: ' . $calculation->inventory_year . ', id: ' . $calculation->id . ' berhasil diselesaikan.'); - } catch (Exception $e) { - $this->logMessage('error', 'Terjadi kesalahan saat kalkulasi emisi, id: ' . $calculation->id . '. Error: ' . $e->getMessage()); - throw $e; - } - } - - - private function createLogFile(Calculation $calculation) - { - $logDirectory = storage_path('logs/calculation/'); - - $this->logMessage('info', 'Checking if log directory exists: ' . $logDirectory); - - if (!File::exists($logDirectory)) { - $this->logMessage('info', 'Directory not found. Creating directory: ' . $logDirectory); - File::makeDirectory($logDirectory, 0755, true); - } - - $logFileName = $logDirectory . $calculation->inventory_year . '_' . $calculation->id . '.log'; - file_put_contents($logFileName, ''); - - $this->logFilePath = $logFileName; - $this->logMessage('info', 'Log file created: ' . $logFileName); - } - - private function executeService($sector, $calculation) - { - // if ($calculation->$sector) { - $wsList = ReferenceWs::where('sector', $sector) - ->whereNotNull('code')->rowActive()->get(); - - foreach ($wsList as $ws) { - if ($ws->code == '4d1_ef') { - continue; - } - - $service = "\\App\\Services\\Emisi\\" . class_basename($ws->model) . "Service"; - $this->logMessage('service yang dijalankan', "{$service}"); - - try { - $serviceClass = app($service); - if (method_exists($service, 'save')) { - $serviceClass->save($ws->code, $calculation->inventory_year); - $this->logMessage('info', "Kalkulasi Emisi pada Worksheet {$ws->ws_code}. {$ws->ws_title} telah berhasil diselesaikan."); - } else { - $this->logMessage('error', "{$service} tidak memiliki method proses kalkulasi."); - } - } catch (\Exception $e) { - $this->logMessage('error', "Error saat menjalankan {$service} pada sektor {$sector}: " . $e->getMessage()); - - $calculation->update([ - 'executed_time' => null, - 'finished_time' => null, - 'calculation_status' => SigdStatus::PENDING, - ]); - - throw $e; - } - } - // } - } - - private function gpcMapping($calculation) - { - $service = "\\App\\Services\\Emisi\\GpcMappingService"; - - try { - $serviceClass = app($service); - if (method_exists($service, 'save')) { - $serviceClass->save($calculation->inventory_year); - $this->logMessage('info', "Mapping dari Emisi IPCC ke GPC telah berhasil diproses"); - } else { - $this->logMessage('error', get_class($serviceClass) . " tidak memiliki method save."); - } - } catch (\Exception $e) { - $this->logMessage('error', "Error saat menjalankan " . get_class($serviceClass) . ": " . $e->getMessage()); - - $calculation->update([ - 'executed_time' => null, - 'finished_time' => null, - 'calculation_status' => SigdStatus::PENDING, - ]); - - throw $e; - } - } - - private function energyGPCCalc($calculation) - { - $service = "\\App\\Services\\Emisi\\EnergyGPCService"; - - try { - $serviceClass = app($service); - if (method_exists($service, 'save')) { - $serviceClass->save($calculation->inventory_year); - $this->logMessage('info', "Kalkulasi Emisi Energi GPC telah berhasil diproses"); - } else { - $this->logMessage('error', get_class($serviceClass) . " tidak memiliki method save."); - } - } catch (\Exception $e) { - $this->logMessage('error', "Error saat menjalankan " . get_class($serviceClass) . ": " . $e->getMessage()); - - $calculation->update([ - 'executed_time' => null, - 'finished_time' => null, - 'calculation_status' => SigdStatus::PENDING, - ]); - - throw $e; - } - } - - - private function logMessage($level, $message) - { - $this->line($message); // Output to console - - // Log to the file - $formattedMessage = '[' . now() . '] ' . strtoupper($level) . ': ' . $message . PHP_EOL; - if ($this->logFilePath) { - file_put_contents($this->logFilePath, $formattedMessage, FILE_APPEND); - } - - $this->info($message); - // Also log to Laravel's default log channel - if ($level === 'error') { - Log::error($message); - } - // } else { - // Log::info($message); - // } - } -} diff --git a/app/Http/Controllers/DashboardAdaptasiController.php b/app/Http/Controllers/DashboardAdaptasiController.php new file mode 100644 index 0000000..7da7e56 --- /dev/null +++ b/app/Http/Controllers/DashboardAdaptasiController.php @@ -0,0 +1,72 @@ +adaptationService = $adaptationService; + } + + public function aksi(Request $request) + { + $year = (int) $request->input('adaptationYear', date('Y')); + + + try { + $dashboardData = $this->adaptationService->getDashboardData($year); + return view('auth.dashboard-adaptation-aksi', [ + 'title' => $this->title, + 'route' => $this->route, + 'stats' => $dashboardData['stats'], + 'charts' => $dashboardData['charts'], + 'today' => Carbon::today()->translatedFormat('d F Y'), + ]); + } catch(\Exception $e) { + Log::error('Error loading aksi mitigasi dashboard', [ + 'message' => $e->getMessage(), + 'trace' => $e->getTraceAsString(), + ]); + + return back()->withErrors(['error' => 'Gagal memuat dashboard aksi mitigasi.']); + } + } + + public function index(Request $request) + { + $year = (int) $request->input('adaptationYear', date('Y')); + + try { + $dashboardData = $this->adaptationService->getDashboardData($year); + + return view('auth.dashboard-adaptation', [ + 'title' => $this->title, + 'route' => $this->route, + 'selectedYear' => $dashboardData['adaptationYear'], + 'tableData' => $dashboardData['tableData'], // ⬅️ collection biasa + 'barData' => $dashboardData['barData'], + 'realisasiData' => $dashboardData['realisasiData'], + 'budgetData' => $dashboardData['budgetData'], + ]); + } catch (\Exception $e) { + Log::error('Error loading dashboard adaptation data', [ + 'message' => $e->getMessage(), + 'trace' => $e->getTraceAsString(), + ]); + + // optional: kasih feedback ke user biar nggak blank + return back()->withErrors(['error' => 'Gagal memuat dashboard adaptasi.']); + } + } +} diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php new file mode 100644 index 0000000..ac07a2c --- /dev/null +++ b/app/Http/Controllers/DashboardController.php @@ -0,0 +1,39 @@ +emissionService = $emissionService; + } + + public function index(Request $request) + { + try { + $data['inventoryYear'] = $request->input('year', date('Y')); + + $data['dashboardData'] = $this->emissionService->getDashboardData($data['inventoryYear']); + $data['title'] = $this->title; + $data['route'] = $this->route; + + return view($this->template.'.dashboard', $data); + + } catch (\Exception $e) { + Log::error('Error loading dashboard data', [ + 'message' => $e->getMessage(), + 'trace' => $e->getTraceAsString(), + ]); + } + } +} diff --git a/app/Http/Controllers/DashboardMitigasiController.php b/app/Http/Controllers/DashboardMitigasiController.php new file mode 100644 index 0000000..d02d32e --- /dev/null +++ b/app/Http/Controllers/DashboardMitigasiController.php @@ -0,0 +1,73 @@ +mitigationService = $mitigationService; + } + + + public function aksi(Request $request) + { + $year = (int) $request->input('mitigationYear', date('Y')); + + try { + $dashboardData = $this->mitigationService->getDashboardData($year); + return view('auth.dashboard-mitigation-aksi', [ + 'title' => $this->title, + 'route' => $this->route, + 'stats' => $dashboardData['stats'], + 'charts' => $dashboardData['charts'], + 'today' => Carbon::today()->translatedFormat('d F Y'), + ]); + } catch(\Exception $e) { + Log::error('Error loading aksi mitigasi dashboard', [ + 'message' => $e->getMessage(), + 'trace' => $e->getTraceAsString(), + ]); + + return back()->withErrors(['error' => 'Gagal memuat dashboard aksi mitigasi.']); + } + } + + public function index(Request $request) + { + $year = (int) $request->input('mitigationYear', date('Y')); + + try { + $dashboardData = $this->mitigationService->getDashboardData($year); + + return view('auth.dashboard-mitigation', [ + 'title' => $this->title, + 'route' => $this->route, + 'selectedYear' => $dashboardData['mitigationYear'], + 'tableData' => $dashboardData['tableData'], + 'barData' => $dashboardData['barData'], + 'budgetData' => $dashboardData['budgetData'], + 'realisasiData' => $dashboardData['realisasiData'], + ]); + } catch(\Exception $e) { + Log::error('Error loading dashboard mitigation data', [ + 'message' => $e->getMessage(), + 'trace' => $e->getTraceAsString(), + ]); + + // optional: kasih feedback ke user biar nggak blank + return back()->withErrors(['error' => 'Gagal memuat dashboard mitigasi.']); + } + } +} diff --git a/app/Services/DashboardService.php.bak b/app/Services/DashboardService.php.bak deleted file mode 100644 index 6d381c6..0000000 --- a/app/Services/DashboardService.php.bak +++ /dev/null @@ -1,101 +0,0 @@ -getActiveGwpRecords()->keyBy('ghg_code'); - $years = activityYearRange($inventoryYear, false, 10); - - $crfData = ActivityCrf::with(['ws.form', 'sektor']) - ->where('inventory_year', $inventoryYear) - ->whereBetween('activity_year', [$years[0], end($years)]) - ->orderBy('ws_code', 'asc')->get(); - - $aggregatedData = $this->aggregateCrfData($crfData, $gwpData); - - return [ - 'cardData' => $this->calculateCardData($aggregatedData, $inventoryYear), - 'barData' => $this->calculateBarData($aggregatedData, $years), - 'pieData' => $this->calculatePieData($aggregatedData, $inventoryYear), - 'tableData' => $this->calculateTableData($aggregatedData, $inventoryYear), - 'inventoryYear' => $inventoryYear, - ]; - } - - private function aggregateCrfData(Collection $crfData, Collection $gwpData) - { - return $crfData->map(function ($data) use ($gwpData) { - - $sector = $data->sector; - $name = $sector === 'energy' ? $data->ws->form->name : $data->sektor->name; - - return [ - 'year' => $data->activity_year, - 'sector' => $sector, - 'name' => $name, - 'co2eq' => $data->co2eq, - 'co2' => $data->co2, - 'ch4' => $data->ch4, - 'n2o' => $data->n2o, - ]; - }); - } - - private function calculateCardData(Collection $aggregatedData, $inventoryYear) - { - $sectorSums = $aggregatedData->where('year', $inventoryYear - 1)->groupBy('sector')->map->sum('co2eq'); - - return [ - 'totalEmisi' => $aggregatedData->where('year', $inventoryYear - 1)->sum('co2eq'), - 'emisiEnergi' => $sectorSums->get('energy', 0), - 'emisiPertanian' => $sectorSums->get('agriculture', 0), - 'emisiLahan' => $sectorSums->get('folu', 0), - 'emisiLimbah' => $sectorSums->get('waste', 0), - ]; - } - - private function calculateBarData(Collection $aggregatedData, array $years) - { - return array_map(function ($year) use ($aggregatedData) { - $yearlyData = $aggregatedData->where('year', $year)->groupBy('name')->map->sum('co2eq'); - $total = $yearlyData->sum(); - - return [ - 'year' => $year, - 'total' => $total, - 'emisi' => $yearlyData, - ]; - }, $years); - } - - private function calculatePieData(Collection $aggregatedData, $inventoryYear) - { - return $aggregatedData->where('year', $inventoryYear - 1)->groupBy('name')->map->sum('co2eq'); - } - - private function calculateTableData(Collection $aggregatedData, $inventoryYear) - { - return $aggregatedData->where('year', $inventoryYear - 1)->groupBy('name')->map(function ($groupedData) { - return [ - 'co2eq' => $groupedData->sum('co2eq'), - 'co2' => $groupedData->sum('co2'), - 'ch4' => $groupedData->sum('ch4'), - 'n2o' => $groupedData->sum('n2o'), - ]; - }); - } - - private function getActiveGwpRecords() - { - return ReferenceGwp::whereHas('ar', function ($query) { - $query->isActive(); - })->get(); - } -} diff --git a/app/View/Components/AdaptationYearSelect.php b/app/View/Components/AdaptationYearSelect.php new file mode 100644 index 0000000..3a58d68 --- /dev/null +++ b/app/View/Components/AdaptationYearSelect.php @@ -0,0 +1,24 @@ +selectedYear = $selectedYear; + $this->name = $name; + } + + public function render(): View|Closure|string + { + return view('modules.components.adaptation-year-select'); + } +} diff --git a/app/View/Components/MitigationYearSelect.php b/app/View/Components/MitigationYearSelect.php new file mode 100644 index 0000000..e4844b0 --- /dev/null +++ b/app/View/Components/MitigationYearSelect.php @@ -0,0 +1,24 @@ +selectedYear = $selectedYear; + $this->name = $name; + } + + public function render(): View|Closure|string + { + return view('modules.components.mitigation-year-select'); + } +} diff --git a/resources/views/auth/dashboard-adaptation-aksi.blade.php b/resources/views/auth/dashboard-adaptation-aksi.blade.php new file mode 100644 index 0000000..fe36b2d --- /dev/null +++ b/resources/views/auth/dashboard-adaptation-aksi.blade.php @@ -0,0 +1,142 @@ +@extends('layouts.master') + +@section('title', 'Aksi Perubahan Iklim') + +@section('content') +
AKSI
+ +ALOKASI (Miliar Rupiah)
+ +REALISASI (Miliar Rupiah)
+ +AKSI
+ +ALOKASI (Miliar Rupiah)
+ +REALISASI (Miliar Rupiah)
+ +| Tipe Kegiatan | +Nama Kegiatan | +Tahun | +Sektor | +Sub-Sektor | +Kategori | +Penurunan Emisi | +Revisi | ++ |
|---|---|---|---|---|---|---|---|---|
| {{ $m->tipe_kegiatan }} | +{{ Str::limit($m->nama_kegiatan, 60) }} | +{{ $m->tahun_kegiatan }} | +{{ $m->sektor }} | +{{ $m->sub_sektor }} | +{{ $m->kategori_perhitungan }} | ++ {{ $m->emission_factor }} + | +{{ $m->revisi ?? '--' }} | ++ + + + + + + | +
+ {{ getFormattedValue(@$cardData['totalEmisi'], 2) }}
++ {{ getFormattedValue(@$cardData['emisiEnergi'], 2) }}
++ {{ getFormattedValue(@$cardData['emisiPertanian'], 2) }}
++ {{ getFormattedValue(@$cardData['emisiLahan'], 2) }}
++ {{ getFormattedValue(@$cardData['emisiLimbah'], 2) }}
+| + | Emissions | +CO2 Eq (Gg) |
+ CO2 (Gg) |
+ CH4 (Gg) |
+ N2O (Gg) |
+
|---|---|---|---|---|---|
| {{ $no++ }} | +{{ $sector }} | +{{ getFormattedValue($emissions['co2eq'], 2) }} | +{{ getFormattedValue($emissions['co2'], 2) }} | +{{ getFormattedValue($emissions['ch4'], 2) }} | +{{ getFormattedValue($emissions['n2o'], 2) }} | +
| + | TOTAL | +{{ getFormattedValue($totalCo2eq, 2) }} | +{{ getFormattedValue($totalCo2, 2) }} | +{{ getFormattedValue($totalCh4, 2) }} | +{{ getFormattedValue($totalN2o, 2) }} | +