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.

DefaultSettingService.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Company;
  4. use App\Models\Setting\Category;
  5. use App\Models\Setting\Currency;
  6. use App\Models\Setting\DefaultSetting;
  7. use App\Models\Setting\Discount;
  8. use App\Models\Setting\DocumentDefault;
  9. use App\Models\Setting\Tax;
  10. use Illuminate\Support\Facades\Auth;
  11. class DefaultSettingService
  12. {
  13. public function createDefaultSettings(Company $company): void
  14. {
  15. $categories = $this->createDefaultCategories($company);
  16. $currency = $this->createDefaultCurrency($company);
  17. $salesTax = $this->createDefaultSalesTax($company);
  18. $purchaseTax = $this->createDefaultPurchaseTax($company);
  19. $salesDiscount = $this->createDefaultSalesDiscount($company);
  20. $purchaseDiscount = $this->createDefaultPurchaseDiscount($company);
  21. $defaultSettings = [
  22. 'company_id' => $company->id,
  23. 'income_category_id' => $categories['income_category_id'],
  24. 'expense_category_id' => $categories['expense_category_id'],
  25. 'currency_code' => $currency->code,
  26. 'sales_tax_id' => $salesTax->id,
  27. 'purchase_tax_id' => $purchaseTax->id,
  28. 'sales_discount_id' => $salesDiscount->id,
  29. 'purchase_discount_id' => $purchaseDiscount->id,
  30. 'created_by' => $company->owner->id,
  31. 'updated_by' => $company->owner->id,
  32. ];
  33. DefaultSetting::create($defaultSettings);
  34. $this->createDefaultDocuments($company);
  35. }
  36. private function createDefaultCategories(Company $company): array
  37. {
  38. $incomeCategories = ['Salary', 'Bonus', 'Interest', 'Dividends', 'Rentals'];
  39. $expenseCategories = ['Rent', 'Utilities', 'Food', 'Transportation', 'Entertainment'];
  40. $shuffledCategories = [
  41. ...array_map(static fn ($name) => ['name' => $name, 'type' => 'income'], $incomeCategories),
  42. ...array_map(static fn ($name) => ['name' => $name, 'type' => 'expense'], $expenseCategories),
  43. ];
  44. shuffle($shuffledCategories);
  45. $incomeEnabled = $expenseEnabled = false;
  46. $defaultSettings = [];
  47. foreach ($shuffledCategories as $category) {
  48. $enabled = false;
  49. if (!$incomeEnabled && $category['type'] === 'income') {
  50. $enabled = $incomeEnabled = true;
  51. } elseif (!$expenseEnabled && $category['type'] === 'expense') {
  52. $enabled = $expenseEnabled = true;
  53. }
  54. $categoryModel = Category::factory()->create([
  55. 'company_id' => $company->id,
  56. 'name' => $category['name'],
  57. 'type' => $category['type'],
  58. 'enabled' => $enabled,
  59. 'created_by' => Auth::id(),
  60. 'updated_by' => Auth::id(),
  61. ]);
  62. $defaultSettings[$category['type'] . '_category_id'] = $categoryModel->id;
  63. }
  64. return $defaultSettings;
  65. }
  66. private function createDefaultDocuments(Company $company): void
  67. {
  68. DocumentDefault::factory()->invoice()->create([
  69. 'company_id' => $company->id,
  70. 'created_by' => Auth::id(),
  71. 'updated_by' => Auth::id(),
  72. ]);
  73. DocumentDefault::factory()->bill()->create([
  74. 'company_id' => $company->id,
  75. 'created_by' => Auth::id(),
  76. 'updated_by' => Auth::id(),
  77. ]);
  78. }
  79. private function createDefaultCurrency(Company $company): Currency
  80. {
  81. return Currency::factory()->create([
  82. 'company_id' => $company->id,
  83. 'created_by' => Auth::id(),
  84. 'updated_by' => Auth::id(),
  85. ]);
  86. }
  87. private function createDefaultSalesTax(Company $company): Tax
  88. {
  89. return Tax::factory()->salesTax()->create([
  90. 'company_id' => $company->id,
  91. 'created_by' => Auth::id(),
  92. 'updated_by' => Auth::id(),
  93. ]);
  94. }
  95. private function createDefaultPurchaseTax(Company $company): Tax
  96. {
  97. return Tax::factory()->purchaseTax()->create([
  98. 'company_id' => $company->id,
  99. 'created_by' => Auth::id(),
  100. 'updated_by' => Auth::id(),
  101. ]);
  102. }
  103. private function createDefaultSalesDiscount(Company $company): Discount
  104. {
  105. return Discount::factory()->salesDiscount()->create([
  106. 'company_id' => $company->id,
  107. 'created_by' => Auth::id(),
  108. 'updated_by' => Auth::id(),
  109. ]);
  110. }
  111. private function createDefaultPurchaseDiscount(Company $company): Discount
  112. {
  113. return Discount::factory()->purchaseDiscount()->create([
  114. 'company_id' => $company->id,
  115. 'created_by' => Auth::id(),
  116. 'updated_by' => Auth::id(),
  117. ]);
  118. }
  119. }