Start work on updating services

This commit is contained in:
Deon George
2022-04-19 17:07:39 +10:00
parent ebf08ea414
commit 621a132e35
63 changed files with 1038 additions and 612 deletions

View File

@@ -18,35 +18,35 @@ use Leenooks\Carbon;
use Symfony\Component\HttpKernel\Exception\HttpException;
use App\Interfaces\IDs;
use App\Traits\NextKey;
/**
* Class Service
* Services that belong to an account
*
* Attributes for services:
* + additional_cost : Pending additional charges for this service (excluding setup)
* + additional_cost : Pending additional charges for this service (excluding setup) //@todo check all these are still valid
* + billing_cost : Charge for this service each invoice period
* + billing_interval : The period that this service is billed for by default
* + billing_interval_string : The period that this service is billed for by default as a name
* + contract_term : The term that this service must be active
* + contract_end : The date that the contract ends for this service
* + name : Service short name with service address
* + name_short : Service Product short name, eg: phone number, domain name, certificate CN
* + name_detail : Service Detail, eg: service_address
* + sid : System ID for service
*
* = Terminology:
* - Offering, what product we supply (we make offerings from supplier's supplied products) - in the DB these are products/*
* - Supplied, our supplier's product that is providing the service - in the DB these are supplier/*
* - Type, what service we are providing, made up of a product we supply - in the DB these are service/*
*
* @package App\Models
* @todo "Billing Start Date" = "connection date" for sub types??
*/
// @todo All the methods/attributes in this file need to be checked.
class Service extends Model implements IDs
{
use HasFactory,NextKey;
const RECORD_ID = 'service';
public $incrementing = FALSE;
protected $table = 'ab_service';
const CREATED_AT = 'date_orig';
const UPDATED_AT = 'date_last';
use HasFactory;
protected $appends = [
'account_name',
@@ -59,17 +59,16 @@ class Service extends Model implements IDs
'status',
];
protected $dates = [
'date_last_invoice',
'date_next_invoice',
'date_start',
'date_end',
];
protected $casts = [
'order_info'=>AsCollection::class,
];
public $dateFormat = 'U';
protected $dates = [
'invoice_last_at',
'invoice_next_at',
'start_at',
'stop_at',
];
protected $visible = [
'account_name',
@@ -484,7 +483,6 @@ class Service extends Model implements IDs
* Return the auto billing details
*
* @return mixed
* @deprecated use billing directly?
*/
public function getAutoPayAttribute()
{
@@ -536,6 +534,33 @@ class Service extends Model implements IDs
return Invoice::billing_name($this->getBillingIntervalAttribute());
}
/**
* The date the contract ends
*
* Service contracts end the later of the start_date + contract_term or the expire date.
*
* @return Carbon
*/
public function getContractEndAttribute(): ?\Carbon\Carbon
{
// If we have no start date or expire date, then NULL;
if (! $this->start_at && ! $this->type->expire_at)
return NULL;
// If we dont have a start date, use the expire date
if (! $this->start_at)
return $this->type->expire_at;
$end = $this->start_at->addMonths($this->getContractTermAttribute());
// If we dont have an expire date, use the start date + contract_term
if (! $this->type->expire_at)
return $end;
// We have both, so it's the later of the two.
return ($end < $this->type->expire_at) ? $this->type->expire_at : $end;
}
/**
* This function will determine the minimum contract term for a service, which is the maximum of
* this::type->contract_term, or the product->type->contract_term();
@@ -544,7 +569,7 @@ class Service extends Model implements IDs
*/
public function getContractTermAttribute(): int
{
abort(500,'To implement (Dec 2021)');
return $this->product->type->supplied->contract_term ?: 0;
}
/**
@@ -570,7 +595,7 @@ class Service extends Model implements IDs
$date = $last
? $last->addDay()
: ($this->date_next_invoice ? $this->date_next_invoice->clone()
: ($this->date_start ?: Carbon::now()));
: ($this->start_at ?: Carbon::now()));
return request()->wantsJson() ? $date->format('Y-m-d') : $date;
}
@@ -705,8 +730,9 @@ class Service extends Model implements IDs
: NULL;
// For SSL Certificates, the invoice_to date is the expiry date of the Cert
if (is_null($result) AND $this->type AND $this->type->type == 'ssl' AND $this->type->valid_to)
return $this->type->valid_to;
// @todo can we use the expire_at attribute?
if (is_null($result) AND $this->type AND $this->type->type == 'ssl' AND $this->type->expire_at)
return $this->type->expire_at;
return $result;
}
@@ -751,6 +777,17 @@ class Service extends Model implements IDs
return $this->getInvoiceNextAttribute();
}
/**
* The product we supply for this service
*
* @return Model
* @todo Remove all references to product->type to use this method
*/
public function getOfferingAttribute(): Model
{
return $this->product->type;
}
public function getOrderInfoNotesAttribute(): ?string
{
return $this->getOrderInfoValue('notes');
@@ -954,6 +991,16 @@ class Service extends Model implements IDs
return ($this->product->type && $this->product->type->supplied) ? $this->product->type->supplied->getTypeAttribute() : '** TBA **';
}
/**
* Get the service providers offering that we are providing for this service
*
* @return Model|null
*/
public function getSupplierProductAttribute(): ?Model
{
dd($this->product,$this->product->type,$this->product->type->supplied);
}
/**
* URL used by an admin to administer the record
*
@@ -1397,11 +1444,11 @@ class Service extends Model implements IDs
switch ($this->product->prod_plugin_file)
{
case 'ADSL': return $this->service_adsl;
case 'BROADBAND': return $this->service_broadband;
case 'DOMAIN': return $this->service_domain;
case 'HOST': return $this->service_host;
case 'SSL': return $this->service_ssl;
case 'VOIP': return $this->service_voip;
case 'PHONE': return $this->service_voip;
default: return NULL;
}