Quellcode durchsuchen

separacao paginas parceiro e convenio

Gustavo Zanatta vor 10 Stunden
Ursprung
Commit
690914fe23

+ 9 - 0
app/Enums/PartnerAgreementTypeEnum.php

@@ -0,0 +1,9 @@
+<?php
+
+namespace App\Enums;
+
+enum PartnerAgreementTypeEnum: string
+{
+    case PARTNER   = 'partner';
+    case AGREEMENT = 'agreement';
+}

+ 4 - 3
app/Http/Controllers/PartnerAgreementController.php

@@ -24,15 +24,16 @@ class PartnerAgreementController extends Controller
         protected MediaService $mediaService,
     ) {}
 
-    public function index(): JsonResponse
+    public function index(Request $request): JsonResponse
     {
-        $items = $this->service->getAll();
+        $filters = $request->only(['type']);
+        $items = $this->service->getAll($filters);
         return $this->successResponse(payload: PartnerAgreementListResource::collection($items));
     }
 
     public function indexPaginated(Request $request): JsonResponse
     {
-        $filters = $request->only(['search', 'expires_in_days', 'created_month']);
+        $filters = $request->only(['search', 'expires_in_days', 'created_month', 'type']);
         $perPage = min((int) $request->get('per_page', 10), 100);
         $paginator = $this->service->getAllPaginated($filters, $perPage);
 

+ 2 - 0
app/Http/Requests/PartnerAgreementRequest.php

@@ -3,6 +3,7 @@
 namespace App\Http\Requests;
 
 use App\Enums\PartnerAgreementStatusEnum;
+use App\Enums\PartnerAgreementTypeEnum;
 use App\Models\PartnerAgreement;
 use Illuminate\Foundation\Http\FormRequest;
 use Illuminate\Validation\Rule;
@@ -41,6 +42,7 @@ class PartnerAgreementRequest extends FormRequest
             'contract_start'      => 'sometimes|nullable|date',
             'contract_end'        => 'sometimes|nullable|date|after_or_equal:contract_start',
             'status'              => ['sometimes', Rule::enum(PartnerAgreementStatusEnum::class)],
+            'type'                => ['sometimes', Rule::enum(PartnerAgreementTypeEnum::class)],
         ];
 
         if ($this->isMethod('post')) {

+ 2 - 0
app/Models/PartnerAgreement.php

@@ -3,6 +3,7 @@
 namespace App\Models;
 
 use App\Enums\PartnerAgreementStatusEnum;
+use App\Enums\PartnerAgreementTypeEnum;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\Relations\BelongsTo;
 use Illuminate\Database\Eloquent\Relations\HasMany;
@@ -23,6 +24,7 @@ class PartnerAgreement extends Model
             'contract_start'      => 'date',
             'contract_end'        => 'date',
             'status'              => PartnerAgreementStatusEnum::class,
+            'type'                => PartnerAgreementTypeEnum::class,
         ];
     }
 

+ 2 - 2
app/Services/ConveniosMedicosImportService.php

@@ -128,7 +128,7 @@ class ConveniosMedicosImportService
                 'phone'       => $clinicData['phone'],
                 'address'     => $clinicData['address'],
                 'category_id' => $convMedicaCategory->id,
-            ]);
+            ], 'agreement');
 
             $importedPartnerIds[] = $partner->id;
             $isNew ? $stats['partners_created']++ : $stats['partners_updated']++;
@@ -190,7 +190,7 @@ class ConveniosMedicosImportService
                 'phone'       => $labData['phone'],
                 'address'     => $labData['address'],
                 'category_id' => $labCategory->id,
-            ]);
+            ], 'agreement');
 
             $importedPartnerIds[] = $partner->id;
             $isNew ? $stats['partners_created']++ : $stats['partners_updated']++;

+ 1 - 1
app/Services/ParceirosImportService.php

@@ -59,7 +59,7 @@ class ParceirosImportService
                 'description' => $col1 ?: null,
                 'phone'       => $col2 ?: null,
                 'category_id' => $currentCategoryId,
-            ]);
+            ], 'partner');
 
             $importedPartnerIds[] = $partner->id;
             $isNew ? $created++ : $updated++;

+ 13 - 4
app/Services/PartnerAgreementService.php

@@ -11,11 +11,16 @@ use Illuminate\Support\Facades\Hash;
 
 class PartnerAgreementService
 {
-    public function getAll(): Collection
+    public function getAll(array $filters = []): Collection
     {
-        return PartnerAgreement::with(['category', 'city', 'logo'])
-            ->orderBy('company_name')
-            ->get();
+        $query = PartnerAgreement::with(['category', 'city', 'logo'])
+            ->orderBy('company_name');
+
+        if (!empty($filters['type'])) {
+            $query->where('type', $filters['type']);
+        }
+
+        return $query->get();
     }
 
     public function getAllPaginated(array $filters = [], int $perPage = 10): LengthAwarePaginator
@@ -23,6 +28,10 @@ class PartnerAgreementService
         $query = PartnerAgreement::with(['category', 'city', 'logo'])
             ->orderBy('company_name');
 
+        if (!empty($filters['type'])) {
+            $query->where('type', $filters['type']);
+        }
+
         if (!empty($filters['search'])) {
             $search = $filters['search'];
             $query->where(function ($q) use ($search) {

+ 18 - 0
app/Services/PartnerAgreementServiceService.php

@@ -2,6 +2,8 @@
 
 namespace App\Services;
 
+use App\Enums\PartnerAgreementTypeEnum;
+use App\Models\PartnerAgreement;
 use App\Models\PartnerAgreementService;
 use Illuminate\Database\Eloquent\Collection;
 
@@ -22,6 +24,7 @@ class PartnerAgreementServiceService
 
     public function create(array $data): PartnerAgreementService
     {
+        $data = $this->enforceSchedulingRule($data);
         return PartnerAgreementService::create($data);
     }
 
@@ -33,10 +36,25 @@ class PartnerAgreementServiceService
             return null;
         }
 
+        $data = $this->enforceSchedulingRule($data, $model->partner_agreement_id);
         $model->update($data);
         return $model->fresh(['category', 'media']);
     }
 
+    private function enforceSchedulingRule(array $data, ?int $partnerAgreementId = null): array
+    {
+        $paId = $partnerAgreementId ?? ($data['partner_agreement_id'] ?? null);
+
+        if ($paId) {
+            $type = PartnerAgreement::where('id', $paId)->value('type');
+            if ($type === PartnerAgreementTypeEnum::PARTNER->value || $type === 'partner') {
+                $data['requires_scheduling'] = false;
+            }
+        }
+
+        return $data;
+    }
+
     public function delete(int $id): bool
     {
         $model = PartnerAgreementService::find($id);

+ 3 - 1
app/Traits/ImportsPartners.php

@@ -12,7 +12,7 @@ use Illuminate\Support\Str;
 
 trait ImportsPartners
 {
-    private function upsertPartner(string $companyName, array $data): array
+    private function upsertPartner(string $companyName, array $data, string $type = 'partner'): array
     {
         $partner = PartnerAgreement::withTrashed()
             ->whereRaw('LOWER(TRIM(company_name)) = LOWER(TRIM(?))', [$companyName])
@@ -24,6 +24,7 @@ trait ImportsPartners
             }
 
             $partner->update(array_merge($data, [
+                'type'   => $type,
                 'status' => PartnerAgreementStatusEnum::ACTIVE,
             ]));
 
@@ -35,6 +36,7 @@ trait ImportsPartners
         $partner = PartnerAgreement::create(array_merge($data, [
             'company_name' => $companyName,
             'user_id'      => $user->id,
+            'type'         => $type,
             'status'       => PartnerAgreementStatusEnum::ACTIVE,
         ]));
 

+ 34 - 0
database/migrations/2026_06_16_000001_add_type_to_partner_agreements_table.php

@@ -0,0 +1,34 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    public function up(): void
+    {
+        Schema::table('partner_agreements', function (Blueprint $table) {
+            $table->string('type')->default('partner')->after('id');
+            $table->index('type');
+        });
+
+        // Registros cujas categorias são de convênios médicos recebem type = 'agreement'
+        DB::statement("
+            UPDATE partner_agreements
+            SET type = 'agreement'
+            FROM categories
+            WHERE partner_agreements.category_id = categories.id
+              AND LOWER(TRIM(categories.name)) IN ('convênios médicos', 'convenios medicos', 'laboratório', 'laboratorio')
+        ");
+    }
+
+    public function down(): void
+    {
+        Schema::table('partner_agreements', function (Blueprint $table) {
+            $table->dropIndex(['type']);
+            $table->dropColumn('type');
+        });
+    }
+};