Lager-v3/app/Http/Controllers/Loan/PdfController.php

224 lines
9.4 KiB
PHP
Raw Normal View History

2022-09-28 07:38:08 +00:00
<?php
namespace App\Http\Controllers\Loan;
use App\Http\Controllers\Controller;
use App\Models\Contract;
use App\Models\Permission;
use App\Models\User;
use DateTime;
use DateTimeZone;
use Illuminate\Auth\Access\Response as Response;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Response as Fresponse;
use Illuminate\Support\Facades\Storage;
class PdfController extends Controller
{
public function index(Request $request){
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'pdf_viewAny'))
? Response::allow()
: Response::deny('you are not the chosen one');
$search_types = [];
array_push($search_types,array("value" => "user", "name" => "user"));
array_push($search_types,array("value" => "date", "name" => "date"));
array_push($search_types,array("value" => "type", "name" => "type"));
$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 "user":
switch($search_compare){
case('='):
$contracts = Contract::where(function ($query) use ($search_term){
$query->whereHas('user',function ($query) use ($search_term){
$query->where('username','=',$search_term);
});
})
->orderBy('user_id')
->orderBy('type')
->orderBy('timestamp','desc')
->paginate($PerPagination);
break;
default:
$contracts = Contract::where(function ($query) use ($search_term){
$query->whereHas('user',function ($query) use ($search_term){
$query->where('username','like','%' . $search_term . '%');
});
})
->orderBy('user_id')
->orderBy('type')
->orderBy('timestamp','desc')
->paginate($PerPagination);
break;
}
break;
case "type":
switch($search_compare){
default:
$contracts = Contract::where('type','=',trans($search_term))
->orderBy('user_id')
->orderBy('type')
->orderBy('timestamp','desc')
->paginate($PerPagination);
break;
}
break;
case "date":
switch($search_compare){
case('>='):
$parts = explode(' ',$search_term);
$date_part = $parts[0];
$time_part = $parts[1];
$date_parts = explode('.',$date_part);
$d = $date_parts[0];
$m = $date_parts[1];
$y = $date_parts[2];
$time_parts = explode(':',$time_part);
$h = $time_parts[0];
$i = $time_parts[1];
$s = $time_parts[2];
$date = new DateTime();
$timezone = new DateTimeZone('Europe/Copenhagen');
$date->setTimezone($timezone);
$date->setDate($y,$m,$d);
$date->setTime($h,$i,$s);
$timestamp = $date->getTimestamp();
$contracts = Contract::where('timestamp','>=',$timestamp)
->orderBy('user_id')
->orderBy('type')
->orderBy('timestamp','desc')
->paginate($PerPagination);
break;
case('<='):
$parts = explode(' ',$search_term);
$date_part = $parts[0];
$time_part = $parts[1];
$date_parts = explode('.',$date_part);
$d = $date_parts[0];
$m = $date_parts[1];
$y = $date_parts[2];
$time_parts = explode(':',$time_part);
$h = $time_parts[0];
$i = $time_parts[1];
$s = $time_parts[2];
$date = new DateTime();
$timezone = new DateTimeZone('Europe/Copenhagen');
$date->setTimezone($timezone);
$date->setDate($y,$m,$d);
$date->setTime($h,$i,$s);
$timestamp = $date->getTimestamp();
$contracts = Contract::where('timestamp','<=',$timestamp)
->orderBy('user_id')
->orderBy('type')
->orderBy('timestamp','desc')
->paginate($PerPagination);
break;
default:
$parts = explode(' ',$search_term);
$date_part = $parts[0];
$time_part = $parts[1];
$date_parts = explode('.',$date_part);
$d = $date_parts[0];
$m = $date_parts[1];
$y = $date_parts[2];
$time_parts = explode(':',$time_part);
$h = $time_parts[0];
$i = $time_parts[1];
$s = $time_parts[2];
$date = new DateTime();
$timezone = new DateTimeZone('Europe/Copenhagen');
$date->setTimezone($timezone);
$date->setDate($y,$m,$d);
$date->setTime($h,$i,$s);
$timestamp = $date->getTimestamp();
$contracts = Contract::where('timestamp','=',$timestamp)
->orderBy('user_id')
->orderBy('type')
->orderBy('timestamp','desc')
->paginate($PerPagination);
break;
}
break;
}
}
else{
$contracts = Contract::orderBy('user_id')
->orderBy('type')
->orderBy('timestamp','desc')
->paginate($PerPagination);
}
return view('contracts.index')
->with('search_types',$search_types)
->with('data_name','contract')
->with('data_names','contracts')
->with('data',$contracts)
;
}
public function show(Request $request,$user){
$user_obj = User::where('username','=',$user)->first();
if(empty($user_obj)){
$user_obj = User::where('name','=',$request->user)->first();
}
if(Auth::user()->id != $user_obj->id){
if(!Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'pdf_view'))){
return redirect()->intended(route('users.show',Auth::user()));
}
else{
$user = $request->user;
$type = $request->type;
$timestamp = $request->timestamp;
$file_name = utf8_encode('app/'.$type."/".$user."_".$timestamp.".pdf");
$file_full = storage_path($file_name);
return Fresponse::file($file_full);
}
}
else{
$user = $request->user;
$type = $request->type;
$timestamp = $request->timestamp;
$file_name = utf8_encode('app/'.$type."/".$user."_".$timestamp.".pdf");
$file_full = storage_path($file_name);
return Fresponse::file($file_full);
}
}
public function destroy(Request $request){
Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'pdf_delete'))
? Response::allow()
: Response::deny('you are not the chosen one');
$type = $request->type;
$user = $request->user;
$timestamp = $request->timestamp;
$file_name = utf8_encode('app/'.$type."/".$user."_".$timestamp.".pdf");
$file_full = storage_path($file_name);
if(file_exists($file_full)){
unlink($file_full);
}
$user_obj = User::where('username','=',$user)->first();
return redirect()->back();
}
}