From ec99a5ff75bdfbcd862875672eb0a85f7be13241 Mon Sep 17 00:00:00 2001 From: Deon George Date: Thu, 29 Sep 2022 17:26:03 +1000 Subject: [PATCH] Add Compoships for multile key relationships, first implemented with Service::class --- app/Models/Account.php | 8 +++-- app/Models/Charge.php | 3 +- app/Models/Invoice.php | 5 +-- app/Models/InvoiceItem.php | 3 +- app/Models/Product.php | 3 +- app/Models/Scopes/SiteScope.php | 9 +++-- app/Models/Service.php | 46 ++++++++---------------- app/Models/User.php | 2 ++ composer.json | 1 + composer.lock | 62 ++++++++++++++++++++++++++++++++- 10 files changed, 99 insertions(+), 43 deletions(-) diff --git a/app/Models/Account.php b/app/Models/Account.php index 69c1ff5..f25e28e 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -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; } diff --git a/app/Models/Charge.php b/app/Models/Charge.php index 85528b0..0783b9f 100644 --- a/app/Models/Charge.php +++ b/app/Models/Charge.php @@ -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', diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index da5f976..e6a3b73 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -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', diff --git a/app/Models/InvoiceItem.php b/app/Models/InvoiceItem.php index ed3ff91..7b98122 100644 --- a/app/Models/InvoiceItem.php +++ b/app/Models/InvoiceItem.php @@ -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', diff --git a/app/Models/Product.php b/app/Models/Product.php index 66fdc98..e1d1c97 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -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', diff --git a/app/Models/Scopes/SiteScope.php b/app/Models/Scopes/SiteScope.php index 475dfa7..54e4727 100644 --- a/app/Models/Scopes/SiteScope.php +++ b/app/Models/Scopes/SiteScope.php @@ -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); + } + ); } } \ No newline at end of file diff --git a/app/Models/Service.php b/app/Models/Service.php index 4d987fe..6283153 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -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 */ diff --git a/app/Models/User.php b/app/Models/User.php index b356752..bb7626e 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -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'), diff --git a/composer.json b/composer.json index 5171345..c210466 100644 --- a/composer.json +++ b/composer.json @@ -9,6 +9,7 @@ "ext-curl": "*", "ext-pdo": "*", "ext-zlib": "*", + "awobaz/compoships": "^2.1", "barryvdh/laravel-snappy": "^1.0", "clarkeash/doorman": "^7.0", "doctrine/dbal": "^2.10", diff --git a/composer.lock b/composer.lock index a343c61..7e6634c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "be607de05c3acfcd58f3b71db50164a6", + "content-hash": "1c7f30fd0cea07af4f43d6ef6dcdab18", "packages": [ { "name": "asm89/stack-cors", @@ -62,6 +62,66 @@ }, "time": "2022-01-18T09:12:03+00:00" }, + { + "name": "awobaz/compoships", + "version": "2.1.4", + "source": { + "type": "git", + "url": "https://github.com/topclaudy/compoships.git", + "reference": "ba86741d9b439d1179a6432dded92b0ecc89a63a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/topclaudy/compoships/zipball/ba86741d9b439d1179a6432dded92b0ecc89a63a", + "reference": "ba86741d9b439d1179a6432dded92b0ecc89a63a", + "shasum": "" + }, + "require": { + "illuminate/database": ">=5.6 <10.0" + }, + "require-dev": { + "ext-sqlite3": "*" + }, + "suggest": { + "awobaz/blade-active": "Blade directives for the Laravel 'Active' package", + "awobaz/eloquent-auto-append": "Automatically append accessors to model serialization", + "awobaz/eloquent-mutators": "Reusable mutators (getters/setters) for Laravel 5's Eloquent", + "awobaz/syntactic": "Syntactic sugar for named and indexed parameters call." + }, + "type": "library", + "autoload": { + "psr-4": { + "Awobaz\\Compoships\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Claudin J. Daniel", + "email": "cdaniel@awobaz.com" + } + ], + "description": "Laravel relationships with support for composite/multiple keys", + "keywords": [ + "laravel", + "laravel composite keys", + "laravel relationships" + ], + "support": { + "issues": "https://github.com/topclaudy/compoships/issues", + "source": "https://github.com/topclaudy/compoships/tree/2.1.4" + }, + "funding": [ + { + "url": "https://paypal.me/awobaz", + "type": "custom" + } + ], + "time": "2022-06-22T11:42:37+00:00" + }, { "name": "barryvdh/laravel-snappy", "version": "v1.0.0",