Просмотр исходного кода

feat(franchisee-contracts): troca inhabitant_classification_id por municipality_size_id

Contrato agora referencia diretamente o porte do municipio
(PP/MP/GP/MGP), consolidando o porte como fonte para o calculo de
royalties. Mesma troca aplicada em franchisee_contract_tax_histories.

- Migrations editadas inline (com municipality_size_id + FK)
- Deleta migration de alter (tbr_fixed_value_percentage) ja consolidada
- Models: remove relacao com InhabitantClassification, adiciona municipalitySize
- Request/Resource/Service: trocam o campo e o eager load

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
ebagabee 3 недель назад
Родитель
Сommit
62261ee16a

+ 1 - 1
app/Http/Requests/FranchiseeContractRequest.php

@@ -13,7 +13,7 @@ public function rules(): array
             'end_date'                     => 'nullable|date|after:start_date',
             'tbr_fixed_value'              => 'nullable|numeric|min:0',
             'invoice_due_date'             => 'nullable|integer|between:1,31',
-            'inhabitant_classification_id' => 'nullable|integer|exists:inhabitant_classifications,id',
+            'municipality_size_id'         => 'nullable|integer|exists:municipality_sizes,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',

+ 5 - 4
app/Http/Resources/FranchiseeContractResource.php

@@ -33,10 +33,11 @@ public function toArray(Request $request): array
             '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,
-            'inhabitant_classification'    => $this->whenLoaded('inhabitantClassification', fn() => [
-                'id'          => $this->inhabitantClassification->id,
-                'description' => $this->inhabitantClassification->description,
+            'municipality_size_id'         => $this->municipality_size_id,
+            'municipality_size'            => $this->whenLoaded('municipalitySize', fn() => [
+                'id'          => $this->municipalitySize->id,
+                'acronym'     => $this->municipalitySize->acronym,
+                'description' => $this->municipalitySize->description,
             ]),
             'validity_months'              => $this->validity_months,
             'contract_month_current'       => $contractMonthCurrent,

+ 5 - 2
app/Http/Resources/FranchiseeContractTaxHistoryResource.php

@@ -11,8 +11,11 @@ public function toArray(Request $request): array
     {
         return [
             'id'                           => $this->id,
-            'inhabitant_classification_id' => $this->inhabitant_classification_id,
-            'inhabitant_classification'    => $this->whenLoaded('inhabitantClassification', fn() => $this->inhabitantClassification?->description),
+            'municipality_size_id'         => $this->municipality_size_id,
+            'municipality_size'            => $this->whenLoaded('municipalitySize', fn() => [
+                'id'          => $this->municipalitySize?->id,
+                'description' => $this->municipalitySize?->description,
+            ]),
             'tbr_fixed_value'              => $this->tbr_fixed_value,
             'tbr_fixed_value_percentage'   => $this->tbr_fixed_value_percentage,
             'marketing_fund_percentage'    => $this->marketing_fund_percentage,

+ 5 - 4
app/Models/FranchiseeContract.php

@@ -29,8 +29,9 @@
  * @property \Illuminate\Support\Carbon|null $start_date
  * @property \Illuminate\Support\Carbon|null $end_date
  * @property int|null $invoice_due_date
- * @property int|null $inhabitant_classification_id
+ * @property int|null $municipality_size_id
  * @property-read \App\Models\Unit $unit
+ * @property-read \App\Models\MunicipalitySize|null $municipalitySize
  * @method static \Illuminate\Database\Eloquent\Builder<static>|FranchiseeContract newModelQuery()
  * @method static \Illuminate\Database\Eloquent\Builder<static>|FranchiseeContract newQuery()
  * @method static \Illuminate\Database\Eloquent\Builder<static>|FranchiseeContract query()
@@ -41,7 +42,7 @@
  * @method static \Illuminate\Database\Eloquent\Builder<static>|FranchiseeContract whereDiscountUntilDueDate($value)
  * @method static \Illuminate\Database\Eloquent\Builder<static>|FranchiseeContract whereEndDate($value)
  * @method static \Illuminate\Database\Eloquent\Builder<static>|FranchiseeContract whereId($value)
- * @method static \Illuminate\Database\Eloquent\Builder<static>|FranchiseeContract whereInhabitantClassificationId($value)
+ * @method static \Illuminate\Database\Eloquent\Builder<static>|FranchiseeContract whereMunicipalitySizeId($value)
  * @method static \Illuminate\Database\Eloquent\Builder<static>|FranchiseeContract whereInvoiceDueDate($value)
  * @method static \Illuminate\Database\Eloquent\Builder<static>|FranchiseeContract whereMaintanceTaxFixedValue($value)
  * @method static \Illuminate\Database\Eloquent\Builder<static>|FranchiseeContract whereMaintanceTaxPercentage($value)
@@ -81,8 +82,8 @@ public function unit(): BelongsTo
         return $this->belongsTo(Unit::class, 'unit_id');
     }
 
-    public function inhabitantClassification(): BelongsTo
+    public function municipalitySize(): BelongsTo
     {
-        return $this->belongsTo(InhabitantClassification::class, 'inhabitant_classification_id');
+        return $this->belongsTo(MunicipalitySize::class, 'municipality_size_id');
     }
 }

+ 5 - 5
app/Models/FranchiseeContractTaxHistory.php

@@ -8,7 +8,7 @@
 /**
  * @property int $id
  * @property int $franchisee_contract_id
- * @property int|null $inhabitant_classification_id
+ * @property int|null $municipality_size_id
  * @property numeric|null $tbr_fixed_value
  * @property numeric|null $marketing_fund_percentage
  * @property numeric|null $maintance_tax_percentage
@@ -16,14 +16,14 @@
  * @property \Illuminate\Support\Carbon|null $updated_at
  * @property numeric|null $tbr_fixed_value_percentage
  * @property-read \App\Models\FranchiseeContract $contract
- * @property-read \App\Models\InhabitantClassification|null $inhabitantClassification
+ * @property-read \App\Models\MunicipalitySize|null $municipalitySize
  * @method static \Illuminate\Database\Eloquent\Builder<static>|FranchiseeContractTaxHistory newModelQuery()
  * @method static \Illuminate\Database\Eloquent\Builder<static>|FranchiseeContractTaxHistory newQuery()
  * @method static \Illuminate\Database\Eloquent\Builder<static>|FranchiseeContractTaxHistory query()
  * @method static \Illuminate\Database\Eloquent\Builder<static>|FranchiseeContractTaxHistory whereCreatedAt($value)
  * @method static \Illuminate\Database\Eloquent\Builder<static>|FranchiseeContractTaxHistory whereFranchiseeContractId($value)
  * @method static \Illuminate\Database\Eloquent\Builder<static>|FranchiseeContractTaxHistory whereId($value)
- * @method static \Illuminate\Database\Eloquent\Builder<static>|FranchiseeContractTaxHistory whereInhabitantClassificationId($value)
+ * @method static \Illuminate\Database\Eloquent\Builder<static>|FranchiseeContractTaxHistory whereMunicipalitySizeId($value)
  * @method static \Illuminate\Database\Eloquent\Builder<static>|FranchiseeContractTaxHistory whereMaintanceTaxPercentage($value)
  * @method static \Illuminate\Database\Eloquent\Builder<static>|FranchiseeContractTaxHistory whereMarketingFundPercentage($value)
  * @method static \Illuminate\Database\Eloquent\Builder<static>|FranchiseeContractTaxHistory whereTbrFixedValue($value)
@@ -49,8 +49,8 @@ public function contract(): BelongsTo
         return $this->belongsTo(FranchiseeContract::class, 'franchisee_contract_id');
     }
 
-    public function inhabitantClassification(): BelongsTo
+    public function municipalitySize(): BelongsTo
     {
-        return $this->belongsTo(InhabitantClassification::class, 'inhabitant_classification_id');
+        return $this->belongsTo(MunicipalitySize::class, 'municipality_size_id');
     }
 }

+ 16 - 16
app/Services/FranchiseeContractService.php

@@ -18,7 +18,7 @@ public function getActive(): Collection
     {
         $today = now()->toDateString();
 
-        return FranchiseeContract::with(['unit', 'inhabitantClassification'])
+        return FranchiseeContract::with(['unit', 'municipalitySize'])
             ->whereNotNull('start_date')
             ->whereNotNull('end_date')
             ->where('start_date', '<=', $today)
@@ -34,7 +34,7 @@ public function findById(int $id): ?FranchiseeContract
 
     public function getByUnitId(int $unitId): Collection
     {
-        return FranchiseeContract::with('inhabitantClassification')
+        return FranchiseeContract::with(['municipalitySize'])
             ->where('unit_id', $unitId)
             ->orderBy('created_at', 'desc')
             ->orderBy('id', 'desc')
@@ -62,7 +62,7 @@ public function update(int $id, array $data): ?FranchiseeContract
             return null;
         }
 
-        $taxFields = ['inhabitant_classification_id', 'tbr_fixed_value', 'tbr_fixed_value_percentage', 'marketing_fund_percentage', 'maintance_tax_percentage'];
+        $taxFields = ['municipality_size_id', 'tbr_fixed_value', 'tbr_fixed_value_percentage', 'marketing_fund_percentage', 'maintance_tax_percentage'];
         $hasTaxChange = collect($taxFields)->contains(fn($field) => array_key_exists($field, $data));
 
         if ($hasTaxChange) {
@@ -70,12 +70,12 @@ public function update(int $id, array $data): ?FranchiseeContract
 
             if (!$hasHistory) {
                 FranchiseeContractTaxHistory::create([
-                    'franchisee_contract_id'       => $model->id,
-                    'inhabitant_classification_id' => $model->inhabitant_classification_id,
-                    'tbr_fixed_value'              => $model->tbr_fixed_value,
-                    'tbr_fixed_value_percentage'   => $model->tbr_fixed_value_percentage,
-                    'marketing_fund_percentage'    => $model->marketing_fund_percentage,
-                    'maintance_tax_percentage'     => $model->maintance_tax_percentage,
+                    'franchisee_contract_id'     => $model->id,
+                    'municipality_size_id'       => $model->municipality_size_id,
+                    'tbr_fixed_value'            => $model->tbr_fixed_value,
+                    'tbr_fixed_value_percentage' => $model->tbr_fixed_value_percentage,
+                    'marketing_fund_percentage'  => $model->marketing_fund_percentage,
+                    'maintance_tax_percentage'   => $model->maintance_tax_percentage,
                 ]);
             }
         }
@@ -84,12 +84,12 @@ public function update(int $id, array $data): ?FranchiseeContract
 
         if ($hasTaxChange) {
             FranchiseeContractTaxHistory::create([
-                'franchisee_contract_id'       => $model->id,
-                'inhabitant_classification_id' => $model->inhabitant_classification_id,
-                'tbr_fixed_value'              => $model->tbr_fixed_value,
-                'tbr_fixed_value_percentage'   => $model->tbr_fixed_value_percentage,
-                'marketing_fund_percentage'    => $model->marketing_fund_percentage,
-                'maintance_tax_percentage'     => $model->maintance_tax_percentage,
+                'franchisee_contract_id'     => $model->id,
+                'municipality_size_id'       => $model->municipality_size_id,
+                'tbr_fixed_value'            => $model->tbr_fixed_value,
+                'tbr_fixed_value_percentage' => $model->tbr_fixed_value_percentage,
+                'marketing_fund_percentage'  => $model->marketing_fund_percentage,
+                'maintance_tax_percentage'   => $model->maintance_tax_percentage,
             ]);
         }
 
@@ -98,7 +98,7 @@ public function update(int $id, array $data): ?FranchiseeContract
 
     public function getTaxHistory(int $id): Collection
     {
-        return FranchiseeContractTaxHistory::with('inhabitantClassification')
+        return FranchiseeContractTaxHistory::with(['municipalitySize'])
             ->where('franchisee_contract_id', $id)
             ->orderBy('created_at', 'desc')
             ->orderBy('id', 'desc')

+ 7 - 4
database/migrations/2026_05_05_000001_add_fields_to_franchisee_contracts_table.php

@@ -12,10 +12,10 @@ public function up(): void
             $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')
+            $table->foreignId('municipality_size_id')
                 ->nullable()
                 ->after('invoice_due_date')
-                ->constrained('inhabitant_classifications')
+                ->constrained('municipality_sizes')
                 ->nullOnDelete();
 
             $table->integer('protocol')->nullable()->change();
@@ -23,18 +23,21 @@ public function up(): void
             $table->text('description')->nullable()->change();
             $table->integer('validity_months')->nullable()->change();
             $table->date('signature_date')->nullable()->change();
+
+            $table->index('municipality_size_id');
         });
     }
 
     public function down(): void
     {
         Schema::table('franchisee_contracts', function (Blueprint $table) {
-            $table->dropForeign(['inhabitant_classification_id']);
+            $table->dropForeign(['municipality_size_id']);
+            $table->dropIndex(['municipality_size_id']);
             $table->dropColumn([
                 'start_date',
                 'end_date',
                 'invoice_due_date',
-                'inhabitant_classification_id',
+                'municipality_size_id',
             ]);
 
             $table->integer('protocol')->nullable(false)->change();

+ 2 - 1
database/migrations/2026_05_05_000003_create_franchisee_contract_tax_histories_table.php

@@ -11,8 +11,9 @@ public function up(): void
         Schema::create('franchisee_contract_tax_histories', function (Blueprint $table) {
             $table->id();
             $table->foreignId('franchisee_contract_id')->constrained('franchisee_contracts')->cascadeOnDelete();
-            $table->foreignId('inhabitant_classification_id')->nullable()->constrained('inhabitant_classifications')->nullOnDelete();
+            $table->foreignId('municipality_size_id')->nullable()->constrained('municipality_sizes')->nullOnDelete();
             $table->decimal('tbr_fixed_value', 10, 2)->nullable();
+            $table->decimal('tbr_fixed_value_percentage', 10, 4)->nullable();
             $table->decimal('marketing_fund_percentage', 10, 4)->nullable();
             $table->decimal('maintance_tax_percentage', 10, 4)->nullable();
             $table->timestamps();

+ 0 - 22
database/migrations/2026_05_06_000001_add_royalties_to_franchisee_contract_tax_histories_table.php

@@ -1,22 +0,0 @@
-<?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_contract_tax_histories', function (Blueprint $table) {
-            $table->decimal('tbr_fixed_value_percentage', 10, 4)->nullable()->after('tbr_fixed_value');
-        });
-    }
-
-    public function down(): void
-    {
-        Schema::table('franchisee_contract_tax_histories', function (Blueprint $table) {
-            $table->dropColumn('tbr_fixed_value_percentage');
-        });
-    }
-};