refactor: utilize query builder for LeaveRequest and EmployeeAdvance status checks across multiple services for improved consistency and readability

This commit is contained in:
Yoga Pangestu 2026-06-22 13:42:41 +07:00
parent 6d6de5fa4b
commit 0fabc7b280
7 changed files with 33 additions and 17 deletions

View File

@ -49,7 +49,8 @@ public function handle(): int
$skippedDuplicate = 0; $skippedDuplicate = 0;
foreach ($employees as $employee) { foreach ($employees as $employee) {
$hasLeave = LeaveRequest::approved() $hasLeave = LeaveRequest::query()
->approved()
->where('employee_id', $employee->id) ->where('employee_id', $employee->id)
->whereDate('start_date', '<=', $date) ->whereDate('start_date', '<=', $date)
->whereDate('end_date', '>=', $date) ->whereDate('end_date', '>=', $date)

View File

@ -30,7 +30,8 @@ public function index(Request $request): Response
$hasPending = false; $hasPending = false;
if ($user?->employee !== null) { if ($user?->employee !== null) {
$hasPending = LeaveRequest::pending() $hasPending = LeaveRequest::query()
->pending()
->where('employee_id', $user->employee->id) ->where('employee_id', $user->employee->id)
->exists(); ->exists();
} }

View File

@ -82,7 +82,8 @@ private function pendingLeaveRequests(Request $request): int
return 0; return 0;
} }
return LeaveRequest::pending() return LeaveRequest::query()
->pending()
->count(); ->count();
} }
@ -94,7 +95,8 @@ private function pendingEmployeeAdvances(Request $request): int
return 0; return 0;
} }
return EmployeeAdvance::pending() return EmployeeAdvance::query()
->pending()
->count(); ->count();
} }
@ -106,7 +108,8 @@ private function pendingCuttings(Request $request): int
return 0; return 0;
} }
return Cutting::completed() return Cutting::query()
->completed()
->count(); ->count();
} }
@ -118,7 +121,8 @@ private function pendingOwnerVerifications(Request $request): int
return 0; return 0;
} }
return Cutting::pendingVerification() return Cutting::query()
->pendingVerification()
->count(); ->count();
} }
} }

View File

@ -26,7 +26,7 @@ public function __construct(
*/ */
public function outstandingSummary(?User $user = null): array 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)); ->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'); $outstandingAmount = (int) $query->sum('amount');

View File

@ -60,7 +60,8 @@ public function todayAttendanceForEmployee(Employee $employee): ?array
public function isOnLeaveToday(Employee $employee): bool public function isOnLeaveToday(Employee $employee): bool
{ {
return LeaveRequest::approved() return LeaveRequest::query()
->approved()
->where('employee_id', $employee->id) ->where('employee_id', $employee->id)
->whereDate('start_date', '<=', today()) ->whereDate('start_date', '<=', today())
->whereDate('end_date', '>=', today()) ->whereDate('end_date', '>=', today())

View File

@ -55,7 +55,8 @@ public function create(array $validated, User $user): void
{ {
$employee = $this->resolveAuthenticatedEmployee($user); $employee = $this->resolveAuthenticatedEmployee($user);
$hasPending = LeaveRequest::pending() $hasPending = LeaveRequest::query()
->pending()
->where('employee_id', $employee->id) ->where('employee_id', $employee->id)
->exists(); ->exists();

View File

@ -133,7 +133,8 @@ public function getTopSuppliers(): array
public function getTopCustomers(): array public function getTopCustomers(): array
{ {
return Order::completed() return Order::query()
->completed()
->join('customers', 'orders.customer_id', '=', 'customers.id') ->join('customers', 'orders.customer_id', '=', 'customers.id')
->selectRaw('customers.name, SUM(orders.total_amount) as total_amount, COUNT(orders.id) as order_count') ->selectRaw('customers.name, SUM(orders.total_amount) as total_amount, COUNT(orders.id) as order_count')
->groupBy('customers.id', 'customers.name') ->groupBy('customers.id', 'customers.name')
@ -272,11 +273,13 @@ public function getOrderStats(): array
public function getRevenueSummary(): 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') ->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(); ->first();
$totalMarketplaceFees = Order::completed() $totalMarketplaceFees = Order::query()
->completed()
->whereNotNull('marketplace_settings_snapshot') ->whereNotNull('marketplace_settings_snapshot')
->get() ->get()
->sum(fn ($order) => (int) ($order->marketplace_settings_snapshot['total_fee_amount'] ?? 0)); ->sum(fn ($order) => (int) ($order->marketplace_settings_snapshot['total_fee_amount'] ?? 0));
@ -294,7 +297,8 @@ public function getRevenueSummary(): array
public function getMarketplaceSummary(): array public function getMarketplaceSummary(): array
{ {
$marketplaceOrders = Order::completed() $marketplaceOrders = Order::query()
->completed()
->whereIn('channel', [OrderChannel::SHOPEE, OrderChannel::TIKTOK]) ->whereIn('channel', [OrderChannel::SHOPEE, OrderChannel::TIKTOK])
->whereNotNull('marketplace_settings_snapshot') ->whereNotNull('marketplace_settings_snapshot')
->get(); ->get();
@ -373,15 +377,18 @@ public function getMonthlyExpenses(Carbon $startOfMonth, Carbon $endOfMonth): ar
public function getKasbonSummary(): array public function getKasbonSummary(): array
{ {
$pending = EmployeeAdvance::pending() $pending = EmployeeAdvance::query()
->pending()
->selectRaw('COUNT(*) as count, COALESCE(SUM(amount), 0) as total') ->selectRaw('COUNT(*) as count, COALESCE(SUM(amount), 0) as total')
->first(); ->first();
$approved = EmployeeAdvance::approved() $approved = EmployeeAdvance::query()
->approved()
->selectRaw('COUNT(*) as count, COALESCE(SUM(amount), 0) as total') ->selectRaw('COUNT(*) as count, COALESCE(SUM(amount), 0) as total')
->first(); ->first();
$paid = EmployeeAdvance::paid() $paid = EmployeeAdvance::query()
->paid()
->selectRaw('COUNT(*) as count, COALESCE(SUM(amount), 0) as total') ->selectRaw('COUNT(*) as count, COALESCE(SUM(amount), 0) as total')
->first(); ->first();
@ -522,7 +529,8 @@ public function getMonthlyRevenueTrend(): array
$start = Carbon::create($month['year'], $month['month'], 1)->startOfMonth(); $start = Carbon::create($month['year'], $month['month'], 1)->startOfMonth();
$end = $start->copy()->endOfMonth(); $end = $start->copy()->endOfMonth();
$monthlyRevenue = Order::completed() $monthlyRevenue = Order::query()
->completed()
->whereBetween('created_at', [$start, $end]) ->whereBetween('created_at', [$start, $end])
->selectRaw('COALESCE(SUM(total_amount), 0) as total, COUNT(*) as count') ->selectRaw('COALESCE(SUM(total_amount), 0) as total, COUNT(*) as count')
->first(); ->first();