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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 $country, string $language = 'en'): static
  35. {
  36. /** @var CurrencyCode $currencyFaker */
  37. $currencyFaker = $this->faker;
  38. $currencyCode = $currencyFaker->currencyCode($country);
  39. $currency = $this->createCurrency($company, $user, $currencyCode);
  40. $salesTax = $this->createSalesTax($company, $user);
  41. $purchaseTax = $this->createPurchaseTax($company, $user);
  42. $salesDiscount = $this->createSalesDiscount($company, $user);
  43. $purchaseDiscount = $this->createPurchaseDiscount($company, $user);
  44. $this->createAppearance($company, $user);
  45. $this->createDocumentDefaults($company, $user);
  46. $this->createLocalization($company, $user, $country, $language);
  47. $companyDefaults = [
  48. 'company_id' => $company->id,
  49. 'currency_code' => $currency->code,
  50. 'sales_tax_id' => $salesTax->id,
  51. 'purchase_tax_id' => $purchaseTax->id,
  52. 'sales_discount_id' => $salesDiscount->id,
  53. 'purchase_discount_id' => $purchaseDiscount->id,
  54. 'created_by' => $user->id,
  55. 'updated_by' => $user->id,
  56. ];
  57. return $this->state($companyDefaults);
  58. }
  59. private function createCurrency(Company $company, User $user, string $currencyCode)
  60. {
  61. return Currency::factory()->forCurrency($currencyCode)->create([
  62. 'company_id' => $company->id,
  63. 'created_by' => $user->id,
  64. 'updated_by' => $user->id,
  65. ]);
  66. }
  67. private function createSalesTax(Company $company, User $user): Tax
  68. {
  69. return Tax::factory()->salesTax()->create([
  70. 'company_id' => $company->id,
  71. 'created_by' => $user->id,
  72. 'updated_by' => $user->id,
  73. ]);
  74. }
  75. private function createPurchaseTax(Company $company, User $user): Tax
  76. {
  77. return Tax::factory()->purchaseTax()->create([
  78. 'company_id' => $company->id,
  79. 'created_by' => $user->id,
  80. 'updated_by' => $user->id,
  81. ]);
  82. }
  83. private function createSalesDiscount(Company $company, User $user): Discount
  84. {
  85. return Discount::factory()->salesDiscount()->create([
  86. 'company_id' => $company->id,
  87. 'created_by' => $user->id,
  88. 'updated_by' => $user->id,
  89. ]);
  90. }
  91. private function createPurchaseDiscount(Company $company, User $user): Discount
  92. {
  93. return Discount::factory()->purchaseDiscount()->create([
  94. 'company_id' => $company->id,
  95. 'created_by' => $user->id,
  96. 'updated_by' => $user->id,
  97. ]);
  98. }
  99. private function createAppearance(Company $company, User $user): void
  100. {
  101. Appearance::factory()->create([
  102. 'company_id' => $company->id,
  103. 'created_by' => $user->id,
  104. 'updated_by' => $user->id,
  105. ]);
  106. }
  107. private function createDocumentDefaults(Company $company, User $user): void
  108. {
  109. DocumentDefault::factory()->invoice()->create([
  110. 'company_id' => $company->id,
  111. 'created_by' => $user->id,
  112. 'updated_by' => $user->id,
  113. ]);
  114. DocumentDefault::factory()->bill()->create([
  115. 'company_id' => $company->id,
  116. 'created_by' => $user->id,
  117. 'updated_by' => $user->id,
  118. ]);
  119. }
  120. private function createLocalization(Company $company, User $user, string $countryCode, string $language): void
  121. {
  122. Localization::factory()->withCountry($countryCode, $language)->create([
  123. 'company_id' => $company->id,
  124. 'created_by' => $user->id,
  125. 'updated_by' => $user->id,
  126. ]);
  127. }
  128. }