fixed recurring route binding and scope

This commit is contained in:
Denis Duliçi 2022-06-05 14:07:00 +03:00
parent 5b3688aaac
commit 776bc44514
6 changed files with 56 additions and 26 deletions

View File

@ -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. * Create a new factory instance for the model.
* *

View File

@ -632,6 +632,24 @@ class Document extends Model
return $actions; 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 protected static function newFactory(): Factory
{ {
return DocumentFactory::new(); return DocumentFactory::new();

View File

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

View File

@ -4,6 +4,7 @@ namespace App\Traits;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
trait Scopes trait Scopes
{ {
@ -16,13 +17,8 @@ trait Scopes
*/ */
public function applyNotRecurringScope(Builder $builder, Model $model) public function applyNotRecurringScope(Builder $builder, Model $model)
{ {
// Skip if recurring is explicitly set // Skip if recurring already in query
if ($this->scopeEquals($builder, 'type', 'like', '%-recurring')) { if ($this->scopeValueExists($builder, 'type', '-recurring')) {
return;
}
// Skip if scope is already applied
if ($this->scopeEquals($builder, 'type', 'not like', '%-recurring')) {
return; return;
} }
@ -39,13 +35,8 @@ trait Scopes
*/ */
public function applyNotSplitScope(Builder $builder, Model $model) public function applyNotSplitScope(Builder $builder, Model $model)
{ {
// Skip if split is explicitly set // Skip if split already in query
if ($this->scopeEquals($builder, 'type', 'like', '%-split')) { if ($this->scopeValueExists($builder, 'type', '-split')) {
return;
}
// Skip if scope is already applied
if ($this->scopeEquals($builder, 'type', 'not like', '%-split')) {
return; return;
} }
@ -60,7 +51,7 @@ trait Scopes
* @param $column * @param $column
* @return boolean * @return boolean
*/ */
public function scopeExists($builder, $column) public function scopeColumnExists($builder, $column)
{ {
$query = $builder->getQuery(); $query = $builder->getQuery();
@ -90,14 +81,15 @@ trait Scopes
* *
* @param \Illuminate\Database\Eloquent\Builder $builder * @param \Illuminate\Database\Eloquent\Builder $builder
* @param $column * @param $column
* @param $value
* @return boolean * @return boolean
*/ */
public function scopeEquals($builder, $column, $operator, $value) public function scopeValueExists($builder, $column, $value)
{ {
$query = $builder->getQuery(); $query = $builder->getQuery();
foreach ((array) $query->wheres as $key => $where) { 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; continue;
} }
@ -111,11 +103,7 @@ trait Scopes
continue; continue;
} }
if ($where['operator'] != $operator) { if (! Str::endsWith($where['value'], $value)) {
continue;
}
if ($where['value'] != $value) {
continue; continue;
} }
@ -124,4 +112,10 @@ trait Scopes
return false; return false;
} }
// @deprecated version 3.0.0
public function scopeExists($builder, $column)
{
return $this->scopeColumnExists($builder, $column);
}
} }

View File

@ -14,7 +14,7 @@
<x-documents.show.message type="recurring" background-color="bg-blue-100" text-color="text-blue-600" message="{{ $recurring_message }}" /> <x-documents.show.message type="recurring" background-color="bg-blue-100" text-color="text-blue-600" message="{{ $recurring_message }}" />
@endif @endif
@if (($parent = $document->parent)) @if ($parent = $document->parent)
@php @php
$recurring_message = trans('recurring.message_parent', [ $recurring_message = trans('recurring.message_parent', [
'type' => mb_strtolower(trans_choice($textRecurringType, 1)), 'type' => mb_strtolower(trans_choice($textRecurringType, 1)),

View File

@ -14,11 +14,11 @@
<x-documents.show.message type="recurring" background-color="bg-blue-100" text-color="text-blue-600" message="{{ $recurring_message }}" /> <x-documents.show.message type="recurring" background-color="bg-blue-100" text-color="text-blue-600" message="{{ $recurring_message }}" />
@endif @endif
@if (($parent = $transaction->parent)) @if ($parent = $transaction->parent)
@php @php
$recurring_message = trans('recurring.message_parent', [ $recurring_message = trans('recurring.message_parent', [
'type' => mb_strtolower(trans_choice($textRecurringType, 1)), 'type' => mb_strtolower(trans_choice($textRecurringType, 1)),
'link' => '<a href="' . route($routeTransactionShow, $parent->id) . '"><u>' . $parent->number . '</u></a>' 'link' => '<a href="' . route(config('type.transaction.' . $transaction->parent->type . '.route.prefix') . '.show', $parent->id) . '"><u>' . $parent->number . '</u></a>'
]); ]);
@endphp @endphp