Product Model optimisation

This commit is contained in:
Deon George 2023-05-05 10:32:04 +10:00
parent 0f91ce4940
commit 96f799f535
13 changed files with 145 additions and 115 deletions

View File

@ -24,7 +24,7 @@ class ProductController extends Controller
return Product\Broadband::select(['id','supplier_item_id'])
->with(['supplied.supplier_detail.supplier'])
->get()
->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier_detail->supplier->name,$item->supplied->name)]; })
->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; })
->sortBy('name')
->values();
@ -32,7 +32,7 @@ class ProductController extends Controller
return Product\Domain::select(['id','supplier_item_id'])
->with(['supplied.supplier_detail.supplier'])
->get()
->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier_detail->supplier->name,$item->supplied->name)]; })
->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; })
->sortBy('name')
->values();
@ -40,7 +40,7 @@ class ProductController extends Controller
return Product\Email::select(['id','supplier_item_id'])
->with(['supplied.supplier_detail.supplier'])
->get()
->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier_detail->supplier->name,$item->supplied->name)]; })
->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; })
->sortBy('name')
->values();
@ -48,7 +48,7 @@ class ProductController extends Controller
return Product\Generic::select(['id','supplier_item_id'])
->with(['supplied.supplier_detail.supplier'])
->get()
->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier_detail->supplier->name,$item->supplied->name)]; })
->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; })
->sortBy('name')
->values();
@ -56,7 +56,7 @@ class ProductController extends Controller
return Product\Host::select(['id','supplier_item_id'])
->with(['supplied.supplier_detail.supplier'])
->get()
->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier_detail->supplier->name,$item->supplied->name)]; })
->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; })
->sortBy('name')
->values();
@ -64,7 +64,7 @@ class ProductController extends Controller
return Product\Phone::select(['id','supplier_item_id'])
->with(['supplied.supplier_detail.supplier'])
->get()
->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier_detail->supplier->name,$item->supplied->name)]; })
->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; })
->sortBy('name')
->values();

View File

@ -161,7 +161,7 @@ class ImportCosts implements ShouldQueue
break;
case 'phone':
case 'telephone':
$to = Cost\Phone::where('site_id',$this->co->site_id)
->where('cost_id',$this->co->id)
->where('service_phone_id',$so->type->id)

View File

@ -40,10 +40,10 @@ class OrderRequestApprove extends Mailable
// @todo This is not consistent with Cancel/Change Request
switch ($this->service->category) {
case 'BROADBAND': $subject = sprintf('%s: %s',$this->service->category,$this->service->type->service_address);
case 'broadband': $subject = sprintf('%s: %s',$this->service->category,$this->service->type->service_address);
break;
case 'PHONE': $subject = sprintf('%s: %s',$this->service->category,$this->service->type->service_number);
case 'telephone': $subject = sprintf('%s: %s',$this->service->category,$this->service->type->service_number);
break;
default:

View File

@ -6,11 +6,12 @@ use Awobaz\Compoships\Compoships;
use Illuminate\Container\Container;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Leenooks\Traits\ScopeActive;
use App\Interfaces\{IDs,ProductItem};
@ -166,7 +167,7 @@ class Product extends Model implements IDs
*/
public function getBaseChargeAttribute(int $timeperiod=NULL,Group $go=NULL): float
{
return $this->getCharge('base',$timeperiod,$go);
return $this->_charge('base',$timeperiod,$go);
}
/**
@ -176,6 +177,7 @@ class Product extends Model implements IDs
* @param Group|null $go
* @param Collection|NULL $taxes
* @return float
* @deprecated move to account::tax_calc
*/
public function getBaseChargeTaxableAttribute(int $timeperiod=NULL,Group $go=NULL,Collection $taxes=NULL): float
{
@ -189,7 +191,7 @@ class Product extends Model implements IDs
*/
public function getBaseCostAttribute(): float
{
return round($this->getSuppliedAttribute()->base_cost*Invoice::billing_change($this->getSuppliedAttribute()->getBillingIntervalAttribute(),$this->getBillingIntervalAttribute()) ?: 0,2);
return $this->supplied->base_cost*Invoice::billing_change($this->supplied->billing_interval,$this->billing_interval) ?: 0;
}
/**
@ -197,6 +199,7 @@ class Product extends Model implements IDs
*
* @param Collection|NULL $taxes
* @return float
* @deprecated move to account::tax_calc
*/
public function getBaseCostTaxableAttribute(Collection $taxes=NULL): float
{
@ -211,20 +214,22 @@ class Product extends Model implements IDs
*/
public function getBillingIntervalAttribute(): int
{
return max($this->price_recur_default,$this->getSuppliedAttribute()->getBillingIntervalAttribute());
return max($this->price_recur_default,$this->supplied->billing_interval);
}
/**
* This will return the category of the product (eg: domain, webhosting, etc) which is the basis for all
* other logic of these types.
*
* @return string
*/
public function getCategoryAttribute(): string
{
return strtolower(Str::studly($this->category_name));
}
/**
* Return the type of service is provided. eg: Broadband, Phone.
*
* @return string|null
*/
public function getCategoryAttribute(): ?string
{
return $this->supplied ? $this->supplied->getCategoryAttribute() : NULL;
}
/**
* This will return the category of the product (eg: domain, hosting, etc) which is the basis for all
* other logic of these types.
*
@ -232,7 +237,7 @@ class Product extends Model implements IDs
*/
public function getCategoryNameAttribute(): string
{
return $this->supplied->getCategoryNameAttribute();
return $this->supplied->category_name;
}
/**
@ -242,7 +247,7 @@ class Product extends Model implements IDs
*/
public function getContractTermAttribute(): int
{
return $this->type->getContractTermAttribute();
return $this->type->contract_term;
}
/**
@ -252,11 +257,11 @@ class Product extends Model implements IDs
*/
public function getDescriptionAttribute(): string
{
return (($x=$this->translate) && $x->description) ? $x->description : 'No Description';
return $this->translate->description;
}
/**
* Get the minimum cost of this product
* Get the minimum charge for this product
*
* @param int|null $timeperiod
* @param Group|null $go
@ -264,7 +269,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_term($this->getContractTermAttribute(),$this->getBillingIntervalAttribute());
return $this->getSetupChargeAttribute($timeperiod,$go)+$this->getBaseChargeAttribute($timeperiod,$go)*Invoice::billing_term($this->type->contract_term,$this->getBillingIntervalAttribute());
}
/**
@ -274,6 +279,7 @@ class Product extends Model implements IDs
* @param Group|null $go
* @param Collection|NULL $taxes
* @return float
* @deprecated move to account::tax_calc
*/
public function getMinChargeTaxableAttribute(int $timeperiod=NULL,Group $go=NULL,Collection $taxes=NULL): float
{
@ -297,7 +303,7 @@ class Product extends Model implements IDs
*/
public function getNameDetailAttribute(): string
{
return $this->translate ? $this->translate->name_detail : 'Unknown Name';
return $this->translate->name_detail;
}
/**
@ -307,30 +313,29 @@ class Product extends Model implements IDs
*/
public function getNameShortAttribute(): string
{
return $this->translate ? $this->translate->name_short : 'Unknown PID';
}
/**
* Suppliers
*
* @return Model|null
*/
public function getSupplierAttribute(): ?Model
{
return $this->getSuppliedAttribute() ? $this->getSuppliedAttribute()->supplier_detail->supplier : NULL;
return $this->translate->name_short;
}
/**
* Suppliers product
*
* @return Model
* @todo make internal as other values off this attribute are used, not the model itself
*/
public function getSuppliedAttribute(): Model
{
return $this->type->supplied;
}
/**
* Suppliers of this product
*
* @return Model|null
*/
public function getSupplierAttribute(): ?Model
{
return $this->supplied->supplier;
}
/**
* The charge to setup this service
*
@ -340,7 +345,7 @@ class Product extends Model implements IDs
*/
public function getSetupChargeAttribute(int $timeperiod=NULL,Group $go=NULL): float
{
return $this->getCharge('setup',$timeperiod,$go);
return $this->_charge('setup',$timeperiod,$go);
}
/**
@ -350,6 +355,7 @@ class Product extends Model implements IDs
* @param Group|null $go
* @param Collection|null $taxes
* @return float
* @deprecated move to account::tax_calc
*/
public function getSetupChargeTaxableAttribute(int $timeperiod=NULL,Group $go=NULL,Collection $taxes=NULL): float
{
@ -357,13 +363,13 @@ class Product extends Model implements IDs
}
/**
* The charge to setup this service
* The cost to setup this service
*
* @return float
*/
public function getSetupCostAttribute(): float
{
return $this->getSuppliedAttribute()->setup_cost ?: 0;
return $this->supplied->setup_cost ?: 0;
}
/**
@ -371,6 +377,7 @@ class Product extends Model implements IDs
*
* @param Collection|null $taxes
* @return float
* @deprecated move to account::tax_calc
*/
public function getSetupCostTaxableAttribute(Collection $taxes=NULL): float
{
@ -379,6 +386,48 @@ class Product extends Model implements IDs
/* METHODS */
/**
* Get a charge value from the pricing array
*
* This will traverse the groups to the parent group to find an appropriate charge, if one is not found,
* it will extrapolate a charge from the parent group, from another time period.
*
* @param string $type
* @param int|NULL $timeperiod
* @param Group|NULL $go
* @return float
*/
private function _charge(string $type,int $timeperiod=NULL,Group $go=NULL): float
{
// We'll cache this for performance
static $default = NULL;
// All public users
if (is_null($default))
$default = Group::whereNull('parent_id')->firstOrFail();
if (! $go)
$go = $default;
if (is_null($timeperiod))
$timeperiod = $this->getBillingIntervalAttribute();
// If the price doesnt exist for $go->id, use $go->id = 0 which is all users.
if (! $price=$this->charge($timeperiod,$go,$type)) {
$alt_tp = $timeperiod;
// Traverse up our timeperiods if the default group to see if there is another time period that has the value
while (is_null($price=$this->charge($alt_tp,$default,$type)) && ($alt_tp >= 0))
$alt_tp--;
// If we havent got a price, we'll extrapolate one, except for setup charges
if (! is_null($price) && ($alt_tp !== $timeperiod) && ($type !== 'setup'))
$price = $price*Invoice::billing_change($alt_tp,$timeperiod);
}
return round($price,2);
}
/**
* Return the charge from the pricing table for the specific time period and group
*
@ -408,50 +457,13 @@ class Product extends Model implements IDs
*
* @note: By definition products are normalised, as their cost price is based on the default billing interval
* @return float
* @todo Move tax calculations out
*/
public function cost_normalized(): float
{
return number_format(Tax::tax_calc($this->supplied->base_cost,config('site')->taxes),2);
}
/**
* Get a charge value from the pricing array
*
* @param string $type
* @param int|NULL $timeperiod
* @param Group|NULL $go
* @return float
* @todo use self::charge()
*/
private function getCharge(string $type,int $timeperiod=NULL,Group $go=NULL): float
{
static $default = NULL;
if (! $go) {
if (is_null($default))
$default = Group::whereNull('parent_id')->firstOrFail(); // All public users
$go = $default;
}
if (is_null($timeperiod))
$timeperiod = $this->getBillingIntervalAttribute();
// If the price doesnt exist for $go->id, use $go->id = 0 which is all users.
if (! $price=Arr::get($this->pricing,sprintf('%d.%d.%s',$timeperiod,$go->id,$type))) {
$alt_tp = $timeperiod;
while (is_null($price=Arr::get($this->pricing,sprintf('%d.%d.%s',$alt_tp,0,$type))) && ($alt_tp >= 0)) {
$alt_tp--;
}
// If we havent got a price, we'll extrapolate one, except for setup charges
if (! is_null($price) && ($alt_tp !== $timeperiod) && ($type !== 'setup'))
$price = $price*Invoice::billing_change($alt_tp,$timeperiod);
}
return round($price,2);
}
/**
* Return if this product captures usage data
*
@ -466,7 +478,7 @@ class Product extends Model implements IDs
* When receiving an order, validate that we have all the required information for the product type
*
* @param Request $request
* @return mixed
* @return Model|null
*/
public function orderValidation(Request $request): ?Model
{

View File

@ -37,31 +37,6 @@ abstract class Type extends Model
return $this->hasOne(static::SupplierModel,'id','supplier_item_id');
}
/**
* This will return the category of the product (eg: domain, hosting, etc) which is the basis for all
* other logic of these types.
*
* @return string
* @deprecated - can this be replaced with product->supplied->category?
*/
final public function getCategoryAttribute(): string
{
abort(500,'use product->supplied->category_name');
return strtolower((new \ReflectionClass($this))->getShortName());
}
/**
* Return a friendly name for this product, used for display
*
* @return string
* @deprecated - can this be replaced with product->supplied->category_name
*/
final public function getCategoryNameAttribute(): string
{
abort(500,'use product->supplied->category_name');
return static::category_name;
}
/* METHODs */
final function normalizeBillingInterval(): int

View File

@ -9,4 +9,9 @@ class ProductTranslate extends Model
protected $table = 'product_translate';
public $timestamps = FALSE;
public function getDescriptionAttribute(string $val): string
{
return $val ?: 'No Description';
}
}

View File

@ -84,4 +84,9 @@ abstract class Type extends Model
{
return Tax::tax_calc($this->attributes['setup_cost'],config('site')->taxes);
}
public function getSupplierAttribute(): Model
{
return $this->supplier_detail->supplier;
}
}

View File

@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::statement('ALTER TABLE supplier_broadband MODIFY product_id varchar(32) NOT NULL, MODIFY product_desc text DEFAULT NULL');
DB::statement('ALTER TABLE supplier_domain MODIFY product_id varchar(32) NOT NULL, MODIFY product_desc text DEFAULT NULL');
DB::statement('ALTER TABLE supplier_email MODIFY product_id varchar(32) NOT NULL, MODIFY product_desc text DEFAULT NULL');
DB::statement('ALTER TABLE supplier_generic MODIFY product_id varchar(32) NOT NULL, MODIFY product_desc text DEFAULT NULL');
DB::statement('ALTER TABLE supplier_phone MODIFY product_id varchar(32) NOT NULL, MODIFY product_desc text DEFAULT NULL');
DB::statement('ALTER TABLE supplier_ssl MODIFY product_id varchar(32) NOT NULL, MODIFY product_desc text DEFAULT NULL');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
abort(500,'Cant go back');
}
};

View File

@ -9,10 +9,10 @@ Please order the following...
| Service ID | {{ $service->sid }} |
| Product | {{ $service->product->name }} |
@switch($service->category)
@case('Broadband')
@case('broadband')
| Address | {{ $service->type->service_address }} |
@break;
@case('Phone')
@case('telephone')
| Number | {{ $service->type->service_number }} |
| Supplier Details | {{ $service->order_info->join(':') }} |
@break;

View File

@ -10,10 +10,10 @@
| Service ID | {{ $service->sid }} |
| Product | {{ $service->product->name }} |
@switch($service->category)
@case('BROADBAND')
@case('broadband')
| Address | {{ is_object($service->type) ? $service->type->service_address : 'Not Supplied' }} |
@break;
@case('PHONE')
@case('telephone')
| Address | {{ is_object($service->type) ? $service->type->service_address : 'Not Supplied' }} |
| Supplier Details | {{ join(':',$service->order_info) }} |
@break;

View File

@ -12,7 +12,7 @@ Please cancel the following...
@case('broadband')
| Address | {{ $service->type->service_address }} |
@break;
@case('phone')
@case('telephone')
| Number | {{ $service->type->service_number }} |
| Supplier Details | {{ $service->order_info->join(':') }} |
@break;

View File

@ -9,10 +9,10 @@ Please change the following...
| Service ID | {{ $service->sid }} |
| Product | {{ $service->product->name }} |
@switch($service->category)
@case('Broadband')
@case('broadband')
| Address | {{ $service->type->service_address }} |
@break;
@case('Phone')
@case('telephone')
| Number | {{ $service->type->service_number }} |
| Supplier Details | {{ $service->order_info->join(':') }} |
@break;

View File

@ -4,7 +4,7 @@
<tr>
<th>&nbsp;</th>
@if (($s=$o->supplied) && $s->exists)
<th>{{ $s->supplier_detail->supplier->name }}</th>
<th>{{ $s->supplier->name }}</th>
@endif
<th>Us</th>
@if ($s->exists)