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.9KB

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