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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. ->whereKeyNot($accountIdForBankAccount)
  53. ->inRandomOrder()
  54. ->first();
  55. if (! $account) {
  56. $account = Account::where('company_id', $company->id)
  57. ->whereKeyNot($accountIdForBankAccount)
  58. ->inRandomOrder()
  59. ->firstOrFail();
  60. }
  61. return [
  62. 'company_id' => $company->id,
  63. 'bank_account_id' => $bankAccount->id,
  64. 'account_id' => $account->id,
  65. 'type' => $type,
  66. ];
  67. });
  68. }
  69. public function forDefaultBankAccount(): static
  70. {
  71. return $this->state(function (array $attributes) {
  72. $defaultBankAccount = CompanyDefault::first()->bankAccount;
  73. return [
  74. 'bank_account_id' => $defaultBankAccount->id,
  75. ];
  76. });
  77. }
  78. public function forBankAccount(?BankAccount $bankAccount = null): static
  79. {
  80. return $this->state(function (array $attributes) use ($bankAccount) {
  81. $bankAccount = $bankAccount ?? BankAccount::factory()->create();
  82. return [
  83. 'bank_account_id' => $bankAccount->id,
  84. ];
  85. });
  86. }
  87. public function forDestinationBankAccount(?Account $account = null): static
  88. {
  89. return $this->state(function (array $attributes) use ($account) {
  90. $destinationBankAccount = $account ?? Account::factory()->withBankAccount('Destination Bank Account')->create();
  91. return [
  92. 'account_id' => $destinationBankAccount->id,
  93. ];
  94. });
  95. }
  96. public function forUncategorizedRevenue(): static
  97. {
  98. return $this->state(function (array $attributes) {
  99. $account = Account::where('type', AccountType::UncategorizedRevenue)->firstOrFail();
  100. return [
  101. 'account_id' => $account->id,
  102. ];
  103. });
  104. }
  105. public function forUncategorizedExpense(): static
  106. {
  107. return $this->state(function (array $attributes) {
  108. $account = Account::where('type', AccountType::UncategorizedExpense)->firstOrFail();
  109. return [
  110. 'account_id' => $account->id,
  111. ];
  112. });
  113. }
  114. public function forAccount(Account $account): static
  115. {
  116. return $this->state([
  117. 'account_id' => $account->id,
  118. ]);
  119. }
  120. public function forType(TransactionType $type, int $amount): static
  121. {
  122. return $this->state(compact('type', 'amount'));
  123. }
  124. public function asDeposit(int $amount): static
  125. {
  126. return $this->state(function () use ($amount) {
  127. return [
  128. 'type' => TransactionType::Deposit,
  129. 'amount' => $amount,
  130. ];
  131. });
  132. }
  133. public function asWithdrawal(int $amount): static
  134. {
  135. return $this->state(function () use ($amount) {
  136. return [
  137. 'type' => TransactionType::Withdrawal,
  138. 'amount' => $amount,
  139. ];
  140. });
  141. }
  142. public function asJournal(int $amount): static
  143. {
  144. return $this->state(function () use ($amount) {
  145. return [
  146. 'type' => TransactionType::Journal,
  147. 'amount' => $amount,
  148. ];
  149. });
  150. }
  151. public function asTransfer(int $amount): static
  152. {
  153. return $this->state(function () use ($amount) {
  154. return [
  155. 'type' => TransactionType::Transfer,
  156. 'amount' => $amount,
  157. ];
  158. });
  159. }
  160. }