98 lines
3.8 KiB
PHP
98 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Expense;
|
|
use App\Models\Order;
|
|
use App\Models\OrderItem;
|
|
use App\Models\Purchase;
|
|
use Carbon\Carbon;
|
|
use Inertia\Inertia;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
public function __invoke()
|
|
{
|
|
$today = Carbon::today();
|
|
$yesterday = Carbon::yesterday();
|
|
|
|
$stats = [
|
|
'total_penjualan' => $this->getStats($today, $yesterday, fn ($date) => Order::whereDate('created_at', $date)->count()),
|
|
'total_pendapatan' => $this->getStats($today, $yesterday, fn ($date) => Order::whereDate('created_at', $date)->sum('total')),
|
|
'total_pengeluaran' => $this->getStats($today, $yesterday, fn ($date) => Expense::whereDate('created_at', $date)->sum('amount')),
|
|
'hpp' => $this->getStats($today, $yesterday, fn ($date) => Order::whereDate('created_at', $date)->sum('hpp')),
|
|
'total_diskon' => $this->getStats($today, $yesterday, fn ($date) => Order::whereDate('created_at', $date)->sum('discount')),
|
|
'total_pembelian' => $this->getStats($today, $yesterday, fn ($date) => Purchase::whereDate('created_at', $date)->sum('total')),
|
|
'produk_terjual' => $this->getStats($today, $yesterday, fn ($date) => OrderItem::whereHas('order', fn ($q) => $q->whereDate('created_at', $date))->sum('qty')),
|
|
'total_pelanggan' => $this->getStats($today, $yesterday, fn ($date) => Order::whereDate('created_at', $date)->distinct('customer_name')->count('customer_name')),
|
|
];
|
|
|
|
// Complex stats
|
|
$stats['aov'] = $this->calculateAov($stats['total_pendapatan'], $stats['total_penjualan']);
|
|
$stats['laba_kotor'] = $this->calculateDiff($stats['total_pendapatan'], $stats['hpp']);
|
|
$stats['laba_bersih'] = $this->calculateDiff($stats['laba_kotor'], $stats['total_pengeluaran']);
|
|
$stats['profit_margin'] = $this->calculateMargin($stats['laba_bersih'], $stats['total_pendapatan']);
|
|
|
|
return Inertia::render('dashboard', [
|
|
'stats' => $stats,
|
|
]);
|
|
}
|
|
|
|
private function getStats($today, $yesterday, $callback)
|
|
{
|
|
$todayVal = $callback($today);
|
|
$yesterdayVal = $callback($yesterday);
|
|
|
|
return [
|
|
'value' => $todayVal,
|
|
'yesterday' => $yesterdayVal,
|
|
'change' => $this->calculatePercentageChange($todayVal, $yesterdayVal),
|
|
];
|
|
}
|
|
|
|
private function calculatePercentageChange($current, $previous)
|
|
{
|
|
if ($previous == 0) {
|
|
return $current > 0 ? 100 : 0;
|
|
}
|
|
|
|
return round((($current - $previous) / $previous) * 100, 2);
|
|
}
|
|
|
|
private function calculateAov($revenue, $sales)
|
|
{
|
|
$todayVal = $sales['value'] > 0 ? $revenue['value'] / $sales['value'] : 0;
|
|
$yesterdayVal = $sales['yesterday'] > 0 ? $revenue['yesterday'] / $sales['yesterday'] : 0;
|
|
|
|
return [
|
|
'value' => $todayVal,
|
|
'yesterday' => $yesterdayVal,
|
|
'change' => $this->calculatePercentageChange($todayVal, $yesterdayVal),
|
|
];
|
|
}
|
|
|
|
private function calculateDiff($a, $b)
|
|
{
|
|
$todayVal = $a['value'] - $b['value'];
|
|
$yesterdayVal = $a['yesterday'] - $b['yesterday'];
|
|
|
|
return [
|
|
'value' => $todayVal,
|
|
'yesterday' => $yesterdayVal,
|
|
'change' => $this->calculatePercentageChange($todayVal, $yesterdayVal),
|
|
];
|
|
}
|
|
|
|
private function calculateMargin($profit, $revenue)
|
|
{
|
|
$todayVal = $revenue['value'] > 0 ? ($profit['value'] / $revenue['value']) * 100 : 0;
|
|
$yesterdayVal = $revenue['yesterday'] > 0 ? ($profit['yesterday'] / $revenue['yesterday']) * 100 : 0;
|
|
|
|
return [
|
|
'value' => round($todayVal, 2),
|
|
'yesterday' => round($yesterdayVal, 2),
|
|
'change' => $this->calculatePercentageChange($todayVal, $yesterdayVal),
|
|
];
|
|
}
|
|
}
|