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

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