osb/app/Models/Supplier.php

125 lines
3.0 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,Type};
class Supplier extends Model
{
/**
* The offerings types we provide
*/
private const offering_types = [
'broadband' => Broadband::class,
'hspa' => HSPA::class,
'ethernet' => Ethernet::class,
'domainname' => Domain::class,
'email' => Email::class,
'generic' => Generic::class,
'hosting' => Host::class,
'phone' => Phone::class,
'ssl' => SSL::class,
];
use ScopeActive;
public $timestamps = FALSE;
/* STATIC METHODS */
/**
* Return the offerings that this supplier provides
*
* @param Supplier|null $so
* @return Collection
*/
public static function offeringTypes(self $so=NULL): Collection
{
$result = collect();
foreach (self::offering_types as $type) {
$class = new $type;
if ($so) {
// If we have a connections configuration for that supplier, then build the child relationships
if (Arr::get($so->detail->connections,$class->category)) {
$result->put($class->category,(object)[
'type' => $class->category_name,
'items' => $class->where('supplier_detail_id',$so->detail->id),
]);
continue;
}
// Even if we dont have any connections, see if we have any products defined
$o = new $class;
$o->where('supplier_detail_id',$so->detail->id);
if ($o->count())
$result->put($class->category,(object)[
'type' => $class->category_name,
'items' => $class->where('supplier_detail_id',$so->detail->id),
]);
} else {
$result->put($class->category_name,$class);
}
}
return $result;
}
/**
* Return a new model object for the offering type
*
* @param string $type
* @return Type
*/
public static function offeringTypeClass(string $type): Type
{
return ($class=collect(self::offering_types)->get($type)) ? new $class : new Generic;
}
/**
* Return our supported offering type keys
*
* @return Collection
*/
public static function offeringTypeKeys(): Collection
{
return collect(self::offering_types)->keys();
}
/* 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 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();
}
}