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.

CompanyDefaultFactory.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Database\Factories\Setting;
  3. use App\Faker\CurrencyCode;
  4. use App\Models\Company;
  5. use App\Models\Setting\Appearance;
  6. use App\Models\Setting\CompanyDefault;
  7. use App\Models\Setting\Currency;
  8. use App\Models\Setting\DocumentDefault;
  9. use App\Models\Setting\Localization;
  10. use App\Models\User;
  11. use Illuminate\Database\Eloquent\Factories\Factory;
  12. /**
  13. * @extends Factory<CompanyDefault>
  14. */
  15. class CompanyDefaultFactory extends Factory
  16. {
  17. /**
  18. * The name of the factory's corresponding model.
  19. */
  20. protected $model = CompanyDefault::class;
  21. /**
  22. * Define the model's default state.
  23. *
  24. * @return array<string, mixed>
  25. */
  26. public function definition(): array
  27. {
  28. return [
  29. //
  30. ];
  31. }
  32. public function withDefault(User $user, Company $company, ?string $currencyCode, string $countryCode, string $language = 'en'): static
  33. {
  34. if ($currencyCode === null) {
  35. /** @var CurrencyCode $currencyFaker */
  36. $currencyFaker = $this->faker;
  37. $currencyCode = $currencyFaker->currencyCode($countryCode);
  38. }
  39. $currency = $this->createCurrency($company, $user, $currencyCode);
  40. $this->createAppearance($company, $user);
  41. $this->createDocumentDefaults($company, $user);
  42. $this->createLocalization($company, $user, $countryCode, $language);
  43. $companyDefaults = [
  44. 'company_id' => $company->id,
  45. 'currency_code' => $currency->code,
  46. 'created_by' => $user->id,
  47. 'updated_by' => $user->id,
  48. ];
  49. return $this->state($companyDefaults);
  50. }
  51. private function createCurrency(Company $company, User $user, string $currencyCode): Currency
  52. {
  53. return Currency::factory()->forCurrency($currencyCode)->createQuietly([
  54. 'company_id' => $company->id,
  55. 'enabled' => true,
  56. 'created_by' => $user->id,
  57. 'updated_by' => $user->id,
  58. ]);
  59. }
  60. private function createAppearance(Company $company, User $user): void
  61. {
  62. Appearance::factory()->createQuietly([
  63. 'company_id' => $company->id,
  64. 'created_by' => $user->id,
  65. 'updated_by' => $user->id,
  66. ]);
  67. }
  68. private function createDocumentDefaults(Company $company, User $user): void
  69. {
  70. DocumentDefault::factory()->invoice()->createQuietly([
  71. 'company_id' => $company->id,
  72. 'created_by' => $user->id,
  73. 'updated_by' => $user->id,
  74. ]);
  75. DocumentDefault::factory()->bill()->createQuietly([
  76. 'company_id' => $company->id,
  77. 'created_by' => $user->id,
  78. 'updated_by' => $user->id,
  79. ]);
  80. }
  81. private function createLocalization(Company $company, User $user, string $countryCode, string $language): void
  82. {
  83. Localization::factory()->withCountry($countryCode, $language)->createQuietly([
  84. 'company_id' => $company->id,
  85. 'created_by' => $user->id,
  86. 'updated_by' => $user->id,
  87. ]);
  88. }
  89. }