%PDF- %PDF-
Mini Shell

Mini Shell

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

<?php

namespace App\Http\Controllers\Backend\Member;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use App\Models\Colling;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Yajra\DataTables\Facades\DataTables;

class DashboardController extends Controller
{

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

        // page title
        page_title('Portal');

        if (Auth::user()->role->slug == 'manager') {
            $query = Colling::toBase()->where('soft_data',0)
                ->whereIn('agent_id',Auth::user()->agents->pluck('id'));
        }else{
            $query = Colling::toBase()->where('soft_data',0);
        }
        $closedStatusQuery = clone $query;
        $potentialStatusQuery = clone $query;
        $inProgressQuery = clone $query;
        $followUpQuery = clone $query;
        $InterestedQuery = clone $query;
        $callNoReplyQuery = clone $query;
        $data = [
            'agents'        => User::toBase()->where(['role_id'=>4])->get(),
            'statuses'      => DB::table('calling_statuses')->where('status',1)->get(),
            'totalCalling'  => $query->get()->count(),
            'openStatus'    => $query->where('status',1)->count(),
            'closedStatus'  => $closedStatusQuery->where('status',2)->count(),
            'potentialCall' => $potentialStatusQuery->where('potential',1)->count(),
            'inProgress'    => $inProgressQuery->where('sub_status', 2)->count(),
            'followUp'      => $followUpQuery->where('sub_status', 4)->count(),
            'interested'    => $InterestedQuery->where('sub_status', 11)->count(),
            'callNoReply'   => $callNoReplyQuery->where('sub_status', 3)->count(),
        ];

        if (Auth::user()->role->slug == 'agent') {
            return view('backend.agent.dashboard');
        }

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


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

            $agent      = $request->agent;
            $status     = $request->status;
            $sub_status = $request->sub_status;
            $potential  = $request->potential;
            $start_date = $request->start_date;
            $end_date   = $request->end_date;

            $query = DB::table('collings')->where('soft_data',0);
            if (!empty($agent)) {
                $query = $query->where('agent_id',$agent);
            }

            if (!empty($status)) {
                $query = $query->where('status',$status);
            }

            if (!empty($potential)) {
                $query = $query->where('potential',$potential);
            }

            if (!empty($sub_status)) {
                $query = $query->where('sub_status',$sub_status);
            }

            if (!empty($start_date) && !empty($end_date)) {
                $query->whereDate('import_date','>=',$start_date)
                    ->whereDate('import_date','<=',$end_date);
            }

            $follow_up             = clone $query;
            $interested            = clone $query;
            $call_no_reply         = clone $query;
            $potential             = clone $query;
            $close_call            = clone $query;
            $data['total_call']    = $query->count();
            $data['open_call']     = $query->where('status',1)->count();
            $data['in_progress']   = $query->where('sub_status',2)->count();
            $data['follow_up']     = $follow_up->where('sub_status',4)->count();
            $data['interested']    = $interested->where('sub_status',11)->count();
            $data['call_no_reply'] = $call_no_reply->where('sub_status',3)->count();
            $data['potential']     = $potential->where('potential',1)->count();
            $data['close_call']    = $close_call->where('status',2)->count();
            $query->get();

            $data = [
                'totalCalling'  => $data['total_call'],
                'openStatus'    => $data['open_call'],
                'closedStatus'  => $data['close_call'],
                'potentialCall' => $data['potential'],
                'inProgress'    => $data['in_progress'],
                'followUp'      => $data['follow_up'],
                'interested'    => $data['interested'],
                'callNoReply'   => $data['call_no_reply']
            ];

            return response()->json($data);
        }else{
            return response()->json(['status'=>'error','message'=>'Unauthorized Block!']);
        }
    }
    
    protected function manager_dashboard_data(string $param){
        if ($param == 'total-call') {
            $getData = Colling::with('agent','callingStatus','callingSubStatus')
                ->orderBy('id','asc')
                ->where('soft_data',0)->whereIn('agent_id',Auth::user()->agents->pluck('id'));
        }

        if ($param == 'open-call') {
            $getData = Colling::with('agent','callingStatus','callingSubStatus')
                ->orderBy('id','asc')
                ->where(['status'=>1,'soft_data'=>0])->whereIn('agent_id',Auth::user()->agents->pluck('id'));
        }

        if ($param == 'closed-call') {
            $getData = Colling::with('agent','callingStatus','callingSubStatus')
                ->orderBy('id','asc')
                ->where(['status'=>2,'soft_data'=>0])->whereIn('agent_id',Auth::user()->agents->pluck('id'));
        }

        if ($param == 'potential-call') {
            $getData = Colling::with('agent','callingStatus','callingSubStatus')
                ->orderBy('id','asc')
                ->where(['potential'=>1,'soft_data'=>0])->whereIn('agent_id',Auth::user()->agents->pluck('id'));
        }

        if ($param == 'in-progress-call') {
            $getData = Colling::with('agent','callingStatus','callingSubStatus')
                ->orderBy('id','asc')
                ->where(['sub_status'=>2,'soft_data'=>0])->whereIn('agent_id',Auth::user()->agents->pluck('id'));
        }

        if ($param == 'follow-up-call') {
            $getData = Colling::with('agent','callingStatus','callingSubStatus')
                ->orderBy('id','asc')
                ->where(['sub_status'=>4,'soft_data'=>0])->whereIn('agent_id',Auth::user()->agents->pluck('id'));
        }

        if ($param == 'interested-call') {
            $getData = Colling::with('agent','callingStatus','callingSubStatus')
                ->orderBy('id','asc')
                ->where(['sub_status'=>11,'soft_data'=>0])->whereIn('agent_id',Auth::user()->agents->pluck('id'));
        }

        if ($param == 'called-no-reply') {
            $getData = Colling::with('agent','callingStatus','callingSubStatus')
                ->orderBy('id','asc')
                ->where(['sub_status'=>3,'soft_data'=>0])->whereIn('agent_id',Auth::user()->agents->pluck('id'));
        }

        return $getData;
    }

    protected function dashboard_data(string $param){
        if ($param == 'total-call') {
            $getData = Colling::with('agent','callingStatus','callingSubStatus')
                ->orderBy('id','asc')
                ->where('soft_data',0);
        }

        if ($param == 'open-call') {
            $getData = Colling::with('agent','callingStatus','callingSubStatus')
                ->orderBy('id','asc')
                ->where(['status'=>1,'soft_data'=>0]);
        }

        if ($param == 'closed-call') {
            $getData = Colling::with('agent','callingStatus','callingSubStatus')
                ->orderBy('id','asc')
                ->where(['status'=>2,'soft_data'=>0]);
        }

        if ($param == 'potential-call') {
            $getData = Colling::with('agent','callingStatus','callingSubStatus')
                ->orderBy('id','asc')
                ->where(['potential'=>1,'soft_data'=>0]);
        }

        if ($param == 'in-progress-call') {
            $getData = Colling::with('agent','callingStatus','callingSubStatus')
                ->orderBy('id','asc')
                ->where(['sub_status'=>2,'soft_data'=>0]);
        }

        if ($param == 'follow-up-call') {
            $getData = Colling::with('agent','callingStatus','callingSubStatus')
                ->orderBy('id','asc')
                ->where(['sub_status'=>4,'soft_data'=>0]);
        }

        if ($param == 'interested-call') {
            $getData = Colling::with('agent','callingStatus','callingSubStatus')
                ->orderBy('id','asc')
                ->where(['sub_status'=>11,'soft_data'=>0]);
        }

        if ($param == 'called-no-reply') {
            $getData = Colling::with('agent','callingStatus','callingSubStatus')
                ->orderBy('id','asc')
                ->where(['sub_status'=>3,'soft_data'=>0]);
        }

        return $getData;
    }

    public function dashboardBoxData(Request $request,string $param){
        if ($request->ajax()) {
            if(Auth::user()->role_id == 2){
                $getData = $this->dashboard_data($param);
            }else{
                $getData = $this->manager_dashboard_data($param);
            }
            
            return DataTables::eloquent($getData)
                ->addIndexColumn()
                ->addColumn('agent', function($user){
                    return $user->agent ? $user->agent->name : 'N/A';
                })
                ->addColumn('date', function($user){
                    return date_formats('d-m-Y',$user->import_date);
                })
                ->addColumn('time_of_call', function($user){
                    return '<button type="button" class="btn-style btn-style-view" id="btn-time-data" data-id="'.$user->id.'"><i class="fas fa-eye"></i></button>';
                })
                ->addColumn('number_1', function($user){
                    return '<a href="tel:'.$user->number_1.'">'.$user->number_1.'</a>';
                })
                ->addColumn('number_2', function($user){
                    return '<a href="tel:'.$user->number_2.'">'.$user->number_2.'</a>';
                })
                ->addColumn('status', function($user){
                    return $user->callingStatus ? $user->callingStatus->name : 'N/A';
                })
                ->addColumn('sub_status', function($user){
                    return $user->callingSubStatus ? $user->callingSubStatus->name : 'N/A';
                })
                ->addColumn('potential', function($user){
                    return $user->potential != '' ? POTENTIAL[$user->potential] : 'N/A';
                })
                ->addColumn('comment', function($user){
                    return '<button type="button" class="btn-style btn-style-view data-view" data-id="'.$user->id.'" data-type="Comment" data-content="'.$user->comment.'" data-serial="'.$user->serial.'"><i class="fas fa-eye"></i></button>';
                })
                ->addColumn('follow_up1', function($user){
                    return '<button type="button" class="btn-style btn-style-view data-view" data-id="'.$user->id.'" data-type="Follow up 1" data-content="'.$user->follow_up1.'" data-serial="'.$user->serial.'"><i class="fas fa-eye"></i></button>';
                })
                ->addColumn('follow_up2', function($user){
                    return '<button type="button" class="btn-style btn-style-view data-view" data-id="'.$user->id.'" data-type="Follow up 2" data-content="'.$user->follow_up2.'" data-serial="'.$user->serial.'"><i class="fas fa-eye"></i></button>';
                })
                ->addColumn('follow_up3', function($user){
                    return '<button type="button" class="btn-style btn-style-view data-view" data-id="'.$user->id.'" data-type="Follow up 3" data-content="'.$user->follow_up3.'" data-serial="'.$user->serial.'"><i class="fas fa-eye"></i></button>';
                })
                ->addColumn('action', function($user){
                    $action = '';
                    if (Gate::allows('app.callings.delete')) {
                        $action .= '<button class="btn-style btn-style-danger ml-1 deletable-btn" data-title="Do you really want to delete this record?" data-id="'.$user->id.'"><i class="fa fa-trash"></i></button>';
                    }

                    if (Gate::allows('app.callings.edit')) {
                        $action .= '<a href="'.route(routeName().'.callings.edit', $user->id).'" class="btn-style btn-style-edit ml-1" data-id="'.$user->id.'"><i class="fa fa-edit"></i></a>';
                    }

                    return '<div class="dropdown show text-right">'.$action.'</div>';
                })
                ->rawColumns(['agent','number_1','number_2','action','follow_up1','follow_up2','follow_up3','comment','time_of_call'])
                ->make(true);
        }

        page_title($param);
        return view('backend.pages.dashboard-data');
    }
}

Zerion Mini Shell 1.0