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.

UserFactory.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Database\Factories;
  3. use App\Models\Company;
  4. use App\Models\Setting\CompanyProfile;
  5. use App\Models\User;
  6. use App\Services\CompanyDefaultService;
  7. use Database\Factories\Accounting\TransactionFactory;
  8. use Illuminate\Database\Eloquent\Factories\Factory;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Hash;
  11. use Illuminate\Support\Str;
  12. use Wallo\FilamentCompanies\FilamentCompanies;
  13. class UserFactory extends Factory
  14. {
  15. /**
  16. * The name of the factory's corresponding model.
  17. *
  18. * @var string
  19. */
  20. protected $model = User::class;
  21. /**
  22. * The current password being used by the factory.
  23. */
  24. protected static ?string $password = null;
  25. /**
  26. * Define the model's default state.
  27. *
  28. * @return array<string, mixed>
  29. */
  30. public function definition(): array
  31. {
  32. return [
  33. 'name' => fake()->name(),
  34. 'email' => fake()->unique()->safeEmail(),
  35. 'email_verified_at' => now(),
  36. 'password' => static::$password ??= Hash::make('password'),
  37. 'remember_token' => Str::random(10),
  38. 'profile_photo_path' => null,
  39. 'current_company_id' => null,
  40. ];
  41. }
  42. /**
  43. * Indicate that the model's email address should be unverified.
  44. */
  45. public function unverified(): static
  46. {
  47. return $this->state(static fn (array $attributes) => [
  48. 'email_verified_at' => null,
  49. ]);
  50. }
  51. /**
  52. * Indicate that the user should have a personal company.
  53. */
  54. public function withPersonalCompany(): static
  55. {
  56. if (! FilamentCompanies::hasCompanyFeatures()) {
  57. return $this->state([]);
  58. }
  59. $countryCode = $this->faker->countryCode;
  60. return $this->afterCreating(function (User $user) use ($countryCode) {
  61. Company::factory()
  62. ->has(
  63. CompanyProfile::factory()
  64. ->withCountry($countryCode)
  65. ->state([
  66. 'created_by' => $user->id,
  67. 'updated_by' => $user->id,
  68. ]),
  69. 'profile'
  70. )
  71. ->afterCreating(function (Company $company) use ($user, $countryCode) {
  72. DB::transaction(function () use ($company, $user, $countryCode) {
  73. $companyDefaultService = app()->make(CompanyDefaultService::class);
  74. $companyDefaultService->createCompanyDefaults($company, $user, 'USD', $countryCode, 'en');
  75. $defaultBankAccount = $company->default->bankAccount;
  76. TransactionFactory::new()
  77. ->forCompanyAndBankAccount($company, $defaultBankAccount)
  78. ->count(2000)
  79. ->createQuietly([
  80. 'created_by' => $user->id,
  81. 'updated_by' => $user->id,
  82. ]);
  83. });
  84. })
  85. ->create([
  86. 'name' => $user->name . '\'s Company',
  87. 'user_id' => $user->id,
  88. 'personal_company' => true,
  89. ]);
  90. });
  91. }
  92. }