osb/app/Models/Service/Adsl.php

119 lines
2.5 KiB
PHP
Raw Normal View History

2018-05-20 12:53:14 +00:00
<?php
namespace App\Models\Service;
2018-05-20 12:53:14 +00:00
use Illuminate\Support\Collection;
2020-02-05 04:47:24 +00:00
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Interfaces\{ServiceItem,ServiceUsage};
use App\Models\AdslSupplierPlan;
2020-02-05 04:47:24 +00:00
use App\Models\Base\ServiceType;
use App\Traits\NextKey;
class Adsl extends ServiceType implements ServiceItem,ServiceUsage
2018-05-20 12:53:14 +00:00
{
private const LOGKEY = 'MSA';
use NextKey;
const RECORD_ID = 'service__adsl';
2020-02-05 04:47:24 +00:00
protected $dates = [
'service_connect_date',
'service_contract_date'
];
2020-02-19 12:37:45 +00:00
protected $table = 'ab_service__adsl';
2018-05-20 12:53:14 +00:00
2020-05-28 05:08:13 +00:00
/** RELATIONSHIPS **/
/**
* The accounts that this user manages
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function traffic()
{
// @todo Need to include site_id in this relation
2020-05-28 05:08:13 +00:00
return $this->hasMany(AdslTraffic::class,'ab_service_adsl_id');
}
2019-06-29 00:14:12 +00:00
/** SCOPES */
/**
* Search for a record
*
* @param $query
* @param string $term
* @return
*/
public function scopeSearch($query,string $term)
{
// Build our where clause
return parent::scopeSearch($query,$term)
->orwhere('service_number','like','%'.$term.'%')
->orWhere('service_address','like','%'.$term.'%')
->orWhere('ipaddress','like','%'.$term.'%');
}
/** ATTRIBUTES **/
/**
2020-02-05 04:47:24 +00:00
* @deprecated use $o->type()->service_name;
* @return mixed|string
2019-06-29 00:14:12 +00:00
*/
2020-02-05 04:47:24 +00:00
public function getNameAttribute()
2018-11-21 03:37:17 +00:00
{
2020-02-05 04:47:24 +00:00
return $this->service_number ?: $this->service_address;
2018-11-21 03:37:17 +00:00
}
2020-02-05 04:47:24 +00:00
/**
* Return the service address
*
* @return string
*/
public function getServiceDescriptionAttribute(): string
2018-05-20 12:53:14 +00:00
{
2020-02-08 11:51:50 +00:00
return strtoupper($this->service_address) ?: 'NO Service Address';
2020-02-05 04:47:24 +00:00
}
/**
* Return the service number
*
* @return string
*/
public function getServiceNameAttribute(): string
{
return $this->service_number ?: 'NO Service Number';
2018-05-20 12:53:14 +00:00
}
2019-06-29 00:14:12 +00:00
2020-02-05 04:47:24 +00:00
/**
* Is this service currently in a contract
*
* @return bool
*/
public function inContract(): bool
2019-06-29 00:14:12 +00:00
{
2020-02-05 04:47:24 +00:00
return $this->service_contract_date AND $this->service_contract_date->addMonths($this->contract_term)->isFuture();
2019-06-29 00:14:12 +00:00
}
/**
* Return service usage data
*
* @param int $days
* @return Collection
*/
public function usage(int $days=31): Collection
{
$maxdate = self::traffic()
->select(DB::raw('max(date) as max'))
->pluck('max')->pop();
Log::debug(sprintf('%s:Getting Usage data for [%d] days from [%s]',self::LOGKEY,$days,$maxdate),['m'=>__METHOD__]);
return $this->traffic()
->where('date','<=',$maxdate)
->where('date','>=',DB::raw(sprintf('date_sub(\'%s\',INTERVAL %s DAY)',$maxdate,$days)))
->get();
}
}