'array',
];
const CREATED_AT = 'date_orig';
const UPDATED_AT = 'date_last';
protected $appends = [
'account_name',
'admin_service_id_url',
'category',
'name',
'name_full',
'next_invoice',
'product_name',
'service_id',
'service_id_url',
'status',
];
protected $visible = [
'account_name',
'admin_service_id_url',
'active',
'category',
'data_orig',
'id',
'name',
'name_full',
'next_invoice',
'product_name',
'service_id',
'service_id_url',
'status',
];
private $inactive_status = [
'CANCELLED',
'ORDER-REJECTED',
'ORDER-CANCELLED',
];
private $valid_status = [
// Order Submitted
'ORDER-SUBMIT' => ['approve'=>'ORDER-SENT','hold'=>'ORDER-HOLD','reject'=>'ORDER-REJECTED','cancel'=>'ORDER-CANCELLED'],
// Order On Hold (Reason)
'ORDER-HOLD' => [],
// Order Rejected (Reason)
'ORDER-REJECTED' => [],
// Order Cancelled
'ORDER-CANCELLED' => [],
// Order Sent to Supplier
'ORDER-SENT' => ['update_reference'=>'ORDER-SENT'],
];
/**
* Account the service belongs to
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function account()
{
return $this->belongsTo(Account::class);
}
/**
* Account that ordered the service
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function orderby()
{
return $this->belongsTo(Account::class);
}
/**
* Tenant that the service belongs to
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function site()
{
return $this->belongsTo(Site::class);
}
public function service_adsl()
{
return $this->belongsTo(ServiceAdsl::class,'id','service_id');
}
public function service_domain()
{
return $this->belongsTo(ServiceDomain::class,'id','service_id');
}
public function service_host()
{
return $this->belongsTo(ServiceHost::class,'id','service_id');
}
public function service_ssl()
{
return $this->belongsTo(ServiceSsl::class,'id','service_id');
}
public function service_voip()
{
return $this->belongsTo(ServiceVoip::class,'id','service_id');
}
/**
* Product of the service
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function product()
{
return $this->belongsTo(Product::class);
}
/**
* Find inactive services.
*
* @param $query
* @return mixed
*/
public function scopeInActive($query)
{
return $query->where(function () use ($query) {
return $query->where('active',FALSE)->orWhereIn('order_status',$this->inactive_status);
});
}
/**
* Only query active categories
*/
public function scopeActive($query)
{
return $query->where(function () use ($query) {
return $query->where('active',TRUE)->orWhereNotIn('order_status',$this->inactive_status);
});
}
public function getAccountNameAttribute()
{
return $this->account->company;
}
public function getAdminServiceIdUrlAttribute()
{
return sprintf('%s',$this->id,$this->service_id);
}
public function getCategoryAttribute()
{
// @todo: All services should be linked to a product. This might require data cleaning for old services not linked to a product.
return is_object($this->product) ? $this->product->category : 'Unknown Product';
}
public function getNameAttribute()
{
if (! isset($this->ServicePlugin()->name))
return 'Unknown';
return $this->ServicePlugin()->name;
}
public function getNameFullAttribute()
{
if (! isset($this->ServicePlugin()->full_name))
return 'Unknown';
return $this->ServicePlugin()->full_name;
}
public function getNextInvoiceAttribute()
{
return $this->date_next_invoice ? $this->date_next_invoice->format('Y-m-d') : NULL;
}
/**
* This function will present the Order Info Details
*/
public function getOrderInfoDetailsAttribute(): string
{
if (! $this->order_info)
return '';
$result = '';
foreach ($this->order_info as $k=>$v)
{
if (in_array($k,['order_reference']))
continue;
$result .= sprintf('%s: %s
',ucfirst($k),$v);
}
return $result;
}
public function getProductNameAttribute()
{
// @todo: All services should be linked to a product. This might require data cleaning for old services not linked to a product.
return is_object($this->product) ? $this->product->name($this->account->language) : 'Unknown Product';
}
public function getServiceExpireAttribute()
{
return 'TBA';
}
public function getServiceIdAttribute()
{
return sprintf('%02s-%04s.%05s',$this->site_id,$this->account_id,$this->id);
}
public function getServiceIdUrlAttribute()
{
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 $this->order_status=='ORDER-SENT'
? sprintf('%s: #%s',$this->order_status,array_get($this->order_info,'order_reference','Unknown'))
: $this->order_status;
}
public function setDateOrigAttribute($value)
{
$this->attributes['date_orig'] = $value->timestamp;
}
public function setDateLastAttribute($value)
{
$this->attributes['date_last'] = $value->timestamp;
}
public function isActive()
{
return $this->active OR ($this->order_status AND ! in_array($this->order_status,$this->inactive_status));
}
public function nextStatus(string $status) {
if ($x=$this->validStatus($status))
{
$this->order_status = $x;
$this->save();
return $this;
}
abort(500,'Next Status not set up for:'.$this->order_status);
}
/**
* This function will return the associated service model for the product type
*/
private function ServicePlugin()
{
// @todo: All services should be linked to a product. This might require data cleaning for old services not linked to a product.
if (! is_object($this->product))
return NULL;
switch ($this->product->prod_plugin_file)
{
case 'ADSL': return $this->service_adsl;
case 'DOMAIN': return $this->service_domain;
case 'HOST': return $this->service_host;
case 'SSL': return $this->service_ssl;
case 'VOIP': return $this->service_voip;
default: return NULL;
}
}
/**
* Return if the proposed status is valid.
*
* @param string $status
* @return string | NULL
*/
public function validStatus(string $status)
{
return array_get(array_get($this->valid_status,$this->order_status,[]),$status,NULL);
}
}