osb/app/Models/Service.php

1385 lines
33 KiB
PHP
Raw Permalink Normal View History

2018-05-20 12:53:14 +00:00
<?php
namespace App\Models;
use Exception;
use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2018-05-20 12:53:14 +00:00
use Illuminate\Database\Eloquent\Model;
2020-02-05 04:47:24 +00:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
2020-04-18 22:33:41 +00:00
use Illuminate\Support\Facades\Auth;
2021-06-29 06:36:34 +00:00
use Leenooks\Carbon;
2020-04-18 22:33:41 +00:00
use Symfony\Component\HttpKernel\Exception\HttpException;
2021-06-29 06:36:34 +00:00
use App\Interfaces\IDs;
2020-04-18 22:33:41 +00:00
use App\Traits\NextKey;
2018-05-20 12:53:14 +00:00
2021-06-29 06:36:34 +00:00
/**
* Class Service
* Services that belong to an account
*
* Attributes for services:
* + billing_period : The period that this service is billed for by default
* + name_short : Service Product short name, eg: phone number, domain name, certificate CN
* + sid : System ID for service
2021-06-29 06:36:34 +00:00
*
* @package App\Models
*/
class Service extends Model implements IDs
2018-05-20 12:53:14 +00:00
{
use NextKey,HasFactory;
2021-06-29 06:36:34 +00:00
const RECORD_ID = 'service';
2020-02-05 04:47:24 +00:00
public $incrementing = FALSE;
2021-06-29 06:36:34 +00:00
protected $table = 'ab_service';
const CREATED_AT = 'date_orig';
const UPDATED_AT = 'date_last';
2018-08-11 05:09:41 +00:00
protected $appends = [
2018-08-11 05:09:41 +00:00
'account_name',
2018-08-23 05:17:26 +00:00
'admin_service_id_url',
'billing_price',
'name_short',
'next_invoice',
'product_category',
'product_name',
'service_id',
'service_id_url',
'status',
];
2018-08-11 05:09:41 +00:00
2020-02-10 11:07:46 +00:00
protected $dates = [
'date_last_invoice',
2020-02-12 10:32:57 +00:00
'date_next_invoice',
2020-02-10 11:07:46 +00:00
'date_start',
'date_end',
];
protected $casts = [
2020-04-18 22:33:41 +00:00
'order_info'=>'collection',
];
2020-02-10 11:07:46 +00:00
public $dateFormat = 'U';
protected $visible = [
2018-08-11 05:09:41 +00:00
'account_name',
2018-08-23 05:17:26 +00:00
'admin_service_id_url',
'active',
'billing_price',
'data_orig',
'id',
'name_short',
'next_invoice',
'product_category',
'product_name',
'service_id',
'service_id_url',
'status',
];
2018-05-20 12:53:14 +00:00
2020-02-10 11:07:46 +00:00
protected $with = [
'account.language',
'charges',
'invoice_items',
'product',
'type',
];
// @todo Change to self::INACTIVE_STATUS
2018-08-11 05:09:41 +00:00
private $inactive_status = [
2019-01-24 10:49:56 +00:00
'CANCELLED',
2018-11-21 02:46:16 +00:00
'ORDER-REJECTED',
2019-01-24 03:40:33 +00:00
'ORDER-CANCELLED',
];
public const INACTIVE_STATUS = [
'CANCELLED',
'ORDER-REJECTED',
'ORDER-CANCELLED',
];
/**
* Valid status shows the applicable next status for an action on a service
* Each status can be
* + Approved, to proceed to the next valid status'
* + Held, to a holding pattern status
* + Rejected, reverted to an different status
* + Cancel, to progress down a decomission route
* + Updated, stay on the current status with new information
*
* @var array
*/
2020-04-18 22:33:41 +00:00
public static $action_progress = [
2019-01-24 10:49:56 +00:00
// Order Submitted
2020-04-18 22:33:41 +00:00
'ORDER-SUBMIT' => [
'fail'=>FALSE,
// Progress to next stages by who
'next'=>[
'ORDER-ACCEPT'=>['customer'],
'SETUP-PAYMENT-WAIT'=>['reseller','wholesaler']
],
// Manual or System moves to the next stage
'system'=>TRUE,
'method'=>'action_order_submit',
'title'=>'Order Submit',
],
// Client accepts order, if performed by RW
'ORDER-ACCEPT' => [
'fail'=>FALSE,
'next'=>[
'SETUP-PAYMENT-WAIT'=>['customer'],
],
'system'=>FALSE,
'method'=>'action_order_accept',
'title'=>'Client Accept Order',
],
// If the product has a setup, collect payment information
'SETUP-PAYMENT-WAIT' => [
'fail'=>FALSE,
'next'=>[
'PAYMENT-WAIT'=>['customer'],
],
'system'=>FALSE,
'method'=>'action_setup_payment_wait',
'title'=>'Setup Payment',
],
'PAYMENT-WAIT' => [
'fail'=>FALSE,
'next'=>[
'PAYMENT-CHECK'=>['reseller','wholesaler'],
],
'system'=>FALSE,
'method'=>'action_payment_wait',
'title'=>'Service Payment',
],
'PAYMENT-CHECK' => [
'fail'=>'ORDER-HOLD',
'next'=>[
'ORDER-SENT'=>[],
],
'system'=>TRUE,
'method'=>'action_payment_check',
'title'=>'Validate Payment Method',
],
2019-01-24 10:49:56 +00:00
// Order On Hold (Reason)
2019-01-24 11:03:43 +00:00
'ORDER-HOLD' => ['release'=>'ORDER-SUBMIT','update_reference'=>'ORDER-SENT'],
2019-01-24 10:49:56 +00:00
// Order Rejected (Reason)
'ORDER-REJECTED' => [],
// Order Cancelled
'ORDER-CANCELLED' => [],
// Order Sent to Supplier
2020-04-18 22:33:41 +00:00
'ORDER-SENT' => [
'fail'=>'ORDER-HOLD',
'next'=>[
'ORDERED'=>['wholesaler'],
],
'system'=>FALSE,
'method'=>'action_order_sent',
'title'=>'Send Order',
],
2019-01-24 10:55:43 +00:00
// Order Confirmed by Supplier
'ORDERED' => [
'fail'=>false,
'next'=>[
'PROVISION-HOLD'=>['wholesaler'],
'PROVISION-PLANNED'=>['wholesaler'],
'PROVISIONED'=>['wholesaler'],
],
'system'=>FALSE,
'method'=>'action_ordered',
'title'=>'Service Ordered',
],
// Service confirmed by supplier, optional connection date
'PROVISION-PLANNED' => [
'fail'=>false,
'next'=>[
'PROVISIONED'=>['wholesaler'],
],
'system'=>FALSE,
'method'=>'action_provision_planned',
'title'=>'Provision Planned',
],
// Service has been provisioned by supplier
'PROVISIONED' => [
'fail'=>false,
'next'=>[
'ACTIVE'=>['wholesaler'],
],
'system'=>FALSE,
'method'=>'action_provisioned',
'title'=>'Provisioned',
],
// Service is Active
2020-04-22 12:54:05 +00:00
'ACTIVE' => [
'fail'=>FALSE,
'next'=>[
'UPGRADE-REQUEST'=>['customer'],
'CANCEL-REQUEST'=>['customer'],
],
'system'=>FALSE,
'method'=>'action_active',
'title'=>'Service Active',
],
// Service to be Upgraded
2020-04-22 12:54:05 +00:00
'UPGRADE-REQUEST' => [
'fail'=>FALSE,
'next'=>[
'UPGRADE-PENDING'=>[],
],
'system'=>FALSE,
'method'=>FALSE,
'title'=>'Upgrade Service',
],
// Service to be Cancelled
2020-04-22 12:54:05 +00:00
'CANCEL-REQUEST' => [
'fail'=>FALSE,
'next'=>[
'CANCEL-PENDING'=>[],
],
'system'=>FALSE,
'method'=>'action_cancel_request',
'title'=>'Cancel Service',
],
2018-08-11 05:09:41 +00:00
];
/* RELATIONS */
2019-01-24 10:49:56 +00:00
/**
* Account the service belongs to
*
2020-02-05 04:47:24 +00:00
* @return BelongsTo
2019-01-24 10:49:56 +00:00
*/
2018-05-20 12:53:14 +00:00
public function account()
{
2018-07-13 04:53:44 +00:00
return $this->belongsTo(Account::class);
2018-05-20 12:53:14 +00:00
}
2020-02-05 04:47:24 +00:00
/**
* Return automatic billing details
*
* @return HasOne
*/
public function billing()
{
return $this->hasOne(AccountBilling::class);
}
2019-07-02 05:28:27 +00:00
/**
* Return Charges associated with this Service
*
2020-02-05 04:47:24 +00:00
* @return HasMany
2019-07-02 05:28:27 +00:00
*/
public function charges()
{
return $this->hasMany(Charge::class)
->where('active','=',TRUE)
->orderBy('date_orig');
}
// @todo changed to invoiced_items
2019-06-29 00:14:12 +00:00
public function invoice_items($active=TRUE)
{
$query = $this->hasMany(InvoiceItem::class)
2019-07-02 05:28:27 +00:00
->where('item_type','=',0)
2020-07-06 04:37:08 +00:00
->orderBy('date_start');
2019-06-29 00:14:12 +00:00
2021-06-29 06:36:34 +00:00
// @todo Change to $query->active();
2019-06-29 00:14:12 +00:00
if ($active)
$query->where('active','=',TRUE);
return $query;
}
/**
* Invoices for this service
*
*/
public function invoices($active=TRUE)
{
$query = $this->hasManyThrough(Invoice::class,InvoiceItem::class,NULL,'id',NULL,'invoice_id')
->distinct('id')
->where('ab_invoice.site_id','=',$this->site_id)
->where('ab_invoice_item.site_id','=',$this->site_id)
->orderBy('date_orig')
->orderBy('due_date');
if ($active)
$query->where('ab_invoice_item.active','=',TRUE)
->where('ab_invoice.active','=',TRUE);
return $query;
}
2019-01-24 10:49:56 +00:00
/**
* Account that ordered the service
*
2020-02-05 04:47:24 +00:00
* @return BelongsTo
2019-01-24 10:49:56 +00:00
*/
2020-02-05 04:47:24 +00:00
public function orderedby()
{
return $this->belongsTo(Account::class);
}
2019-01-24 10:49:56 +00:00
/**
2019-06-29 00:14:12 +00:00
* Product of the service
2019-01-24 10:49:56 +00:00
*
2020-02-05 04:47:24 +00:00
* @return BelongsTo
* @deprecated use type->product
2019-01-24 10:49:56 +00:00
*/
2019-06-29 00:14:12 +00:00
public function product()
{
2019-06-29 00:14:12 +00:00
return $this->belongsTo(Product::class);
}
/**
* The site this service is configured for
*
* @todo It may be more appropriate to get this from the account->user attribute (ie: for mail actions)
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function site()
{
return $this->belongsTo(Site::class);
}
/**
* Return a child model with details of the service
*
2020-02-05 04:47:24 +00:00
* @return MorphTo
*/
public function type()
{
return $this->morphTo(null,'model','id','service_id');
}
/* SCOPES */
2019-01-24 10:49:56 +00:00
/**
* Only query active categories
2019-01-24 10:49:56 +00:00
*/
public function scopeActive($query)
2018-05-20 12:53:14 +00:00
{
return $query->where(function () use ($query) {
2021-06-29 06:36:34 +00:00
$query->where($this->getTable().'.active',TRUE)
2020-02-05 04:47:24 +00:00
->orWhereNotIn('order_status',$this->inactive_status);
});
2018-05-20 12:53:14 +00:00
}
/**
* Only query records that the user is authorised to see
*/
public function scopeAuthorised($query,User $uo)
{
return $query
->whereIN($this->getTable().'.account_id',$uo->all_accounts()->pluck('id')->unique()->toArray());
}
2018-11-21 02:46:16 +00:00
/**
* Find inactive services.
*
* @param $query
* @return mixed
*/
public function scopeInActive($query)
{
return $query->where(function () use ($query) {
2021-06-29 06:36:34 +00:00
$query->where($this->getTable().'.active',FALSE)
2020-02-05 04:47:24 +00:00
->orWhereIn('order_status',$this->inactive_status);
2018-11-21 02:46:16 +00:00
});
}
/**
* Enable to perform queries without eager loading
*
* @param $query
* @return mixed
*/
public function scopeNoEagerLoads($query){
return $query->setEagerLoads([]);
}
/**
* Search for a record
*
* @param $query
* @param string $term
* @return
*/
public function scopeSearch($query,string $term)
{
return $query->where('id','like','%'.$term.'%');
}
/* ATTRIBUTES */
/**
* Name of the account for this service
*
* @return mixed
2021-06-29 06:36:34 +00:00
* @deprecated use $this->>account->name directly
*/
public function getAccountNameAttribute(): string
2018-08-11 05:09:41 +00:00
{
return $this->account->name;
}
/**
* @deprecated Use getUrlAdminAttribute()
*/
public function getAdminServiceIdUrlAttribute()
2018-08-23 05:17:26 +00:00
{
return $this->getUrlAdminAttribute();
2018-08-23 05:17:26 +00:00
}
2020-02-05 04:47:24 +00:00
/**
* Return the auto billing details
*
* @return mixed
2021-06-29 06:36:34 +00:00
* @deprecated use billing directly?
2020-02-05 04:47:24 +00:00
*/
public function getAutoPayAttribute()
{
return $this->billing;
}
2019-06-29 00:14:12 +00:00
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)))
$this->price=0;
return $this->addTax(is_null($this->price) ? $this->product->price($this->recur_schedule) : $this->price);
2019-06-29 00:14:12 +00:00
}
public function getBillingMonthlyPriceAttribute(): float
{
$d = 0;
switch ($this->recur_schedule) {
case 0: $d = 12/52; break;
case 1: $d = 1; break;
case 2: $d = 3; break;
case 3: $d = 6; break;
case 4: $d = 12; break;
case 5: $d = 24; break;
case 6: $d = 36; break;
}
return number_format($this->getBillingPriceAttribute()/$d,2);
}
/**
2019-06-29 00:14:12 +00:00
* Return the service billing period
*
* @return string
*/
2019-06-29 00:14:12 +00:00
public function getBillingPeriodAttribute(): string
2018-06-05 11:13:57 +00:00
{
2019-06-29 00:14:12 +00:00
return Arr::get($this->product->PricePeriods(),$this->recur_schedule,'Unknown');
2018-06-05 11:13:57 +00:00
}
/**
2019-06-29 00:14:12 +00:00
* Date the service expires, also represents when it is paid up to
*
* @return string
2021-06-29 06:36:34 +00:00
* @todo
*/
2019-06-29 00:14:12 +00:00
public function getExpiresAttribute(): string
2018-08-01 07:09:38 +00:00
{
2019-06-29 00:14:12 +00:00
return 'TBA';
2018-08-01 07:09:38 +00:00
}
/**
* Return the date for the next invoice
*
2020-02-12 10:32:57 +00:00
* @todo Change date_next_invoice to connect_date/invoice_start_date
* @return Carbon|string
*/
2019-07-02 05:28:27 +00:00
public function getInvoiceNextAttribute()
2018-11-21 03:37:17 +00:00
{
2019-06-29 00:14:12 +00:00
$last = $this->getInvoiceToAttribute();
2020-02-12 10:32:57 +00:00
$date = $last
? $last->addDay()
: ($this->date_next_invoice ? $this->date_next_invoice->clone()
: ($this->date_start ?: Carbon::now()));
2019-07-02 05:28:27 +00:00
return request()->wantsJson() ? $date->format('Y-m-d') : $date;
2019-07-02 05:28:27 +00:00
}
/**
* Return the end date for the next invoice
*
* @return mixed
* @throws Exception
*/
2019-07-02 05:28:27 +00:00
public function getInvoiceNextEndAttribute()
{
switch ($this->recur_schedule) {
2019-07-02 05:28:27 +00:00
// Weekly
case 0: $date = $this->product->price_recurr_strict
? $this->getInvoiceNextAttribute()->endOfWeek()
: $this->getInvoiceNextAttribute()->addWeek()->subDay();
break;
// Monthly
case 1:
$date = $this->product->price_recurr_strict
? $this->getInvoiceNextAttribute()->endOfMonth()
: $this->getInvoiceNextAttribute()->addMonth()->subDay();
break;
// Quarterly
case 2:
$date = $this->product->price_recurr_strict
? $this->getInvoiceNextAttribute()->endOfQuarter()
: $this->getInvoiceNextAttribute()->addQuarter()->subDay();
break;
// Half Yearly
case 3:
$date = $this->product->price_recurr_strict
? $this->getInvoiceNextAttribute()->endOfHalf()
: $this->getInvoiceNextAttribute()->addQuarter(2)->subDay();
break;
// Yearly
case 4:
$date = $this->product->price_recurr_strict
? $this->getInvoiceNextAttribute()->endOfYear()
: $this->getInvoiceNextAttribute()->addYear()->subDay();
break;
// Two Yearly
case 5:
if (!$this->product->price_recurr_strict)
$date = $this->getInvoiceNextAttribute()->addYear(2)->subDay();
else {
$date = $this->getInvoiceNextAttribute()->addYear(2)->subDay()->endOfYear();
if ($date->clone()->addDay()->year % 2) {
$date = $date->subYear();
}
}
break;
// Three Yearly
// NOTE: price_recurr_strict ignored
case 6: $date = $this->getInvoiceNextAttribute()->addYear(3)->subDay(); break;
default: throw new Exception('Unknown recur_schedule');
}
2020-04-22 12:54:05 +00:00
// If the invoice has an end date, our invoice period shouldnt be greater than that.
if ($this->date_end AND $this->date_end < $date)
2020-04-22 12:54:05 +00:00
$date = $this->date_end;
return $date;
}
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)
return 1;
$n = $this->invoice_next->diff($this->invoice_next_end)->days+1;
switch ($this->recur_schedule) {
// Weekly
case 0:
$d = $this->invoice_next->addWeek()->startOfWeek()->diff($this->invoice_next_end->startOfWeek())->days;
break;
2019-07-02 05:28:27 +00:00
// Monthly
case 1:
$d = $this->invoice_next->addMonth()->startOfMonth()->diff($this->invoice_next_end->startOfMonth())->days;
break;
2019-07-02 05:28:27 +00:00
// Quarterly
case 2:
$d = $this->invoice_next->addQuarter()->startOfQuarter()->diff($this->invoice_next_end->startOfQuarter())->days;
break;
2019-07-02 05:28:27 +00:00
// Half Yearly
case 3:
2020-07-06 06:13:53 +00:00
$d = $this->invoice_next->addQuarter(2)->startOfHalf()->diff($this->invoice_next_end->startOfHalf())->days;
break;
2019-07-02 05:28:27 +00:00
// Yearly
case 4:
$d = $this->invoice_next->addYear()->startOfYear()->diff($this->invoice_next_end->startOfYear())->days;
break;
2019-07-02 05:28:27 +00:00
// Two Yearly
case 5:
$d = $this->invoice_next->addYear(2)->startOfYear()->diff($this->invoice_next_end->subyear(2))->days-1;
break;
2019-07-02 05:28:27 +00:00
// Three Yearly
case 6:
$d = $this->invoice_next->addYear(3)->startOfYear()->diff($this->invoice_next_end->subyear(3))->days-1;
break;
default: throw new Exception('Unknown recur_schedule');
2019-07-02 05:28:27 +00:00
}
2019-06-29 00:14:12 +00:00
return round($n/$d,2);
2019-06-29 00:14:12 +00:00
}
/**
* Get the date that the service has been invoiced to
*/
public function getInvoiceToAttribute()
{
2020-04-01 12:35:06 +00:00
$result = ($x=$this->invoice_items->filter(function($item) { return $item->item_type === 0;}))->count()
? $x->last()->date_stop
: NULL;
// For SSL Certificates, the invoice_to date is the expiry date of the Cert
2020-04-01 21:59:22 +00:00
if (is_null($result) AND $this->type AND $this->type->type == 'ssl' AND $this->type->valid_to)
2020-04-01 12:35:06 +00:00
return $this->type->valid_to;
return $result;
2018-11-21 03:37:17 +00:00
}
2021-06-29 06:36:34 +00:00
/**
* Service Local ID
*
* @return string
*/
public function getLIDattribute(): string
{
return sprintf('%05s',$this->id);
}
2019-07-02 05:28:27 +00:00
public function getNameAttribute(): string
{
return $this->product->name_short.': '.$this->getNameShortAttribute();
}
/**
* Return the short name for the service.
*
* EG:
2020-04-01 12:35:06 +00:00
* + For ADSL, this would be the phone number,
* + For Hosting, this would be the domain name, etc
*/
public function getNameShortAttribute()
{
2020-07-22 04:58:32 +00:00
return $this->type ? $this->type->service_name : $this->id;
}
/**
* @deprecated see getInvoiceNextAttribute()
*/
public function getNextInvoiceAttribute()
2018-06-05 11:13:57 +00:00
{
return $this->getInvoiceNextAttribute();
}
2020-04-18 22:33:41 +00:00
public function getOrderInfoNotesAttribute(): ?string
2019-01-24 03:40:33 +00:00
{
2020-04-18 22:33:41 +00:00
return $this->getOrderInfoValue('notes');
}
2019-01-24 03:40:33 +00:00
2020-04-18 22:33:41 +00:00
public function getOrderInfoReferenceAttribute(): ?string
{
return $this->getOrderInfoValue('reference');
2019-01-24 03:40:33 +00:00
}
2020-02-05 04:47:24 +00:00
/**
* Work out when this service has been paid to.
*
* @todo This might need to be optimised
*/
public function getPaidToAttribute()
{
foreach ($this->invoices->reverse() as $o) {
if ($o->due == 0) {
return $o->items
->filter(function($item) {
return $item->item_type === 0;
})
->last()
->date_stop;
}
}
}
/**
* Get the Product's Category for this service
*
2021-06-29 06:36:34 +00:00
* @deprecated use product->category directly
*/
public function getProductCategoryAttribute(): string
2018-05-20 12:53:14 +00:00
{
return $this->product->category;
2018-05-20 12:53:14 +00:00
}
/**
* Get the Product's Short Name for the service
*
* @return string
2021-06-29 06:36:34 +00:00
* @deprecated use product->name directly
*/
public function getProductNameAttribute(): string
{
return $this->product->name($this->account->language);
}
public function getRecurScheduleAttribute($value): int
{
// If recur_schedule not set, default to 2
return $value ?? 2;
}
/**
2020-02-05 04:47:24 +00:00
* @deprecated see getSIDAttribute()
*/
2020-02-05 04:47:24 +00:00
public function getServiceIdAttribute(): string
{
return $this->getSIDAttribute();
}
/**
* @deprecated see getUrlUserAttribute()
*/
public function getServiceIdUrlAttribute()
{
return $this->getUrlUserAttribute();
}
/**
* @deprecated see getServiceIdAttribute()
*/
2018-05-20 12:53:14 +00:00
public function getServiceNumberAttribute()
{
return $this->getSIDAttribute();
2018-05-20 12:53:14 +00:00
}
2018-08-11 05:09:41 +00:00
2019-06-29 00:14:12 +00:00
/**
2021-06-29 06:36:34 +00:00
* Services System ID
2019-06-29 00:14:12 +00:00
*
* @return string
*/
public function getSIDAttribute(): string
{
2021-06-29 06:36:34 +00:00
return sprintf('%02s-%04s.%s',$this->site_id,$this->account_id,$this->getLIDattribute());
2019-06-29 00:14:12 +00:00
}
2020-02-05 04:47:24 +00:00
/**
* Return the service description.
* For:
2020-04-01 12:35:06 +00:00
* + Broadband, this is the service address
* + Domains, blank
* + Hosting, blank
* + SSL, blank
2020-02-05 04:47:24 +00:00
*
* @return string
*/
public function getSDescAttribute(): string
{
2020-02-12 10:32:57 +00:00
return ($this->type AND $this->type->service_description)
? $this->type->service_description
: 'Service Description NOT Defined for :'.($this->type ? $this->type->type : $this->id);
2020-02-05 04:47:24 +00:00
}
/**
* Return the service name.
* For:
2020-04-01 12:35:06 +00:00
* + Broadband, this is the service number
* + Domains, this is the full domain name
* + Hosting, this is the full domain name
* + SSL, this is the DN
2020-02-05 04:47:24 +00:00
*
* @return string
*/
public function getSNameAttribute(): string
{
2020-02-12 10:32:57 +00:00
return ($this->type AND $this->type->service_name)
? $this->type->service_name
: 'Service Name NOT Defined for :'.($this->type ? $this->type->type : $this->id);
2020-02-05 04:47:24 +00:00
}
/**
* Return the service product type
* This is used for view specific details
*
* @return string
*/
public function getSTypeAttribute(): string
{
switch($this->product->model) {
case 'App\Models\Product\Adsl': return 'broadband';
default: return $this->type->type;
2020-02-05 04:47:24 +00:00
}
}
/**
* Return the Service Status
*
* @return string
*/
public function getStatusAttribute(): string
2018-08-11 05:09:41 +00:00
{
if (! $this->order_status)
return $this->active ? 'ACTIVE' : 'INACTIVE';
else
return $this->order_status;
}
/**
* Return the detailed order Status, with order reference numbers.
*
* @return string
*/
public function getStatusDetailAttribute(): string
{
2019-01-24 10:55:43 +00:00
return in_array($this->order_status,['ORDER-SENT','ORDER-HOLD','ORDERED'])
? sprintf('%s: <span class="white-space: nowrap"><small><b>#%s</b></small></span>',$this->order_status,Arr::get($this->order_info,'order_reference','Unknown'))
: '';
2018-08-11 05:09:41 +00:00
}
2019-06-29 00:14:12 +00:00
/**
* Return a HTML status box
*
* @return string
*/
public function getStatusHTMLAttribute(): string
{
$class = NULL;
if ($this->isPending())
$class = 'badge-warning';
else
switch ($this->status)
{
case 'ACTIVE':
$class = 'badge-success';
break;
case 'INACTIVE':
$class = 'badge-danger';
break;
}
return $class
? sprintf('<span class="badge %s">%s</span>',$class,$this->status)
: $this->status;
2019-06-29 00:14:12 +00:00
}
/**
* URL used by an admin to administer the record
*
* @return string
*/
public function getUrlAdminAttribute(): string
{
return sprintf('<a href="/a/service/%s">%s</a>',$this->id,$this->service_id);
}
/**
* URL used by an user to see the record
*
* @return string
*/
public function getUrlUserAttribute(): string
{
return sprintf('<a href="/u/service/%s">%s</a>',$this->id,$this->service_id);
}
/* SETTERS */
2021-06-29 06:36:34 +00:00
// @todo is this required?
2018-08-11 05:09:41 +00:00
public function setDateOrigAttribute($value)
{
$this->attributes['date_orig'] = $value->timestamp;
}
2021-06-29 06:36:34 +00:00
// @todo is this required?
2018-08-11 05:09:41 +00:00
public function setDateLastAttribute($value)
{
$this->attributes['date_last'] = $value->timestamp;
}
2021-06-29 06:36:34 +00:00
/* GENERAL METHODS */
2020-04-18 22:33:41 +00:00
// The action methods will return: NULL for no progress|FALSE for a failed status|next stage name.
2020-04-22 12:54:05 +00:00
/**
* Action required before order can leave the ACTIVE status.
*
* @return bool
*/
private function action_active(): ?bool
{
// N/A
return TRUE;
}
/**
* Request cancellation for an order when status ACTIVE stage.
* This method should have the client confirm/accept the cancellation, if it was placed by a reseller/wholesaler.
*
* @return bool
*/
private function action_cancel_request(): ?bool
{
throw new HttpException(301,url('u/service/cancel',$this->id));
}
/**
* Processing when service has been ordered.
*
* @return bool|null
*/
private function action_ordered(): ?bool
{
// N/A
return TRUE;
}
2020-04-18 22:33:41 +00:00
/**
* Process for an order when status ORDER-ACCEPT stage.
* This method should have the client confirm/accept the order, if it was placed by a reseller/wholesaler.
*
* @return bool
*/
private function action_order_accept(): ?bool
{
// @todo TO IMPLEMENT
return TRUE;
}
/**
* Action method when status ORDER_SENT
* This method redirects to a form, where updating the form will progress to the next stage.
*/
private function action_order_sent(string $next)
2020-04-18 22:33:41 +00:00
{
// We can proceed to the ordered status
if ($next == 'ORDERED' AND $this->order_info_reference)
return TRUE;
2020-04-18 22:33:41 +00:00
throw new HttpException(301,url('r/service/update',$this->id));
}
/**
* Action method when status ORDER_SUBMIT
*
* @return bool
*/
private function action_order_submit(): ?bool
{
// @todo TO IMPLEMENT
return TRUE;
}
/**
* Action when supplier has confirmed provisioning.
*
* @param string $next
* @return bool
*/
private function action_provision_planned(string $next)
{
throw new HttpException(301,url('r/service/update',$this->id));
}
2020-04-18 22:33:41 +00:00
/**
* Process for an order when status SETUP-PAYMENT-WAIT stage.
* This method should collect any setup fees payment.
*
* @return bool
*/
private function action_setup_payment_wait(): ?bool
{
// @todo TO IMPLEMENT
return TRUE;
}
/**
* Process for an order when status PAYMENT-CHECK stage.
* This method should validate any payment details.
*
* @return bool
*/
private function action_payment_check(): ?bool
{
// @todo TO IMPLEMENT
return TRUE;
}
/**
* Process for an order when status PAYMENT-WAIT stage.
* This method should collect any service payment details.
*
* @return bool
*/
private function action_payment_wait(): ?bool
{
// @todo TO IMPLEMENT
return TRUE;
}
private function getOrderInfoValue(string $key): ?string
{
return $this->order_info ? $this->order_info->get($key) : NULL;
}
/**
* Get the current stage parameters
*
* @param string $stage
* @return array
*/
private function getStageParameters(string $stage): Collection
{
$result = collect(Arr::get(self::$action_progress,$stage));
$myrole = array_search(Auth::user()->role(),User::$role_order);
// If we have no valid next stage, return an empty collection.
if (! $result->count() OR $myrole===FALSE)
return $result;
// Filter the result based on who we are
$next = collect();
foreach ($result->get('next') as $action=>$roles) {
// Can the current user do this role?
$cando = FALSE;
foreach ($roles as $role) {
if ($myrole <= array_search($role,User::$role_order)) {
2020-04-18 22:33:41 +00:00
$cando = TRUE;
break;
}
}
if ($cando OR $result->get('system')) {
$next->put($action,$roles);
}
}
$result->put('next',$next);
return $result;
}
/**
* @notes
* + When progressing stages, we know who the user is that initiated the stage,
* + If no user, then we perform stages SYSTEM=TRUE
* + We need to validate that the current stage is complete, before progressing to the next stage
* + The current stage may require input from a user, or automation process to progress
* + Before leaving this method, we update the service with the stage that it is currently on.
*
* @param string $stage
* @return bool|int|string|null
*/
public function action(string $stage)
{
// While stage has a string value, that indicates the next stage we want to go to
// If stage is NULL, the current stage hasnt been completed
// If stage is FALSE, then the current stage failed, and may optionally be directed to another stage.
while ($stage) {
// Check that stage is a valid next action for the user currently performing it
$current = $this->getStageParameters($this->order_status);
$next = $this->getStageParameters($stage);
// If valid, call the method to confirm that the current stage is complete
if (method_exists($this,$current['method'])) {
try {
$result = $this->{$current['method']}($stage);
2020-04-18 22:33:41 +00:00
// If we have a form to complete, we need to return with a URL, so we can catch that with an Exception
} catch (HttpException $e) {
if ($e->getStatusCode() == 301)
return ($e->getMessage());
}
// @todo Implement a status message
if (is_null($result)) {
$stage = NULL;
abort(500,'Current Method Cannot Proceed: '.$result);
// @todo Implement a status message
} elseif (! $result) {
$stage = NULL;
abort(500,'Current Method FAILED: '.$result);
} else {
$this->order_status = $stage;
$this->save();
// If we have more than 1 next step for the next stage, we'll have to end.
if ($this->actions()->count() > 1) {
$stage = NULL;
} else {
$stage = $this->actions()->keys()->first();
}
}
// @todo Implement a status message
} else {
// Cant do anything, dont have a method to check if we can leave
$stage = NULL;
2020-04-22 12:54:05 +00:00
abort(500,'NO Method Cannot Proceed to leave this stage: '.$current['method']);
2020-04-18 22:33:41 +00:00
}
// If valid, call the method to start the next stage
}
}
/**
* Work out the next applicable actions for this service status
*
* @notes
* + Clients can only progress 1 step, if they are in the next step.
* + Resellers/Wholesales can progress to the next Reseller/Wholesaler and any steps in between.
*
* @param bool $next Only show next actions
* @return Collection
*/
public function actions(): Collection
{
$result = collect();
$action = $this->getStageParameters($this->order_status);
if (! $action->count())
return $result;
// Next Action
foreach ($this->getStageParameters($this->order_status)->get('next') as $k=>$v) {
$result->put($k,Arr::get(self::$action_progress,$k.'.title'));
}
// No next actions, that will mean the current action hasnt completed.
if (! $result->count())
$result->put($this->order_status,Arr::get($action,'title'));
return $result;
}
2019-06-29 00:14:12 +00:00
/**
* Add applicable tax to the cost
*
* @todo This needs to be calculated, not fixed at 1.1
* @param float $value
* @return float
*/
private function addTax(float $value): float
2019-06-29 00:14:12 +00:00
{
return round($value*1.1,2);
}
public function invoices_due(): DatabaseCollection
2019-06-29 00:14:12 +00:00
{
2019-07-02 05:28:27 +00:00
$this->load('invoice_items.invoice');
2019-06-29 00:14:12 +00:00
return $this->invoice_items->filter(function($item) {
return $item->invoice->due > 0;
});
}
/**
* Does this service have traffic data to be graphed
*
* @return bool
*/
public function hasUsage(): bool
{
return $this->product->hasUsage();
}
/**
* Determine if a service is active. It is active, if active=1, or the order_status is not in inactive_status[]
*
* @return bool
* @todo Remove active and have order_status reflect whether active or not
*/
public function isActive(): bool
2018-08-11 05:09:41 +00:00
{
return $this->active OR ($this->order_status AND ! in_array($this->order_status,$this->inactive_status));
}
/**
* Do we bill for this service
*
* @return bool
*/
public function isBilled(): bool
{
return ! ($this->external_billing && $this->suspend_billing);
}
/**
* Should this service be invoiced soon
*
* @todo get the number of days from account setup
* @todo Use self::isBilled();
* @return bool
*/
public function isInvoiceDueSoon($days=30): bool
2019-07-02 05:28:27 +00:00
{
return (! $this->external_billing) AND (! $this->suspend_billing) AND $this->getInvoiceNextAttribute()->lessThan(now()->addDays($days));
}
2019-07-02 05:28:27 +00:00
2020-02-05 04:47:24 +00:00
/**
* Identify if a service is being ordered
*
* @return bool
*/
public function isPending(): bool
{
return ! $this->active
AND ! is_null($this->order_status)
AND ! in_array($this->order_status,array_merge($this->inactive_status,['INACTIVE']));
2020-02-05 04:47:24 +00:00
}
/**
* Generate a collection of invoice_item objects that will be billed for the next invoice
*
2020-04-01 12:35:06 +00:00
* @param bool $future Next item to be billed (not in the next x days)
* @return Collection
* @throws Exception
* @todo Use self::isBilled();
*/
public function next_invoice_items(bool $future,Carbon $billdate=NULL): Collection
{
2020-04-18 22:33:41 +00:00
if ($this->wasCancelled() OR $this->suspend_billing OR $this->external_billing OR (! $future AND ! $this->active))
2020-02-12 10:32:57 +00:00
return collect();
if (is_null($billdate))
$billdate = Carbon::now()->addDays(30);
// If pending, add any connection charges
2020-02-12 10:32:57 +00:00
// Connection charges are only charged once
if ((! $this->invoice_items->filter(function($item) { return $item->item_type==4; })->sum('total'))
2020-04-01 12:35:06 +00:00
AND ($this->isPending() OR is_null($this->invoice_to))
AND $this->product->price($this->recur_schedule,'price_setup'))
2020-02-12 10:32:57 +00:00
{
$o = new InvoiceItem;
2020-02-12 10:32:57 +00:00
$o->active = TRUE;
$o->service_id = $this->id;
$o->product_id = $this->product_id;
2020-04-01 12:35:06 +00:00
$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->recurring_schedule = $this->recur_schedule;
$o->date_start = $this->invoice_next;
$o->date_stop = $this->invoice_next;
$o->quantity = 1;
2020-04-01 12:35:06 +00:00
$o->site_id = 1; // @todo
2020-02-08 11:51:50 +00:00
$o->addTaxes($this->account->country->taxes);
2020-02-12 10:32:57 +00:00
$this->invoice_items->push($o);
}
// If the service is active, there will be service charges
2020-02-20 11:54:28 +00:00
if ((! $this->invoice_items->filter(function($item) { return $item->item_type==0 AND ! $item->exists; })->count())
2020-04-01 12:35:06 +00:00
AND ($this->active OR $this->isPending())
2020-04-22 12:54:05 +00:00
AND (
(($future == TRUE) AND $this->invoice_next < $this->invoice_next_end) OR
(($future == FALSE) AND ($this->invoice_to < ($this->date_end ?: $billdate)))
2020-04-22 12:54:05 +00:00
))
{
2020-02-12 10:32:57 +00:00
do {
$o = new InvoiceItem;
$o->active = TRUE;
$o->service_id = $this->id;
$o->product_id = $this->product_id;
$o->item_type = 0;
2020-04-01 12:35:06 +00:00
$o->price_base = is_null($this->price)
? (is_null($this->price_override) ? $this->product->price($this->recur_schedule) : $this->price_override)
: $this->price; // @todo change to a method in this class
2020-02-12 10:32:57 +00:00
$o->recurring_schedule = $this->recur_schedule;
$o->date_start = $this->invoice_next;
$o->date_stop = $this->invoice_next_end;
$o->quantity = $this->invoice_next_quantity;
2020-04-01 12:35:06 +00:00
$o->site_id = 1; // @todo
2020-02-12 10:32:57 +00:00
$o->addTaxes($this->account->country->taxes);
$this->invoice_items->push($o);
} while ($future == FALSE AND ($this->invoice_to < ($this->date_end ?: $billdate)));
}
2020-04-01 12:35:06 +00:00
// Add additional charges
if ((($future == TRUE) OR (($future == FALSE) AND ($this->invoice_to >= $billdate)))
AND ! $this->invoice_items->filter(function($item) { return $item->module_id == 30 AND ! $item->exists; })->count())
{
2020-04-01 12:35:06 +00:00
foreach ($this->charges->filter(function($item) { return ! $item->processed; }) as $oo) {
$o = new InvoiceItem;
$o->active = TRUE;
$o->service_id = $oo->service_id;
$o->product_id = $this->product_id;
$o->quantity = $oo->quantity;
$o->item_type = $oo->type;
$o->price_base = $oo->amount;
$o->date_start = $oo->date_charge;
$o->date_stop = $oo->date_charge;
$o->module_id = 30; // @todo This shouldnt be hard coded
$o->module_ref = $oo->id;
$o->site_id = 1; // @todo
$o->addTaxes($this->account->country->taxes);
$this->invoice_items->push($o);
}
}
2019-07-02 05:28:27 +00:00
2020-02-12 10:32:57 +00:00
return $this->invoice_items->filter(function($item) { return ! $item->exists; });
2019-07-02 05:28:27 +00:00
}
2018-08-11 05:09:41 +00:00
/**
* This function will return the associated service model for the product type
* @deprecated use $this->type
2018-08-11 05:09:41 +00:00
*/
private function ServicePlugin()
{
2018-11-21 02:46:16 +00:00
// @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;
2018-08-11 05:09:41 +00:00
switch ($this->product->prod_plugin_file)
{
case 'ADSL': return $this->service_adsl;
case 'DOMAIN': return $this->service_domain;
case 'HOST': return $this->service_host;
case 'SSL': return $this->service_ssl;
case 'VOIP': return $this->service_voip;
default: return NULL;
}
}
2019-01-24 03:40:33 +00:00
/**
2020-04-18 22:33:41 +00:00
* Store order info details
2019-01-24 03:40:33 +00:00
*
2020-04-18 22:33:41 +00:00
* @param string $key
* @param string $value
2019-01-24 03:40:33 +00:00
*/
public function setOrderInfo(string $key,?string $value): void
{
2020-04-18 22:33:41 +00:00
$x = is_null($this->order_info) ? collect() : $this->order_info;
$x->put($key,$value);
2020-04-18 22:33:41 +00:00
$this->order_info = $x;
2019-01-24 03:40:33 +00:00
}
2020-02-12 10:32:57 +00:00
/**
* Service that was cancelled or never provisioned
*
* @return bool
*/
public function wasCancelled(): bool
{
return in_array($this->order_status,$this->inactive_status);
}
}