UnitService.php 3.9 KB

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