2018-06-05 11:13:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2022-04-20 06:24:58 +00:00
|
|
|
use Carbon\Carbon;
|
2018-06-05 11:13:57 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2021-12-24 01:14:01 +00:00
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
use Illuminate\Support\Collection;
|
2021-12-20 03:25:43 +00:00
|
|
|
use Leenooks\Traits\ScopeActive;
|
2018-06-05 11:13:57 +00:00
|
|
|
|
2022-08-07 02:17:20 +00:00
|
|
|
use App\Models\Scopes\SiteScope;
|
2022-06-30 13:51:20 +00:00
|
|
|
use App\Models\Supplier\{Broadband,Domain,Email,Ethernet,Generic,Host,HSPA,Phone,SSL,Type};
|
2021-12-24 01:14:01 +00:00
|
|
|
|
2018-06-05 11:13:57 +00:00
|
|
|
class Supplier extends Model
|
|
|
|
{
|
2022-06-12 01:21:20 +00:00
|
|
|
/**
|
2022-06-30 13:51:20 +00:00
|
|
|
* The offerings types we provide
|
2022-06-12 01:21:20 +00:00
|
|
|
*/
|
2022-06-30 13:51:20 +00:00
|
|
|
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,
|
2021-12-24 01:14:01 +00:00
|
|
|
];
|
|
|
|
|
2022-06-30 13:51:20 +00:00
|
|
|
use ScopeActive;
|
2021-12-20 03:25:43 +00:00
|
|
|
|
2022-06-30 13:51:20 +00:00
|
|
|
public $timestamps = FALSE;
|
2021-12-24 01:14:01 +00:00
|
|
|
|
2022-06-30 13:51:20 +00:00
|
|
|
/* STATIC METHODS */
|
2021-12-24 01:14:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the offerings that this supplier provides
|
|
|
|
*
|
2022-06-30 13:51:20 +00:00
|
|
|
* @param Supplier|null $so
|
|
|
|
* @return Collection
|
2021-12-24 01:14:01 +00:00
|
|
|
*/
|
2022-06-30 13:51:20 +00:00
|
|
|
public static function offeringTypes(self $so=NULL): Collection
|
2021-12-24 01:14:01 +00:00
|
|
|
{
|
|
|
|
$result = collect();
|
|
|
|
|
2022-06-30 13:51:20 +00:00
|
|
|
foreach (self::offering_types as $type) {
|
|
|
|
$class = new $type;
|
2021-12-24 01:14:01 +00:00
|
|
|
|
2022-06-30 13:51:20 +00:00
|
|
|
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),
|
2021-12-24 01:14:01 +00:00
|
|
|
]);
|
|
|
|
|
2022-06-30 13:51:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-12-24 01:14:01 +00:00
|
|
|
|
2022-06-30 13:51:20 +00:00
|
|
|
// 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);
|
2021-12-24 01:14:01 +00:00
|
|
|
|
2022-06-30 13:51:20 +00:00
|
|
|
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);
|
|
|
|
}
|
2021-12-24 01:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2022-04-20 06:24:58 +00:00
|
|
|
|
2022-06-30 13:51:20 +00:00
|
|
|
/**
|
|
|
|
* 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 */
|
|
|
|
|
2022-08-07 02:17:20 +00:00
|
|
|
/**
|
|
|
|
* Supplier Detail Configuration
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
|
|
*/
|
2022-06-30 13:51:20 +00:00
|
|
|
public function detail()
|
|
|
|
{
|
|
|
|
return $this->hasOne(SupplierDetail::class)
|
2022-08-07 02:17:20 +00:00
|
|
|
->withoutGlobalScope(SiteScope::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function users()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(User::class)
|
|
|
|
->withPivot('id','created_at');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for a record
|
|
|
|
*
|
|
|
|
* @param $query
|
|
|
|
* @param string $term
|
|
|
|
*/
|
|
|
|
public function scopeSearch($query,string $term)
|
|
|
|
{
|
|
|
|
if (is_numeric($term)) {
|
|
|
|
$query->select('suppliers.*')
|
|
|
|
->join('supplier_user',['supplier_user.supplier_id'=>'suppliers.id'])
|
|
|
|
->orWhere('supplier_user.id','like','%'.$term.'%');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
2022-06-30 13:51:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* METHODS */
|
|
|
|
|
2022-08-07 02:17:20 +00:00
|
|
|
public function api_class(): ?string
|
|
|
|
{
|
|
|
|
return config('services.supplier.'.strtolower($this->name).'.api');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function api_key(): string
|
|
|
|
{
|
|
|
|
return Arr::get($this->detail->connections,'api_key');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function api_secret(): string
|
|
|
|
{
|
|
|
|
return Arr::get($this->detail->connections,'api_secret');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function API(bool $forceprod=FALSE): mixed
|
|
|
|
{
|
|
|
|
return $this->hasAPIdetails() ? new ($this->api_class())($this->api_key(),$this->api_secret(),$forceprod) : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do we have API details for this supplier
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasAPIdetails(): bool
|
|
|
|
{
|
|
|
|
return $this->api_class() && (collect(['api_key','api_secret'])->diff($this->detail->connections->keys())->count() === 0);
|
|
|
|
}
|
|
|
|
|
2022-08-10 05:18:56 +00:00
|
|
|
/**
|
|
|
|
* If this supplier has a domain registrar, return it.
|
|
|
|
*
|
|
|
|
* @return DomainRegistrar|null
|
|
|
|
*/
|
|
|
|
public function registrar(): ?DomainRegistrar
|
|
|
|
{
|
|
|
|
return ($x=config('services.supplier.'.strtolower($this->name).'.registrar')) ? DomainRegistrar::where('name',$x)->single() : NULL;
|
|
|
|
}
|
|
|
|
|
2022-04-20 06:24:58 +00:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
2021-12-20 03:25:43 +00:00
|
|
|
}
|