From 2b06eca08071bff1881305131d6417fe4b4a627c Mon Sep 17 00:00:00 2001 From: Deon George Date: Wed, 21 May 2025 13:39:35 +1000 Subject: [PATCH] Move some service::class methods into __get(), no functional changes --- app/Models/Account.php | 4 +- app/Models/Service.php | 77 ++++--------------- .../adminlte/service/domain/list.blade.php | 2 +- .../adminlte/service/email/list.blade.php | 2 +- .../adminlte/service/hosting/list.blade.php | 2 +- .../service/widget/information.blade.php | 2 +- .../service/widget/internal.blade.php | 16 ++-- 7 files changed, 31 insertions(+), 74 deletions(-) diff --git a/app/Models/Account.php b/app/Models/Account.php index 8d7ca14..cb2ffda 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -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; diff --git a/app/Models/Service.php b/app/Models/Service.php index 6c6c7b3..d8854b4 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -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(); diff --git a/resources/views/theme/backend/adminlte/service/domain/list.blade.php b/resources/views/theme/backend/adminlte/service/domain/list.blade.php index 7d1ea8d..7f22b3b 100644 --- a/resources/views/theme/backend/adminlte/service/domain/list.blade.php +++ b/resources/views/theme/backend/adminlte/service/domain/list.blade.php @@ -45,7 +45,7 @@ {{ $oo->service_expire ? $oo->service_expire->format('Y-m-d') : '-' }} {{ $oo->registrar->name }} {{ $oo->registrar_ns }} - @if ($oo->service->isBilled()) {{ $oo->service->invoice_next->format('Y-m-d') }}@else - @endif + @if ($oo->service->is_billed) {{ $oo->service->invoice_next->format('Y-m-d') }}@else - @endif @if (! $oo->service->external_billing)${{ number_format($oo->service->next_invoice_items()->sum('total'),2) }}@else - @endif {{ $oo->service->billing_interval_string }} diff --git a/resources/views/theme/backend/adminlte/service/email/list.blade.php b/resources/views/theme/backend/adminlte/service/email/list.blade.php index 15fdded..a83db6a 100644 --- a/resources/views/theme/backend/adminlte/service/email/list.blade.php +++ b/resources/views/theme/backend/adminlte/service/email/list.blade.php @@ -49,7 +49,7 @@ {{ $oo->admin_url }} @if($oo->admin_user){{ $oo->admin_user }}/{{ $oo->admin_pass }}@else   @endif {{ number_format($oo->accounts ?: 0) }} - @if ($oo->service->isBilled()) {{ $oo->service->invoice_next->format('Y-m-d') }} @else - @endif + @if ($oo->service->is_billed) {{ $oo->service->invoice_next->format('Y-m-d') }} @else - @endif @if (! $oo->service->external_billing)${{ number_format($oo->service->next_invoice_items()->sum('total'),2) }}@else - @endif {{ $oo->service->billing_interval_string }} diff --git a/resources/views/theme/backend/adminlte/service/hosting/list.blade.php b/resources/views/theme/backend/adminlte/service/hosting/list.blade.php index 4cd237c..79fd84c 100644 --- a/resources/views/theme/backend/adminlte/service/hosting/list.blade.php +++ b/resources/views/theme/backend/adminlte/service/hosting/list.blade.php @@ -43,7 +43,7 @@ {{ $oo->service->name }} {{ $oo->service_expire ? $oo->service_expire->format('Y-m-d') : '-' }} {{ $oo->service->product->supplier->name }} - @if ($oo->service->isBilled()) {{ $oo->service->invoice_next->format('Y-m-d') }}@else - @endif + @if ($oo->service->is_billed) {{ $oo->service->invoice_next->format('Y-m-d') }}@else - @endif @if (! $oo->service->external_billing)${{ number_format($oo->service->next_invoice_items()->sum('total'),2) }}@else - @endif {{ $oo->service->billing_interval_string }} diff --git a/resources/views/theme/backend/adminlte/service/widget/information.blade.php b/resources/views/theme/backend/adminlte/service/widget/information.blade.php index 1c5631d..033ad4f 100644 --- a/resources/views/theme/backend/adminlte/service/widget/information.blade.php +++ b/resources/views/theme/backend/adminlte/service/widget/information.blade.php @@ -47,7 +47,7 @@ Amount - @if($o->isChargeOverridden()) + @if($o->is_charge_overridden) @if($o->billing_charge < $o->charge_orig)${{ number_format($o->charge_orig,2) }} @endif${{ number_format($o->billing_charge,2) }} @else ${{ number_format($o->billing_charge,2) }} diff --git a/resources/views/theme/backend/adminlte/service/widget/internal.blade.php b/resources/views/theme/backend/adminlte/service/widget/internal.blade.php index fe6668c..db69007 100644 --- a/resources/views/theme/backend/adminlte/service/widget/internal.blade.php +++ b/resources/views/theme/backend/adminlte/service/widget/internal.blade.php @@ -67,11 +67,11 @@ Billing Price - isChargeOverridden())class="text-danger"@endif>${{ number_format($b=$o->billing_charge,2) }} - isCostOverridden())class="text-danger"@endif>${{ number_format($a=$o->billing_cost,2) }} + $o->is_charge_overridden])>${{ number_format($b=$o->billing_charge,2) }} + $o->is_cost_overridden])>${{ number_format($a=$o->billing_cost,2) }} {!! markup($a,$b) !!} @if($p->exists) - isChargeOverridden())class="text-danger"@endif>${{ number_format($b=$o->account->taxed($p->base_charge),2) }} + is_charge_overridden)class="text-danger"@endif>${{ number_format($b=$o->account->taxed($p->base_charge),2) }} ${{ number_format($a=$o->account->taxed($p->base_cost),2) }} {!! markup($a,$b) !!} @endif @@ -79,15 +79,15 @@ Monthly Price - isChargeOverridden()) class="text-danger" @endif> - @if($x) + $o->is_charge_overridden])> + @if($o->is_charge_overridden) ${{ number_format($b=$o->billing_charge_normalised,2) }} @else ${{ number_format($b=$o->billing_charge_normalised,2) }} @endif - isCostOverridden()) class="text-danger" @endif> - @if($x) + $o->is_cost_overridden])> + @if($o->is_cost_overridden) ${{ number_format($b=$o->billing_cost_normalised,2) }} @else ${{ number_format($a=$o->billing_cost_normalised,2) }} @@ -95,7 +95,7 @@ {!! markup($a,$b) !!} @if($p->exists) - isChargeOverridden()) class="text-danger" @endif>${{ number_format($b=$o->account->taxed($p->base_charge)*Invoice::billing_change($o->billing_interval,Invoice::BILL_MONTHLY),2) }} + is_charge_overridden) class="text-danger" @endif>${{ number_format($b=$o->account->taxed($p->base_charge)*Invoice::billing_change($o->billing_interval,Invoice::BILL_MONTHLY),2) }} ${{ number_format($a=$o->account->taxed($p->base_cost)*Invoice::billing_change($o->billing_interval,Invoice::BILL_MONTHLY),2) }} {!! markup($a,$b) !!} @endif