ConveniosMedicosImportService.php 7.3 KB

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