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

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