2018-05-20 12:53:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2020-02-06 09:31:43 +00:00
|
|
|
use Exception;
|
|
|
|
use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
|
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;
|
2019-06-21 06:21:48 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2020-02-06 09:31:43 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2019-06-07 06:54:27 +00:00
|
|
|
|
2018-08-11 05:09:41 +00:00
|
|
|
use App\Traits\NextKey;
|
2020-02-06 09:31:43 +00:00
|
|
|
use Leenooks\Carbon;
|
2018-05-20 12:53:14 +00:00
|
|
|
|
|
|
|
class Service extends Model
|
|
|
|
{
|
2018-08-11 05:09:41 +00:00
|
|
|
use NextKey;
|
2019-06-13 04:32:34 +00:00
|
|
|
const RECORD_ID = 'service';
|
2020-02-05 04:47:24 +00:00
|
|
|
public $incrementing = FALSE;
|
2019-06-13 04:32:34 +00:00
|
|
|
|
2019-06-07 06:54:27 +00:00
|
|
|
const CREATED_AT = 'date_orig';
|
|
|
|
const UPDATED_AT = 'date_last';
|
2018-08-11 05:09:41 +00:00
|
|
|
|
2018-06-19 12:31:49 +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',
|
2019-07-04 04:55:05 +00:00
|
|
|
'billing_price',
|
2019-06-07 06:54:27 +00:00
|
|
|
'name_short',
|
2018-06-19 12:31:49 +00:00
|
|
|
'next_invoice',
|
2019-06-07 06:54:27 +00:00
|
|
|
'product_category',
|
2018-06-19 12:31:49 +00:00
|
|
|
'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 $casts = [
|
|
|
|
'order_info'=>'array',
|
|
|
|
];
|
|
|
|
|
|
|
|
protected $dates = [
|
|
|
|
'date_last_invoice',
|
|
|
|
'date_next_invoice'.
|
|
|
|
'date_start',
|
|
|
|
'date_end',
|
|
|
|
];
|
|
|
|
public $dateFormat = 'U';
|
|
|
|
|
|
|
|
protected $table = 'ab_service';
|
|
|
|
|
2018-06-19 12:31:49 +00:00
|
|
|
protected $visible = [
|
2018-08-11 05:09:41 +00:00
|
|
|
'account_name',
|
2018-08-23 05:17:26 +00:00
|
|
|
'admin_service_id_url',
|
2018-06-19 12:31:49 +00:00
|
|
|
'active',
|
2019-07-04 04:55:05 +00:00
|
|
|
'billing_price',
|
2018-06-19 12:31:49 +00:00
|
|
|
'data_orig',
|
|
|
|
'id',
|
2019-06-07 06:54:27 +00:00
|
|
|
'name_short',
|
2018-06-19 12:31:49 +00:00
|
|
|
'next_invoice',
|
2019-06-07 06:54:27 +00:00
|
|
|
'product_category',
|
2018-06-19 12:31:49 +00:00
|
|
|
'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',
|
|
|
|
];
|
|
|
|
|
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',
|
|
|
|
];
|
|
|
|
|
2019-06-21 06:21:48 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2019-01-24 03:40:33 +00:00
|
|
|
private $valid_status = [
|
2019-01-24 10:49:56 +00:00
|
|
|
// Order Submitted
|
|
|
|
'ORDER-SUBMIT' => ['approve'=>'ORDER-SENT','hold'=>'ORDER-HOLD','reject'=>'ORDER-REJECTED','cancel'=>'ORDER-CANCELLED'],
|
|
|
|
// 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
|
2019-01-24 10:55:43 +00:00
|
|
|
'ORDER-SENT' => ['update_reference'=>'ORDER-SENT','confirm'=>'ORDERED'],
|
|
|
|
// Order Confirmed by Supplier
|
|
|
|
'ORDERED' => ['update_reference'=>'ORDER-SENT'],
|
2018-08-11 05:09:41 +00:00
|
|
|
];
|
|
|
|
|
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)
|
2019-06-29 00:14:12 +00:00
|
|
|
->orderBy('date_orig');
|
|
|
|
|
|
|
|
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()
|
2018-09-18 02:37:04 +00:00
|
|
|
{
|
|
|
|
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
|
2019-01-24 10:49:56 +00:00
|
|
|
*/
|
2019-06-29 00:14:12 +00:00
|
|
|
public function product()
|
2018-09-18 02:37:04 +00:00
|
|
|
{
|
2019-06-29 00:14:12 +00:00
|
|
|
return $this->belongsTo(Product::class);
|
2018-09-18 02:37:04 +00:00
|
|
|
}
|
|
|
|
|
2020-02-09 12:12:34 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2019-06-21 06:21:48 +00:00
|
|
|
/**
|
|
|
|
* Return a child model with details of the service
|
|
|
|
*
|
2020-02-05 04:47:24 +00:00
|
|
|
* @return MorphTo
|
2019-06-21 06:21:48 +00:00
|
|
|
*/
|
2019-06-07 06:54:27 +00:00
|
|
|
public function type()
|
2018-06-19 12:31:49 +00:00
|
|
|
{
|
2019-06-07 06:54:27 +00:00
|
|
|
return $this->morphTo(null,'model','id','service_id');
|
2018-06-19 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 06:21:48 +00:00
|
|
|
/** SCOPES **/
|
|
|
|
|
2019-01-24 10:49:56 +00:00
|
|
|
/**
|
2019-06-07 06:54:27 +00:00
|
|
|
* Only query active categories
|
2019-01-24 10:49:56 +00:00
|
|
|
*/
|
2019-06-07 06:54:27 +00:00
|
|
|
public function scopeActive($query)
|
2018-05-20 12:53:14 +00:00
|
|
|
{
|
2019-06-07 06:54:27 +00:00
|
|
|
return $query->where(function () use ($query) {
|
2020-02-05 04:47:24 +00:00
|
|
|
$query->where('active',TRUE)
|
|
|
|
->orWhereNotIn('order_status',$this->inactive_status);
|
2019-06-07 06:54:27 +00:00
|
|
|
});
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2020-02-05 04:47:24 +00:00
|
|
|
$query->where('active',FALSE)
|
|
|
|
->orWhereIn('order_status',$this->inactive_status);
|
2018-11-21 02:46:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-04 04:55:05 +00:00
|
|
|
/**
|
|
|
|
* Enable to perform queries without eager loading
|
|
|
|
*
|
|
|
|
* @param $query
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function scopeNoEagerLoads($query){
|
|
|
|
return $query->setEagerLoads([]);
|
|
|
|
}
|
|
|
|
|
2020-01-12 12:42:32 +00:00
|
|
|
/**
|
|
|
|
* Search for a record
|
|
|
|
*
|
|
|
|
* @param $query
|
|
|
|
* @param string $term
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public function scopeSearch($query,string $term)
|
|
|
|
{
|
|
|
|
return $query->where('id','like','%'.$term.'%');
|
|
|
|
}
|
|
|
|
|
2019-06-21 06:21:48 +00:00
|
|
|
/** ATTRIBUTES **/
|
|
|
|
|
2018-06-19 12:31:49 +00:00
|
|
|
/**
|
2019-06-07 06:54:27 +00:00
|
|
|
* Name of the account for this service
|
|
|
|
*
|
|
|
|
* @return mixed
|
2018-06-19 12:31:49 +00:00
|
|
|
*/
|
2019-06-21 06:21:48 +00:00
|
|
|
public function getAccountNameAttribute(): string
|
2018-08-11 05:09:41 +00:00
|
|
|
{
|
2019-06-07 06:54:27 +00:00
|
|
|
return $this->account->name;
|
2018-06-19 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 06:54:27 +00:00
|
|
|
/**
|
2019-06-21 06:21:48 +00:00
|
|
|
* @deprecated Use getUrlAdminAttribute()
|
2019-06-07 06:54:27 +00:00
|
|
|
*/
|
2019-06-21 06:21:48 +00:00
|
|
|
public function getAdminServiceIdUrlAttribute()
|
2018-08-23 05:17:26 +00:00
|
|
|
{
|
2019-06-21 06:21:48 +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
|
|
|
|
*/
|
|
|
|
public function getAutoPayAttribute()
|
|
|
|
{
|
|
|
|
return $this->billing;
|
|
|
|
}
|
|
|
|
|
2019-06-29 00:14:12 +00:00
|
|
|
public function getBillingPriceAttribute(): float
|
|
|
|
{
|
2019-07-04 04:55:05 +00:00
|
|
|
// @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
|
|
|
}
|
|
|
|
|
2019-06-07 06:54:27 +00:00
|
|
|
/**
|
2019-06-29 00:14:12 +00:00
|
|
|
* Return the service billing period
|
2019-06-07 06:54:27 +00:00
|
|
|
*
|
|
|
|
* @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-07 06:54:27 +00:00
|
|
|
/**
|
2019-06-29 00:14:12 +00:00
|
|
|
* Date the service expires, also represents when it is paid up to
|
2019-06-21 06:21:48 +00:00
|
|
|
*
|
|
|
|
* @return string
|
2019-06-07 06:54:27 +00:00
|
|
|
*/
|
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
|
|
|
}
|
2019-06-17 03:17:58 +00:00
|
|
|
|
2019-06-07 06:54:27 +00:00
|
|
|
/**
|
|
|
|
* Return the date for the next invoice
|
|
|
|
*
|
2019-06-29 00:14:12 +00:00
|
|
|
* @todo This function negates the need for date_next_invoice
|
2020-02-06 09:31:43 +00:00
|
|
|
* @return Carbon|string
|
2019-06-07 06:54:27 +00:00
|
|
|
*/
|
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-06 09:31:43 +00:00
|
|
|
$date = $last ? $last->addDay() : Carbon::now();
|
2019-07-02 05:28:27 +00:00
|
|
|
|
|
|
|
return request()->wantsJson() ? $date->format('Y-m-d') : $date;
|
|
|
|
}
|
|
|
|
|
2020-02-06 09:31:43 +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()
|
|
|
|
{
|
2020-02-06 09:31:43 +00:00
|
|
|
switch ($this->recur_schedule) {
|
2019-07-02 05:28:27 +00:00
|
|
|
// Weekly
|
2020-02-06 09:31:43 +00:00
|
|
|
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
|
|
|
|
// NOTE: price_recurr_strict ignored
|
|
|
|
case 5: $date = $this->getInvoiceNextAttribute()->addYear(2)->subDay(); break;
|
|
|
|
|
|
|
|
// Three Yearly
|
|
|
|
// NOTE: price_recurr_strict ignored
|
|
|
|
case 6: $date = $this->getInvoiceNextAttribute()->addYear(3)->subDay(); break;
|
|
|
|
|
|
|
|
default: throw new Exception('Unknown recur_schedule');
|
|
|
|
}
|
|
|
|
|
|
|
|
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_end->diff($this->invoice_next_end->startOfWeek())->days;
|
|
|
|
break;
|
|
|
|
|
2019-07-02 05:28:27 +00:00
|
|
|
// Monthly
|
2020-02-06 09:31:43 +00:00
|
|
|
case 1:
|
|
|
|
$d = $this->invoice_next_end->diff($this->invoice_next_end->startOfMonth())->days;
|
|
|
|
break;
|
|
|
|
|
2019-07-02 05:28:27 +00:00
|
|
|
// Quarterly
|
2020-02-06 09:31:43 +00:00
|
|
|
case 2:
|
|
|
|
$d = $this->invoice_next_end->diff($this->invoice_next_end->startOfQuarter())->days;
|
|
|
|
break;
|
|
|
|
|
2019-07-02 05:28:27 +00:00
|
|
|
// Half Yearly
|
2020-02-06 09:31:43 +00:00
|
|
|
case 3:
|
|
|
|
$d = $this->invoice_next_end->diff($this->invoice_next_end->startOfHalf())->days;
|
|
|
|
break;
|
|
|
|
|
2019-07-02 05:28:27 +00:00
|
|
|
// Yearly
|
2020-02-06 09:31:43 +00:00
|
|
|
case 4:
|
|
|
|
$d = $this->invoice_next_end->diff($this->invoice_next_end->startOfYear())->days;
|
|
|
|
break;
|
|
|
|
|
2019-07-02 05:28:27 +00:00
|
|
|
// Two Yearly
|
2020-02-06 09:31:43 +00:00
|
|
|
case 5:
|
|
|
|
$d = $this->invoice_next_end->diff($this->invoice_next_end->subyear(2))->days-1;
|
|
|
|
break;
|
|
|
|
|
2019-07-02 05:28:27 +00:00
|
|
|
// Three Yearly
|
2020-02-06 09:31:43 +00:00
|
|
|
case 6:
|
|
|
|
$d = $this->invoice_next_end->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
|
|
|
|
2020-02-06 09:31:43 +00:00
|
|
|
// Include the start date and end date.
|
|
|
|
$d += 1;
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
return $this->invoice_items->count() ? $this->invoice_items->last()->date_stop : NULL;
|
2018-11-21 03:37:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 05:28:27 +00:00
|
|
|
public function getNameAttribute(): string
|
|
|
|
{
|
|
|
|
return $this->product->name_short.': '.$this->getNameShortAttribute();
|
|
|
|
}
|
|
|
|
|
2019-06-21 06:21:48 +00:00
|
|
|
/**
|
|
|
|
* Return the short name for the service.
|
|
|
|
*
|
|
|
|
* EG:
|
|
|
|
* For ADSL, this would be the phone number,
|
|
|
|
* For Hosting, this would be the domain name, etc
|
2020-02-05 04:47:24 +00:00
|
|
|
* @deprecated
|
2019-06-21 06:21:48 +00:00
|
|
|
*/
|
2020-01-12 12:42:32 +00:00
|
|
|
public function getNameShortAttribute()
|
2019-06-21 06:21:48 +00:00
|
|
|
{
|
|
|
|
return $this->model ? $this->type->name : 'NAME UNKNOWN';
|
|
|
|
}
|
2019-06-07 06:54:27 +00:00
|
|
|
|
2019-06-21 06:21:48 +00:00
|
|
|
/**
|
|
|
|
* @deprecated see getInvoiceNextAttribute()
|
|
|
|
*/
|
|
|
|
public function getNextInvoiceAttribute()
|
2018-06-05 11:13:57 +00:00
|
|
|
{
|
2019-06-21 06:21:48 +00:00
|
|
|
return $this->getInvoiceNextAttribute();
|
2018-06-19 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 03:40:33 +00:00
|
|
|
/**
|
|
|
|
* This function will present the Order Info Details
|
|
|
|
*/
|
2019-01-24 06:16:51 +00:00
|
|
|
public function getOrderInfoDetailsAttribute(): string
|
2019-01-24 03:40:33 +00:00
|
|
|
{
|
2019-01-24 06:16:51 +00:00
|
|
|
if (! $this->order_info)
|
|
|
|
return '';
|
|
|
|
|
2019-01-24 03:40:33 +00:00
|
|
|
$result = '';
|
2019-01-24 06:16:51 +00:00
|
|
|
|
2019-01-24 03:40:33 +00:00
|
|
|
foreach ($this->order_info as $k=>$v)
|
2019-01-24 09:12:00 +00:00
|
|
|
{
|
|
|
|
if (in_array($k,['order_reference']))
|
|
|
|
continue;
|
|
|
|
|
2019-01-24 03:40:33 +00:00
|
|
|
$result .= sprintf('%s: <b>%s</b><br>',ucfirst($k),$v);
|
2019-01-24 09:12:00 +00:00
|
|
|
}
|
2019-01-24 03:40:33 +00:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-21 06:21:48 +00:00
|
|
|
/**
|
|
|
|
* Get the Product's Category for this service
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function getProductCategoryAttribute(): string
|
2018-05-20 12:53:14 +00:00
|
|
|
{
|
2019-06-21 06:21:48 +00:00
|
|
|
return $this->product->category;
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 06:21:48 +00:00
|
|
|
/**
|
|
|
|
* Get the Product's Short Name for the service
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getProductNameAttribute(): string
|
|
|
|
{
|
|
|
|
return $this->product->name($this->account->language);
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:11:02 +00:00
|
|
|
public function getRecurScheduleAttribute($value): int
|
|
|
|
{
|
|
|
|
// If recur_schedule not set, default to 2
|
|
|
|
return $value ?? 2;
|
|
|
|
}
|
|
|
|
|
2019-06-21 06:21:48 +00:00
|
|
|
/**
|
2020-02-05 04:47:24 +00:00
|
|
|
* @deprecated see getSIDAttribute()
|
2019-06-21 06:21:48 +00:00
|
|
|
*/
|
2020-02-05 04:47:24 +00:00
|
|
|
public function getServiceIdAttribute(): string
|
2018-06-19 12:31:49 +00:00
|
|
|
{
|
2019-06-21 06:21:48 +00:00
|
|
|
return $this->getSIDAttribute();
|
2018-06-19 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 06:21:48 +00:00
|
|
|
/**
|
|
|
|
* @deprecated see getUrlUserAttribute()
|
|
|
|
*/
|
2018-06-19 12:31:49 +00:00
|
|
|
public function getServiceIdUrlAttribute()
|
|
|
|
{
|
2019-06-21 06:21:48 +00:00
|
|
|
return $this->getUrlUserAttribute();
|
2018-06-19 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 06:21:48 +00:00
|
|
|
/**
|
|
|
|
* @deprecated see getServiceIdAttribute()
|
|
|
|
*/
|
2018-05-20 12:53:14 +00:00
|
|
|
public function getServiceNumberAttribute()
|
|
|
|
{
|
2019-06-21 06:21:48 +00:00
|
|
|
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
|
|
|
/**
|
|
|
|
* Services Unique Identifier
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSIDAttribute(): string
|
|
|
|
{
|
|
|
|
return sprintf('%02s-%04s.%05s',$this->site_id,$this->account_id,$this->id);
|
|
|
|
}
|
|
|
|
|
2020-02-05 04:47:24 +00:00
|
|
|
/**
|
|
|
|
* Return the service description.
|
|
|
|
* For:
|
|
|
|
* + Broadband, this is the service address
|
|
|
|
* + Domains, blank
|
|
|
|
* + Hosting, blank
|
|
|
|
* + SSL, blank
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSDescAttribute(): string
|
|
|
|
{
|
2020-02-06 22:11:02 +00:00
|
|
|
return $this->type->service_description ?: 'Service Description NOT Defined for :'.$this->type->type;
|
2020-02-05 04:47:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the service name.
|
|
|
|
* For:
|
|
|
|
* + 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
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSNameAttribute(): string
|
|
|
|
{
|
2020-02-06 22:11:02 +00:00
|
|
|
return $this->type->service_name ?: 'Service Name NOT Defined for :'.$this->type->type;
|
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';
|
2020-02-06 22:11:02 +00:00
|
|
|
default: return $this->type->type;
|
2020-02-05 04:47:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-21 06:21:48 +00:00
|
|
|
/**
|
|
|
|
* Return the Service Status
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getStatusAttribute(): string
|
2018-08-11 05:09:41 +00:00
|
|
|
{
|
2019-01-24 09:12:00 +00:00
|
|
|
if (! $this->order_status)
|
2019-06-21 06:21:48 +00:00
|
|
|
return $this->active ? 'ACTIVE' : 'INACTIVE';
|
|
|
|
else
|
|
|
|
return $this->order_status;
|
|
|
|
}
|
2019-01-24 09:12:00 +00:00
|
|
|
|
2019-06-21 06:21:48 +00:00
|
|
|
/**
|
|
|
|
* 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'])
|
2019-06-21 06:21:48 +00:00
|
|
|
? 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;
|
|
|
|
|
2020-02-06 09:31:43 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-06-21 06:21:48 +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 **/
|
|
|
|
|
2018-08-11 05:09:41 +00:00
|
|
|
public function setDateOrigAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes['date_orig'] = $value->timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setDateLastAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes['date_last'] = $value->timestamp;
|
|
|
|
}
|
|
|
|
|
2019-06-21 06:21:48 +00:00
|
|
|
/** FUNCTIONS **/
|
|
|
|
|
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
|
|
|
|
*/
|
2019-07-04 04:55:05 +00:00
|
|
|
private function addTax(float $value): float
|
2019-06-29 00:14:12 +00:00
|
|
|
{
|
|
|
|
return round($value*1.1,2);
|
|
|
|
}
|
|
|
|
|
2020-02-06 09:31:43 +00:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-21 06:21:48 +00:00
|
|
|
/**
|
|
|
|
* 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));
|
|
|
|
}
|
|
|
|
|
2019-07-04 04:55:05 +00:00
|
|
|
/**
|
|
|
|
* Should this service be invoiced soon
|
|
|
|
*
|
|
|
|
* @todo get the number of days from account setup
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isInvoiceDueSoon($days=30): bool
|
2019-07-02 05:28:27 +00:00
|
|
|
{
|
2019-07-04 04:55:05 +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
|
|
|
|
{
|
2020-02-06 09:31:43 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-06 09:31:43 +00:00
|
|
|
/**
|
|
|
|
* Generate a collection of invoice_item objects that will be billed for the next invoice
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function next_invoice_items(): Collection
|
2019-07-04 04:55:05 +00:00
|
|
|
{
|
|
|
|
$result = collect();
|
2019-07-02 05:28:27 +00:00
|
|
|
|
2019-07-04 04:55:05 +00:00
|
|
|
$o = new InvoiceItem;
|
2020-02-06 09:31:43 +00:00
|
|
|
|
|
|
|
// If the service is active, there will be service charges
|
|
|
|
if ($this->active or $this->isPending()) {
|
|
|
|
$o->active = TRUE;
|
|
|
|
$o->service_id = $this->id;
|
|
|
|
$o->product_id = $this->product_id;
|
|
|
|
$o->item_type = 0;
|
|
|
|
$o->price_base = $this->price ?: $this->product->price($this->recur_schedule); // @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_end;
|
|
|
|
$o->quantity = $this->invoice_next_quantity;
|
|
|
|
|
2020-02-08 11:51:50 +00:00
|
|
|
$o->addTaxes($this->account->country->taxes);
|
2020-02-06 09:31:43 +00:00
|
|
|
$result->push($o);
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:11:02 +00:00
|
|
|
// If pending, add any connection charges
|
|
|
|
if ($this->isPending()) {
|
|
|
|
$o = new InvoiceItem;
|
|
|
|
$o->active = TRUE;
|
|
|
|
$o->service_id = $this->id;
|
|
|
|
$o->product_id = $this->product_id;
|
|
|
|
$o->item_type = 4;
|
|
|
|
$o->price_base = $this->price ?: $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-02-08 11:51:50 +00:00
|
|
|
$o->addTaxes($this->account->country->taxes);
|
2020-02-06 22:11:02 +00:00
|
|
|
$result->push($o);
|
|
|
|
}
|
|
|
|
|
2020-02-06 09:31:43 +00:00
|
|
|
// Add additional charges
|
|
|
|
foreach ($this->charges->filter(function($item) { return ! $item->processed; }) as $oo) {
|
2019-07-04 04:55:05 +00:00
|
|
|
$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;
|
|
|
|
|
2020-02-08 11:51:50 +00:00
|
|
|
$o->addTaxes($this->account->country->taxes);
|
2019-07-04 04:55:05 +00:00
|
|
|
$result->push($o);
|
2019-07-02 05:28:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2019-06-21 06:21:48 +00:00
|
|
|
/**
|
|
|
|
* @todo
|
|
|
|
* @param string $status
|
|
|
|
* @return $this
|
|
|
|
*/
|
2019-01-24 06:16:51 +00:00
|
|
|
public function nextStatus(string $status) {
|
|
|
|
if ($x=$this->validStatus($status))
|
2018-08-23 05:17:26 +00:00
|
|
|
{
|
2019-01-24 06:16:51 +00:00
|
|
|
$this->order_status = $x;
|
|
|
|
$this->save();
|
2018-08-23 05:17:26 +00:00
|
|
|
|
2019-01-24 06:16:51 +00:00
|
|
|
return $this;
|
2018-08-23 05:17:26 +00:00
|
|
|
}
|
2019-01-24 06:16:51 +00:00
|
|
|
|
|
|
|
abort(500,'Next Status not set up for:'.$this->order_status);
|
2018-08-23 05:17:26 +00:00
|
|
|
}
|
|
|
|
|
2018-08-11 05:09:41 +00:00
|
|
|
/**
|
|
|
|
* This function will return the associated service model for the product type
|
2019-06-21 06:21:48 +00:00
|
|
|
* @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
|
|
|
|
|
|
|
/**
|
|
|
|
* Return if the proposed status is valid.
|
|
|
|
*
|
|
|
|
* @param string $status
|
|
|
|
* @return string | NULL
|
|
|
|
*/
|
2019-06-21 06:21:48 +00:00
|
|
|
private function testNextStatusValid(string $status)
|
|
|
|
{
|
|
|
|
return Arr::get(Arr::get($this->valid_status,$this->order_status,[]),$status,NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated use testNextStatusValid()
|
|
|
|
*/
|
2019-01-24 03:40:33 +00:00
|
|
|
public function validStatus(string $status)
|
|
|
|
{
|
2019-06-21 06:21:48 +00:00
|
|
|
return $this->testNextStatusValid($status);
|
2019-01-24 03:40:33 +00:00
|
|
|
}
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|