canManage($request->tenant)){ return $this->response([ 'success' => false, 'error' => __eva("You cannot access.") ]); } $yearmonth = $request->yearmonth; // e.g. '2026-03' $tenantId = $request->tenantId(); // Get all missed call logs for this month $logs = MissedCallLog::where('restaurant_id', $tenantId) ->where('date_created', '>=', $yearmonth . '-01 00:00:00') ->where('date_created', '<', $this->nextMonth($yearmonth) . '-01 00:00:00') ->orderBy('date_created', 'DESC') ->get(); // Build stats $total = count($logs); $smsCount = 0; $whatsappCount = 0; $bookingCount = 0; $items = []; foreach ($logs as $log) { if ($log->sms_sent) $smsCount++; if ($log->whatsapp_sent) $whatsappCount++; if ($log->booking_id) $bookingCount++; $bookingUuid = null; if ($log->booking_id) { $booking = Booking::find($log->booking_id); if ($booking) { $bookingUuid = $booking->uuid; } } $items[] = [ 'id' => $log->id, 'caller_phone' => $log->caller_phone, 'sms_sent' => (bool) $log->sms_sent, 'whatsapp_sent' => (bool) $log->whatsapp_sent, 'booking_id' => $log->booking_id, 'booking_uuid' => $bookingUuid, 'date_created' => $log->date_created, ]; } return $this->response([ 'success' => true, 'stats' => [ 'total' => $total, 'sms_count' => $smsCount, 'whatsapp_count' => $whatsappCount, 'booking_count' => $bookingCount, ], 'items' => $items, ]); } /** * Calculate next month from yearmonth string (e.g. '2026-03' -> '2026-04') */ protected function nextMonth($yearmonth) { $parts = explode('-', $yearmonth); $year = intval($parts[0]); $month = intval($parts[1]); $month++; if ($month > 12) { $month = 1; $year++; } return sprintf('%04d-%02d', $year, $month); } }