Changed account search to user search, show connection charges on invoice for pending services

This commit is contained in:
Deon George
2020-02-07 07:11:02 +09:00
parent ebd4367975
commit b61e00d80f
17 changed files with 314 additions and 145 deletions

View File

@@ -5,52 +5,64 @@ namespace App\Exceptions;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* Report or log an exception.
*
* @param \Exception $exception
* @return void
*/
public function report(Exception $exception)
{
parent::report($exception);
}
/**
* Report or log an exception.
*
* @param Exception $exception
* @return void
* @throws Exception
*/
public function report(Exception $exception)
{
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
// We'll render a 404 for any authorisation exceptions to hide the fact that the resource exists
if ($exception instanceof AuthorizationException)
abort(404,'Not here...');
/**
* Render an exception into an HTTP response.
*
* @param Request $request
* @param Exception $exception
* @return Response
* @throws Exception
*/
public function render($request, Exception $exception)
{
// We'll render a 404 for any authorisation exceptions to hide the fact that the resource exists
if ($exception instanceof AuthorizationException) {
Log::error('Request not authorised',['user'=>Auth::user()->id,'request'=>$request->path()]);
return parent::render($request, $exception);
}
}
if ($request->ajax())
return response()->json(['data'=>[]],200);
else
abort(404,'Not here...');
}
return parent::render($request, $exception);
}
}

View File

@@ -6,6 +6,7 @@ use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use App\User;
use App\Models\{Account,Invoice,Service,Service\Adsl};
class SearchController extends Controller
@@ -23,17 +24,17 @@ class SearchController extends Controller
return [];
$result = collect();
$accounts = Auth::user()->all_accounts()->pluck('id');
$accounts = ($x=Auth::user()->all_accounts())->pluck('id');
$users = $x->transform(function($item) { return $item->user;});
# Look for Account
foreach (Account::Search($request->input('term'))
->whereIN('id',$accounts)
->orderBy('company')
->orderBy('last_name')
->orderBy('first_name')
foreach (User::Search($request->input('term'))
->whereIN('id',$users->pluck('id'))
->orderBy('lastname')
->orderBy('firstname')
->limit(10)->get() as $o)
{
$result->push(['label'=>sprintf('AC:%s %s',$o->aid,$o->name),'value'=>'/u/account/'.$o->id]);
$result->push(['label'=>sprintf('US:%s %s',$o->aid,$o->name),'value'=>'/u/home/'.$o->id]);
}
# Look for a Service

View File

@@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
use Barryvdh\Snappy\Facades\SnappyPdf as PDF;
@@ -19,22 +20,23 @@ class UserHomeController extends Controller
/**
* Logged in users home page
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @return Factory|View
*/
public function home(): View
public function home(User $o=NULL): View
{
if (is_null($o))
$o = Auth::user();
switch (Auth::user()->role()) {
case 'customer':
return View('u.home',['o'=>Auth::user()]);
return View('u.home',['o'=>$o]);
case 'reseller':
return View('r.home',['o'=>Auth::user()]);
case 'wholesaler':
return View('r.home',['o'=>Auth::user()]);
return View('r.home',['o'=>$o]);
default:
abort(500,'Unknown role: '.Auth::user()->role());
abort(500,'Unknown role: '.$o->role());
}
}
@@ -83,20 +85,5 @@ class UserHomeController extends Controller
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]);
}
}

View File

@@ -2,22 +2,23 @@
namespace App\Http\Controllers;
use Auth;
use Illuminate\Support\Facades\Auth;
use App\User;
class UserServicesController extends Controller
{
public function invoices()
public function invoices(User $o)
{
return ['data'=>Auth::user()->invoices_due->values()];
return ['data'=>$o->invoices_due->values()];
}
public function payments()
public function payments(User $o)
{
return ['data'=>Auth::user()->payment_history->values()];
return ['data'=>$o->payment_history->values()];
}
public function services()
public function services(User $o)
{
return ['data'=>Auth::user()->services_active->values()];
return ['data'=>$o->services_active->values()];
}
}

View File

@@ -14,8 +14,8 @@ class AccountPolicy
/**
* Determine whether the user can view the service.
*
* @param \App\User $user
* @param Account $o
* @param User $user
* @param Account $o
* @return mixed
*/
public function view(User $user, Account $o)
@@ -33,7 +33,7 @@ class AccountPolicy
/**
* Determine whether the user can create services.
*
* @param \App\User $user
* @param User $user
* @return mixed
*/
public function create(User $user)
@@ -44,8 +44,8 @@ class AccountPolicy
/**
* Determine whether the user can update the service.
*
* @param \App\User $user
* @param Account $o
* @param User $user
* @param Account $o
* @return mixed
*/
public function update(User $user, Account $o)
@@ -56,8 +56,8 @@ class AccountPolicy
/**
* Determine whether the user can delete the service.
*
* @param \App\User $user
* @param Account $o
* @param User $user
* @param Account $o
* @return mixed
*/
public function delete(User $user, Account $o)
@@ -68,8 +68,8 @@ class AccountPolicy
/**
* Determine whether the user can restore the service.
*
* @param \App\User $user
* @param Account $o
* @param User $user
* @param Account $o
* @return mixed
*/
public function restore(User $user, Account $o)
@@ -80,8 +80,8 @@ class AccountPolicy
/**
* Determine whether the user can permanently delete the service.
*
* @param \App\User $user
* @param Account $o
* @param User $user
* @param Account $o
* @return mixed
*/
public function forceDelete(User $user, Account $o)

View File

@@ -186,12 +186,12 @@ class Product extends Model
* @param int $period
* @return mixed
*/
public function price(int $period)
public function price(int $period,string $key='price_base')
{
return Arr::get(
$this->price_array,
sprintf('%s.1.price_base',$period),
Arr::get($this->price_array,sprintf('%s.0.price_base',$period))
sprintf('%s.1.%s',$period,$key),
Arr::get($this->price_array,sprintf('%s.0.%s',$period,$key))
);
}

View File

@@ -522,6 +522,12 @@ class Service extends Model
return $this->product->name($this->account->language);
}
public function getRecurScheduleAttribute($value): int
{
// If recur_schedule not set, default to 2
return $value ?? 2;
}
/**
* @deprecated see getSIDAttribute()
*/
@@ -568,7 +574,7 @@ class Service extends Model
*/
public function getSDescAttribute(): string
{
return $this->type->service_description;
return $this->type->service_description ?: 'Service Description NOT Defined for :'.$this->type->type;
}
/**
@@ -583,7 +589,7 @@ class Service extends Model
*/
public function getSNameAttribute(): string
{
return $this->type->service_name;
return $this->type->service_name ?: 'Service Name NOT Defined for :'.$this->type->type;
}
/**
@@ -596,7 +602,7 @@ class Service extends Model
{
switch($this->product->model) {
case 'App\Models\Product\Adsl': return 'broadband';
default: abort(500,'Product type not configured',['product'=>$this->product]);
default: return $this->type->type;
}
}
@@ -770,6 +776,23 @@ class Service extends Model
$result->push($o);
}
// If pending, add any connection charges
if ($this->isPending()) {
$o = new InvoiceItem;
$o->active = TRUE;
$o->service_id = $this->id;
$o->product_id = $this->product_id;
$o->item_type = 4;
$o->price_base = $this->price ?: $this->product->price($this->recur_schedule,'price_setup'); // @todo change to a method in this class
//$o->recurring_schedule = $this->recur_schedule;
$o->date_start = $this->invoice_next;
$o->date_stop = $this->invoice_next;
$o->quantity = 1;
$o->addTaxes();
$result->push($o);
}
// Add additional charges
foreach ($this->charges->filter(function($item) { return ! $item->processed; }) as $oo) {
$o = new InvoiceItem;

View File

@@ -0,0 +1,90 @@
<?php
namespace App\Policies;
use Illuminate\Auth\Access\HandlesAuthorization;
use App\User;
class UserPolicy
{
use HandlesAuthorization;
/**
* Determine whether the user can view the service.
*
* @param User $user
* @param User $o
* @return mixed
*/
public function view(User $user, User $o)
{
// If this is a service for an account managed by a user.
return ($user->id == $o->id)
// The user is the wholesaler
OR $user->isWholesaler()
// The user is the reseller
OR $user->all_accounts()->pluck('id')->search($o->id);
}
/**
* Determine whether the user can create services.
*
* @param User $user
* @return mixed
*/
public function create(User $user)
{
//
}
/**
* Determine whether the user can update the service.
*
* @param User $user
* @param User $o
* @return mixed
*/
public function update(User $user, User $o)
{
//
}
/**
* Determine whether the user can delete the service.
*
* @param User $user
* @param User $o
* @return mixed
*/
public function delete(User $user, User $o)
{
//
}
/**
* Determine whether the user can restore the service.
*
* @param User $user
* @param User $o
* @return mixed
*/
public function restore(User $user, User $o)
{
//
}
/**
* Determine whether the user can permanently delete the service.
*
* @param User $user
* @param User $o
* @return mixed
*/
public function forceDelete(User $user, User $o)
{
//
}
}

View File

@@ -104,7 +104,7 @@ class User extends Authenticatable
return $this->hasMany(static::class,'parent_id','id');
}
/** Attributes **/
/** ATTRIBUTES **/
public function getActiveDisplayAttribute($value)
{
@@ -203,13 +203,50 @@ class User extends Authenticatable
$this->notify((new ResetPasswordNotification($token))->onQueue('high'));
}
/** Scopes **/
/** SCOPES */
public function scopeActive()
{
return $this->where('active',TRUE);
}
/**
* Search for a record
*
* @param $query
* @param string $term
* @return
*/
public function scopeSearch($query,string $term)
{
// Build our where clause
// First Name, Last name
if (preg_match('/\ /',$term)) {
list($fn,$ln) = explode(' ',$term,2);
$query->where(function($query1) use ($fn,$ln,$term) {
$query1->where(function($query2) use ($fn,$ln) {
return $query2
->where('firstname','like','%'.$fn.'%')
->where('lastname','like','%'.$ln.'%');
});
});
} elseif (is_numeric($term)) {
$query->where('id','like','%'.$term.'%');
} elseif (preg_match('/\@/',$term)) {
$query->where('email','like','%'.$term.'%');
} else {
$query
->Where('firstname','like','%'.$term.'%')
->orWhere('lastname','like','%'.$term.'%');
}
return $query;
}
/**
* Determine if the user is an admin of the account with $id
*