2021-12-20 03:25:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2022-06-30 13:51:20 +00:00
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
2021-12-20 03:25:43 +00:00
|
|
|
|
2022-06-30 13:51:20 +00:00
|
|
|
use App\Models\Supplier\{Broadband,Generic,Phone,Type};
|
2021-12-20 03:25:43 +00:00
|
|
|
use App\Traits\SiteID;
|
|
|
|
|
|
|
|
class SupplierDetail extends Model
|
|
|
|
{
|
|
|
|
use SiteID;
|
|
|
|
|
2021-12-24 01:14:01 +00:00
|
|
|
protected $casts = [ 'connections'=>'collection' ];
|
|
|
|
|
2021-12-20 03:25:43 +00:00
|
|
|
/* RELATIONS */
|
|
|
|
|
2022-06-30 13:51:20 +00:00
|
|
|
public function broadbands()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Broadband::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function generics()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Generic::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function phones()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Phone::class);
|
|
|
|
}
|
|
|
|
|
2021-12-20 03:25:43 +00:00
|
|
|
public function supplier()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Supplier::class);
|
|
|
|
}
|
2022-06-30 13:51:20 +00:00
|
|
|
|
|
|
|
/* METHODS */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a supplier product of a particular type
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param int $id
|
|
|
|
* @return Type
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function find(string $type,int $id): Type
|
|
|
|
{
|
|
|
|
switch ($type) {
|
|
|
|
case 'broadband':
|
|
|
|
$item = $this->broadbands->where('id',$id);
|
|
|
|
break;
|
|
|
|
|
2022-08-20 13:01:03 +00:00
|
|
|
case 'phone':
|
|
|
|
$item = $this->phones->where('id',$id);
|
|
|
|
break;
|
|
|
|
|
2022-06-30 13:51:20 +00:00
|
|
|
default:
|
|
|
|
throw new \Exception('Unknown type: '.$type);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($item->count() !== 1)
|
|
|
|
throw new ModelNotFoundException(sprintf('Unknown Model of type [%s] with id [%d]',$type,$id));
|
|
|
|
|
|
|
|
return $item->pop();
|
|
|
|
}
|
2021-12-20 03:25:43 +00:00
|
|
|
}
|