Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

UserFactory.php 3.5KB

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