%PDF- %PDF-
Mini Shell

Mini Shell

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

<?php

namespace App\Http\Controllers\Backend\Super;

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 DataTables;

class MonthlyDataController extends Controller
{

    public function index()
    {
        // page title
        page_title('Monthly Data');
        // breadcrumb
        $breadcrumb = ['Dashboard'=>route('super.dashboard'),'Monthly Data'=>''];
        $agents = MonthlyData::select('id','agent_name')->groupBy('agent_name')->get();
        return view('backend.super-admin.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){
        $getData = MonthlyData::latest('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){
                return '<div class="d-flex align-items-center justify-content-end">
                    <a href="'.route('super.monthly-data.edit', $data->id).'" class="btn-style btn-style-edit mr-1"><i class="fa fa-edit"></i></a>

                    <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>
                </div>';
            })
            ->rawColumns(['created_at','action','remark'])
            ->make(true);
    }

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

        $roles = User::where('role_id',4)->get();

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

    public function store(Request $request)
    {
        $request->validate([
            'revenue_date'       => 'required',
            'agent_name'         => 'required',
            'sale_achievment'    => 'nullable|integer',
            'revenue_achievment' => 'nullable|integer',
            's_media_lead'       => 'nullable|integer',
            'dj_lead'            => 'nullable|integer',
            'telesale_l'         => 'nullable|integer',
            'c_caling_l'         => 'nullable|integer',
            'old_s_media_l'      => 'nullable|integer',
            'meeting_attendance' => 'nullable|integer',
            'n_of_deal'          => 'nullable|integer',
            'n_of_call'          => 'nullable|integer',
            'connected_call'     => 'nullable|integer'
        ]);

        $managerId = $request->manager;
        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('super.monthly-data.index')->with('success', ALERT_SUCCESS);
    }


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

        $roles = User::where('role_id',4)->select('name')->get();

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

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

        $request->validate([
            'revenue_date'       => 'required',
            'agent_name'         => 'required',
            'sale_achievment'    => 'nullable|integer',
            'revenue_achievment' => 'nullable|integer',
            's_media_lead'       => 'nullable|integer',
            'dj_lead'            => 'nullable|integer',
            'telesale_l'         => 'nullable|integer',
            'c_caling_l'         => 'nullable|integer',
            'old_s_media_l'      => 'nullable|integer',
            'meeting_attendance' => 'nullable|integer',
            'n_of_deal'          => 'nullable|integer',
            'n_of_call'          => 'nullable|integer',
            'connected_call'     => 'nullable|integer'
        ]);

        $monthlyData = MonthlyData::findOrFail($id);
        $managerId = $request->manager;
        $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()) {
            MonthlyData::findOrFail($request->data_id)->delete();
            $output = ['status'=>'success','message'=>ALERT_DELETE];
            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