More Product Model optimisation

This commit is contained in:
2023-05-05 15:48:24 +10:00
parent 96f799f535
commit 820ff2be00
37 changed files with 161 additions and 592 deletions

View File

@@ -5,20 +5,22 @@ namespace App\Models\Supplier;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use App\Interfaces\SupplierItem;
use App\Models\Product\Broadband as ProductBroadband;
class Broadband extends Type implements SupplierItem
class Broadband extends Type
{
protected const category_name = 'Broadband';
protected $table = 'supplier_broadband';
// The model of the product that is supplied by this model
const ProductModel = ProductBroadband::class;
protected $casts = [
'offpeak_start' => 'datetime:H:i',
'offpeak_end' => 'datetime:H:i',
];
protected $table = 'supplier_broadband';
// Map the table fields, with the extra fields
public const traffic_map = [
'base_up_offpeak' => 'extra_up_offpeak',
@@ -35,22 +37,6 @@ class Broadband extends Type implements SupplierItem
'extra_down_peak' => 'base_down_peak',
];
/* INTERFACES */
/**
* @return int
* @deprecated use Product::normalizeBillingInterval()
*/
public function getBillingIntervalAttribute(): int
{
return 1; // Monthly
}
public function products()
{
return $this->hasMany(ProductBroadband::class,'supplier_item_id','id');
}
/* METHODS */
/**

View File

@@ -4,33 +4,25 @@ namespace App\Models\Supplier;
use Illuminate\Support\Collection;
use App\Interfaces\SupplierItem;
use App\Models\Product\Domain as ProductDomain;
use App\Models\TLD;
final class Domain extends Type implements SupplierItem
final class Domain extends Type
{
protected const category_name = 'Domain Name';
protected const category_name = 'Domain';
protected $table = 'supplier_domain';
/* INTERFACES */
// The model of the product that is supplied by this model
const ProductModel = ProductDomain::class;
public function getBillingIntervalAttribute(): int
{
return 4; // Yearly
}
/* INTERFACES */
public function getNameAttribute(): string
{
return sprintf('%s: %s',$this->product_id,$this->tld->name);
}
public function products()
{
return $this->hasMany(ProductDomain::class,'supplier_item_id','id');
}
/* STATIC */
/**

View File

@@ -2,24 +2,14 @@
namespace App\Models\Supplier;
use App\Interfaces\SupplierItem;
use App\Models\Product\Email as ProductEmail;
final class Email extends Type implements SupplierItem
final class Email extends Type
{
protected const category_name = 'Email Hosting';
protected const category_name = 'Email';
protected $table = 'supplier_email';
/* INTERFACES */
public function getBillingIntervalAttribute(): int
{
return 4; // Yearly
}
public function products()
{
return $this->hasMany(ProductEmail::class,'supplier_item_id','id');
}
// The model of the product that is supplied by this model
const ProductModel = ProductEmail::class;
}

View File

@@ -4,5 +4,5 @@ namespace App\Models\Supplier;
class Ethernet extends Broadband
{
protected const category_name = 'Broadband Ethernet';
protected const category_name = 'Ethernet';
}

View File

@@ -2,24 +2,14 @@
namespace App\Models\Supplier;
use App\Interfaces\SupplierItem;
use App\Models\Product\Generic as ProductGeneric;
final class Generic extends Type implements SupplierItem
final class Generic extends Type
{
protected const category_name = 'Generic';
protected $table = 'supplier_generic';
/* INTERFACES */
public function getBillingIntervalAttribute(): int
{
return 1; // Monthly
}
public function products()
{
return $this->hasMany(ProductGeneric::class,'supplier_item_id','id');
}
// The model of the product that is supplied by this model
const ProductModel = ProductGeneric::class;
}

View File

@@ -2,24 +2,14 @@
namespace App\Models\Supplier;
use App\Interfaces\SupplierItem;
use App\Models\Product\Host as ProductHost;
final class Host extends Type implements SupplierItem
final class Host extends Type
{
protected const category_name = 'Web Hosting';
protected const category_name = 'Hosting';
protected $table = 'supplier_host';
/* INTERFACES */
public function getBillingIntervalAttribute(): int
{
return 4; // Yearly
}
public function products()
{
return $this->hasMany(ProductHost::class,'supplier_item_id','id');
}
// The model of the product that is supplied by this model
const ProductModel = ProductHost::class;
}

View File

@@ -2,24 +2,14 @@
namespace App\Models\Supplier;
use App\Interfaces\SupplierItem;
use App\Models\Product\Phone as ProductVoip;
final class Phone extends Type implements SupplierItem
final class Phone extends Type
{
protected const category_name = 'Telephone';
protected const category_name = 'Phone';
protected $table = 'supplier_phone';
/* INTERFACES */
public function getBillingIntervalAttribute(): int
{
return 1; // Monthly
}
public function products()
{
return $this->hasMany(ProductVoip::class,'supplier_item_id','id');
}
// The model of the product that is supplied by this model
const ProductModel = ProductVoip::class;
}

View File

@@ -2,24 +2,14 @@
namespace App\Models\Supplier;
use App\Interfaces\SupplierItem;
use App\Models\Product\SSL as ProductSSL;
final class SSL extends Type implements SupplierItem
final class SSL extends Type
{
protected const category_name = 'SSL Certificate';
protected const category_name = 'SSL';
protected $table = 'supplier_ssl';
/* INTERFACES */
public function getBillingIntervalAttribute(): int
{
return 4; // Yearly
}
public function products()
{
return $this->belongsToMany(ProductSSL::class,'supplier_item_id','id');
}
// The model of the product that is supplied by this model
const ProductModel = ProductSSL::class;
}

View File

@@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Leenooks\Traits\ScopeActive;
use App\Models\{Invoice,Supplier,SupplierDetail,Tax};
use App\Models\{Invoice,SupplierDetail};
use App\Traits\{ProductDetails,SiteID};
abstract class Type extends Model
@@ -15,16 +15,26 @@ abstract class Type extends Model
/* RELATIONS */
public function supplier_detail()
/**
* The offering supplied with this product
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
final public function products()
{
return $this->hasMany(static::ProductModel,'supplier_item_id','id');
}
final public function supplier_detail()
{
return $this->belongsTo(SupplierDetail::class);
}
/* ATTRIBUTES */
public function getBaseCostTaxableAttribute(): float
public function getBaseCostAttribute(?float $val): float
{
return Tax::tax_calc($this->attributes['base_cost'],config('site')->taxes);
return $val ?: 0;
}
/**
@@ -57,7 +67,7 @@ abstract class Type extends Model
*/
public function getContractTermAttribute(): int
{
return max(Invoice::billing_period(static::getBillingIntervalAttribute()),Arr::get($this->attributes,'contract_term'));
return max(Invoice::billing_period(static::getBillingIntervalAttribute()),Arr::get($this->attributes,'contract_term',0));
}
public function getMinCostAttribute(): float
@@ -65,11 +75,6 @@ abstract class Type extends Model
return $this->attributes['setup_cost']+$this->attributes['base_cost']*Invoice::billing_term($this->getContractTermAttribute(),$this->getBillingIntervalAttribute());
}
public function getMinCostTaxableAttribute(): float
{
return Tax::tax_calc($this->getMinCostAttribute(),config('site')->taxes);
}
public function getNameAttribute(): string
{
return $this->product_id ?: 'Supplier PID Unknown';
@@ -80,9 +85,9 @@ abstract class Type extends Model
return $this->product_desc ?: 'Supplier NAME Unknown';
}
public function getSetupCostTaxableAttribute(): float
public function getSetupCostAttribute(?float $val): float
{
return Tax::tax_calc($this->attributes['setup_cost'],config('site')->taxes);
return $val ?: 0;
}
public function getSupplierAttribute(): Model