Work on products, first completed broadband

This commit is contained in:
Deon George
2021-12-24 12:14:01 +11:00
parent 8f5293662e
commit 1e9f15b40f
62 changed files with 2139 additions and 894 deletions

View File

@@ -18,14 +18,15 @@ 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:
* + billing_period : The period that this service is billed for by default
* + additional_cost : Pending additional charges for this service (excluding setup)
* + billing_cost : Charge for this service each invoice period
* + billing_interval : The period that this service is billed for by default
* + 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
@@ -33,9 +34,10 @@ use App\Traits\NextKey;
*
* @package App\Models
*/
// @todo All the methods/attributes in this file need to be checked.
class Service extends Model implements IDs
{
use NextKey,HasFactory;
use HasFactory;
const RECORD_ID = 'service';
public $incrementing = FALSE;
@@ -85,6 +87,7 @@ class Service extends Model implements IDs
'status',
];
/*
protected $with = [
'account.language',
'charges',
@@ -92,6 +95,7 @@ class Service extends Model implements IDs
'product',
'type',
];
*/
// @todo Change to self::INACTIVE_STATUS
private $inactive_status = [
@@ -370,7 +374,6 @@ class Service extends Model implements IDs
* Product of the service
*
* @return BelongsTo
* @deprecated use type->product
*/
public function product()
{
@@ -491,10 +494,10 @@ class Service extends Model implements IDs
public function getBillingPriceAttribute(): float
{
// @todo Temporary for services that dont have recur_schedule set.
if (is_null($this->recur_schedule) OR is_null($this->product->price($this->recur_schedule)))
if (is_null($this->recur_schedule) OR is_null($this->product->getBaseChargeAttribute($this->recur_schedule,$this->account->group)))
$this->price=0;
return $this->addTax(is_null($this->price) ? $this->product->price($this->recur_schedule) : $this->price);
return $this->addTax(is_null($this->price) ? $this->product->getBaseChargeAttribute($this->recur_schedule,$this->account->group) : $this->price);
}
public function getBillingMonthlyPriceAttribute(): float
@@ -516,11 +519,32 @@ class Service extends Model implements IDs
/**
* Return the service billing period
*
* @return int
*/
public function getBillingIntervalAttribute(): int
{
return $this->recur_schedule ?: $this->product->getBillingIntervalAttribute();
}
/**
* Return a human friendly name for the billing interval
*
* @return string
*/
public function getBillingPeriodAttribute(): string
public function getBillingIntervalStringAttribute(): string
{
return Arr::get($this->product->PricePeriods(),$this->recur_schedule,'Unknown');
return Invoice::billing_name($this->getBillingIntervalAttribute());
}
/**
* 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();
*
* @return int
*/
public function getContractTermAttribute(): int
{
abort(500,'To implement (Dec 2021)');
}
/**
@@ -561,42 +585,42 @@ class Service extends Model implements IDs
{
switch ($this->recur_schedule) {
// Weekly
case 0: $date = $this->product->price_recurr_strict
case 0: $date = $this->product->price_recur_strict
? $this->getInvoiceNextAttribute()->endOfWeek()
: $this->getInvoiceNextAttribute()->addWeek()->subDay();
break;
// Monthly
case 1:
$date = $this->product->price_recurr_strict
$date = $this->product->price_recur_strict
? $this->getInvoiceNextAttribute()->endOfMonth()
: $this->getInvoiceNextAttribute()->addMonth()->subDay();
break;
// Quarterly
case 2:
$date = $this->product->price_recurr_strict
$date = $this->product->price_recur_strict
? $this->getInvoiceNextAttribute()->endOfQuarter()
: $this->getInvoiceNextAttribute()->addQuarter()->subDay();
break;
// Half Yearly
case 3:
$date = $this->product->price_recurr_strict
$date = $this->product->price_recur_strict
? $this->getInvoiceNextAttribute()->endOfHalf()
: $this->getInvoiceNextAttribute()->addQuarter(2)->subDay();
break;
// Yearly
case 4:
$date = $this->product->price_recurr_strict
$date = $this->product->price_recur_strict
? $this->getInvoiceNextAttribute()->endOfYear()
: $this->getInvoiceNextAttribute()->addYear()->subDay();
break;
// Two Yearly
case 5:
if (!$this->product->price_recurr_strict)
if (!$this->product->price_recur_strict)
$date = $this->getInvoiceNextAttribute()->addYear(2)->subDay();
else {
$date = $this->getInvoiceNextAttribute()->addYear(2)->subDay()->endOfYear();
@@ -608,7 +632,7 @@ class Service extends Model implements IDs
break;
// Three Yearly
// NOTE: price_recurr_strict ignored
// NOTE: price_recur_strict ignored
case 6: $date = $this->getInvoiceNextAttribute()->addYear(3)->subDay(); break;
default: throw new Exception('Unknown recur_schedule');
@@ -624,7 +648,7 @@ class Service extends Model implements IDs
public function getInvoiceNextQuantityAttribute()
{
// If we are not rounding to the first day of the cycle, then it is always a full cycle
if (! $this->product->price_recurr_strict)
if (! $this->product->price_recur_strict)
return 1;
$n = $this->invoice_next->diff($this->invoice_next_end)->days+1;
@@ -759,11 +783,12 @@ class Service extends Model implements IDs
/**
* Get the Product's Category for this service
*
* @deprecated use product->category directly
* @deprecated use product->getProductTypeAttribute() directly
*/
public function getProductCategoryAttribute(): string
{
return $this->product->category;
abort(500,'deprecated');
return $this->product->getProductTypeAttribute();
}
/**
@@ -774,6 +799,7 @@ class Service extends Model implements IDs
*/
public function getProductNameAttribute(): string
{
abort(500,'deprecated');
return $this->product->name($this->account->language);
}
@@ -860,7 +886,7 @@ class Service extends Model implements IDs
public function getSTypeAttribute(): string
{
switch($this->product->model) {
case 'App\Models\Product\Adsl': return 'broadband';
case 'App\Models\Product\Broadband': return 'broadband';
default: return $this->type->type;
}
}
@@ -918,6 +944,18 @@ class Service extends Model implements IDs
: $this->status;
}
/**
* Return the type of service is provided.
*
* @return string
*/
public function getServiceTypeAttribute(): string
{
// @todo This is temporary, while we clean the database.
return ($this->product->type && $this->product->type->supplied) ? $this->product->type->supplied->getTypeAttribute() : '** TBA **';
}
/**
* URL used by an admin to administer the record
*
@@ -1275,7 +1313,7 @@ class Service extends Model implements IDs
// Connection charges are only charged once
if ((! $this->invoice_items->filter(function($item) { return $item->item_type==4; })->sum('total'))
AND ($this->isPending() OR is_null($this->invoice_to))
AND $this->product->price($this->recur_schedule,'price_setup'))
AND $this->product->getSetupChargeAttribute($this->recur_schedule,$this->account->group))
{
$o = new InvoiceItem;
@@ -1283,7 +1321,7 @@ class Service extends Model implements IDs
$o->service_id = $this->id;
$o->product_id = $this->product_id;
$o->item_type = 4; // @todo change to const or something
$o->price_base = $this->product->price($this->recur_schedule,'price_setup'); // @todo change to a method in this class
$o->price_base = $this->product->getSetupChargeAttribute($this->recur_schedule,$this->account->group);
//$o->recurring_schedule = $this->recur_schedule;
$o->date_start = $this->invoice_next;
$o->date_stop = $this->invoice_next;
@@ -1309,7 +1347,7 @@ class Service extends Model implements IDs
$o->product_id = $this->product_id;
$o->item_type = 0;
$o->price_base = is_null($this->price)
? (is_null($this->price_override) ? $this->product->price($this->recur_schedule) : $this->price_override)
? (is_null($this->price_override) ? $this->product->getBaseChargeAttribute($this->recur_schedule,$this->account->group) : $this->price_override)
: $this->price; // @todo change to a method in this class
$o->recurring_schedule = $this->recur_schedule;
$o->date_start = $this->invoice_next;
@@ -1354,6 +1392,7 @@ class Service extends Model implements IDs
*/
private function ServicePlugin()
{
abort(500,'deprecated');
// @todo: All services should be linked to a product. This might require data cleaning for old services not linked to a product.
if (! is_object($this->product))
return NULL;