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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. class CompanyFactory extends Factory
  11. {
  12. /**
  13. * The name of the factory's corresponding model.
  14. *
  15. * @var string
  16. */
  17. protected $model = Company::class;
  18. /**
  19. * Define the model's default state.
  20. *
  21. * @return array<string, mixed>
  22. */
  23. public function definition(): array
  24. {
  25. return [
  26. 'name' => $this->faker->unique()->company(),
  27. 'user_id' => User::factory(),
  28. 'personal_company' => true,
  29. ];
  30. }
  31. public function withCompanyProfile(): self
  32. {
  33. return $this->afterCreating(function (Company $company) {
  34. CompanyProfile::factory()->forCompany($company)->create();
  35. });
  36. }
  37. /**
  38. * Set up default settings for the company after creation.
  39. */
  40. public function withCompanyDefaults(): self
  41. {
  42. return $this->afterCreating(function (Company $company) {
  43. DB::transaction(function () use ($company) {
  44. $countryCode = $company->profile->country;
  45. $companyDefaultService = app(CompanyDefaultService::class);
  46. $companyDefaultService->createCompanyDefaults($company, $company->owner, 'USD', $countryCode, 'en');
  47. });
  48. });
  49. }
  50. public function withTransactions(int $count = 2000): self
  51. {
  52. return $this->afterCreating(function (Company $company) use ($count) {
  53. $defaultBankAccount = $company->default->bankAccount;
  54. TransactionFactory::new()
  55. ->forCompanyAndBankAccount($company, $defaultBankAccount)
  56. ->count($count)
  57. ->createQuietly([
  58. 'created_by' => $company->user_id,
  59. 'updated_by' => $company->user_id,
  60. ]);
  61. });
  62. }
  63. }