From 776bc44514f6b56b8fc1e8c16a603aba507dca74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Duli=C3=A7i?= Date: Sun, 5 Jun 2022 14:07:00 +0300 Subject: [PATCH] fixed recurring route binding and scope --- app/Models/Banking/Transaction.php | 18 +++++++++ app/Models/Document/Document.php | 18 +++++++++ app/Scopes/Company.php | 2 +- app/Traits/Scopes.php | 38 ++++++++----------- .../documents/show/content.blade.php | 2 +- .../transactions/show/content.blade.php | 4 +- 6 files changed, 56 insertions(+), 26 deletions(-) diff --git a/app/Models/Banking/Transaction.php b/app/Models/Banking/Transaction.php index eee044630..f35e9e4b6 100644 --- a/app/Models/Banking/Transaction.php +++ b/app/Models/Banking/Transaction.php @@ -595,6 +595,24 @@ class Transaction extends Model }; } + /** + * Retrieve the model for a bound value. + * + * @param mixed $value + * @param string|null $field + * @return \Illuminate\Database\Eloquent\Model|null + */ + public function resolveRouteBinding($value, $field = null) + { + $query = $this->where('id', $value); + + if (request()->route()->hasParameter('recurring_transaction')) { + $query->isRecurring(); + } + + return $query->firstOrFail(); + } + /** * Create a new factory instance for the model. * diff --git a/app/Models/Document/Document.php b/app/Models/Document/Document.php index d271b53f0..489b441d5 100644 --- a/app/Models/Document/Document.php +++ b/app/Models/Document/Document.php @@ -632,6 +632,24 @@ class Document extends Model return $actions; } + /** + * Retrieve the model for a bound value. + * + * @param mixed $value + * @param string|null $field + * @return \Illuminate\Database\Eloquent\Model|null + */ + public function resolveRouteBinding($value, $field = null) + { + $query = $this->where('id', $value); + + if (request()->route()->hasParameter('recurring_invoice') || request()->route()->hasParameter('recurring_bill')) { + $query->isRecurring(); + } + + return $query->firstOrFail(); + } + protected static function newFactory(): Factory { return DocumentFactory::new(); diff --git a/app/Scopes/Company.php b/app/Scopes/Company.php index 0f27e6b53..a7f88ae4b 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->scopeExists($builder, 'company_id')) { + if ($this->scopeColumnExists($builder, 'company_id')) { return; } diff --git a/app/Traits/Scopes.php b/app/Traits/Scopes.php index 83621c0d5..b93a2dd93 100644 --- a/app/Traits/Scopes.php +++ b/app/Traits/Scopes.php @@ -4,6 +4,7 @@ namespace App\Traits; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Str; trait Scopes { @@ -16,13 +17,8 @@ trait Scopes */ public function applyNotRecurringScope(Builder $builder, Model $model) { - // Skip if recurring is explicitly set - if ($this->scopeEquals($builder, 'type', 'like', '%-recurring')) { - return; - } - - // Skip if scope is already applied - if ($this->scopeEquals($builder, 'type', 'not like', '%-recurring')) { + // Skip if recurring already in query + if ($this->scopeValueExists($builder, 'type', '-recurring')) { return; } @@ -39,13 +35,8 @@ trait Scopes */ public function applyNotSplitScope(Builder $builder, Model $model) { - // Skip if split is explicitly set - if ($this->scopeEquals($builder, 'type', 'like', '%-split')) { - return; - } - - // Skip if scope is already applied - if ($this->scopeEquals($builder, 'type', 'not like', '%-split')) { + // Skip if split already in query + if ($this->scopeValueExists($builder, 'type', '-split')) { return; } @@ -60,7 +51,7 @@ trait Scopes * @param $column * @return boolean */ - public function scopeExists($builder, $column) + public function scopeColumnExists($builder, $column) { $query = $builder->getQuery(); @@ -90,14 +81,15 @@ trait Scopes * * @param \Illuminate\Database\Eloquent\Builder $builder * @param $column + * @param $value * @return boolean */ - public function scopeEquals($builder, $column, $operator, $value) + public function scopeValueExists($builder, $column, $value) { $query = $builder->getQuery(); foreach ((array) $query->wheres as $key => $where) { - if (empty($where) || empty($where['column']) || empty($where['operator']) || empty($where['value'])) { + if (empty($where) || empty($where['column']) || empty($where['value'])) { continue; } @@ -111,11 +103,7 @@ trait Scopes continue; } - if ($where['operator'] != $operator) { - continue; - } - - if ($where['value'] != $value) { + if (! Str::endsWith($where['value'], $value)) { continue; } @@ -124,4 +112,10 @@ trait Scopes return false; } + + // @deprecated version 3.0.0 + public function scopeExists($builder, $column) + { + return $this->scopeColumnExists($builder, $column); + } } diff --git a/resources/views/components/documents/show/content.blade.php b/resources/views/components/documents/show/content.blade.php index f455f7c96..be70205ef 100644 --- a/resources/views/components/documents/show/content.blade.php +++ b/resources/views/components/documents/show/content.blade.php @@ -14,7 +14,7 @@ @endif - @if (($parent = $document->parent)) + @if ($parent = $document->parent) @php $recurring_message = trans('recurring.message_parent', [ 'type' => mb_strtolower(trans_choice($textRecurringType, 1)), diff --git a/resources/views/components/transactions/show/content.blade.php b/resources/views/components/transactions/show/content.blade.php index 469c5aaa8..64d885530 100644 --- a/resources/views/components/transactions/show/content.blade.php +++ b/resources/views/components/transactions/show/content.blade.php @@ -14,11 +14,11 @@ @endif - @if (($parent = $transaction->parent)) + @if ($parent = $transaction->parent) @php $recurring_message = trans('recurring.message_parent', [ 'type' => mb_strtolower(trans_choice($textRecurringType, 1)), - 'link' => '' . $parent->number . '' + 'link' => '' . $parent->number . '' ]); @endphp