2021-12-24 01:14:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models\Product;
|
|
|
|
|
|
|
|
use Illuminate\Support\Collection;
|
2022-04-02 07:06:34 +00:00
|
|
|
use Leenooks\Traits\ScopeActive;
|
2021-12-24 01:14:01 +00:00
|
|
|
|
2022-02-01 05:40:46 +00:00
|
|
|
use App\Interfaces\ProductItem;
|
2021-12-24 01:14:01 +00:00
|
|
|
use App\Models\Service\Broadband as ServiceBroadband;
|
|
|
|
use App\Models\Supplier\Broadband as SupplierBroadband;
|
|
|
|
|
2022-02-01 05:40:46 +00:00
|
|
|
final class Broadband extends Type implements ProductItem
|
2021-12-24 01:14:01 +00:00
|
|
|
{
|
2022-04-02 07:06:34 +00:00
|
|
|
use ScopeActive;
|
|
|
|
|
2021-12-24 01:14:01 +00:00
|
|
|
protected $table = 'product_broadband';
|
|
|
|
|
|
|
|
// Information required during the order process
|
2022-04-04 10:29:45 +00:00
|
|
|
protected array $order_attributes = [
|
2021-12-24 01:14:01 +00:00
|
|
|
'options.address'=>[
|
|
|
|
'request'=>'options.address',
|
|
|
|
'key'=>'service_address',
|
2022-04-20 06:24:58 +00:00
|
|
|
'validation'=>'required|string:10|unique:service_broadband,service_address',
|
2021-12-24 01:14:01 +00:00
|
|
|
'validation_message'=>'Address is a required field.',
|
|
|
|
],
|
|
|
|
'options.notes'=>[
|
|
|
|
'request'=>'options.notes',
|
|
|
|
'key'=>'order_info.notes',
|
|
|
|
'validation'=>'present',
|
|
|
|
'validation_message'=>'Special Instructions here.',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2022-02-01 05:40:46 +00:00
|
|
|
// The model that is referenced when this product is ordered
|
2021-12-24 01:14:01 +00:00
|
|
|
protected string $order_model = ServiceBroadband::class;
|
|
|
|
|
2023-05-04 00:02:25 +00:00
|
|
|
// The model that the supplier supplies
|
|
|
|
const SupplierModel = SupplierBroadband::class;
|
2021-12-24 01:14:01 +00:00
|
|
|
|
|
|
|
/* INTERFACES */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the allowance array or traffic used array
|
|
|
|
*
|
|
|
|
* @param array Traffic Used in each metric.
|
|
|
|
* @param bool $ceil Round the numbers to integers
|
|
|
|
* @return array|string
|
|
|
|
*/
|
|
|
|
public function allowance(array $data=[],bool $ceil=TRUE): Collection
|
|
|
|
{
|
|
|
|
$config = collect();
|
|
|
|
|
2022-04-22 01:47:46 +00:00
|
|
|
foreach (array_keys(SupplierBroadband::traffic_map) as $k => $v) {
|
2021-12-24 01:14:01 +00:00
|
|
|
// Base Config
|
|
|
|
$config->put($k,$this->{$k});
|
|
|
|
// Excess Config
|
|
|
|
$config->put($v,$this->{$v});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shaped or Charge
|
|
|
|
$config->put('shaped',$this->extra_shaped);
|
|
|
|
$config->put('charged',$this->extra_charged);
|
|
|
|
|
|
|
|
// Metric - used to round down data in $data.
|
|
|
|
$config->put('metric',$this->metric);
|
|
|
|
|
|
|
|
return $this->supplied->allowance($config,$data,$ceil);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ATTRIBUTES */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the suppliers cost for this service
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
// @todo To check
|
|
|
|
public function allowance_cost(): float
|
|
|
|
{
|
|
|
|
$result = 0;
|
|
|
|
foreach ($this->supplied->allowance(NULL,$this->allowance([])->toArray()) as $k=>$v) {
|
2022-04-22 01:47:46 +00:00
|
|
|
$result += -$v*$this->supplied->{SupplierBroadband::traffic_map[$k]};
|
2021-12-24 01:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the allowance as a string
|
|
|
|
* eg: 50/100
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-02-01 05:40:46 +00:00
|
|
|
// @todo To check
|
2021-12-24 01:14:01 +00:00
|
|
|
public function allowance_string(): string
|
|
|
|
{
|
|
|
|
$result = '';
|
|
|
|
$data = $this->allowance();
|
|
|
|
|
|
|
|
foreach ([
|
|
|
|
'base_down_peak',
|
|
|
|
'base_up_peak',
|
|
|
|
'base_down_offpeak',
|
|
|
|
'base_up_offpeak',
|
|
|
|
] as $k)
|
|
|
|
{
|
|
|
|
if ($data->has($k)) {
|
|
|
|
if ($result)
|
|
|
|
$result .= '/';
|
|
|
|
|
|
|
|
$result .= $data->get($k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The product contract term is the highest of
|
|
|
|
* + This defined contract_term
|
|
|
|
* + The suppliers contract_term
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getContractTermAttribute(): int
|
|
|
|
{
|
|
|
|
return max($this->attributes['contract_term'],$this->supplied->getContractTermAttribute());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCostAttribute(): float
|
|
|
|
{
|
|
|
|
abort(500,'deprecated');
|
|
|
|
// @todo Tax shouldnt be hard coded
|
|
|
|
return ($this->supplied->base_cost+$this->allowance_cost())*1.1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSupplierAttribute()
|
|
|
|
{
|
|
|
|
abort(500,'deprecated');
|
|
|
|
return $this->getRelationValue('supplier');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasUsage(): bool
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|