Add supplier linking

This commit is contained in:
Deon George
2022-08-07 12:17:20 +10:00
parent a1fd36aa6f
commit 2722c92bcf
10 changed files with 339 additions and 11 deletions

View File

@@ -8,6 +8,7 @@ use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Leenooks\Traits\ScopeActive;
use App\Models\Scopes\SiteScope;
use App\Models\Supplier\{Broadband,Domain,Email,Ethernet,Generic,Host,HSPA,Phone,SSL,Type};
class Supplier extends Model
@@ -98,17 +99,72 @@ class Supplier extends Model
/* 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".
/**
* Supplier Detail Configuration
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function detail()
{
return $this->hasOne(SupplierDetail::class)
->withoutGlobalScope(\App\Models\Scopes\SiteScope::class);
->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;
}
/* METHODS */
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);
}
/**
* Return the traffic records, that were not matched to a service.
*

View File

@@ -161,6 +161,13 @@ class User extends Authenticatable implements IDs
->active();
}
public function suppliers()
{
return $this->belongsToMany(Supplier::class)
->where('supplier_user.site_id',$this->site_id)
->withPivot('id','created_at');
}
/* ATTRIBUTES */
/**
@@ -231,7 +238,7 @@ class User extends Authenticatable implements IDs
});
} elseif (is_numeric($term)) {
$query->where('id','like','%'.$term.'%');
$query->where('users.id','like','%'.$term.'%');
} elseif (preg_match('/\@/',$term)) {
$query->where('email','like','%'.$term.'%');