osb/app/Http/Controllers/UserHomeController.php
2020-02-05 13:47:24 +09:00

102 lines
2.1 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
use Barryvdh\Snappy\Facades\SnappyPdf as PDF;
use App\Models\{Invoice,Service};
use App\User;
class UserHomeController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
/**
* Logged in users home page
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function home(): View
{
switch (Auth::user()->role()) {
case 'customer':
return View('u.home',['o'=>Auth::user()]);
case 'reseller':
return View('r.home',['o'=>Auth::user()]);
case 'wholesaler':
return View('r.home',['o'=>Auth::user()]);
default:
abort(500,'Unknown role: '.Auth::user()->role());
}
}
/**
* Render a specific invoice for the user
*
* @param Invoice $o
* @return View
*/
public function invoice(Invoice $o): View
{
return View('u.invoice',['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', ['o'=>$o])->stream(sprintf('%s.pdf',$o->invoice_account_id));
}
/**
* 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));
}
/**
* Return details on the users service
*
* @param Service $o
* @return View
*/
public function service(Service $o): View
{
return View('u.service',['o'=>$o]);
foreach ([
sprintf('u.service.%s.%s',$o->type->type,$o->status),
sprintf('u.service.%s',$o->status),
] as $v)
if (view()->exists($v))
return View($v,['o'=>$o]);
// View doesnt exist, fall back to default view
return View('u.service',['o'=>$o]);
}
public function User(User $o)
{
// @todo Check authorised to see this account.
return View('u.home',['o'=>$o]);
}
}