Work on Service rendering

This commit is contained in:
Deon George
2020-02-05 13:47:24 +09:00
parent 01a7d73c17
commit 5f5d114f42
12 changed files with 412 additions and 313 deletions

View File

@@ -4,6 +4,10 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Arr;
use App\Traits\NextKey;
@@ -11,17 +15,18 @@ use App\Traits\NextKey;
class Service extends Model
{
use NextKey;
const RECORD_ID = 'service';
public $incrementing = FALSE;
const CREATED_AT = 'date_orig';
const UPDATED_AT = 'date_last';
public $incrementing = FALSE;
protected $table = 'ab_service';
protected $dates = ['date_last_invoice','date_next_invoice'];
protected $dates = [
'date_last_invoice',
'date_next_invoice'
];
public $dateFormat = 'U';
protected $with = ['product.descriptions','account.language','type'];
protected $table = 'ab_service';
protected $casts = [
'order_info'=>'array',
@@ -91,17 +96,27 @@ class Service extends Model
/**
* Account the service belongs to
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
* @return BelongsTo
*/
public function account()
{
return $this->belongsTo(Account::class);
}
/**
* Return automatic billing details
*
* @return HasOne
*/
public function billing()
{
return $this->hasOne(AccountBilling::class);
}
/**
* Return Charges associated with this Service
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
* @return HasMany
*/
public function charges()
{
@@ -146,10 +161,9 @@ class Service extends Model
/**
* Account that ordered the service
*
* @todo changed to orderedby
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
* @return BelongsTo
*/
public function orderby()
public function orderedby()
{
return $this->belongsTo(Account::class);
}
@@ -157,30 +171,23 @@ class Service extends Model
/**
* Product of the service
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
* @return BelongsTo
*/
public function product()
{
return $this->belongsTo(Product::class);
}
/**
* Tenant that the service belongs to
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function site()
{
return $this->belongsTo(Site::class);
}
/**
* Return a child model with details of the service
*
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
* @return MorphTo
*/
public function type()
{
if (! $this->model)
abort(500,'Missing Model in',['service'=>$this]);
return $this->morphTo(null,'model','id','service_id');
}
@@ -192,7 +199,8 @@ class Service extends Model
public function scopeActive($query)
{
return $query->where(function () use ($query) {
$query->where('active',TRUE)->orWhereNotIn('order_status',$this->inactive_status);
$query->where('active',TRUE)
->orWhereNotIn('order_status',$this->inactive_status);
});
}
@@ -205,7 +213,8 @@ class Service extends Model
public function scopeInActive($query)
{
return $query->where(function () use ($query) {
$query->where('active',FALSE)->orWhereIn('order_status',$this->inactive_status);
$query->where('active',FALSE)
->orWhereIn('order_status',$this->inactive_status);
});
}
@@ -251,6 +260,16 @@ class Service extends Model
return $this->getUrlAdminAttribute();
}
/**
* Return the auto billing details
*
* @return mixed
*/
public function getAutoPayAttribute()
{
return $this->billing;
}
public function getBillingPriceAttribute(): float
{
// @todo Temporary for services that dont have recur_schedule set.
@@ -337,6 +356,7 @@ class Service extends Model
* EG:
* For ADSL, this would be the phone number,
* For Hosting, this would be the domain name, etc
* @deprecated
*/
public function getNameShortAttribute()
{
@@ -372,6 +392,25 @@ class Service extends Model
return $result;
}
/**
* Work out when this service has been paid to.
*
* @todo This might need to be optimised
*/
public function getPaidToAttribute()
{
foreach ($this->invoices->reverse() as $o) {
if ($o->due == 0) {
return $o->items
->filter(function($item) {
return $item->item_type === 0;
})
->last()
->date_stop;
}
}
}
/**
* Get the Product's Category for this service
*
@@ -392,9 +431,9 @@ class Service extends Model
}
/**
* @deprecated see getServiceIdAttribute()
* @deprecated see getSIDAttribute()
*/
public function getServiceIdAttribute()
public function getServiceIdAttribute(): string
{
return $this->getSIDAttribute();
}
@@ -425,6 +464,50 @@ class Service extends Model
return sprintf('%02s-%04s.%05s',$this->site_id,$this->account_id,$this->id);
}
/**
* Return the service description.
* For:
* + Broadband, this is the service address
* + Domains, blank
* + Hosting, blank
* + SSL, blank
*
* @return string
*/
public function getSDescAttribute(): string
{
return $this->type->service_description;
}
/**
* Return the service name.
* For:
* + Broadband, this is the service number
* + Domains, this is the full domain name
* + Hosting, this is the full domain name
* + SSL, this is the DN
*
* @return string
*/
public function getSNameAttribute(): string
{
return $this->type->service_name;
}
/**
* Return the service product type
* This is used for view specific details
*
* @return string
*/
public function getSTypeAttribute(): string
{
switch($this->product->model) {
case 'App\Models\Product\Adsl': return 'broadband';
default: abort(500,'Product type not configured',['product'=>$this->product]);
}
}
/**
* Return the Service Status
*
@@ -545,6 +628,16 @@ class Service extends Model
return (! $this->external_billing) AND (! $this->suspend_billing) AND $this->getInvoiceNextAttribute()->lessThan(now()->addDays($days));
}
/**
* Identify if a service is being ordered
*
* @return bool
*/
public function isPending(): bool
{
return ! $this->active AND ! in_array($this->order_status,$this->inactive_status);
}
public function next_invoice_items(): \Illuminate\Support\Collection
{
$result = collect();