improved default scopes

This commit is contained in:
Denis Duliçi 2022-06-10 19:26:32 +03:00
parent 5890a99bf9
commit 92065bd49d
13 changed files with 68 additions and 79 deletions

View File

@ -51,12 +51,12 @@ class Account extends Model
public function expense_transactions()
{
return $this->transactions()->whereIn('type', (array) $this->getExpenseTypes());
return $this->transactions()->whereIn('transactions.type', (array) $this->getExpenseTypes());
}
public function income_transactions()
{
return $this->transactions()->whereIn('type', (array) $this->getIncomeTypes());
return $this->transactions()->whereIn('transactions.type', (array) $this->getIncomeTypes());
}
public function transactions()

View File

@ -98,7 +98,7 @@ class Transaction extends Model
public function bill()
{
return $this->belongsTo('App\Models\Document\Document', 'document_id');
return $this->belongsTo('App\Models\Document\Document', 'document_id')->withoutGlobalScope('App\Scopes\Document');
}
public function category()
@ -123,12 +123,12 @@ class Transaction extends Model
public function invoice()
{
return $this->belongsTo('App\Models\Document\Document', 'document_id');
return $this->belongsTo('App\Models\Document\Document', 'document_id')->withoutGlobalScope('App\Scopes\Document');
}
public function document()
{
return $this->belongsTo('App\Models\Document\Document', 'document_id');
return $this->belongsTo('App\Models\Document\Document', 'document_id')->withoutGlobalScope('App\Scopes\Document');
}
public function parent()

View File

@ -54,34 +54,42 @@ class Transfer extends Model
public function expense_transaction()
{
return $this->belongsTo('App\Models\Banking\Transaction', 'expense_transaction_id')->withDefault(['name' => trans('general.na')]);
return $this->belongsTo('App\Models\Banking\Transaction', 'expense_transaction_id')
->withoutGlobalScope('App\Scopes\Transaction')
->withDefault(['name' => trans('general.na')]);
}
public function expense_account()
{
return $this->belongsToThrough(
'App\Models\Banking\Account',
'App\Models\Banking\Transaction',
null,
'',
['App\Models\Banking\Transaction' => 'expense_transaction_id']
)->withDefault(['name' => trans('general.na')]);
'App\Models\Banking\Account',
'App\Models\Banking\Transaction',
null,
'',
['App\Models\Banking\Transaction' => 'expense_transaction_id']
)
->withoutGlobalScope('App\Scopes\Transaction')
->withDefault(['name' => trans('general.na')]);
}
public function income_transaction()
{
return $this->belongsTo('App\Models\Banking\Transaction', 'income_transaction_id')->withDefault(['name' => trans('general.na')]);
return $this->belongsTo('App\Models\Banking\Transaction', 'income_transaction_id')
->withoutGlobalScope('App\Scopes\Transaction')
->withDefault(['name' => trans('general.na')]);
}
public function income_account()
{
return $this->belongsToThrough(
'App\Models\Banking\Account',
'App\Models\Banking\Transaction',
null,
'',
['App\Models\Banking\Transaction' => 'income_transaction_id']
)->withDefault(['name' => trans('general.na')]);
'App\Models\Banking\Account',
'App\Models\Banking\Transaction',
null,
'',
['App\Models\Banking\Transaction' => 'income_transaction_id']
)
->withoutGlobalScope('App\Scopes\Transaction')
->withDefault(['name' => trans('general.na')]);
}
/**

View File

@ -94,7 +94,7 @@ class Contact extends Model
public function bills()
{
return $this->documents()->where('type', Document::BILL_TYPE);
return $this->documents()->where('documents.type', Document::BILL_TYPE);
}
public function currency()
@ -104,17 +104,17 @@ class Contact extends Model
public function expense_transactions()
{
return $this->transactions()->whereIn('type', (array) $this->getExpenseTypes());
return $this->transactions()->whereIn('transactions.type', (array) $this->getExpenseTypes());
}
public function income_transactions()
{
return $this->transactions()->whereIn('type', (array) $this->getIncomeTypes());
return $this->transactions()->whereIn('transactions.type', (array) $this->getIncomeTypes());
}
public function invoices()
{
return $this->documents()->where('type', Document::INVOICE_TYPE);
return $this->documents()->where('documents.type', Document::INVOICE_TYPE);
}
public function transactions()

View File

@ -17,7 +17,7 @@ class DocumentHistory extends Model
public function document()
{
return $this->belongsTo('App\Models\Document\Document');
return $this->belongsTo('App\Models\Document\Document')->withoutGlobalScope('App\Scopes\Document');
}
public function scopeType(Builder $query, string $type)

View File

@ -69,7 +69,7 @@ class DocumentItem extends Model
public function document()
{
return $this->belongsTo('App\Models\Document\Document');
return $this->belongsTo('App\Models\Document\Document')->withoutGlobalScope('App\Scopes\Document');
}
public function item()

View File

@ -26,7 +26,7 @@ class DocumentItemTax extends Model
public function document()
{
return $this->belongsTo('App\Models\Document\Document');
return $this->belongsTo('App\Models\Document\Document')->withoutGlobalScope('App\Scopes\Document');
}
public function item()

View File

@ -29,7 +29,7 @@ class DocumentTotal extends Model
public function document()
{
return $this->belongsTo('App\Models\Document\Document');
return $this->belongsTo('App\Models\Document\Document')->withoutGlobalScope('App\Scopes\Document');
}
public function scopeType(Builder $query, string $type)

View File

@ -112,22 +112,22 @@ class Category extends Model
public function bills()
{
return $this->documents()->where('type', Document::BILL_TYPE);
return $this->documents()->where('documents.type', Document::BILL_TYPE);
}
public function expense_transactions()
{
return $this->transactions()->whereIn('type', (array) $this->getExpenseTypes());
return $this->transactions()->whereIn('transactions.type', (array) $this->getExpenseTypes());
}
public function income_transactions()
{
return $this->transactions()->whereIn('type', (array) $this->getIncomeTypes());
return $this->transactions()->whereIn('transactions.type', (array) $this->getIncomeTypes());
}
public function invoices()
{
return $this->documents()->where('type', Document::INVOICE_TYPE);
return $this->documents()->where('documents.type', Document::INVOICE_TYPE);
}
public function items()

View File

@ -63,7 +63,7 @@ class Currency extends Model
public function bills()
{
return $this->documents()->where('type', Document::BILL_TYPE);
return $this->documents()->where('documents.type', Document::BILL_TYPE);
}
public function contacts()
@ -73,22 +73,22 @@ class Currency extends Model
public function customers()
{
return $this->contacts()->whereIn('type', (array) $this->getCustomerTypes());
return $this->contacts()->whereIn('contacts.type', (array) $this->getCustomerTypes());
}
public function expense_transactions()
{
return $this->transactions()->whereIn('type', (array) $this->getExpenseTypes());
return $this->transactions()->whereIn('transactions.type', (array) $this->getExpenseTypes());
}
public function income_transactions()
{
return $this->transactions()->whereIn('type', (array) $this->getIncomeTypes());
return $this->transactions()->whereIn('transactions.type', (array) $this->getIncomeTypes());
}
public function invoices()
{
return $this->documents()->where('type', Document::INVOICE_TYPE);
return $this->documents()->where('documents.type', Document::INVOICE_TYPE);
}
public function transactions()
@ -98,7 +98,7 @@ class Currency extends Model
public function vendors()
{
return $this->contacts()->whereIn('type', (array) $this->getVendorTypes());
return $this->contacts()->whereIn('contacts.type', (array) $this->getVendorTypes());
}
/**

View File

@ -55,12 +55,12 @@ class Tax extends Model
public function bill_items()
{
return $this->document_items()->where('type', Document::BILL_TYPE);
return $this->document_items()->where('documents.type', Document::BILL_TYPE);
}
public function invoice_items()
{
return $this->document_items()->where('type', Document::INVOICE_TYPE);
return $this->document_items()->where('documents.type', Document::INVOICE_TYPE);
}
public function scopeName($query, $name)

View File

@ -37,7 +37,7 @@ class Company implements Scope
}
// Skip if already exists
if ($this->scopeColumnExists($builder, 'company_id')) {
if ($this->scopeColumnExists($builder, '', 'company_id')) {
return;
}

View File

@ -8,17 +8,10 @@ use Illuminate\Support\Str;
trait Scopes
{
/**
* Apply the scope to a given Eloquent query builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function applyNotRecurringScope(Builder $builder, Model $model)
public function applyNotRecurringScope(Builder $builder, Model $model): void
{
// Skip if recurring already in query
if ($this->scopeValueExists($builder, 'type', '-recurring')) {
// Skip if type already set
if ($this->scopeColumnExists($builder, $model->getTable(), 'type')) {
return;
}
@ -26,17 +19,10 @@ trait Scopes
$builder->isNotRecurring();
}
/**
* Apply the scope to a given Eloquent query builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function applyNotSplitScope(Builder $builder, Model $model)
public function applyNotSplitScope(Builder $builder, Model $model): void
{
// Skip if split already in query
if ($this->scopeValueExists($builder, 'type', '-split')) {
// Skip if type already set
if ($this->scopeColumnExists($builder, $model->getTable(), 'type')) {
return;
}
@ -44,14 +30,7 @@ trait Scopes
$builder->isNotSplit();
}
/**
* Check if scope exists.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param $column
* @return boolean
*/
public function scopeColumnExists($builder, $column)
public function scopeColumnExists(Builder $builder, string $table, string $column): bool
{
$query = $builder->getQuery();
@ -63,9 +42,14 @@ trait Scopes
if (strstr($where['column'], '.')) {
$whr = explode('.', $where['column']);
$where['table'] = $whr[0];
$where['column'] = $whr[1];
}
if (! empty($where['table']) && ! empty($table) && ($where['table'] != $table)) {
continue;
}
if ($where['column'] != $column) {
continue;
}
@ -76,15 +60,7 @@ trait Scopes
return false;
}
/**
* Check if scope has the exact value.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param $column
* @param $value
* @return boolean
*/
public function scopeValueExists($builder, $column, $value)
public function scopeValueExists(Builder $builder, string $table, string $column, string $value): bool
{
$query = $builder->getQuery();
@ -96,9 +72,14 @@ trait Scopes
if (strstr($where['column'], '.')) {
$whr = explode('.', $where['column']);
$where['table'] = $whr[0];
$where['column'] = $whr[1];
}
if (! empty($where['table']) && ! empty($table) && ($where['table'] != $table)) {
continue;
}
if ($where['column'] != $column) {
continue;
}
@ -114,8 +95,8 @@ trait Scopes
}
// @deprecated version 3.0.0
public function scopeExists($builder, $column)
public function scopeExists($builder, $column): bool
{
return $this->scopeColumnExists($builder, $column);
return $this->scopeColumnExists($builder, '', $column);
}
}