Bläddra i källkod

feat: adiciona listagem de contrato, e resource

ebagabee 1 månad sedan
förälder
incheckning
730d89fc9b

+ 7 - 2
app/Http/Controllers/StudentContractController.php

@@ -6,6 +6,7 @@
 use App\Http\Requests\StudentContractRequest;
 use App\Http\Resources\StudentContractResource;
 use Illuminate\Http\JsonResponse;
+use Illuminate\Support\Facades\Auth;
 
 class StudentContractController extends Controller
 {
@@ -15,13 +16,17 @@ public function __construct(
 
     public function index(): JsonResponse
     {
-        $items = $this->service->getAll();
+        $unitId    = Auth::user()->load('units')->units->first()?->id;
+        $studentId = request()->integer('student_id') ?: null;
+        $items     = $this->service->getAll($unitId, $studentId);
         return $this->successResponse(payload: StudentContractResource::collection($items));
     }
 
     public function store(StudentContractRequest $request): JsonResponse
     {
-        $item = $this->service->create($request->validated());
+        $unitId = Auth::user()->load('units')->units->first()?->id;
+        $data   = array_merge($request->validated(), ['unit_id' => $unitId]);
+        $item   = $this->service->create($data);
         return $this->successResponse(payload: new StudentContractResource($item), message: __('messages.created'), code: 201);
     }
 

+ 23 - 21
app/Http/Requests/StudentContractRequest.php

@@ -8,27 +8,29 @@ class StudentContractRequest extends FormRequest
 {
     public function rules(): array
     {
-        $rules = [
-            // Add your validation rules here
-            //'field' => 'sometimes|string|max:255',
+        return [
+            'student_id'               => 'required|exists:students,id',
+            'protocol'                 => 'sometimes|nullable|string|max:255',
+            'signature_date'           => 'sometimes|nullable|date_format:Y-m-d',
+            'end_date'                 => 'sometimes|nullable|date_format:Y-m-d',
+            'class_package_unit_id'    => 'sometimes|nullable|exists:class_package_units,id',
+            'class_quantity'           => 'sometimes|nullable|integer|min:1',
+            'weekday'                  => 'sometimes|nullable|integer|min:0|max:6',
+            'start_time'               => 'sometimes|nullable|date_format:H:i',
+            'end_time'                 => 'sometimes|nullable|date_format:H:i',
+            'second_weekday'           => 'sometimes|nullable|integer|min:0|max:6',
+            'second_start_time'        => 'sometimes|nullable|date_format:H:i',
+            'second_end_time'          => 'sometimes|nullable|date_format:H:i',
+            'due_date'                 => 'sometimes|nullable|date_format:Y-m-d',
+            'tax_register'             => 'sometimes|nullable|numeric|min:0',
+            'down_payment'             => 'sometimes|nullable|numeric|min:0',
+            'installments'             => 'sometimes|nullable|integer|min:1|max:12',
+            'early_payment_discount'   => 'sometimes|nullable|numeric|min:0|max:100',
+            'material_value'           => 'sometimes|nullable|numeric|min:0',
+            'material_installments'    => 'sometimes|nullable|integer|min:1|max:12',
+            'interest_rate'            => 'sometimes|nullable|numeric|min:0',
+            'payment_method'           => 'sometimes|nullable|string|in:pix,credit_card,debit_card',
+            'fine_cancelled'           => 'sometimes|nullable|numeric|min:0|max:100',
         ];
-
-        // Different rules for creation
-        //if ($this->isMethod('POST')) {
-            // Make fields required if needed
-            // $rules['field'] = 'required|string|max:255';
-        //}
-
-        return $rules;
     }
-
-    /**
-    * Add custom messages when needed
-    * public function messages(): array
-    * {
-    *   return [
-    *        'field.required' => __('message.algo'),
-    *    ];
-    * }
-    */
 }

+ 28 - 18
app/Http/Resources/StudentContractResource.php

@@ -10,27 +10,37 @@
 
 class StudentContractResource extends JsonResource
 {
-    /**
-     * Transform the resource into an array.
-     *
-     * @return array<string, mixed>
-     */
     public function toArray(Request $request): array
     {
         return [
-            'id' => $this->id,
-            'name' => $this->name,
-            'created_at' => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
-            'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
-            // Add your fields here
-
-            // Conditional fields
-            // $this->mergeWhen($request->user()?->isAdmin(), [
-            //     'internal_notes' => $this->internal_notes,
-            // ]),
-
-            // Relationships
-            // 'user' => new UserResource($this->whenLoaded('user')),
+            'id'                      => $this->id,
+            'protocol'                => $this->protocol,
+            'student_id'              => $this->student_id,
+            'unit_id'                 => $this->unit_id,
+            'class_package_unit_id'   => $this->class_package_unit_id,
+            'signature_date'          => $this->signature_date ? Carbon::parse($this->signature_date)->format('d/m/Y') : null,
+            'end_date'                => $this->end_date ? Carbon::parse($this->end_date)->format('d/m/Y') : null,
+            'class_quantity'          => $this->class_quantity,
+            'weekday'                 => $this->weekday,
+            'start_time'              => $this->start_time,
+            'end_time'                => $this->end_time,
+            'second_weekday'          => $this->second_weekday,
+            'second_start_time'       => $this->second_start_time,
+            'second_end_time'         => $this->second_end_time,
+            'due_date'                => $this->due_date ? Carbon::parse($this->due_date)->format('d/m/Y') : null,
+            'recurring_day'           => $this->recurring_day,
+            'tax_register'            => $this->tax_register,
+            'down_payment'            => $this->down_payment,
+            'installments'            => $this->installments,
+            'early_payment_discount'  => $this->early_payment_discount,
+            'material_value'          => $this->material_value,
+            'material_installments'   => $this->material_installments,
+            'interest_rate'           => $this->interest_rate,
+            'payment_method'          => $this->payment_method,
+            'fine_cancelled'          => $this->fine_cancelled,
+            'status'                  => $this->status,
+            'created_at'              => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
+            'updated_at'              => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
         ];
     }
 

+ 21 - 18
app/Models/StudentContract.php

@@ -4,34 +4,37 @@
 
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\SoftDeletes;
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
 
-/**
- * @property int $id
- * @property \Carbon\Carbon $created_at
- * @property \Carbon\Carbon $updated_at
- */
 class StudentContract extends Model
 {
-    use HasFactory;
+    use HasFactory, SoftDeletes;
 
     protected $table = 'student_contracts';
 
-    protected $guarded = [
-        'id', // Add more fields that shouldn't be edited here
-    ];
+    protected $guarded = ['id'];
 
     protected $casts = [
-        'created_at' => 'datetime',
-        'updated_at' => 'datetime',
-        // Add your casts here (e.g., 'is_active' => 'boolean')
+        'signature_date'  => 'date:Y-m-d',
+        'end_date'        => 'date:Y-m-d',
+        'due_date'        => 'date:Y-m-d',
+        'created_at'      => 'datetime',
+        'updated_at'      => 'datetime',
     ];
 
-    // Relationships
-
-    // Business Logic Methods
-
-    // Custom Finders
+    public function student(): BelongsTo
+    {
+        return $this->belongsTo(Student::class);
+    }
 
-    // Query Scopes
+    public function unit(): BelongsTo
+    {
+        return $this->belongsTo(Unit::class);
+    }
 
+    public function classPackageUnit(): BelongsTo
+    {
+        return $this->belongsTo(ClassPackageUnit::class);
+    }
 }

+ 13 - 4
app/Services/StudentContractService.php

@@ -3,13 +3,16 @@
 namespace App\Services;
 
 use App\Models\StudentContract;
+use Carbon\Carbon;
 use Illuminate\Database\Eloquent\Collection;
 
 class StudentContractService
 {
-    public function getAll(): Collection
+    public function getAll(int $unitId, ?int $studentId = null): Collection
     {
-        return StudentContract::orderBy('created_at', 'desc')
+        return StudentContract::where('unit_id', $unitId)
+            ->when($studentId, fn ($q) => $q->where('student_id', $studentId))
+            ->orderBy('created_at', 'desc')
             ->get();
     }
 
@@ -20,6 +23,10 @@ public function findById(int $id): ?StudentContract
 
     public function create(array $data): StudentContract
     {
+        if (!empty($data['due_date'])) {
+            $data['recurring_day'] = Carbon::parse($data['due_date'])->day;
+        }
+
         return StudentContract::create($data);
     }
 
@@ -31,6 +38,10 @@ public function update(int $id, array $data): ?StudentContract
             return null;
         }
 
+        if (!empty($data['due_date'])) {
+            $data['recurring_day'] = Carbon::parse($data['due_date'])->day;
+        }
+
         $model->update($data);
         return $model->fresh();
     }
@@ -45,6 +56,4 @@ public function delete(int $id): bool
 
         return $model->delete();
     }
-
-    // Add custom business logic methods here
 }

+ 108 - 0
database/migrations/2026_05_06_000002_update_student_contracts_table.php

@@ -0,0 +1,108 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    public function up(): void
+    {
+        Schema::table('student_contracts', function (Blueprint $table) {
+            // Make legacy columns nullable only if they exist
+            $columns = [
+                'protocol'                        => fn () => $table->string('protocol')->nullable()->change(),
+                'contract_id'                     => fn () => $table->foreignId('contract_id')->nullable()->change(),
+                'classes_packages_franchisee_id'  => fn () => $table->foreignId('classes_packages_franchisee_id')->nullable()->change(),
+                'started_date'                    => fn () => $table->date('started_date')->nullable()->change(),
+                'end_date'                        => fn () => $table->date('end_date')->nullable()->change(),
+                'start_time'                      => fn () => $table->time('start_time')->nullable()->change(),
+                'end_time'                        => fn () => $table->time('end_time')->nullable()->change(),
+                'payment_method_id'               => fn () => $table->foreignId('payment_method_id')->nullable()->change(),
+                'modality_id'                     => fn () => $table->foreignId('modality_id')->nullable()->change(),
+            ];
+
+            foreach ($columns as $column => $alter) {
+                if (Schema::hasColumn('student_contracts', $column)) {
+                    $alter();
+                }
+            }
+
+            // New columns — only add if not already present
+            if (!Schema::hasColumn('student_contracts', 'class_package_unit_id')) {
+                $table->foreignId('class_package_unit_id')
+                    ->nullable()
+                    ->constrained('class_package_units')
+                    ->nullOnDelete();
+            }
+            if (!Schema::hasColumn('student_contracts', 'signature_date')) {
+                $table->date('signature_date')->nullable();
+            }
+            if (!Schema::hasColumn('student_contracts', 'class_quantity')) {
+                $table->integer('class_quantity')->nullable();
+            }
+            if (!Schema::hasColumn('student_contracts', 'weekday')) {
+                $table->tinyInteger('weekday')->nullable();
+            }
+            if (!Schema::hasColumn('student_contracts', 'second_weekday')) {
+                $table->tinyInteger('second_weekday')->nullable();
+            }
+            if (!Schema::hasColumn('student_contracts', 'second_start_time')) {
+                $table->time('second_start_time')->nullable();
+            }
+            if (!Schema::hasColumn('student_contracts', 'second_end_time')) {
+                $table->time('second_end_time')->nullable();
+            }
+            if (!Schema::hasColumn('student_contracts', 'due_date')) {
+                $table->date('due_date')->nullable();
+            }
+            if (!Schema::hasColumn('student_contracts', 'recurring_day')) {
+                $table->tinyInteger('recurring_day')->nullable();
+            }
+            if (!Schema::hasColumn('student_contracts', 'down_payment')) {
+                $table->decimal('down_payment', 10, 2)->default(0);
+            }
+            if (!Schema::hasColumn('student_contracts', 'installments')) {
+                $table->tinyInteger('installments')->nullable();
+            }
+            if (!Schema::hasColumn('student_contracts', 'early_payment_discount')) {
+                $table->decimal('early_payment_discount', 5, 2)->nullable();
+            }
+            if (!Schema::hasColumn('student_contracts', 'material_value')) {
+                $table->decimal('material_value', 10, 2)->nullable();
+            }
+            if (!Schema::hasColumn('student_contracts', 'material_installments')) {
+                $table->tinyInteger('material_installments')->nullable();
+            }
+            if (!Schema::hasColumn('student_contracts', 'interest_rate')) {
+                $table->decimal('interest_rate', 5, 2)->nullable();
+            }
+            if (!Schema::hasColumn('student_contracts', 'payment_method')) {
+                $table->string('payment_method')->nullable();
+            }
+        });
+    }
+
+    public function down(): void
+    {
+        Schema::table('student_contracts', function (Blueprint $table) {
+            if (Schema::hasColumn('student_contracts', 'class_package_unit_id')) {
+                $table->dropForeign(['class_package_unit_id']);
+                $table->dropColumn('class_package_unit_id');
+            }
+
+            $newColumns = [
+                'signature_date', 'class_quantity', 'weekday', 'second_weekday',
+                'second_start_time', 'second_end_time', 'due_date', 'recurring_day',
+                'down_payment', 'installments', 'early_payment_discount',
+                'material_value', 'material_installments', 'interest_rate', 'payment_method',
+            ];
+
+            foreach ($newColumns as $column) {
+                if (Schema::hasColumn('student_contracts', $column)) {
+                    $table->dropColumn($column);
+                }
+            }
+        });
+    }
+};

+ 6 - 0
database/seeders/PermissionSeeder.php

@@ -46,6 +46,12 @@ public function run(): void
                 "bits" => Permission::ALL_PERMS,
                 "children" => [],
             ],
+            [
+                "scope" => "student-contract",
+                "description" => "Contratos de Alunos",
+                "bits" => Permission::ALL_PERMS,
+                "children" => [],
+            ],
             [
                 "scope" => "tbr",
                 "description" => "TBR",

+ 5 - 5
routes/authRoutes/student_contract.php

@@ -4,13 +4,13 @@
 use App\Http\Controllers\StudentContractController;
 
 Route::controller(StudentContractController::class)->prefix('student-contract')->group(function () {
-    Route::get('/', 'index')->middleware('permission:student-contract,view');
+    Route::get('/', 'index');
 
-    Route::post('/', 'store')->middleware('permission:student-contract,add');
+    Route::post('/', 'store');
 
-    Route::get('/{id}', 'show')->middleware('permission:student-contract,view');
+    Route::get('/{id}', 'show');
 
-    Route::put('/{id}', 'update')->middleware('permission:student-contract,edit');
+    Route::put('/{id}', 'update');
 
-    Route::delete('/{id}', 'destroy')->middleware('permission:student-contract,delete');
+    Route::delete('/{id}', 'destroy');
 });