osb/app/Http/Controllers/HomeController.php

135 lines
2.9 KiB
PHP
Raw 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
2018-08-23 05:17:26 +00:00
use App\Models\{Invoice,Service};
2018-08-01 07:09:38 +00:00
use App\User;
2018-07-13 04:53:44 +00:00
2021-06-29 00:37:24 +00:00
class HomeController extends Controller
2018-05-20 12:53:14 +00:00
{
public function __construct()
{
2020-05-25 07:45:17 +00:00
// Route protection is in routes.web
// $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
*/
public function home(User $o=NULL): View
2018-05-20 12:53:14 +00:00
{
2020-02-08 11:51:50 +00:00
if ($o)
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();
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:
abort(500,'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);
}
2018-07-16 05:06:43 +00:00
/**
* Helper to redirect to the old site, when functions are not available in this one.
*
* @param $type
* @param $action
* @param $id
* @return void
* @deprecated @todo Remove once all functions added
*/
public function oldsite($type,$action,$id)
{
abort(307,sprintf('http://www.graytech.net.au/u/%s/%s/%s',$type,$action,$id));
}
2018-08-01 07:09:38 +00:00
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
}