您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CompanyDefaultFactory.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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)->create([
  64. 'company_id' => $company->id,
  65. 'created_by' => $user->id,
  66. 'updated_by' => $user->id,
  67. ]);
  68. }
  69. private function createSalesTax(Company $company, User $user): Tax
  70. {
  71. return Tax::factory()->salesTax()->create([
  72. 'company_id' => $company->id,
  73. 'created_by' => $user->id,
  74. 'updated_by' => $user->id,
  75. ]);
  76. }
  77. private function createPurchaseTax(Company $company, User $user): Tax
  78. {
  79. return Tax::factory()->purchaseTax()->create([
  80. 'company_id' => $company->id,
  81. 'created_by' => $user->id,
  82. 'updated_by' => $user->id,
  83. ]);
  84. }
  85. private function createSalesDiscount(Company $company, User $user): Discount
  86. {
  87. return Discount::factory()->salesDiscount()->create([
  88. 'company_id' => $company->id,
  89. 'created_by' => $user->id,
  90. 'updated_by' => $user->id,
  91. ]);
  92. }
  93. private function createPurchaseDiscount(Company $company, User $user): Discount
  94. {
  95. return Discount::factory()->purchaseDiscount()->create([
  96. 'company_id' => $company->id,
  97. 'created_by' => $user->id,
  98. 'updated_by' => $user->id,
  99. ]);
  100. }
  101. private function createAppearance(Company $company, User $user): void
  102. {
  103. Appearance::factory()->create([
  104. 'company_id' => $company->id,
  105. 'created_by' => $user->id,
  106. 'updated_by' => $user->id,
  107. ]);
  108. }
  109. private function createDocumentDefaults(Company $company, User $user): void
  110. {
  111. DocumentDefault::factory()->invoice()->create([
  112. 'company_id' => $company->id,
  113. 'created_by' => $user->id,
  114. 'updated_by' => $user->id,
  115. ]);
  116. DocumentDefault::factory()->bill()->create([
  117. 'company_id' => $company->id,
  118. 'created_by' => $user->id,
  119. 'updated_by' => $user->id,
  120. ]);
  121. }
  122. private function createLocalization(Company $company, User $user, string $countryCode, string $language): void
  123. {
  124. Localization::factory()->withCountry($countryCode, $language)->create([
  125. 'company_id' => $company->id,
  126. 'created_by' => $user->id,
  127. 'updated_by' => $user->id,
  128. ]);
  129. }
  130. }