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() 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() public function income_transactions()
{ {
return $this->transactions()->whereIn('type', (array) $this->getIncomeTypes()); return $this->transactions()->whereIn('transactions.type', (array) $this->getIncomeTypes());
} }
public function transactions() public function transactions()

View File

@ -98,7 +98,7 @@ class Transaction extends Model
public function bill() 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() public function category()
@ -123,12 +123,12 @@ class Transaction extends Model
public function invoice() 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() 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() public function parent()

View File

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

View File

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

View File

@ -17,7 +17,7 @@ class DocumentHistory extends Model
public function document() 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) public function scopeType(Builder $query, string $type)

View File

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

View File

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

View File

@ -29,7 +29,7 @@ class DocumentTotal extends Model
public function document() 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) public function scopeType(Builder $query, string $type)

View File

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

View File

@ -63,7 +63,7 @@ class Currency extends Model
public function bills() public function bills()
{ {
return $this->documents()->where('type', Document::BILL_TYPE); return $this->documents()->where('documents.type', Document::BILL_TYPE);
} }
public function contacts() public function contacts()
@ -73,22 +73,22 @@ class Currency extends Model
public function customers() public function customers()
{ {
return $this->contacts()->whereIn('type', (array) $this->getCustomerTypes()); return $this->contacts()->whereIn('contacts.type', (array) $this->getCustomerTypes());
} }
public function expense_transactions() 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() public function income_transactions()
{ {
return $this->transactions()->whereIn('type', (array) $this->getIncomeTypes()); return $this->transactions()->whereIn('transactions.type', (array) $this->getIncomeTypes());
} }
public function invoices() public function invoices()
{ {
return $this->documents()->where('type', Document::INVOICE_TYPE); return $this->documents()->where('documents.type', Document::INVOICE_TYPE);
} }
public function transactions() public function transactions()
@ -98,7 +98,7 @@ class Currency extends Model
public function vendors() 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() 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() 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) public function scopeName($query, $name)

View File

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

View File

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