@php use Illuminate\Support\Facades\DB; use App\Models\Order; use App\Models\Payment; use App\Models\InventoryItem; use Illuminate\Support\Facades\Auth; /* |-------------------------------------------------------------------------- | CURRENT USER |-------------------------------------------------------------------------- */ $currentUser = Auth::user(); /* |-------------------------------------------------------------------------- | SETTINGS |-------------------------------------------------------------------------- | | Your settings table may not have "key" and "value" columns. | Therefore we safely read the first settings record. | */ $appSettings = DB::table('settings')->first(); /* |-------------------------------------------------------------------------- | NOTIFICATION SETTINGS |-------------------------------------------------------------------------- */ $notifyLowStock = true; $notifyNewOrder = true; $notifyPayment = true; $lowStockAlert = 10; if ($appSettings) { if (isset($appSettings->notify_low_stock)) { $notifyLowStock = (bool) $appSettings->notify_low_stock; } if (isset($appSettings->notify_new_order)) { $notifyNewOrder = (bool) $appSettings->notify_new_order; } if (isset($appSettings->notify_payment)) { $notifyPayment = (bool) $appSettings->notify_payment; } if (isset($appSettings->low_stock_alert)) { $lowStockAlert = (int) $appSettings->low_stock_alert; } } /* |-------------------------------------------------------------------------- | NEW ORDERS |-------------------------------------------------------------------------- */ $recentOrders = collect(); if ($notifyNewOrder) { $recentOrders = Order::with(['customer', 'status']) ->orderByDesc('order_id') ->limit(5) ->get(); } /* |-------------------------------------------------------------------------- | NEW ORDER COUNT |-------------------------------------------------------------------------- */ $newOrderCount = 0; if ($notifyNewOrder) { $newOrderCount = Order::whereHas('status', function ($query) { $query->whereRaw('LOWER(status_name) = ?', ['pending']); })->count(); } /* |-------------------------------------------------------------------------- | LOW STOCK |-------------------------------------------------------------------------- */ $lowStockItems = collect(); if ($notifyLowStock) { try { $lowStockItems = InventoryItem::where('quantity', '<=', $lowStockAlert) ->orderBy('quantity', 'asc') ->limit(5) ->get(); } catch (\Throwable $e) { $lowStockItems = collect(); } } /* |-------------------------------------------------------------------------- | PAYMENTS |-------------------------------------------------------------------------- */ $recentPayments = collect(); if ($notifyPayment) { try { $recentPayments = Payment::orderByDesc('payment_id') ->limit(5) ->get(); } catch (\Throwable $e) { $recentPayments = collect(); } } /* |-------------------------------------------------------------------------- | NOTIFICATION COUNT |-------------------------------------------------------------------------- */ $notificationCount = ($notifyNewOrder ? $newOrderCount : 0) + ($notifyLowStock ? $lowStockItems->count() : 0) + ($notifyPayment ? $recentPayments->count() : 0); @endphp