UnitService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace App\Services;
  3. use App\Models\ClassPackage;
  4. use App\Models\Franchisee;
  5. use App\Models\FranchiseeUnit;
  6. use App\Models\Unit;
  7. use App\Models\UnitFinancial;
  8. use App\Models\UserType;
  9. use Illuminate\Database\Eloquent\Collection;
  10. use Illuminate\Http\UploadedFile;
  11. use Illuminate\Support\Facades\Storage;
  12. class UnitService
  13. {
  14. public function __construct(
  15. protected ClassPackageUnitService $packageUnitService,
  16. ) {}
  17. public function getAll(): Collection
  18. {
  19. return Unit::with(['city', 'state'])
  20. ->orderBy('fantasy_name')
  21. ->get();
  22. }
  23. public function findById(int $id): ?Unit
  24. {
  25. return Unit::with(['city', 'state', 'partners'])->find($id);
  26. }
  27. public function create(array $data): Unit
  28. {
  29. $franchiseeData = [
  30. 'name' => $data['name'],
  31. 'cnpj' => $data['cnpj'],
  32. 'cpf' => null,
  33. 'rg' => null,
  34. 'phone' => $data['phone_number'] ?? null,
  35. 'cellphone_number' => $data['cell_number'],
  36. 'street' => $data['street'],
  37. 'address_number' => $data['address_number'] ?? null,
  38. 'neighborhood' => $data['neighborhood'],
  39. 'postal_code' => $data['postal_code'],
  40. 'city_id' => $data['city_id'],
  41. 'state_id' => $data['state_id'],
  42. 'birth_date' => null,
  43. ];
  44. $data = $this->handleAvatar($data);
  45. $unit = Unit::create($data);
  46. $userType = UserType::create([
  47. 'unit_id' => $unit->id,
  48. 'system_key' => 'ADMIN_FRANCHISEE',
  49. 'scope_key' => "unit:{$unit->id}",
  50. 'slug' => 'ADMIN_FRANCHISEE',
  51. 'label' => 'Administrador',
  52. 'is_system' => true,
  53. 'access_scope' => 'unit',
  54. ]);
  55. // Clona os perfis customizados (Neurotrainer, Financeiro, Gestor) baseados na unidade da Andreia
  56. $andreiaUnitId = \App\Models\User::find(11)?->units()->value('units.id');
  57. if ($andreiaUnitId) {
  58. $templateRoles = UserType::where('unit_id', $andreiaUnitId)
  59. ->where('is_system', false)
  60. ->with('permissions')
  61. ->get();
  62. foreach ($templateRoles as $templateRole) {
  63. $newRole = UserType::create([
  64. 'unit_id' => $unit->id,
  65. 'system_key' => $templateRole->system_key,
  66. 'scope_key' => "unit:{$unit->id}",
  67. 'slug' => \Illuminate\Support\Str::slug($templateRole->label . '-' . $unit->id),
  68. 'label' => $templateRole->label,
  69. 'is_system' => false,
  70. 'access_scope' => 'unit',
  71. ]);
  72. foreach ($templateRole->permissions as $p) {
  73. \App\Models\UserTypePermission::create([
  74. 'user_type' => $newRole->slug,
  75. 'user_type_id' => $newRole->id,
  76. 'permission_id' => $p->permission_id,
  77. 'bits' => $p->bits,
  78. ]);
  79. }
  80. }
  81. }
  82. $franchisee = Franchisee::create($franchiseeData);
  83. FranchiseeUnit::create([
  84. 'franchisee_id' => $franchisee->id,
  85. 'unit_id' => $unit->id,
  86. ]);
  87. // Dados financeiros da unidade já nascem com a cobrança de ROI/FNM ativa;
  88. // para desativar, edita-se na tab Financeiro da unidade.
  89. UnitFinancial::create([
  90. 'unit_id' => $unit->id,
  91. 'charge_roi' => true,
  92. 'charge_fnm' => true,
  93. ]);
  94. $this->replicatePackagesToUnit($unit->id);
  95. return $unit;
  96. }
  97. public function update(int $id, array $data): ?Unit
  98. {
  99. $model = $this->findById($id);
  100. if (!$model) {
  101. return null;
  102. }
  103. unset($data['franchisee']);
  104. $data = $this->handleAvatar($data, $model->avatar_url);
  105. $model->update($data);
  106. return $model->fresh(['city', 'state']);
  107. }
  108. public function delete(int $id): bool
  109. {
  110. $model = $this->findById($id);
  111. if (!$model) {
  112. return false;
  113. }
  114. if ($model->avatar_url) {
  115. Storage::delete($model->avatar_url);
  116. }
  117. return $model->delete();
  118. }
  119. //
  120. private function replicatePackagesToUnit(int $unitId): void
  121. {
  122. ClassPackage::with(['pavaoProposal.products', 'irrecusableProposal.products'])->get()->each(function (ClassPackage $package) use ($unitId) {
  123. $this->packageUnitService->replicateFromBasePackage($unitId, $package);
  124. });
  125. }
  126. private function handleAvatar(array $data, ?string $oldAvatarPath = null): array
  127. {
  128. if (!isset($data['avatar'])) {
  129. return $data;
  130. }
  131. if ($data['avatar'] instanceof UploadedFile) {
  132. if ($oldAvatarPath) {
  133. Storage::delete($oldAvatarPath);
  134. }
  135. $data['avatar_url'] = $data['avatar']->store('units/avatars');
  136. } elseif (is_null($data['avatar'])) {
  137. if ($oldAvatarPath) {
  138. Storage::delete($oldAvatarPath);
  139. }
  140. $data['avatar_url'] = null;
  141. }
  142. unset($data['avatar']);
  143. return $data;
  144. }
  145. }