2018-05-20 12:53:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2018-08-11 05:09:41 +00:00
|
|
|
use App\Traits\NextKey;
|
2018-05-20 12:53:14 +00:00
|
|
|
|
|
|
|
class Service extends Model
|
|
|
|
{
|
2018-08-11 05:09:41 +00:00
|
|
|
use NextKey;
|
2018-08-20 12:15:28 +00:00
|
|
|
public $incrementing = FALSE;
|
2018-08-11 05:09:41 +00:00
|
|
|
|
2018-05-20 12:53:14 +00:00
|
|
|
protected $table = 'ab_service';
|
2018-06-19 12:31:49 +00:00
|
|
|
protected $with = ['product.descriptions','account.language','service_adsl','service_domain.tld','service_ssl','service_voip'];
|
2018-05-20 12:53:14 +00:00
|
|
|
protected $dates = ['date_last_invoice','date_next_invoice'];
|
2018-08-11 05:09:41 +00:00
|
|
|
protected $casts = [
|
|
|
|
'order_info'=>'array',
|
|
|
|
];
|
|
|
|
|
|
|
|
const CREATED_AT = 'date_orig';
|
|
|
|
const UPDATED_AT = 'date_last';
|
2018-06-19 12:31:49 +00:00
|
|
|
|
|
|
|
protected $appends = [
|
2018-08-11 05:09:41 +00:00
|
|
|
'account_name',
|
2018-08-23 05:17:26 +00:00
|
|
|
'admin_service_id_url',
|
2018-06-19 12:31:49 +00:00
|
|
|
'category',
|
2018-08-01 07:09:38 +00:00
|
|
|
'name',
|
2018-11-21 03:37:17 +00:00
|
|
|
'name_full',
|
2018-06-19 12:31:49 +00:00
|
|
|
'next_invoice',
|
|
|
|
'product_name',
|
|
|
|
'service_id',
|
|
|
|
'service_id_url',
|
|
|
|
'status',
|
|
|
|
];
|
2018-08-11 05:09:41 +00:00
|
|
|
|
2018-06-19 12:31:49 +00:00
|
|
|
protected $visible = [
|
2018-08-11 05:09:41 +00:00
|
|
|
'account_name',
|
2018-08-23 05:17:26 +00:00
|
|
|
'admin_service_id_url',
|
2018-06-19 12:31:49 +00:00
|
|
|
'active',
|
|
|
|
'category',
|
|
|
|
'data_orig',
|
|
|
|
'id',
|
2018-08-01 07:09:38 +00:00
|
|
|
'name',
|
2018-11-21 03:37:17 +00:00
|
|
|
'name_full',
|
2018-06-19 12:31:49 +00:00
|
|
|
'next_invoice',
|
|
|
|
'product_name',
|
|
|
|
'service_id',
|
|
|
|
'service_id_url',
|
|
|
|
'status',
|
|
|
|
];
|
2018-05-20 12:53:14 +00:00
|
|
|
|
2018-08-11 05:09:41 +00:00
|
|
|
private $inactive_status = [
|
2019-01-24 10:49:56 +00:00
|
|
|
'CANCELLED',
|
2018-11-21 02:46:16 +00:00
|
|
|
'ORDER-REJECTED',
|
2019-01-24 03:40:33 +00:00
|
|
|
'ORDER-CANCELLED',
|
|
|
|
];
|
|
|
|
|
|
|
|
private $valid_status = [
|
2019-01-24 10:49:56 +00:00
|
|
|
// 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
|
2019-01-24 10:55:43 +00:00
|
|
|
'ORDER-SENT' => ['update_reference'=>'ORDER-SENT','confirm'=>'ORDERED'],
|
|
|
|
// Order Confirmed by Supplier
|
|
|
|
'ORDERED' => ['update_reference'=>'ORDER-SENT'],
|
2018-08-11 05:09:41 +00:00
|
|
|
];
|
|
|
|
|
2019-01-24 10:49:56 +00:00
|
|
|
/**
|
|
|
|
* Account the service belongs to
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2018-05-20 12:53:14 +00:00
|
|
|
public function account()
|
|
|
|
{
|
2018-07-13 04:53:44 +00:00
|
|
|
return $this->belongsTo(Account::class);
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 10:49:56 +00:00
|
|
|
/**
|
|
|
|
* Account that ordered the service
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2018-09-18 02:37:04 +00:00
|
|
|
public function orderby()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Account::class);
|
|
|
|
}
|
|
|
|
|
2019-01-24 10:49:56 +00:00
|
|
|
/**
|
|
|
|
* Tenant that the service belongs to
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2018-09-18 02:37:04 +00:00
|
|
|
public function site()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Site::class);
|
|
|
|
}
|
|
|
|
|
2018-05-20 12:53:14 +00:00
|
|
|
public function service_adsl()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(ServiceAdsl::class,'id','service_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function service_domain()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(ServiceDomain::class,'id','service_id');
|
|
|
|
}
|
|
|
|
|
2018-06-19 12:31:49 +00:00
|
|
|
public function service_host()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(ServiceHost::class,'id','service_id');
|
|
|
|
}
|
|
|
|
|
2018-05-20 12:53:14 +00:00
|
|
|
public function service_ssl()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(ServiceSsl::class,'id','service_id');
|
|
|
|
}
|
|
|
|
|
2018-06-19 12:31:49 +00:00
|
|
|
public function service_voip()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(ServiceVoip::class,'id','service_id');
|
|
|
|
}
|
|
|
|
|
2019-01-24 10:49:56 +00:00
|
|
|
/**
|
|
|
|
* Product of the service
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2018-05-20 12:53:14 +00:00
|
|
|
public function product()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Product::class);
|
|
|
|
}
|
|
|
|
|
2018-11-21 02:46:16 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-19 12:31:49 +00:00
|
|
|
/**
|
|
|
|
* Only query active categories
|
|
|
|
*/
|
2018-08-11 05:09:41 +00:00
|
|
|
public function scopeActive($query)
|
2018-06-19 12:31:49 +00:00
|
|
|
{
|
2018-08-11 05:09:41 +00:00
|
|
|
return $query->where(function () use ($query) {
|
|
|
|
return $query->where('active',TRUE)->orWhereNotIn('order_status',$this->inactive_status);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAccountNameAttribute()
|
|
|
|
{
|
|
|
|
return $this->account->company;
|
2018-06-19 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 05:17:26 +00:00
|
|
|
public function getAdminServiceIdUrlAttribute()
|
|
|
|
{
|
|
|
|
return sprintf('<a href="/a/service/%s">%s</a>',$this->id,$this->service_id);
|
|
|
|
}
|
|
|
|
|
2018-06-05 11:13:57 +00:00
|
|
|
public function getCategoryAttribute()
|
|
|
|
{
|
2018-11-21 02:46:16 +00:00
|
|
|
// @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';
|
2018-06-05 11:13:57 +00:00
|
|
|
}
|
|
|
|
|
2018-08-01 07:09:38 +00:00
|
|
|
public function getNameAttribute()
|
|
|
|
{
|
2018-08-11 05:09:41 +00:00
|
|
|
if (! isset($this->ServicePlugin()->name))
|
2018-08-01 07:09:38 +00:00
|
|
|
return 'Unknown';
|
|
|
|
|
2018-08-11 05:09:41 +00:00
|
|
|
return $this->ServicePlugin()->name;
|
2018-08-01 07:09:38 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 03:37:17 +00:00
|
|
|
public function getNameFullAttribute()
|
|
|
|
{
|
|
|
|
if (! isset($this->ServicePlugin()->full_name))
|
|
|
|
return 'Unknown';
|
|
|
|
|
|
|
|
return $this->ServicePlugin()->full_name;
|
|
|
|
}
|
|
|
|
|
2018-06-05 11:13:57 +00:00
|
|
|
public function getNextInvoiceAttribute()
|
|
|
|
{
|
2018-06-19 12:31:49 +00:00
|
|
|
return $this->date_next_invoice ? $this->date_next_invoice->format('Y-m-d') : NULL;
|
|
|
|
}
|
|
|
|
|
2019-01-24 03:40:33 +00:00
|
|
|
/**
|
|
|
|
* This function will present the Order Info Details
|
|
|
|
*/
|
2019-01-24 06:16:51 +00:00
|
|
|
public function getOrderInfoDetailsAttribute(): string
|
2019-01-24 03:40:33 +00:00
|
|
|
{
|
2019-01-24 06:16:51 +00:00
|
|
|
if (! $this->order_info)
|
|
|
|
return '';
|
|
|
|
|
2019-01-24 03:40:33 +00:00
|
|
|
$result = '';
|
2019-01-24 06:16:51 +00:00
|
|
|
|
2019-01-24 03:40:33 +00:00
|
|
|
foreach ($this->order_info as $k=>$v)
|
2019-01-24 09:12:00 +00:00
|
|
|
{
|
|
|
|
if (in_array($k,['order_reference']))
|
|
|
|
continue;
|
|
|
|
|
2019-01-24 03:40:33 +00:00
|
|
|
$result .= sprintf('%s: <b>%s</b><br>',ucfirst($k),$v);
|
2019-01-24 09:12:00 +00:00
|
|
|
}
|
2019-01-24 03:40:33 +00:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2018-06-19 12:31:49 +00:00
|
|
|
public function getProductNameAttribute()
|
|
|
|
{
|
2018-11-21 02:46:16 +00:00
|
|
|
// @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';
|
2018-06-05 11:13:57 +00:00
|
|
|
}
|
|
|
|
|
2018-05-20 12:53:14 +00:00
|
|
|
public function getServiceExpireAttribute()
|
|
|
|
{
|
|
|
|
return 'TBA';
|
|
|
|
}
|
|
|
|
|
2018-06-19 12:31:49 +00:00
|
|
|
public function getServiceIdAttribute()
|
|
|
|
{
|
2018-07-16 05:06:43 +00:00
|
|
|
return sprintf('%02s-%04s.%05s',$this->site_id,$this->account_id,$this->id);
|
2018-06-19 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getServiceIdUrlAttribute()
|
|
|
|
{
|
2019-01-24 09:12:00 +00:00
|
|
|
return sprintf('<a href="/u/service/%s">%s</a>',$this->id,$this->service_id);
|
2018-06-19 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
2018-05-20 12:53:14 +00:00
|
|
|
public function getServiceNumberAttribute()
|
|
|
|
{
|
|
|
|
return sprintf('%02s.%04s.%04s',$this->site_id,$this->account_id,$this->id);
|
|
|
|
}
|
2018-08-11 05:09:41 +00:00
|
|
|
|
|
|
|
public function getStatusAttribute()
|
|
|
|
{
|
2019-01-24 09:12:00 +00:00
|
|
|
if (! $this->order_status)
|
|
|
|
return $this->active ? 'Active' : 'Inactive';
|
|
|
|
|
2019-01-24 10:55:43 +00:00
|
|
|
return in_array($this->order_status,['ORDER-SENT','ORDER-HOLD','ORDERED'])
|
2019-01-24 09:12:00 +00:00
|
|
|
? 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;
|
2018-08-11 05:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2019-01-24 06:16:51 +00:00
|
|
|
public function nextStatus(string $status) {
|
|
|
|
if ($x=$this->validStatus($status))
|
2018-08-23 05:17:26 +00:00
|
|
|
{
|
2019-01-24 06:16:51 +00:00
|
|
|
$this->order_status = $x;
|
|
|
|
$this->save();
|
2018-08-23 05:17:26 +00:00
|
|
|
|
2019-01-24 06:16:51 +00:00
|
|
|
return $this;
|
2018-08-23 05:17:26 +00:00
|
|
|
}
|
2019-01-24 06:16:51 +00:00
|
|
|
|
|
|
|
abort(500,'Next Status not set up for:'.$this->order_status);
|
2018-08-23 05:17:26 +00:00
|
|
|
}
|
|
|
|
|
2018-08-11 05:09:41 +00:00
|
|
|
/**
|
|
|
|
* This function will return the associated service model for the product type
|
|
|
|
*/
|
|
|
|
private function ServicePlugin()
|
|
|
|
{
|
2018-11-21 02:46:16 +00:00
|
|
|
// @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;
|
|
|
|
|
2018-08-11 05:09:41 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2019-01-24 03:40:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|