Эх сурвалжийг харах

feat: cria seeds, adiciona preview

ebagabee 1 сар өмнө
parent
commit
042277942e

+ 19 - 0
app/Http/Controllers/TbrBillingPreviewController.php

@@ -0,0 +1,19 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Services\TbrBillingPreviewService;
+use Illuminate\Http\JsonResponse;
+
+class TbrBillingPreviewController extends Controller
+{
+    public function __construct(
+        protected TbrBillingPreviewService $service,
+    ) {}
+
+    public function index(): JsonResponse
+    {
+        $items = $this->service->getAll();
+        return $this->successResponse(payload: $items);
+    }
+}

+ 59 - 0
app/Services/TbrBillingPreviewService.php

@@ -0,0 +1,59 @@
+<?php
+
+namespace App\Services;
+
+use App\Models\FranchiseeContract;
+use Illuminate\Support\Collection;
+
+class TbrBillingPreviewService
+{
+    private const MAINTENANCE_RATE = 0.30;
+    private const EXEMPT_MONTHS    = 3;
+
+    public function getAll(): Collection
+    {
+        $latestContracts = FranchiseeContract::with('unit')
+            ->whereNotNull('tbr_fixed_value')
+            ->whereNotNull('unit_id')
+            ->orderByDesc('created_at')
+            ->get()
+            ->unique('unit_id')
+            ->values();
+
+        return $latestContracts->map(fn ($contract) => $this->buildPreview($contract));
+    }
+
+    private function buildPreview(FranchiseeContract $contract): array
+    {
+        $tbrValue      = (float) $contract->tbr_fixed_value;
+        $contractMonth = $this->resolveContractMonth($contract);
+        $isExempt      = $contractMonth <= self::EXEMPT_MONTHS;
+
+        $royaltiesValue    = $isExempt ? 0.0 : round($tbrValue * (float) $contract->tbr_fixed_value_percentage, 2);
+        $fnmValue          = $isExempt ? 0.0 : round($tbrValue * (float) $contract->marketing_fund_percentage, 2);
+        $maintenanceValue  = round($tbrValue * self::MAINTENANCE_RATE, 2);
+
+        return [
+            'id'               => $contract->unit_id,
+            'unit_name'        => $contract->unit?->fantasy_name,
+            'tbr_value'        => $tbrValue,
+            'royalties_value'  => $royaltiesValue,
+            'royalties_rule'   => 'Fixo TBR',
+            'fnm_value'        => $fnmValue,
+            'fnm_rule'         => 'Fixo TBR',
+            'maintenance_value' => $maintenanceValue,
+            'total'            => round($royaltiesValue + $fnmValue + $maintenanceValue, 2),
+        ];
+    }
+
+    private function resolveContractMonth(FranchiseeContract $contract): int
+    {
+        $startDate = $contract->start_date ?? $contract->signature_date;
+
+        if (!$startDate) {
+            return 1;
+        }
+
+        return (int) $startDate->startOfMonth()->diffInMonths(now()->startOfMonth()) + 1;
+    }
+}

+ 3 - 0
database/seeders/DatabaseSeeder.php

@@ -19,6 +19,9 @@ public function run(): void
             ClassPackageSeeder::class,
             ProductSeeder::class,
             InhabitantClassificationSeeder::class,
+            RoyaltiesBaseBracketSeeder::class,
+            FnmBaseBracketSeeder::class,
+            MaintenanceBaseBracketSeeder::class,
         ]);
     }
 }

+ 40 - 0
database/seeders/FnmBaseBracketSeeder.php

@@ -0,0 +1,40 @@
+<?php
+
+namespace Database\Seeders;
+
+use App\Models\FnmBaseBracket;
+use Illuminate\Database\Seeder;
+
+class FnmBaseBracketSeeder extends Seeder
+{
+    public function run(): void
+    {
+        $brackets = [
+            [
+                'description' => 'Faixa 1 — Meses 1 a 3 (Isento)',
+                'start_month' => 1,
+                'end_month'   => 3,
+                'percentage'  => 0.0000,
+            ],
+            [
+                'description' => 'Faixa 2 — Meses 4 a 12',
+                'start_month' => 4,
+                'end_month'   => 12,
+                'percentage'  => 0.5000,
+            ],
+            [
+                'description' => 'Faixa 3 — Meses 13 em diante',
+                'start_month' => 13,
+                'end_month'   => null,
+                'percentage'  => 0.5000,
+            ],
+        ];
+
+        foreach ($brackets as $bracket) {
+            FnmBaseBracket::firstOrCreate(
+                ['start_month' => $bracket['start_month'], 'end_month' => $bracket['end_month']],
+                $bracket
+            );
+        }
+    }
+}

+ 22 - 0
database/seeders/MaintenanceBaseBracketSeeder.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace Database\Seeders;
+
+use App\Models\MaintenanceBaseBracket;
+use Illuminate\Database\Seeder;
+
+class MaintenanceBaseBracketSeeder extends Seeder
+{
+    public function run(): void
+    {
+        MaintenanceBaseBracket::firstOrCreate(
+            ['start_month' => 1, 'end_month' => null],
+            [
+                'description' => 'Faixa Única — 30% fixo da TBR (todos os meses)',
+                'start_month' => 1,
+                'end_month'   => null,
+                'percentage'  => 0.3000,
+            ]
+        );
+    }
+}

+ 40 - 0
database/seeders/RoyaltiesBaseBracketSeeder.php

@@ -0,0 +1,40 @@
+<?php
+
+namespace Database\Seeders;
+
+use App\Models\RoyaltiesBaseBracket;
+use Illuminate\Database\Seeder;
+
+class RoyaltiesBaseBracketSeeder extends Seeder
+{
+    public function run(): void
+    {
+        $brackets = [
+            [
+                'description' => 'Faixa 1 — Meses 1 a 3 (Isento)',
+                'start_month' => 1,
+                'end_month'   => 3,
+                'percentage'  => 0.0000,
+            ],
+            [
+                'description' => 'Faixa 2 — Meses 4 a 12',
+                'start_month' => 4,
+                'end_month'   => 12,
+                'percentage'  => 0.5000,
+            ],
+            [
+                'description' => 'Faixa 3 — Meses 13 em diante',
+                'start_month' => 13,
+                'end_month'   => null,
+                'percentage'  => 0.5000,
+            ],
+        ];
+
+        foreach ($brackets as $bracket) {
+            RoyaltiesBaseBracket::firstOrCreate(
+                ['start_month' => $bracket['start_month'], 'end_month' => $bracket['end_month']],
+                $bracket
+            );
+        }
+    }
+}

+ 8 - 0
routes/authRoutes/tbr_billing_preview.php

@@ -0,0 +1,8 @@
+<?php
+
+use Illuminate\Support\Facades\Route;
+use App\Http\Controllers\TbrBillingPreviewController;
+
+Route::controller(TbrBillingPreviewController::class)->prefix('tbr-billing-preview')->group(function () {
+    Route::get('/', 'index')->middleware('permission:tbr,view');
+});