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.

CompanyDefaultService.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace App\Services;
  3. use App\Enums\CategoryType;
  4. use App\Models\Company;
  5. use App\Models\Setting\Appearance;
  6. use App\Models\Setting\Category;
  7. use App\Models\Setting\CompanyDefault;
  8. use App\Models\Setting\Currency;
  9. use App\Models\Setting\Discount;
  10. use App\Models\Setting\DocumentDefault;
  11. use App\Models\Setting\Localization;
  12. use App\Models\Setting\Tax;
  13. use App\Models\User;
  14. use Illuminate\Support\Facades\DB;
  15. class CompanyDefaultService
  16. {
  17. public function createCompanyDefaults(Company $company, User $user, string $currencyCode, string $countryCode, string $language): void
  18. {
  19. DB::transaction(function () use ($company, $user, $currencyCode, $countryCode, $language) {
  20. $categories = $this->createCategories($company, $user);
  21. $currency = $this->createCurrency($company, $user, $currencyCode);
  22. $salesTax = $this->createSalesTax($company, $user);
  23. $purchaseTax = $this->createPurchaseTax($company, $user);
  24. $salesDiscount = $this->createSalesDiscount($company, $user);
  25. $purchaseDiscount = $this->createPurchaseDiscount($company, $user);
  26. $this->createAppearance($company, $user);
  27. $this->createDocumentDefaults($company, $user);
  28. $this->createLocalization($company, $user, $countryCode, $language);
  29. $companyDefaults = [
  30. 'company_id' => $company->id,
  31. 'income_category_id' => $categories['income_category_id'],
  32. 'expense_category_id' => $categories['expense_category_id'],
  33. 'currency_code' => $currency->code,
  34. 'sales_tax_id' => $salesTax->id,
  35. 'purchase_tax_id' => $purchaseTax->id,
  36. 'sales_discount_id' => $salesDiscount->id,
  37. 'purchase_discount_id' => $purchaseDiscount->id,
  38. 'created_by' => $user->id,
  39. 'updated_by' => $user->id,
  40. ];
  41. CompanyDefault::firstOrCreate(['company_id' => $company->id], $companyDefaults);
  42. }, 5);
  43. }
  44. private function createCategories(Company $company, User $user): array
  45. {
  46. $incomeCategories = ['Salary', 'Bonus', 'Interest', 'Dividends', 'Rentals'];
  47. $expenseCategories = ['Rent', 'Utilities', 'Food', 'Transportation', 'Entertainment'];
  48. $shuffledCategories = [
  49. ...array_map(static fn ($name) => ['name' => $name, 'type' => CategoryType::Income->value], $incomeCategories),
  50. ...array_map(static fn ($name) => ['name' => $name, 'type' => CategoryType::Expense->value], $expenseCategories),
  51. ];
  52. shuffle($shuffledCategories);
  53. $incomeEnabled = $expenseEnabled = false;
  54. $enabledIncomeCategoryId = null;
  55. $enabledExpenseCategoryId = null;
  56. foreach ($shuffledCategories as $category) {
  57. $enabled = false;
  58. if (! $incomeEnabled && $category['type'] === CategoryType::Income->value) {
  59. $enabled = $incomeEnabled = true;
  60. } elseif (! $expenseEnabled && $category['type'] === CategoryType::Expense->value) {
  61. $enabled = $expenseEnabled = true;
  62. }
  63. $categoryModel = Category::factory()->create([
  64. 'company_id' => $company->id,
  65. 'name' => $category['name'],
  66. 'type' => $category['type'],
  67. 'enabled' => $enabled,
  68. 'created_by' => $user->id,
  69. 'updated_by' => $user->id,
  70. ]);
  71. if ($enabled && $category['type'] === CategoryType::Income->value) {
  72. $enabledIncomeCategoryId = $categoryModel->id;
  73. } elseif ($enabled && $category['type'] === CategoryType::Expense->value) {
  74. $enabledExpenseCategoryId = $categoryModel->id;
  75. }
  76. }
  77. return [
  78. 'income_category_id' => $enabledIncomeCategoryId,
  79. 'expense_category_id' => $enabledExpenseCategoryId,
  80. ];
  81. }
  82. private function createCurrency(Company $company, User $user, string $currencyCode)
  83. {
  84. return Currency::factory()->forCurrency($currencyCode)->create([
  85. 'company_id' => $company->id,
  86. 'created_by' => $user->id,
  87. 'updated_by' => $user->id,
  88. ]);
  89. }
  90. private function createSalesTax(Company $company, User $user)
  91. {
  92. return Tax::factory()->salesTax()->create([
  93. 'company_id' => $company->id,
  94. 'created_by' => $user->id,
  95. 'updated_by' => $user->id,
  96. ]);
  97. }
  98. private function createPurchaseTax(Company $company, User $user)
  99. {
  100. return Tax::factory()->purchaseTax()->create([
  101. 'company_id' => $company->id,
  102. 'created_by' => $user->id,
  103. 'updated_by' => $user->id,
  104. ]);
  105. }
  106. private function createSalesDiscount(Company $company, User $user)
  107. {
  108. return Discount::factory()->salesDiscount()->create([
  109. 'company_id' => $company->id,
  110. 'created_by' => $user->id,
  111. 'updated_by' => $user->id,
  112. ]);
  113. }
  114. private function createPurchaseDiscount(Company $company, User $user)
  115. {
  116. return Discount::factory()->purchaseDiscount()->create([
  117. 'company_id' => $company->id,
  118. 'created_by' => $user->id,
  119. 'updated_by' => $user->id,
  120. ]);
  121. }
  122. private function createAppearance(Company $company, User $user): void
  123. {
  124. Appearance::factory()->create([
  125. 'company_id' => $company->id,
  126. 'created_by' => $user->id,
  127. 'updated_by' => $user->id,
  128. ]);
  129. }
  130. private function createDocumentDefaults(Company $company, User $user): void
  131. {
  132. DocumentDefault::factory()->invoice()->create([
  133. 'company_id' => $company->id,
  134. 'created_by' => $user->id,
  135. 'updated_by' => $user->id,
  136. ]);
  137. DocumentDefault::factory()->bill()->create([
  138. 'company_id' => $company->id,
  139. 'created_by' => $user->id,
  140. 'updated_by' => $user->id,
  141. ]);
  142. }
  143. private function createLocalization(Company $company, User $user, string $countryCode, string $language): void
  144. {
  145. Localization::factory()->withCountry($countryCode, $language)->create([
  146. 'company_id' => $company->id,
  147. 'created_by' => $user->id,
  148. 'updated_by' => $user->id,
  149. ]);
  150. }
  151. }