Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TransactionFactory.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 forAccount(Account $account): static
  116. {
  117. return $this->state([
  118. 'account_id' => $account->id,
  119. ]);
  120. }
  121. public function forType(TransactionType $type, int $amount): static
  122. {
  123. return $this->state(compact('type', 'amount'));
  124. }
  125. public function asDeposit(int $amount): static
  126. {
  127. return $this->state(function () use ($amount) {
  128. return [
  129. 'type' => TransactionType::Deposit,
  130. 'amount' => $amount,
  131. ];
  132. });
  133. }
  134. public function asWithdrawal(int $amount): static
  135. {
  136. return $this->state(function () use ($amount) {
  137. return [
  138. 'type' => TransactionType::Withdrawal,
  139. 'amount' => $amount,
  140. ];
  141. });
  142. }
  143. public function asJournal(int $amount): static
  144. {
  145. return $this->state(function () use ($amount) {
  146. return [
  147. 'type' => TransactionType::Journal,
  148. 'amount' => $amount,
  149. ];
  150. });
  151. }
  152. public function asTransfer(int $amount): static
  153. {
  154. return $this->state(function () use ($amount) {
  155. return [
  156. 'type' => TransactionType::Transfer,
  157. 'amount' => $amount,
  158. ];
  159. });
  160. }
  161. }