osb/app/Http/Controllers/HomeController.php

130 lines
2.8 KiB
PHP
Raw Permalink Normal View History

2018-05-20 12:53:14 +00:00
<?php
namespace App\Http\Controllers;
2020-05-25 07:45:17 +00:00
use Clarkeash\Doorman\Exceptions\{DoormanException,ExpiredInviteCode};
use Clarkeash\Doorman\Facades\Doorman;
use Illuminate\Contracts\View\Factory;
2018-07-13 04:53:44 +00:00
use Illuminate\Support\Facades\Auth;
2020-05-25 07:45:17 +00:00
use Illuminate\Support\Facades\Log;
2020-02-05 04:47:24 +00:00
use Illuminate\View\View;
use Barryvdh\Snappy\Facades\SnappyPdf as PDF;
2019-07-02 05:28:27 +00:00
2021-06-29 03:18:52 +00:00
use App\Models\{Invoice,Service,User};
2018-07-13 04:53:44 +00:00
2021-06-29 03:18:52 +00:00
/**
* 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
*/
2021-06-29 00:37:24 +00:00
class HomeController extends Controller
2018-05-20 12:53:14 +00:00
{
public function __construct()
{
2021-06-29 03:18:52 +00:00
$this->middleware('auth');
2018-05-20 12:53:14 +00:00
}
2020-02-05 04:47:24 +00:00
/**
* Logged in users home page
*
* @return Factory|View
2020-02-05 04:47:24 +00:00
*/
2021-06-29 03:18:52 +00:00
public function home(User $o): View
2018-05-20 12:53:14 +00:00
{
2021-06-29 03:18:52 +00:00
// If we are passed a user to view, we'll open up their home page.
if ($o->exists) {
$o->load(['accounts','services']);
2020-02-08 11:51:50 +00:00
return View('u.home',['o'=>$o]);
}
2020-02-08 11:51:50 +00:00
// If User was null, then test and see what type of logged on user we have
$o = Auth::user();
2018-07-13 04:53:44 +00:00
switch (Auth::user()->role()) {
2018-07-31 04:11:00 +00:00
case 'customer':
return View('u.home',['o'=>$o]);
2018-07-13 04:53:44 +00:00
2018-07-31 04:11:00 +00:00
case 'reseller':
case 'wholesaler':
return View('r.home',['o'=>$o]);
2018-07-13 04:53:44 +00:00
default:
2021-06-29 03:18:52 +00:00
abort(404,'Unknown role: '.$o->role());
2018-07-13 04:53:44 +00:00
}
2018-05-20 12:53:14 +00:00
}
2018-07-16 05:06:43 +00:00
2020-02-05 04:47:24 +00:00
/**
* Render a specific invoice for the user
*
* @param Invoice $o
* @return View
*/
public function invoice(Invoice $o): View
2018-08-23 05:17:26 +00:00
{
2020-07-27 04:49:59 +00:00
return View('u.invoice.home',['o'=>$o]);
2018-08-23 05:17:26 +00:00
}
2020-02-05 04:47:24 +00:00
/**
* Return the invoice in PDF format, ready to download
*
* @param Invoice $o
* @return mixed
*/
2018-08-23 05:17:26 +00:00
public function invoice_pdf(Invoice $o)
{
2020-07-27 04:49:59 +00:00
return PDF::loadView('u.invoice.home',['o'=>$o])->stream(sprintf('%s.pdf',$o->invoice_account_id));
2018-08-23 05:17:26 +00:00
}
2020-05-25 07:45:17 +00:00
/**
* 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_pdf($o);
}
2020-02-05 04:47:24 +00:00
/**
* Return details on the users service
*
* @param Service $o
* @return View
*/
public function service(Service $o): View
2018-08-23 05:17:26 +00:00
{
2020-04-18 22:33:41 +00:00
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
*/
public function service_progress(Service $o,string $status)
{
return redirect()->to($o->action($status) ?: url('u/service',$o->id));
2018-08-01 07:09:38 +00:00
}
2018-05-20 12:53:14 +00:00
}