feat: update dashboard titles to reflect today's data for attendance, revenue, and expenses

This commit is contained in:
Yoga Pangestu 2026-08-08 12:41:52 +07:00
parent 515b2e9848
commit f7c13826eb
2 changed files with 37 additions and 45 deletions

View File

@ -18,52 +18,34 @@ class DashboardService
{
public function getAttendanceStats(): array
{
$now = Carbon::now();
$startOfMonth = $now->copy()->startOfMonth();
$endOfMonth = $now->copy()->endOfMonth();
$workingDays = 0;
$current = $startOfMonth->copy();
while ($current->lte($endOfMonth)) {
if (! in_array($current->dayOfWeek, [Carbon::SATURDAY, Carbon::SUNDAY])) {
$workingDays++;
}
$current->addDay();
}
$today = Carbon::now()->toDateString();
$totalEmployees = Employee::whereHas('user', fn ($q) => $q->where('is_active', true))->count();
$present = Attendance::whereYear('attendance_date', $now->year)
->whereMonth('attendance_date', $now->month)
$present = Attendance::where('attendance_date', $today)->count();
$onLeave = LeaveRequest::approved()
->where('start_date', '<=', $today)
->where('end_date', '>=', $today)
->count();
$leaveDays = LeaveRequest::approved()
->where('start_date', '<=', $endOfMonth)
->where('end_date', '>=', $startOfMonth)
->get()
->reduce(function ($carry, $leave) use ($startOfMonth, $endOfMonth) {
$leaveStart = max($leave->start_date->timestamp, $startOfMonth->timestamp);
$leaveEnd = min($leave->end_date->timestamp, $endOfMonth->timestamp);
$days = Carbon::createFromTimestamp($leaveStart)->diffInDays(Carbon::createFromTimestamp($leaveEnd)) + 1;
return $carry + max(0, $days);
}, 0);
$absent = max(0, $workingDays - $present - $leaveDays);
$absent = max(0, $totalEmployees - $present - $onLeave);
return [
'total_employees' => $totalEmployees,
'present' => $present,
'absent' => $absent,
'on_leave' => $leaveDays,
'percentage' => $workingDays > 0 ? round(($present / $workingDays) * 100) : 0,
'on_leave' => $onLeave,
'percentage' => $totalEmployees > 0 ? round(($present / $totalEmployees) * 100) : 0,
];
}
public function getRevenueSummary(): array
{
$today = Carbon::now()->toDateString();
$stats = Order::where('status', OrderStatus::COMPLETED)
->whereDate('created_at', $today)
->selectRaw('COUNT(*) as total_orders')
->selectRaw('COALESCE(SUM(total_amount), 0) as total_revenue')
->selectRaw('COALESCE(SUM(discount), 0) as total_discount')
@ -83,13 +65,20 @@ public function getRevenueSummary(): array
public function getExpenseSummary(): array
{
$expenses = Expense::selectRaw('COALESCE(SUM(amount), 0) as total')->first();
$today = Carbon::now()->toDateString();
$cashAdvanceTotal = EmployeeAdvance::where('status', 'paid')
$expenses = Expense::whereDate('created_at', $today)
->selectRaw('COALESCE(SUM(amount), 0) as total')
->first();
$expenseTotal = Expense::selectRaw('COALESCE(SUM(amount), 0) as total')->first();
$cashAdvanceTotal = EmployeeAdvance::where('status', 'paid')
->whereDate('created_at', $today)
->selectRaw('COALESCE(SUM(amount), 0) as total')
->first();
$expenseTotal = Expense::whereDate('created_at', $today)
->selectRaw('COALESCE(SUM(amount), 0) as total')
->first();
return [
'total' => (int) $expenses->total,
@ -101,7 +90,9 @@ public function getExpenseSummary(): array
public function getOrderStats(): array
{
$baseQuery = Order::where('status', OrderStatus::COMPLETED);
$today = Carbon::now()->toDateString();
$baseQuery = Order::where('status', OrderStatus::COMPLETED)
->whereDate('created_at', $today);
$byChannel = collect(OrderChannel::values())->map(function ($channel) use ($baseQuery) {
$count = (clone $baseQuery)->where('channel', $channel)->count();
@ -128,6 +119,7 @@ public function getOrderStats(): array
});
$byMarketing = Order::where('status', OrderStatus::COMPLETED)
->whereDate('created_at', $today)
->whereNotNull('marketing_id')
->select('marketing_id')
->selectRaw('COUNT(*) as count')

View File

@ -349,7 +349,7 @@ export default function Dashboard({
<div className={statsGridClass}>
{can('dashboard.attendance') && (
<StatCard
title="Kehadiran"
title="Kehadiran Hari Ini"
icon={UserCheck}
mainLabel="Total Karyawan"
mainValue={attendanceStats.total_employees}
@ -364,7 +364,7 @@ export default function Dashboard({
{can('dashboard.revenue') && (
<StatCard
title="Pendapatan"
title="Pendapatan Hari Ini"
icon={TrendingUp}
mainLabel="Total"
mainValue={`Rp${formatRupiah(revenueSummary.total_revenue)}`}
@ -388,7 +388,7 @@ export default function Dashboard({
{can('dashboard.expense') && (
<StatCard
title="Pengeluaran"
title="Pengeluaran Hari Ini"
icon={TrendingDown}
mainLabel="Total"
mainValue={`Rp${formatRupiah(expenseSummary.total)}`}
@ -410,8 +410,8 @@ export default function Dashboard({
{visibleChartsCount > 0 && (
<div className={chartsGridClass}>
{can('dashboard.orders_channel') && (
<DashboardDonutChart
title="Pesanan per Channel"
<DashboardDonutChart
title="Pesanan per Channel Hari Ini"
data={orderStats.by_channel.map((item, index) => ({
...item,
color: CHART_COLORS[index % CHART_COLORS.length],
@ -422,8 +422,8 @@ export default function Dashboard({
)}
{can('dashboard.orders_payment') && (
<DashboardDonutChart
title="Pesanan per Pembayaran"
<DashboardDonutChart
title="Pesanan per Pembayaran Hari Ini"
data={orderStats.by_payment_type.map((item, index) => ({
...item,
color: CHART_COLORS[index % CHART_COLORS.length],
@ -434,8 +434,8 @@ export default function Dashboard({
)}
{can('dashboard.orders_marketing') && (
<DashboardDonutChart
title="Pesanan per Marketing"
<DashboardDonutChart
title="Pesanan per Marketing Hari Ini"
data={orderStats.by_marketing.map((item, index) => ({
...item,
label: item.name,
@ -447,8 +447,8 @@ export default function Dashboard({
)}
{can('dashboard.orders_status') && (
<DashboardDonutChart
title="Pesanan per Status"
<DashboardDonutChart
title="Pesanan per Status Hari Ini"
data={orderStats.by_status.map((item, index) => ({
...item,
color: CHART_COLORS[index % CHART_COLORS.length],