From 04ae35b1dddeafa246bae0cafc9fb3a611db4027 Mon Sep 17 00:00:00 2001 From: Deon George Date: Thu, 22 May 2025 11:55:09 +1000 Subject: [PATCH] Move base_cost/base_charge product::class methods into __get(), and base_charge(), no functional changes --- app/Http/Controllers/ServiceController.php | 2 +- app/Models/Product.php | 48 +++++++++---------- app/Models/Service.php | 4 +- app/Models/Supplier/Type.php | 5 -- .../service/widget/internal.blade.php | 4 +- .../adminlte/service/widget/update.blade.php | 2 +- .../supplier/widget/products.blade.php | 2 +- .../metronic/order/widget/info/base.blade.php | 2 +- 8 files changed, 31 insertions(+), 38 deletions(-) diff --git a/app/Http/Controllers/ServiceController.php b/app/Http/Controllers/ServiceController.php index 7134721..5b5a31c 100644 --- a/app/Http/Controllers/ServiceController.php +++ b/app/Http/Controllers/ServiceController.php @@ -343,7 +343,7 @@ class ServiceController extends Controller $co->type = $iio->item_type; $co->start_at = $start_at; $co->stop_at = $iio->stop_at; - $co->amount = Arr::get($request->broadband,'price') ?: $po->base_charge; + $co->amount = Arr::get($request->broadband,'price') ?: $po->base_charge(); $co->taxable = TRUE; // @todo this should be determined $co->quantity = $start_at->diffInDays($iio->stop_at)/$iio->start_at->diffInDays($iio->stop_at); $charges->push($co); diff --git a/app/Models/Product.php b/app/Models/Product.php index a91292f..438b6a6 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -34,6 +34,7 @@ use App\Traits\{ProductDetails,ProviderRef}; * + lid : Local ID for product (part number) * + sid : System ID for product (part number) * + base_charge : Default billing amount + * * + billing_interval : Default Billing Interval * + billing_interval_string: Default Billing Interval in human-readable form * + category : Type of product supplied @@ -62,8 +63,6 @@ use App\Traits\{ProductDetails,ProviderRef}; * group => [ pricing/setup ] * ] * ] - * - * @package App\Models */ class Product extends Model implements IDs { @@ -73,6 +72,15 @@ class Product extends Model implements IDs 'pricing' => CollectionOrNull::class, ]; + public function __get($key): mixed + { + return match ($key) { + 'base_cost' => round($this->supplied->base_cost,2)*Invoice::billing_change($this->type->billing_interval,$this->billing_interval) ?: 0, + + default => parent::__get($key), + }; + } + /* STATIC */ /** @@ -162,28 +170,6 @@ class Product extends Model implements IDs /* ATTRIBUTES */ - /** - * The amount we invoice each time period for this service - * - * @param int|NULL $timeperiod - * @param Group|NULL $go - * @return float - */ - public function getBaseChargeAttribute(int $timeperiod=NULL,Group $go=NULL): float - { - return $this->_charge('base',$timeperiod,$go); - } - - /** - * The base cost of this product at the appropriate billing interval - * - * @return float - */ - public function getBaseCostAttribute(): float - { - return round($this->supplied->base_cost,2)*Invoice::billing_change($this->type->billing_interval,$this->billing_interval) ?: 0; - } - /** * Our default billing interval * Its the max of what we define, or what the supplier bills us at @@ -248,7 +234,7 @@ class Product extends Model implements IDs public function getMinChargeAttribute(int $timeperiod=NULL,Group $go=NULL): float { return $this->getSetupChargeAttribute($timeperiod,$go) - + $this->getBaseChargeAttribute($timeperiod,$go)*Invoice::billing_change($this->billing_interval,$this->type->billing_interval)*$this->type->contract_term; + + $this->base_charge($timeperiod,$go)*Invoice::billing_change($this->billing_interval,$this->type->billing_interval)*$this->type->contract_term; } /** @@ -383,6 +369,18 @@ class Product extends Model implements IDs ->values(); } + /** + * The amount we invoice each time period for this service + * + * @param int|NULL $timeperiod + * @param Group|NULL $go + * @return float + */ + public function base_charge(?int $timeperiod=NULL,?Group $go=NULL): float + { + return $this->_charge('base',$timeperiod,$go); + } + /** * Return the charge from the pricing table for the specific time period and group * diff --git a/app/Models/Service.php b/app/Models/Service.php index cbafdb2..d036856 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -291,7 +291,7 @@ class Service extends Model implements IDs 'billing_cost_orig_normalised_taxed' => $this->billing_cost_orig_taxed*Invoice::billing_change($this->product->type->billing_interval,Invoice::BILL_MONTHLY), 'billing_cost_orig_taxed' => $this->account->taxed($this->billing_cost_orig), - 'billing_charge_orig' => $this->product->getBaseChargeAttribute($this->billing_interval,$this->account->group), + 'billing_charge_orig' => $this->product->base_charge($this->billing_interval,$this->account->group), 'billing_charge_orig_normalised_taxed' => $this->billing_charge_orig_taxed*Invoice::billing_change($this->billing_interval,Invoice::BILL_MONTHLY), 'billing_charge_orig_taxed' => $this->account->taxed($this->billing_charge_orig), 'billing_charge' => $this->billing_charge(), @@ -908,7 +908,7 @@ class Service extends Model implements IDs private function billing_cost(): float { return is_null($this->cost) - ? $this->product->getBaseCostAttribute() + ? $this->product->base_cost : $this->cost*Invoice::billing_change($this->product->type->billing_interval,$this->billing_interval); } diff --git a/app/Models/Supplier/Type.php b/app/Models/Supplier/Type.php index 67e9c0e..e4d7d81 100644 --- a/app/Models/Supplier/Type.php +++ b/app/Models/Supplier/Type.php @@ -33,11 +33,6 @@ abstract class Type extends Model /* ATTRIBUTES */ - public function getBaseCostAttribute(?float $val): float - { - return $val ?: 0; - } - /** * This will return the category of the product (eg: domain, hosting, etc) which is the basis for all * other logic of these types. 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 c5ba318..c93acb1 100644 --- a/resources/views/theme/backend/adminlte/service/widget/internal.blade.php +++ b/resources/views/theme/backend/adminlte/service/widget/internal.blade.php @@ -73,7 +73,7 @@ $o->is_cost_overridden])>${{ number_format($a=$o->billing_cost_taxed,2) }} {!! markup($a,$b) !!} @if($p->exists) - is_charge_overridden)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 @@ -97,7 +97,7 @@ {!! markup($a,$b) !!} @if($p->exists) - $o->is_charge_overridden])>${{ number_format($b=$o->account->taxed($p->base_charge)*Invoice::billing_change($o->billing_interval,Invoice::BILL_MONTHLY),2) }} + $o->is_charge_overridden])>${{ number_format($b=$o->account->taxed($p->base_charge())*Invoice::billing_change($o->billing_interval,Invoice::BILL_MONTHLY),2) }} $o->is_cost_overridden])>${{ number_format($a=$o->account->taxed($p->base_cost)*Invoice::billing_change($o->billing_interval,Invoice::BILL_MONTHLY),2) }} {!! markup($a,$b) !!} @endif diff --git a/resources/views/theme/backend/adminlte/service/widget/update.blade.php b/resources/views/theme/backend/adminlte/service/widget/update.blade.php index 87a3dd0..425da21 100644 --- a/resources/views/theme/backend/adminlte/service/widget/update.blade.php +++ b/resources/views/theme/backend/adminlte/service/widget/update.blade.php @@ -57,7 +57,7 @@ 'id'=>$item->id, 'value'=>sprintf('%s $%3.2f [%s/%3.2f]', $item->name, - $o->account->taxed($item->base_charge)*Invoice::billing_change($item->billing_interval,Invoice::BILL_MONTHLY), + $o->account->taxed($item->base_charge())*Invoice::billing_change($item->billing_interval,Invoice::BILL_MONTHLY), $item->supplied->name, $o->account->taxed($item->base_cost)*Invoice::billing_change($item->billing_interval,Invoice::BILL_MONTHLY), )])" :required="true"/> diff --git a/resources/views/theme/backend/adminlte/supplier/widget/products.blade.php b/resources/views/theme/backend/adminlte/supplier/widget/products.blade.php index b891e33..55686db 100644 --- a/resources/views/theme/backend/adminlte/supplier/widget/products.blade.php +++ b/resources/views/theme/backend/adminlte/supplier/widget/products.blade.php @@ -47,7 +47,7 @@ {{ number_format($site->taxed($po->setup_cost),2) }} {{ number_format($site->taxed($po->base_cost),2) }} {{ number_format($site->taxed($po->setup_charge),2) }} - {{ number_format($site->taxed($po->base_charge),2) }} + {{ number_format($site->taxed($po->base_charge()),2) }} {{ number_format($po->services->count()) }} {{ number_format($po->services->where('active')->count()) }} diff --git a/resources/views/theme/frontend/metronic/order/widget/info/base.blade.php b/resources/views/theme/frontend/metronic/order/widget/info/base.blade.php index c938b3e..7eef0fc 100644 --- a/resources/views/theme/frontend/metronic/order/widget/info/base.blade.php +++ b/resources/views/theme/frontend/metronic/order/widget/info/base.blade.php @@ -20,7 +20,7 @@ Cost + {{-- @todo this should use account::taxed() when the user is known --}} - ${{ number_format($user->exists ? Config::get('site')->taxed($pdo->base_charge) : Config::get('site')->taxed($pdo->base_charge),2) }} + ${{ number_format($user->exists ? Config::get('site')->taxed($pdo->base_charge()) : Config::get('site')->taxed($pdo->base_charge()),2) }} Default Billing