osb/app/Models/Service.php
2018-08-11 15:21:15 +10:00

181 lines
3.7 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Traits\NextKey;
class Service extends Model
{
use NextKey;
protected $table = 'ab_service';
protected $with = ['product.descriptions','account.language','service_adsl','service_domain.tld','service_ssl','service_voip'];
protected $dates = ['date_last_invoice','date_next_invoice'];
protected $casts = [
'order_info'=>'array',
];
public $incrementing = FALSE;
const CREATED_AT = 'date_orig';
const UPDATED_AT = 'date_last';
protected $appends = [
'account_name',
'category',
'name',
'next_invoice',
'product_name',
'service_id',
'service_id_url',
'status',
];
protected $visible = [
'account_name',
'active',
'category',
'data_orig',
'id',
'name',
'next_invoice',
'product_name',
'service_id',
'service_id_url',
'status',
];
private $inactive_status = [
'CANCELLED',
];
public function account()
{
return $this->belongsTo(Account::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');
}
public function product()
{
return $this->belongsTo(Product::class);
}
/**
* 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 getCategoryAttribute()
{
return $this->product->category;
}
public function getNameAttribute()
{
if (! isset($this->ServicePlugin()->name))
return 'Unknown';
return $this->ServicePlugin()->name;
}
public function getNextInvoiceAttribute()
{
return $this->date_next_invoice ? $this->date_next_invoice->format('Y-m-d') : NULL;
}
public function getProductNameAttribute()
{
return $this->product->name($this->account->language);
}
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('<a href="/u/service/view/%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()
{
return $this->order_status ? $this->order_status : ( $this->active ? 'Active' : 'Inactive');
}
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));
}
/**
* This function will return the associated service model for the product type
*/
private function ServicePlugin()
{
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;
}
}
}