%PDF- %PDF-
Direktori : /home/silvzytp/dsr_code/app/Http/Controllers/Backend/ |
Current File : //home/silvzytp/dsr_code/app/Http/Controllers/Backend/AgentController.php |
<?php namespace App\Http\Controllers\Backend; use DataTables; use App\Models\Role; use App\Models\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Hash; use App\Http\Requests\AgentRequest; use App\Http\Requests\ManagerFormRequest; class AgentController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { Gate::authorize('app.agents.index'); // page title page_title('Agents'); // breadcrumb $breadcrumb = ['Dashboard'=>route('super.dashboard'),'Agents'=>'']; // managers $managers = User::toBase()->where('role_id',3)->select('id','name')->get(); return view('backend.pages.agents.index',['breadcrumb'=>$breadcrumb,'managers'=>$managers]); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function getData(Request $request){ if ($request->ajax()) { if (Auth::user()->role->slug == 'admin' || Auth::user()->role->slug == 'super-admin') { $users = User::with('role')->where('role_id',4)->latest('id'); }else{ $users = User::with('role')->where(['role_id'=>4,'manager_id'=>Auth::id()])->latest('id'); } return DataTables::eloquent($users) ->addIndexColumn() ->filter(function ($query) use ($request){ if (!empty($request->search_text)) { $query->where('role_id',4) ->where('name', 'LIKE',"%{$request->search_text}%") ->orWhere(function($whereQuery) use ($request){ $whereQuery->where('role_id',4) ->where('email', 'LIKE',"%{$request->search_text}%"); }); } }) ->addColumn('role', function($user){ $roleName = $user->role ? $user->role->name : ''; return '<span class="badge badge-warning">'.$roleName.'</span>'; }) ->addColumn('bulk_check', function($user){ if($user->manager_id != 0){ return ' <label class="ui-checkbox ui-checkbox-gray disabled"> <input type="checkbox"> <span class="input-span disabled"></span> </label>'; }else{ return ' <label class="ui-checkbox"> <input type="checkbox" name="checked" value="'.$user->id.'"> <span class="input-span"></span> </label> '; } }) ->addColumn('action', function($user){ $action = ''; if (Gate::allows('app.agents.show')) { $action .= ' <a href="'.route(routeName().'.agents.show', $user->id).'" class="btn-style btn-style-view btn-view ml-1" data-id="'.$user->id.'"><i class="fa fa-eye"></i> </a>'; } if (Gate::allows('app.agents.edit')) { $action .= '<a class="btn-style btn-style-edit ml-1" href="'.route(routeName().'.agents.edit',$user->id).'"><i class="fa fa-edit"></i></a>'; } if (Gate::allows('app.agents.destroy')) { $action .= '<button class="btn-style btn-style-danger deletable-btn ml-1" data-title="Do you really want to delete this record?" data-id="'.$user->id.'"><i class="fa fa-trash"></i></button>'; } return '<div class="dropdown show text-right">'.$action.'</div>'; }) ->rawColumns(['bulk_check','profile','role','action']) ->make(true); } } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { Gate::authorize('app.agents.create'); // page title page_title('New Agent'); // breadcrumb $breadcrumb = ['Dashboard'=>route('super.dashboard'),'Agents'=>route(routeName().'.agents.index'),'Create'=>'']; return view('backend.pages.agents.form',['breadcrumb'=>$breadcrumb]); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\AgentRequest $request * @return \Illuminate\Http\Response */ public function store(AgentRequest $request) { // agent create User::create([ 'role_id' => 4, 'name' => $request->full_name, 'email' => $request->email, 'password' => Hash::make(unique_data_generate(0,8)), 'password_text' => unique_data_generate(0,8), 'phone_no' => $request->phone, 'target' => $request->target, 'note' => $request->description, ]); return redirect()->route(routeName().'.agents.index')->with('success',ALERT_SUCCESS); } public function show($id){ Gate::authorize('app.agents.show'); $agent = User::findOrFail($id); // page title page_title('Agent Details'); // breadcrumb $breadcrumb = ['Dashboard'=>route('super.dashboard'),'Agents'=>route(routeName().'.agents.index'),'Details'=>'']; return view('backend.pages.agents.view',['breadcrumb'=>$breadcrumb,'agent'=>$agent]); } /** * User status * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function status(Request $request){ if ($request->ajax()) { User::findOrFail($request->data_id)->update(['status'=>$request->status]); $output = ['status'=>'success','message'=>'Status updated.']; return response()->json($output); } } /** * Show the form for editing the specified resource. * * @param \App\Models\User $user * @return \Illuminate\Http\Response */ public function edit($id) { Gate::authorize('app.agents.edit'); $agent = User::findOrFail($id); $managers = User::where('role_id', 3)->get(); // page title page_title('Edit Agent'); // breadcrumb $breadcrumb = ['Dashboard'=>route('super.dashboard'),'Agents'=>route(routeName().'.agents.index'),'Edit'=>'']; return view('backend.pages.agents.form',['breadcrumb'=>$breadcrumb,'agent'=>$agent,'managers'=>$managers]); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\AgentRequest $request * @param \App\Models\User $id * @return \Illuminate\Http\Response */ public function update(AgentRequest $request, $id) { $agent = User::findOrFail($id); $manager = User::findOrFail($agent->manager_id); if($agent->manager_id != $request->manager){ $manager = User::find($request->manager); $agentTarget = ($manager->target + $request->target); $manager->update(['target'=>$agentTarget]); // old manager target minus $oldManager = User::find($agent->manager_id); $agentOldTarget = ($oldManager->target - $request->target); $oldManager->update(['target'=>$agentOldTarget]); } if ($agent->target > $request->target) { $target = ($agent->target - $request->target); // manager target minus update $managerTarget = ($manager->target - $target); }elseif ($agent->target < $request->target) { $target = ($request->target - $agent->target); // manager target sum update $managerTarget = ($manager->target + $target); }else{ $managerTarget = $manager->target; } // manager target update $manager->update([ 'target'=>$managerTarget ]); // agent update $agent->update([ 'manager_id' => $request->manager, 'name' => $request->full_name, 'email' => $request->email, 'phone_no' => $request->phone, 'target' => $request->target, 'note' => $request->description, ]); return back()->with('success', ALERT_UPDATE); } /** * Remove the specified resource from storage. * * @param \App\Models\User $user * @return \Illuminate\Http\Response */ public function destroy(Request $request) { if ($request->ajax()) { if (Gate::allows('app.agents.destroy')) { $agent = User::with('manager')->find($request->data_id); if ($agent == true) { if ($agent->manager && $agent->manager_id != 0) { // old manager target minus $manager = User::find($agent->manager_id); $managerTarget = ($manager->target - $agent->target); $manager->update(['target'=>$managerTarget]); } file_remove($agent->avatar); // profile image remove from storage $agent->delete(); // user remove from database $output = response_alert('success',ALERT_DELETE); }else{ $output = response_alert('error',SERVER_ERROR); } } else { $output = response_alert('error',UNAUTHORIZED); } return response()->json($output); } } /** * Remove the users resource from storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function bulkDestroy(Request $request){ if ($request->ajax()) { if (!empty($request->bulk_id)) { $userId = explode(',', $request->bulk_id); $agent = User::whereIn('id', $userId); if ($agent) { if ($request->operation_type == 'delete') { $agent->delete(); $output = response_alert('success',ALERT_DELETE); }elseif($request->operation_type == 'assign'){ if (!empty($request->manager)) { $manager = User::find($request->manager); $t_target = ($manager->target + $agent->sum('target')); if ($manager == true) { // assign aggent by manager id $agent->update([ 'manager_id'=>$request->manager ]); // manager target update $manager->update([ 'target'=>$t_target, 'target_date' => now() ]); $output = response_alert('success','Agent assign successfull.'); }else{ $output = response_alert('error','Somthing Wrong!'); } }else{ $output = response_alert('error','Please selected try'); } }else{ $output = response_alert('error','Operation somthing wrong!'); } }else{ $output = response_alert('error',SERVER_ERROR); } }else{ $output = response_alert('error','Please selected try'); } return response()->json($output); } } }