Compare commits
3 Commits
67824d4ab5
...
09f2eb8d9d
Author | SHA1 | Date | |
---|---|---|---|
09f2eb8d9d | |||
76889728cd | |||
0d9dbafcf1 |
29
app/Casts/CollectionOrNull.php
Normal file
29
app/Casts/CollectionOrNull.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Casts;
|
||||
|
||||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class CollectionOrNull implements CastsAttributes
|
||||
{
|
||||
/**
|
||||
* Cast the given value.
|
||||
*
|
||||
* @param array<string, mixed> $attributes
|
||||
*/
|
||||
public function get(Model $model, string $key, mixed $value, array $attributes): mixed
|
||||
{
|
||||
return collect(json_decode($value, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the given value for storage.
|
||||
*
|
||||
* @param array<string, mixed> $attributes
|
||||
*/
|
||||
public function set(Model $model, string $key, mixed $value, array $attributes): mixed
|
||||
{
|
||||
return $value->count() ? json_encode($value) : NULL;
|
||||
}
|
||||
}
|
@ -206,10 +206,9 @@ class Account extends Model implements IDs
|
||||
/**
|
||||
* Get the address for the account
|
||||
*
|
||||
* @return array
|
||||
* @todo Change this to return a collection
|
||||
* @return Collection
|
||||
*/
|
||||
public function getAddressAttribute(): array
|
||||
public function getAddressAttribute(): Collection
|
||||
{
|
||||
return collect([
|
||||
'address1' => $this->address1,
|
||||
@ -219,9 +218,7 @@ class Account extends Model implements IDs
|
||||
$this->state,
|
||||
$this->zip)
|
||||
])
|
||||
->filter()
|
||||
->values()
|
||||
->toArray();
|
||||
->filter();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -5,6 +5,7 @@ namespace App\Models;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
use App\Casts\CollectionOrNull;
|
||||
use App\Traits\SiteID;
|
||||
|
||||
/**
|
||||
@ -19,7 +20,7 @@ class Charge extends Model
|
||||
use SiteID;
|
||||
|
||||
protected $casts = [
|
||||
'attributes' => 'json',
|
||||
'attributes' => CollectionOrNull::class,
|
||||
];
|
||||
|
||||
protected $dates = [
|
||||
|
@ -11,8 +11,8 @@ class Cost extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $dates = [
|
||||
'billed_at',
|
||||
protected $casts = [
|
||||
'billed_at' => 'datetime:Y-m-d',
|
||||
];
|
||||
|
||||
protected $with = [
|
||||
|
@ -7,8 +7,10 @@ use Clarkeash\Doorman\Facades\Doorman;
|
||||
use Clarkeash\Doorman\Models\Invite;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Arr;
|
||||
use Leenooks\Casts\LeenooksCarbon;
|
||||
use Leenooks\Traits\ScopeActive;
|
||||
|
||||
use App\Casts\CollectionOrNull;
|
||||
use App\Interfaces\IDs;
|
||||
use App\Traits\PushNew;
|
||||
|
||||
@ -37,8 +39,8 @@ class Invoice extends Model implements IDs
|
||||
|
||||
protected $casts = [
|
||||
'created_at' => 'datetime:Y-m-d',
|
||||
'due_at' => 'datetime:Y-m-d',
|
||||
'reminders' => 'json',
|
||||
'due_at' => LeenooksCarbon::class,
|
||||
'reminders' => CollectionOrNull::class,
|
||||
'_paid_at' => 'datetime:Y-m-d',
|
||||
];
|
||||
|
||||
|
@ -2,20 +2,17 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Exception;
|
||||
use Illuminate\Database\Eloquent\Casts\AsCollection;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
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;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
use Leenooks\Carbon as LeenooksCarbon;
|
||||
use Leenooks\Carbon;
|
||||
use Leenooks\Casts\LeenooksCarbon;
|
||||
|
||||
use App\Models\Product\Type;
|
||||
use App\Interfaces\IDs;
|
||||
@ -48,7 +45,7 @@ use App\Traits\ScopeServiceUserAuthorised;
|
||||
* + supplied : The model of the supplier's product used for this service.
|
||||
*
|
||||
* Methods:
|
||||
* + isChargeOverriden : Has the price been overridden?
|
||||
* + isChargeOverridden : Has the price been overridden?
|
||||
* + isPending : Is this a pending active service
|
||||
*
|
||||
* @package App\Models
|
||||
@ -61,33 +58,9 @@ class Service extends Model implements IDs
|
||||
|
||||
protected $casts = [
|
||||
'order_info' => AsCollection::class,
|
||||
'invoice_last_at' => 'datetime:Y-m-d', // @todo Can these be removed, since we can work out invoice dynamically now
|
||||
'invoice_next_at' => 'datetime:Y-m-d', // @todo Can these be removed, since we can work out invoice dynamically now
|
||||
'stop_at' => 'datetime:Y-m-d',
|
||||
'start_at' => 'datetime:Y-m-d',
|
||||
];
|
||||
|
||||
/** @deprecated */
|
||||
protected $appends = [
|
||||
'category_name',
|
||||
'name_short',
|
||||
];
|
||||
|
||||
/** @deprecated */
|
||||
protected $visible = [
|
||||
// 'account_name',
|
||||
// 'admin_service_id_url',
|
||||
'active',
|
||||
'category_name',
|
||||
// 'billing_price',
|
||||
// 'data_orig',
|
||||
'id',
|
||||
'name_short',
|
||||
// 'next_invoice',
|
||||
// 'product.name',
|
||||
// 'service_id',
|
||||
// 'service_id_url',
|
||||
// 'status',
|
||||
'invoice_next_at' => LeenooksCarbon::class, // @todo Can this be removed, since start_at provides this functionality
|
||||
'stop_at' => LeenooksCarbon::class,
|
||||
'start_at' => LeenooksCarbon::class,
|
||||
];
|
||||
|
||||
protected $with = [
|
||||
@ -295,6 +268,26 @@ class Service extends Model implements IDs
|
||||
],
|
||||
];
|
||||
|
||||
/* 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();
|
||||
}
|
||||
|
||||
/* INTERFACES */
|
||||
|
||||
/**
|
||||
@ -317,24 +310,10 @@ class Service extends Model implements IDs
|
||||
return sprintf('%02s-%04s.%s',$this->site_id,$this->account_id,$this->getLIDattribute());
|
||||
}
|
||||
|
||||
/* STATIC */
|
||||
|
||||
public static function movements(User $uo): Collection
|
||||
{
|
||||
return (new self)
|
||||
->active()
|
||||
->serviceUserAuthorised($uo)
|
||||
->where('order_status','!=','ACTIVE')
|
||||
->with(['account','product'])
|
||||
->get();
|
||||
}
|
||||
|
||||
/* RELATIONS */
|
||||
|
||||
/**
|
||||
* Account the service belongs to
|
||||
*
|
||||
* @return BelongsTo
|
||||
*/
|
||||
public function account()
|
||||
{
|
||||
@ -343,8 +322,6 @@ class Service extends Model implements IDs
|
||||
|
||||
/**
|
||||
* Return automatic billing details
|
||||
*
|
||||
* @return HasOne
|
||||
*/
|
||||
public function billing()
|
||||
{
|
||||
@ -353,8 +330,6 @@ class Service extends Model implements IDs
|
||||
|
||||
/**
|
||||
* Return Charges associated with this Service
|
||||
*
|
||||
* @return HasMany
|
||||
*/
|
||||
public function charges()
|
||||
{
|
||||
@ -424,8 +399,7 @@ class Service extends Model implements IDs
|
||||
{
|
||||
return $this->hasMany(InvoiceItem::class)
|
||||
->where('item_type','<>',0)
|
||||
->orderBy('service_id')
|
||||
->orderBy('start_at');
|
||||
->orderBy('start_at','desc');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -444,8 +418,7 @@ class Service extends Model implements IDs
|
||||
{
|
||||
return $this->hasMany(InvoiceItem::class)
|
||||
->where('item_type','=',0)
|
||||
->orderBy('service_id')
|
||||
->orderBy('start_at');
|
||||
->orderBy('start_at','desc');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -457,10 +430,14 @@ class Service extends Model implements IDs
|
||||
->where('active',TRUE);
|
||||
}
|
||||
|
||||
public function invoiced_service_items_active_recent()
|
||||
{
|
||||
return $this->invoiced_service_items_active()
|
||||
->limit(10);
|
||||
}
|
||||
|
||||
/**
|
||||
* User that ordered the service
|
||||
*
|
||||
* @return BelongsTo
|
||||
*/
|
||||
public function orderedby()
|
||||
{
|
||||
@ -469,8 +446,6 @@ class Service extends Model implements IDs
|
||||
|
||||
/**
|
||||
* Product of the service
|
||||
*
|
||||
* @return BelongsTo
|
||||
*/
|
||||
public function product()
|
||||
{
|
||||
@ -479,8 +454,6 @@ class Service extends Model implements IDs
|
||||
|
||||
/**
|
||||
* Return a child model with details of the service
|
||||
*
|
||||
* @return MorphTo
|
||||
*/
|
||||
public function type()
|
||||
{
|
||||
@ -548,16 +521,32 @@ class Service extends Model implements IDs
|
||||
|
||||
/**
|
||||
* How much do we charge for this service, base on the current recur schedule
|
||||
* price in the DB overrides the base price used
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getBillingChargeAttribute(): float
|
||||
{
|
||||
// @todo Temporary for services that dont have recur_schedule set.
|
||||
if (is_null($this->recur_schedule) OR is_null($this->product->getBaseChargeAttribute($this->recur_schedule,$this->account->group)))
|
||||
// If recur_schedule is null, then we only bill this item once
|
||||
if (is_null($this->recur_schedule) && $this->getInvoiceToAttribute())
|
||||
$this->price = 0;
|
||||
|
||||
return $this->addTax(is_null($this->price) ? $this->product->getBaseChargeAttribute($this->recur_schedule,$this->account->group) : $this->price);
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -585,18 +574,29 @@ class Service extends Model implements IDs
|
||||
*
|
||||
* @return float
|
||||
* @throws Exception
|
||||
* @deprecated use class::charge_normalized()
|
||||
* @deprecated use class::billing_charge_normalised()
|
||||
*/
|
||||
public function getBillingMonthlyPriceAttribute(): float
|
||||
{
|
||||
return number_format($this->getBillingChargeAttribute()/Arr::get(Invoice::billing_periods,$this->recur_schedule.'.interval',1),2);
|
||||
Log::alert('SMO:! Deprecated function getBillingMonthlyPriceAttribute()');
|
||||
return $this->getBillingChargeNormalisedAttribute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Service Category ID
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCategoryAttribute(): string
|
||||
{
|
||||
return $this->product->category;
|
||||
}
|
||||
|
||||
/**
|
||||
* Service Category Name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCategoryNameAttribute(): string
|
||||
{
|
||||
return $this->product->category_name;
|
||||
@ -607,7 +607,7 @@ class Service extends Model implements IDs
|
||||
*
|
||||
* Service contracts end the later of the start_date + contract_term or the expire date.
|
||||
*
|
||||
* @return Carbon
|
||||
* @return Carbon|null
|
||||
*/
|
||||
public function getContractEndAttribute(): ?Carbon
|
||||
{
|
||||
@ -640,49 +640,32 @@ class Service extends Model implements IDs
|
||||
return max($this->supplied->contract_term,$this->product->type->contract_term);
|
||||
}
|
||||
|
||||
/**
|
||||
* Date the service expires, also represents when it is paid up to
|
||||
*
|
||||
* @return string
|
||||
* @todo
|
||||
*/
|
||||
public function getExpiresAttribute(): string
|
||||
{
|
||||
abort(500,'Not implemented');
|
||||
return 'TBA';
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the date for the next invoice
|
||||
*
|
||||
* @return LeenooksCarbon
|
||||
* 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
|
||||
*/
|
||||
public function getInvoiceNextAttribute(): LeenooksCarbon
|
||||
public function getInvoiceNextAttribute(): Carbon
|
||||
{
|
||||
$last = $this->getInvoiceToAttribute();
|
||||
|
||||
return $last
|
||||
? $last->addDay()
|
||||
: ($this->invoice_next_at ? $this->invoice_next_at->clone() : ($this->start_at ?: LeenooksCarbon::now()));
|
||||
}
|
||||
|
||||
/**
|
||||
* We need to cast some dates to LeenooksCarbon to get access to startOfHalf()/endOfHalf() methods
|
||||
*
|
||||
* @param $value
|
||||
* @return LeenooksCarbon|null
|
||||
*/
|
||||
public function getInvoiceNextAtAttribute($value): ?LeenooksCarbon
|
||||
{
|
||||
return $value ? LeenooksCarbon::create($value) : NULL;
|
||||
: (min($this->start_at,$this->invoice_next_at) ?: Carbon::now());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the end date for the next invoice
|
||||
*
|
||||
* @return mixed
|
||||
* @return Carbon
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getInvoiceNextEndAttribute()
|
||||
public function getInvoiceNextEndAttribute(): Carbon
|
||||
{
|
||||
switch ($this->recur_schedule) {
|
||||
case Invoice::BILL_WEEKLY:
|
||||
@ -706,7 +689,7 @@ class Service extends Model implements IDs
|
||||
case Invoice::BILL_SEMI_YEARLY:
|
||||
$date = $this->product->price_recur_strict
|
||||
? $this->getInvoiceNextAttribute()->endOfHalf()
|
||||
: $this->getInvoiceNextAttribute()->addQuarter(2)->subDay();
|
||||
: $this->getInvoiceNextAttribute()->addQuarters(2)->subDay();
|
||||
break;
|
||||
|
||||
case Invoice::BILL_YEARLY:
|
||||
@ -717,11 +700,12 @@ class Service extends Model implements IDs
|
||||
|
||||
case Invoice::BILL_TWOYEARS:
|
||||
if (! $this->product->price_recur_strict) {
|
||||
$date = $this->getInvoiceNextAttribute()->addYear(2)->subDay();
|
||||
$date = $this->getInvoiceNextAttribute()->addYears(2)->subDay();
|
||||
|
||||
} else {
|
||||
$date = $this->getInvoiceNextAttribute()->addYear(2)->subDay()->endOfYear();
|
||||
$date = $this->getInvoiceNextAttribute()->addYears(2)->subDay()->endOfYear();
|
||||
|
||||
// Make sure we end on an even year
|
||||
if ($date->clone()->addDay()->year%2)
|
||||
$date = $date->subYear();
|
||||
}
|
||||
@ -729,25 +713,25 @@ class Service extends Model implements IDs
|
||||
|
||||
// NOTE: price_recur_strict ignored
|
||||
case Invoice::BILL_THREEYEARS:
|
||||
$date = $this->getInvoiceNextAttribute()->addYear(3)->subDay();
|
||||
$date = $this->getInvoiceNextAttribute()->addYears(3)->subDay();
|
||||
break;
|
||||
|
||||
// NOTE: price_recur_strict ignored
|
||||
case Invoice::BILL_FOURYEARS:
|
||||
$date = $this->getInvoiceNextAttribute()->addYear(4)->subDay();
|
||||
$date = $this->getInvoiceNextAttribute()->addYears(4)->subDay();
|
||||
break;
|
||||
|
||||
// NOTE: price_recur_strict ignored
|
||||
case Invoice::BILL_FIVEYEARS:
|
||||
$date = $this->getInvoiceNextAttribute()->addYear(5)->subDay();
|
||||
$date = $this->getInvoiceNextAttribute()->addYears(5)->subDay();
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Exception('Unknown recur_schedule');
|
||||
}
|
||||
|
||||
// If the invoice has an end date, our invoice period shouldnt be greater than that.
|
||||
if ($this->stop_at AND $this->stop_at < $date)
|
||||
// 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))
|
||||
$date = $this->stop_at;
|
||||
|
||||
return $date;
|
||||
@ -765,43 +749,43 @@ class Service extends Model implements IDs
|
||||
if (! $this->product->price_recur_strict)
|
||||
return 1;
|
||||
|
||||
$n = round($this->invoice_next->diffInDays($this->invoice_next_end),0);
|
||||
$n = $this->invoice_next->diffInDays($this->invoice_next_end);
|
||||
|
||||
switch ($this->recur_schedule) {
|
||||
case Invoice::BILL_WEEKLY:
|
||||
$d = $this->invoice_next->addWeek()->startOfWeek()->diff($this->invoice_next_end->startOfWeek())->days;
|
||||
$d = $this->invoice_next_end->addWeek()->startOfWeek()->diffInDays($this->invoice_next->startOfWeek());
|
||||
break;
|
||||
|
||||
case Invoice::BILL_MONTHLY:
|
||||
$d = $this->invoice_next->addMonth()->startOfMonth()->diff($this->invoice_next_end->startOfMonth())->days;
|
||||
$d = $this->invoice_next_end->addMonth()->startOfMonth()->diffInDays($this->invoice_next->startOfMonth());
|
||||
break;
|
||||
|
||||
case Invoice::BILL_QUARTERLY:
|
||||
$d = round($this->invoice_next_end->startOfQuarter()->diffInDays($this->invoice_next->addQuarter()->startOfQuarter()),1);
|
||||
$d = $this->invoice_next_end->startOfQuarter()->diffInDays($this->invoice_next->addQuarter()->startOfQuarter());
|
||||
break;
|
||||
|
||||
case Invoice::BILL_SEMI_YEARLY:
|
||||
$d = $this->invoice_next->addQuarter(2)->startOfHalf()->diff($this->invoice_next_end->startOfHalf())->days;
|
||||
$d = $this->invoice_next_end->addQuarter(2)->startOfHalf()->diffInDays($this->invoice_next->startOfHalf());
|
||||
break;
|
||||
|
||||
case Invoice::BILL_YEARLY:
|
||||
$d = $this->invoice_next->addYear()->startOfYear()->diff($this->invoice_next_end->startOfYear())->days;
|
||||
$d = $this->invoice_next_end->addYear()->startOfYear()->diffInDays($this->invoice_next->startOfYear());
|
||||
break;
|
||||
|
||||
case Invoice::BILL_TWOYEARS:
|
||||
$d = $this->invoice_next->addYear(2)->startOfYear()->diff($this->invoice_next_end->subyear(2))->days-1;
|
||||
$d = $this->invoice_next_end->addYear(2)->startOfYear()->diffInDays($this->invoice_next->subyear(2))-1;
|
||||
break;
|
||||
|
||||
case Invoice::BILL_THREEYEARS:
|
||||
$d = $this->invoice_next->addYear(3)->startOfYear()->diff($this->invoice_next_end->subyear(3))->days-1;
|
||||
$d = $this->invoice_next_end->addYear(3)->startOfYear()->diffInDays($this->invoice_next->subyear(3))-1;
|
||||
break;
|
||||
|
||||
case Invoice::BILL_FOURYEARS:
|
||||
$d = $this->invoice_next->addYear(3)->startOfYear()->diff($this->invoice_next_end->subyear(4))->days-1;
|
||||
$d = $this->invoice_next_end->addYear(3)->startOfYear()->diffInDays($this->invoice_next->subyear(4))-1;
|
||||
break;
|
||||
|
||||
case Invoice::BILL_FIVEYEARS:
|
||||
$d = $this->invoice_next->addYear(3)->startOfYear()->diff($this->invoice_next_end->subyear(5))->days-1;
|
||||
$d = $this->invoice_next_end->addYear(3)->startOfYear()->diffInDays($this->invoice_next->subyear(5))-1;
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -814,12 +798,12 @@ class Service extends Model implements IDs
|
||||
/**
|
||||
* Get the date that the service has been invoiced to
|
||||
*
|
||||
* @return LeenooksCarbon|null
|
||||
* @return Carbon|null
|
||||
*/
|
||||
public function getInvoiceToAttribute(): ?LeenooksCarbon
|
||||
public function getInvoiceToAttribute(): ?Carbon
|
||||
{
|
||||
return ($x=$this->invoice_items->filter(function($item) { return $item->item_type === 0;}))->count()
|
||||
? $x->last()->stop_at
|
||||
return ($x=$this->invoiced_service_items_active_recent)->count()
|
||||
? $x->first()->stop_at
|
||||
: NULL;
|
||||
}
|
||||
|
||||
@ -863,7 +847,7 @@ class Service extends Model implements IDs
|
||||
/**
|
||||
* The product we supply for this service
|
||||
*
|
||||
* @return Model
|
||||
* @return Type
|
||||
*/
|
||||
public function getOfferingAttribute(): Type
|
||||
{
|
||||
@ -872,12 +856,12 @@ class Service extends Model implements IDs
|
||||
|
||||
public function getOrderInfoNotesAttribute(): ?string
|
||||
{
|
||||
return $this->getOrderInfoValue('notes');
|
||||
return $this->orderInfo('notes');
|
||||
}
|
||||
|
||||
public function getOrderInfoReferenceAttribute(): ?string
|
||||
{
|
||||
return $this->getOrderInfoValue('reference');
|
||||
return $this->orderInfo('reference');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -904,23 +888,12 @@ class Service extends Model implements IDs
|
||||
* @param $value
|
||||
* @return int
|
||||
*/
|
||||
public function getRecurScheduleAttribute($value): int
|
||||
public function xgetRecurScheduleAttribute($value): int
|
||||
{
|
||||
// If recur_schedule not set, default to quarterly
|
||||
return $value ?? Invoice::BILL_QUARTERLY;
|
||||
}
|
||||
|
||||
/**
|
||||
* We need to cast some dates to LeenooksCarbon to get access to startOfHalf()/endOfHalf() methods
|
||||
*
|
||||
* @param $value
|
||||
* @return LeenooksCarbon
|
||||
*/
|
||||
public function getStartAtAttribute($value): LeenooksCarbon
|
||||
{
|
||||
return LeenooksCarbon::create($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the Service Status
|
||||
*
|
||||
@ -928,39 +901,7 @@ class Service extends Model implements IDs
|
||||
*/
|
||||
public function getStatusAttribute(): string
|
||||
{
|
||||
if (! $this->order_status)
|
||||
return $this->active ? 'ACTIVE' : 'INACTIVE';
|
||||
else
|
||||
return $this->order_status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an HTML status box
|
||||
*
|
||||
* @return string
|
||||
* @todo Add in the other status'
|
||||
*/
|
||||
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;
|
||||
return $this->active ? $this->order_status : 'INACTIVE';
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1107,34 +1048,6 @@ class Service extends Model implements IDs
|
||||
: collect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add applicable tax to the cost
|
||||
*
|
||||
* @todo This needs to be calculated, not fixed at 1.1
|
||||
* @todo move all tax calculations into product
|
||||
* @param float $value
|
||||
* @return float
|
||||
*/
|
||||
private function addTax(float $value): float
|
||||
{
|
||||
return round($value*1.1,2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a normalize price dependent on the product, ie: Broadband = Monthly, Domain = Yearly, etc
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function charge_normalized(): float
|
||||
{
|
||||
return number_format($this->getBillingChargeAttribute()*Invoice::billing_change($this->recur_schedule,$this->offering->billing_interval),2);
|
||||
}
|
||||
|
||||
private function getOrderInfoValue(string $key): ?string
|
||||
{
|
||||
return $this->order_info ? $this->order_info->get($key) : NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the stage parameters
|
||||
*
|
||||
@ -1176,16 +1089,6 @@ class Service extends Model implements IDs
|
||||
return collect($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Does this service have traffic data to be graphed
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasUsage(): bool
|
||||
{
|
||||
return $this->product->hasUsage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return this service invoices
|
||||
*/
|
||||
@ -1200,11 +1103,10 @@ class Service extends Model implements IDs
|
||||
* Determine if a service is active. It is active, if active=1, or the order_status is not in self::INACTIVE_STATUS[]
|
||||
*
|
||||
* @return bool
|
||||
* @todo Remove active and have order_status reflect whether active or not
|
||||
*/
|
||||
public function isActive(): bool
|
||||
{
|
||||
return $this->active OR ($this->order_status AND ! in_array($this->order_status,self::INACTIVE_STATUS));
|
||||
return $this->attributes['active'] || ($this->order_status && (! in_array($this->order_status,self::INACTIVE_STATUS)));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1214,7 +1116,7 @@ class Service extends Model implements IDs
|
||||
* @return bool
|
||||
* @todo Can we use the gates to achieve this?
|
||||
*/
|
||||
public function isAuthorised(string $role): bool
|
||||
private function isAuthorised(string $role): bool
|
||||
{
|
||||
switch(Auth::user()->role()) {
|
||||
// Wholesalers are site admins, they can see everything
|
||||
@ -1267,10 +1169,11 @@ class Service extends Model implements IDs
|
||||
}
|
||||
|
||||
/**
|
||||
* Has the price for this service been override
|
||||
* Has the price for this service been overridden
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isChargeOverriden(): bool
|
||||
public function isChargeOverridden(): bool
|
||||
{
|
||||
return ! is_null($this->price);
|
||||
}
|
||||
@ -1293,9 +1196,9 @@ class Service extends Model implements IDs
|
||||
*/
|
||||
public function isPending(): bool
|
||||
{
|
||||
return ! $this->active
|
||||
AND ! is_null($this->order_status)
|
||||
AND ! in_array($this->order_status,array_merge(self::INACTIVE_STATUS,['INACTIVE']));
|
||||
return (! $this->active)
|
||||
&& (! is_null($this->order_status))
|
||||
&& (! in_array($this->order_status,array_merge(self::INACTIVE_STATUS,['INACTIVE'])));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1392,6 +1295,18 @@ class Service extends Model implements IDs
|
||||
return $this->invoice_items->filter(function($item) { return ! $item->exists; });
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Service that was cancelled or never provisioned
|
||||
*
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models\Service;
|
||||
|
||||
use App\Casts\CollectionOrNull;
|
||||
use App\Models\SupplierHostServer;
|
||||
use App\Traits\ServiceDomains;
|
||||
|
||||
@ -13,6 +14,10 @@ class Host extends Type
|
||||
{
|
||||
use ServiceDomains;
|
||||
|
||||
protected $casts = [
|
||||
'server_data' => CollectionOrNull::class,
|
||||
];
|
||||
|
||||
protected $table = 'service_host';
|
||||
protected $with = [
|
||||
'tld',
|
||||
|
@ -4,6 +4,7 @@ namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
@ -68,15 +69,19 @@ class Site extends Model
|
||||
/**
|
||||
* Return the site address as an array
|
||||
*
|
||||
* @return array
|
||||
* @return Collection
|
||||
*/
|
||||
public function getAddressAttribute(): array
|
||||
public function getAddressAttribute(): Collection
|
||||
{
|
||||
return array_filter([
|
||||
$this->site_address1,
|
||||
$this->site_address2,
|
||||
sprintf('%s %s %s',$this->site_city.(($this->site_state OR $this->site_postcode) ? ',' : ''),$this->site_state,$this->site_postcode)
|
||||
]);
|
||||
return collect([
|
||||
'address1' => $this->site_address1,
|
||||
'address2' => $this->site_address2,
|
||||
'location' => sprintf('%s %s %s',
|
||||
$this->site_city.(($this->site_state || $this->site_postcode) ? ',' : ''),
|
||||
$this->site_state,
|
||||
$this->site_postcode)
|
||||
])
|
||||
->filter();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -4,6 +4,7 @@ namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
use App\Casts\CollectionOrNull;
|
||||
use App\Traits\SiteID;
|
||||
|
||||
class UserOauth extends Model
|
||||
@ -13,7 +14,7 @@ class UserOauth extends Model
|
||||
protected $table = 'user_oauth';
|
||||
|
||||
protected $casts = [
|
||||
'oauth_data'=>'json',
|
||||
'oauth_data' => CollectionOrNull::class,
|
||||
];
|
||||
|
||||
public function User()
|
||||
|
@ -1,17 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Set defaults of QueryCacheable
|
||||
*/
|
||||
namespace App\Traits;
|
||||
|
||||
use Rennokki\QueryCache\Traits\QueryCacheable;
|
||||
|
||||
trait QueryCacheableConfig
|
||||
{
|
||||
use QueryCacheable;
|
||||
|
||||
public $cacheFor = 3600*6; // cache time, in seconds
|
||||
protected static $flushCacheOnUpdate = TRUE;
|
||||
public $cacheDriver = 'memcached';
|
||||
}
|
@ -31,7 +31,7 @@ return new class extends Migration
|
||||
$table->text('prod_attr')->nullable();
|
||||
|
||||
$table->string('model')->nullable();
|
||||
$table->string('order_status')->nullable();
|
||||
$table->string('order_status');
|
||||
$table->text('order_info')->nullable();
|
||||
|
||||
$table->date('invoice_last_at')->nullable();
|
||||
|
@ -26,7 +26,7 @@ return new class extends Migration
|
||||
$table->float('amount', 10, 0)->nullable();
|
||||
$table->float('quantity', 10, 0)->nullable();
|
||||
$table->boolean('taxable')->default(true);
|
||||
$table->binary('attributes', 65535)->nullable();
|
||||
$table->jsonb('attributes')->nullable();
|
||||
$table->string('description', 128)->nullable();
|
||||
|
||||
$table->date('start_at')->nullable();
|
||||
|
@ -60,7 +60,7 @@ return new class extends Migration
|
||||
$table->string('host_password',45)->nullable();
|
||||
$table->string('ftp_username',16)->nullable();
|
||||
$table->string('ftp_password',16)->nullable();
|
||||
$table->binary('server_data',65535)->nullable();
|
||||
$table->jsonb('server_data')->nullable();
|
||||
$table->date('expire_at')->nullable();
|
||||
|
||||
$table->integer('service_id')->unsigned();
|
||||
|
@ -27,7 +27,7 @@ return new class extends Migration
|
||||
$table->boolean('print_status')->default(false);
|
||||
$table->integer('account_billing_id')->nullable();
|
||||
$table->float('discount_amt',10,0)->nullable();
|
||||
$table->binary('reminders', 65535)->nullable();
|
||||
$table->jsonb('reminders')->nullable();
|
||||
|
||||
$table->integer('account_id')->unsigned();
|
||||
$table->foreign(['account_id','site_id'])->references(['id','site_id'])->on('accounts');
|
||||
|
@ -20,7 +20,7 @@ return new class extends Migration
|
||||
$table->integer('site_id')->unsigned();
|
||||
|
||||
$table->string('userid',128);
|
||||
$table->binary('oauth_data', 65535)->nullable();
|
||||
$table->jsonb('oauth_data')->nullable();
|
||||
|
||||
$table->integer('user_id')->unsigned()->nullable();
|
||||
|
||||
|
@ -34,22 +34,20 @@
|
||||
<!-- info row -->
|
||||
<div class="row invoice-info">
|
||||
<div class="col-4 invoice-col">
|
||||
FROM:
|
||||
<address>
|
||||
<strong>{{ $site->site_name }}</strong><br>
|
||||
{!! join('<br>',$site->address) !!}
|
||||
<br>
|
||||
FROM: <strong>{{ $site->site_name }}</strong><br>
|
||||
{!! $site->address->join('<br>') !!}
|
||||
<br><br>
|
||||
<strong>Email:</strong> {{ $site->site_email }}<br>
|
||||
<strong>Phone:</strong> {{ $site->site_phone }}
|
||||
</address>
|
||||
</div>
|
||||
|
||||
<div class="col-4 invoice-col">
|
||||
TO:
|
||||
<address>
|
||||
<strong>{{ $o->account->company }}</strong><br>
|
||||
{!! join('<br>',$o->account->address) !!}
|
||||
<br>
|
||||
TO: <strong>{{ $o->account->company }}</strong><br>
|
||||
{!! $o->account->address->join('<br>') !!}
|
||||
<br><br>
|
||||
<strong>Email:</strong> {{ $o->account->user->email }}<br>
|
||||
@if ($o->account->phone)
|
||||
<strong>Phone:</strong> {{ $o->account->phone }}<br>
|
||||
|
@ -24,7 +24,7 @@
|
||||
<td>{{ $so->stop_at ? $so->stop_at->format('Y-m-d') : '-' }}</td>
|
||||
<td>{{ $so->invoice_to ? $so->invoice_to->format('Y-m-d') : '-' }}</td>
|
||||
<td>{{ $so->active ? 'YES' : 'NO' }}</td>
|
||||
<td class="text-right">{{ $a=number_format($so->charge_normalized(),2) }}</td>
|
||||
<td class="text-right">{{ $a=number_format($so->billing_charge_normalised,2) }}</td>
|
||||
<td class="text-right">{{ $b=number_format($so->product->cost_normalized(),2) }}</td>
|
||||
<td><button class="btn btn-sm @if($a<$b)btn-danger @else btn-success @endif"><small>@if($a<$b)<i class="fas fa-fw fa-exclamation"></i> @else <i class="fas fa-fw fa-check"></i> @endif</small></button></td>
|
||||
</tr>
|
||||
|
@ -33,7 +33,7 @@
|
||||
@if (! $o->suspend_billing AND ! $o->external_billing)
|
||||
<li class="nav-item"><a class="nav-link {{ (! session()->has('service_update')) ? 'active' : '' }}" href="#pending_items" data-toggle="tab">Pending Items</a></li>
|
||||
@endif
|
||||
@if ($o->hasUsage())
|
||||
@if ($o->product->hasUsage())
|
||||
<li class="nav-item"><a class="nav-link {{ (! $o->isBilled() && (! session()->has('service_update'))) ? 'active' : '' }}" href="#traffic" data-toggle="tab">Traffic</a></li>
|
||||
@endif
|
||||
|
||||
@ -74,7 +74,7 @@
|
||||
@include('theme.backend.adminlte.service.widget.invoice')
|
||||
</div>
|
||||
@endif
|
||||
@if ($o->hasUsage())
|
||||
@if ($o->product->hasUsage())
|
||||
<div class="tab-pane fade {{ (! $o->isBilled() && (! session()->has('service_update'))) ? 'active show' : '' }}" id="traffic" role="tabpanel">
|
||||
@if ($o->type->usage(30)->count())
|
||||
@include('theme.backend.adminlte.service.widget.'.$o->product->category.'.usagegraph',['o'=>$o->type])
|
||||
|
@ -1,3 +1,4 @@
|
||||
<!-- $o=Service::class -->
|
||||
<table class="table table-sm" id="svc_bill_hist">
|
||||
<thead>
|
||||
<tr>
|
||||
|
@ -20,7 +20,7 @@
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Status</th>
|
||||
<td>{!! $o->status_html !!}</td>
|
||||
<td>@include('theme.backend.adminlte.service.widget.status')</td>
|
||||
</tr>
|
||||
@if ($o->order_status == 'ORDER-SENT')
|
||||
<tr>
|
||||
@ -47,7 +47,7 @@
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Amount</th>
|
||||
@if($o->isChargeOverriden())
|
||||
@if($o->isChargeOverridden())
|
||||
<td>@if ($o->billing_charge < $o->charge_orig)<del>${{ number_format($o->charge_orig,2) }}</del> @endif${{ number_format($o->billing_charge,2) }}</td>
|
||||
@else
|
||||
<td>${{ number_format($o->billing_charge,2) }}</td>
|
||||
|
@ -61,11 +61,11 @@
|
||||
|
||||
<tr>
|
||||
<th>Billing Price</th>
|
||||
<td @if($o->isChargeOverriden())class="text-danger"@endif>${{ number_format($b=$o->billing_charge,2) }}</td>
|
||||
<td @if($o->isChargeOverridden())class="text-danger"@endif>${{ number_format($b=$o->billing_charge,2) }}</td>
|
||||
<td>${{ number_format($a=$o->account->taxed($c->base_cost),2) }}</td>
|
||||
<td>{!! markup($a,$b) !!}</td>
|
||||
@if ($p->exists)
|
||||
<td @if($o->isChargeOverriden())class="text-danger"@endif>${{ number_format($b=$o->account->taxed($p->base_charge),2) }}</td>
|
||||
<td @if($o->isChargeOverridden())class="text-danger"@endif>${{ number_format($b=$o->account->taxed($p->base_charge),2) }}</td>
|
||||
<td>${{ number_format($a=$o->account->taxed($p->base_cost),2) }}</td>
|
||||
<td>{!! markup($a,$b) !!}</td>
|
||||
@endif
|
||||
@ -73,7 +73,7 @@
|
||||
|
||||
<tr>
|
||||
<th>Monthly Price</th>
|
||||
<td @if($x=$o->isChargeOverriden()) class="text-danger" @endif>
|
||||
<td @if($x=$o->isChargeOverridden()) class="text-danger" @endif>
|
||||
@if($x)
|
||||
<abbr title="${{ number_format($b=$o->account->taxed($c->base_charge)*\App\Models\Invoice::billing_change($o->billing_interval,\App\Models\Invoice::BILL_MONTHLY),2) }}">${{ number_format($b=$o->billing_monthly_price,2) }}
|
||||
@else
|
||||
@ -83,7 +83,7 @@
|
||||
<td>${{ number_format($a=$o->account->taxed($c->base_cost)*\App\Models\Invoice::billing_change($o->billing_interval,\App\Models\Invoice::BILL_MONTHLY),2) }}</td>
|
||||
<td>{!! markup($a,$b) !!}</td>
|
||||
@if ($p->exists)
|
||||
<td @if($x=$o->isChargeOverriden()) class="text-danger" @endif>${{ number_format($b=$o->account->taxed($p->base_charge)*\App\Models\Invoice::billing_change($o->billing_interval,\App\Models\Invoice::BILL_MONTHLY),2) }}</td>
|
||||
<td @if($x=$o->isChargeOverridden()) class="text-danger" @endif>${{ number_format($b=$o->account->taxed($p->base_charge)*\App\Models\Invoice::billing_change($o->billing_interval,\App\Models\Invoice::BILL_MONTHLY),2) }}</td>
|
||||
<td>${{ number_format($a=$o->account->taxed($p->base_cost)*\App\Models\Invoice::billing_change($o->billing_interval,\App\Models\Invoice::BILL_MONTHLY),2) }}</td>
|
||||
<td>{!! markup($a,$b) !!}</td>
|
||||
@endif
|
||||
|
@ -0,0 +1,12 @@
|
||||
<!-- $o=Service::class -->
|
||||
<span class="badge @if($o->isPending())badge-warning @else
|
||||
@switch ($o->status)
|
||||
@case('ACTIVE')
|
||||
badge-success
|
||||
@break;
|
||||
@case('INACTIVE')
|
||||
badge-danger
|
||||
@break;
|
||||
@endswitch
|
||||
@endif
|
||||
">{{ $o->status }}</span>
|
@ -23,7 +23,7 @@
|
||||
@component('mail::footer')
|
||||
<div class="fixedw" style="text-align: right; font-size: 0.8em;">
|
||||
{{ config('mail.from.name') }}<br>
|
||||
{!! join('<br>',$site->address) !!}<br>
|
||||
{!! $site->address->join('<br>') !!}<br>
|
||||
{{ $site->site_email }}
|
||||
</div>
|
||||
@endcomponent
|
||||
|
@ -26,7 +26,7 @@
|
||||
<h2 style="text-align: right;">Our Contact Details</h2>
|
||||
<address class="margin-bottom-40" style="float: right;">
|
||||
<table>
|
||||
<tr><th style="vertical-align:top; padding-right: 5px;">Address</th><td>{!! join('<br>',$site->address) !!}</td></tr>
|
||||
<tr><th style="vertical-align:top; padding-right: 5px;">Address</th><td>{!! $site->address->join('<br>') !!}</td></tr>
|
||||
@isset($site->site_fax)
|
||||
<tr><th>Fax</th><td>{{ $site->site_fax }}</tr>
|
||||
@endif
|
||||
|
Loading…
Reference in New Issue
Block a user