246 lines
7.3 KiB
PHP
246 lines
7.3 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Admin;
|
||
|
|
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Absent;
|
||
|
|
use App\User;
|
||
|
|
use Session;
|
||
|
|
use DB;
|
||
|
|
use Carbon;
|
||
|
|
|
||
|
|
class AbsentsController extends Controller
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Create a new controller instance.
|
||
|
|
*
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
public function __construct()
|
||
|
|
{
|
||
|
|
$this->middleware('auth:admin');
|
||
|
|
}
|
||
|
|
/**
|
||
|
|
* Display a listing of the resource.
|
||
|
|
*
|
||
|
|
* @return \Illuminate\Http\Response
|
||
|
|
*/
|
||
|
|
public function reset(Request $request)
|
||
|
|
{
|
||
|
|
if($request->session()->has('search_options')):
|
||
|
|
$request->session()->forget('search_options');
|
||
|
|
endif;
|
||
|
|
return redirect()->route('absents');
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public function index(Request $request)
|
||
|
|
{
|
||
|
|
$condition=$search_options=[];
|
||
|
|
$input = $request->all();
|
||
|
|
if(!empty($input)):
|
||
|
|
$request->session()->put('search_options', $input);
|
||
|
|
endif;
|
||
|
|
if($request->session()->has('search_options')):
|
||
|
|
$search_options=$request->session()->get('search_options');
|
||
|
|
if($search_options['user_id'] != ""):
|
||
|
|
array_push($condition, ['absents.user_id', '=', $search_options['user_id']]);
|
||
|
|
endif;
|
||
|
|
if($search_options['absent_date'] != ""):
|
||
|
|
$search_options['absent_date'] = Carbon\Carbon::createFromFormat('m/d/Y', $search_options['absent_date'])->format('Y-m-d');
|
||
|
|
array_push($condition, ['absents.start_date', '<=', $search_options['absent_date']]);
|
||
|
|
array_push($condition, ['absents.end_date', '>=', $search_options['absent_date']]);
|
||
|
|
endif;
|
||
|
|
endif;
|
||
|
|
|
||
|
|
if(!empty($condition))
|
||
|
|
{
|
||
|
|
$absents = DB::table('absents')
|
||
|
|
->join('users', 'absents.user_id', '=', 'users.id')
|
||
|
|
->join('role_user', 'absents.user_id', '=', 'role_user.user_id')
|
||
|
|
->join('roles', 'role_user.role_id', '=', 'roles.id')
|
||
|
|
->select(DB::raw("CONCAT(users.first_name, ' ',users.last_name) AS user_details"), 'absents.*')
|
||
|
|
->orderBy('absents.id', 'desc')
|
||
|
|
->where($condition)
|
||
|
|
->get();
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
$absents = DB::table('absents')
|
||
|
|
->join('users', 'absents.user_id', '=', 'users.id')
|
||
|
|
->join('role_user', 'absents.user_id', '=', 'role_user.user_id')
|
||
|
|
->join('roles', 'role_user.role_id', '=', 'roles.id')
|
||
|
|
->select(DB::raw("CONCAT(users.first_name, ' ',users.last_name) AS user_details"), 'absents.*', 'roles.name as role_name')
|
||
|
|
->orderBy('absents.id', 'desc')
|
||
|
|
->get();
|
||
|
|
}
|
||
|
|
$user_lists = DB::table('users')
|
||
|
|
->join('role_user', 'users.id', '=', 'role_user.user_id')
|
||
|
|
->join('roles', 'role_user.role_id', '=', 'roles.id')
|
||
|
|
->where('users.status', '=', '1')
|
||
|
|
->select('users.*', 'roles.name as role_name')
|
||
|
|
->orderBy('users.id', 'desc')
|
||
|
|
->get();
|
||
|
|
return view('admin.absents.index', compact('absents', 'user_lists', 'search_options'));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Show the form for creating a new resource.
|
||
|
|
*
|
||
|
|
* @return \Illuminate\Http\Response
|
||
|
|
*/
|
||
|
|
public function create()
|
||
|
|
{
|
||
|
|
$user_lists = DB::table('users')
|
||
|
|
->join('role_user', 'users.id', '=', 'role_user.user_id')
|
||
|
|
->join('roles', 'role_user.role_id', '=', 'roles.id')
|
||
|
|
->where('users.status', '=', '1')
|
||
|
|
->select('users.*', 'roles.name as role_name')
|
||
|
|
->orderBy('users.id', 'desc')
|
||
|
|
->get();
|
||
|
|
return view('admin.absents.create', compact('user_lists'));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Store a newly created resource in storage.
|
||
|
|
*
|
||
|
|
* @param \Illuminate\Http\Request $request
|
||
|
|
* @return \Illuminate\Http\Response
|
||
|
|
*/
|
||
|
|
public function store(Request $request)
|
||
|
|
{
|
||
|
|
$validatedData = $this->validate($request, [
|
||
|
|
'user_id' => 'required',
|
||
|
|
'start_date' => 'required',
|
||
|
|
]);
|
||
|
|
$input = $request->all();
|
||
|
|
if($input['start_date'] != "")
|
||
|
|
$input['start_date'] = Carbon\Carbon::createFromFormat('m/d/Y', $input['start_date'])->format('Y-m-d');
|
||
|
|
if($input['end_date'] != ""):
|
||
|
|
$input['end_date'] = Carbon\Carbon::createFromFormat('m/d/Y', $input['end_date'])->format('Y-m-d');
|
||
|
|
else:
|
||
|
|
$input['end_date']=$input['start_date'];
|
||
|
|
endif;
|
||
|
|
$saved_data=Absent::create($input);
|
||
|
|
if($saved_data)
|
||
|
|
{
|
||
|
|
Session::flash('success_message', 'Absent has been added successfully');
|
||
|
|
return redirect()->route('absents');
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
Session::flash('error_message', 'We are having some problem. Please try later.');
|
||
|
|
return redirect()->route('add-absent');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Display the specified resource.
|
||
|
|
*
|
||
|
|
* @param int $id
|
||
|
|
* @return \Illuminate\Http\Response
|
||
|
|
*/
|
||
|
|
public function show($id)
|
||
|
|
{
|
||
|
|
//
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Show the form for editing the specified resource.
|
||
|
|
*
|
||
|
|
* @param int $id
|
||
|
|
* @return \Illuminate\Http\Response
|
||
|
|
*/
|
||
|
|
public function edit($id)
|
||
|
|
{
|
||
|
|
$id=base64_decode($id);
|
||
|
|
$user_lists = DB::table('users')
|
||
|
|
->join('role_user', 'users.id', '=', 'role_user.user_id')
|
||
|
|
->join('roles', 'role_user.role_id', '=', 'roles.id')
|
||
|
|
->where('users.status', '=', '1')
|
||
|
|
->select('users.*', 'roles.name as role_name')
|
||
|
|
->orderBy('users.id', 'desc')
|
||
|
|
->get();
|
||
|
|
$absent = Absent::where('id', '=', $id)->first();
|
||
|
|
if($absent):
|
||
|
|
return view('admin.absents.edit', compact('absent', 'user_lists'));
|
||
|
|
else:
|
||
|
|
Session::flash('error_message', 'Invalid Absent id.');
|
||
|
|
return redirect()->route('absents');
|
||
|
|
endif;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Update the specified resource in storage.
|
||
|
|
*
|
||
|
|
* @param \Illuminate\Http\Request $request
|
||
|
|
* @param int $id
|
||
|
|
* @return \Illuminate\Http\Response
|
||
|
|
*/
|
||
|
|
public function update(Request $request, $id)
|
||
|
|
{
|
||
|
|
$id=base64_decode($id);
|
||
|
|
$absent = Absent::findOrFail($id);
|
||
|
|
$validatedData = $this->validate($request, [
|
||
|
|
'user_id' => 'required',
|
||
|
|
'start_date' => 'required',
|
||
|
|
]);
|
||
|
|
$input = $request->all();
|
||
|
|
if($input['start_date'] != "")
|
||
|
|
$input['start_date'] = Carbon\Carbon::createFromFormat('m/d/Y', $input['start_date'])->format('Y-m-d');
|
||
|
|
if($input['end_date'] != ""):
|
||
|
|
$input['end_date'] = Carbon\Carbon::createFromFormat('m/d/Y', $input['end_date'])->format('Y-m-d');
|
||
|
|
else:
|
||
|
|
$input['end_date']=$input['start_date'];
|
||
|
|
endif;
|
||
|
|
$saved_data=$absent->fill($input)->save();
|
||
|
|
if($saved_data):
|
||
|
|
Session::flash('success_message', 'Absent has been updated successfully');
|
||
|
|
return redirect()->route('absents');
|
||
|
|
else:
|
||
|
|
Session::flash('error_message', 'We are having some problem. Please try later.');
|
||
|
|
return redirect()->route('edit-absent');
|
||
|
|
endif;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Remove the specified resource from storage.
|
||
|
|
*
|
||
|
|
* @param int $id
|
||
|
|
* @return \Illuminate\Http\Response
|
||
|
|
*/
|
||
|
|
public function destroy($id)
|
||
|
|
{
|
||
|
|
$id=base64_decode($id);
|
||
|
|
$delete_role = Absent::where('id', $id)->delete();
|
||
|
|
if($delete_role):
|
||
|
|
Session::flash('success_message', 'Absent has been deleted successfully');
|
||
|
|
return redirect()->route('absents');
|
||
|
|
else:
|
||
|
|
Session::flash('error_message', 'We are having some problem. Please try later.');
|
||
|
|
return redirect()->route('absents');
|
||
|
|
endif;
|
||
|
|
}
|
||
|
|
public function statusupdate($id)
|
||
|
|
{
|
||
|
|
$id=base64_decode($id);
|
||
|
|
$find_role = Absent::findOrFail($id);
|
||
|
|
if($find_role->status==1)
|
||
|
|
$status=0;
|
||
|
|
else
|
||
|
|
$status=1;
|
||
|
|
$input=[
|
||
|
|
'status'=>$status
|
||
|
|
];
|
||
|
|
$saved_data=$find_role->fill($input)->save();
|
||
|
|
if($saved_data):
|
||
|
|
Session::flash('success_message', 'Absent has been updated successfully');
|
||
|
|
return redirect()->route('absents');
|
||
|
|
else:
|
||
|
|
Session::flash('error_message', 'We are having some problem. Please try later.');
|
||
|
|
return redirect()->route('absents');
|
||
|
|
endif;
|
||
|
|
}
|
||
|
|
}
|