2018-05-20 12:53:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2020-02-06 09:31:43 +00:00
|
|
|
use Exception;
|
2021-09-29 04:57:25 +00:00
|
|
|
use Illuminate\Database\Eloquent\Casts\AsCollection;
|
2021-06-30 04:00:41 +00:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2018-05-20 12:53:14 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2019-06-21 06:21:48 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2020-02-06 09:31:43 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2020-04-18 22:33:41 +00:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2024-07-07 05:02:02 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2020-04-18 22:33:41 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
2024-07-07 05:02:02 +00:00
|
|
|
use Leenooks\Carbon;
|
|
|
|
use Leenooks\Casts\LeenooksCarbon;
|
2019-06-07 06:54:27 +00:00
|
|
|
|
2023-05-04 12:17:42 +00:00
|
|
|
use App\Models\Product\Type;
|
2021-06-29 06:36:34 +00:00
|
|
|
use App\Interfaces\IDs;
|
2022-04-21 04:41:26 +00:00
|
|
|
use App\Traits\ScopeServiceUserAuthorised;
|
2018-05-20 12:53:14 +00:00
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
/**
|
|
|
|
* Class Service
|
|
|
|
* Services that belong to an account
|
|
|
|
*
|
2023-05-06 03:53:14 +00:00
|
|
|
* So each service attribute has:
|
|
|
|
* - 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/*
|
|
|
|
*
|
2021-06-29 06:36:34 +00:00
|
|
|
* Attributes for services:
|
2022-04-19 07:07:39 +00:00
|
|
|
* + additional_cost : Pending additional charges for this service (excluding setup) //@todo check all these are still valid
|
2022-04-22 00:36:41 +00:00
|
|
|
* + billing_charge : Charge for this service each invoice period // @todo change to "charge"
|
2021-12-24 01:14:01 +00:00
|
|
|
* + billing_interval : The period that this service is billed for by default
|
2022-04-02 07:06:34 +00:00
|
|
|
* + billing_interval_string : The period that this service is billed for by default as a name
|
2022-06-12 08:32:54 +00:00
|
|
|
* + billed_to : When this service has been billed to // @todo rename all references to invoice_to
|
2022-04-22 00:36:41 +00:00
|
|
|
* + category : The type of service this is, eg: broadband, phone
|
2022-06-13 04:21:48 +00:00
|
|
|
* + category_name : The type of service this is, eg: Broadband, Telephone (in human friendly)
|
2022-04-19 07:07:39 +00:00
|
|
|
* + contract_term : The term that this service must be active
|
|
|
|
* + contract_end : The date that the contract ends for this service
|
2021-09-30 02:54:44 +00:00
|
|
|
* + name : Service short name with service address
|
2021-07-13 02:31:56 +00:00
|
|
|
* + name_short : Service Product short name, eg: phone number, domain name, certificate CN
|
2021-09-30 02:54:44 +00:00
|
|
|
* + name_detail : Service Detail, eg: service_address
|
2022-04-22 00:36:41 +00:00
|
|
|
* + product : Our product that is providing this service
|
2021-07-13 02:31:56 +00:00
|
|
|
* + sid : System ID for service
|
2022-04-22 00:36:41 +00:00
|
|
|
* + supplied : The model of the supplier's product used for this service.
|
2021-06-29 06:36:34 +00:00
|
|
|
*
|
2023-05-09 01:09:00 +00:00
|
|
|
* Methods:
|
2024-07-07 05:02:02 +00:00
|
|
|
* + isChargeOverridden : Has the price been overridden?
|
2023-05-09 01:09:00 +00:00
|
|
|
* + isPending : Is this a pending active service
|
|
|
|
*
|
2021-06-29 06:36:34 +00:00
|
|
|
* @package App\Models
|
2023-05-05 12:05:42 +00:00
|
|
|
* @todo Add min_charge
|
2023-05-09 01:09:00 +00:00
|
|
|
* @todo Add charge_orig : The original chargeable price
|
2021-06-29 06:36:34 +00:00
|
|
|
*/
|
|
|
|
class Service extends Model implements IDs
|
2018-05-20 12:53:14 +00:00
|
|
|
{
|
2024-07-05 02:04:08 +00:00
|
|
|
use HasFactory,ScopeServiceUserAuthorised;
|
2018-08-11 05:09:41 +00:00
|
|
|
|
2020-02-13 11:18:20 +00:00
|
|
|
protected $casts = [
|
2024-07-05 02:04:08 +00:00
|
|
|
'order_info' => AsCollection::class,
|
2024-07-07 05:02:02 +00:00
|
|
|
'invoice_next_at' => LeenooksCarbon::class, // @todo Can this be removed, since start_at provides this functionality
|
|
|
|
'stop_at' => LeenooksCarbon::class,
|
|
|
|
'start_at' => LeenooksCarbon::class,
|
2022-06-13 04:21:48 +00:00
|
|
|
];
|
|
|
|
|
2020-02-10 11:07:46 +00:00
|
|
|
protected $with = [
|
2023-05-09 10:28:51 +00:00
|
|
|
'type',
|
2020-02-10 11:07:46 +00:00
|
|
|
];
|
|
|
|
|
2021-07-13 02:31:56 +00:00
|
|
|
public const INACTIVE_STATUS = [
|
|
|
|
'CANCELLED',
|
|
|
|
'ORDER-REJECTED',
|
|
|
|
'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
|
2021-09-29 04:57:25 +00:00
|
|
|
*
|
|
|
|
* The structure of each item is:
|
|
|
|
* <ORDER_STATUS> => [
|
|
|
|
* 'next' == next possible levels, initiated by who (array).
|
|
|
|
* who = 'role','system'
|
|
|
|
* 'enter_method' == the method run when we enter this stage - could be used to send email for example, if this method doesnt complete successfully, we dont enter this stage
|
|
|
|
* + If it returns NULL, An error condition
|
|
|
|
* + If it returns FALSE, the service cannot enter this stage
|
|
|
|
* + If it returns TRUE, the service has successfully changed to this stage
|
|
|
|
* + If it returns a VIEW/REDIRECT, that resulting page will handle the status change
|
|
|
|
* 'exit_method' == the method that determines that the order can leave this status (true = can, false = cant)
|
|
|
|
* + If it returns NULL, An error condition
|
|
|
|
* + If it returns FALSE, the service CANNOT leave this stage
|
|
|
|
* + If it returns TRUE, the service CAN leave this stage
|
|
|
|
* it will receive the next method as an argument
|
|
|
|
* 'title' == title shown on the menu, for the user to choose
|
|
|
|
* ]
|
|
|
|
* So when an order goes to the next state, the exit method will be tested immediately (if there is only 1 exit method for the user)
|
|
|
|
* to see if it can proceed further if not, it'll wait here for user/admin intervention
|
2019-06-21 06:21:48 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2022-06-12 08:32:54 +00:00
|
|
|
* @todo This needs an overhaul, its not implemented correctly.
|
2019-06-21 06:21:48 +00:00
|
|
|
*/
|
2021-09-29 04:57:25 +00:00
|
|
|
private const ACTION_PROGRESS = [
|
|
|
|
// Order Submitted @todo redo
|
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',
|
|
|
|
],
|
2021-09-29 04:57:25 +00:00
|
|
|
// Client accepts order, if performed by RW @todo redo
|
2020-04-18 22:33:41 +00:00
|
|
|
'ORDER-ACCEPT' => [
|
|
|
|
'fail'=>FALSE,
|
|
|
|
'next'=>[
|
|
|
|
'SETUP-PAYMENT-WAIT'=>['customer'],
|
|
|
|
],
|
|
|
|
'system'=>FALSE,
|
|
|
|
'method'=>'action_order_accept',
|
|
|
|
'title'=>'Client Accept Order',
|
|
|
|
],
|
2021-09-29 04:57:25 +00:00
|
|
|
// If the product has a setup, collect payment information @todo redo
|
2020-04-18 22:33:41 +00:00
|
|
|
'SETUP-PAYMENT-WAIT' => [
|
|
|
|
'fail'=>FALSE,
|
|
|
|
'next'=>[
|
|
|
|
'PAYMENT-WAIT'=>['customer'],
|
|
|
|
],
|
|
|
|
'system'=>FALSE,
|
|
|
|
'method'=>'action_setup_payment_wait',
|
|
|
|
'title'=>'Setup Payment',
|
|
|
|
],
|
2021-09-29 04:57:25 +00:00
|
|
|
// @todo redo
|
2020-04-18 22:33:41 +00:00
|
|
|
'PAYMENT-WAIT' => [
|
|
|
|
'fail'=>FALSE,
|
|
|
|
'next'=>[
|
|
|
|
'PAYMENT-CHECK'=>['reseller','wholesaler'],
|
|
|
|
],
|
|
|
|
'system'=>FALSE,
|
|
|
|
'method'=>'action_payment_wait',
|
|
|
|
'title'=>'Service Payment',
|
|
|
|
],
|
2021-09-29 04:57:25 +00:00
|
|
|
// @todo redo
|
2020-04-18 22:33:41 +00:00
|
|
|
'PAYMENT-CHECK' => [
|
|
|
|
'fail'=>'ORDER-HOLD',
|
|
|
|
'next'=>[
|
2022-02-01 05:40:46 +00:00
|
|
|
'ORDER-SENT'=>['wholesaler'],
|
2020-04-18 22:33:41 +00:00
|
|
|
],
|
|
|
|
'system'=>TRUE,
|
|
|
|
'method'=>'action_payment_check',
|
|
|
|
'title'=>'Validate Payment Method',
|
|
|
|
],
|
2021-09-29 04:57:25 +00:00
|
|
|
// Order On Hold (Reason) @todo redo
|
2019-01-24 11:03:43 +00:00
|
|
|
'ORDER-HOLD' => ['release'=>'ORDER-SUBMIT','update_reference'=>'ORDER-SENT'],
|
2021-09-29 04:57:25 +00:00
|
|
|
// Order Rejected (Reason) @todo redo
|
2019-01-24 10:49:56 +00:00
|
|
|
'ORDER-REJECTED' => [],
|
2021-09-29 04:57:25 +00:00
|
|
|
// Order Cancelled @todo redo
|
2019-01-24 10:49:56 +00:00
|
|
|
'ORDER-CANCELLED' => [],
|
2021-09-29 04:57:25 +00:00
|
|
|
// Order Sent to Supplier @todo redo
|
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',
|
|
|
|
],
|
2021-09-29 04:57:25 +00:00
|
|
|
// Order Confirmed by Supplier @todo redo
|
2020-04-23 07:38:09 +00:00
|
|
|
'ORDERED' => [
|
|
|
|
'fail'=>false,
|
|
|
|
'next'=>[
|
|
|
|
'PROVISION-HOLD'=>['wholesaler'],
|
|
|
|
'PROVISION-PLANNED'=>['wholesaler'],
|
|
|
|
'PROVISIONED'=>['wholesaler'],
|
|
|
|
],
|
|
|
|
'system'=>FALSE,
|
|
|
|
'method'=>'action_ordered',
|
|
|
|
'title'=>'Service Ordered',
|
|
|
|
],
|
2021-09-29 04:57:25 +00:00
|
|
|
// Service confirmed by supplier, optional connection date @todo redo
|
2020-04-23 07:38:09 +00:00
|
|
|
'PROVISION-PLANNED' => [
|
|
|
|
'fail'=>false,
|
|
|
|
'next'=>[
|
|
|
|
'PROVISIONED'=>['wholesaler'],
|
|
|
|
],
|
|
|
|
'system'=>FALSE,
|
|
|
|
'method'=>'action_provision_planned',
|
|
|
|
'title'=>'Provision Planned',
|
|
|
|
],
|
2021-09-29 04:57:25 +00:00
|
|
|
// Service has been provisioned by supplier @todo redo
|
2020-04-23 07:38:09 +00:00
|
|
|
'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' => [
|
|
|
|
'next'=>[
|
|
|
|
'CANCEL-REQUEST'=>['customer'],
|
2021-09-29 04:57:25 +00:00
|
|
|
'CHANGE-REQUEST'=>['customer'],
|
2020-04-22 12:54:05 +00:00
|
|
|
],
|
2021-09-29 04:57:25 +00:00
|
|
|
'exit'=>'action_active_exit',
|
2020-04-22 12:54:05 +00:00
|
|
|
'title'=>'Service Active',
|
|
|
|
],
|
2020-04-23 07:38:09 +00:00
|
|
|
// Service to be Upgraded
|
2021-09-29 04:57:25 +00:00
|
|
|
'CANCEL-CANCEL' => [
|
2020-04-22 12:54:05 +00:00
|
|
|
'next'=>[
|
2021-09-29 04:57:25 +00:00
|
|
|
'ACTIVE'=>['wholesaler'],
|
2020-04-22 12:54:05 +00:00
|
|
|
],
|
2021-09-29 04:57:25 +00:00
|
|
|
'enter_method'=>'action_cancel_cancel',
|
|
|
|
'title'=>'Cancel Cancellation Request',
|
2020-04-22 12:54:05 +00:00
|
|
|
],
|
2020-04-23 07:38:09 +00:00
|
|
|
// Service to be Cancelled
|
2020-04-22 12:54:05 +00:00
|
|
|
'CANCEL-REQUEST' => [
|
|
|
|
'next'=>[
|
2021-09-29 04:57:25 +00:00
|
|
|
'CANCEL-CANCEL'=>['wholesaler'],
|
|
|
|
'CANCEL-PENDING'=>['wholesaler'],
|
2020-04-22 12:54:05 +00:00
|
|
|
],
|
2021-09-29 04:57:25 +00:00
|
|
|
'enter_method'=>'action_request_enter_redirect',
|
|
|
|
'exit_method'=>'action_cancel_request_exit',
|
|
|
|
'title'=>'Cancel Request',
|
|
|
|
],
|
|
|
|
// Service Cancellation being processed
|
|
|
|
'CANCEL-PENDING' => [
|
|
|
|
'next'=>[
|
|
|
|
'CANCELLED'=>['wholesaler'],
|
|
|
|
],
|
|
|
|
'enter_method'=>'action_cancel_pending_enter',
|
|
|
|
'exit_method'=>'action_cancel_pending_exit',
|
|
|
|
'title'=>'Cancel Pending',
|
|
|
|
],
|
2022-08-03 05:47:09 +00:00
|
|
|
'CANCELLED'=> [
|
|
|
|
'title'=>'Service Cancelled',
|
|
|
|
'enter_method'=>'action_cancelled',
|
|
|
|
],
|
2021-09-29 04:57:25 +00:00
|
|
|
// Service to be Upgraded
|
|
|
|
'CHANGE-CANCEL' => [
|
|
|
|
'next'=>[
|
|
|
|
'ACTIVE'=>['wholesaler'],
|
|
|
|
],
|
|
|
|
'enter_method'=>'action_change_cancel',
|
|
|
|
'title'=>'Cancel Change Request',
|
|
|
|
],
|
|
|
|
// Service to be Upgraded
|
|
|
|
'CHANGE-REQUEST' => [
|
|
|
|
'next'=>[
|
|
|
|
'CHANGE-PENDING'=>['wholesaler'],
|
|
|
|
'CHANGE-CANCEL'=>['wholesaler'],
|
|
|
|
],
|
|
|
|
'enter_method'=>'action_request_enter_redirect',
|
|
|
|
'title'=>'Change Service',
|
2020-04-22 12:54:05 +00:00
|
|
|
],
|
2022-08-03 05:47:09 +00:00
|
|
|
// Service is waiting on a supplier to activate a change
|
2022-06-12 08:32:54 +00:00
|
|
|
'CHANGE-PENDING' => [
|
|
|
|
'next'=>[
|
|
|
|
'ACTIVE'=>['wholesaler'],
|
|
|
|
],
|
|
|
|
'enter_method'=>'action_request_enter_redirect',
|
|
|
|
'title'=>'Activate Change',
|
|
|
|
],
|
2018-08-11 05:09:41 +00:00
|
|
|
];
|
|
|
|
|
2024-07-07 05:02:02 +00:00
|
|
|
/* STATIC */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of services that are being changed
|
|
|
|
*
|
|
|
|
* Services are being changed, when they are active, but their order status is not active
|
|
|
|
*
|
|
|
|
* @param User $uo
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public static function movements(User $uo): Collection
|
|
|
|
{
|
|
|
|
return (new self)
|
|
|
|
->active()
|
|
|
|
->serviceUserAuthorised($uo)
|
|
|
|
->where('order_status','!=','ACTIVE')
|
|
|
|
->with(['account','product'])
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
|
2022-04-22 00:36:41 +00:00
|
|
|
/* INTERFACES */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Service Local ID
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLIDattribute(): string
|
|
|
|
{
|
|
|
|
return sprintf('%05s',$this->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Services System ID
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSIDAttribute(): string
|
|
|
|
{
|
|
|
|
return sprintf('%02s-%04s.%s',$this->site_id,$this->account_id,$this->getLIDattribute());
|
|
|
|
}
|
|
|
|
|
2021-02-17 13:22:50 +00:00
|
|
|
/* RELATIONS */
|
|
|
|
|
2019-01-24 10:49:56 +00:00
|
|
|
/**
|
|
|
|
* Account the service belongs to
|
|
|
|
*/
|
2018-05-20 12:53:14 +00:00
|
|
|
public function account()
|
|
|
|
{
|
2024-07-05 02:04:08 +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
|
|
|
|
*/
|
|
|
|
public function billing()
|
|
|
|
{
|
|
|
|
return $this->hasOne(AccountBilling::class);
|
|
|
|
}
|
|
|
|
|
2019-07-02 05:28:27 +00:00
|
|
|
/**
|
|
|
|
* Return Charges associated with this Service
|
|
|
|
*/
|
|
|
|
public function charges()
|
|
|
|
{
|
2024-07-05 02:04:08 +00:00
|
|
|
return $this->hasMany(Charge::class)
|
2022-06-11 07:43:09 +00:00
|
|
|
->orderBy('created_at');
|
2019-07-02 05:28:27 +00:00
|
|
|
}
|
|
|
|
|
2024-07-05 02:04:08 +00:00
|
|
|
/**
|
|
|
|
* Return only the active charges
|
|
|
|
*/
|
|
|
|
public function charges_active()
|
|
|
|
{
|
|
|
|
return $this->charges()
|
|
|
|
->active();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return only the charges not yet processed
|
|
|
|
*/
|
|
|
|
public function charges_pending()
|
|
|
|
{
|
|
|
|
return $this->charges()
|
|
|
|
->pending();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Product changes for this service
|
|
|
|
*/
|
2023-05-06 03:53:14 +00:00
|
|
|
public function changes()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Product::class,'service__change','service_id','product_id','id','id')
|
|
|
|
->where('service__change.site_id',$this->site_id)
|
|
|
|
->withPivot(['ordered_at','effective_at','ordered_by','active','complete','notes'])
|
|
|
|
->withTimestamps();
|
|
|
|
}
|
|
|
|
|
2024-07-05 02:04:08 +00:00
|
|
|
/**
|
|
|
|
* @deprecated use invoiced_items
|
|
|
|
*/
|
2019-06-29 00:14:12 +00:00
|
|
|
public function invoice_items($active=TRUE)
|
|
|
|
{
|
2024-07-05 02:04:08 +00:00
|
|
|
return $this->invoiced_items_active();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoices that this service is itemised on
|
|
|
|
*/
|
|
|
|
public function invoiced_items()
|
|
|
|
{
|
|
|
|
return $this->hasMany(InvoiceItem::class)
|
|
|
|
->with(['taxes']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoices that this service is itemised on that is active
|
|
|
|
*/
|
|
|
|
public function invoiced_items_active()
|
|
|
|
{
|
|
|
|
return $this->invoiced_items()
|
|
|
|
->where('active',TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the extra charged items for this service (ie: item_type != 0)
|
|
|
|
*/
|
|
|
|
public function invoiced_extra_items()
|
|
|
|
{
|
|
|
|
return $this->hasMany(InvoiceItem::class)
|
|
|
|
->where('item_type','<>',0)
|
2024-07-07 05:02:02 +00:00
|
|
|
->orderBy('start_at','desc');
|
2024-07-05 02:04:08 +00:00
|
|
|
}
|
2019-06-29 00:14:12 +00:00
|
|
|
|
2024-07-05 02:04:08 +00:00
|
|
|
/**
|
|
|
|
* Return active extra items charged
|
|
|
|
*/
|
|
|
|
public function invoiced_extra_items_active()
|
|
|
|
{
|
|
|
|
return $this->invoiced_extra_items()
|
|
|
|
->where('active',TRUE);
|
|
|
|
}
|
2019-06-29 00:14:12 +00:00
|
|
|
|
2024-07-05 02:04:08 +00:00
|
|
|
/**
|
|
|
|
* Return the service charged items for this service (ie: item_type == 0)
|
|
|
|
*/
|
|
|
|
public function invoiced_service_items()
|
|
|
|
{
|
|
|
|
return $this->hasMany(InvoiceItem::class)
|
|
|
|
->where('item_type','=',0)
|
2024-07-07 05:02:02 +00:00
|
|
|
->orderBy('start_at','desc');
|
2019-06-29 00:14:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-07-05 02:04:08 +00:00
|
|
|
* Return active service items charged
|
2019-06-29 00:14:12 +00:00
|
|
|
*/
|
2024-07-05 02:04:08 +00:00
|
|
|
public function invoiced_service_items_active()
|
|
|
|
{
|
|
|
|
return $this->invoiced_service_items()
|
|
|
|
->where('active',TRUE);
|
2019-06-29 00:14:12 +00:00
|
|
|
}
|
|
|
|
|
2024-07-07 05:02:02 +00:00
|
|
|
public function invoiced_service_items_active_recent()
|
|
|
|
{
|
|
|
|
return $this->invoiced_service_items_active()
|
|
|
|
->limit(10);
|
|
|
|
}
|
|
|
|
|
2019-01-24 10:49:56 +00:00
|
|
|
/**
|
2024-07-05 02:04:08 +00:00
|
|
|
* User that ordered the service
|
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
|
|
|
{
|
2024-07-05 02:04:08 +00:00
|
|
|
return $this->belongsTo(User::class);
|
2018-09-18 02:37:04 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2019-06-29 00:14:12 +00:00
|
|
|
public function product()
|
2018-09-18 02:37:04 +00:00
|
|
|
{
|
2024-07-05 02:04:08 +00:00
|
|
|
return $this->belongsTo(Product::class);
|
2018-09-18 02:37:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 06:21:48 +00:00
|
|
|
/**
|
|
|
|
* Return a child model with details of the service
|
|
|
|
*/
|
2019-06-07 06:54:27 +00:00
|
|
|
public function type()
|
2018-06-19 12:31:49 +00:00
|
|
|
{
|
2022-09-29 07:26:03 +00:00
|
|
|
return $this->morphTo(null,'model','id','service_id');
|
2018-06-19 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 13:22:50 +00:00
|
|
|
/* SCOPES */
|
2019-06-21 06:21:48 +00:00
|
|
|
|
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
|
|
|
{
|
2024-07-05 02:04:08 +00:00
|
|
|
return $query->where(
|
|
|
|
fn($query)=>
|
|
|
|
$query->where($this->getTable().'.active',TRUE)
|
|
|
|
->orWhereNotIn('order_status',self::INACTIVE_STATUS)
|
|
|
|
);
|
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)
|
|
|
|
{
|
2024-07-05 02:04:08 +00:00
|
|
|
return $query->where(
|
|
|
|
fn($query)=>
|
|
|
|
$query->where($this->getTable().'.active',FALSE)
|
|
|
|
->orWhereIn('order_status',self::INACTIVE_STATUS)
|
|
|
|
);
|
2019-07-04 04:55:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-12 12:42:32 +00:00
|
|
|
/**
|
|
|
|
* Search for a record
|
|
|
|
*
|
|
|
|
* @param $query
|
|
|
|
* @param string $term
|
2022-04-22 00:36:41 +00:00
|
|
|
* @return mixed
|
2020-01-12 12:42:32 +00:00
|
|
|
*/
|
|
|
|
public function scopeSearch($query,string $term)
|
|
|
|
{
|
2022-06-14 06:51:18 +00:00
|
|
|
$t = '%'.$term.'%';
|
|
|
|
|
|
|
|
return $query->select('services.*')
|
|
|
|
->where('services.id','like',$t)
|
|
|
|
->leftJoin('service_broadband',['service_broadband.service_id'=>'services.id'])
|
2024-07-06 12:56:51 +00:00
|
|
|
->orWhere('service_broadband.service_number','ilike',$t)
|
|
|
|
->orWhere('service_broadband.service_address','ilike',$t)
|
|
|
|
->orWhere('service_broadband.ipaddress','ilike',$t)
|
2022-06-14 06:51:18 +00:00
|
|
|
->leftJoin('service_phone',['service_phone.service_id'=>'services.id'])
|
2024-07-06 12:56:51 +00:00
|
|
|
->orWhere('service_phone.service_number','ilike',$t)
|
|
|
|
->orWhere('service_phone.service_address','ilike',$t)
|
2022-06-14 06:51:18 +00:00
|
|
|
->leftJoin('service_domain',['service_domain.service_id'=>'services.id'])
|
2024-07-06 12:56:51 +00:00
|
|
|
->orWhere('service_domain.domain_name','ilike',$t)
|
2022-06-14 06:51:18 +00:00
|
|
|
->leftJoin('service_email',['service_email.service_id'=>'services.id'])
|
2024-07-06 12:56:51 +00:00
|
|
|
->orWhere('service_email.domain_name','ilike',$t)
|
2022-06-14 06:51:18 +00:00
|
|
|
->leftJoin('service_host',['service_host.service_id'=>'services.id'])
|
2024-07-06 12:56:51 +00:00
|
|
|
->orWhere('service_host.domain_name','ilike',$t);
|
2020-01-12 12:42:32 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 13:22:50 +00:00
|
|
|
/* ATTRIBUTES */
|
2019-06-21 06:21:48 +00:00
|
|
|
|
2018-06-19 12:31:49 +00:00
|
|
|
/**
|
2022-04-22 00:36:41 +00:00
|
|
|
* How much do we charge for this service, base on the current recur schedule
|
2024-07-07 05:02:02 +00:00
|
|
|
* price in the DB overrides the base price used
|
2020-02-05 04:47:24 +00:00
|
|
|
*
|
2022-04-22 00:36:41 +00:00
|
|
|
* @return float
|
2020-02-05 04:47:24 +00:00
|
|
|
*/
|
2022-04-22 00:36:41 +00:00
|
|
|
public function getBillingChargeAttribute(): float
|
2019-06-29 00:14:12 +00:00
|
|
|
{
|
2024-07-07 05:02:02 +00:00
|
|
|
// If recur_schedule is null, then we only bill this item once
|
|
|
|
if (is_null($this->recur_schedule) && $this->getInvoiceToAttribute())
|
|
|
|
$this->price = 0;
|
2019-07-04 04:55:05 +00:00
|
|
|
|
2024-07-07 05:02:02 +00:00
|
|
|
return $this->account->taxed(
|
|
|
|
is_null($this->price)
|
|
|
|
? $this->product->getBaseChargeAttribute($this->recur_schedule,$this->account->group)
|
|
|
|
: $this->price
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine a monthly price for a service, even if it is billed at a different frequency
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function getBillingChargeNormalisedAttribute(): float
|
|
|
|
{
|
|
|
|
return number_format($this->getBillingChargeAttribute()*Invoice::billing_change($this->recur_schedule,$this->offering->billing_interval),2);
|
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
|
|
|
*
|
2021-12-24 01:14:01 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getBillingIntervalAttribute(): int
|
|
|
|
{
|
|
|
|
return $this->recur_schedule ?: $this->product->getBillingIntervalAttribute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a human friendly name for the billing interval
|
|
|
|
*
|
2019-06-07 06:54:27 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2021-12-24 01:14:01 +00:00
|
|
|
public function getBillingIntervalStringAttribute(): string
|
|
|
|
{
|
|
|
|
return Invoice::billing_name($this->getBillingIntervalAttribute());
|
|
|
|
}
|
|
|
|
|
2022-04-22 00:36:41 +00:00
|
|
|
/**
|
|
|
|
* Determine a monthly price for a service, even if it is billed at a different frequency
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
* @throws Exception
|
2024-07-07 05:02:02 +00:00
|
|
|
* @deprecated use class::billing_charge_normalised()
|
2022-04-22 00:36:41 +00:00
|
|
|
*/
|
|
|
|
public function getBillingMonthlyPriceAttribute(): float
|
|
|
|
{
|
2024-07-07 05:02:02 +00:00
|
|
|
Log::alert('SMO:! Deprecated function getBillingMonthlyPriceAttribute()');
|
|
|
|
return $this->getBillingChargeNormalisedAttribute();
|
2022-04-22 00:36:41 +00:00
|
|
|
}
|
|
|
|
|
2024-07-07 05:02:02 +00:00
|
|
|
/**
|
|
|
|
* Service Category ID
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-06-13 04:21:48 +00:00
|
|
|
public function getCategoryAttribute(): string
|
|
|
|
{
|
|
|
|
return $this->product->category;
|
|
|
|
}
|
|
|
|
|
2024-07-07 05:02:02 +00:00
|
|
|
/**
|
|
|
|
* Service Category Name
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-06-13 04:21:48 +00:00
|
|
|
public function getCategoryNameAttribute(): string
|
|
|
|
{
|
|
|
|
return $this->product->category_name;
|
|
|
|
}
|
|
|
|
|
2022-04-19 07:07:39 +00:00
|
|
|
/**
|
|
|
|
* The date the contract ends
|
|
|
|
*
|
|
|
|
* Service contracts end the later of the start_date + contract_term or the expire date.
|
|
|
|
*
|
2024-07-07 05:02:02 +00:00
|
|
|
* @return Carbon|null
|
2022-04-19 07:07:39 +00:00
|
|
|
*/
|
2022-04-22 01:47:46 +00:00
|
|
|
public function getContractEndAttribute(): ?Carbon
|
2022-04-19 07:07:39 +00:00
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2021-12-24 01:14:01 +00:00
|
|
|
/**
|
|
|
|
* This function will determine the minimum contract term for a service, which is the maximum of
|
2023-05-05 12:05:42 +00:00
|
|
|
* supplied->contract_term, or the product->type->contract_term;
|
2021-12-24 01:14:01 +00:00
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getContractTermAttribute(): int
|
2018-06-05 11:13:57 +00:00
|
|
|
{
|
2023-05-05 12:05:42 +00:00
|
|
|
return max($this->supplied->contract_term,$this->product->type->contract_term);
|
2018-06-05 11:13:57 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 06:54:27 +00:00
|
|
|
/**
|
|
|
|
* Return the date for the next invoice
|
|
|
|
*
|
2024-07-07 05:02:02 +00:00
|
|
|
* In priority order, the next is either
|
|
|
|
* + The next day after it was last invoiced to (stop_at)
|
|
|
|
* + The earlier of invoice_next_at and start_at dates
|
|
|
|
* + Today
|
|
|
|
*
|
|
|
|
* @return Carbon
|
2019-06-07 06:54:27 +00:00
|
|
|
*/
|
2024-07-07 05:02:02 +00:00
|
|
|
public function getInvoiceNextAttribute(): Carbon
|
2018-11-21 03:37:17 +00:00
|
|
|
{
|
2019-06-29 00:14:12 +00:00
|
|
|
$last = $this->getInvoiceToAttribute();
|
2024-07-07 05:02:02 +00:00
|
|
|
|
2022-04-22 00:36:41 +00:00
|
|
|
return $last
|
2020-02-12 10:32:57 +00:00
|
|
|
? $last->addDay()
|
2024-07-07 05:02:02 +00:00
|
|
|
: (min($this->start_at,$this->invoice_next_at) ?: Carbon::now());
|
2019-07-02 05:28:27 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 09:31:43 +00:00
|
|
|
/**
|
|
|
|
* Return the end date for the next invoice
|
|
|
|
*
|
2024-07-07 05:02:02 +00:00
|
|
|
* @return Carbon
|
2020-02-06 09:31:43 +00:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
2024-07-07 05:02:02 +00:00
|
|
|
public function getInvoiceNextEndAttribute(): Carbon
|
2019-07-02 05:28:27 +00:00
|
|
|
{
|
2020-02-06 09:31:43 +00:00
|
|
|
switch ($this->recur_schedule) {
|
2022-04-22 00:36:41 +00:00
|
|
|
case Invoice::BILL_WEEKLY:
|
|
|
|
$date = $this->product->price_recur_strict
|
|
|
|
? $this->getInvoiceNextAttribute()->endOfWeek()
|
|
|
|
: $this->getInvoiceNextAttribute()->addWeek()->subDay();
|
2020-02-06 09:31:43 +00:00
|
|
|
break;
|
|
|
|
|
2022-04-22 00:36:41 +00:00
|
|
|
case Invoice::BILL_MONTHLY:
|
2021-12-24 01:14:01 +00:00
|
|
|
$date = $this->product->price_recur_strict
|
2020-02-06 09:31:43 +00:00
|
|
|
? $this->getInvoiceNextAttribute()->endOfMonth()
|
|
|
|
: $this->getInvoiceNextAttribute()->addMonth()->subDay();
|
|
|
|
break;
|
|
|
|
|
2022-04-22 00:36:41 +00:00
|
|
|
case Invoice::BILL_QUARTERLY:
|
2021-12-24 01:14:01 +00:00
|
|
|
$date = $this->product->price_recur_strict
|
2020-02-06 09:31:43 +00:00
|
|
|
? $this->getInvoiceNextAttribute()->endOfQuarter()
|
|
|
|
: $this->getInvoiceNextAttribute()->addQuarter()->subDay();
|
|
|
|
break;
|
|
|
|
|
2022-04-22 00:36:41 +00:00
|
|
|
case Invoice::BILL_SEMI_YEARLY:
|
2021-12-24 01:14:01 +00:00
|
|
|
$date = $this->product->price_recur_strict
|
2020-02-06 09:31:43 +00:00
|
|
|
? $this->getInvoiceNextAttribute()->endOfHalf()
|
2024-07-07 05:02:02 +00:00
|
|
|
: $this->getInvoiceNextAttribute()->addQuarters(2)->subDay();
|
2020-02-06 09:31:43 +00:00
|
|
|
break;
|
|
|
|
|
2022-04-22 00:36:41 +00:00
|
|
|
case Invoice::BILL_YEARLY:
|
2021-12-24 01:14:01 +00:00
|
|
|
$date = $this->product->price_recur_strict
|
2020-02-06 09:31:43 +00:00
|
|
|
? $this->getInvoiceNextAttribute()->endOfYear()
|
|
|
|
: $this->getInvoiceNextAttribute()->addYear()->subDay();
|
|
|
|
break;
|
|
|
|
|
2022-04-22 00:36:41 +00:00
|
|
|
case Invoice::BILL_TWOYEARS:
|
|
|
|
if (! $this->product->price_recur_strict) {
|
2024-07-07 05:02:02 +00:00
|
|
|
$date = $this->getInvoiceNextAttribute()->addYears(2)->subDay();
|
2022-04-22 00:36:41 +00:00
|
|
|
|
|
|
|
} else {
|
2024-07-07 05:02:02 +00:00
|
|
|
$date = $this->getInvoiceNextAttribute()->addYears(2)->subDay()->endOfYear();
|
2021-01-04 12:18:57 +00:00
|
|
|
|
2024-07-07 05:02:02 +00:00
|
|
|
// Make sure we end on an even year
|
2022-04-22 00:36:41 +00:00
|
|
|
if ($date->clone()->addDay()->year%2)
|
2021-01-04 12:18:57 +00:00
|
|
|
$date = $date->subYear();
|
|
|
|
}
|
|
|
|
break;
|
2020-02-06 09:31:43 +00:00
|
|
|
|
2021-12-24 01:14:01 +00:00
|
|
|
// NOTE: price_recur_strict ignored
|
2022-04-22 00:36:41 +00:00
|
|
|
case Invoice::BILL_THREEYEARS:
|
2024-07-07 05:02:02 +00:00
|
|
|
$date = $this->getInvoiceNextAttribute()->addYears(3)->subDay();
|
2022-04-22 00:36:41 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// NOTE: price_recur_strict ignored
|
|
|
|
case Invoice::BILL_FOURYEARS:
|
2024-07-07 05:02:02 +00:00
|
|
|
$date = $this->getInvoiceNextAttribute()->addYears(4)->subDay();
|
2022-04-22 00:36:41 +00:00
|
|
|
break;
|
2020-02-06 09:31:43 +00:00
|
|
|
|
2022-04-22 00:36:41 +00:00
|
|
|
// NOTE: price_recur_strict ignored
|
|
|
|
case Invoice::BILL_FIVEYEARS:
|
2024-07-07 05:02:02 +00:00
|
|
|
$date = $this->getInvoiceNextAttribute()->addYears(5)->subDay();
|
2022-04-22 00:36:41 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Exception('Unknown recur_schedule');
|
2020-02-06 09:31:43 +00:00
|
|
|
}
|
|
|
|
|
2024-07-07 05:02:02 +00:00
|
|
|
// If the invoice has an end date, our invoice period shouldnt be greater than that (might be terminating).
|
|
|
|
if ($this->stop_at && ($this->stop_at < $date))
|
2022-05-11 23:09:49 +00:00
|
|
|
$date = $this->stop_at;
|
2020-04-22 12:54:05 +00:00
|
|
|
|
2020-02-06 09:31:43 +00:00
|
|
|
return $date;
|
|
|
|
}
|
|
|
|
|
2022-04-22 00:36:41 +00:00
|
|
|
/**
|
2022-04-22 04:41:18 +00:00
|
|
|
* Determine how much quantity (at the charge rate) is required for the next invoice
|
2022-04-22 00:36:41 +00:00
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function getInvoiceNextQuantityAttribute(): float
|
2020-02-06 09:31:43 +00:00
|
|
|
{
|
|
|
|
// If we are not rounding to the first day of the cycle, then it is always a full cycle
|
2021-12-24 01:14:01 +00:00
|
|
|
if (! $this->product->price_recur_strict)
|
2020-02-06 09:31:43 +00:00
|
|
|
return 1;
|
|
|
|
|
2024-07-07 05:02:02 +00:00
|
|
|
$n = $this->invoice_next->diffInDays($this->invoice_next_end);
|
2020-02-06 09:31:43 +00:00
|
|
|
|
|
|
|
switch ($this->recur_schedule) {
|
2022-04-22 00:36:41 +00:00
|
|
|
case Invoice::BILL_WEEKLY:
|
2024-07-07 05:02:02 +00:00
|
|
|
$d = $this->invoice_next_end->addWeek()->startOfWeek()->diffInDays($this->invoice_next->startOfWeek());
|
2020-02-06 09:31:43 +00:00
|
|
|
break;
|
|
|
|
|
2022-04-22 00:36:41 +00:00
|
|
|
case Invoice::BILL_MONTHLY:
|
2024-07-07 05:02:02 +00:00
|
|
|
$d = $this->invoice_next_end->addMonth()->startOfMonth()->diffInDays($this->invoice_next->startOfMonth());
|
2020-02-06 09:31:43 +00:00
|
|
|
break;
|
|
|
|
|
2022-04-22 00:36:41 +00:00
|
|
|
case Invoice::BILL_QUARTERLY:
|
2024-07-07 05:02:02 +00:00
|
|
|
$d = $this->invoice_next_end->startOfQuarter()->diffInDays($this->invoice_next->addQuarter()->startOfQuarter());
|
2020-02-06 09:31:43 +00:00
|
|
|
break;
|
|
|
|
|
2022-04-22 00:36:41 +00:00
|
|
|
case Invoice::BILL_SEMI_YEARLY:
|
2024-07-07 05:02:02 +00:00
|
|
|
$d = $this->invoice_next_end->addQuarter(2)->startOfHalf()->diffInDays($this->invoice_next->startOfHalf());
|
2020-02-06 09:31:43 +00:00
|
|
|
break;
|
|
|
|
|
2022-04-22 00:36:41 +00:00
|
|
|
case Invoice::BILL_YEARLY:
|
2024-07-07 05:02:02 +00:00
|
|
|
$d = $this->invoice_next_end->addYear()->startOfYear()->diffInDays($this->invoice_next->startOfYear());
|
2020-02-06 09:31:43 +00:00
|
|
|
break;
|
|
|
|
|
2022-04-22 00:36:41 +00:00
|
|
|
case Invoice::BILL_TWOYEARS:
|
2024-07-07 05:02:02 +00:00
|
|
|
$d = $this->invoice_next_end->addYear(2)->startOfYear()->diffInDays($this->invoice_next->subyear(2))-1;
|
2020-02-06 09:31:43 +00:00
|
|
|
break;
|
|
|
|
|
2022-04-22 00:36:41 +00:00
|
|
|
case Invoice::BILL_THREEYEARS:
|
2024-07-07 05:02:02 +00:00
|
|
|
$d = $this->invoice_next_end->addYear(3)->startOfYear()->diffInDays($this->invoice_next->subyear(3))-1;
|
2020-02-06 09:31:43 +00:00
|
|
|
break;
|
|
|
|
|
2022-04-22 00:36:41 +00:00
|
|
|
case Invoice::BILL_FOURYEARS:
|
2024-07-07 05:02:02 +00:00
|
|
|
$d = $this->invoice_next_end->addYear(3)->startOfYear()->diffInDays($this->invoice_next->subyear(4))-1;
|
2022-04-22 00:36:41 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Invoice::BILL_FIVEYEARS:
|
2024-07-07 05:02:02 +00:00
|
|
|
$d = $this->invoice_next_end->addYear(3)->startOfYear()->diffInDays($this->invoice_next->subyear(5))-1;
|
2022-04-22 00:36:41 +00:00
|
|
|
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
|
|
|
return round($n/$d,2);
|
2019-06-29 00:14:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the date that the service has been invoiced to
|
2022-04-22 01:47:46 +00:00
|
|
|
*
|
2024-07-07 05:02:02 +00:00
|
|
|
* @return Carbon|null
|
2019-06-29 00:14:12 +00:00
|
|
|
*/
|
2024-07-07 05:02:02 +00:00
|
|
|
public function getInvoiceToAttribute(): ?Carbon
|
2019-06-29 00:14:12 +00:00
|
|
|
{
|
2024-07-07 05:02:02 +00:00
|
|
|
return ($x=$this->invoiced_service_items_active_recent)->count()
|
|
|
|
? $x->first()->stop_at
|
2020-04-01 12:35:06 +00:00
|
|
|
: NULL;
|
2018-11-21 03:37:17 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 06:36:34 +00:00
|
|
|
/**
|
2022-04-22 00:36:41 +00:00
|
|
|
* The full name for a service, comprised of the short name and the description
|
2021-06-29 06:36:34 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-07-02 05:28:27 +00:00
|
|
|
public function getNameAttribute(): string
|
|
|
|
{
|
2021-09-30 02:54:44 +00:00
|
|
|
return $this->getNameShortAttribute().(($x=$this->getNameDetailAttribute()) ? ': '.$x : '');
|
2019-07-02 05:28:27 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 06:21:48 +00:00
|
|
|
/**
|
|
|
|
* 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
|
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
|
|
|
{
|
2023-05-03 13:41:48 +00:00
|
|
|
return $this->type->getServiceNameAttribute() ? $this->type->getServiceNameAttribute() : 'SID:'.$this->sid;
|
2021-09-30 02:54:44 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 06:21:48 +00:00
|
|
|
/**
|
2022-04-22 00:36:41 +00:00
|
|
|
* Return the service description.
|
|
|
|
* For:
|
|
|
|
* + Broadband, this is the service address
|
|
|
|
* + Domains, blank
|
|
|
|
* + Hosting, blank
|
|
|
|
* + SSL, blank
|
|
|
|
*
|
|
|
|
* @return string
|
2019-06-21 06:21:48 +00:00
|
|
|
*/
|
2022-04-22 00:36:41 +00:00
|
|
|
public function getNameDetailAttribute()
|
2018-06-05 11:13:57 +00:00
|
|
|
{
|
2023-05-03 13:41:48 +00:00
|
|
|
return ($this->type->getServiceDescriptionAttribute() !== NULL) ? $this->type->getServiceDescriptionAttribute() : 'No Description';
|
2018-06-19 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 07:07:39 +00:00
|
|
|
/**
|
|
|
|
* The product we supply for this service
|
|
|
|
*
|
2024-07-07 05:02:02 +00:00
|
|
|
* @return Type
|
2022-04-19 07:07:39 +00:00
|
|
|
*/
|
2023-05-04 12:17:42 +00:00
|
|
|
public function getOfferingAttribute(): Type
|
2022-04-19 07:07:39 +00:00
|
|
|
{
|
|
|
|
return $this->product->type;
|
|
|
|
}
|
|
|
|
|
2020-04-18 22:33:41 +00:00
|
|
|
public function getOrderInfoNotesAttribute(): ?string
|
2019-01-24 03:40:33 +00:00
|
|
|
{
|
2024-07-07 05:02:02 +00:00
|
|
|
return $this->orderInfo('notes');
|
2020-04-18 22:33:41 +00:00
|
|
|
}
|
2019-01-24 03:40:33 +00:00
|
|
|
|
2020-04-18 22:33:41 +00:00
|
|
|
public function getOrderInfoReferenceAttribute(): ?string
|
|
|
|
{
|
2024-07-07 05:02:02 +00:00
|
|
|
return $this->orderInfo('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.
|
2022-04-22 01:47:46 +00:00
|
|
|
*
|
2022-09-29 08:06:16 +00:00
|
|
|
* @return Carbon|null
|
2020-02-05 04:47:24 +00:00
|
|
|
*/
|
2022-09-29 08:06:16 +00:00
|
|
|
public function getPaidToAttribute(): ?Carbon
|
2020-02-05 04:47:24 +00:00
|
|
|
{
|
2024-07-05 02:04:08 +00:00
|
|
|
// Last paid invoice
|
|
|
|
$lastpaid = $this
|
|
|
|
->invoices()
|
|
|
|
->filter(fn($item)=>$item->_balance <= 0)
|
|
|
|
->last();
|
|
|
|
|
|
|
|
return $lastpaid
|
|
|
|
? $this->invoiced_service_items_active->where('invoice_id',$lastpaid->id)->where('type',0)->max('stop_at')
|
|
|
|
: NULL;
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 06:21:48 +00:00
|
|
|
/**
|
2022-04-22 00:36:41 +00:00
|
|
|
* Return the billing recurring configuration for this service
|
2019-06-21 06:21:48 +00:00
|
|
|
*
|
2022-04-22 00:36:41 +00:00
|
|
|
* @param $value
|
|
|
|
* @return int
|
2019-06-21 06:21:48 +00:00
|
|
|
*/
|
2024-07-07 05:02:02 +00:00
|
|
|
public function xgetRecurScheduleAttribute($value): int
|
2020-02-06 22:11:02 +00:00
|
|
|
{
|
2022-04-22 00:36:41 +00:00
|
|
|
// If recur_schedule not set, default to quarterly
|
|
|
|
return $value ?? Invoice::BILL_QUARTERLY;
|
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
|
|
|
{
|
2024-07-24 10:17:23 +00:00
|
|
|
return $this->active
|
|
|
|
? strtolower($this->order_status)
|
|
|
|
: ((strtolower($this->order_status) === 'cancelled') ? 'cancelled' : 'inactive');
|
2019-06-29 00:14:12 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 01:14:01 +00:00
|
|
|
/**
|
2022-04-22 00:36:41 +00:00
|
|
|
* Return the product that supplies this service
|
|
|
|
* ie: product/*
|
2021-12-24 01:14:01 +00:00
|
|
|
*
|
2022-04-22 00:36:41 +00:00
|
|
|
* @return Model
|
2019-06-21 06:21:48 +00:00
|
|
|
*/
|
2022-04-22 00:36:41 +00:00
|
|
|
public function getSuppliedAttribute(): Model
|
2019-06-21 06:21:48 +00:00
|
|
|
{
|
2022-04-22 00:36:41 +00:00
|
|
|
return $this->getOfferingAttribute()->supplied;
|
2018-08-11 05:09:41 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 04:57:25 +00:00
|
|
|
/* METHODS */
|
2020-04-22 12:54:05 +00:00
|
|
|
|
2020-04-23 07:38:09 +00:00
|
|
|
/**
|
|
|
|
* Processing when service has been ordered.
|
|
|
|
*
|
|
|
|
* @return bool|null
|
2021-09-29 04:57:25 +00:00
|
|
|
* @todo Check
|
2020-04-23 07:38:09 +00:00
|
|
|
*/
|
|
|
|
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
|
2021-09-29 04:57:25 +00:00
|
|
|
* @todo Check
|
2020-04-18 22:33:41 +00:00
|
|
|
*/
|
|
|
|
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.
|
2021-09-29 04:57:25 +00:00
|
|
|
*
|
|
|
|
* @todo Check
|
2020-04-18 22:33:41 +00:00
|
|
|
*/
|
2020-04-23 07:38:09 +00:00
|
|
|
private function action_order_sent(string $next)
|
2020-04-18 22:33:41 +00:00
|
|
|
{
|
2020-04-23 07:38:09 +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
|
2021-09-29 04:57:25 +00:00
|
|
|
* @todo Check
|
2020-04-18 22:33:41 +00:00
|
|
|
*/
|
|
|
|
private function action_order_submit(): ?bool
|
|
|
|
{
|
|
|
|
// @todo TO IMPLEMENT
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:38:09 +00:00
|
|
|
/**
|
|
|
|
* Action when supplier has confirmed provisioning.
|
|
|
|
*
|
|
|
|
* @param string $next
|
|
|
|
* @return bool
|
2021-09-29 04:57:25 +00:00
|
|
|
* @todo Check
|
2020-04-23 07:38:09 +00:00
|
|
|
*/
|
|
|
|
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
|
2021-09-29 04:57:25 +00:00
|
|
|
* @todo Check
|
2020-04-18 22:33:41 +00:00
|
|
|
*/
|
|
|
|
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
|
2021-09-29 04:57:25 +00:00
|
|
|
* @todo Check
|
2020-04-18 22:33:41 +00:00
|
|
|
*/
|
|
|
|
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
|
2021-09-29 04:57:25 +00:00
|
|
|
* @todo Check
|
2020-04-18 22:33:41 +00:00
|
|
|
*/
|
|
|
|
private function action_payment_wait(): ?bool
|
|
|
|
{
|
|
|
|
// @todo TO IMPLEMENT
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-09-29 04:57:25 +00:00
|
|
|
/**
|
|
|
|
* Work out the next applicable actions for this service status, taking into account the user's role
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function actions(): Collection
|
|
|
|
{
|
|
|
|
$next = $this->getStageParameters($this->order_status)->get('next');
|
|
|
|
return $next
|
|
|
|
? $next->map(function($item,$key) {
|
|
|
|
$authorized = FALSE;
|
|
|
|
|
|
|
|
if ($x=Arr::get(self::ACTION_PROGRESS,$key))
|
|
|
|
foreach ($item as $role) {
|
|
|
|
if ($this->isAuthorised($role)) {
|
|
|
|
$authorized = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $authorized ? $x['title'] : NULL;
|
|
|
|
})->filter()->sort()
|
|
|
|
: collect();
|
|
|
|
}
|
|
|
|
|
2020-04-18 22:33:41 +00:00
|
|
|
/**
|
2021-09-29 04:57:25 +00:00
|
|
|
* Get the stage parameters
|
2020-04-18 22:33:41 +00:00
|
|
|
*
|
|
|
|
* @param string $stage
|
2021-09-29 04:57:25 +00:00
|
|
|
* @return Collection
|
2020-04-18 22:33:41 +00:00
|
|
|
*/
|
2021-09-29 04:57:25 +00:00
|
|
|
public function getStageParameters(string $stage): Collection
|
2020-04-18 22:33:41 +00:00
|
|
|
{
|
2021-09-29 04:57:25 +00:00
|
|
|
$result = Arr::get(self::ACTION_PROGRESS,$stage);
|
2022-04-21 06:25:29 +00:00
|
|
|
$myrole = array_search(Auth::user()->role(),User::role_order);
|
2020-04-18 22:33:41 +00:00
|
|
|
|
|
|
|
// If we have no valid next stage, return an empty collection.
|
2021-09-29 04:57:25 +00:00
|
|
|
if (($myrole === FALSE) || (! $result))
|
|
|
|
return collect();
|
2020-04-18 22:33:41 +00:00
|
|
|
|
|
|
|
// Filter the result based on who we are
|
|
|
|
$next = collect();
|
|
|
|
|
2021-09-29 04:57:25 +00:00
|
|
|
if (array_key_exists('next',$result) && count($result['next'])) {
|
|
|
|
foreach ($result['next'] as $action => $roles) {
|
|
|
|
// Can the current user do this role?
|
|
|
|
$cando = FALSE;
|
2020-04-18 22:33:41 +00:00
|
|
|
|
2021-09-29 04:57:25 +00:00
|
|
|
foreach ($roles as $role) {
|
2022-04-21 06:25:29 +00:00
|
|
|
if ($myrole <= array_search($role,User::role_order)) {
|
2021-09-29 04:57:25 +00:00
|
|
|
$cando = TRUE;
|
2020-04-18 22:33:41 +00:00
|
|
|
|
2021-09-29 04:57:25 +00:00
|
|
|
break;
|
2020-04-18 22:33:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-29 04:57:25 +00:00
|
|
|
if ($cando)
|
|
|
|
$next->put($action,$roles);
|
2020-04-18 22:33:41 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 04:57:25 +00:00
|
|
|
$result['next'] = $next;
|
2020-04-18 22:33:41 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 04:57:25 +00:00
|
|
|
return collect($result);
|
2020-04-18 22:33:41 +00:00
|
|
|
}
|
|
|
|
|
2024-07-05 02:04:08 +00:00
|
|
|
/**
|
|
|
|
* Return this service invoices
|
|
|
|
*/
|
|
|
|
public function invoices()
|
|
|
|
{
|
|
|
|
return $this->account
|
2024-07-05 06:38:31 +00:00
|
|
|
->invoiceSummary($this->invoiced_service_items_active->pluck('invoice_id'))
|
|
|
|
->get();
|
2024-07-05 02:04:08 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 06:21:48 +00:00
|
|
|
/**
|
2022-04-21 06:25:29 +00:00
|
|
|
* Determine if a service is active. It is active, if active=1, or the order_status is not in self::INACTIVE_STATUS[]
|
2019-06-21 06:21:48 +00:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isActive(): bool
|
2018-08-11 05:09:41 +00:00
|
|
|
{
|
2024-07-07 05:02:02 +00:00
|
|
|
return $this->attributes['active'] || ($this->order_status && (! in_array($this->order_status,self::INACTIVE_STATUS)));
|
2018-08-11 05:09:41 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 04:57:25 +00:00
|
|
|
/**
|
|
|
|
* Determine if the current user has the role for this service
|
|
|
|
*
|
|
|
|
* @param string $role
|
|
|
|
* @return bool
|
2022-04-22 00:36:41 +00:00
|
|
|
* @todo Can we use the gates to achieve this?
|
2021-09-29 04:57:25 +00:00
|
|
|
*/
|
2024-07-07 05:02:02 +00:00
|
|
|
private function isAuthorised(string $role): bool
|
2021-09-29 04:57:25 +00:00
|
|
|
{
|
|
|
|
switch(Auth::user()->role()) {
|
|
|
|
// Wholesalers are site admins, they can see everything
|
|
|
|
case 'wholesaler':
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
case 'reseller':
|
|
|
|
switch ($role) {
|
|
|
|
case 'wholesaler':
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
// Check service is in the resellers/customers list
|
|
|
|
case 'reseller':
|
|
|
|
case 'customer':
|
2021-09-29 07:11:46 +00:00
|
|
|
return TRUE;
|
2021-09-29 04:57:25 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
abort(500,'Unknown role for reseller: '.$role);
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'customer':
|
|
|
|
switch ($role) {
|
|
|
|
case 'reseller':
|
|
|
|
case 'wholesaler':
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
// Check service is in the customers list
|
|
|
|
case 'customer':
|
2021-09-29 07:11:46 +00:00
|
|
|
return TRUE;
|
2021-09-29 04:57:25 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
abort(500,'Unknown role for customer: '.$role);
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
abort(500,'Unknown user role: ',Auth::user()->role());
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-07-13 02:31:56 +00:00
|
|
|
/**
|
|
|
|
* Do we bill for this service
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isBilled(): bool
|
|
|
|
{
|
2022-10-18 06:24:53 +00:00
|
|
|
return ! ($this->external_billing || $this->suspend_billing || ($this->price === 0));
|
2021-07-13 02:31:56 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 01:09:00 +00:00
|
|
|
/**
|
2024-07-07 05:02:02 +00:00
|
|
|
* Has the price for this service been overridden
|
|
|
|
*
|
2023-05-09 01:09:00 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2024-07-07 05:02:02 +00:00
|
|
|
public function isChargeOverridden(): bool
|
2023-05-09 01:09:00 +00:00
|
|
|
{
|
|
|
|
return ! is_null($this->price);
|
|
|
|
}
|
|
|
|
|
2019-07-04 04:55:05 +00:00
|
|
|
/**
|
|
|
|
* Should this service be invoiced soon
|
|
|
|
*
|
2022-04-22 00:36:41 +00:00
|
|
|
* @param int $days
|
2019-07-04 04:55:05 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isInvoiceDueSoon($days=30): bool
|
2019-07-02 05:28:27 +00:00
|
|
|
{
|
2022-04-22 00:36:41 +00:00
|
|
|
return $this->isBilled() AND $this->getInvoiceNextAttribute()->lessThan(now()->addDays($days));
|
2019-07-04 04:55:05 +00:00
|
|
|
}
|
2019-07-02 05:28:27 +00:00
|
|
|
|
2020-02-05 04:47:24 +00:00
|
|
|
/**
|
2022-04-22 00:36:41 +00:00
|
|
|
* Identify if a service is being ordered, ie: not active yet nor cancelled
|
2020-02-05 04:47:24 +00:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isPending(): bool
|
|
|
|
{
|
2024-07-07 05:02:02 +00:00
|
|
|
return (! $this->active)
|
|
|
|
&& (! is_null($this->order_status))
|
|
|
|
&& (! in_array($this->order_status,array_merge(self::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
|
|
|
|
*
|
2020-04-01 12:35:06 +00:00
|
|
|
* @param bool $future Next item to be billed (not in the next x days)
|
2022-04-22 00:36:41 +00:00
|
|
|
* @param Carbon|null $billdate
|
2020-02-06 09:31:43 +00:00
|
|
|
* @return Collection
|
|
|
|
* @throws Exception
|
2022-04-21 04:41:26 +00:00
|
|
|
* @todo This query is expensive.
|
2020-02-06 09:31:43 +00:00
|
|
|
*/
|
2020-12-01 10:23:07 +00:00
|
|
|
public function next_invoice_items(bool $future,Carbon $billdate=NULL): Collection
|
2019-07-04 04:55:05 +00:00
|
|
|
{
|
2022-04-22 00:36:41 +00:00
|
|
|
if ($this->wasCancelled() OR (! $this->isBilled()) OR (! $future AND ! $this->active))
|
2020-02-12 10:32:57 +00:00
|
|
|
return collect();
|
2020-02-06 09:31:43 +00:00
|
|
|
|
2020-12-01 10:23:07 +00:00
|
|
|
if (is_null($billdate))
|
|
|
|
$billdate = Carbon::now()->addDays(30);
|
|
|
|
|
2020-02-06 22:11:02 +00:00
|
|
|
// 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))
|
2021-12-24 01:14:01 +00:00
|
|
|
AND $this->product->getSetupChargeAttribute($this->recur_schedule,$this->account->group))
|
2020-02-12 10:32:57 +00:00
|
|
|
{
|
2020-02-06 22:11:02 +00:00
|
|
|
$o = new InvoiceItem;
|
2020-02-12 10:32:57 +00:00
|
|
|
|
2020-02-06 22:11:02 +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
|
2021-12-24 01:14:01 +00:00
|
|
|
$o->price_base = $this->product->getSetupChargeAttribute($this->recur_schedule,$this->account->group);
|
2020-02-06 22:11:02 +00:00
|
|
|
//$o->recurring_schedule = $this->recur_schedule;
|
2022-04-22 04:41:18 +00:00
|
|
|
$o->start_at = $this->invoice_next;
|
|
|
|
$o->stop_at = $this->invoice_next;
|
2020-02-06 22:11:02 +00:00
|
|
|
$o->quantity = 1;
|
2020-04-01 12:35:06 +00:00
|
|
|
$o->site_id = 1; // @todo
|
2020-02-06 22:11:02 +00:00
|
|
|
|
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 (
|
2020-12-01 10:23:07 +00:00
|
|
|
(($future == TRUE) AND $this->invoice_next < $this->invoice_next_end) OR
|
2022-05-11 23:09:49 +00:00
|
|
|
(($future == FALSE) AND ($this->invoice_to < ($this->stop_at ?: $billdate)))
|
2020-04-22 12:54:05 +00:00
|
|
|
))
|
2020-02-13 11:18:20 +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)
|
2023-05-09 01:09:00 +00:00
|
|
|
? (is_null($this->price) ? $this->product->getBaseChargeAttribute($this->recur_schedule,$this->account->group) : $this->price)
|
2020-04-01 12:35:06 +00:00
|
|
|
: $this->price; // @todo change to a method in this class
|
2022-04-22 04:41:18 +00:00
|
|
|
$o->recur_schedule = $this->recur_schedule;
|
|
|
|
$o->start_at = $this->invoice_next;
|
|
|
|
$o->stop_at = $this->invoice_next_end;
|
2020-02-12 10:32:57 +00:00
|
|
|
$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);
|
2022-05-11 23:09:49 +00:00
|
|
|
} while ($future == FALSE AND ($this->invoice_to < ($this->stop_at ?: $billdate)));
|
2020-02-06 22:11:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 12:35:06 +00:00
|
|
|
// Add additional charges
|
2020-12-01 10:23:07 +00:00
|
|
|
if ((($future == TRUE) OR (($future == FALSE) AND ($this->invoice_to >= $billdate)))
|
2020-04-02 01:03:26 +00:00
|
|
|
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;
|
2022-06-12 08:32:54 +00:00
|
|
|
$o->start_at = $oo->start_at;
|
|
|
|
$o->stop_at = $oo->stop_at;
|
2020-04-01 12:35:06 +00:00
|
|
|
$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);
|
|
|
|
}
|
2020-04-02 01:03:26 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2024-07-07 05:02:02 +00:00
|
|
|
/**
|
|
|
|
* Extract data from the order_info column
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return string|null
|
|
|
|
* @todo This should be a json column, so we shouldnt need this by using order_info->key
|
|
|
|
*/
|
|
|
|
private function orderInfo(string $key): ?string
|
|
|
|
{
|
|
|
|
return $this->order_info ? $this->order_info->get($key) : NULL;
|
|
|
|
}
|
|
|
|
|
2020-02-12 10:32:57 +00:00
|
|
|
/**
|
|
|
|
* Service that was cancelled or never provisioned
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function wasCancelled(): bool
|
|
|
|
{
|
2022-04-21 06:25:29 +00:00
|
|
|
return in_array($this->order_status,self::INACTIVE_STATUS);
|
2020-02-12 10:32:57 +00:00
|
|
|
}
|
2022-04-22 00:36:41 +00:00
|
|
|
}
|