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.

AccountFactory.php 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\Currency\CurrencyAccessor;
  8. use Illuminate\Database\Eloquent\Factories\Factory;
  9. /**
  10. * @extends Factory<Account>
  11. */
  12. class AccountFactory extends Factory
  13. {
  14. /**
  15. * The name of the factory's corresponding model.
  16. */
  17. protected $model = Account::class;
  18. /**
  19. * Define the model's default state.
  20. *
  21. * @return array<string, mixed>
  22. */
  23. public function definition(): array
  24. {
  25. return [
  26. 'company_id' => 1,
  27. 'subtype_id' => 1,
  28. 'name' => $this->faker->unique()->word,
  29. 'currency_code' => CurrencyAccessor::getDefaultCurrency() ?? 'USD',
  30. 'description' => $this->faker->sentence,
  31. 'archived' => false,
  32. 'default' => false,
  33. ];
  34. }
  35. public function withBankAccount(string $name): static
  36. {
  37. return $this->afterCreating(function (Account $account) use ($name) {
  38. $accountSubtype = AccountSubtype::where('name', 'Cash and Cash Equivalents')->first();
  39. // Create and associate a BankAccount with the Account
  40. $bankAccount = BankAccount::factory()->create([
  41. 'account_id' => $account->id, // Associate with Account
  42. ]);
  43. // Update the Account with the subtype and name
  44. $account->update([
  45. 'subtype_id' => $accountSubtype->id,
  46. 'name' => $name,
  47. ]);
  48. });
  49. }
  50. public function withForeignBankAccount(string $name, string $currencyCode, float $rate): static
  51. {
  52. return $this->afterCreating(function (Account $account) use ($currencyCode, $rate, $name) {
  53. $accountSubtype = AccountSubtype::where('name', 'Cash and Cash Equivalents')->first();
  54. // Create the Currency and BankAccount
  55. $currency = Currency::factory()->forCurrency($currencyCode, $rate)->create();
  56. $bankAccount = BankAccount::factory()->create([
  57. 'account_id' => $account->id, // Associate with Account
  58. ]);
  59. // Update the Account with the subtype, name, and currency code
  60. $account->update([
  61. 'subtype_id' => $accountSubtype->id,
  62. 'name' => $name,
  63. 'currency_code' => $currency->code,
  64. ]);
  65. });
  66. }
  67. }