osb/app/Models/Base/ServiceType.php

99 lines
2.0 KiB
PHP
Raw Normal View History

<?php
namespace App\Models\Base;
2022-04-19 07:07:39 +00:00
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
2022-04-19 07:07:39 +00:00
use App\Interfaces\ServiceItem;
use App\Models\{Account,Service};
use App\Models\Supplier\Type;
use App\Traits\{ScopeServiceActive,ScopeServiceUserAuthorised};
2019-06-29 00:14:12 +00:00
2022-04-19 07:07:39 +00:00
abstract class ServiceType extends Model implements ServiceItem
{
2022-04-19 07:07:39 +00:00
use ScopeServiceActive,ScopeServiceUserAuthorised;
protected $dates = [
'expire_at',
];
public $timestamps = FALSE;
2022-04-19 07:07:39 +00:00
/* RELATIONS */
/**
* Account this service belongs to
* @return \Illuminate\Database\Eloquent\Relations\HasOneThrough
*/
public function account()
{
return $this->hasOneThrough(Account::class,Service::class);
}
/**
* @NOTE: The service_id column could be discarded, if the id column=service_id
* @return \Illuminate\Database\Eloquent\Relations\MorphOne
*/
public function service()
{
2020-02-19 12:37:45 +00:00
return $this->morphOne(Service::class,'type','model','id','service_id');
}
2019-06-29 00:14:12 +00:00
2022-04-19 07:07:39 +00:00
/* SCOPES */
2019-06-29 00:14:12 +00:00
/**
* Search for a record
*
* @param $query
* @param string $term
2022-04-19 07:07:39 +00:00
* @return mixed
2019-06-29 00:14:12 +00:00
*/
public function scopeSearch($query,string $term)
{
return $query
->with(['service'])
2022-04-19 07:07:39 +00:00
->join('services','services.id','=',$this->getTable().'.service_id')
->Where('services.id','like','%'.$term.'%');
2019-06-29 00:14:12 +00:00
}
2022-04-19 07:07:39 +00:00
/* INTERFACE */
public function getContractTermAttribute(): int
{
return $this->service->offering->contract_term;
}
public function getServiceExpireAttribute(): ?Carbon
{
return $this->expire_at ?: $this->service->invoice_next_at;
}
public function hasExpired(): bool
{
return (! $this->inContract()) && ($this->service->invoice_next_at && $this->service->invoice_next_at->isPast());
}
public function inContract(): bool
{
return $this->expire_at && $this->expire_at->isFuture();
}
/* ATTRIBUTES */
2019-06-29 00:14:12 +00:00
public function getTypeAttribute()
{
return strtolower((new \ReflectionClass($this))->getShortName());
}
2022-04-19 07:07:39 +00:00
/* METHODS */
/**
* The supplier's service that we provide
*
* @return Type
*/
public function supplied(): Type
{
return $this->service->product->type->supplied;
}
}