update tgl 2 juni
This commit is contained in:
parent
d49e890dc3
commit
c29be2538a
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -17,20 +17,30 @@
|
|||
<!-- Filter Section -->
|
||||
<div class="flex flex-wrap items-center gap-4 mb-6">
|
||||
<h2 class="text-xl font-bold text-indigo-700 dark:text-white">Dashboard Keuangan</h2>
|
||||
<form action="{{ route('home.filter') }}" method="GET" class="flex flex-wrap items-center gap-4 sm:ml-auto">
|
||||
<div class="flex flex-wrap items-center gap-4 sm:ml-auto">
|
||||
<!-- Switch Toggle -->
|
||||
<div class="flex items-center gap-2">
|
||||
<label class="relative inline-flex items-center cursor-pointer">
|
||||
<input type="checkbox" id="showAllSwitch" class="sr-only peer" {{ !request('start_date') && !request('end_date') ? 'checked' : '' }}>
|
||||
<div class="w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-indigo-300 dark:peer-focus:ring-indigo-800 rounded-full peer dark:bg-gray-700 peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-indigo-600"></div>
|
||||
<span class="ml-3 text-sm font-medium text-gray-600 dark:text-gray-300">Tampilkan Semua</span>
|
||||
</label>
|
||||
</div>
|
||||
<form action="{{ route('home.filter') }}" method="GET" class="flex flex-wrap items-center gap-4">
|
||||
<div class="flex items-center gap-2">
|
||||
<span class="text-sm text-gray-600">Tanggal Awal</span>
|
||||
<input type="date" name="start_date" class="form-input box bg-white border border-indigo-200 rounded-lg text-gray-600" value="{{ $startDate }}">
|
||||
<input type="date" name="start_date" class="form-input box bg-white border border-indigo-200 rounded-lg text-gray-600" value="{{ request('start_date', $startDate ?? '') }}">
|
||||
</div>
|
||||
<div class="flex items-center gap-2">
|
||||
<span class="text-sm text-gray-600">Tanggal Akhir</span>
|
||||
<input type="date" name="end_date" class="form-input box bg-white border border-indigo-200 rounded-lg text-gray-600" value="{{ $endDate }}">
|
||||
<input type="date" name="end_date" class="form-input box bg-white border border-indigo-200 rounded-lg text-gray-600" value="{{ request('end_date', $endDate ?? '') }}">
|
||||
</div>
|
||||
<button type="submit" class="btn bg-indigo-500 text-white px-4 py-2 rounded-lg hover:bg-indigo-600">
|
||||
Filter
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Main Metrics -->
|
||||
<div class="grid grid-cols-2 gap-4 mb-6">
|
||||
|
@ -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')]) }}";
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
@endsection
|
|
@ -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');
|
||||
|
|
Loading…
Reference in New Issue