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ů.

AccountFactory.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace Database\Factories\Accounting;
  3. use App\Models\Accounting\Account;
  4. use App\Models\Accounting\AccountSubtype;
  5. use App\Models\Banking\BankAccount;
  6. use App\Models\Setting\Currency;
  7. use App\Utilities\Accounting\AccountCode;
  8. use App\Utilities\Currency\CurrencyAccessor;
  9. use Illuminate\Database\Eloquent\Factories\Factory;
  10. /**
  11. * @extends Factory<Account>
  12. */
  13. class AccountFactory extends Factory
  14. {
  15. /**
  16. * The name of the factory's corresponding model.
  17. */
  18. protected $model = Account::class;
  19. /**
  20. * Define the model's default state.
  21. *
  22. * @return array<string, mixed>
  23. */
  24. public function definition(): array
  25. {
  26. return [
  27. 'company_id' => 1,
  28. 'subtype_id' => 1,
  29. 'name' => $this->faker->unique()->word,
  30. 'currency_code' => CurrencyAccessor::getDefaultCurrency() ?? 'USD',
  31. 'description' => $this->faker->sentence,
  32. 'archived' => false,
  33. 'default' => false,
  34. ];
  35. }
  36. public function withBankAccount(string $name): static
  37. {
  38. return $this->afterCreating(function (Account $account) use ($name) {
  39. $accountSubtype = AccountSubtype::where('name', 'Cash and Cash Equivalents')->first();
  40. // Create and associate a BankAccount with the Account
  41. $bankAccount = BankAccount::factory()->create([
  42. 'account_id' => $account->id, // Associate with Account
  43. ]);
  44. // Update the Account with the subtype and name
  45. $account->update([
  46. 'subtype_id' => $accountSubtype->id,
  47. 'name' => $name,
  48. ]);
  49. });
  50. }
  51. public function withForeignBankAccount(string $name, string $currencyCode, float $rate): static
  52. {
  53. return $this->afterCreating(function (Account $account) use ($currencyCode, $rate, $name) {
  54. $accountSubtype = AccountSubtype::where('name', 'Cash and Cash Equivalents')->first();
  55. // Create the Currency and BankAccount
  56. $currency = Currency::factory()->forCurrency($currencyCode, $rate)->create();
  57. $bankAccount = BankAccount::factory()->create([
  58. 'account_id' => $account->id, // Associate with Account
  59. ]);
  60. // Update the Account with the subtype, name, and currency code
  61. $account->update([
  62. 'subtype_id' => $accountSubtype->id,
  63. 'name' => $name,
  64. 'currency_code' => $currency->code,
  65. ]);
  66. });
  67. }
  68. public function forSalesTax(?string $name = null, ?string $description = null): static
  69. {
  70. $accountSubtype = AccountSubtype::where('name', 'Sales Taxes')->first();
  71. return $this->state([
  72. 'name' => $name,
  73. 'description' => $description,
  74. 'category' => $accountSubtype->category,
  75. 'type' => $accountSubtype->type,
  76. 'subtype_id' => $accountSubtype->id,
  77. 'code' => AccountCode::generate($accountSubtype),
  78. ]);
  79. }
  80. public function forPurchaseTax(?string $name = null, ?string $description = null): static
  81. {
  82. $accountSubtype = AccountSubtype::where('name', 'Purchase Taxes')->first();
  83. return $this->state([
  84. 'name' => $name,
  85. 'description' => $description,
  86. 'category' => $accountSubtype->category,
  87. 'type' => $accountSubtype->type,
  88. 'subtype_id' => $accountSubtype->id,
  89. 'code' => AccountCode::generate($accountSubtype),
  90. ]);
  91. }
  92. public function forSalesDiscount(?string $name = null, ?string $description = null): static
  93. {
  94. $accountSubtype = AccountSubtype::where('name', 'Sales Discounts')->first();
  95. return $this->state([
  96. 'name' => $name,
  97. 'description' => $description,
  98. 'category' => $accountSubtype->category,
  99. 'type' => $accountSubtype->type,
  100. 'subtype_id' => $accountSubtype->id,
  101. 'code' => AccountCode::generate($accountSubtype),
  102. ]);
  103. }
  104. public function forPurchaseDiscount(?string $name = null, ?string $description = null): static
  105. {
  106. $accountSubtype = AccountSubtype::where('name', 'Purchase Discounts')->first();
  107. return $this->state([
  108. 'name' => $name,
  109. 'description' => $description,
  110. 'category' => $accountSubtype->category,
  111. 'type' => $accountSubtype->type,
  112. 'subtype_id' => $accountSubtype->id,
  113. 'code' => AccountCode::generate($accountSubtype),
  114. ]);
  115. }
  116. }