From a426c7b1a410c75e43273d7c2fa35fc95a050e3a Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 21 Jun 2019 16:21:48 +1000 Subject: [PATCH] Start of search, reorged Service and Account models --- app/Http/Controllers/SearchController.php | 40 +++++ app/Models/Account.php | 84 +++++++++- app/Models/Service.php | 190 +++++++++++++++++----- routes/web.php | 4 +- 4 files changed, 277 insertions(+), 41 deletions(-) create mode 100644 app/Http/Controllers/SearchController.php diff --git a/app/Http/Controllers/SearchController.php b/app/Http/Controllers/SearchController.php new file mode 100644 index 0000000..0ecd9cb --- /dev/null +++ b/app/Http/Controllers/SearchController.php @@ -0,0 +1,40 @@ +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; + } +} \ No newline at end of file diff --git a/app/Models/Account.php b/app/Models/Account.php index 633eeeb..4aa5e72 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\User; use Illuminate\Database\Eloquent\Model; use App\Traits\NextKey; @@ -69,19 +70,76 @@ class Account extends Model 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) { return sprintf('%s',$this->active ? 'success' : 'danger',$this->active ? 'Active' : 'Inactive'); } + /** + * @deprecated use getAIDAttribute() + */ public function getAccountIdAttribute() { - return sprintf('%02s-%04s',$this->site_id,$this->id); + return $this->getAIDAttribute(); } + /** + * @deprecated use getUrlAdminAttribute() + */ public function getAccountIdUrlAttribute() { - return sprintf('%s',$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() @@ -104,6 +162,28 @@ class Account extends Model return $this->company ? 'Business' : 'Private'; } + /** + * Return the Admin URL to manage the account + * + * @return string + */ + public function getUrlAdminAttribute(): string + { + return sprintf('%s',$this->id,$this->account_id); + } + + /** + * Return the User URL to manage the account + * + * @return string + */ + public function getUrlUserAttribute(): string + { + return sprintf('%s',$this->id,$this->account_id); + } + + /** FUNCTIONS **/ + private function _address() { $return = []; diff --git a/app/Models/Service.php b/app/Models/Service.php index c94f269..e8a7caf 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -3,6 +3,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Arr; use App\Traits\NextKey; @@ -58,6 +59,17 @@ class Service extends Model '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 = [ // Order Submitted '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 a child model with details of the service + * + * @return \Illuminate\Database\Eloquent\Relations\MorphTo + */ public function type() { return $this->morphTo(null,'model','id','service_id'); } + /** SCOPES **/ + /** * Only query active categories */ @@ -141,44 +160,44 @@ class Service extends Model }); } + /** ATTRIBUTES **/ + /** * Name of the account for this service * * @return mixed */ - public function getAccountNameAttribute() + public function getAccountNameAttribute(): string { 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 */ - public function getProductNameAttribute(): string + public function getExpiresAttribute(): string { - return $this->product->name($this->account->language); + return 'TBA'; } /** - * Return the short name for the service. - * EG: - * For ADSL, this would be the phone number, - * For Hosting, this would be the domain name, etc + * Services Unique Identifier + * + * @return string */ - 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 */ - public function getNextInvoiceAttribute() + public function getInvoiceNextAttribute() { return $this->date_next_invoice ? $this->date_next_invoice->format('Y-m-d') : NULL; } - //@todo - - public function getAdminServiceIdUrlAttribute() + /** + * Return the short name for the service. + * + * EG: + * For ADSL, this would be the phone number, + * For Hosting, this would be the domain name, etc + */ + public function getNameShortAttribute(): string { - return sprintf('%s',$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; } - 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() { - return sprintf('%02s-%04s.%05s',$this->site_id,$this->account_id,$this->id); + return $this->getSIDAttribute(); } + /** + * @deprecated see getUrlUserAttribute() + */ 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: #%s',$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('%s',$this->id,$this->service_id); + } + + /** + * URL used by an user to see the record + * + * @return string + */ + public function getUrlUserAttribute(): string { return sprintf('%s',$this->id,$this->service_id); } - public function getServiceNumberAttribute() - { - 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: #%s',$this->order_status,array_get($this->order_info,'order_reference','Unknown')) - : $this->order_status; - } + /** SETTERS **/ public function setDateOrigAttribute($value) { @@ -259,11 +351,24 @@ class Service extends Model $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)); } + /** + * @todo + * @param string $status + * @return $this + */ public function nextStatus(string $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 + * @deprecated use $this->type */ private function ServicePlugin() { @@ -303,8 +409,16 @@ class Service extends Model * @param string $status * @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) { - return array_get(array_get($this->valid_status,$this->order_status,[]),$status,NULL); + return $this->testNextStatusValid($status); } } \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index f50924c..2ad6594 100644 --- a/routes/web.php +++ b/routes/web.php @@ -58,4 +58,6 @@ Route::get('product_info/{o}', 'OrderController@product_info'); Route::redirect('/home','/u/home'); Route::demoAccess('/uc-access'); Route::redirect('/under-construction','http://www.graytech.net.au'); -Route::get('/u/{type}/{action}/{id}','UserHomeController@oldsite'); \ No newline at end of file +Route::get('/u/{type}/{action}/{id}','UserHomeController@oldsite'); + +Route::get('/search','SearchController@search'); \ No newline at end of file