97 lines
2.1 KiB
PHP
97 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Supplier;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Arr;
|
|
use Leenooks\Traits\ScopeActive;
|
|
|
|
use App\Models\{Invoice,SupplierDetail};
|
|
use App\Traits\{ProductDetails,SiteID};
|
|
|
|
abstract class Type extends Model
|
|
{
|
|
use SiteID,ScopeActive,ProductDetails;
|
|
|
|
/* RELATIONS */
|
|
|
|
/**
|
|
* 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 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.
|
|
*
|
|
* @return string
|
|
*/
|
|
final public function getCategoryAttribute(): string
|
|
{
|
|
return strtolower((new \ReflectionClass($this))->getShortName());
|
|
}
|
|
|
|
/**
|
|
* Return a friendly name for this product, used for display
|
|
*
|
|
* @return string
|
|
*/
|
|
final public function getCategoryNameAttribute(): string
|
|
{
|
|
return static::category_name;
|
|
}
|
|
|
|
/**
|
|
* This contract term is the highest of
|
|
* + The defined contract_term
|
|
* + The default months in a billing interval
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getContractTermAttribute(): int
|
|
{
|
|
return max(Invoice::billing_period(static::getBillingIntervalAttribute()),Arr::get($this->attributes,'contract_term',0));
|
|
}
|
|
|
|
public function getMinCostAttribute(): float
|
|
{
|
|
return $this->attributes['setup_cost']+$this->attributes['base_cost']*Invoice::billing_term($this->getContractTermAttribute(),$this->getBillingIntervalAttribute());
|
|
}
|
|
|
|
public function getNameAttribute(): string
|
|
{
|
|
return $this->product_id ?: 'Supplier PID Unknown';
|
|
}
|
|
|
|
public function getNameLongAttribute(): string
|
|
{
|
|
return $this->product_desc ?: 'Supplier NAME Unknown';
|
|
}
|
|
|
|
public function getSetupCostAttribute(?float $val): float
|
|
{
|
|
return $val ?: 0;
|
|
}
|
|
|
|
public function getSupplierAttribute(): Model
|
|
{
|
|
return $this->supplier_detail->supplier;
|
|
}
|
|
} |