/** * Refill Tracker - Shows which shops have been refilled and which haven't */ public function refillTracker($agentId) { $agent = Agent::findOrFail($agentId); // Get past 2 weeks date range $twoWeeksAgo = Carbon::now()->subWeeks(2); // Get all agent's shops $shops = Shop::where('agent_id', $agentId)->get(); // For each shop, get refills from past 2 weeks $shopsData = $shops->map(function($shop) use ($twoWeeksAgo) { $refills = RefillRequest::where('shop_id', $shop->id) ->where('status', 'approved') ->where('request_date', '>=', $twoWeeksAgo) ->with('product') ->orderBy('request_date', 'desc') ->get(); return [ 'shop' => $shop, 'refills' => $refills, 'total_refilled' => $refills->sum('quantity_approved') ]; }); // Calculate summary $shopsWithRefills = $shopsData->filter(function($data) { return $data['refills']->count() > 0; })->count(); $shopsWithoutRefills = $shopsData->filter(function($data) { return $data['refills']->count() === 0; })->count(); $totalUnitsRefilled = $shopsData->sum('total_refilled'); return view('agent.sales.refill-tracker', compact( 'agent', 'shopsData', 'shopsWithRefills', 'shopsWithoutRefills', 'totalUnitsRefilled' )); }