diff --git a/, b/, new file mode 100644 index 00000000..e69de29b diff --git a/Dashboard statistics retrieved successfully, b/Dashboard statistics retrieved successfully, new file mode 100644 index 00000000..e69de29b diff --git "a/Response\357\200\272\357\200\272HTTP_OK," "b/Response\357\200\272\357\200\272HTTP_OK," new file mode 100644 index 00000000..e69de29b diff --git a/app/Http/Controllers/Api/V1/Admin/AdminDashboardController.php b/app/Http/Controllers/Api/V1/Admin/AdminDashboardController.php new file mode 100644 index 00000000..832694bd --- /dev/null +++ b/app/Http/Controllers/Api/V1/Admin/AdminDashboardController.php @@ -0,0 +1,78 @@ +startOfMonth(); + $lastMonth = now()->subMonth()->startOfMonth(); + + // Revenue statistics + $currentMonthRevenue = Order::where('created_at', '>=', $currentMonth) + ->sum('total_amount'); + $lastMonthRevenue = Order::whereBetween('created_at', [$lastMonth, $currentMonth]) + ->sum('total_amount'); + $revenuePercentageDifference = $this->calculatePercentageDifference($currentMonthRevenue, $lastMonthRevenue); + + // User statistics + $totalUsers = User::count(); + $lastMonthUsers = User::where('created_at', '<', $currentMonth)->count(); + $userPercentageDifference = $this->calculatePercentageDifference($totalUsers, $lastMonthUsers); + + // Product statistics + $totalProducts = Product::count(); + $lastMonthProducts = Product::where('created_at', '<', $currentMonth)->count(); + $productPercentageDifference = $this->calculatePercentageDifference($totalProducts, $lastMonthProducts); + + // Lifetime sales statistics + $lifetimeSales = Order::sum('total_amount'); + $lastMonthSales = Order::where('created_at', '<', $currentMonth)->sum('total_amount'); + $salesPercentageDifference = $this->calculatePercentageDifference($lifetimeSales, $lastMonthSales); + + return response()->json([ + 'message' => 'Dashboard statistics retrieved successfully', + 'status_code' => Response::HTTP_OK, + 'data' => [ + 'total_revenue' => [ + 'current_month' => $currentMonthRevenue, + 'previous_month' => $lastMonthRevenue, + 'percentage_difference' => round($revenuePercentageDifference, 2) . '%', + ], + 'total_users' => [ + 'current_month' => $totalUsers, + 'previous_month' => $lastMonthUsers, + 'percentage_difference' => round($userPercentageDifference, 2) . '%', + ], + 'total_products' => [ + 'current_month' => $totalProducts, + 'previous_month' => $lastMonthProducts, + 'percentage_difference' => round($productPercentageDifference, 2) . '%', + ], + 'lifetime_sales' => [ + 'current_month' => $lifetimeSales, + 'previous_month' => $lastMonthSales, + 'percentage_difference' => round($salesPercentageDifference, 2) . '%', + ], + ] + ]); + + } + + private function calculatePercentageDifference($current, $previous) + { + if ($previous > 0 && $current > 0) { + return (($current - $previous) / $previous) * 100; + } elseif ($current > 0 && $previous === 0) { + return 100; + } + return 0; + } +} diff --git a/resources/docs/documentation.json b/resources/docs/documentation.json index b3b0f390..109f38ad 100644 --- a/resources/docs/documentation.json +++ b/resources/docs/documentation.json @@ -500,53 +500,131 @@ }, "/api/v1/statistics": { "get": { - "tags": [ - "Dashboard" - ], - "summary": "Get dashboard statistics", - "description": "Retrieve the statistics for the dashboard, including total revenue, subscriptions, and sales.", - "responses": { - "200": { - "description": "Successful operation", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/inline_response_200_5" + "tags": [ + "Admin Dashboard" + ], + "summary": "Get admin dashboard statistics", + "description": "Retrieve the statistics for the admin dashboard, including total revenue, users, products, and lifetime sales.", + "responses": { + "200": { + "description": "Successful operation", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "message": { + "type": "string", + "example": "Dashboard statistics retrieved successfully" + }, + "status_code": { + "type": "integer", + "example": 200 + }, + "data": { + "type": "object", + "properties": { + "revenue": { + "type": "object", + "properties": { + "current_month": { + "type": "number", + "example": 45000 + }, + "previous_month": { + "type": "number", + "example": 37500 + }, + "percentage_difference": { + "type": "string", + "example": "20%" + } } }, - "application/xml": { - "schema": { - "$ref": "#/components/schemas/inline_response_200_5" + "users": { + "type": "object", + "properties": { + "current_month": { + "type": "integer", + "example": 4000 + }, + "previous_month": { + "type": "integer", + "example": 3636 + }, + "percentage_difference": { + "type": "string", + "example": "10%" } - } - } - }, - "400": { - "description": "Bad Request", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/inline_response_400" } - } - } - }, - "500": { - "description": "Internal Server Error", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/inline_response_500_3" + }, + "products": { + "type": "object", + "properties": { + "current_month": { + "type": "integer", + "example": 1000 + }, + "previous_month": { + "type": "integer", + "example": 833 + }, + "percentage_difference": { + "type": "string", + "example": "20%" + } + } + }, + "lifetime_sales": { + "type": "object", + "properties": { + "current_month": { + "type": "number", + "example": 450000 + }, + "previous_month": { + "type": "number", + "example": 180000 + }, + "percentage_difference": { + "type": "string", + "example": "150%" + } } } + } } + } + } } + } }, - "security": [ - { - "bearerAuth": [] + "400": { + "description": "Bad Request", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/inline_response_400" } - ] + } + } + }, + "500": { + "description": "Internal Server Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/inline_response_500_3" + } + } + } + } + }, + "security": [ + { + "bearerAuth": [] + } + ] } }, "/api/v1/export/{format}": { diff --git a/routes/api.php b/routes/api.php index 4653f17a..ce2da066 100755 --- a/routes/api.php +++ b/routes/api.php @@ -46,8 +46,7 @@ use App\Http\Controllers\QuestController; use App\Http\Controllers\UserNotificationController; use Illuminate\Support\Facades\Route; - - +use App\Http\Controllers\Api\V1\Admin\AdminDashboardController; /* |-------------------------------------------------------------------------- | API Routes @@ -240,6 +239,7 @@ Route::delete('/blogs/{id}', [BlogController::class, 'destroy']); Route::get('/waitlists', [WaitListController::class, 'index']); Route::apiResource('squeeze-pages', SqueezePageCoontroller::class); + Route::get('/statistics', [AdminDashboardController::class, 'getStatistics']); Route::apiResource('faqs', FaqController::class); });