osb/app/Models/Supplier.php

126 lines
2.9 KiB
PHP

<?php
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Leenooks\Traits\ScopeActive;
use App\Models\Supplier\{Broadband,Domain,Email,Ethernet,Generic,Host,HSPA,Phone,SSL};
class Supplier extends Model
{
use ScopeActive;
public $timestamps = FALSE;
/**
* The offerings we provide
* @todo Use the product/* category instead of this const. The assumption is the supplier/* type is the same as the product/* type.
* @deprecated - use the product/* category instead.
*/
public const offering_types = [
'broadband' => [
'name' => 'Broadband',
'class' => Broadband::class,
],
'hspa' => [
'name' => 'Mobile Broadband',
'class' => HSPA::class,
],
'ethernet' => [
'name' => 'Ethernet Broadband',
'class' => Ethernet::class,
],
'domainname' => [
'name' => 'Domain Name',
'class' => Domain::class,
],
'email' => [
'name' => 'Email Hosting',
'class' => Email::class,
],
'generic' => [
'name' => 'Generic',
'class' => Generic::class,
],
'hosting' => [
'name' => 'Hosting',
'class' => Host::class,
],
'phone' => [
'name' => 'Phone',
'class' => Phone::class,
],
'ssl' => [
'name' => 'SSL',
'class' => SSL::class,
],
];
/* RELATIONS */
// @todo Need to put in an integrity constraint to support the hasOne()
// @todo Some suppliers have multiple different configuration urls/passwords and contacts for different types of services, perhaps this should be hasMany()?
// EG: Crazy Domains, "domains" and "hosting".
public function detail()
{
return $this->hasOne(SupplierDetail::class)
->withoutGlobalScope(\App\Models\Scopes\SiteScope::class);
}
/* METHODS */
/**
* Return the offerings that this supplier provides
*
* @return void
*/
public function offeringTypes(): Collection
{
$result = collect();
// See if we have any configurations
foreach (self::offering_types as $key => $type) {
if (! ($class=Arr::get($type,'class')))
continue;
if (Arr::get($this->detail->connections,$key)) {
$result->put($key,(object)[
'type' => Arr::get($type,'name'),
'items' => (new $class)->where('supplier_detail_id',$this->detail->id),
]);
continue;
}
// See if we have any products defined
$o = new $class;
$o->where('supplier_detail_id',$this->detail->id);
if ($o->count())
$result->put($key,(object)[
'type' => Arr::get($type,'name'),
'items' => (new $class)->where('supplier_detail_id',$this->detail->id),
]);
}
return $result;
}
/**
* Return the traffic records, that were not matched to a service.
*
* @param Carbon $date
* @return \Illuminate\Database\Eloquent\Collection
*/
public function trafficMismatch(Carbon $date): Collection
{
return Usage\Broadband::where('date',$date->format('Y-m-d'))
->where('supplier_id',$this->id)
->whereNULL('service_item_id')
->get();
}
}