diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 1e0a239..9f828fb 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -79,15 +79,21 @@ private function calculateBalance($previousBalance, $debit, $kredit, $accountTyp public function index(Request $request) { - // Default date range - $startDate = $request->input('start_date', Carbon::now()->startOfMonth()->format('Y-m-d')); - $endDate = $request->input('end_date', Carbon::now()->endOfMonth()->format('Y-m-d')); + // Default date range hanya jika ada parameter filter + $startDate = $request->input('start_date'); + $endDate = $request->input('end_date'); try { - // Ambil semua transaksi dalam periode - $transaksis = DB::table('laporan_transaksis') - ->whereBetween('Tanggal', [$startDate, $endDate]) - ->get(); + // Query dasar + $query = DB::table('laporan_transaksis'); + + // Terapkan filter tanggal hanya jika ada parameter + if ($startDate && $endDate) { + $query->whereBetween('Tanggal', [$startDate, $endDate]); + } + + // Ambil semua transaksi + $transaksis = $query->get(); // Array untuk menyimpan total per kategori $totalsPerAkun = []; @@ -330,4 +336,252 @@ public function filter(Request $request) { return $this->index($request); } + + public function showAll() + { + try { + // Set tanggal default untuk semua data + $startDate = '1970-01-01'; // Tanggal awal yang sangat lama + $endDate = '2099-12-31'; // Tanggal akhir yang sangat jauh + + // Query dasar tanpa filter tanggal + $query = DB::table('laporan_transaksis'); + + // Ambil semua transaksi + $transaksis = $query->get(); + + // Array untuk menyimpan total per kategori + $totalsPerAkun = []; + $totalPendapatan = 0; + $totalBeban = 0; + + // Fungsi untuk menambahkan atau memperbarui total per akun + $processAkun = function($kode, $kategori, $debit, $kredit) use (&$totalsPerAkun) { + if (!empty($kode) && !empty($kategori)) { + if (!isset($totalsPerAkun[$kategori])) { + $totalsPerAkun[$kategori] = [ + 'kode' => $kode, + 'kategori' => $kategori, + 'debit' => 0, + 'kredit' => 0 + ]; + } + $totalsPerAkun[$kategori]['debit'] += floatval($debit ?? 0); + $totalsPerAkun[$kategori]['kredit'] += floatval($kredit ?? 0); + } + }; + + foreach ($transaksis as $transaksi) { + // Proses untuk semua kategori + for ($i = 1; $i <= 5; $i++) { + $kategoriField = $i === 1 ? 'kategori' : "kategori{$i}"; + $uangMasukField = $i === 1 ? 'uang_masuk' : "uang_masuk{$i}"; + $uangKeluarField = $i === 1 ? 'uang_keluar' : "uang_keluar{$i}"; + + if (!empty($transaksi->$kategoriField)) { + $kodeAkun = $this->getKodeAkun($transaksi->$kategoriField); + if ($kodeAkun) { + $processAkun( + $kodeAkun, + $transaksi->$kategoriField, + $transaksi->$uangMasukField, + $transaksi->$uangKeluarField + ); + + // Hitung total berdasarkan jenis akun + $accountType = $this->getAccountType($kodeAkun); + if ($accountType === 'PENDAPATAN') { + $totalPendapatan = $this->calculateBalance( + $totalPendapatan, + floatval($transaksi->$uangMasukField ?? 0), + floatval($transaksi->$uangKeluarField ?? 0), + 'PENDAPATAN' + ); + } elseif ($accountType === 'BEBAN') { + $totalBeban = $this->calculateBalance( + $totalBeban, + floatval($transaksi->$uangMasukField ?? 0), + floatval($transaksi->$uangKeluarField ?? 0), + 'BEBAN' + ); + } + } + } + } + } + + // Hitung laba rugi + $labaRugiTotal = $totalPendapatan - $totalBeban; + $labaRugiBulanIni = $labaRugiTotal; + + // Kelompokkan data untuk tampilan + $pendapatan = []; + $beban = []; + foreach ($totalsPerAkun as $kategori => $data) { + $accountType = $this->getAccountType($data['kode']); + if ($accountType === 'PENDAPATAN') { + $pendapatan[$kategori] = [ + 'kategori' => $kategori, + 'kode_akun' => $data['kode'], + 'nominal' => $this->calculateBalance(0, $data['debit'], $data['kredit'], 'PENDAPATAN') + ]; + } elseif ($accountType === 'BEBAN') { + $beban[$kategori] = [ + 'kategori' => $kategori, + 'kode_akun' => $data['kode'], + 'nominal' => $this->calculateBalance(0, $data['debit'], $data['kredit'], 'BEBAN') + ]; + } + } + + // Get daily totals for chart + $monthlyTotals = DB::table('laporan_transaksis') + ->select( + DB::raw('DATE_FORMAT(Tanggal, "%Y-%m-%d") as periode'), + DB::raw('SUM(uang_masuk) as total_debit'), + DB::raw('SUM(uang_keluar) as total_kredit') + ) + ->groupBy('periode') + ->orderBy('periode') + ->get(); + + // Debug untuk memastikan data terisi + Log::info('Daily Totals:', ['data' => $monthlyTotals->toArray()]); + + // Get category totals for distribution chart + $categoryTotals = DB::table('laporan_transaksis') + ->select( + 'kategori', + DB::raw('SUM(CASE WHEN LEFT(kategori, 3) IN ("241", "242") THEN uang_masuk ELSE 0 END) as total_debit'), + DB::raw('SUM(CASE WHEN LEFT(kategori, 3) IN ("251", "252") THEN uang_keluar ELSE 0 END) as total_kredit') + ) + ->whereNotNull('kategori') + ->groupBy('kategori') + ->get(); + + // Get recent transactions + $recentTransactions = DB::table('laporan_transaksis') + ->orderBy('Tanggal', 'desc') + ->limit(10) + ->get(); + + // Calculate growth percentage + $lastMonthStart = Carbon::parse($startDate)->subMonth()->startOfMonth(); + $lastMonthEnd = Carbon::parse($startDate)->subMonth()->endOfMonth(); + + $lastMonthData = DB::table('laporan_transaksis') + ->whereBetween('Tanggal', [$lastMonthStart, $lastMonthEnd]) + ->get(); + + $lastMonthPendapatan = 0; + $lastMonthBeban = 0; + + foreach ($lastMonthData as $transaksi) { + $kodeAkun = $this->getKodeAkun($transaksi->kategori); + if ($kodeAkun) { + $accountType = $this->getAccountType($kodeAkun); + if ($accountType === 'PENDAPATAN') { + $lastMonthPendapatan = $this->calculateBalance( + $lastMonthPendapatan, + floatval($transaksi->uang_masuk ?? 0), + floatval($transaksi->uang_keluar ?? 0), + 'PENDAPATAN' + ); + } elseif ($accountType === 'BEBAN') { + $lastMonthBeban = $this->calculateBalance( + $lastMonthBeban, + floatval($transaksi->uang_masuk ?? 0), + floatval($transaksi->uang_keluar ?? 0), + 'BEBAN' + ); + } + } + } + + $lastMonthLabaRugi = $lastMonthPendapatan - $lastMonthBeban; + $currentMonthLabaRugi = $labaRugiBulanIni; + + $growthPercentage = $lastMonthLabaRugi != 0 ? + (($currentMonthLabaRugi - $lastMonthLabaRugi) / abs($lastMonthLabaRugi)) * 100 : + 100; + + // Ambil data neraca saldo untuk pie chart + $rawTransaksis = DB::table('laporan_transaksis') + ->whereBetween('Tanggal', [$startDate, $endDate]) + ->orderBy('kode', 'asc') + ->get(); + $totalsPerAkun = []; + foreach ($rawTransaksis as $transaksi) { + $processAkun = function($kode, $kategori, $debit, $kredit) use (&$totalsPerAkun) { + if (!empty($kode) && !empty($kategori)) { + if (!isset($totalsPerAkun[$kategori])) { + $totalsPerAkun[$kategori] = [ + 'kode' => $kode, + 'kategori' => $kategori, + 'debit' => 0, + 'kredit' => 0 + ]; + } + $totalsPerAkun[$kategori]['debit'] += floatval($debit ?? 0); + $totalsPerAkun[$kategori]['kredit'] += floatval($kredit ?? 0); + } + }; + $processAkun($transaksi->kode, $transaksi->kategori, $transaksi->uang_masuk, $transaksi->uang_keluar); + $processAkun($transaksi->kode2, $transaksi->kategori2, $transaksi->uang_masuk2, $transaksi->uang_keluar2); + $processAkun($transaksi->kode3, $transaksi->kategori3, $transaksi->uang_masuk3, $transaksi->uang_keluar3); + $processAkun($transaksi->kode4, $transaksi->kategori4, $transaksi->uang_masuk4, $transaksi->uang_keluar4); + $processAkun($transaksi->kode5, $transaksi->kategori5, $transaksi->uang_masuk5, $transaksi->uang_keluar5); + } + $finalTransaksis = []; + foreach ($totalsPerAkun as $kategori => $data) { + $kodeAwal = substr($data['kode'], 0, 3); + $saldo = $data['debit'] - $data['kredit']; + if (in_array($kodeAwal, ['111', '112']) || in_array($kodeAwal, ['251', '252'])) { + if ($saldo != 0) { + $finalTransaksis[] = [ + 'kode' => $data['kode'], + 'kategori' => $kategori, + 'debit' => $saldo, + 'kredit' => 0 + ]; + } + } else { + if ($saldo != 0) { + $finalTransaksis[] = [ + 'kode' => $data['kode'], + 'kategori' => $kategori, + 'debit' => 0, + 'kredit' => -$saldo + ]; + } + } + } + $neracaTransaksis = collect($finalTransaksis); + $pieLabels = $neracaTransaksis->pluck('kategori')->toArray(); + $pieData = $neracaTransaksis->map(function($item) { + return abs($item['debit']) + abs($item['kredit']); + })->toArray(); + + return view('home', compact( + 'startDate', + 'endDate', + 'totalPendapatan', + 'totalBeban', + 'labaRugiTotal', + 'labaRugiBulanIni', + 'growthPercentage', + 'monthlyTotals', + 'categoryTotals', + 'recentTransactions', + 'pendapatan', + 'beban', + 'pieLabels', + 'pieData' + )); + + } catch (\Exception $e) { + Log::error("Error in HomeController: " . $e->getMessage()); + throw $e; + } + } } \ No newline at end of file diff --git a/resources/views/Home.blade.php b/resources/views/Home.blade.php index 885f928..c6ca9b8 100644 --- a/resources/views/Home.blade.php +++ b/resources/views/Home.blade.php @@ -17,19 +17,29 @@

Dashboard Keuangan

-
+
+
- Tanggal Awal - +
-
- Tanggal Akhir - -
- - +
+
+ Tanggal Awal + +
+
+ Tanggal Akhir + +
+ +
+
@@ -546,6 +556,21 @@ function updateChartType(type) { window.trendChart.update(); } } + +// Script untuk menangani toggle switch +document.addEventListener('DOMContentLoaded', function() { + const showAllSwitch = document.getElementById('showAllSwitch'); + + showAllSwitch.addEventListener('change', function() { + if (this.checked) { + // Redirect ke halaman tampilkan semua + window.location.href = "{{ route('home.all') }}"; + } else { + // Redirect ke halaman dengan filter tanggal default + window.location.href = "{{ route('home.filter', ['start_date' => date('Y-m-d'), 'end_date' => date('Y-m-d')]) }}"; + } + }); +}); @endsection \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index 9642e41..2c52d70 100644 --- a/routes/web.php +++ b/routes/web.php @@ -54,6 +54,7 @@ Route::delete('/laporan/{id}', [LaporanController::class, 'destroy'])->name('laporan.destroy'); Route::get('/home', [HomeController::class, 'index'])->name('home'); Route::get('/home/filter', [HomeController::class, 'filter'])->name('home.filter'); + Route::get('/home/all', [HomeController::class, 'showAll'])->name('home.all'); Route::get('/User', [UserController::class, 'index'])->name('User.index'); Route::get('/User/create', [UserController::class, 'create'])->name('User.create'); Route::post('/User', [UserController::class, 'store'])->name('User.store');