Lager-v3/app/Http/Controllers/Product/ProductController.php

507 lines
22 KiB
PHP

<?php
namespace App\Http\Controllers\Product;
use App\Helpers\Logger;
use App\Helpers\PaginationHelper;
use App\Http\Controllers\Controller;
use App\Models\Brand;
use App\Models\Permission;
use App\Models\Product;
use App\Models\ProductCategory;
use App\Models\ProductModel;
use App\Models\ProductSubcategory;
use Illuminate\Auth\Access\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class ProductController 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', '=', 'products_viewAny'))
? Response::allow()
: Response::deny('you are not the chosen one');
$search_types = [];
if(config('app.barcode_mode') == 'static'){
array_push($search_types,array("value" => "barcode", "name" => "barcode"));
}
array_push($search_types,array("value" => "category", "name" => "category"));
array_push($search_types,array("value" => "subcategory", "name" => "subcategory"));
array_push($search_types,array("value" => "brand", "name" => "brand"));
array_push($search_types,array("value" => "model", "name" => "model"));
array_push($search_types,array("value" => "name", "name" => "name"));
array_push($search_types,array("value" => "description", "name" => "description"));
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("="):
$products = Product::where(function ($query) use ($search_term){
$query->whereHas('category',function ($query) use ($search_term){
$query->where('name','=',$search_term);
});
})->paginate($PerPagination);
break;
default:
$products = Product::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 "subcategory":
switch($search_compare){
case("="):
$products = Product::where(function ($query) use ($search_term){
$query->whereHas('subcategory',function ($query) use ($search_term){
$query->where('name','=',$search_term);
});
})->paginate($PerPagination);
break;
default:
$products = Product::where(function ($query) use ($search_term){
$query->whereHas('subcategory',function ($query) use ($search_term){
$query->where('name','like','%' . $search_term . '%');
});
})->paginate($PerPagination);
break;
}
break;
case "brand":
switch($search_compare){
case("="):
$products = Product::where(function ($query) use ($search_term){
$query->whereHas('brand',function ($query) use ($search_term){
$query->where('name','=',$search_term);
});
})->paginate($PerPagination);
break;
default:
$products = Product::where(function ($query) use ($search_term){
$query->whereHas('brand',function ($query) use ($search_term){
$query->where('name','like','%' . $search_term . '%');
});
})->paginate($PerPagination);
break;
}
break;
case "model":
switch($search_compare){
case("="):
$products = Product::where(function ($query) use ($search_term){
$query->whereHas('model',function ($query) use ($search_term){
$query->where('name','=',$search_term);
});
})->paginate($PerPagination);
break;
default:
$products = Product::where(function ($query) use ($search_term){
$query->whereHas('model',function ($query) use ($search_term){
$query->where('name','like','%' . $search_term . '%');
});
})->paginate($PerPagination);
break;
}
break;
case "name":
switch($search_compare){
case("="):
$products = Product::where('name','=',$search_term)->paginate($PerPagination);
break;
default:
$products = Product::where('name','like','%' . $search_term . '%')->paginate($PerPagination);
break;
}
break;
case "description":
switch($search_compare){
case("="):
$products = Product::where('description','=',$search_term)->paginate($PerPagination);
break;
default:
$products = Product::where('description','like','%' . $search_term . '%')->paginate($PerPagination);
break;
}
break;
case "available":
$all_products = Product::all();
$product_collection = collect();
foreach($all_products as $product){
$loans = count($product->loans);
$reservations = count($product->reservations);
$total = $product->total;
$available = $total - ($loans + $reservations);
switch($search_compare){
case(">="):
if($available >= $search_term){
$product_collection->add($product);
}
break;
case("<="):
if($available <= $search_term){
$product_collection->add($product);
}
break;
case("="):
if($available == $search_term){
$product_collection->add($product);
}
break;
default:
if($available == $search_term){
$product_collection->add($product);
}
break;
}
}
$products = PaginationHelper::paginate($product_collection, $PerPagination);
break;
case "loans":
switch($search_compare){
case(">="):
$products = Product::has('loans', '>=' , $search_term)->paginate($PerPagination);
break;
case("<="):
$products = Product::has('loans', '<=' , $search_term)->paginate($PerPagination);
break;
case("="):
$products = Product::has('loans', '=' , $search_term)->paginate($PerPagination);
break;
default:
$products = Product::has('loans', '=' , $search_term)->paginate($PerPagination);
break;
}
break;
case "reservations":
switch($search_compare){
case(">="):
$products = Product::has('reservations', '>=' , $search_term)->paginate($PerPagination);
break;
case("<="):
$products = Product::has('reservations', '<=' , $search_term)->paginate($PerPagination);
break;
case("="):
$products = Product::has('reservations', '=' , $search_term)->paginate($PerPagination);
break;
default:
$products = Product::has('reservations', '=' , $search_term)->paginate($PerPagination);
break;
}
break;
case "total":
switch($search_compare){
case(">="):
$products = Product::where('total','>=',$search_term)->paginate($PerPagination);
break;
case("<="):
$products = Product::where('total','<=',$search_term)->paginate($PerPagination);
break;
case("="):
$products = Product::where('total','=',$search_term)->paginate($PerPagination);
break;
default:
$products = Product::where('total','=',$search_term)->paginate($PerPagination);
break;
}
break;
}
}
else{
$products = Product::paginate($PerPagination);
}
return view('products.index')
->with('search_types',$search_types)
->with('data',$products)
->with('data_name','product')
->with('data_names','products')
;
}
/**
* 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', '=', 'products_viewAny_deleted'))
? Response::allow()
: Response::deny('you are not the chosen one');
$search_types = [];
if(config('app.barcode_mode') == 'static'){
array_push($search_types,array("value" => "barcode", "name" => "barcode"));
}
array_push($search_types,array("value" => "category", "name" => "category"));
array_push($search_types,array("value" => "subcategory", "name" => "subcategory"));
array_push($search_types,array("value" => "brand", "name" => "brand"));
array_push($search_types,array("value" => "model", "name" => "model"));
array_push($search_types,array("value" => "name", "name" => "name"));
array_push($search_types,array("value" => "description", "name" => "description"));
$PerPagination = $request->input('p') ?? 10;
$search_term = $request->input('search_term');
$search_type = $request->input('search_type');
$search_compare = $request->input('search_compare');
$products = Product::onlyTrashed()->Paginate($PerPagination);
return view('products.deleted')
->with('search_types',$search_types)
->with('data',$products)
->with('data_name','product')
->with('data_names','products')
;
}
/**
* 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', '=', 'products_create'))
? Response::allow()
: Response::deny('you are not the chosen one');
return view('products.create')
->with('categories',ProductCategory::all())
->with('subcategories',ProductSubcategory::all())
->with('brands',Brand::has('models')->get())
->with('models',ProductModel::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', '=', 'products_create'))
? Response::allow()
: Response::deny('you are not the chosen one');
$product = new Product();
if(isset($request->barcode)){
$product->barcode = $request->barcode;
}
$product->product_category_id = $request->category_id;
$product->product_subcategory_id = $request->subcategory_id;
$product->brand_id = $request->brand_id;
$product->product_model_id = $request->model_id;
$product->name = $request->name;
$product->description = $request->description;
$product->save();
Logger::LogCreated($product->id,get_class($product));
return redirect()->route('products.index');
}
/**
* Display the specified resource.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
*/
public function show($product)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'products_view'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Product::withTrashed()->where('id','=',$product)->first();
return view('products.show')
->with('data',$object)
;
}
/**
* Show the form for editing the specified resource.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
*/
public function edit($product)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'products_edit'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Product::withTrashed()->where('id','=',$product)->first();
return view('products.edit')
->with('categories',ProductCategory::withTrashed()->get())
->with('subcategories',ProductSubcategory::withTrashed()->get())
->with('brands',Brand::withTrashed()->has('models')->get())
->with('models',ProductModel::withTrashed()->get())
->with('data',$object)
->with('data_name','product')
;
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function update(Request $request,$product)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'products_edit'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Product::withTrashed()->where('id','=',$product)->first();
if(isset($request->barcode)){
if($object->barcode != $request->barcode){
Logger::LogEdited($object->id,get_class($object),"Stregkode : ".$object->barcode." til ".$request->barcode);
$object->barcode = $request->barcode;
}
}
if($object->product_category_id != $request->category_id) {
$category = ProductCategory::where('id','=', $request->category_id)->first();
Logger::LogEdited($object->id,get_class($object),"Kategori : ".$object->category->name." til ".$category->name);
$object->product_category_id = $request->category_id;
}
if($object->product_subcategory_id != $request->subcategory_id) {
$subcategory = ProductSubcategory::where('id','=', $request->subcategory_id)->first();
Logger::LogEdited($object->id,get_class($object),"Underkategori : ".$object->subcategory->name." til ".$subcategory->name);
$object->product_subcategory_id = $request->subcategory_id;
}
if($object->brand_id != $request->brand_id) {
$brand = Brand::where('id','=', $request->brand_id)->first();
Logger::LogEdited($object->id,get_class($object),"Fabrikant : ".$object->brand_id->name." til ".$brand->name);
$object->brand_id = $request->brand_id;
}
if($object->product_model_id != $request->model_id) {
$model = ProductModel::where('id','=', $request->model_id)->first();
Logger::LogEdited($object->id,get_class($object),"Model : ".$object->model->name." til ".$model->name);
$object->product_model_id = $request->model_id;
}
if($object->name != $request->name) {
Logger::LogEdited($object->id,get_class($object),"Navn : ".$object->name." til ".$request->name);
$object->name = $request->name;
}
if($object->description != $request->description) {
Logger::LogEdited($object->id,get_class($object),"Beskrivels : ".$object->description." til ".$request->description);
$object->description = $request->description;
}
$object->save();
return redirect()->route('products.index');
}
/**
* Remove the specified resource from storage.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy($product)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'products_delete'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Product::withTrashed()->where('id','=',$product)->first();
Logger::LogDeleted($object->id,get_class($object));
$object->delete();
return redirect()->route('products.index');
}
/**
* Restore the specified resource from storage.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function restore($product)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'products_restore'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Product::withTrashed()->where('id','=',$product)->first();
Logger::LogRestored($object->id,get_class($object));
$object->restore();
return redirect()->route('products.deleted');
}
/**
* Remove the specified resource from storage.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function forceDelete($product)
{
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'products_delete_force'))
? Response::allow()
: Response::deny('you are not the chosen one');
$object = Product::withTrashed()->where('id','=',$product)->first();
Logger::LogForceDeleted($object->id,get_class($object));
$object->forceDelete();
return redirect()->route('products.deleted');
}
/**
* Add the specified amount to the Pool.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function amount_add($product,Request $request)
{
$object = Product::withTrashed()->where('id','=',$product)->first();
$object->total += $request->amount;
$object->save();
Logger::LogAmountAdded($object->id,get_class($object),$request->amount);
return redirect()->route('products.show',['product' => $product]);
}
/**
* Remove the specified amount from the Pool.
*
* @param \App\Models\Product $product
* @return \Illuminate\Http\RedirectResponse
*/
public function amount_remove(Product $product,Request $request)
{
$object = Product::withTrashed()->where('id','=',$product)->first();
$object->total -= $request->amount;
$object->save();
Logger::LogAmountRemoved($object->id,get_class($object),$request->amount);
return redirect()->route('products.show',['product' => $product]);
}
}