2018-05-20 12:53:14 +00:00
|
|
|
<?php
|
|
|
|
|
2019-06-07 06:54:27 +00:00
|
|
|
namespace App\Models\Service;
|
2018-05-20 12:53:14 +00:00
|
|
|
|
2022-04-22 01:47:46 +00:00
|
|
|
use Carbon\Carbon;
|
2021-02-17 13:22:50 +00:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2021-03-12 10:08:40 +00:00
|
|
|
|
2022-04-19 07:07:39 +00:00
|
|
|
use App\Interfaces\ServiceUsage;
|
2021-12-24 01:14:01 +00:00
|
|
|
use App\Models\Supplier\Broadband as SupplierBroadband;
|
2022-04-22 01:47:46 +00:00
|
|
|
use App\Models\Supplier\Type as SupplierType;
|
2022-04-20 06:24:58 +00:00
|
|
|
use App\Models\Usage\Broadband as UsageBroadband;
|
2022-08-01 10:34:10 +00:00
|
|
|
use App\Rules\IPv6_CIDR;
|
2019-06-13 04:32:34 +00:00
|
|
|
|
2022-04-19 07:07:39 +00:00
|
|
|
/**
|
|
|
|
* Class Broadband (Service)
|
|
|
|
* Services that are Internet Broadband
|
|
|
|
*/
|
2022-04-22 01:47:46 +00:00
|
|
|
class Broadband extends Type implements ServiceUsage
|
2018-05-20 12:53:14 +00:00
|
|
|
{
|
2022-04-19 07:07:39 +00:00
|
|
|
private const LOGKEY = 'MSB';
|
2019-06-13 04:32:34 +00:00
|
|
|
|
2020-02-05 04:47:24 +00:00
|
|
|
protected $dates = [
|
2022-04-19 07:07:39 +00:00
|
|
|
'connect_at',
|
|
|
|
'expire_at',
|
2020-02-05 04:47:24 +00:00
|
|
|
];
|
2022-04-19 07:07:39 +00:00
|
|
|
protected $table = 'service_broadband';
|
2020-05-28 05:08:13 +00:00
|
|
|
|
2022-04-19 07:07:39 +00:00
|
|
|
/* INTERFACES */
|
|
|
|
|
2019-06-29 00:14:12 +00:00
|
|
|
/**
|
2022-04-19 07:07:39 +00:00
|
|
|
* Return the service address
|
|
|
|
*
|
|
|
|
* @return string
|
2019-06-29 00:14:12 +00:00
|
|
|
*/
|
2022-04-19 07:07:39 +00:00
|
|
|
public function getServiceDescriptionAttribute(): string
|
2018-11-21 03:37:17 +00:00
|
|
|
{
|
2022-04-19 07:07:39 +00:00
|
|
|
return strtoupper($this->service_address) ?: '-';
|
2018-11-21 03:37:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 04:47:24 +00:00
|
|
|
/**
|
2022-04-19 07:07:39 +00:00
|
|
|
* Return the service number
|
2020-02-05 04:47:24 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-04-19 07:07:39 +00:00
|
|
|
public function getServiceNameAttribute(): string
|
2018-05-20 12:53:14 +00:00
|
|
|
{
|
2022-04-19 07:07:39 +00:00
|
|
|
return $this->service_number ?: ($this->service_address ?: '-');
|
2020-02-05 04:47:24 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 07:07:39 +00:00
|
|
|
/**
|
2022-04-20 06:24:58 +00:00
|
|
|
* The usage information for broadband
|
2022-04-19 07:07:39 +00:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function traffic()
|
2021-07-13 02:31:56 +00:00
|
|
|
{
|
2022-04-20 06:24:58 +00:00
|
|
|
return $this->hasMany(UsageBroadband::class,'service_item_id')
|
|
|
|
->where('site_id',$this->site_id);
|
2021-07-13 02:31:56 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 07:07:39 +00:00
|
|
|
/* ATTRIBUTES */
|
|
|
|
|
2020-02-05 04:47:24 +00:00
|
|
|
/**
|
2022-04-19 07:07:39 +00:00
|
|
|
* @deprecated use $o->service_name;
|
|
|
|
* @return mixed|string
|
2020-02-05 04:47:24 +00:00
|
|
|
*/
|
2022-04-19 07:07:39 +00:00
|
|
|
public function getNameAttribute()
|
2020-02-05 04:47:24 +00:00
|
|
|
{
|
2022-04-19 07:07:39 +00:00
|
|
|
abort(500,'deprecated - use $o->service_name');
|
2021-07-19 06:42:37 +00:00
|
|
|
return $this->service_number ?: $this->service_address;
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|
2019-06-29 00:14:12 +00:00
|
|
|
|
2020-02-05 04:47:24 +00:00
|
|
|
/**
|
2022-04-19 07:07:39 +00:00
|
|
|
* The type of technology used to provide this Internet Service
|
2020-02-05 04:47:24 +00:00
|
|
|
*
|
2022-04-19 07:07:39 +00:00
|
|
|
* @param $value
|
|
|
|
* @return null|string
|
2020-02-05 04:47:24 +00:00
|
|
|
*/
|
2022-04-19 07:07:39 +00:00
|
|
|
public function getTechnologyAttribute($value): ?string
|
2019-06-29 00:14:12 +00:00
|
|
|
{
|
2022-04-19 07:07:39 +00:00
|
|
|
return $value ?: $this->supplied()->technology;
|
2019-06-29 00:14:12 +00:00
|
|
|
}
|
2021-02-17 13:22:50 +00:00
|
|
|
|
2022-08-01 10:34:10 +00:00
|
|
|
/* OVERRIDES */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Service update validation
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function validation(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'service_number' => 'nullable|string|min:10|max:10',
|
2022-09-29 07:45:43 +00:00
|
|
|
'service_address' => 'nullable|string|min:3',
|
2022-08-01 10:34:10 +00:00
|
|
|
'service_username' => 'nullable|string',
|
|
|
|
'service_password' => 'nullable|string',
|
|
|
|
'connect_at' => 'nullable|date',
|
|
|
|
'start_at' => 'nullable|date',
|
|
|
|
'expire_at' => 'nullable|date|after:start_at',
|
|
|
|
'ipaddress' => 'nullable|ipv4',
|
|
|
|
'ip6address' => ['nullable',new IPv6_CIDR],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-04-19 07:07:39 +00:00
|
|
|
/* METHODS */
|
|
|
|
|
2021-12-24 01:14:01 +00:00
|
|
|
/**
|
|
|
|
* Return the suppliers offering that this service is providing
|
|
|
|
*
|
2022-04-22 01:47:46 +00:00
|
|
|
* @return SupplierType
|
2022-06-12 01:21:20 +00:00
|
|
|
* @todo This column provided_adsl_plan_id should either be deprecated or renamed.
|
2021-12-24 01:14:01 +00:00
|
|
|
*/
|
2022-04-22 01:47:46 +00:00
|
|
|
public function supplied(): SupplierType
|
2021-12-24 01:14:01 +00:00
|
|
|
{
|
|
|
|
return $this->provided_adsl_plan_id
|
|
|
|
? SupplierBroadband::findOrFail($this->provided_adsl_plan_id)
|
2022-06-12 01:21:20 +00:00
|
|
|
: $this->service->offering->supplied;
|
2021-12-24 01:14:01 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 13:22:50 +00:00
|
|
|
/**
|
|
|
|
* Return service usage data
|
|
|
|
*
|
|
|
|
* @param int $days
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function usage(int $days=31): Collection
|
|
|
|
{
|
2021-03-12 10:08:40 +00:00
|
|
|
$maxdate = $this->usage_last_date();
|
2021-02-17 13:22:50 +00:00
|
|
|
|
2021-03-12 10:08:40 +00:00
|
|
|
if (! $maxdate)
|
|
|
|
return collect();
|
|
|
|
|
2021-02-17 13:22:50 +00:00
|
|
|
return $this->traffic()
|
2021-03-12 10:08:40 +00:00
|
|
|
->where('date','<=',$maxdate->date->format('Y-m-d'))
|
|
|
|
->where('date','>=',$maxdate->date->subDays($days)->format('Y-m-d'))
|
2021-02-17 13:22:50 +00:00
|
|
|
->get();
|
|
|
|
}
|
2021-03-12 10:08:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the last date any traffic was recorded for a service
|
|
|
|
*
|
2022-04-20 06:24:58 +00:00
|
|
|
* @return UsageBroadband|null
|
2021-03-12 10:08:40 +00:00
|
|
|
*/
|
2022-04-20 06:24:58 +00:00
|
|
|
private function usage_last_date(): ?UsageBroadband
|
2021-03-12 10:08:40 +00:00
|
|
|
{
|
|
|
|
return $this->traffic
|
|
|
|
->sortBy('date')
|
|
|
|
->last();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function usage_summary(int $months=2): Collection
|
|
|
|
{
|
|
|
|
$maxdate = $this->usage_last_date();
|
|
|
|
|
|
|
|
if (! $maxdate)
|
|
|
|
return collect();
|
|
|
|
|
|
|
|
Log::debug(sprintf('%s:Getting Usage data for [%d] months from [%s]',self::LOGKEY,$months,$maxdate),['m'=>__METHOD__]);
|
|
|
|
|
|
|
|
// Go back an extra month;
|
|
|
|
$start = $maxdate->date->subMonths($months);
|
|
|
|
|
|
|
|
// If we are before the 15th
|
|
|
|
if ($start->day < 15) {
|
2021-03-12 10:50:42 +00:00
|
|
|
$start = Carbon::createFromFormat('Y-m-d',$start->subMonth()->format('Y-m-').'15');
|
2021-03-12 10:08:40 +00:00
|
|
|
} else {
|
|
|
|
$start = $start->subDays($start->day-15);
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::debug(sprintf('%s:Getting Usage data from [%s]',self::LOGKEY,$start->format('Y-m-d')),['m'=>__METHOD__]);
|
|
|
|
|
|
|
|
$result = collect();
|
|
|
|
|
|
|
|
foreach ($this->traffic()
|
|
|
|
->where('date','>=',$start->format('Y-m-d'))
|
|
|
|
->where('date','<=',$maxdate->date->format('Y-m-d'))
|
|
|
|
->get()->groupBy(function($item) {
|
2021-03-12 11:59:19 +00:00
|
|
|
return sprintf('%s->%s',$item->trafficMonthStart->format('d M'),$item->trafficMonthEnd->format('d M'));
|
2021-03-12 10:08:40 +00:00
|
|
|
}) as $key => $o) {
|
|
|
|
$result->put($key,$o->sum('total'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2022-04-19 07:07:39 +00:00
|
|
|
}
|