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.
*

View File

@ -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();

View File

@ -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;
}

View File

@ -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);
}
}

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 }}" />
@endif
@if (($parent = $document->parent))
@if ($parent = $document->parent)
@php
$recurring_message = trans('recurring.message_parent', [
'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 }}" />
@endif
@if (($parent = $transaction->parent))
@if ($parent = $transaction->parent)
@php
$recurring_message = trans('recurring.message_parent', [
'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