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
4 changed files with 277 additions and 41 deletions

View File

@@ -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('<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;
}
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: <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);
}
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: <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;
}
/** 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);
}
}