From e78c5a3bc248293014618f6ab20371ceeaf95830 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihan=20=C5=9Eent=C3=BCrk?= Date: Wed, 17 May 2023 10:16:49 +0300 Subject: [PATCH] fixed factory transaction number --- app/Utilities/TransactionNumber.php | 4 ++-- database/factories/Transaction.php | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/app/Utilities/TransactionNumber.php b/app/Utilities/TransactionNumber.php index df0a0f18e..a60a78e8d 100644 --- a/app/Utilities/TransactionNumber.php +++ b/app/Utilities/TransactionNumber.php @@ -8,7 +8,7 @@ use App\Models\Common\Contact; class TransactionNumber implements TransactionNumberInterface { - public function getNextNumber($suffix = '', ?Contact $contact): string + public function getNextNumber($type, $suffix = '', ?Contact $contact): string { $prefix = setting('transaction' . $suffix . '.number_prefix'); $next = (string) setting('transaction' . $suffix . '.number_next'); @@ -34,7 +34,7 @@ class TransactionNumber implements TransactionNumberInterface } - public function increaseNextNumber($suffix = '', ?Contact $contact): void + public function increaseNextNumber($type, $suffix = '', ?Contact $contact): void { $next = setting('transaction' . $suffix . '.number_next', 1) + 1; diff --git a/database/factories/Transaction.php b/database/factories/Transaction.php index 2ad38084d..525d06c2e 100644 --- a/database/factories/Transaction.php +++ b/database/factories/Transaction.php @@ -3,6 +3,7 @@ namespace Database\Factories; use App\Abstracts\Factory; +use App\Interfaces\Utility\TransactionNumber; use App\Models\Banking\Transaction as Model; use App\Models\Common\Contact; use App\Traits\Transactions; @@ -41,7 +42,7 @@ class Transaction extends Factory return [ 'company_id' => $this->company->id, 'type' => $this->type, - 'number' => $this->getNumber(), + 'number' => $this->getNumber($this->type), 'account_id' => setting('default.account'), 'paid_at' => $this->faker->dateTimeBetween(now()->startOfYear(), now()->endOfYear())->format('Y-m-d H:i:s'), 'amount' => $this->faker->randomFloat(2, 1, 1000), @@ -73,6 +74,7 @@ class Transaction extends Factory return [ 'type' => 'income', + 'number' => $this->getNumber('income', '', $contact), 'contact_id' => $contact->id, 'category_id' => $this->company->categories()->income()->get()->random(1)->pluck('id')->first(), ]; @@ -97,6 +99,7 @@ class Transaction extends Factory return [ 'type' => 'expense', + 'number' => $this->getNumber('expense', '', $contact), 'contact_id' => $contact->id, 'category_id' => $this->company->categories()->expense()->get()->random(1)->pluck('id')->first(), ]; @@ -110,9 +113,11 @@ class Transaction extends Factory */ public function recurring() { + $type = $this->getRawAttribute('type') . '-recurring'; + return $this->state([ - 'type' => $this->getRawAttribute('type') . '-recurring', - 'number' => $this->getNumber('-recurring'), + 'type' => $type, + 'number' => $this->getNumber($type, '-recurring'), 'recurring_started_at' => Date::now()->format('Y-m-d H:i:s'), 'recurring_frequency' => 'daily', 'recurring_custom_frequency' => 'daily', @@ -129,11 +134,13 @@ class Transaction extends Factory * Get transaction number * */ - public function getNumber($suffix = '') + public function getNumber($type, $suffix = '', $contact = null) { - $number = $this->getNextTransactionNumber($suffix); + $utility = app(TransactionNumber::class); - $this->increaseNextTransactionNumber($suffix); + $number = $utility->getNextNumber($type, $suffix, $contact); + + $utility->increaseNextNumber($type, $suffix ,$contact); return $number; }