%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home/silvzytp/dsr_code/app/Http/Controllers/Backend/Member/
Upload File :
Create Path :
Current File : //home/silvzytp/dsr_code/app/Http/Controllers/Backend/Member/DashboardController.php

<?php

namespace App\Http\Controllers\Backend\Member;

use App\Models\User;
use App\Models\MonthlyData;
use Illuminate\Http\Request;
use App\Models\IndividualTarget;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;

class DashboardController extends Controller
{

    public function dashboard(){
        // authorized
        Gate::authorize('app.dashboard');

        // page title
        page_title('Portal');

        $start_date = date('Y-m-d'); // today date

        $agents = User::toBase()->where('manager_id', Auth::id())
            ->get()->count();

        $target = Auth::user();

        $revenueAchievment = MonthlyData::toBase()->where('user_id',Auth::id())->orderBy('id', 'desc')
            ->sum('revenue_achievment');

        $saleAchieved = MonthlyData::toBase()->where('user_id',Auth::id())->orderBy('id', 'desc')
            ->sum('sale_achievment');

        $saleTarget = IndividualTarget::toBase()->where('user_id', Auth::id())->orderBy('id', 'desc')
            ->sum('s_target');

        $revenueDeficit = ($target->target - $revenueAchievment);
        $saleDeficit = ($saleTarget - $saleAchieved);

        $data = [
            'target'            => $target->target ?? 0,
            'revenueAchievment' => $revenueAchievment ?? 0,
            'revenueDeficit'    => $revenueDeficit ?? 0,
            'sale_target'       => $saleTarget ?? 0,
            'saleAchieved'      => $saleAchieved ?? 0,
            'saleDeficit'       => $saleDeficit ?? 0,
            'agents'            => $agents ?? 0
        ];

        return view('backend.member.dashboard', $data);
    }


    public function filter(Request $request){
        // authorized
        Gate::authorize('app.dashboard.filter');

        // page title
        page_title('Portal');

        // filter date
        $agents = User::toBase()->where('manager_id', Auth::id())
            ->get()->count();

        $target = Auth::user();

        $revenueAchievment = MonthlyData::toBase()->where('user_id',Auth::id())
            ->whereDate('revenue_date','>=',$request->start_date)
            ->whereDate('revenue_date','<=',$request->end_date)
            ->sum('revenue_achievment');
            
        $saleAchieved = MonthlyData::toBase()->where('user_id',Auth::id())
            ->whereDate('revenue_date','>=',$request->start_date)
            ->whereDate('revenue_date','<=',$request->end_date)
            ->sum('sale_achievment');

        $saleTarget = IndividualTarget::toBase()->where('user_id', Auth::id())
            ->whereDate('date','>=',$request->start_date)
            ->whereDate('date','<=',$request->end_date)
            ->sum('s_target');

        $revenueDeficit = ($target->target - $revenueAchievment);
        $saleDeficit = ($saleTarget - $saleAchieved);

        $data = [
            'target'            => $target->target ?? 0,
            'revenueAchievment' => $revenueAchievment ?? 0,
            'revenueDeficit'    => $revenueDeficit ?? 0,
            'sale_target'       => $saleTarget ?? 0,
            'saleAchieved'      => $saleAchieved ?? 0,
            'saleDeficit'       => $saleDeficit ?? 0,
            'agents'            => $agents ?? 0
        ];


        return view('backend.member.dashboard', $data);
    }

    /**
     * Dashboard Chart Bar
     *
     * @return \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function dashboardChart(Request $request){
        if ($request->ajax()) {

            $month = ['1','2','3','4','5','6','7','8','9','10','11','12'];
            // monthly publisher
            $monthlyData = [];
            foreach ($month as $value) {
                if (Auth::user()->role->slug == 'admin') {
                    $monthlyData[] = DB::table('monthly_data')
                    ->whereMonth('updated_at', '=', $value)->get()->count();
                }else{
                    $monthlyData[] = DB::table('monthly_data')->where('user_id',Auth::id())
                        ->whereMonth('updated_at', '=', $value)->get()->count();
                }

            }

            // monthly advertiser
            $monthlyCommit = [];
            foreach ($month as $value) {
                if (Auth::user()->role->slug == 'admin') {
                    $monthlyCommit[] = DB::table('monthly_commitments')
                        ->whereMonth('updated_at', '=', $value)->get()->count();
                }else{
                    $monthlyCommit[] = DB::table('monthly_commitments')->where('user_id',Auth::id())
                        ->whereMonth('updated_at', '=', $value)->get()->count();
                }
            }

            return response()->json([
                'monthly_data'  => $monthlyData,
                'monthly_commit' => $monthlyCommit
            ]);
        }
    }

}

Zerion Mini Shell 1.0