hasMany(AdslTraffic::class,'ab_service_adsl_id'); } /** 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 **/ /** * @deprecated use $o->type()->service_name; * @return mixed|string */ public function getNameAttribute() { return $this->service_number ?: $this->service_address; } /** * Return the service address * * @return string */ public function getServiceDescriptionAttribute(): string { return strtoupper($this->service_address) ?: 'NO Service Address'; } /** * Return the service number * * @return string */ public function getServiceNameAttribute(): string { return $this->service_number ?: 'NO Service Number'; } /** * Is this service currently in a contract * * @return bool */ public function inContract(): bool { return $this->service_contract_date AND $this->service_contract_date->addMonths($this->contract_term)->isFuture(); } /** * 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(); } }