diff --git a/app/Models/Banking/Account.php b/app/Models/Banking/Account.php index 7ffafeeea..e2b31976d 100644 --- a/app/Models/Banking/Account.php +++ b/app/Models/Banking/Account.php @@ -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() diff --git a/app/Models/Banking/Transaction.php b/app/Models/Banking/Transaction.php index f35e9e4b6..013301fc2 100644 --- a/app/Models/Banking/Transaction.php +++ b/app/Models/Banking/Transaction.php @@ -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() diff --git a/app/Models/Banking/Transfer.php b/app/Models/Banking/Transfer.php index f9a37b9d3..bd1d92c53 100644 --- a/app/Models/Banking/Transfer.php +++ b/app/Models/Banking/Transfer.php @@ -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')]); } /** diff --git a/app/Models/Common/Contact.php b/app/Models/Common/Contact.php index 9d1453ee7..2c470f360 100644 --- a/app/Models/Common/Contact.php +++ b/app/Models/Common/Contact.php @@ -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() diff --git a/app/Models/Document/DocumentHistory.php b/app/Models/Document/DocumentHistory.php index e7245862c..ea9ac38c1 100644 --- a/app/Models/Document/DocumentHistory.php +++ b/app/Models/Document/DocumentHistory.php @@ -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) diff --git a/app/Models/Document/DocumentItem.php b/app/Models/Document/DocumentItem.php index d7b303f31..d4bb8d1cc 100644 --- a/app/Models/Document/DocumentItem.php +++ b/app/Models/Document/DocumentItem.php @@ -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() diff --git a/app/Models/Document/DocumentItemTax.php b/app/Models/Document/DocumentItemTax.php index 1db6aa2f8..70af4a2a0 100644 --- a/app/Models/Document/DocumentItemTax.php +++ b/app/Models/Document/DocumentItemTax.php @@ -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() diff --git a/app/Models/Document/DocumentTotal.php b/app/Models/Document/DocumentTotal.php index c6e2a4c18..8ef203f5c 100644 --- a/app/Models/Document/DocumentTotal.php +++ b/app/Models/Document/DocumentTotal.php @@ -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) diff --git a/app/Models/Setting/Category.php b/app/Models/Setting/Category.php index c3a85e64e..962e4e795 100644 --- a/app/Models/Setting/Category.php +++ b/app/Models/Setting/Category.php @@ -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() diff --git a/app/Models/Setting/Currency.php b/app/Models/Setting/Currency.php index 18aa2cde1..3b6ee0da1 100644 --- a/app/Models/Setting/Currency.php +++ b/app/Models/Setting/Currency.php @@ -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()); } /** diff --git a/app/Models/Setting/Tax.php b/app/Models/Setting/Tax.php index 5eb66775d..f20299ad3 100644 --- a/app/Models/Setting/Tax.php +++ b/app/Models/Setting/Tax.php @@ -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) diff --git a/app/Scopes/Company.php b/app/Scopes/Company.php index a7f88ae4b..5e2edb75c 100644 --- a/app/Scopes/Company.php +++ b/app/Scopes/Company.php @@ -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; } diff --git a/app/Traits/Scopes.php b/app/Traits/Scopes.php index b93a2dd93..6a40279e6 100644 --- a/app/Traits/Scopes.php +++ b/app/Traits/Scopes.php @@ -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); } }