Add Compoships for multile key relationships, first implemented with Service::class

This commit is contained in:
Deon George
2022-09-29 17:26:03 +10:00
parent 2a19f14adb
commit ec99a5ff75
10 changed files with 99 additions and 43 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Models;
use Awobaz\Compoships\Compoships;
use Carbon\Carbon;
use Exception;
use Illuminate\Database\Eloquent\Casts\AsCollection;
@@ -53,7 +54,7 @@ use App\Traits\SiteID;
*/
class Service extends Model implements IDs
{
use HasFactory,ScopeServiceUserAuthorised,SiteID;
use HasFactory,ScopeServiceUserAuthorised,SiteID,Compoships;
protected $casts = [
'order_info'=>AsCollection::class,
@@ -325,11 +326,8 @@ class Service extends Model implements IDs
*/
public function account()
{
return $this->belongsTo(Account::class)
->when($this->site_id,function($q) {
return $q->where('site_id', $this->site_id)
->withoutGlobalScope(SiteScope::class);
});
return $this->belongsTo(Account::class,['account_id','site_id'],['id','site_id'])
->withoutGlobalScope(SiteScope::class);
}
/**
@@ -349,11 +347,8 @@ class Service extends Model implements IDs
*/
public function charges()
{
return $this->hasMany(Charge::class)
->when($this->site_id,function($q) {
return $q->where('site_id', $this->site_id)
->withoutGlobalScope(SiteScope::class);
})
return $this->hasMany(Charge::class,['service_id','site_id'],['id','site_id'])
->withoutGlobalScope(SiteScope::class)
->where('active','=',TRUE)
->orderBy('created_at');
}
@@ -361,11 +356,8 @@ class Service extends Model implements IDs
// @todo changed to invoiced_items
public function invoice_items($active=TRUE)
{
$query = $this->hasMany(InvoiceItem::class)
->when($this->site_id,function($q) {
return $q->where('site_id', $this->site_id)
->withoutGlobalScope(SiteScope::class);
})
$query = $this->hasMany(InvoiceItem::class,['service_id','site_id'],['id','site_id'])
->withoutGlobalScope(SiteScope::class)
->where('item_type','=',0)
->orderBy('start_at');
@@ -383,7 +375,7 @@ class Service extends Model implements IDs
{
$query = $this->hasManyThrough(Invoice::class,InvoiceItem::class,NULL,'id',NULL,'invoice_id')
->when($this->site_id,function($q) {
return $q->where('site_id', $this->site_id)
return $q->where('invoices.site_id', $this->site_id)
->withoutGlobalScope(SiteScope::class);
})
->distinct('id')
@@ -406,11 +398,8 @@ class Service extends Model implements IDs
*/
public function orderedby()
{
return $this->belongsTo(Account::class)
->when($this->site_id,function($q) {
return $q->where('site_id', $this->site_id)
->withoutGlobalScope(SiteScope::class);
});
return $this->belongsTo(Account::class,['ordered_by','site_id'],['id','site_id'])
->withoutGlobalScope(SiteScope::class);
}
/**
@@ -420,11 +409,8 @@ class Service extends Model implements IDs
*/
public function product()
{
return $this->belongsTo(Product::class)
->when($this->site_id,function($q) {
return $q->where('site_id', $this->site_id)
->withoutGlobalScope(SiteScope::class);
});
return $this->belongsTo(Product::class,['product_id','site_id'],['id','site_id'])
->withoutGlobalScope(SiteScope::class);
}
/**
@@ -434,11 +420,7 @@ class Service extends Model implements IDs
*/
public function type()
{
return $this->morphTo(null,'model','id','service_id')
->when($this->site_id,function($q) {
return $q->where('site_id', $this->site_id)
->withoutGlobalScope(SiteScope::class);
});
return $this->morphTo(null,'model','id','service_id');
}
/* SCOPES */