選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CompanyObserver.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Observers;
  3. use App\Models\Company;
  4. use App\Models\Setting\Category;
  5. use App\Models\Setting\Currency;
  6. use App\Models\Setting\Discount;
  7. use App\Models\Setting\DocumentDefault;
  8. use App\Models\Setting\Tax;
  9. class CompanyObserver
  10. {
  11. /**
  12. * Handle the Company "created" event.
  13. */
  14. public function created(Company $company): void
  15. {
  16. $incomeCategories = ['Salary', 'Bonus', 'Interest', 'Dividends', 'Rentals'];
  17. $expenseCategories = ['Rent', 'Utilities', 'Food', 'Transportation', 'Entertainment'];
  18. $shuffledCategories = [
  19. ...array_map(static fn ($name) => ['name' => $name, 'type' => 'income'], $incomeCategories),
  20. ...array_map(static fn ($name) => ['name' => $name, 'type' => 'expense'], $expenseCategories),
  21. ];
  22. shuffle($shuffledCategories);
  23. $incomeEnabled = $expenseEnabled = false;
  24. foreach ($shuffledCategories as $category) {
  25. $enabled = false;
  26. if (!$incomeEnabled && $category['type'] === 'income') {
  27. $enabled = $incomeEnabled = true;
  28. } elseif (!$expenseEnabled && $category['type'] === 'expense') {
  29. $enabled = $expenseEnabled = true;
  30. }
  31. Category::factory()->create([
  32. 'company_id' => $company->id,
  33. 'name' => $category['name'],
  34. 'type' => $category['type'],
  35. 'enabled' => $enabled,
  36. 'created_by' => $company->user_id,
  37. ]);
  38. }
  39. DocumentDefault::factory()->invoice()->create([
  40. 'company_id' => $company->id,
  41. ]);
  42. DocumentDefault::factory()->bill()->create([
  43. 'company_id' => $company->id,
  44. ]);
  45. Currency::factory()->create([
  46. 'company_id' => $company->id,
  47. 'created_by' => $company->user_id,
  48. ]);
  49. Tax::factory()->salesTax()->create([
  50. 'company_id' => $company->id,
  51. 'created_by' => $company->user_id,
  52. ]);
  53. Tax::factory()->purchaseTax()->create([
  54. 'company_id' => $company->id,
  55. 'created_by' => $company->user_id,
  56. ]);
  57. Discount::factory()->salesDiscount()->create([
  58. 'company_id' => $company->id,
  59. 'created_by' => $company->user_id,
  60. ]);
  61. Discount::factory()->purchaseDiscount()->create([
  62. 'company_id' => $company->id,
  63. 'created_by' => $company->user_id,
  64. ]);
  65. }
  66. /**
  67. * Handle the Company "updated" event.
  68. */
  69. public function updated(Company $company): void
  70. {
  71. //
  72. }
  73. /**
  74. * Handle the Company "deleted" event.
  75. */
  76. public function deleted(Company $company): void
  77. {
  78. //
  79. }
  80. /**
  81. * Handle the Company "restored" event.
  82. */
  83. public function restored(Company $company): void
  84. {
  85. //
  86. }
  87. /**
  88. * Handle the Company "force deleted" event.
  89. */
  90. public function forceDeleted(Company $company): void
  91. {
  92. //
  93. }
  94. }