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 6.1KB

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