dress/app/Http/Controllers/DashboardController.php

101 lines
3.9 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_sales' => $this->getStats($today, $yesterday, fn ($date) => Order::whereDate('created_at', $date)->count()),
'total_revenue' => $this->getStats($today, $yesterday, fn ($date) => Order::whereDate('created_at', $date)->sum('total')),
'total_expenses' => $this->getStats($today, $yesterday, fn ($date) => Expense::whereDate('created_at', $date)->sum('amount')),
'cogs' => $this->getStats($today, $yesterday, fn ($date) => Order::whereDate('created_at', $date)->sum('hpp')),
'total_discount' => $this->getStats($today, $yesterday, fn ($date) => Order::whereDate('created_at', $date)->sum('discount')),
'total_purchases' => $this->getStats($today, $yesterday, fn ($date) => Purchase::whereDate('created_at', $date)->sum('total')),
'products_sold' => $this->getStats($today, $yesterday, fn ($date) => OrderItem::whereHas('order', fn ($q) => $q->whereDate('created_at', $date))->sum('qty')),
'total_customers' => $this->getStats($today, $yesterday, fn ($date) => Order::whereDate('created_at', $date)->distinct('customer_name')->count('customer_name')),
];
$stats['aov'] = $this->calculateAov($stats['total_revenue'], $stats['total_sales']);
$stats['gross_profit'] = $this->calculateDiff($stats['total_revenue'], $stats['cogs']);
$stats['net_profit'] = $this->calculateDiff($stats['gross_profit'], $stats['total_expenses']);
$stats['profit_margin'] = $this->calculateMargin($stats['net_profit'], $stats['total_revenue']);
return Inertia::render('dashboard', [
'stats' => $stats,
]);
}
private function getStats($today, $yesterday, $callback): array
{
$todayVal = $callback($today);
$yesterdayVal = $callback($yesterday);
$diff = $todayVal - $yesterdayVal;
$change = $this->calculatePercentageChange($todayVal, $yesterdayVal);
return [
'value' => $todayVal,
'yesterday' => $yesterdayVal,
'diff' => $diff,
'change' => $change,
];
}
private function calculatePercentageChange($current, $previous): ?string
{
if ($previous == 0) {
return $current == 0 ? 0 : null;
}
return round((($current - $previous) / abs($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): array
{
$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): array
{
$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),
];
}
}