UnitService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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['fantasy_name'],
  31. 'cnpj' => $data['cnpj'],
  32. 'cpf' => null,
  33. 'rg' => null,
  34. 'phone' => $data['cell_number'] ?? null,
  35. 'cellphone_number' => $data['phone_number'] ?? null,
  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::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. $franchisee = Franchisee::create($franchiseeData);
  56. FranchiseeUnit::create([
  57. 'franchisee_id' => $franchisee->id,
  58. 'unit_id' => $unit->id,
  59. ]);
  60. // Dados financeiros da unidade já nascem com a cobrança de ROI/FNM ativa;
  61. // para desativar, edita-se na tab Financeiro da unidade.
  62. UnitFinancial::create([
  63. 'unit_id' => $unit->id,
  64. 'charge_roi' => true,
  65. 'charge_fnm' => true,
  66. ]);
  67. $this->replicatePackagesToUnit($unit->id);
  68. return $unit;
  69. }
  70. public function update(int $id, array $data): ?Unit
  71. {
  72. $model = $this->findById($id);
  73. if (!$model) {
  74. return null;
  75. }
  76. unset($data['franchisee']);
  77. $data = $this->handleAvatar($data, $model->avatar_url);
  78. $model->update($data);
  79. return $model->fresh(['city', 'state']);
  80. }
  81. public function delete(int $id): bool
  82. {
  83. $model = $this->findById($id);
  84. if (!$model) {
  85. return false;
  86. }
  87. if ($model->avatar_url) {
  88. Storage::delete($model->avatar_url);
  89. }
  90. return $model->delete();
  91. }
  92. //
  93. private function replicatePackagesToUnit(int $unitId): void
  94. {
  95. ClassPackage::with('products')->get()->each(function (ClassPackage $package) use ($unitId) {
  96. $this->packageUnitService->replicateFromBasePackage($unitId, $package);
  97. });
  98. }
  99. private function handleAvatar(array $data, ?string $oldAvatarPath = null): array
  100. {
  101. if (!isset($data['avatar'])) {
  102. return $data;
  103. }
  104. if ($data['avatar'] instanceof UploadedFile) {
  105. if ($oldAvatarPath) {
  106. Storage::delete($oldAvatarPath);
  107. }
  108. $data['avatar_url'] = $data['avatar']->store('units/avatars');
  109. } elseif (is_null($data['avatar'])) {
  110. if ($oldAvatarPath) {
  111. Storage::delete($oldAvatarPath);
  112. }
  113. $data['avatar_url'] = null;
  114. }
  115. unset($data['avatar']);
  116. return $data;
  117. }
  118. }