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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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\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\Support\Facades\DB;
  14. class CompanyDefaultService
  15. {
  16. public function createCompanyDefaults(Company $company, User $user, string $currencyCode, string $countryCode, string $language): void
  17. {
  18. DB::transaction(function () use ($company, $user, $currencyCode, $countryCode, $language) {
  19. $this->createCategories($company, $user);
  20. $this->createCurrency($company, $user, $currencyCode);
  21. $this->createSalesTax($company, $user);
  22. $this->createPurchaseTax($company, $user);
  23. $this->createSalesDiscount($company, $user);
  24. $this->createPurchaseDiscount($company, $user);
  25. $this->createAppearance($company, $user);
  26. $this->createDocumentDefaults($company, $user);
  27. $this->createLocalization($company, $user, $countryCode, $language);
  28. }, 5);
  29. }
  30. private function createCategories(Company $company, User $user): void
  31. {
  32. $incomeCategories = ['Dividends', 'Interest Earned', 'Wages', 'Sales', 'Other Income'];
  33. $expenseCategories = ['Rent or Mortgage', 'Utilities', 'Groceries', 'Transportation', 'Other Expense'];
  34. $otherCategories = ['Transfer', 'Other'];
  35. $defaultIncomeCategory = 'Sales';
  36. $defaultExpenseCategory = 'Rent or Mortgage';
  37. $this->createCategory($company, $user, $defaultIncomeCategory, CategoryType::Income, true);
  38. $this->createCategory($company, $user, $defaultExpenseCategory, CategoryType::Expense, true);
  39. foreach ($incomeCategories as $incomeCategory) {
  40. if ($incomeCategory !== $defaultIncomeCategory) {
  41. $this->createCategory($company, $user, $incomeCategory, CategoryType::Income);
  42. }
  43. }
  44. foreach ($expenseCategories as $expenseCategory) {
  45. if ($expenseCategory !== $defaultExpenseCategory) {
  46. $this->createCategory($company, $user, $expenseCategory, CategoryType::Expense);
  47. }
  48. }
  49. foreach ($otherCategories as $otherCategory) {
  50. $this->createCategory($company, $user, $otherCategory, CategoryType::Other);
  51. }
  52. }
  53. private function createCategory(Company $company, User $user, string $name, CategoryType $type, bool $enabled = false): void
  54. {
  55. Category::factory()->create([
  56. 'company_id' => $company->id,
  57. 'name' => $name,
  58. 'type' => $type,
  59. 'enabled' => $enabled,
  60. 'created_by' => $user->id,
  61. 'updated_by' => $user->id,
  62. ]);
  63. }
  64. private function createCurrency(Company $company, User $user, string $currencyCode): void
  65. {
  66. Currency::factory()->forCurrency($currencyCode)->create([
  67. 'company_id' => $company->id,
  68. 'created_by' => $user->id,
  69. 'updated_by' => $user->id,
  70. ]);
  71. }
  72. private function createSalesTax(Company $company, User $user): void
  73. {
  74. Tax::factory()->salesTax()->create([
  75. 'company_id' => $company->id,
  76. 'created_by' => $user->id,
  77. 'updated_by' => $user->id,
  78. ]);
  79. }
  80. private function createPurchaseTax(Company $company, User $user): void
  81. {
  82. Tax::factory()->purchaseTax()->create([
  83. 'company_id' => $company->id,
  84. 'created_by' => $user->id,
  85. 'updated_by' => $user->id,
  86. ]);
  87. }
  88. private function createSalesDiscount(Company $company, User $user): void
  89. {
  90. Discount::factory()->salesDiscount()->create([
  91. 'company_id' => $company->id,
  92. 'created_by' => $user->id,
  93. 'updated_by' => $user->id,
  94. ]);
  95. }
  96. private function createPurchaseDiscount(Company $company, User $user): void
  97. {
  98. Discount::factory()->purchaseDiscount()->create([
  99. 'company_id' => $company->id,
  100. 'created_by' => $user->id,
  101. 'updated_by' => $user->id,
  102. ]);
  103. }
  104. private function createAppearance(Company $company, User $user): void
  105. {
  106. Appearance::factory()->create([
  107. 'company_id' => $company->id,
  108. 'created_by' => $user->id,
  109. 'updated_by' => $user->id,
  110. ]);
  111. }
  112. private function createDocumentDefaults(Company $company, User $user): void
  113. {
  114. DocumentDefault::factory()->invoice()->create([
  115. 'company_id' => $company->id,
  116. 'created_by' => $user->id,
  117. 'updated_by' => $user->id,
  118. ]);
  119. DocumentDefault::factory()->bill()->create([
  120. 'company_id' => $company->id,
  121. 'created_by' => $user->id,
  122. 'updated_by' => $user->id,
  123. ]);
  124. }
  125. private function createLocalization(Company $company, User $user, string $countryCode, string $language): void
  126. {
  127. Localization::factory()->withCountry($countryCode, $language)->create([
  128. 'company_id' => $company->id,
  129. 'created_by' => $user->id,
  130. 'updated_by' => $user->id,
  131. ]);
  132. }
  133. }