osb/app/Http/Controllers/HomeController.php
2021-09-29 17:36:55 +10:00

127 lines
2.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use Clarkeash\Doorman\Exceptions\{DoormanException,ExpiredInviteCode};
use Clarkeash\Doorman\Facades\Doorman;
use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\View\View;
use Barryvdh\Snappy\Facades\SnappyPdf as PDF;
use App\Models\{Invoice,Service,User};
/**
* Class HomeController
* This controller is the logged in users home page
*
* The methods to this controller should be projected by the route
*
* @package App\Http\Controllers
*/
class HomeController extends Controller
{
/**
* Logged in users home page
*
* @return Factory|View
*/
public function home(User $o): View
{
// If we are passed a user to view, we'll open up their home page.
if ($o->exists) {
$o->load(['accounts','services']);
return View('u.home',['o'=>$o]);
}
// If User was null, then test and see what type of logged on user we have
$o = Auth::user();
switch (Auth::user()->role()) {
case 'customer':
return View('u.home',['o'=>$o]);
case 'reseller':
case 'wholesaler':
return View('r.home',['o'=>$o]);
default:
abort(404,'Unknown role: '.$o->role());
}
}
/**
* Render a specific invoice for the user
*
* @param Invoice $o
* @return View
*/
public function invoice(Invoice $o): View
{
return View('u.invoice.home',['o'=>$o]);
}
/**
* Return the invoice in PDF format, ready to download
*
* @param Invoice $o
* @return mixed
*/
public function invoice_pdf(Invoice $o)
{
return PDF::loadView('u.invoice.home',['o'=>$o])->stream(sprintf('%s.pdf',$o->invoice_account_id));
}
/**
* Enable the user to down an invoice by providing a link in email
*
* @param Invoice $o
* @param string $code
* @return \Illuminate\Http\RedirectResponse|mixed
*/
public function invoice_pdf_email(Invoice $o,string $code)
{
try {
Doorman::redeem($code,$o->account->user->email);
} catch (ExpiredInviteCode $e) {
Log::alert(sprintf('User is using an expired token for invoice [%s] using [%s]',$o->id,$code));
return redirect()->to('/login');
} catch (DoormanException $e) {
Log::alert(sprintf('An attempt to read invoice id [%s] using [%s]',$o->id,$code));
abort(404);
}
return $this->invoice($o);
}
/**
* Return details on the users service
*
* @param Service $o
* @return View
*/
public function service(Service $o): View
{
return View('u.service.home',['o'=>$o]);
}
/**
* Progress the order to the next stage
*
* @note: Route Middleware protects this path
* @param Service $o
* @param string $status
* @return \Illuminate\Http\RedirectResponse
* @deprecated
*/
public function service_progress(Service $o,string $status)
{
abort(500,'deprecated');
return redirect()->to($o->action($status) ?: url('u/service',$o->id));
}
}