Преглед изворни кода

fix importacao convenio medico

Gustavo Zanatta пре 4 часа
родитељ
комит
056f4cfa30
2 измењених фајлова са 43 додато и 22 уклоњено
  1. 11 2
      app/Http/Requests/AppointmentRequest.php
  2. 32 20
      app/Services/ConveniosMedicosImportService.php

+ 11 - 2
app/Http/Requests/AppointmentRequest.php

@@ -3,6 +3,7 @@
 namespace App\Http\Requests;
 
 use App\Enums\AppointmentStatusEnum;
+use App\Enums\PartnerAgreementTypeEnum;
 use Illuminate\Foundation\Http\FormRequest;
 use Illuminate\Validation\Rule;
 
@@ -10,8 +11,16 @@ class AppointmentRequest extends FormRequest
 {
     public function rules(): array
     {
-        $partnerExists = Rule::exists('partner_agreements', 'id')->whereNull('deleted_at');
-        $serviceExists = Rule::exists('partner_agreement_services', 'id')->whereNull('deleted_at');
+        $partnerExists = Rule::exists('partner_agreements', 'id')
+            ->where('type', PartnerAgreementTypeEnum::AGREEMENT->value)
+            ->whereNull('deleted_at');
+
+        $serviceExists = Rule::exists('partner_agreement_services', 'id')
+            ->whereNull('deleted_at')
+            ->when(
+                $this->filled('partner_agreement_id'),
+                fn ($rule) => $rule->where('partner_agreement_id', $this->input('partner_agreement_id')),
+            );
 
         $rules = [
             'user_id'                      => 'sometimes|integer|exists:users,id',

+ 32 - 20
app/Services/ConveniosMedicosImportService.php

@@ -29,23 +29,25 @@ class ConveniosMedicosImportService
         $labRows      = [];
 
         foreach ($rows as $row) {
-            $col0 = $this->cell($row[0] ?? '');
-            $col1 = $this->cell($row[1] ?? '');
-            $col2 = $this->cell($row[2] ?? '');
+            $cells = collect($row)
+                ->map(fn ($value) => $this->cell($value))
+                ->values()
+                ->all();
 
-            if ($col0 === '' && $col1 === '' && $col2 === '') {
+            $filled = array_values(array_filter($cells, fn ($value) => $value !== ''));
+
+            if (empty($filled)) {
                 continue;
             }
 
-            $upper0 = mb_strtoupper($col0);
-            $upper1 = mb_strtoupper($col1);
+            $upperFilled = array_map(fn ($value) => mb_strtoupper($value), $filled);
 
-            if ($upper0 === 'ESPECIALIDADE') {
+            if ($upperFilled[0] === 'ESPECIALIDADE') {
                 $mode = self::MODE_CONSULTAS;
                 continue;
             }
 
-            if (str_starts_with($upper0, 'LABORAT') && $upper1 === 'TELEFONE') {
+            if (str_starts_with($upperFilled[0], 'LABORAT') && in_array('TELEFONE', $upperFilled, true)) {
                 $mode = self::MODE_LABORATORIO;
                 continue;
             }
@@ -55,12 +57,12 @@ class ConveniosMedicosImportService
             }
 
             if ($mode === self::MODE_CONSULTAS) {
-                $specialty  = $col0;
-                $clinicName = $col1;
-                $doctor     = $col2;
-                $phone      = $this->cell($row[3] ?? '');
-                $address    = $this->cell($row[4] ?? '');
-                $priceRaw   = $this->cell($row[5] ?? '');
+                $specialty  = $cells[0] ?? '';
+                $clinicName = $cells[1] ?? '';
+                $doctor     = $cells[2] ?? '';
+                $phone      = $cells[3] ?? '';
+                $address    = $cells[4] ?? '';
+                $priceRaw   = $cells[5] ?? '';
 
                 if ($clinicName === '' || $specialty === '') {
                     continue;
@@ -90,14 +92,15 @@ class ConveniosMedicosImportService
                     'price' => $this->parsePrice($priceRaw),
                 ];
             } elseif ($mode === self::MODE_LABORATORIO) {
-                if ($col0 === '') {
-                    continue;
-                }
+                $rest = array_slice($filled, 1);
+
+                $phones = array_values(array_filter($rest, fn ($value) => $this->looksLikePhone($value)));
+                $others = array_values(array_filter($rest, fn ($value) => !$this->looksLikePhone($value)));
 
                 $labRows[] = [
-                    'name'    => $col0,
-                    'phone'   => $col1 ?: null,
-                    'address' => $col2 ?: null,
+                    'name'    => $filled[0],
+                    'phone'   => $phones[0] ?? null,
+                    'address' => $others[0] ?? null,
                 ];
             }
         }
@@ -218,4 +221,13 @@ class ConveniosMedicosImportService
 
         return $stats;
     }
+
+    private function looksLikePhone(string $value): bool
+    {
+        if (preg_match('/\p{L}/u', $value)) {
+            return false;
+        }
+
+        return strlen(preg_replace('/\D/', '', $value)) >= 8;
+    }
 }