diff --git a/app/Services/DashboardService.php b/app/Services/DashboardService.php index 369f5de..2f19ffb 100644 --- a/app/Services/DashboardService.php +++ b/app/Services/DashboardService.php @@ -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') diff --git a/resources/js/pages/dashboard.tsx b/resources/js/pages/dashboard.tsx index 33fb946..5f6060c 100644 --- a/resources/js/pages/dashboard.tsx +++ b/resources/js/pages/dashboard.tsx @@ -349,7 +349,7 @@ export default function Dashboard({
{can('dashboard.attendance') && ( 0 && (
{can('dashboard.orders_channel') && ( - ({ ...item, color: CHART_COLORS[index % CHART_COLORS.length], @@ -422,8 +422,8 @@ export default function Dashboard({ )} {can('dashboard.orders_payment') && ( - ({ ...item, color: CHART_COLORS[index % CHART_COLORS.length], @@ -434,8 +434,8 @@ export default function Dashboard({ )} {can('dashboard.orders_marketing') && ( - ({ ...item, label: item.name, @@ -447,8 +447,8 @@ export default function Dashboard({ )} {can('dashboard.orders_status') && ( - ({ ...item, color: CHART_COLORS[index % CHART_COLORS.length],