Quellcode durchsuchen

feat: adiciona tab de contratos em tbr

ebagabee vor 1 Monat
Ursprung
Commit
289e299545

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

@@ -14,6 +14,12 @@ public function __construct(
         protected FranchiseeContractService $service,
     ) {}
 
+    public function active(): JsonResponse
+    {
+        $items = $this->service->getActive();
+        return $this->successResponse(payload: FranchiseeContractResource::collection($items));
+    }
+
     public function index(Request $request): JsonResponse
     {
         $items = $request->has('unit_id')

+ 6 - 0
app/Http/Resources/FranchiseeContractResource.php

@@ -17,9 +17,14 @@ class FranchiseeContractResource extends JsonResource
      */
     public function toArray(Request $request): array
     {
+        $contractMonthCurrent = $this->start_date
+            ? (int) $this->start_date->diffInMonths(now()) + 1
+            : null;
+
         return [
             'id'                           => $this->id,
             'unit_id'                      => $this->unit_id,
+            'unit_name'                    => $this->whenLoaded('unit', fn() => $this->unit->fantasy_name),
             'protocol'                     => $this->protocol,
             'start_date'                   => $this->start_date?->format('Y-m-d'),
             'end_date'                     => $this->end_date?->format('Y-m-d'),
@@ -30,6 +35,7 @@ public function toArray(Request $request): array
             'maintance_tax_percentage'     => $this->maintance_tax_percentage,
             'inhabitant_classification_id' => $this->inhabitant_classification_id,
             'validity_months'              => $this->validity_months,
+            'contract_month_current'       => $contractMonthCurrent,
             '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'),
         ];

+ 13 - 0
app/Services/FranchiseeContractService.php

@@ -13,6 +13,19 @@ public function getAll(): Collection
             ->get();
     }
 
+    public function getActive(): Collection
+    {
+        $today = now()->toDateString();
+
+        return FranchiseeContract::with('unit')
+            ->whereNotNull('start_date')
+            ->whereNotNull('end_date')
+            ->where('start_date', '<=', $today)
+            ->where('end_date', '>=', $today)
+            ->orderBy('start_date')
+            ->get();
+    }
+
     public function findById(int $id): ?FranchiseeContract
     {
         return FranchiseeContract::find($id);

+ 2 - 0
routes/authRoutes/franchisee_contract.php

@@ -4,6 +4,8 @@
 use App\Http\Controllers\FranchiseeContractController;
 
 Route::controller(FranchiseeContractController::class)->prefix('franchisee-contract')->group(function () {
+    Route::get('/active', 'active');
+
     Route::get('/', 'index');
 
     Route::post('/', 'store');