From 0fabc7b28006ea6aa7e28cb9e6dab20684f8e225 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Mon, 22 Jun 2026 13:42:41 +0700 Subject: [PATCH] refactor: utilize query builder for LeaveRequest and EmployeeAdvance status checks across multiple services for improved consistency and readability --- .../Commands/ApplyAttendancePenalties.php | 3 ++- .../Admin/Hr/LeaveRequestController.php | 3 ++- app/Http/Middleware/HandleInertiaRequests.php | 12 ++++++---- .../Finance/EmployeeAdvanceService.php | 2 +- app/Services/Hr/AttendanceService.php | 3 ++- app/Services/Hr/LeaveRequestService.php | 3 ++- app/Services/System/DashboardService.php | 24 ++++++++++++------- 7 files changed, 33 insertions(+), 17 deletions(-) diff --git a/app/Console/Commands/ApplyAttendancePenalties.php b/app/Console/Commands/ApplyAttendancePenalties.php index 7e4753a..a4b6968 100644 --- a/app/Console/Commands/ApplyAttendancePenalties.php +++ b/app/Console/Commands/ApplyAttendancePenalties.php @@ -49,7 +49,8 @@ public function handle(): int $skippedDuplicate = 0; foreach ($employees as $employee) { - $hasLeave = LeaveRequest::approved() + $hasLeave = LeaveRequest::query() + ->approved() ->where('employee_id', $employee->id) ->whereDate('start_date', '<=', $date) ->whereDate('end_date', '>=', $date) diff --git a/app/Http/Controllers/Admin/Hr/LeaveRequestController.php b/app/Http/Controllers/Admin/Hr/LeaveRequestController.php index 1cc8bd9..8f00c50 100644 --- a/app/Http/Controllers/Admin/Hr/LeaveRequestController.php +++ b/app/Http/Controllers/Admin/Hr/LeaveRequestController.php @@ -30,7 +30,8 @@ public function index(Request $request): Response $hasPending = false; if ($user?->employee !== null) { - $hasPending = LeaveRequest::pending() + $hasPending = LeaveRequest::query() + ->pending() ->where('employee_id', $user->employee->id) ->exists(); } diff --git a/app/Http/Middleware/HandleInertiaRequests.php b/app/Http/Middleware/HandleInertiaRequests.php index ef0269b..2624718 100644 --- a/app/Http/Middleware/HandleInertiaRequests.php +++ b/app/Http/Middleware/HandleInertiaRequests.php @@ -82,7 +82,8 @@ private function pendingLeaveRequests(Request $request): int return 0; } - return LeaveRequest::pending() + return LeaveRequest::query() + ->pending() ->count(); } @@ -94,7 +95,8 @@ private function pendingEmployeeAdvances(Request $request): int return 0; } - return EmployeeAdvance::pending() + return EmployeeAdvance::query() + ->pending() ->count(); } @@ -106,7 +108,8 @@ private function pendingCuttings(Request $request): int return 0; } - return Cutting::completed() + return Cutting::query() + ->completed() ->count(); } @@ -118,7 +121,8 @@ private function pendingOwnerVerifications(Request $request): int return 0; } - return Cutting::pendingVerification() + return Cutting::query() + ->pendingVerification() ->count(); } } diff --git a/app/Services/Finance/EmployeeAdvanceService.php b/app/Services/Finance/EmployeeAdvanceService.php index 324319c..ebb6220 100644 --- a/app/Services/Finance/EmployeeAdvanceService.php +++ b/app/Services/Finance/EmployeeAdvanceService.php @@ -26,7 +26,7 @@ public function __construct( */ public function outstandingSummary(?User $user = null): array { - $query = EmployeeAdvance::approved() + $query = EmployeeAdvance::query()->approved() ->when(! $user->hasAnyRole([Role::OWNER->value, Role::DEVELOPER->value, Role::DIREKTUR->value]), fn (Builder $query) => $query->where('employee_id', $user->employee?->id ?? -1)); $outstandingAmount = (int) $query->sum('amount'); diff --git a/app/Services/Hr/AttendanceService.php b/app/Services/Hr/AttendanceService.php index a4aabd7..af90cff 100644 --- a/app/Services/Hr/AttendanceService.php +++ b/app/Services/Hr/AttendanceService.php @@ -60,7 +60,8 @@ public function todayAttendanceForEmployee(Employee $employee): ?array public function isOnLeaveToday(Employee $employee): bool { - return LeaveRequest::approved() + return LeaveRequest::query() + ->approved() ->where('employee_id', $employee->id) ->whereDate('start_date', '<=', today()) ->whereDate('end_date', '>=', today()) diff --git a/app/Services/Hr/LeaveRequestService.php b/app/Services/Hr/LeaveRequestService.php index 5d53fe1..941aaaa 100644 --- a/app/Services/Hr/LeaveRequestService.php +++ b/app/Services/Hr/LeaveRequestService.php @@ -55,7 +55,8 @@ public function create(array $validated, User $user): void { $employee = $this->resolveAuthenticatedEmployee($user); - $hasPending = LeaveRequest::pending() + $hasPending = LeaveRequest::query() + ->pending() ->where('employee_id', $employee->id) ->exists(); diff --git a/app/Services/System/DashboardService.php b/app/Services/System/DashboardService.php index 7bab1be..c5ac42a 100644 --- a/app/Services/System/DashboardService.php +++ b/app/Services/System/DashboardService.php @@ -133,7 +133,8 @@ public function getTopSuppliers(): array public function getTopCustomers(): array { - return Order::completed() + return Order::query() + ->completed() ->join('customers', 'orders.customer_id', '=', 'customers.id') ->selectRaw('customers.name, SUM(orders.total_amount) as total_amount, COUNT(orders.id) as order_count') ->groupBy('customers.id', 'customers.name') @@ -272,11 +273,13 @@ public function getOrderStats(): array public function getRevenueSummary(): array { - $revenueSummary = Order::completed() + $revenueSummary = Order::query() + ->completed() ->selectRaw('SUM(total_amount) as total_revenue, SUM(discount) as total_discount, SUM(shipping_cost) as total_shipping, COUNT(*) as total_orders, AVG(total_amount) as avg_order') ->first(); - $totalMarketplaceFees = Order::completed() + $totalMarketplaceFees = Order::query() + ->completed() ->whereNotNull('marketplace_settings_snapshot') ->get() ->sum(fn ($order) => (int) ($order->marketplace_settings_snapshot['total_fee_amount'] ?? 0)); @@ -294,7 +297,8 @@ public function getRevenueSummary(): array public function getMarketplaceSummary(): array { - $marketplaceOrders = Order::completed() + $marketplaceOrders = Order::query() + ->completed() ->whereIn('channel', [OrderChannel::SHOPEE, OrderChannel::TIKTOK]) ->whereNotNull('marketplace_settings_snapshot') ->get(); @@ -373,15 +377,18 @@ public function getMonthlyExpenses(Carbon $startOfMonth, Carbon $endOfMonth): ar public function getKasbonSummary(): array { - $pending = EmployeeAdvance::pending() + $pending = EmployeeAdvance::query() + ->pending() ->selectRaw('COUNT(*) as count, COALESCE(SUM(amount), 0) as total') ->first(); - $approved = EmployeeAdvance::approved() + $approved = EmployeeAdvance::query() + ->approved() ->selectRaw('COUNT(*) as count, COALESCE(SUM(amount), 0) as total') ->first(); - $paid = EmployeeAdvance::paid() + $paid = EmployeeAdvance::query() + ->paid() ->selectRaw('COUNT(*) as count, COALESCE(SUM(amount), 0) as total') ->first(); @@ -522,7 +529,8 @@ public function getMonthlyRevenueTrend(): array $start = Carbon::create($month['year'], $month['month'], 1)->startOfMonth(); $end = $start->copy()->endOfMonth(); - $monthlyRevenue = Order::completed() + $monthlyRevenue = Order::query() + ->completed() ->whereBetween('created_at', [$start, $end]) ->selectRaw('COALESCE(SUM(total_amount), 0) as total, COUNT(*) as count') ->first();