ConveniosMedicosImportService.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace App\Services;
  3. use App\Enums\PartnerAgreementServiceStatusEnum;
  4. use App\Enums\PartnerAgreementStatusEnum;
  5. use App\Imports\ParceirosImport;
  6. use App\Models\Category;
  7. use App\Models\PartnerAgreement;
  8. use App\Models\PartnerAgreementService;
  9. use App\Traits\ImportsPartners;
  10. use Illuminate\Http\UploadedFile;
  11. use Maatwebsite\Excel\Facades\Excel;
  12. class ConveniosMedicosImportService
  13. {
  14. use ImportsPartners;
  15. private const MODE_CONSULTAS = 'consultas';
  16. private const MODE_LABORATORIO = 'laboratorio';
  17. public function syncFromExcel(UploadedFile $file): array
  18. {
  19. $import = new ParceirosImport();
  20. Excel::import($import, $file);
  21. $rows = $import->rows ?? collect();
  22. $mode = null;
  23. $clinicGroups = [];
  24. $labRows = [];
  25. foreach ($rows as $row) {
  26. $col0 = $this->cell($row[0] ?? '');
  27. $col1 = $this->cell($row[1] ?? '');
  28. $col2 = $this->cell($row[2] ?? '');
  29. if ($col0 === '' && $col1 === '' && $col2 === '') {
  30. continue;
  31. }
  32. $upper0 = mb_strtoupper($col0);
  33. $upper1 = mb_strtoupper($col1);
  34. if ($upper0 === 'ESPECIALIDADE') {
  35. $mode = self::MODE_CONSULTAS;
  36. continue;
  37. }
  38. if (str_starts_with($upper0, 'LABORAT') && $upper1 === 'TELEFONE') {
  39. $mode = self::MODE_LABORATORIO;
  40. continue;
  41. }
  42. if ($mode === null) {
  43. continue;
  44. }
  45. if ($mode === self::MODE_CONSULTAS) {
  46. $specialty = $col0;
  47. $clinicName = $col1;
  48. $doctor = $col2;
  49. $phone = $this->cell($row[3] ?? '');
  50. $address = $this->cell($row[4] ?? '');
  51. $priceRaw = $this->cell($row[5] ?? '');
  52. if ($clinicName === '' || $specialty === '') {
  53. continue;
  54. }
  55. if (!isset($clinicGroups[$clinicName])) {
  56. $clinicGroups[$clinicName] = [
  57. 'phone' => null,
  58. 'address' => null,
  59. 'services' => [],
  60. ];
  61. }
  62. if ($phone && !$clinicGroups[$clinicName]['phone']) {
  63. $clinicGroups[$clinicName]['phone'] = $phone;
  64. }
  65. if ($address && !$clinicGroups[$clinicName]['address']) {
  66. $clinicGroups[$clinicName]['address'] = $address;
  67. }
  68. $serviceName = $doctor
  69. ? "{$specialty} - {$doctor}"
  70. : $specialty;
  71. $clinicGroups[$clinicName]['services'][] = [
  72. 'name' => $serviceName,
  73. 'price' => $this->parsePrice($priceRaw),
  74. ];
  75. } elseif ($mode === self::MODE_LABORATORIO) {
  76. if ($col0 === '') {
  77. continue;
  78. }
  79. $labRows[] = [
  80. 'name' => $col0,
  81. 'phone' => $col1 ?: null,
  82. 'address' => $col2 ?: null,
  83. ];
  84. }
  85. }
  86. $convMedicaCategory = Category::firstOrCreate(
  87. ['name' => 'Convênios Médicos', 'type' => 'partner'],
  88. ['active' => true]
  89. );
  90. $labCategory = Category::firstOrCreate(
  91. ['name' => 'Laboratório', 'type' => 'partner'],
  92. ['active' => true]
  93. );
  94. $stats = [
  95. 'partners_created' => 0,
  96. 'partners_updated' => 0,
  97. 'partners_inactivated'=> 0,
  98. 'services_created' => 0,
  99. 'services_updated' => 0,
  100. 'services_inactivated'=> 0,
  101. ];
  102. $importedPartnerIds = [];
  103. foreach ($clinicGroups as $clinicName => $clinicData) {
  104. [$partner, $isNew] = $this->upsertPartner($clinicName, [
  105. 'phone' => $clinicData['phone'],
  106. 'address' => $clinicData['address'],
  107. 'category_id' => $convMedicaCategory->id,
  108. ]);
  109. $importedPartnerIds[] = $partner->id;
  110. $isNew ? $stats['partners_created']++ : $stats['partners_updated']++;
  111. $processedServiceIds = [];
  112. foreach ($clinicData['services'] as $svcData) {
  113. $service = PartnerAgreementService::withTrashed()
  114. ->where('partner_agreement_id', $partner->id)
  115. ->whereRaw('LOWER(TRIM(name)) = LOWER(TRIM(?))', [$svcData['name']])
  116. ->first();
  117. if ($service) {
  118. if ($service->trashed()) {
  119. $service->restore();
  120. }
  121. $changed = false;
  122. if ($svcData['price'] !== null && (float) $service->associate_price !== (float) $svcData['price']) {
  123. $service->associate_price = $svcData['price'];
  124. $changed = true;
  125. }
  126. if ($service->status !== PartnerAgreementServiceStatusEnum::ACTIVE) {
  127. $service->status = PartnerAgreementServiceStatusEnum::ACTIVE;
  128. $changed = true;
  129. }
  130. if ($changed) {
  131. $service->save();
  132. $stats['services_updated']++;
  133. }
  134. $processedServiceIds[] = $service->id;
  135. } else {
  136. $created = PartnerAgreementService::create([
  137. 'partner_agreement_id' => $partner->id,
  138. 'name' => $svcData['name'],
  139. 'associate_price' => $svcData['price'],
  140. 'status' => PartnerAgreementServiceStatusEnum::ACTIVE,
  141. ]);
  142. $processedServiceIds[] = $created->id;
  143. $stats['services_created']++;
  144. }
  145. }
  146. if (!empty($processedServiceIds)) {
  147. $stats['services_inactivated'] += PartnerAgreementService::where('partner_agreement_id', $partner->id)
  148. ->where('status', PartnerAgreementServiceStatusEnum::ACTIVE)
  149. ->whereNotIn('id', $processedServiceIds)
  150. ->update(['status' => PartnerAgreementServiceStatusEnum::INACTIVE]);
  151. }
  152. }
  153. foreach ($labRows as $labData) {
  154. [$partner, $isNew] = $this->upsertPartner($labData['name'], [
  155. 'phone' => $labData['phone'],
  156. 'address' => $labData['address'],
  157. 'category_id' => $labCategory->id,
  158. ]);
  159. $importedPartnerIds[] = $partner->id;
  160. $isNew ? $stats['partners_created']++ : $stats['partners_updated']++;
  161. }
  162. if (!empty($importedPartnerIds)) {
  163. $stats['partners_inactivated'] = PartnerAgreement::whereIn('category_id', [
  164. $convMedicaCategory->id,
  165. $labCategory->id,
  166. ])
  167. ->where('status', PartnerAgreementStatusEnum::ACTIVE)
  168. ->whereNotIn('id', $importedPartnerIds)
  169. ->update(['status' => PartnerAgreementStatusEnum::INACTIVE]);
  170. }
  171. return $stats;
  172. }
  173. }