فهرست منبع

feat: adiciona historico de contrato

ebagabee 1 ماه پیش
والد
کامیت
4823f7cc17

+ 7 - 0
app/Http/Controllers/FranchiseeContractController.php

@@ -5,6 +5,7 @@
 use App\Services\FranchiseeContractService;
 use App\Http\Requests\FranchiseeContractRequest;
 use App\Http\Resources\FranchiseeContractResource;
+use App\Http\Resources\FranchiseeContractTaxHistoryResource;
 use Illuminate\Http\JsonResponse;
 use Illuminate\Http\Request;
 
@@ -52,4 +53,10 @@ public function destroy(int $id): JsonResponse
         $this->service->delete($id);
         return $this->successResponse(message: __('messages.deleted'), code: 204);
     }
+
+    public function taxHistory(int $id): JsonResponse
+    {
+        $items = $this->service->getTaxHistory($id);
+        return $this->successResponse(payload: FranchiseeContractTaxHistoryResource::collection($items));
+    }
 }

+ 8 - 5
app/Http/Requests/FranchiseeContractRequest.php

@@ -9,10 +9,10 @@ class FranchiseeContractRequest extends FormRequest
     public function rules(): array
     {
         $rules = [
-            'start_date'                   => 'required|date',
-            'end_date'                     => 'required|date|after:start_date',
-            'tbr_fixed_value'              => 'required|numeric|min:0',
-            'invoice_due_date'             => 'nullable|date',
+            'start_date'                   => 'nullable|date',
+            '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',
             'tbr_fixed_value_percentage'   => 'nullable|numeric|between:0,1',
             'marketing_fund_percentage'    => 'nullable|numeric|between:0,1',
@@ -20,7 +20,10 @@ public function rules(): array
         ];
 
         if ($this->isMethod('POST')) {
-            $rules['unit_id'] = 'required|integer|exists:units,id';
+            $rules['unit_id']        = 'required|integer|exists:units,id';
+            $rules['start_date']     = 'required|date';
+            $rules['end_date']       = 'required|date|after:start_date';
+            $rules['tbr_fixed_value'] = 'required|numeric|min:0';
         }
 
         return $rules;

+ 1 - 1
app/Http/Resources/FranchiseeContractResource.php

@@ -28,7 +28,7 @@ public function toArray(Request $request): array
             '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'),
+            'invoice_due_date'             => $this->invoice_due_date,
             'tbr_fixed_value'              => $this->tbr_fixed_value,
             'tbr_fixed_value_percentage'   => $this->tbr_fixed_value_percentage,
             'marketing_fund_percentage'    => $this->marketing_fund_percentage,

+ 22 - 0
app/Http/Resources/FranchiseeContractTaxHistoryResource.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace App\Http\Resources;
+
+use Illuminate\Http\Request;
+use Illuminate\Http\Resources\Json\JsonResource;
+
+class FranchiseeContractTaxHistoryResource extends JsonResource
+{
+    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),
+            'tbr_fixed_value'              => $this->tbr_fixed_value,
+            'marketing_fund_percentage'    => $this->marketing_fund_percentage,
+            'maintance_tax_percentage'     => $this->maintance_tax_percentage,
+            'created_at'                   => $this->created_at?->format('Y-m-d H:i:s'),
+        ];
+    }
+}

+ 6 - 6
app/Models/FranchiseeContract.php

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

+ 29 - 0
app/Models/FranchiseeContractTaxHistory.php

@@ -0,0 +1,29 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
+
+class FranchiseeContractTaxHistory extends Model
+{
+    protected $table = 'franchisee_contract_tax_histories';
+
+    protected $guarded = ['id'];
+
+    protected $casts = [
+        'tbr_fixed_value'           => 'decimal:2',
+        'marketing_fund_percentage' => 'decimal:4',
+        'maintance_tax_percentage'  => 'decimal:4',
+    ];
+
+    public function contract(): BelongsTo
+    {
+        return $this->belongsTo(FranchiseeContract::class, 'franchisee_contract_id');
+    }
+
+    public function inhabitantClassification(): BelongsTo
+    {
+        return $this->belongsTo(InhabitantClassification::class, 'inhabitant_classification_id');
+    }
+}

+ 23 - 0
app/Services/FranchiseeContractService.php

@@ -3,6 +3,7 @@
 namespace App\Services;
 
 use App\Models\FranchiseeContract;
+use App\Models\FranchiseeContractTaxHistory;
 use Illuminate\Database\Eloquent\Collection;
 
 class FranchiseeContractService
@@ -59,10 +60,32 @@ public function update(int $id, array $data): ?FranchiseeContract
             return null;
         }
 
+        $taxFields = ['inhabitant_classification_id', 'tbr_fixed_value', 'marketing_fund_percentage', 'maintance_tax_percentage'];
+        $hasTaxChange = collect($taxFields)->contains(fn($field) => array_key_exists($field, $data));
+
         $model->update($data);
+
+        if ($hasTaxChange) {
+            FranchiseeContractTaxHistory::create([
+                'franchisee_contract_id'       => $model->id,
+                'inhabitant_classification_id' => $model->inhabitant_classification_id,
+                'tbr_fixed_value'              => $model->tbr_fixed_value,
+                'marketing_fund_percentage'    => $model->marketing_fund_percentage,
+                'maintance_tax_percentage'     => $model->maintance_tax_percentage,
+            ]);
+        }
+
         return $model->fresh();
     }
 
+    public function getTaxHistory(int $id): Collection
+    {
+        return FranchiseeContractTaxHistory::with('inhabitantClassification')
+            ->where('franchisee_contract_id', $id)
+            ->orderBy('created_at', 'desc')
+            ->get();
+    }
+
     public function delete(int $id): bool
     {
         $model = $this->findById($id);

+ 19 - 0
database/migrations/2026_05_05_000002_change_invoice_due_date_in_franchisee_contracts_table.php

@@ -0,0 +1,19 @@
+<?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
+    {
+        DB::statement('ALTER TABLE franchisee_contracts ALTER COLUMN invoice_due_date TYPE smallint USING EXTRACT(DAY FROM invoice_due_date)::smallint');
+    }
+
+    public function down(): void
+    {
+        DB::statement('ALTER TABLE franchisee_contracts ALTER COLUMN invoice_due_date TYPE date USING NULL');
+    }
+};

+ 28 - 0
database/migrations/2026_05_05_000003_create_franchisee_contract_tax_histories_table.php

@@ -0,0 +1,28 @@
+<?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::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->decimal('tbr_fixed_value', 10, 2)->nullable();
+            $table->decimal('marketing_fund_percentage', 10, 4)->nullable();
+            $table->decimal('maintance_tax_percentage', 10, 4)->nullable();
+            $table->timestamps();
+
+            $table->index('franchisee_contract_id');
+        });
+    }
+
+    public function down(): void
+    {
+        Schema::dropIfExists('franchisee_contract_tax_histories');
+    }
+};

+ 2 - 0
routes/authRoutes/franchisee_contract.php

@@ -12,6 +12,8 @@
 
     Route::get('/{id}', 'show');
 
+    Route::get('/{id}/tax-history', 'taxHistory');
+
     Route::put('/{id}', 'update');
 
     Route::delete('/{id}', 'destroy');