More work on ordering

This commit is contained in:
Deon George
2018-08-11 15:09:41 +10:00
parent 499d44289e
commit 5373e6b246
33 changed files with 730 additions and 163 deletions

View File

@@ -3,14 +3,25 @@
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',
@@ -19,7 +30,9 @@ class Service extends Model
'service_id_url',
'status',
];
protected $visible = [
'account_name',
'active',
'category',
'data_orig',
@@ -32,6 +45,10 @@ class Service extends Model
'status',
];
private $inactive_status = [
'CANCELLED',
];
public function account()
{
return $this->belongsTo(Account::class);
@@ -70,9 +87,16 @@ class Service extends Model
/**
* Only query active categories
*/
public function scopeActive()
public function scopeActive($query)
{
return $this->where('active',TRUE);
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()
@@ -82,10 +106,10 @@ class Service extends Model
public function getNameAttribute()
{
if (! isset($this->getServiceDetail()->name))
if (! isset($this->ServicePlugin()->name))
return 'Unknown';
return $this->getServiceDetail()->name;
return $this->ServicePlugin()->name;
}
public function getNextInvoiceAttribute()
@@ -98,28 +122,6 @@ class Service extends Model
return $this->product->name($this->account->language);
}
/**
* This function will return the associated service model for the product type
*/
public function getServiceDetail()
{
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;
}
}
public function getStatusAttribute()
{
return $this->active ? 'Active' : 'Inactive';
}
public function getServiceExpireAttribute()
{
return 'TBA';
@@ -139,4 +141,41 @@ class Service extends Model
{
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;
}
}
}