Add Compoships for multile key relationships, first implemented with Service::class
This commit is contained in:
@@ -2,10 +2,12 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Awobaz\Compoships\Compoships;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Leenooks\Traits\ScopeActive;
|
||||
|
||||
use App\Models\Scopes\SiteScope;
|
||||
use App\Interfaces\IDs;
|
||||
use App\Traits\SiteID;
|
||||
|
||||
@@ -23,8 +25,7 @@ use App\Traits\SiteID;
|
||||
*/
|
||||
class Account extends Model implements IDs
|
||||
{
|
||||
use SiteID;
|
||||
use HasFactory,ScopeActive;
|
||||
use Compoships,HasFactory,ScopeActive,SiteID;
|
||||
|
||||
/* INTERFACES */
|
||||
|
||||
@@ -87,7 +88,8 @@ class Account extends Model implements IDs
|
||||
|
||||
public function services($active=FALSE)
|
||||
{
|
||||
$query = $this->hasMany(Service::class);
|
||||
$query = $this->hasMany(Service::class,['account_id','site_id'],['id','site_id'])
|
||||
->withoutGlobalScope(SiteScope::class);
|
||||
|
||||
return $active ? $query->active() : $query;
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Awobaz\Compoships\Compoships;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
@@ -16,7 +17,7 @@ use App\Traits\SiteID;
|
||||
*/
|
||||
class Charge extends Model
|
||||
{
|
||||
use SiteID;
|
||||
use Compoships,SiteID;
|
||||
|
||||
protected $casts = [
|
||||
'attributes' => 'json',
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Awobaz\Compoships\Compoships;
|
||||
use Carbon\Carbon;
|
||||
use Clarkeash\Doorman\Facades\Doorman;
|
||||
use Clarkeash\Doorman\Models\Invite;
|
||||
@@ -10,7 +11,7 @@ use Illuminate\Support\Arr;
|
||||
use Leenooks\Traits\ScopeActive;
|
||||
|
||||
use App\Interfaces\IDs;
|
||||
use App\Traits\{NextKey,PushNew};
|
||||
use App\Traits\PushNew;
|
||||
|
||||
/**
|
||||
* Class Invoice
|
||||
@@ -33,7 +34,7 @@ use App\Traits\{NextKey,PushNew};
|
||||
*/
|
||||
class Invoice extends Model implements IDs
|
||||
{
|
||||
use PushNew,ScopeActive;
|
||||
use Compoships,PushNew,ScopeActive;
|
||||
|
||||
protected $casts = [
|
||||
'reminders'=>'json',
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Awobaz\Compoships\Compoships;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Arr;
|
||||
@@ -20,7 +21,7 @@ use App\Traits\PushNew;
|
||||
*/
|
||||
class InvoiceItem extends Model
|
||||
{
|
||||
use PushNew;
|
||||
use Compoships,PushNew;
|
||||
|
||||
protected $dates = [
|
||||
'start_at',
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Awobaz\Compoships\Compoships;
|
||||
use Illuminate\Container\Container;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
@@ -66,7 +67,7 @@ use App\Traits\{ProductDetails,SiteID};
|
||||
*/
|
||||
class Product extends Model implements IDs
|
||||
{
|
||||
use HasFactory,SiteID,ProductDetails,ScopeActive;
|
||||
use Compoships,HasFactory,SiteID,ProductDetails,ScopeActive;
|
||||
|
||||
protected $casts = [
|
||||
'pricing'=>'collection',
|
||||
|
@@ -17,7 +17,12 @@ class SiteScope implements Scope
|
||||
*/
|
||||
public function apply(Builder $builder, Model $model)
|
||||
{
|
||||
// @todo Need to only do this, if the original query doesnt already have a where condition with a site_id
|
||||
$builder->where($model->getTable().'.site_id',config('site')->site_id);
|
||||
$builder->when(
|
||||
! collect($builder->getQuery()->wheres)->pluck('column')->contains(function($item) { return preg_match('/^(.*[^.]\.)?site_id/',$item); }),
|
||||
function($q) use ($model)
|
||||
{
|
||||
return $q->where($model->getTable().'.site_id',config('site')->site_id);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
@@ -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 */
|
||||
|
@@ -15,6 +15,7 @@ use Leenooks\Traits\ScopeActive;
|
||||
use Leenooks\Traits\UserSwitch;
|
||||
|
||||
use App\Interfaces\IDs;
|
||||
use App\Models\Scopes\SiteScope;
|
||||
use App\Notifications\ResetPassword as ResetPasswordNotification;
|
||||
use App\Traits\{QueryCacheableConfig,SiteID};
|
||||
|
||||
@@ -412,6 +413,7 @@ class User extends Authenticatable implements IDs
|
||||
->groupBy(['invoice_id']);
|
||||
|
||||
$summary = (new Invoice)
|
||||
->withoutGlobalScope(SiteScope::class)
|
||||
->select([
|
||||
'invoice_id',
|
||||
DB::raw('SUM(discount) AS discount'),
|
||||
|
Reference in New Issue
Block a user