fixed factory transaction number

This commit is contained in:
Cihan Şentürk 2023-05-17 10:16:49 +03:00
parent 7a3aa98c56
commit e78c5a3bc2
2 changed files with 15 additions and 8 deletions

View File

@ -8,7 +8,7 @@ use App\Models\Common\Contact;
class TransactionNumber implements TransactionNumberInterface 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'); $prefix = setting('transaction' . $suffix . '.number_prefix');
$next = (string) setting('transaction' . $suffix . '.number_next'); $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; $next = setting('transaction' . $suffix . '.number_next', 1) + 1;

View File

@ -3,6 +3,7 @@
namespace Database\Factories; namespace Database\Factories;
use App\Abstracts\Factory; use App\Abstracts\Factory;
use App\Interfaces\Utility\TransactionNumber;
use App\Models\Banking\Transaction as Model; use App\Models\Banking\Transaction as Model;
use App\Models\Common\Contact; use App\Models\Common\Contact;
use App\Traits\Transactions; use App\Traits\Transactions;
@ -41,7 +42,7 @@ class Transaction extends Factory
return [ return [
'company_id' => $this->company->id, 'company_id' => $this->company->id,
'type' => $this->type, 'type' => $this->type,
'number' => $this->getNumber(), 'number' => $this->getNumber($this->type),
'account_id' => setting('default.account'), 'account_id' => setting('default.account'),
'paid_at' => $this->faker->dateTimeBetween(now()->startOfYear(), now()->endOfYear())->format('Y-m-d H:i:s'), 'paid_at' => $this->faker->dateTimeBetween(now()->startOfYear(), now()->endOfYear())->format('Y-m-d H:i:s'),
'amount' => $this->faker->randomFloat(2, 1, 1000), 'amount' => $this->faker->randomFloat(2, 1, 1000),
@ -73,6 +74,7 @@ class Transaction extends Factory
return [ return [
'type' => 'income', 'type' => 'income',
'number' => $this->getNumber('income', '', $contact),
'contact_id' => $contact->id, 'contact_id' => $contact->id,
'category_id' => $this->company->categories()->income()->get()->random(1)->pluck('id')->first(), 'category_id' => $this->company->categories()->income()->get()->random(1)->pluck('id')->first(),
]; ];
@ -97,6 +99,7 @@ class Transaction extends Factory
return [ return [
'type' => 'expense', 'type' => 'expense',
'number' => $this->getNumber('expense', '', $contact),
'contact_id' => $contact->id, 'contact_id' => $contact->id,
'category_id' => $this->company->categories()->expense()->get()->random(1)->pluck('id')->first(), 'category_id' => $this->company->categories()->expense()->get()->random(1)->pluck('id')->first(),
]; ];
@ -110,9 +113,11 @@ class Transaction extends Factory
*/ */
public function recurring() public function recurring()
{ {
$type = $this->getRawAttribute('type') . '-recurring';
return $this->state([ return $this->state([
'type' => $this->getRawAttribute('type') . '-recurring', 'type' => $type,
'number' => $this->getNumber('-recurring'), 'number' => $this->getNumber($type, '-recurring'),
'recurring_started_at' => Date::now()->format('Y-m-d H:i:s'), 'recurring_started_at' => Date::now()->format('Y-m-d H:i:s'),
'recurring_frequency' => 'daily', 'recurring_frequency' => 'daily',
'recurring_custom_frequency' => 'daily', 'recurring_custom_frequency' => 'daily',
@ -129,11 +134,13 @@ class Transaction extends Factory
* Get transaction number * 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; return $number;
} }