GOOD SHELL MAS BOY
Server: Apache/2.4.52 (Ubuntu)
System: Linux vmi1836763.contaboserver.net 5.15.0-130-generic #140-Ubuntu SMP Wed Dec 18 17:59:53 UTC 2024 x86_64
User: www-data (33)
PHP: 8.4.10
Disabled: NONE
Upload Files
File: /var/www/console.fixgini.com/app/Http/Controllers/DashboardController.php
<?php

namespace App\Http\Controllers;

use App\Models\Review;
use App\Models\Country;
use App\Models\BookingForm;
use Illuminate\Http\Request;
use App\Models\BookingPayment;
use Illuminate\Support\Facades\Auth;

class DashboardController extends Controller
{
    public function customerStatistics()
    {
        $user = Auth::user();

        // get user curreny
        $currency = $user->country->symbol;
        
        // Task statistics
        $total_tasks = BookingForm::where('customer_id', $user->id)->count();
        $ongoing_tasks = BookingForm::where('customer_id', $user->id)->where('status', 'ongoing')->count();
        $pending_tasks = BookingForm::where('customer_id', $user->id)->where('status', 'pending')->count();
        $completed_tasks = BookingForm::where('customer_id', $user->id)->where('status', 'completed')->count();

        // Payment statistics
        $latest_payment = BookingPayment::where('customer_id', $user->id)->latest()->first();
        $last_payment_amount = $latest_payment ? $latest_payment->amount : 0;
        $total_completed_payment = BookingPayment::where('customer_id', $user->id)->where('payment_status', 'completed')->sum('amount');

        // Reviews count
        $total_reviews = Review::where('user_id', $user->id)->count();

        // Returning data in a response
        return response()->json([
            'status' => 'success',
            'message' => 'Customer statistics retrieved successfully',
            'data' => [
                'total_tasks' => $total_tasks,
                'ongoing_tasks' => $ongoing_tasks,
                'pending_tasks' => $pending_tasks,
                'completed_tasks' => $completed_tasks,
                'last_payment_amount' => $last_payment_amount,
                'total_completed_payment' => $total_completed_payment,
                'total_reviews' => $total_reviews,
                'currency' => $currency,
            ]
        ], 200);
    }
    public function sellerStatistics()
    {
        $user = Auth::user();

        // get user curreny
        $currency = $user->country->symbol;
        
        // Task statistics
        $total_tasks = BookingForm::where('service_provider_id', $user->id)->count();
        $ongoing_tasks = BookingForm::where('service_provider_id', $user->id)->where('status', 'ongoing')->count();
        $pending_tasks = BookingForm::where('service_provider_id', $user->id)->where('status', 'pending')->count();
        $completed_tasks = BookingForm::where('service_provider_id', $user->id)->where('status', 'completed')->count();

        // Payment statistics
        // Payment statistics
        $latest_payment = BookingPayment::whereHas('booking', function ($query) use ($user) {
            $query->where('service_provider_id', $user->id);
        })->latest()->first();

        $last_payment_amount = $latest_payment ? $latest_payment->amount : 0;

        $total_completed_payment = BookingPayment::whereHas('booking', function ($query) use ($user) {
            $query->where('service_provider_id', $user->id);
        })->where('payment_status', 'completed')->sum('amount');
        
        // Reviews count
        $total_reviews = Review::where('user_id', $user->id)->count();

        // Returning data in a response
        return response()->json([
            'status' => 'success',
            'message' => 'Seller statistics retrieved successfully',
            'data' => [
                'total_tasks' => $total_tasks,
                'ongoing_tasks' => $ongoing_tasks,
                'pending_tasks' => $pending_tasks,
                'completed_tasks' => $completed_tasks,
                'last_payment_amount' => $last_payment_amount,
                'total_completed_payment' => $total_completed_payment,
                'total_reviews' => $total_reviews,
                'currency' => $currency,
            ]
        ], 200);
    }

}