Start of search, reorged Service and Account models

This commit is contained in:
Deon George 2019-06-21 16:21:48 +10:00
parent 3ff82f5f10
commit a426c7b1a4
No known key found for this signature in database
GPG Key ID: 7670E8DC27415254
4 changed files with 277 additions and 41 deletions

View File

@ -0,0 +1,40 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Models\{Account};
class SearchController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function search(Request $request)
{
if (! $request->input('term'))
return [];
$result = collect();
$uo = Auth::user();
# Look for Account
foreach (Account::Search($request->input('term'))
->whereIN('id',$uo->all_clients()->pluck('id'))
->orderBy('company')
->orderBy('last_name')
->orderBy('first_name')
->limit(10)->get() as $o)
{
$result->push(['label'=>sprintf('A:%s %s',$o->aid,$o->name),'value'=>'/u/account/'.$o->id]);
}
return $result;
}
}

View File

@ -2,6 +2,7 @@
namespace App\Models; namespace App\Models;
use App\User;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use App\Traits\NextKey; use App\Traits\NextKey;
@ -69,19 +70,76 @@ class Account extends Model
return $this->belongsTo(\App\User::class); return $this->belongsTo(\App\User::class);
} }
/** SCOPES */
/**
* Search for a record
*
* @param User $uo
* @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('first_name','like','%'.$fn.'%')
->where('last_name','like','%'.$ln.'%');
})
->orWhere('company','like','%'.$term.'%');
});
} elseif (is_numeric($term)) {
$query->where('id','like','%'.$term.'%');
} elseif (preg_match('/\@/',$term)) {
$query->where('email','like','%'.$term.'%');
} else {
$query
->where('company','like','%'.$term.'%')
->orWhere('first_name','like','%'.$term.'%')
->orWhere('last_name','like','%'.$term.'%');
}
return $query;
}
/** ATTRIBUTES **/
public function getActiveDisplayAttribute($value) public function getActiveDisplayAttribute($value)
{ {
return sprintf('<span class="btn-sm btn-block btn-%s text-center">%s</span>',$this->active ? 'success' : 'danger',$this->active ? 'Active' : 'Inactive'); return sprintf('<span class="btn-sm btn-block btn-%s text-center">%s</span>',$this->active ? 'success' : 'danger',$this->active ? 'Active' : 'Inactive');
} }
/**
* @deprecated use getAIDAttribute()
*/
public function getAccountIdAttribute() public function getAccountIdAttribute()
{ {
return sprintf('%02s-%04s',$this->site_id,$this->id); return $this->getAIDAttribute();
} }
/**
* @deprecated use getUrlAdminAttribute()
*/
public function getAccountIdUrlAttribute() public function getAccountIdUrlAttribute()
{ {
return sprintf('<a href="/r/account/view/%s">%s</a>',$this->id,$this->account_id); return $this->getUrlAdminAttribute();
}
/**
* Return the Account Unique Identifier
* @return string
*/
public function getAIDAttribute()
{
return sprintf('%02s-%04s',$this->site_id,$this->id);
} }
public function getNameAttribute() public function getNameAttribute()
@ -104,6 +162,28 @@ class Account extends Model
return $this->company ? 'Business' : 'Private'; return $this->company ? 'Business' : 'Private';
} }
/**
* Return the Admin URL to manage the account
*
* @return string
*/
public function getUrlAdminAttribute(): string
{
return sprintf('<a href="/r/account/view/%s">%s</a>',$this->id,$this->account_id);
}
/**
* Return the User URL to manage the account
*
* @return string
*/
public function getUrlUserAttribute(): string
{
return sprintf('<a href="/u/account/view/%s">%s</a>',$this->id,$this->account_id);
}
/** FUNCTIONS **/
private function _address() private function _address()
{ {
$return = []; $return = [];

View File

@ -3,6 +3,7 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use App\Traits\NextKey; use App\Traits\NextKey;
@ -58,6 +59,17 @@ class Service extends Model
'ORDER-CANCELLED', 'ORDER-CANCELLED',
]; ];
/**
* Valid status shows the applicable next status for an action on a service
* Each status can be
* + Approved, to proceed to the next valid status'
* + Held, to a holding pattern status
* + Rejected, reverted to an different status
* + Cancel, to progress down a decomission route
* + Updated, stay on the current status with new information
*
* @var array
*/
private $valid_status = [ private $valid_status = [
// Order Submitted // Order Submitted
'ORDER-SUBMIT' => ['approve'=>'ORDER-SENT','hold'=>'ORDER-HOLD','reject'=>'ORDER-REJECTED','cancel'=>'ORDER-CANCELLED'], 'ORDER-SUBMIT' => ['approve'=>'ORDER-SENT','hold'=>'ORDER-HOLD','reject'=>'ORDER-REJECTED','cancel'=>'ORDER-CANCELLED'],
@ -113,11 +125,18 @@ class Service extends Model
return $this->belongsTo(Product::class); return $this->belongsTo(Product::class);
} }
/**
* Return a child model with details of the service
*
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function type() public function type()
{ {
return $this->morphTo(null,'model','id','service_id'); return $this->morphTo(null,'model','id','service_id');
} }
/** SCOPES **/
/** /**
* Only query active categories * Only query active categories
*/ */
@ -141,44 +160,44 @@ class Service extends Model
}); });
} }
/** ATTRIBUTES **/
/** /**
* Name of the account for this service * Name of the account for this service
* *
* @return mixed * @return mixed
*/ */
public function getAccountNameAttribute() public function getAccountNameAttribute(): string
{ {
return $this->account->name; return $this->account->name;
} }
/** /**
* Get the Product's Category for this service * @deprecated Use getUrlAdminAttribute()
*
*/ */
public function getProductCategoryAttribute(): string public function getAdminServiceIdUrlAttribute()
{ {
return $this->product->category; return $this->getUrlAdminAttribute();
} }
/** /**
* Get the Product's Short Name for the service * Date the service expires, also represents when it is paid up to
* *
* @return string * @return string
*/ */
public function getProductNameAttribute(): string public function getExpiresAttribute(): string
{ {
return $this->product->name($this->account->language); return 'TBA';
} }
/** /**
* Return the short name for the service. * Services Unique Identifier
* EG: *
* For ADSL, this would be the phone number, * @return string
* For Hosting, this would be the domain name, etc
*/ */
public function getNameShortAttribute() public function getSIDAttribute(): string
{ {
return $this->model ? $this->type->name : NULL; return sprintf('%02s-%04s.%05s',$this->site_id,$this->account_id,$this->id);
} }
/** /**
@ -186,16 +205,29 @@ class Service extends Model
* *
* @return null * @return null
*/ */
public function getNextInvoiceAttribute() public function getInvoiceNextAttribute()
{ {
return $this->date_next_invoice ? $this->date_next_invoice->format('Y-m-d') : NULL; return $this->date_next_invoice ? $this->date_next_invoice->format('Y-m-d') : NULL;
} }
//@todo /**
* Return the short name for the service.
public function getAdminServiceIdUrlAttribute() *
* EG:
* For ADSL, this would be the phone number,
* For Hosting, this would be the domain name, etc
*/
public function getNameShortAttribute(): string
{ {
return sprintf('<a href="/a/service/%s">%s</a>',$this->id,$this->service_id); return $this->model ? $this->type->name : 'NAME UNKNOWN';
}
/**
* @deprecated see getInvoiceNextAttribute()
*/
public function getNextInvoiceAttribute()
{
return $this->getInvoiceNextAttribute();
} }
/** /**
@ -219,35 +251,95 @@ class Service extends Model
return $result; return $result;
} }
public function getServiceExpireAttribute() /**
* Get the Product's Category for this service
*
*/
public function getProductCategoryAttribute(): string
{ {
return 'TBA'; return $this->product->category;
} }
/**
* Get the Product's Short Name for the service
*
* @return string
*/
public function getProductNameAttribute(): string
{
return $this->product->name($this->account->language);
}
/**
* @deprecated see getServiceIdAttribute()
*/
public function getServiceIdAttribute() public function getServiceIdAttribute()
{ {
return sprintf('%02s-%04s.%05s',$this->site_id,$this->account_id,$this->id); return $this->getSIDAttribute();
} }
/**
* @deprecated see getUrlUserAttribute()
*/
public function getServiceIdUrlAttribute() public function getServiceIdUrlAttribute()
{
return $this->getUrlUserAttribute();
}
/**
* @deprecated see getServiceIdAttribute()
*/
public function getServiceNumberAttribute()
{
return $this->getSIDAttribute();
}
/**
* Return the Service Status
*
* @return string
*/
public function getStatusAttribute(): string
{
if (! $this->order_status)
return $this->active ? 'ACTIVE' : 'INACTIVE';
else
return $this->order_status;
}
/**
* Return the detailed order Status, with order reference numbers.
*
* @return string
*/
public function getStatusDetailAttribute(): string
{
return in_array($this->order_status,['ORDER-SENT','ORDER-HOLD','ORDERED'])
? sprintf('%s: <span class="white-space: nowrap"><small><b>#%s</b></small></span>',$this->order_status,Arr::get($this->order_info,'order_reference','Unknown'))
: '';
}
/**
* URL used by an admin to administer the record
*
* @return string
*/
public function getUrlAdminAttribute(): string
{
return sprintf('<a href="/a/service/%s">%s</a>',$this->id,$this->service_id);
}
/**
* URL used by an user to see the record
*
* @return string
*/
public function getUrlUserAttribute(): string
{ {
return sprintf('<a href="/u/service/%s">%s</a>',$this->id,$this->service_id); return sprintf('<a href="/u/service/%s">%s</a>',$this->id,$this->service_id);
} }
public function getServiceNumberAttribute() /** SETTERS **/
{
return sprintf('%02s.%04s.%04s',$this->site_id,$this->account_id,$this->id);
}
public function getStatusAttribute()
{
if (! $this->order_status)
return $this->active ? 'Active' : 'Inactive';
return in_array($this->order_status,['ORDER-SENT','ORDER-HOLD','ORDERED'])
? sprintf('%s: <span class="white-space: nowrap"><small><b>#%s</b></small></span>',$this->order_status,array_get($this->order_info,'order_reference','Unknown'))
: $this->order_status;
}
public function setDateOrigAttribute($value) public function setDateOrigAttribute($value)
{ {
@ -259,11 +351,24 @@ class Service extends Model
$this->attributes['date_last'] = $value->timestamp; $this->attributes['date_last'] = $value->timestamp;
} }
public function isActive() /** FUNCTIONS **/
/**
* Determine if a service is active. It is active, if active=1, or the order_status is not in inactive_status[]
*
* @return bool
* @todo Remove active and have order_status reflect whether active or not
*/
public function isActive(): bool
{ {
return $this->active OR ($this->order_status AND ! in_array($this->order_status,$this->inactive_status)); return $this->active OR ($this->order_status AND ! in_array($this->order_status,$this->inactive_status));
} }
/**
* @todo
* @param string $status
* @return $this
*/
public function nextStatus(string $status) { public function nextStatus(string $status) {
if ($x=$this->validStatus($status)) if ($x=$this->validStatus($status))
{ {
@ -278,6 +383,7 @@ class Service extends Model
/** /**
* This function will return the associated service model for the product type * This function will return the associated service model for the product type
* @deprecated use $this->type
*/ */
private function ServicePlugin() private function ServicePlugin()
{ {
@ -303,8 +409,16 @@ class Service extends Model
* @param string $status * @param string $status
* @return string | NULL * @return string | NULL
*/ */
private function testNextStatusValid(string $status)
{
return Arr::get(Arr::get($this->valid_status,$this->order_status,[]),$status,NULL);
}
/**
* @deprecated use testNextStatusValid()
*/
public function validStatus(string $status) public function validStatus(string $status)
{ {
return array_get(array_get($this->valid_status,$this->order_status,[]),$status,NULL); return $this->testNextStatusValid($status);
} }
} }

View File

@ -59,3 +59,5 @@ Route::redirect('/home','/u/home');
Route::demoAccess('/uc-access'); Route::demoAccess('/uc-access');
Route::redirect('/under-construction','http://www.graytech.net.au'); Route::redirect('/under-construction','http://www.graytech.net.au');
Route::get('/u/{type}/{action}/{id}','UserHomeController@oldsite'); Route::get('/u/{type}/{action}/{id}','UserHomeController@oldsite');
Route::get('/search','SearchController@search');