Start work on updating services

This commit is contained in:
Deon George
2022-04-19 17:07:39 +10:00
parent ebf08ea414
commit 621a132e35
63 changed files with 1038 additions and 612 deletions

View File

@@ -2,14 +2,34 @@
namespace App\Models\Base;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use App\Models\Service;
use App\Interfaces\ServiceItem;
use App\Models\{Account,Service};
use App\Models\Supplier\Type;
use App\Traits\{ScopeServiceActive,ScopeServiceUserAuthorised};
abstract class ServiceType extends Model
abstract class ServiceType extends Model implements ServiceItem
{
use ScopeServiceActive,ScopeServiceUserAuthorised;
protected $dates = [
'expire_at',
];
public $timestamps = FALSE;
/* 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
@@ -19,27 +39,61 @@ abstract class ServiceType extends Model
return $this->morphOne(Service::class,'type','model','id','service_id');
}
/** SCOPES */
/* SCOPES */
/**
* Search for a record
*
* @param $query
* @param string $term
* @return
* @return mixed
*/
public function scopeSearch($query,string $term)
{
return $query
->with(['service'])
->join('ab_service','ab_service.id','=',$this->getTable().'.service_id')
->Where('ab_service.id','like','%'.$term.'%');
->join('services','services.id','=',$this->getTable().'.service_id')
->Where('services.id','like','%'.$term.'%');
}
/** ATTRIBUTES **/
/* 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 */
public function getTypeAttribute()
{
return strtolower((new \ReflectionClass($this))->getShortName());
}
/* METHODS */
/**
* The supplier's service that we provide
*
* @return Type
*/
public function supplied(): Type
{
return $this->service->product->type->supplied;
}
}