From 50eba765d23f7879f39627b359682b5df10dea97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=BCneyt=20=C5=9Eent=C3=BCrk?= Date: Tue, 29 Aug 2023 10:14:22 +0300 Subject: [PATCH] Recurring model relation issue and new transaction relation, --- app/Models/Common/Recurring.php | 49 ++++++++++++++++++- .../recurring_transactions/index.blade.php | 12 ++++- .../index/recurring_templates.blade.php | 12 ++++- 3 files changed, 68 insertions(+), 5 deletions(-) diff --git a/app/Models/Common/Recurring.php b/app/Models/Common/Recurring.php index 41b851edd..1828f8cce 100644 --- a/app/Models/Common/Recurring.php +++ b/app/Models/Common/Recurring.php @@ -61,7 +61,13 @@ class Recurring extends Model public function documents() { - return $this->morphedByMany('App\Models\Document\Document', 'recurable'); + return $this->morphedByMany( + 'App\Models\Document\Document', + 'recurable', + 'recurring', + 'recurable_id', + 'id' + ); } public function invoices() @@ -74,6 +80,27 @@ class Recurring extends Model return $this->documents()->where('type', self::BILL_RECURRING_TYPE); } + public function transactions() + { + return $this->morphedByMany( + 'App\Models\Banking\Transaction', + 'recurable', + 'recurring', + 'recurable_id', + 'id' + ); + } + + public function incomes() + { + return $this->transactions()->where('type', self::INCOME_RECURRING_TYPE); + } + + public function expenses() + { + return $this->transactions()->where('type', self::EXPENSE_RECURRING_TYPE); + } + public function scopeActive(Builder $query): Builder { return $query->where($this->qualifyColumn('status'), '=', static::ACTIVE_STATUS); @@ -133,4 +160,24 @@ class Recurring extends Model $query->where('type', self::INCOME_RECURRING_TYPE); }); } + + /** + * Determine if recurring is a document. + * + * @return bool + */ + public function isDocument() + { + return (bool) ($this->recurable_type == 'App\\Models\\Document\\Document'); + } + + /** + * Determine if recurring is a transaction. + * + * @return bool + */ + public function isTransaction() + { + return (bool) ($this->recurable_type == 'App\\Models\\Banking\\Transaction'); + } } diff --git a/resources/views/banking/recurring_transactions/index.blade.php b/resources/views/banking/recurring_transactions/index.blade.php index 71b76b537..6258d49de 100644 --- a/resources/views/banking/recurring_transactions/index.blade.php +++ b/resources/views/banking/recurring_transactions/index.blade.php @@ -102,8 +102,16 @@ - @if ($last = $item->recurring->getLastRecurring()) - {{ $last->format(company_date_format()) }} + @if ($item->recurring->status == 'ended') + @if ($last = $item->recurring->transactions->last()?->paid_at) + {{ $last->format(company_date_format()) }} + @else + + @endif + @else + @if ($last = $item->recurring->getLastRecurring()) + {{ $last->format(company_date_format()) }} + @endif @endif diff --git a/resources/views/components/documents/index/recurring_templates.blade.php b/resources/views/components/documents/index/recurring_templates.blade.php index fc2311845..f7f4e5807 100644 --- a/resources/views/components/documents/index/recurring_templates.blade.php +++ b/resources/views/components/documents/index/recurring_templates.blade.php @@ -58,8 +58,16 @@ - @if ($last = $item->recurring->getLastRecurring()) - {{ $last->format(company_date_format()) }} + @if ($item->recurring->status == 'ended') + @if ($last = $item->recurring->documents->last()?->issued_at) + {{ $last->format(company_date_format()) }} + @else + + @endif + @else + @if ($last = $item->recurring->getLastRecurring()) + {{ $last->format(company_date_format()) }} + @endif @endif