Files
intaleq_v2/app/Http/Controllers/Admin/StatsController.php
2026-04-22 23:16:23 +03:00

96 lines
4.1 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
/**
* Admin Stats Controller
* Replaces: serviceapp/getRidesStatic.php, getPassengersStatic.php,
* getEmployeeStatic.php, getdriverstotalMonthly.php, getEditorStatsCalls.php
*
* متحكم الإحصائيات (Stats Controller)
*
* الغرض من الملف:
* توليد تقارير وإحصائيات حول أداء المنصة للمسؤولين.
*
* كيفية العمل:
* 1. يحسب إجمالي عدد الرحلات، السائقين النشطين، والركاب.
* 2. يحلل البيانات المالية والنمو الشهري للسائقين.
* 3. يعرض ملخصاً سريعاً (Overview) لمؤشرات الأداء الرئيسية.
*/
class StatsController extends Controller
{
/** GET /v2/admin/stats/overview */
public function overview(): JsonResponse
{
$totalDrivers = DB::connection('ride')->table('driver')->count();
$activeDrivers = DB::connection('ride')->table('driver')->where('status', 'notDeleted')->count();
$totalPassengers = DB::connection('primary')->table('passengers')->count();
$activePassengers = DB::connection('primary')->table('passengers')->where('status', 'notDeleted')->count();
$totalRides = DB::connection('ride')->table('ride')->count();
$finishedRides = DB::connection('ride')->table('ride')->where('status', 'finish')->count();
$todayRides = DB::connection('ride')->table('ride')
->where('status', 'finish')->whereDate('rideTimeFinish', today())->count();
$todayRevenue = DB::connection('ride')->table('ride')
->where('status', 'finish')->whereDate('rideTimeFinish', today())
->sum('price_for_passenger');
$onlineDrivers = DB::connection('tracking')->table('car_locations')
->where('status', 'on')->where('updated_at', '>', now()->subMinutes(10))->count();
return response()->json([
'status' => 'success',
'data' => [
'drivers' => ['total' => $totalDrivers, 'active' => $activeDrivers, 'online' => $onlineDrivers],
'passengers' => ['total' => $totalPassengers, 'active' => $activePassengers],
'rides' => ['total' => $totalRides, 'finished' => $finishedRides, 'today' => $todayRides],
'revenue' => ['today' => round($todayRevenue, 2)],
],
]);
}
/** GET /v2/admin/stats/rides?from=2026-01-01&to=2026-04-22 */
public function rides(Request $request): JsonResponse
{
$from = $request->input('from', today()->subDays(30)->toDateString());
$to = $request->input('to', today()->toDateString());
$daily = DB::connection('ride')->table('ride')
->selectRaw("DATE(rideTimeFinish) as date, COUNT(*) as count, SUM(price_for_passenger) as revenue")
->where('status', 'finish')
->whereBetween('rideTimeFinish', [$from . ' 00:00:00', $to . ' 23:59:59'])
->groupByRaw('DATE(rideTimeFinish)')
->orderBy('date')
->get();
return response()->json(['status' => 'success', 'data' => $daily]);
}
/** GET /v2/admin/stats/drivers-monthly */
public function driversMonthly(): JsonResponse
{
$monthly = DB::connection('ride')->table('driver')
->selectRaw("DATE_FORMAT(created_at, '%Y-%m') as month, COUNT(*) as count")
->groupByRaw("DATE_FORMAT(created_at, '%Y-%m')")
->orderBy('month', 'desc')
->limit(12)
->get();
return response()->json(['status' => 'success', 'data' => $monthly]);
}
/** GET /v2/admin/stats/employees */
public function employees(): JsonResponse
{
$employees = DB::connection('ride')->table('employee')
->select('id', 'name', 'phone', 'status', 'created_at')
->orderBy('created_at', 'desc')
->get();
return response()->json(['status' => 'success', 'data' => $employees]);
}
}