%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/MonthlyDataController.php

<?php

namespace App\Http\Controllers\Backend\Member;

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

class MonthlyDataController extends Controller
{
    public function index()
    {
        // authorized
        Gate::authorize('app.monthly-data.index');

        // page title
        page_title('Monthly Data');
        // breadcrumb
        $breadcrumb = ['Dashboard'=>route('app.dashboard'),'Monthly Data'=>''];

        if (Auth::user()->role->slug == 'admin') {
            $agents = MonthlyData::select('id','agent_name')->groupBy('agent_name')->orderBy('agent_name','asc')->get();
        }else{
            $agents = MonthlyData::where('user_id',Auth::id())->select('id','agent_name')->groupBy('agent_name')->orderBy('agent_name','asc')->get();
        }

        return view('backend.member.pages.daily_data.index',['breadcrumb'=>$breadcrumb,'agents'=>$agents]);
    }

    public function managerSelect(Request $request){
        if ($request->ajax()) {
            $agents = User::where('manager_id',$request->manager_id)->select('id','name')->orderBy('name','asc')->get();
            $output = '';
            if (!$agents->isEmpty()) {
                foreach($agents as $agent){
                    $output .= '
                    <option value="'.$agent->name.'">'.$agent->name.'</option>
                    ';
                }
            }else{
                $output .= '<option value="">-- Select Agent --</option>';
            }

            return response()->json($output);
        }
    }

    public function getData(Request $request){
        if (Auth::user()->role->slug == 'admin') {
            $getData = MonthlyData::latest('id');
        }
        else{
            $getData = MonthlyData::latest('id')->where('user_id', Auth::id());
        }

        return DataTables::eloquent($getData)
            ->addIndexColumn()
            ->filter(function ($query) use ($request){
                if (!empty($request->start_date) && !empty($request->end_date) && !empty($request->agent_name)) {
                    $query->whereDate('revenue_date','>=',$request->start_date)
                        ->whereDate('revenue_date','<=',$request->end_date)
                        ->where('agent_name',$request->agent_name);
                }
                if(!empty($request->start_date) && !empty($request->end_date)){
                    $query->whereDate('revenue_date','>=',$request->start_date)
                        ->whereDate('revenue_date','<=',$request->end_date);
                }
                if(!empty($request->agent_name)){
                    $query->where('agent_name',$request->agent_name);
                }
            })
            ->addColumn('r_date', function($data){
                return date_formats($data->revenue_date, 'd-M-Y');
            })
            ->addColumn('remark', function($data){
                if ($data->remark != '') {
                    return '<button type="button" class="btn-style btn-style-view remark-btn" data-id="'.$data->id.'" title="View"><i class="fa fa-eye"></i></button>';
                }
            })
            ->addColumn('created_at', function($data){
                return date_formats($data->created_at, 'd-M-Y');
            })
            ->addColumn('action', function($data){
                $action = '';
                if (Gate::allows('app.monthly-data.edit')) {
                    $action .= '<a href="'.route('app.monthly-data.edit', $data->id).'" class="btn-style btn-style-edit mr-1"><i class="fa fa-edit"></i></a>';
                }else{
                    $action .= '';
                }

                if (Gate::allows('app.monthly-data.destroy')) {
                    $action .= '<button type="button" class="btn-style btn-style-danger deletable-btn" data-title="Do you really want to delete this record?" data-id="'.$data->id.'"><i class="fa fa-trash"></i></button>';
                }else{
                    $action .= '';
                }

                return '<div class="d-flex align-items-center justify-content-end">'.$action.'</div>';
            })
            ->rawColumns(['created_at','action','remark'])
            ->make(true);
    }

    public function create()
    {
        // authorized
        Gate::authorize('app.monthly-data.create');

        // page title
        page_title('New Monthly Data');
        // breadcrumb
        $breadcrumb = ['Dashboard'=>route('app.dashboard'),'Monthly Data'=>route('app.monthly-data.index'),'Create'=>''];

        if (Auth::user()->role->name == 'Admin') {
            $roles = User::where('role_id',4)->select('name')->get();
        }else{
            $roles = User::where('manager_id',Auth::id())->select('name')->get();
        }

        return view('backend.member.pages.daily_data.form',['breadcrumb'=>$breadcrumb,'roles'=>$roles]);
    }

    public function store(Request $request)
    {
        // authorized
        Gate::authorize('app.monthly-data.create');

        $request->validate([
            'revenue_date'=>'required',
            'agent_name'=>'required',
        ]);

        $managerId = Auth::user()->role->slug == 'admin' ? $request->manager : Auth::id();
        MonthlyData::create([
            'user_id'            => $managerId,
            'revenue_date'       => date('Y-m-d', strtotime($request->revenue_date)),
            'agent_name'         => $request->agent_name,
            'sale_achievment'    => $request->sale_achievment,
            'revenue_achievment' => $request->revenue_achievment,
            's_media_lead'       => $request->s_media_lead,
            'dj_lead'            => $request->dj_lead,
            'telesale_l'         => $request->telesale_l,
            'c_caling_l'         => $request->c_caling_l,
            'old_s_media_l'      => $request->old_s_media_l,
            'meeting_attendance'     => $request->meeting_attendance,
            'meeting_source'     => $request->meeting_source,
            'n_of_deal'          => $request->n_of_deal,
            'n_of_call'          => $request->n_of_call,
            'connected_call'     => $request->connected_call,
            'remark'             => $request->remark,
        ]);

        return redirect()->route('app.monthly-data.index')->with('success', ALERT_SUCCESS);
    }

    public function edit($id){
        // authorized
        Gate::authorize('app.monthly-data.edit');

        $monthlyData = MonthlyData::findOrFail($id);
        // page title
        page_title('Edit Monthly Data');
        // breadcrumb
        $breadcrumb = ['Dashboard'=>route('app.dashboard'),'Monthly Data'=>route('app.monthly-data.index'),'Edit'=>''];

        if (Auth::user()->role->name == 'Admin') {
            $roles = User::where('role_id',4)->select('name')->get();
        }else{
            $roles = User::where('manager_id',Auth::id())->select('name')->get();
        }

        return view('backend.member.pages.daily_data.form',['breadcrumb'=>$breadcrumb,'roles'=>$roles,'monthlyData'=>$monthlyData]);
    }

    public function update(Request $request, $id)
    {

        // authorized
        Gate::authorize('app.monthly-data.edit');

        $request->validate([
            'revenue_date' => 'required',
            'agent_name'   => 'required',
        ]);

        $monthlyData = MonthlyData::findOrFail($id);
        $managerId = Auth::user()->role->slug == 'admin' ? $request->manager : Auth::id();
        $monthlyData->update([
            'user_id'            => $managerId,
            'revenue_date'       => date('Y-m-d', strtotime($request->revenue_date)),
            'agent_name'         => $request->agent_name,
            'sale_achievment'    => $request->sale_achievment,
            'revenue_achievment' => $request->revenue_achievment,
            's_media_lead'       => $request->s_media_lead,
            'dj_lead'            => $request->dj_lead,
            'telesale_l'         => $request->telesale_l,
            'c_caling_l'         => $request->c_caling_l,
            'old_s_media_l'      => $request->old_s_media_l,
            'meeting_attendance'     => $request->meeting_attendance,
            'meeting_source'     => $request->meeting_source,
            'n_of_deal'          => $request->n_of_deal,
            'n_of_call'          => $request->n_of_call,
            'connected_call'     => $request->connected_call,
            'remark'             => $request->remark,
        ]);

        return back()->with('success', ALERT_UPDATE);
    }

    public function agentSelected(Request $request){
        if ($request->ajax()) {

            $agents = User::where('manager_id',$request->manager_id)->select('id','name')->orderBy('name','asc')->get();
            $manager = MonthlyData::where('id', $request->data_id)->first();

            $output = '';
            if (!$agents->isEmpty()) {
                foreach($agents as $agent){
                    $selected = $manager->agent_name == $agent->name ? 'selected' : '';
                    $output .= '
                    <option value="'.$agent->name.'" '.$selected.'>'.$agent->name.'</option>
                    ';
                }
            }else{
                $output .= '<option value="">-- Select Agent --</option>';
            }

            return response()->json($output);
        }

    }

    public function destroy(Request $request){
        if ($request->ajax()) {
            if (Gate::allows('app.monthly-data.destroy')) {
                MonthlyData::findOrFail($request->data_id)->delete();
                $output = ['status'=>'success','message'=>ALERT_DELETE];
            } else {
                $output = ['status'=>'success','message'=>UNAUTHORIZED];
            }

            return response()->json($output);
        }
    }

    public function remark(Request $request){
        if ($request->ajax()) {
            $data = MonthlyData::find($request->data_id);
            return response()->json($data);
        }
    }
}

Zerion Mini Shell 1.0