Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

TransactionFactory.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 App\Services\TransactionService;
  11. use Illuminate\Database\Eloquent\Factories\Factory;
  12. use Illuminate\Support\Facades\DB;
  13. /**
  14. * @extends Factory<Transaction>
  15. */
  16. class TransactionFactory extends Factory
  17. {
  18. /**
  19. * The name of the factory's corresponding model.
  20. */
  21. protected $model = Transaction::class;
  22. /**
  23. * Define the model's default state.
  24. *
  25. * @return array<string, mixed>
  26. */
  27. public function definition(): array
  28. {
  29. return [
  30. 'company_id' => 1,
  31. 'bank_account_id' => 1,
  32. 'account_id' => $this->faker->numberBetween(2, 30),
  33. 'type' => $this->faker->randomElement([TransactionType::Deposit, TransactionType::Withdrawal]),
  34. 'description' => $this->faker->sentence,
  35. 'notes' => $this->faker->paragraph,
  36. 'amount' => $this->faker->numberBetween(100, 5000),
  37. 'reviewed' => $this->faker->boolean,
  38. 'posted_at' => $this->faker->dateTimeBetween('-2 years'),
  39. 'created_by' => 1,
  40. 'updated_by' => 1,
  41. ];
  42. }
  43. public function configure(): static
  44. {
  45. return $this->afterCreating(function (Transaction $transaction) {
  46. if (DB::getDefaultConnection() === 'sqlite') {
  47. app(TransactionService::class)->createJournalEntries($transaction);
  48. }
  49. });
  50. }
  51. public function forCompanyAndBankAccount(Company $company, BankAccount $bankAccount): static
  52. {
  53. return $this->state(function (array $attributes) use ($bankAccount, $company) {
  54. $type = $this->faker->randomElement([TransactionType::Deposit, TransactionType::Withdrawal]);
  55. $associatedAccountTypes = match ($type) {
  56. TransactionType::Deposit => ['asset', 'liability', 'equity', 'revenue'],
  57. TransactionType::Withdrawal => ['asset', 'liability', 'equity', 'expense'],
  58. };
  59. $accountIdForBankAccount = $bankAccount->account->id;
  60. $account = Account::where('category', $this->faker->randomElement($associatedAccountTypes))
  61. ->where('company_id', $company->id)
  62. ->where('id', '<>', $accountIdForBankAccount)
  63. ->inRandomOrder()
  64. ->first();
  65. // If no matching account is found, use a fallback
  66. if (! $account) {
  67. $account = Account::where('company_id', $company->id)
  68. ->where('id', '<>', $accountIdForBankAccount)
  69. ->inRandomOrder()
  70. ->firstOrFail(); // Ensure there is at least some account
  71. }
  72. return [
  73. 'company_id' => $company->id,
  74. 'bank_account_id' => $bankAccount->id,
  75. 'account_id' => $account->id,
  76. 'type' => $type,
  77. ];
  78. });
  79. }
  80. public function forDefaultBankAccount(): static
  81. {
  82. return $this->state(function (array $attributes) {
  83. $defaultBankAccount = CompanyDefault::first()->bankAccount;
  84. return [
  85. 'bank_account_id' => $defaultBankAccount->id,
  86. ];
  87. });
  88. }
  89. public function forUncategorizedRevenue(): static
  90. {
  91. return $this->state(function (array $attributes) {
  92. $account = Account::where('type', AccountType::UncategorizedRevenue)->firstOrFail();
  93. return [
  94. 'account_id' => $account->id,
  95. ];
  96. });
  97. }
  98. public function forUncategorizedExpense(): static
  99. {
  100. return $this->state(function (array $attributes) {
  101. $account = Account::where('type', AccountType::UncategorizedExpense)->firstOrFail();
  102. return [
  103. 'account_id' => $account->id,
  104. ];
  105. });
  106. }
  107. public function asDeposit(int $amount): static
  108. {
  109. return $this->state(function () use ($amount) {
  110. return [
  111. 'type' => TransactionType::Deposit,
  112. 'amount' => $amount,
  113. ];
  114. });
  115. }
  116. public function asWithdrawal(int $amount): static
  117. {
  118. return $this->state(function () use ($amount) {
  119. return [
  120. 'type' => TransactionType::Withdrawal,
  121. 'amount' => $amount,
  122. ];
  123. });
  124. }
  125. }