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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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\Discount;
  9. use App\Models\Setting\DocumentDefault;
  10. use App\Models\Setting\Localization;
  11. use App\Models\Setting\Tax;
  12. use App\Models\User;
  13. use Illuminate\Database\Eloquent\Factories\Factory;
  14. /**
  15. * @extends Factory<CompanyDefault>
  16. */
  17. class CompanyDefaultFactory extends Factory
  18. {
  19. /**
  20. * The name of the factory's corresponding model.
  21. */
  22. protected $model = CompanyDefault::class;
  23. /**
  24. * Define the model's default state.
  25. *
  26. * @return array<string, mixed>
  27. */
  28. public function definition(): array
  29. {
  30. return [
  31. //
  32. ];
  33. }
  34. public function withDefault(User $user, Company $company, ?string $currencyCode, string $countryCode, string $language = 'en'): static
  35. {
  36. if ($currencyCode === null) {
  37. /** @var CurrencyCode $currencyFaker */
  38. $currencyFaker = $this->faker;
  39. $currencyCode = $currencyFaker->currencyCode($countryCode);
  40. }
  41. $currency = $this->createCurrency($company, $user, $currencyCode);
  42. $salesTax = $this->createSalesTax($company, $user);
  43. $purchaseTax = $this->createPurchaseTax($company, $user);
  44. $salesDiscount = $this->createSalesDiscount($company, $user);
  45. $purchaseDiscount = $this->createPurchaseDiscount($company, $user);
  46. $this->createAppearance($company, $user);
  47. $this->createDocumentDefaults($company, $user);
  48. $this->createLocalization($company, $user, $countryCode, $language);
  49. $companyDefaults = [
  50. 'company_id' => $company->id,
  51. 'currency_code' => $currency->code,
  52. 'sales_tax_id' => $salesTax->id,
  53. 'purchase_tax_id' => $purchaseTax->id,
  54. 'sales_discount_id' => $salesDiscount->id,
  55. 'purchase_discount_id' => $purchaseDiscount->id,
  56. 'created_by' => $user->id,
  57. 'updated_by' => $user->id,
  58. ];
  59. return $this->state($companyDefaults);
  60. }
  61. private function createCurrency(Company $company, User $user, string $currencyCode): Currency
  62. {
  63. return Currency::factory()->forCurrency($currencyCode)->createQuietly([
  64. 'company_id' => $company->id,
  65. 'enabled' => true,
  66. 'created_by' => $user->id,
  67. 'updated_by' => $user->id,
  68. ]);
  69. }
  70. private function createSalesTax(Company $company, User $user): Tax
  71. {
  72. return Tax::factory()->salesTax()->createQuietly([
  73. 'company_id' => $company->id,
  74. 'enabled' => true,
  75. 'created_by' => $user->id,
  76. 'updated_by' => $user->id,
  77. ]);
  78. }
  79. private function createPurchaseTax(Company $company, User $user): Tax
  80. {
  81. return Tax::factory()->purchaseTax()->createQuietly([
  82. 'company_id' => $company->id,
  83. 'enabled' => true,
  84. 'created_by' => $user->id,
  85. 'updated_by' => $user->id,
  86. ]);
  87. }
  88. private function createSalesDiscount(Company $company, User $user): Discount
  89. {
  90. return Discount::factory()->salesDiscount()->createQuietly([
  91. 'company_id' => $company->id,
  92. 'enabled' => true,
  93. 'created_by' => $user->id,
  94. 'updated_by' => $user->id,
  95. ]);
  96. }
  97. private function createPurchaseDiscount(Company $company, User $user): Discount
  98. {
  99. return Discount::factory()->purchaseDiscount()->createQuietly([
  100. 'company_id' => $company->id,
  101. 'enabled' => true,
  102. 'created_by' => $user->id,
  103. 'updated_by' => $user->id,
  104. ]);
  105. }
  106. private function createAppearance(Company $company, User $user): void
  107. {
  108. Appearance::factory()->createQuietly([
  109. 'company_id' => $company->id,
  110. 'created_by' => $user->id,
  111. 'updated_by' => $user->id,
  112. ]);
  113. }
  114. private function createDocumentDefaults(Company $company, User $user): void
  115. {
  116. DocumentDefault::factory()->invoice()->createQuietly([
  117. 'company_id' => $company->id,
  118. 'created_by' => $user->id,
  119. 'updated_by' => $user->id,
  120. ]);
  121. DocumentDefault::factory()->bill()->createQuietly([
  122. 'company_id' => $company->id,
  123. 'created_by' => $user->id,
  124. 'updated_by' => $user->id,
  125. ]);
  126. }
  127. private function createLocalization(Company $company, User $user, string $countryCode, string $language): void
  128. {
  129. Localization::factory()->withCountry($countryCode, $language)->createQuietly([
  130. 'company_id' => $company->id,
  131. 'created_by' => $user->id,
  132. 'updated_by' => $user->id,
  133. ]);
  134. }
  135. }