UnitService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 getSelectList(): \Illuminate\Support\Collection
  24. {
  25. return Unit::orderBy('fantasy_name')
  26. ->get(['id', 'fantasy_name']);
  27. }
  28. public function findById(int $id): ?Unit
  29. {
  30. return Unit::with(['city', 'state', 'partners'])->find($id);
  31. }
  32. public function create(array $data): Unit
  33. {
  34. $franchiseeData = [
  35. 'name' => $data['fantasy_name'],
  36. 'cnpj' => $data['cnpj'],
  37. 'cpf' => null,
  38. 'rg' => null,
  39. 'phone' => $data['cell_number'] ?? null,
  40. 'cellphone_number' => $data['phone_number'] ?? null,
  41. 'street' => $data['street'],
  42. 'address_number' => $data['address_number'] ?? null,
  43. 'neighborhood' => $data['neighborhood'],
  44. 'postal_code' => $data['postal_code'],
  45. 'city_id' => $data['city_id'],
  46. 'state_id' => $data['state_id'],
  47. 'birth_date' => null,
  48. ];
  49. $data = $this->handleAvatar($data);
  50. $unit = Unit::create($data);
  51. UserType::create([
  52. 'unit_id' => $unit->id,
  53. 'system_key' => 'ADMIN_FRANCHISEE',
  54. 'scope_key' => "unit:{$unit->id}",
  55. 'slug' => 'ADMIN_FRANCHISEE',
  56. 'label' => 'Administrador',
  57. 'is_system' => true,
  58. 'access_scope' => 'unit',
  59. ]);
  60. $franchisee = Franchisee::create($franchiseeData);
  61. FranchiseeUnit::create([
  62. 'franchisee_id' => $franchisee->id,
  63. 'unit_id' => $unit->id,
  64. ]);
  65. // Dados financeiros da unidade já nascem com a cobrança de ROI/FNM ativa;
  66. // para desativar, edita-se na tab Financeiro da unidade.
  67. UnitFinancial::create([
  68. 'unit_id' => $unit->id,
  69. 'charge_roi' => true,
  70. 'charge_fnm' => true,
  71. ]);
  72. $this->replicatePackagesToUnit($unit->id);
  73. return $unit;
  74. }
  75. public function update(int $id, array $data): ?Unit
  76. {
  77. $model = $this->findById($id);
  78. if (!$model) {
  79. return null;
  80. }
  81. unset($data['franchisee']);
  82. $data = $this->handleAvatar($data, $model->avatar_url);
  83. $model->update($data);
  84. return $model->fresh(['city', 'state']);
  85. }
  86. public function delete(int $id): bool
  87. {
  88. $model = $this->findById($id);
  89. if (!$model) {
  90. return false;
  91. }
  92. if ($model->avatar_url) {
  93. Storage::delete($model->avatar_url);
  94. }
  95. return $model->delete();
  96. }
  97. //
  98. private function replicatePackagesToUnit(int $unitId): void
  99. {
  100. ClassPackage::with('products')->get()->each(function (ClassPackage $package) use ($unitId) {
  101. $this->packageUnitService->replicateFromBasePackage($unitId, $package);
  102. });
  103. }
  104. private function handleAvatar(array $data, ?string $oldAvatarPath = null): array
  105. {
  106. if (!isset($data['avatar'])) {
  107. return $data;
  108. }
  109. if ($data['avatar'] instanceof UploadedFile) {
  110. if ($oldAvatarPath) {
  111. Storage::delete($oldAvatarPath);
  112. }
  113. $data['avatar_url'] = $data['avatar']->store('units/avatars');
  114. } elseif (is_null($data['avatar'])) {
  115. if ($oldAvatarPath) {
  116. Storage::delete($oldAvatarPath);
  117. }
  118. $data['avatar_url'] = null;
  119. }
  120. unset($data['avatar']);
  121. return $data;
  122. }
  123. }