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 4.5KB

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