Cleanup usage of Leenooks\Carbon, change ServiceType to be consistent with Products/ Suppliers/

This commit is contained in:
Deon George 2022-04-22 11:47:46 +10:00
parent 8ed9e38290
commit a16277d9bb
19 changed files with 65 additions and 64 deletions

View File

@ -35,7 +35,7 @@ class SearchController extends Controller
->orderBy('firstname') ->orderBy('firstname')
->limit(10)->get() as $o) ->limit(10)->get() as $o)
{ {
$result->push(['name'=>sprintf('%s (%s)',$o->lid,$o->name),'value'=>'/u/home/'.$o->id,'category'=>'Users']); $result->push(['name'=>sprintf('%s (%s)',$o->name,$o->lid),'value'=>'/u/home/'.$o->id,'category'=>'Users']);
} }
# Look for Account # Look for Account
@ -44,7 +44,7 @@ class SearchController extends Controller
->orderBy('company') ->orderBy('company')
->limit(10)->get() as $o) ->limit(10)->get() as $o)
{ {
$result->push(['name'=>sprintf('%s (%s)',$o->lid,$o->company),'value'=>'/u/home/'.$o->user_id,'category'=>'Accounts']); $result->push(['name'=>sprintf('%s (%s)',$o->company,$o->lid),'value'=>'/u/home/'.$o->user_id,'category'=>'Accounts']);
} }
# Look for a Service # Look for a Service

View File

@ -5,10 +5,10 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Leenooks\Carbon;
use App\Traits\NextKey; use App\Traits\NextKey;
use App\Traits\PushNew; use App\Traits\PushNew;
use Leenooks\Carbon;
/** /**
* Class Invoice Items * Class Invoice Items
@ -85,8 +85,6 @@ class InvoiceItem extends Model
/** /**
* Start date for the invoice item line * Start date for the invoice item line
* *
* We need cast this value to a Leenooks\Carbon for access to startOfHalf() endOfHalf() methods
*
* @param $value * @param $value
* @return Carbon * @return Carbon
* @throws \Exception * @throws \Exception
@ -100,8 +98,6 @@ class InvoiceItem extends Model
/** /**
* End date for the invoice item line * End date for the invoice item line
* *
* We need cast this value to a Leenooks\Carbon for access to startOfHalf() endOfHalf() methods
*
* @param $value * @param $value
* @return Carbon * @return Carbon
* @throws \Exception * @throws \Exception

View File

@ -6,11 +6,9 @@ use Illuminate\Support\Collection;
use Leenooks\Traits\ScopeActive; use Leenooks\Traits\ScopeActive;
use App\Interfaces\ProductItem; use App\Interfaces\ProductItem;
use App\Models\Supplier;
use App\Models\Service\Broadband as ServiceBroadband; use App\Models\Service\Broadband as ServiceBroadband;
use App\Models\Supplier\Broadband as SupplierBroadband; use App\Models\Supplier\Broadband as SupplierBroadband;
// @todo does this need to extend Type? Perhaps have a ProductType consistent with ServiceType.
final class Broadband extends Type implements ProductItem final class Broadband extends Type implements ProductItem
{ {
use ScopeActive; use ScopeActive;
@ -61,7 +59,7 @@ final class Broadband extends Type implements ProductItem
{ {
$config = collect(); $config = collect();
foreach (array_keys(Supplier\Broadband::traffic_map) as $k => $v) { foreach (array_keys(SupplierBroadband::traffic_map) as $k => $v) {
// Base Config // Base Config
$config->put($k,$this->{$k}); $config->put($k,$this->{$k});
// Excess Config // Excess Config
@ -90,7 +88,7 @@ final class Broadband extends Type implements ProductItem
{ {
$result = 0; $result = 0;
foreach ($this->supplied->allowance(NULL,$this->allowance([])->toArray()) as $k=>$v) { foreach ($this->supplied->allowance(NULL,$this->allowance([])->toArray()) as $k=>$v) {
$result += -$v*$this->supplied->{Supplier\Broadband::traffic_map[$k]}; $result += -$v*$this->supplied->{SupplierBroadband::traffic_map[$k]};
} }
return $result; return $result;

View File

@ -1,10 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ProductType extends Model
{
//
}

View File

@ -2,6 +2,7 @@
namespace App\Models; namespace App\Models;
use Carbon\Carbon;
use Exception; use Exception;
use Illuminate\Database\Eloquent\Casts\AsCollection; use Illuminate\Database\Eloquent\Casts\AsCollection;
use Illuminate\Database\Eloquent\Collection as DatabaseCollection; use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
@ -14,8 +15,8 @@ use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Leenooks\Carbon;
use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\HttpException;
use Leenooks\Carbon as LeenooksCarbon;
use App\Interfaces\IDs; use App\Interfaces\IDs;
use App\Traits\ScopeServiceUserAuthorised; use App\Traits\ScopeServiceUserAuthorised;
@ -539,7 +540,7 @@ class Service extends Model implements IDs
* *
* @return Carbon * @return Carbon
*/ */
public function getContractEndAttribute(): ?\Carbon\Carbon public function getContractEndAttribute(): ?Carbon
{ {
// If we have no start date or expire date, then NULL; // If we have no start date or expire date, then NULL;
if (! $this->start_at && ! $this->type->expire_at) if (! $this->start_at && ! $this->type->expire_at)
@ -585,15 +586,14 @@ class Service extends Model implements IDs
/** /**
* Return the date for the next invoice * Return the date for the next invoice
* *
* @return Carbon|string * @return LeenooksCarbon
*/ */
public function getInvoiceNextAttribute(): \Carbon\Carbon public function getInvoiceNextAttribute(): LeenooksCarbon
{ {
$last = $this->getInvoiceToAttribute(); $last = $this->getInvoiceToAttribute();
return $last return $last
? $last->addDay() ? $last->addDay()
: ($this->date_next_invoice ? $this->date_next_invoice->clone() : ($this->date_next_invoice ? $this->date_next_invoice->clone() : ($this->start_at ?: LeenooksCarbon::now()));
: ($this->start_at ?: Carbon::now()));
} }
/** /**
@ -733,8 +733,10 @@ class Service extends Model implements IDs
/** /**
* Get the date that the service has been invoiced to * Get the date that the service has been invoiced to
*
* @return Carbon|null
*/ */
public function getInvoiceToAttribute(): ?\Carbon\Carbon public function getInvoiceToAttribute(): ?Carbon
{ {
$result = ($x=$this->invoice_items->filter(function($item) { return $item->item_type === 0;}))->count() $result = ($x=$this->invoice_items->filter(function($item) { return $item->item_type === 0;}))->count()
? $x->last()->date_stop ? $x->last()->date_stop
@ -807,6 +809,8 @@ class Service extends Model implements IDs
/** /**
* Work out when this service has been paid to. * Work out when this service has been paid to.
*
* @return Carbon
*/ */
public function getPaidToAttribute(): Carbon public function getPaidToAttribute(): Carbon
{ {
@ -834,6 +838,17 @@ class Service extends Model implements IDs
return $value ?? Invoice::BILL_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 * Return the Service Status
* *

View File

@ -2,21 +2,20 @@
namespace App\Models\Service; namespace App\Models\Service;
use Carbon\Carbon;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Leenooks\Carbon;
use App\Interfaces\ServiceUsage; use App\Interfaces\ServiceUsage;
use App\Models\Base\ServiceType;
use App\Models\Supplier\Broadband as SupplierBroadband; use App\Models\Supplier\Broadband as SupplierBroadband;
use App\Models\Supplier\Type; use App\Models\Supplier\Type as SupplierType;
use App\Models\Usage\Broadband as UsageBroadband; use App\Models\Usage\Broadband as UsageBroadband;
/** /**
* Class Broadband (Service) * Class Broadband (Service)
* Services that are Internet Broadband * Services that are Internet Broadband
*/ */
class Broadband extends ServiceType implements ServiceUsage class Broadband extends Type implements ServiceUsage
{ {
private const LOGKEY = 'MSB'; private const LOGKEY = 'MSB';
@ -105,9 +104,9 @@ class Broadband extends ServiceType implements ServiceUsage
/** /**
* Return the suppliers offering that this service is providing * Return the suppliers offering that this service is providing
* *
* @return Type * @return SupplierType
*/ */
public function supplied(): Type public function supplied(): SupplierType
{ {
return $this->provided_adsl_plan_id return $this->provided_adsl_plan_id
? SupplierBroadband::findOrFail($this->provided_adsl_plan_id) ? SupplierBroadband::findOrFail($this->provided_adsl_plan_id)

View File

@ -2,15 +2,14 @@
namespace App\Models\Service; namespace App\Models\Service;
use App\Models\Base\ServiceType;
use App\Traits\ServiceDomains;
use App\Models\DomainRegistrar; use App\Models\DomainRegistrar;
use App\Traits\ServiceDomains;
/** /**
* Class Domain (Service) * Class Domain (Service)
* Services that are managed Domain Names * Services that are managed Domain Names
*/ */
class Domain extends ServiceType class Domain extends Type
{ {
use ServiceDomains; use ServiceDomains;

View File

@ -2,14 +2,13 @@
namespace App\Models\Service; namespace App\Models\Service;
use App\Models\Base\ServiceType;
use App\Traits\ServiceDomains; use App\Traits\ServiceDomains;
/** /**
* Class Email (Service) * Class Email (Service)
* Services that are Email Hosting * Services that are Email Hosting
*/ */
class Email extends ServiceType class Email extends Type
{ {
use ServiceDomains; use ServiceDomains;

View File

@ -2,10 +2,8 @@
namespace App\Models\Service; namespace App\Models\Service;
use App\Models\Base\ServiceType;
// @todo Document how this is used. // @todo Document how this is used.
class Generic extends ServiceType class Generic extends Type
{ {
protected $table = 'service__generic'; protected $table = 'service__generic';
public $timestamps = FALSE; public $timestamps = FALSE;

View File

@ -2,15 +2,14 @@
namespace App\Models\Service; namespace App\Models\Service;
use App\Models\Base\ServiceType;
use App\Traits\ServiceDomains;
use App\Models\HostServer; use App\Models\HostServer;
use App\Traits\ServiceDomains;
/** /**
* Class Host (Service) * Class Host (Service)
* Services that are Web Hosting * Services that are Web Hosting
*/ */
class Host extends ServiceType class Host extends Type
{ {
use ServiceDomains; use ServiceDomains;

View File

@ -2,13 +2,11 @@
namespace App\Models\Service; namespace App\Models\Service;
use App\Models\Base\ServiceType;
/** /**
* Class Phone (Service) * Class Phone (Service)
* Services that are Voice Telephony * Services that are Voice Telephony
*/ */
class Phone extends ServiceType class Phone extends Type
{ {
protected $dates = [ protected $dates = [
'connect_at', 'connect_at',

View File

@ -2,16 +2,14 @@
namespace App\Models\Service; namespace App\Models\Service;
use Illuminate\Support\Arr;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Support\Arr;
use App\Models\Base\ServiceType;
/** /**
* Class SSL (Service) * Class SSL (Service)
* Services that are provide an SSL Certificate * Services that are provide an SSL Certificate
*/ */
class SSL extends ServiceType class SSL extends Type
{ {
protected $table = 'service_ssl'; protected $table = 'service_ssl';

View File

@ -1,16 +1,16 @@
<?php <?php
namespace App\Models\Base; namespace App\Models\Service;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use App\Interfaces\ServiceItem; use App\Interfaces\ServiceItem;
use App\Models\{Account,Service}; use App\Models\{Account,Service};
use App\Models\Supplier\Type; use App\Models\Supplier\Type as SupplierType;
use App\Traits\{ScopeServiceActive,ScopeServiceUserAuthorised}; use App\Traits\{ScopeServiceActive,ScopeServiceUserAuthorised};
abstract class ServiceType extends Model implements ServiceItem abstract class Type extends Model implements ServiceItem
{ {
use ScopeServiceActive,ScopeServiceUserAuthorised; use ScopeServiceActive,ScopeServiceUserAuthorised;
@ -90,9 +90,9 @@ abstract class ServiceType extends Model implements ServiceItem
/** /**
* The supplier's service that we provide * The supplier's service that we provide
* *
* @return Type * @return SupplierType
*/ */
public function supplied(): Type public function supplied(): SupplierType
{ {
return $this->service->product->type->supplied; return $this->service->product->type->supplied;
} }

View File

@ -8,7 +8,6 @@ use Illuminate\Support\Collection;
use App\Interfaces\SupplierItem; use App\Interfaces\SupplierItem;
use App\Models\Product\Broadband as ProductBroadband; use App\Models\Product\Broadband as ProductBroadband;
// @todo does this need to extend Type? Perhaps have a SupplierType consistent with ServiceType.
class Broadband extends Type implements SupplierItem class Broadband extends Type implements SupplierItem
{ {
protected $casts = [ protected $casts = [

View File

@ -2,8 +2,8 @@
namespace App\Models\Usage; namespace App\Models\Usage;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Leenooks\Carbon;
use App\Models\Service\Broadband as ServiceBroadband; use App\Models\Service\Broadband as ServiceBroadband;

View File

@ -14,6 +14,7 @@ use Laravel\Passport\HasApiTokens;
use Leenooks\Traits\ScopeActive; use Leenooks\Traits\ScopeActive;
use Leenooks\Traits\UserSwitch; use Leenooks\Traits\UserSwitch;
use App\Interfaces\IDs;
use App\Notifications\ResetPassword as ResetPasswordNotification; use App\Notifications\ResetPassword as ResetPasswordNotification;
use App\Traits\{QueryCacheableConfig,SiteID}; use App\Traits\{QueryCacheableConfig,SiteID};
@ -23,7 +24,7 @@ use App\Traits\{QueryCacheableConfig,SiteID};
* Attributes for users: * Attributes for users:
* + role : User's role * + role : User's role
*/ */
class User extends Authenticatable class User extends Authenticatable implements IDs
{ {
use HasFactory,HasApiTokens,Notifiable,UserSwitch,QueryCacheableConfig,SiteID,ScopeActive; use HasFactory,HasApiTokens,Notifiable,UserSwitch,QueryCacheableConfig,SiteID,ScopeActive;
@ -79,6 +80,18 @@ class User extends Authenticatable
$this->notify((new ResetPasswordNotification($token))->onQueue('high')); $this->notify((new ResetPasswordNotification($token))->onQueue('high'));
} }
/* INTERFACES */
public function getLIDAttribute(): string
{
return sprintf('#%04s',$this->id);
}
public function getSIDAttribute(): string
{
return sprintf('%02s-%s',$this->site_id,$this->getLIDAttribute());
}
/* RELATIONS */ /* RELATIONS */
/** /**

View File

@ -45,7 +45,7 @@
<td>{{ $oo->service_expire ? $oo->service_expire->format('Y-m-d') : '-' }}</td> <td>{{ $oo->service_expire ? $oo->service_expire->format('Y-m-d') : '-' }}</td>
<td>{{ $oo->registrar->name }}</td> <td>{{ $oo->registrar->name }}</td>
<td>{{ $oo->registrar_ns }}</td> <td>{{ $oo->registrar_ns }}</td>
<td>@if ($oo->service->isBilled()) <span class="@if($oo->service->suspend_billing)strike @endif">{{ $oo->service->invoice_next->format('Y-m-d') }}</span> @else - @endif</td> <td>@if ($oo->service->isBilled()) {{ $oo->service->invoice_next->format('Y-m-d') }}@else - @endif</td>
<td>@if (! $oo->service->external_billing)${{ number_format($oo->service->next_invoice_items(TRUE)->sum('total'),2) }}@else - @endif</td> <td>@if (! $oo->service->external_billing)${{ number_format($oo->service->next_invoice_items(TRUE)->sum('total'),2) }}@else - @endif</td>
<td>{{ $oo->service->billing_interval_string }}</td> <td>{{ $oo->service->billing_interval_string }}</td>
</tr> </tr>

View File

@ -48,8 +48,8 @@
<td>{{ $oo->service->product->supplier->name }}</td> <td>{{ $oo->service->product->supplier->name }}</td>
<td>{{ $oo->admin_url }}</td> <td>{{ $oo->admin_url }}</td>
<td>@if($oo->admin_user){{ $oo->admin_user }}/{{ $oo->admin_pass }}@else &nbsp; @endif</td> <td>@if($oo->admin_user){{ $oo->admin_user }}/{{ $oo->admin_pass }}@else &nbsp; @endif</td>
<td class="text-right">{{ number_format($oo->accounts ?: 0,0) }}</td> <td class="text-right">{{ number_format($oo->accounts ?: 0) }}</td>
<td>@if ($oo->service->isBilled()) <span class="@if($oo->service->suspend_billing)strike @endif">{{ $oo->service->invoice_next->format('Y-m-d') }}</span> @else - @endif</td> <td>@if ($oo->service->isBilled()) {{ $oo->service->invoice_next->format('Y-m-d') }} @else - @endif</td>
<td>@if (! $oo->service->external_billing)${{ number_format($oo->service->next_invoice_items(TRUE)->sum('total'),2) }}@else - @endif</td> <td>@if (! $oo->service->external_billing)${{ number_format($oo->service->next_invoice_items(TRUE)->sum('total'),2) }}@else - @endif</td>
<td>{{ $oo->service->billing_interval_string }}</td> <td>{{ $oo->service->billing_interval_string }}</td>
</tr> </tr>

View File

@ -43,7 +43,7 @@
<td>{{ $oo->service->name }}</td> <td>{{ $oo->service->name }}</td>
<td>{{ $oo->service_expire ? $oo->service_expire->format('Y-m-d') : '-' }}</td> <td>{{ $oo->service_expire ? $oo->service_expire->format('Y-m-d') : '-' }}</td>
<td>{{ $oo->service->product->supplier->name }}</td> <td>{{ $oo->service->product->supplier->name }}</td>
<td>@if ($oo->service->isBilled()) <span class="@if($oo->service->suspend_billing)strike @endif">{{ $oo->service->invoice_next->format('Y-m-d') }}</span> @else - @endif</td> <td>@if ($oo->service->isBilled()) {{ $oo->service->invoice_next->format('Y-m-d') }}@else - @endif</td>
<td>@if (! $oo->service->external_billing)${{ number_format($oo->service->next_invoice_items(TRUE)->sum('total'),2) }}@else - @endif</td> <td>@if (! $oo->service->external_billing)${{ number_format($oo->service->next_invoice_items(TRUE)->sum('total'),2) }}@else - @endif</td>
<td>{{ $oo->service->billing_interval_string }}</td> <td>{{ $oo->service->billing_interval_string }}</td>
</tr> </tr>