Procházet zdrojové kódy

feat: :sparkles: crud prestadores formas pagamento

crud prestadores formas pagamento
Gustavo Zanatta před 1 měsícem
rodič
revize
dd4adc8f55

+ 9 - 0
app/Enums/AccountTypeEnum.php

@@ -0,0 +1,9 @@
+<?php
+
+namespace App\Enums;
+
+enum AccountTypeEnum: string
+{
+    case PIX = 'pix';
+    case BANK_ACCOUNT = 'bank_account';
+}

+ 9 - 0
app/Enums/BankAccountTypeEnum.php

@@ -0,0 +1,9 @@
+<?php
+
+namespace App\Enums;
+
+enum BankAccountTypeEnum: string
+{
+    case CHECKING = 'checking';
+    case SAVINGS = 'savings';
+}

+ 65 - 0
app/Http/Controllers/ProviderPaymentMethodController.php

@@ -0,0 +1,65 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Http\Requests\ProviderPaymentMethodRequest;
+use App\Http\Resources\ProviderPaymentMethodResource;
+use App\Services\ProviderPaymentMethodService;
+use Illuminate\Http\JsonResponse;
+use Illuminate\Http\Request;
+use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
+
+class ProviderPaymentMethodController extends Controller
+{
+    public function __construct(
+        private readonly ProviderPaymentMethodService $service
+    ) {}
+
+    public function index($id): JsonResponse
+    {
+        // $providerId = $request->query('provider_id');
+        $paymentMethods = $this->service->getByProvider($id);
+        // return ProviderPaymentMethodResource::collection($paymentMethods);
+        return $this->successResponse(
+            payload: ProviderPaymentMethodResource::collection($paymentMethods)
+        );
+    }
+
+    public function show(int $id): ProviderPaymentMethodResource
+    {
+        $paymentMethod = $this->service->findById($id);
+        abort_if(!$paymentMethod, 404);
+        return new ProviderPaymentMethodResource($paymentMethod);
+    }
+
+    public function store(ProviderPaymentMethodRequest $request): JsonResponse
+    {
+        $paymentMethod = $this->service->create($request->validated());
+        return $this->successResponse(
+            payload: new ProviderPaymentMethodResource($paymentMethod)
+        );
+    }
+
+    public function update(ProviderPaymentMethodRequest $request, int $id): JsonResponse
+    {
+        $paymentMethod = $this->service->findById($id);
+        abort_if(!$paymentMethod, 404);
+        
+        $paymentMethod = $this->service->update($paymentMethod, $request->validated());
+        return $this->successResponse(
+            payload: new ProviderPaymentMethodResource($paymentMethod)
+        );
+    }
+
+    public function destroy(int $id): JsonResponse
+    {
+        $paymentMethod = $this->service->findById($id);
+        abort_if(!$paymentMethod, 404);
+        
+        $this->service->delete($paymentMethod);
+        return $this->successResponse(
+            message: __("messages.deleted"),
+            code: 204,
+        );
+    }
+}

+ 29 - 0
app/Http/Requests/ProviderPaymentMethodRequest.php

@@ -0,0 +1,29 @@
+<?php
+
+namespace App\Http\Requests;
+
+use App\Enums\AccountTypeEnum;
+use App\Enums\BankAccountTypeEnum;
+use Illuminate\Foundation\Http\FormRequest;
+use Illuminate\Validation\Rule;
+
+class ProviderPaymentMethodRequest extends FormRequest
+{
+    public function authorize(): bool
+    {
+        return true;
+    }
+
+    public function rules(): array
+    {
+        return [
+            'provider_id' => ['required', 'exists:providers,id'],
+            'account_type' => ['required', Rule::in([AccountTypeEnum::PIX->value, AccountTypeEnum::BANK_ACCOUNT->value])],
+            'pix_key' => ['nullable', 'string', 'max:255', Rule::requiredIf($this->account_type === AccountTypeEnum::PIX->value)],
+            'bank_account_type' => ['nullable', Rule::in([BankAccountTypeEnum::CHECKING->value, BankAccountTypeEnum::SAVINGS->value]), Rule::requiredIf($this->account_type === AccountTypeEnum::BANK_ACCOUNT->value)],
+            'agency' => ['nullable', 'string', 'max:255', Rule::requiredIf($this->account_type === AccountTypeEnum::BANK_ACCOUNT->value)],
+            'account' => ['nullable', 'string', 'max:255', Rule::requiredIf($this->account_type === AccountTypeEnum::BANK_ACCOUNT->value)],
+            'digit' => ['nullable', 'string', 'max:255', Rule::requiredIf($this->account_type === AccountTypeEnum::BANK_ACCOUNT->value)],
+        ];
+    }
+}

+ 25 - 0
app/Http/Resources/ProviderPaymentMethodResource.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace App\Http\Resources;
+
+use Illuminate\Http\Request;
+use Illuminate\Http\Resources\Json\JsonResource;
+
+class ProviderPaymentMethodResource extends JsonResource
+{
+    public function toArray(Request $request): array
+    {
+        return [
+            'id' => $this->id,
+            'provider_id' => $this->provider_id,
+            'account_type' => $this->account_type?->value,
+            'pix_key' => $this->pix_key,
+            'bank_account_type' => $this->bank_account_type?->value,
+            'agency' => $this->agency,
+            'account' => $this->account,
+            'digit' => $this->digit,
+            'created_at' => $this->created_at,
+            'updated_at' => $this->updated_at,
+        ];
+    }
+}

+ 35 - 0
app/Models/ProviderPaymentMethod.php

@@ -0,0 +1,35 @@
+<?php
+
+namespace App\Models;
+
+use App\Enums\AccountTypeEnum;
+use App\Enums\BankAccountTypeEnum;
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
+use Illuminate\Database\Eloquent\SoftDeletes;
+
+class ProviderPaymentMethod extends Model
+{
+    use HasFactory, SoftDeletes;
+
+    protected $fillable = [
+        'provider_id',
+        'account_type',
+        'pix_key',
+        'bank_account_type',
+        'agency',
+        'account',
+        'digit',
+    ];
+
+    protected $casts = [
+        'account_type' => AccountTypeEnum::class,
+        'bank_account_type' => BankAccountTypeEnum::class,
+    ];
+
+    public function provider(): BelongsTo
+    {
+        return $this->belongsTo(Provider::class);
+    }
+}

+ 37 - 0
app/Services/ProviderPaymentMethodService.php

@@ -0,0 +1,37 @@
+<?php
+
+namespace App\Services;
+
+use App\Models\ProviderPaymentMethod;
+use Illuminate\Database\Eloquent\Collection;
+use Illuminate\Support\Facades\Log;
+
+class ProviderPaymentMethodService
+{
+    public function getByProvider(int $providerId): Collection
+    {
+        return ProviderPaymentMethod::where('provider_id', $providerId)->get();
+    }
+
+    public function findById(int $id): ?ProviderPaymentMethod
+    {
+        return ProviderPaymentMethod::find($id);
+    }
+
+    public function create(array $data): ProviderPaymentMethod
+    {
+        Log::info($data);
+        return ProviderPaymentMethod::create($data);
+    }
+
+    public function update(ProviderPaymentMethod $paymentMethod, array $data): ProviderPaymentMethod
+    {
+        $paymentMethod->update($data);
+        return $paymentMethod->fresh();
+    }
+
+    public function delete(ProviderPaymentMethod $paymentMethod): bool
+    {
+        return $paymentMethod->delete();
+    }
+}

+ 37 - 0
database/migrations/2026_02_09_133752_create_provider_payment_methods_table.php

@@ -0,0 +1,37 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     */
+    public function up(): void
+    {
+        Schema::create('provider_payment_methods', function (Blueprint $table) {
+            $table->id();
+            $table->foreignId('provider_id')->constrained('providers')->onDelete('cascade');
+            $table->enum('account_type', ['pix', 'bank_account']);
+            $table->string('pix_key')->nullable();
+            $table->enum('bank_account_type', ['checking', 'savings'])->nullable();
+            $table->string('agency')->nullable();
+            $table->string('account')->nullable();
+            $table->string('digit')->nullable();
+            $table->timestamps();
+            $table->softDeletes();
+
+            $table->index('provider_id');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+        Schema::dropIfExists('provider_payment_methods');
+    }
+};

+ 6 - 0
database/seeders/PermissionSeeder.php

@@ -100,6 +100,12 @@ class PermissionSeeder extends Seeder
                         "bits" => 271,
                         "children" => [],
                     ],
+                    [
+                        "scope" => "config.provider_payment_method",
+                        "description" => "Configurações de Métodos de Pagamento do Prestador",
+                        "bits" => 271,
+                        "children" => [],
+                    ],
                     [
                         "scope" => "config.improvement_type",
                         "description" => "Configurações de Tipos de Melhoria",

+ 1 - 0
database/seeders/UserTypePermissionSeeder.php

@@ -39,6 +39,7 @@ class UserTypePermissionSeeder extends Seeder
                         ['scope' => 'config.provider', 'bits' => 271],
                         ['scope' => 'config.provider_speciality', 'bits' => 271],
                         ['scope' => 'config.provider_services_types', 'bits' => 271],
+                        ['scope' => 'config.provider_payment_method', 'bits' => 271],
                         ['scope' => 'config.improvement_type', 'bits' => 271],
                         ['scope' => 'config.media', 'bits' => 271],
                         ['scope' => 'config.service_type', 'bits' => 271],

+ 10 - 0
routes/authRoutes/provider_payment_method.php

@@ -0,0 +1,10 @@
+<?php
+
+use App\Http\Controllers\ProviderPaymentMethodController;
+use Illuminate\Support\Facades\Route;
+
+Route::get('/provider/payment-methods/{id}', [ProviderPaymentMethodController::class, 'index'])->middleware('permission:config.provider_payment_method,view');
+Route::get('/provider/payment-method/{id}', [ProviderPaymentMethodController::class, 'show'])->middleware('permission:config.provider_payment_method,view');
+Route::post('/provider/payment-method', [ProviderPaymentMethodController::class, 'store'])->middleware('permission:config.provider_payment_method,add');
+Route::put('/provider/payment-method/{id}', [ProviderPaymentMethodController::class, 'update'])->middleware('permission:config.provider_payment_method,edit');
+Route::delete('/provider/payment-method/{id}', [ProviderPaymentMethodController::class, 'destroy'])->middleware('permission:config.provider_payment_method,delete');