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,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;
}

View File

@ -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',

View File

@ -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',

View File

@ -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',

View File

@ -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',

View File

@ -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);
}
);
}
}

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)
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)
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)
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 */

View File

@ -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'),

View File

@ -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",

62
composer.lock generated
View File

@ -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",