You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TransactionFactory.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Database\Factories\Accounting;
  3. use App\Enums\Accounting\AccountType;
  4. use App\Enums\Accounting\TransactionType;
  5. use App\Models\Accounting\Account;
  6. use App\Models\Accounting\Transaction;
  7. use App\Models\Banking\BankAccount;
  8. use App\Models\Company;
  9. use App\Models\Setting\CompanyDefault;
  10. use Illuminate\Database\Eloquent\Factories\Factory;
  11. /**
  12. * @extends Factory<Transaction>
  13. */
  14. class TransactionFactory extends Factory
  15. {
  16. /**
  17. * The name of the factory's corresponding model.
  18. */
  19. protected $model = Transaction::class;
  20. /**
  21. * Define the model's default state.
  22. *
  23. * @return array<string, mixed>
  24. */
  25. public function definition(): array
  26. {
  27. return [
  28. 'company_id' => 1,
  29. 'bank_account_id' => 1,
  30. 'account_id' => $this->faker->numberBetween(2, 30),
  31. 'type' => $this->faker->randomElement([TransactionType::Deposit, TransactionType::Withdrawal]),
  32. 'description' => $this->faker->sentence,
  33. 'notes' => $this->faker->paragraph,
  34. 'amount' => $this->faker->numberBetween(100, 5000),
  35. 'reviewed' => $this->faker->boolean,
  36. 'posted_at' => $this->faker->dateTimeBetween('-2 years'),
  37. 'created_by' => 1,
  38. 'updated_by' => 1,
  39. ];
  40. }
  41. public function forCompanyAndBankAccount(Company $company, BankAccount $bankAccount): static
  42. {
  43. return $this->state(function (array $attributes) use ($bankAccount, $company) {
  44. $type = $this->faker->randomElement([TransactionType::Deposit, TransactionType::Withdrawal]);
  45. $associatedAccountTypes = match ($type) {
  46. TransactionType::Deposit => ['asset', 'liability', 'equity', 'revenue'],
  47. TransactionType::Withdrawal => ['asset', 'liability', 'equity', 'expense'],
  48. };
  49. $accountIdForBankAccount = $bankAccount->account->id;
  50. $account = Account::where('category', $this->faker->randomElement($associatedAccountTypes))
  51. ->where('company_id', $company->id)
  52. ->where('id', '<>', $accountIdForBankAccount)
  53. ->inRandomOrder()
  54. ->first();
  55. // If no matching account is found, use a fallback
  56. if (! $account) {
  57. $account = Account::where('company_id', $company->id)
  58. ->where('id', '<>', $accountIdForBankAccount)
  59. ->inRandomOrder()
  60. ->firstOrFail(); // Ensure there is at least some account
  61. }
  62. return [
  63. 'company_id' => $company->id,
  64. 'bank_account_id' => $bankAccount->id,
  65. 'account_id' => $account->id,
  66. 'type' => $type,
  67. ];
  68. });
  69. }
  70. public function forDefaultBankAccount(): static
  71. {
  72. return $this->state(function (array $attributes) {
  73. $defaultBankAccount = CompanyDefault::first()->bankAccount;
  74. return [
  75. 'bank_account_id' => $defaultBankAccount->id,
  76. ];
  77. });
  78. }
  79. public function forBankAccount(?BankAccount $bankAccount = null): static
  80. {
  81. return $this->state(function (array $attributes) use ($bankAccount) {
  82. $bankAccount = $bankAccount ?? BankAccount::factory()->create();
  83. return [
  84. 'bank_account_id' => $bankAccount->id,
  85. ];
  86. });
  87. }
  88. public function forDestinationBankAccount(?Account $account = null): static
  89. {
  90. return $this->state(function (array $attributes) use ($account) {
  91. $destinationBankAccount = $account ?? Account::factory()->withBankAccount('Destination Bank Account')->create();
  92. return [
  93. 'account_id' => $destinationBankAccount->id,
  94. ];
  95. });
  96. }
  97. public function forUncategorizedRevenue(): static
  98. {
  99. return $this->state(function (array $attributes) {
  100. $account = Account::where('type', AccountType::UncategorizedRevenue)->firstOrFail();
  101. return [
  102. 'account_id' => $account->id,
  103. ];
  104. });
  105. }
  106. public function forUncategorizedExpense(): static
  107. {
  108. return $this->state(function (array $attributes) {
  109. $account = Account::where('type', AccountType::UncategorizedExpense)->firstOrFail();
  110. return [
  111. 'account_id' => $account->id,
  112. ];
  113. });
  114. }
  115. public function asDeposit(int $amount): static
  116. {
  117. return $this->state(function () use ($amount) {
  118. return [
  119. 'type' => TransactionType::Deposit,
  120. 'amount' => $amount,
  121. ];
  122. });
  123. }
  124. public function asWithdrawal(int $amount): static
  125. {
  126. return $this->state(function () use ($amount) {
  127. return [
  128. 'type' => TransactionType::Withdrawal,
  129. 'amount' => $amount,
  130. ];
  131. });
  132. }
  133. public function asJournal(int $amount): static
  134. {
  135. return $this->state(function () use ($amount) {
  136. return [
  137. 'type' => TransactionType::Journal,
  138. 'amount' => $amount,
  139. ];
  140. });
  141. }
  142. public function asTransfer(int $amount): static
  143. {
  144. return $this->state(function () use ($amount) {
  145. return [
  146. 'type' => TransactionType::Transfer,
  147. 'amount' => $amount,
  148. ];
  149. });
  150. }
  151. }