Sfoglia il codice sorgente

feat: adiciona edicao de contrato

ebagabee 1 mese fa
parent
commit
75c5e2c393

+ 6 - 2
app/Http/Controllers/FranchiseeContractController.php

@@ -6,6 +6,7 @@
 use App\Http\Requests\FranchiseeContractRequest;
 use App\Http\Resources\FranchiseeContractResource;
 use Illuminate\Http\JsonResponse;
+use Illuminate\Http\Request;
 
 class FranchiseeContractController extends Controller
 {
@@ -13,9 +14,12 @@ public function __construct(
         protected FranchiseeContractService $service,
     ) {}
 
-    public function index(): JsonResponse
+    public function index(Request $request): JsonResponse
     {
-        $items = $this->service->getAll();
+        $items = $request->has('unit_id')
+            ? $this->service->getByUnitId($request->integer('unit_id'))
+            : $this->service->getAll();
+
         return $this->successResponse(payload: FranchiseeContractResource::collection($items));
     }
 

+ 6 - 0
app/Http/Controllers/TbrController.php

@@ -13,6 +13,12 @@ public function __construct(
         protected TbrService $service,
     ) {}
 
+    public function current(): JsonResponse
+    {
+        $item = $this->service->getLatestCurrentYear();
+        return $this->successResponse(payload: $item ? new TbrResource($item) : null);
+    }
+
     public function index(): JsonResponse
     {
         $items = $this->service->getAll();

+ 11 - 17
app/Http/Requests/FranchiseeContractRequest.php

@@ -9,26 +9,20 @@ class FranchiseeContractRequest extends FormRequest
     public function rules(): array
     {
         $rules = [
-            // Add your validation rules here
-            //'field' => 'sometimes|string|max:255',
+            'start_date'                   => 'required|date',
+            'end_date'                     => 'required|date|after:start_date',
+            'tbr_fixed_value'              => 'required|numeric|min:0',
+            'invoice_due_date'             => 'nullable|date',
+            'inhabitant_classification_id' => 'nullable|integer|exists:inhabitant_classifications,id',
+            'tbr_fixed_value_percentage'   => 'nullable|numeric|between:0,1',
+            'marketing_fund_percentage'    => 'nullable|numeric|between:0,1',
+            'maintance_tax_percentage'     => 'nullable|numeric|between:0,1',
         ];
 
-        // Different rules for creation
-        //if ($this->isMethod('POST')) {
-            // Make fields required if needed
-            // $rules['field'] = 'required|string|max:255';
-        //}
+        if ($this->isMethod('POST')) {
+            $rules['unit_id'] = 'required|integer|exists:units,id';
+        }
 
         return $rules;
     }
-
-    /**
-    * Add custom messages when needed
-    * public function messages(): array
-    * {
-    *   return [
-    *        'field.required' => __('message.algo'),
-    *    ];
-    * }
-    */
 }

+ 14 - 13
app/Http/Resources/FranchiseeContractResource.php

@@ -18,19 +18,20 @@ class FranchiseeContractResource extends JsonResource
     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,
+            'unit_id'                      => $this->unit_id,
+            'protocol'                     => $this->protocol,
+            'start_date'                   => $this->start_date?->format('Y-m-d'),
+            'end_date'                     => $this->end_date?->format('Y-m-d'),
+            'invoice_due_date'             => $this->invoice_due_date?->format('Y-m-d'),
+            'tbr_fixed_value'              => $this->tbr_fixed_value,
+            'tbr_fixed_value_percentage'   => $this->tbr_fixed_value_percentage,
+            'marketing_fund_percentage'    => $this->marketing_fund_percentage,
+            'maintance_tax_percentage'     => $this->maintance_tax_percentage,
+            'inhabitant_classification_id' => $this->inhabitant_classification_id,
+            'validity_months'              => $this->validity_months,
+            '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'),
         ];
     }
 

+ 7 - 0
app/Http/Resources/UnitResource.php

@@ -52,6 +52,13 @@ public function toArray(Request $request): array
                 'name' => $this->state->name,
                 'code' => $this->state->code,
             ]),
+
+            'partners' => $this->whenLoaded('partners', fn() => $this->partners->map(fn($partner) => [
+                'id'         => $partner->id,
+                'name'       => $partner->name,
+                'cpf'        => $partner->cpf,
+                'birth_date' => $partner->birth_date?->format('d/m/Y'),
+            ])),
         ];
     }
 

+ 6 - 3
app/Models/FranchiseeContract.php

@@ -23,9 +23,12 @@ class FranchiseeContract extends Model
     protected $guarded = ['id'];
 
     protected $casts = [
-        'signature_date' => 'date',
-        'created_at'     => 'datetime',
-        'updated_at'     => 'datetime',
+        'start_date'      => 'date',
+        'end_date'        => 'date',
+        'invoice_due_date' => 'date',
+        'signature_date'  => 'date',
+        'created_at'      => 'datetime',
+        'updated_at'      => 'datetime',
     ];
 
     public function unit(): BelongsTo

+ 15 - 0
app/Services/FranchiseeContractService.php

@@ -18,8 +18,23 @@ public function findById(int $id): ?FranchiseeContract
         return FranchiseeContract::find($id);
     }
 
+    public function getByUnitId(int $unitId): Collection
+    {
+        return FranchiseeContract::where('unit_id', $unitId)
+            ->orderBy('created_at', 'desc')
+            ->get();
+    }
+
     public function create(array $data): FranchiseeContract
     {
+        $data['protocol'] = (FranchiseeContract::where('unit_id', $data['unit_id'])->max('protocol') ?? 0) + 1;
+        $data['signature_date'] = $data['start_date'];
+
+        if (isset($data['start_date'], $data['end_date'])) {
+            $data['validity_months'] = \Carbon\Carbon::parse($data['start_date'])
+                ->diffInMonths(\Carbon\Carbon::parse($data['end_date']));
+        }
+
         return FranchiseeContract::create($data);
     }
 

+ 7 - 0
app/Services/TbrService.php

@@ -22,6 +22,13 @@ public function findByYear(int $year): ?Tbr
         return Tbr::where('year', $year)->first();
     }
 
+    public function getLatestCurrentYear(): ?Tbr
+    {
+        return Tbr::where('year', now()->year)
+            ->orderBy('id', 'desc')
+            ->first();
+    }
+
     public function create(array $data): Tbr
     {
         return Tbr::create($data);

+ 1 - 1
app/Services/UnitService.php

@@ -26,7 +26,7 @@ public function getSelectList(): \Illuminate\Support\Collection
 
     public function findById(int $id): ?Unit
     {
-        return Unit::with(['city', 'state'])->find($id);
+        return Unit::with(['city', 'state', 'partners'])->find($id);
     }
 
     public function create(array $data): Unit

+ 47 - 0
database/migrations/2026_05_05_000001_add_fields_to_franchisee_contracts_table.php

@@ -0,0 +1,47 @@
+<?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('franchisee_contracts', function (Blueprint $table) {
+            $table->date('start_date')->nullable()->after('unit_id');
+            $table->date('end_date')->nullable()->after('start_date');
+            $table->date('invoice_due_date')->nullable()->after('end_date');
+            $table->foreignId('inhabitant_classification_id')
+                ->nullable()
+                ->after('invoice_due_date')
+                ->constrained('inhabitant_classifications')
+                ->nullOnDelete();
+
+            $table->integer('protocol')->nullable()->change();
+            $table->string('name')->nullable()->change();
+            $table->text('description')->nullable()->change();
+            $table->integer('validity_months')->nullable()->change();
+            $table->date('signature_date')->nullable()->change();
+        });
+    }
+
+    public function down(): void
+    {
+        Schema::table('franchisee_contracts', function (Blueprint $table) {
+            $table->dropForeign(['inhabitant_classification_id']);
+            $table->dropColumn([
+                'start_date',
+                'end_date',
+                'invoice_due_date',
+                'inhabitant_classification_id',
+            ]);
+
+            $table->integer('protocol')->nullable(false)->change();
+            $table->string('name')->nullable(false)->change();
+            $table->text('description')->nullable(false)->change();
+            $table->integer('validity_months')->nullable(false)->change();
+            $table->date('signature_date')->nullable(false)->change();
+        });
+    }
+};

+ 1 - 0
database/seeders/DatabaseSeeder.php

@@ -18,6 +18,7 @@ public function run(): void
             BrazilCitiesSeeder::class,
             ClassPackageSeeder::class,
             ProductSeeder::class,
+            InhabitantClassificationSeeder::class,
         ]);
     }
 }

+ 28 - 0
database/seeders/InhabitantClassificationSeeder.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace Database\Seeders;
+
+use App\Models\InhabitantClassification;
+use Illuminate\Database\Seeder;
+
+class InhabitantClassificationSeeder extends Seeder
+{
+    public function run(): void
+    {
+        $classifications = [
+            ['acronym' => 'P1', 'description' => 'Até 30.000 habitantes'],
+            ['acronym' => 'P2', 'description' => 'De 30.001 a 70.000 habitantes'],
+            ['acronym' => 'M1', 'description' => 'De 70.001 a 150.000 habitantes'],
+            ['acronym' => 'M2', 'description' => 'De 150.001 a 300.000 habitantes'],
+            ['acronym' => 'G1', 'description' => 'De 300.001 a 500.000 habitantes'],
+            ['acronym' => 'G2', 'description' => 'Acima de 500.000 habitantes'],
+        ];
+
+        foreach ($classifications as $item) {
+            InhabitantClassification::firstOrCreate(
+                ['acronym' => $item['acronym']],
+                $item,
+            );
+        }
+    }
+}

+ 5 - 5
routes/authRoutes/franchisee_contract.php

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

+ 2 - 0
routes/authRoutes/tbr.php

@@ -4,6 +4,8 @@
 use App\Http\Controllers\TbrController;
 
 Route::controller(TbrController::class)->prefix('tbr')->group(function () {
+    Route::get('/current', 'current')->middleware('permission:tbr,view');
+
     Route::get('/', 'index')->middleware('permission:tbr,view');
 
     Route::post('/', 'store')->middleware('permission:tbr,add');