Move some service::class methods into __get(), no functional changes

This commit is contained in:
2025-05-21 13:39:35 +10:00
parent 98d18c00a3
commit 2b06eca080
7 changed files with 31 additions and 74 deletions

View File

@@ -257,7 +257,7 @@ class Account extends Model implements IDs
{
$svs = $this
->services_active
->filter(fn($item)=>$item->isBilled() && $item->invoice_next)
->filter(fn($item)=>$item->is_billed && $item->invoice_next)
->sortBy(fn($item)=>(string)$item->invoice_next);
// Collect all the invoice items for our active services
@@ -280,7 +280,7 @@ class Account extends Model implements IDs
->flatten();
// Add any account charges (charges with no active service)
foreach ($this->charges->filter(function($item) { return $item->unprocessed && ((! $this->service_id) || (! $item->service->isBilled())); }) as $oo) {
foreach ($this->charges->filter(function($item) { return $item->unprocessed && ((! $this->service_id) || (! $item->service->is_billed)); }) as $oo) {
$ii = new InvoiceItem;
$ii->active = TRUE;

View File

@@ -29,6 +29,11 @@ use App\Traits\{ScopeAccountUserAuthorised,ScopeServiceActive,SiteID};
* - Type, what service we are providing, made up of a product we supply - in the DB these are service/*
*
* Attributes for services:
* + is_billed : Does this service generate an invoice
* + is_charge_overridden : Has the price been overridden
* + is_cost_overridden : Has the cost been overridden
*
* Attributes for services (OLD):
* + additional_cost : Pending additional charges for this service (excluding setup) //@todo check all these are still valid
* + billing_charge : Charge for this service each invoice period
* + billing_interval : The period that this service is billed for by default
@@ -44,8 +49,6 @@ use App\Traits\{ScopeAccountUserAuthorised,ScopeServiceActive,SiteID};
* + supplied : The model of the supplier's product used for this service.
*
* Methods:
* + isChargeOverridden : Has the price been overridden?
* + isCostOverridden : Has the cost been overridden?
* + isPending : Is this a pending active service
*
* @package App\Models
@@ -268,6 +271,16 @@ class Service extends Model implements IDs
],
];
public function __get($key): mixed
{
return match ($key) {
'is_billed' => (! ($this->external_billing || $this->suspend_billing || ($this->price === 0))),
'is_charge_overridden' => (! is_null($this->price)),
'is_cost_overridden' => (! is_null($this->cost)),
default => parent::__get($key),
};
}
/* STATIC */
/**
@@ -295,7 +308,7 @@ class Service extends Model implements IDs
*
* @return string
*/
public function getLIDattribute(): string
public function getLIDAttribute(): string
{
return sprintf('%05s',$this->id);
}
@@ -464,37 +477,6 @@ class Service extends Model implements IDs
/* SCOPES */
/**
* Only query active categories
* @deprecated use ScopeServiceActive
*/
public function scopeActive($query)
{
throw new \Exception('deprecated');
return $query->where(
fn($query)=>
$query->where($this->getTable().'.active',TRUE)
->orWhereNotIn('order_status',self::INACTIVE_STATUS)
);
}
/**
* Find inactive services.
*
* @param $query
* @return mixed
* @deprecated use ScopeServiceInactive
*/
public function scopeInactive($query)
{
dd('deprecated');
return $query->where(
fn($query)=>
$query->where($this->getTable().'.active',FALSE)
->orWhereIn('order_status',self::INACTIVE_STATUS)
);
}
/**
* Search for a record
*
@@ -1107,31 +1089,6 @@ class Service extends Model implements IDs
return FALSE;
}
/**
* Do we bill for this service
*
* @return bool
*/
public function isBilled(): bool
{
return ! ($this->external_billing || $this->suspend_billing || ($this->price === 0));
}
/**
* Has the price for this service been overridden
*
* @return bool
*/
public function isChargeOverridden(): bool
{
return ! is_null($this->price);
}
public function isCostOverridden(): bool
{
return ! is_null($this->cost);
}
public function isContract(): bool
{
return $this->getContractEndAttribute() && $this->getContractEndAttribute()->greaterThan(Carbon::now());
@@ -1163,7 +1120,7 @@ class Service extends Model implements IDs
*/
public function next_invoice_items(Carbon $billdate=NULL): Collection
{
if ($this->wasCancelled() || (! $this->isBilled()))
if ($this->wasCancelled() || (! $this->is_billed))
return collect();
$o = collect();