Lager-v3/app/Http/Controllers/Cabel/CabelController.php

520 lines
21 KiB
PHP

<?php
namespace App\Http\Controllers\Cabel;
use App\Helpers\Logger;
use App\Helpers\PaginationHelper;
use App\Http\Controllers\Controller;
use App\Models\Cabel;
use App\Models\CabelCategory;
use App\Models\Permission;
use App\Models\Product;
use Illuminate\Auth\Access\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class CabelController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
*/
public function index(Request $request)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'cabels_viewAny'))
? Response::allow()
: Response::deny('you are not the chosen one');
$search_types = [];
array_push($search_types,array("value" => "name", "name" => "name"));
array_push($search_types,array("value" => "category", "name" => "category"));
array_push($search_types,array("value" => "available", "name" => "available"));
array_push($search_types,array("value" => "loans", "name" => "loaned"));
array_push($search_types,array("value" => "reservations", "name" => "reserved"));
array_push($search_types,array("value" => "total", "name" => "total"));
$PerPagination = $request->input('p') ?? 10;
$search_term = $request->input('search_term');
$search_type = $request->input('search_type');
$search_compare = $request->input('search_compare');
if($search_term != ""){
switch ($search_type){
case "category":
switch($search_compare){
case("="):
$cabels = Cabel::where(function ($query) use ($search_term){
$query->whereHas('category',function ($query) use ($search_term){
$query->where('name','=',$search_term);
});
})->paginate($PerPagination);
break;
default:
$cabels = Cabel::where(function ($query) use ($search_term){
$query->whereHas('category',function ($query) use ($search_term){
$query->where('name','like','%' . $search_term . '%');
});
})->paginate($PerPagination);
break;
}
break;
case "name":
switch($search_compare){
case("="):
$cabels = Cabel::where('name','=',$search_term)->paginate($PerPagination);
break;
default:
$cabels = Cabel::where('name','like','%' . $search_term . '%')->paginate($PerPagination);
break;
}
break;
case "available":
$all_cabels = Cabel::all();
$cabel_collection = collect();
foreach($all_cabels as $cabel){
$loans = count($cabel->loans);
$reservations = count($cabel->reservations);
$total = $cabel->total;
$available = $total - ($loans + $reservations);
switch($search_compare){
case(">="):
if($available >= $search_term){
$cabel_collection->add($cabel);
}
break;
case("<="):
if($available <= $search_term){
$cabel_collection->add($cabel);
}
break;
case("="):
if($available == $search_term){
$cabel_collection->add($cabel);
}
break;
default:
if($available == $search_term){
$cabel_collection->add($cabel);
}
break;
}
}
$cabels = PaginationHelper::paginate($cabel_collection, $PerPagination);
break;
case "loans":
switch($search_compare){
case(">="):
$cabels = Cabel::has('loans', '>=' , $search_term)->paginate($PerPagination);
break;
case("<="):
$cabels = Cabel::has('loans', '<=' , $search_term)->paginate($PerPagination);
break;
case("="):
$cabels = Cabel::has('loans', '=' , $search_term)->paginate($PerPagination);
break;
default:
$cabels = Cabel::has('loans', '=' , $search_term)->paginate($PerPagination);
break;
}
break;
case "reservations":
switch($search_compare){
case(">="):
$cabels = Cabel::has('reservations', '>=' , $search_term)->paginate($PerPagination);
break;
case("<="):
$cabels = Cabel::has('reservations', '<=' , $search_term)->paginate($PerPagination);
break;
case("="):
$cabels = Cabel::has('reservations', '=' , $search_term)->paginate($PerPagination);
break;
default:
$cabels = Cabel::has('reservations', '=' , $search_term)->paginate($PerPagination);
break;
}
break;
case "total":
switch($search_compare){
case(">="):
$cabels = Cabel::where('total','>=',$search_term)->paginate($PerPagination);
break;
case("<="):
$cabels = Cabel::where('total','<=',$search_term)->paginate($PerPagination);
break;
case("="):
$cabels = Cabel::where('total','=',$search_term)->paginate($PerPagination);
break;
default:
$cabels = Cabel::where('total','=',$search_term)->paginate($PerPagination);
break;
}
break;
}
}
else{
$cabels = Cabel::paginate($PerPagination);
}
return view('cabels.index')
->with('search_types',$search_types)
->with('data',$cabels)
->with('data_name','cabel')
->with('data_names','cabels')
;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
*/
public function deleted(Request $request)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'cabels_viewAny_deleted'))
? Response::allow()
: Response::deny('you are not the chosen one');
$search_types = [];
array_push($search_types,array("value" => "name", "name" => "name"));
array_push($search_types,array("value" => "category", "name" => "category"));
array_push($search_types,array("value" => "available", "name" => "available"));
array_push($search_types,array("value" => "loans", "name" => "loaned"));
array_push($search_types,array("value" => "reservations", "name" => "reserved"));
array_push($search_types,array("value" => "total", "name" => "total"));
$PerPagination = $request->input('p') ?? 10;
$search_term = $request->input('search_term');
$search_type = $request->input('search_type');
$search_compare = $request->input('search_compare');
if($search_term != ""){
switch ($search_type){
case "category":
switch($search_compare){
case("="):
$cabels = Cabel::onlyTrashed()->where(function ($query) use ($search_term){
$query->whereHas('category',function ($query) use ($search_term){
$query->where('name','=',$search_term);
});
})->paginate($PerPagination);
break;
default:
$cabels = Cabel::onlyTrashed()->where(function ($query) use ($search_term){
$query->whereHas('category',function ($query) use ($search_term){
$query->where('name','like','%' . $search_term . '%');
});
})->paginate($PerPagination);
break;
}
break;
case "name":
switch($search_compare){
case("="):
$cabels = Cabel::onlyTrashed()->where('name','=',$search_term)->paginate($PerPagination);
break;
default:
$cabels = Cabel::onlyTrashed()->where('name','like','%' . $search_term . '%')->paginate($PerPagination);
break;
}
break;
case "available":
$all_cabels = Cabel::all();
$cabel_collection = collect();
foreach($all_cabels as $cabel){
$loans = count($cabel->loans);
$reservations = count($cabel->reservations);
$total = $cabel->total;
$available = $total - ($loans + $reservations);
switch($search_compare){
case(">="):
if($available >= $search_term){
$cabel_collection->add($cabel);
}
break;
case("<="):
if($available <= $search_term){
$cabel_collection->add($cabel);
}
break;
case("="):
if($available == $search_term){
$cabel_collection->add($cabel);
}
break;
default:
if($available == $search_term){
$cabel_collection->add($cabel);
}
break;
}
}
$cabels = PaginationHelper::paginate($cabel_collection, $PerPagination);
break;
case "loans":
switch($search_compare){
case(">="):
$cabels = Cabel::has('loans', '>=' , $search_term)->paginate($PerPagination);
break;
case("<="):
$cabels = Cabel::has('loans', '<=' , $search_term)->paginate($PerPagination);
break;
case("="):
$cabels = Cabel::has('loans', '=' , $search_term)->paginate($PerPagination);
break;
default:
$cabels = Cabel::has('loans', '=' , $search_term)->paginate($PerPagination);
break;
}
break;
case "reservations":
switch($search_compare){
case(">="):
$cabels = Cabel::has('reservations', '>=' , $search_term)->paginate($PerPagination);
break;
case("<="):
$cabels = Cabel::has('reservations', '<=' , $search_term)->paginate($PerPagination);
break;
case("="):
$cabels = Cabel::has('reservations', '=' , $search_term)->paginate($PerPagination);
break;
default:
$cabels = Cabel::has('reservations', '=' , $search_term)->paginate($PerPagination);
break;
}
break;
case "total":
switch($search_compare){
case(">="):
$cabels = Cabel::where('total','>=',$search_term)->paginate($PerPagination);
break;
case("<="):
$cabels = Cabel::where('total','<=',$search_term)->paginate($PerPagination);
break;
case("="):
$cabels = Cabel::where('total','=',$search_term)->paginate($PerPagination);
break;
default:
$cabels = Cabel::where('total','=',$search_term)->paginate($PerPagination);
break;
}
break;
}
}
else{
$cabels = Cabel::onlyTrashed()->paginate($PerPagination);
}
return view('cabels.deleted')
->with('search_types',$search_types)
->with('data',$cabels)
->with('data_name','cabel')
->with('data_names','cabels')
;
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
*/
public function create()
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'cabels_create'))
? Response::allow()
: Response::deny('you are not the chosen one');
return view('cabels.create')
->with('categories',CabelCategory::all())
;
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(Request $request)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'cabels_create'))
? Response::allow()
: Response::deny('you are not the chosen one');
$cabel = new Cabel();
$cabel->cabel_category_id = $request->category_id;
$cabel->name = $request->name;
$cabel->save();
Logger::LogCreated($cabel->id,get_class($cabel));
return redirect()->route('cabels.show',['cabel' => $cabel]);
}
/**
* Display the specified resource.
*
* @param \App\Models\Cabel $cabel
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
*/
public function show($cabel)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'cabels_view'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Cabel::where('id','=',$cabel)->withTrashed()->first();
return view('cabels.show')
->with('data',$object)
->with('data_name','cabel')
;
}
/**
* Show the form for editing the specified resource.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
*/
public function edit($cabel)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'cabels_edit'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Cabel::where('id','=',$cabel)->withTrashed()->first();
return view('cabels.edit')
->with('categories',CabelCategory::all())
->with('data',$object)
->with('data_name','cabel')
;
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function update(Request $request,$cabel)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'cabels_edit'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Cabel::where('id','=',$cabel)->withTrashed()->first();
if($object->cabel_category_id != $request->category_id){
$CabelCategory = CabelCategory::where('id','=',$request->category_id)->first();
Logger::LogEdited($object->id,get_class($object),$logMessage = "Kategori : ".$object->category->name." til ".$CabelCategory->name);
$object->cabel_category_id = $request->category_id;
}
if($object->name != $request->name){
Logger::LogEdited($object->id,get_class($object),$logMessage = "Navn : ".$object->name." til ".$request->name);
$object->name = $request->name;
}
$object->save();
return redirect()->route('cabels.show',['cabel' => $cabel]);
}
/**
* Remove the specified resource from storage.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy($cabel)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'cabels_delete'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Cabel::where('id','=',$cabel)->withTrashed()->first();
Logger::LogDeleted($object->id,get_class($object));
$object->delete();
return redirect()->route('cabels.index');
}
/**
* Permanently Remove the specified resource from storage.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function delete_force($cabel)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'cabels_delete_force'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Cabel::withTrashed()->where('id','=',$cabel)->first();
Logger::LogForceDeleted($object->id,get_class($object));
$object->forceDelete();
return redirect()->route('cabels.index');
}
/**
* Restore the specified resource from storage.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function restore($cabel)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'cabels_restore'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Cabel::withTrashed()->where('id','=',$cabel)->first();
$object->restore();
Logger::LogRestored($object->id,get_class($object));
return redirect()->route('cabels.deleted');
}
/**
* Add the specified amount to the Pool.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function amount_add($cabel,Request $request)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'cabels_amount_add'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Cabel::where('id','=',$cabel)->first();
$object->total += $request->amount;
$object->save();
Logger::LogAmountAdded($object->id,get_class($object),$request->amount);
return redirect()->route('cabels.show',['cabel' => $cabel]);
}
/**Logger::LogAmountAdded($object->id,get_class($object),$request->amount);
* Remove the specified amount from the Pool.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function amount_remove($cabel,Request $request)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'cabels_amount_remove'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Cabel::where('id','=',$cabel)->first();
$object->total -= $request->amount;
$object->save();
Logger::LogAmountRemoved($object->id,get_class($object),$request->amount);
return redirect()->route('cabels.show',['cabel' => $cabel]);
}
}