Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

UserFactory.php 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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(CompanyProfile::factory()->withCountry($countryCode), 'profile')
  63. ->afterCreating(function (Company $company) use ($user, $countryCode) {
  64. DB::transaction(function () use ($company, $user, $countryCode) {
  65. $companyDefaultService = app()->make(CompanyDefaultService::class);
  66. $companyDefaultService->createCompanyDefaults($company, $user, 'USD', $countryCode, 'en');
  67. $defaultBankAccount = $company->default->bankAccount;
  68. TransactionFactory::new()
  69. ->forCompanyAndBankAccount($company, $defaultBankAccount)
  70. ->count(2000)
  71. ->createQuietly([
  72. 'created_by' => $user->id,
  73. 'updated_by' => $user->id,
  74. ]);
  75. });
  76. })
  77. ->create([
  78. 'name' => $user->name . '\'s Company',
  79. 'user_id' => $user->id,
  80. 'personal_company' => true,
  81. ]);
  82. });
  83. }
  84. }