UnitService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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\InhabitantClassification;
  7. use App\Models\Unit;
  8. use App\Models\UnitInhabitantClassification;
  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. $franchisee = Franchisee::create($franchiseeData);
  52. FranchiseeUnit::create([
  53. 'franchisee_id' => $franchisee->id,
  54. 'unit_id' => $unit->id,
  55. ]);
  56. $this->replicatePackagesToUnit($unit->id);
  57. $this->replicateInhabitantClassificationsToUnit($unit->id);
  58. return $unit;
  59. }
  60. public function update(int $id, array $data): ?Unit
  61. {
  62. $model = $this->findById($id);
  63. if (!$model) {
  64. return null;
  65. }
  66. unset($data['franchisee']);
  67. $data = $this->handleAvatar($data, $model->avatar_url);
  68. $model->update($data);
  69. return $model->fresh(['city', 'state']);
  70. }
  71. public function delete(int $id): bool
  72. {
  73. $model = $this->findById($id);
  74. if (!$model) {
  75. return false;
  76. }
  77. if ($model->avatar_url) {
  78. Storage::delete($model->avatar_url);
  79. }
  80. return $model->delete();
  81. }
  82. //
  83. private function replicatePackagesToUnit(int $unitId): void
  84. {
  85. ClassPackage::with('products')->get()->each(function (ClassPackage $package) use ($unitId) {
  86. $this->packageUnitService->replicateFromBasePackage($unitId, $package);
  87. });
  88. }
  89. private function replicateInhabitantClassificationsToUnit(int $unitId): void
  90. {
  91. InhabitantClassification::all()->each(function (InhabitantClassification $parent) use ($unitId) {
  92. UnitInhabitantClassification::create([
  93. 'unit_id' => $unitId,
  94. 'description' => $parent->description,
  95. 'start' => $parent->start,
  96. 'end' => $parent->end,
  97. 'tbr_percentage' => $parent->tbr_percentage,
  98. ]);
  99. });
  100. }
  101. private function handleAvatar(array $data, ?string $oldAvatarPath = null): array
  102. {
  103. if (!isset($data['avatar'])) {
  104. return $data;
  105. }
  106. if ($data['avatar'] instanceof UploadedFile) {
  107. if ($oldAvatarPath) {
  108. Storage::delete($oldAvatarPath);
  109. }
  110. $data['avatar_url'] = $data['avatar']->store('units/avatars');
  111. } elseif (is_null($data['avatar'])) {
  112. if ($oldAvatarPath) {
  113. Storage::delete($oldAvatarPath);
  114. }
  115. $data['avatar_url'] = null;
  116. }
  117. unset($data['avatar']);
  118. return $data;
  119. }
  120. }