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.

CompanyFactory.php 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace Database\Factories;
  3. use App\Models\Accounting\Transaction;
  4. use App\Models\Company;
  5. use App\Models\Setting\CompanyProfile;
  6. use App\Models\User;
  7. use App\Services\CompanyDefaultService;
  8. use Illuminate\Database\Eloquent\Factories\Factory;
  9. class CompanyFactory extends Factory
  10. {
  11. /**
  12. * The name of the factory's corresponding model.
  13. *
  14. * @var string
  15. */
  16. protected $model = Company::class;
  17. /**
  18. * Define the model's default state.
  19. *
  20. * @return array<string, mixed>
  21. */
  22. public function definition(): array
  23. {
  24. return [
  25. 'name' => $this->faker->unique()->company(),
  26. 'user_id' => User::factory(),
  27. 'personal_company' => true,
  28. ];
  29. }
  30. public function withCompanyProfile(): self
  31. {
  32. return $this->afterCreating(function (Company $company) {
  33. CompanyProfile::factory()->forCompany($company)->withCountry('US')->create();
  34. });
  35. }
  36. /**
  37. * Set up default settings for the company after creation.
  38. */
  39. public function withCompanyDefaults(): self
  40. {
  41. return $this->afterCreating(function (Company $company) {
  42. $countryCode = $company->profile->country;
  43. $companyDefaultService = app(CompanyDefaultService::class);
  44. $companyDefaultService->createCompanyDefaults($company, $company->owner, 'USD', $countryCode, 'en');
  45. });
  46. }
  47. public function withTransactions(int $count = 2000): self
  48. {
  49. return $this->afterCreating(function (Company $company) use ($count) {
  50. $defaultBankAccount = $company->default->bankAccount;
  51. Transaction::factory()
  52. ->forCompanyAndBankAccount($company, $defaultBankAccount)
  53. ->count($count)
  54. ->create([
  55. 'created_by' => $company->user_id,
  56. 'updated_by' => $company->user_id,
  57. ]);
  58. });
  59. }
  60. }