feat: update dashboard titles to reflect today's data for attendance, revenue, and expenses
This commit is contained in:
parent
515b2e9848
commit
f7c13826eb
@ -18,52 +18,34 @@ class DashboardService
|
|||||||
{
|
{
|
||||||
public function getAttendanceStats(): array
|
public function getAttendanceStats(): array
|
||||||
{
|
{
|
||||||
$now = Carbon::now();
|
$today = Carbon::now()->toDateString();
|
||||||
$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();
|
|
||||||
}
|
|
||||||
|
|
||||||
$totalEmployees = Employee::whereHas('user', fn ($q) => $q->where('is_active', true))->count();
|
$totalEmployees = Employee::whereHas('user', fn ($q) => $q->where('is_active', true))->count();
|
||||||
|
|
||||||
$present = Attendance::whereYear('attendance_date', $now->year)
|
$present = Attendance::where('attendance_date', $today)->count();
|
||||||
->whereMonth('attendance_date', $now->month)
|
|
||||||
|
$onLeave = LeaveRequest::approved()
|
||||||
|
->where('start_date', '<=', $today)
|
||||||
|
->where('end_date', '>=', $today)
|
||||||
->count();
|
->count();
|
||||||
|
|
||||||
$leaveDays = LeaveRequest::approved()
|
$absent = max(0, $totalEmployees - $present - $onLeave);
|
||||||
->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);
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'total_employees' => $totalEmployees,
|
'total_employees' => $totalEmployees,
|
||||||
'present' => $present,
|
'present' => $present,
|
||||||
'absent' => $absent,
|
'absent' => $absent,
|
||||||
'on_leave' => $leaveDays,
|
'on_leave' => $onLeave,
|
||||||
'percentage' => $workingDays > 0 ? round(($present / $workingDays) * 100) : 0,
|
'percentage' => $totalEmployees > 0 ? round(($present / $totalEmployees) * 100) : 0,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getRevenueSummary(): array
|
public function getRevenueSummary(): array
|
||||||
{
|
{
|
||||||
|
$today = Carbon::now()->toDateString();
|
||||||
|
|
||||||
$stats = Order::where('status', OrderStatus::COMPLETED)
|
$stats = Order::where('status', OrderStatus::COMPLETED)
|
||||||
|
->whereDate('created_at', $today)
|
||||||
->selectRaw('COUNT(*) as total_orders')
|
->selectRaw('COUNT(*) as total_orders')
|
||||||
->selectRaw('COALESCE(SUM(total_amount), 0) as total_revenue')
|
->selectRaw('COALESCE(SUM(total_amount), 0) as total_revenue')
|
||||||
->selectRaw('COALESCE(SUM(discount), 0) as total_discount')
|
->selectRaw('COALESCE(SUM(discount), 0) as total_discount')
|
||||||
@ -83,13 +65,20 @@ public function getRevenueSummary(): array
|
|||||||
|
|
||||||
public function getExpenseSummary(): 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')
|
->selectRaw('COALESCE(SUM(amount), 0) as total')
|
||||||
->first();
|
->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 [
|
return [
|
||||||
'total' => (int) $expenses->total,
|
'total' => (int) $expenses->total,
|
||||||
@ -101,7 +90,9 @@ public function getExpenseSummary(): array
|
|||||||
|
|
||||||
public function getOrderStats(): 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) {
|
$byChannel = collect(OrderChannel::values())->map(function ($channel) use ($baseQuery) {
|
||||||
$count = (clone $baseQuery)->where('channel', $channel)->count();
|
$count = (clone $baseQuery)->where('channel', $channel)->count();
|
||||||
@ -128,6 +119,7 @@ public function getOrderStats(): array
|
|||||||
});
|
});
|
||||||
|
|
||||||
$byMarketing = Order::where('status', OrderStatus::COMPLETED)
|
$byMarketing = Order::where('status', OrderStatus::COMPLETED)
|
||||||
|
->whereDate('created_at', $today)
|
||||||
->whereNotNull('marketing_id')
|
->whereNotNull('marketing_id')
|
||||||
->select('marketing_id')
|
->select('marketing_id')
|
||||||
->selectRaw('COUNT(*) as count')
|
->selectRaw('COUNT(*) as count')
|
||||||
|
|||||||
@ -349,7 +349,7 @@ export default function Dashboard({
|
|||||||
<div className={statsGridClass}>
|
<div className={statsGridClass}>
|
||||||
{can('dashboard.attendance') && (
|
{can('dashboard.attendance') && (
|
||||||
<StatCard
|
<StatCard
|
||||||
title="Kehadiran"
|
title="Kehadiran Hari Ini"
|
||||||
icon={UserCheck}
|
icon={UserCheck}
|
||||||
mainLabel="Total Karyawan"
|
mainLabel="Total Karyawan"
|
||||||
mainValue={attendanceStats.total_employees}
|
mainValue={attendanceStats.total_employees}
|
||||||
@ -364,7 +364,7 @@ export default function Dashboard({
|
|||||||
|
|
||||||
{can('dashboard.revenue') && (
|
{can('dashboard.revenue') && (
|
||||||
<StatCard
|
<StatCard
|
||||||
title="Pendapatan"
|
title="Pendapatan Hari Ini"
|
||||||
icon={TrendingUp}
|
icon={TrendingUp}
|
||||||
mainLabel="Total"
|
mainLabel="Total"
|
||||||
mainValue={`Rp${formatRupiah(revenueSummary.total_revenue)}`}
|
mainValue={`Rp${formatRupiah(revenueSummary.total_revenue)}`}
|
||||||
@ -388,7 +388,7 @@ export default function Dashboard({
|
|||||||
|
|
||||||
{can('dashboard.expense') && (
|
{can('dashboard.expense') && (
|
||||||
<StatCard
|
<StatCard
|
||||||
title="Pengeluaran"
|
title="Pengeluaran Hari Ini"
|
||||||
icon={TrendingDown}
|
icon={TrendingDown}
|
||||||
mainLabel="Total"
|
mainLabel="Total"
|
||||||
mainValue={`Rp${formatRupiah(expenseSummary.total)}`}
|
mainValue={`Rp${formatRupiah(expenseSummary.total)}`}
|
||||||
@ -411,7 +411,7 @@ export default function Dashboard({
|
|||||||
<div className={chartsGridClass}>
|
<div className={chartsGridClass}>
|
||||||
{can('dashboard.orders_channel') && (
|
{can('dashboard.orders_channel') && (
|
||||||
<DashboardDonutChart
|
<DashboardDonutChart
|
||||||
title="Pesanan per Channel"
|
title="Pesanan per Channel Hari Ini"
|
||||||
data={orderStats.by_channel.map((item, index) => ({
|
data={orderStats.by_channel.map((item, index) => ({
|
||||||
...item,
|
...item,
|
||||||
color: CHART_COLORS[index % CHART_COLORS.length],
|
color: CHART_COLORS[index % CHART_COLORS.length],
|
||||||
@ -423,7 +423,7 @@ export default function Dashboard({
|
|||||||
|
|
||||||
{can('dashboard.orders_payment') && (
|
{can('dashboard.orders_payment') && (
|
||||||
<DashboardDonutChart
|
<DashboardDonutChart
|
||||||
title="Pesanan per Pembayaran"
|
title="Pesanan per Pembayaran Hari Ini"
|
||||||
data={orderStats.by_payment_type.map((item, index) => ({
|
data={orderStats.by_payment_type.map((item, index) => ({
|
||||||
...item,
|
...item,
|
||||||
color: CHART_COLORS[index % CHART_COLORS.length],
|
color: CHART_COLORS[index % CHART_COLORS.length],
|
||||||
@ -435,7 +435,7 @@ export default function Dashboard({
|
|||||||
|
|
||||||
{can('dashboard.orders_marketing') && (
|
{can('dashboard.orders_marketing') && (
|
||||||
<DashboardDonutChart
|
<DashboardDonutChart
|
||||||
title="Pesanan per Marketing"
|
title="Pesanan per Marketing Hari Ini"
|
||||||
data={orderStats.by_marketing.map((item, index) => ({
|
data={orderStats.by_marketing.map((item, index) => ({
|
||||||
...item,
|
...item,
|
||||||
label: item.name,
|
label: item.name,
|
||||||
@ -448,7 +448,7 @@ export default function Dashboard({
|
|||||||
|
|
||||||
{can('dashboard.orders_status') && (
|
{can('dashboard.orders_status') && (
|
||||||
<DashboardDonutChart
|
<DashboardDonutChart
|
||||||
title="Pesanan per Status"
|
title="Pesanan per Status Hari Ini"
|
||||||
data={orderStats.by_status.map((item, index) => ({
|
data={orderStats.by_status.map((item, index) => ({
|
||||||
...item,
|
...item,
|
||||||
color: CHART_COLORS[index % CHART_COLORS.length],
|
color: CHART_COLORS[index % CHART_COLORS.length],
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user