فهرست منبع

feat(treasury): scope account listing by unit

getAll filters whereNull(unit_id) by default (network) or by the given
unit_id; create normalizes unit_id; index forwards the unit_id query param.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
ebagabee 1 ماه پیش
والد
کامیت
586c4434e6
2فایلهای تغییر یافته به همراه20 افزوده شده و 4 حذف شده
  1. 4 2
      app/Http/Controllers/TreasuryAccountController.php
  2. 16 2
      app/Services/TreasuryAccountService.php

+ 4 - 2
app/Http/Controllers/TreasuryAccountController.php

@@ -6,6 +6,7 @@
 use App\Http\Requests\TreasuryAccountRequest;
 use App\Http\Requests\TreasuryAccountRequest;
 use App\Http\Resources\TreasuryAccountResource;
 use App\Http\Resources\TreasuryAccountResource;
 use Illuminate\Http\JsonResponse;
 use Illuminate\Http\JsonResponse;
+use Illuminate\Http\Request;
 
 
 class TreasuryAccountController extends Controller
 class TreasuryAccountController extends Controller
 {
 {
@@ -13,9 +14,10 @@ public function __construct(
         protected TreasuryAccountService $service,
         protected TreasuryAccountService $service,
     ) {}
     ) {}
 
 
-    public function index(): JsonResponse
+    public function index(Request $request): JsonResponse
     {
     {
-        $items = $this->service->getAll();
+        $unitId = $request->filled('unit_id') ? (int) $request->input('unit_id') : null;
+        $items = $this->service->getAll($unitId);
         return $this->successResponse(payload: TreasuryAccountResource::collection($items));
         return $this->successResponse(payload: TreasuryAccountResource::collection($items));
     }
     }
 
 

+ 16 - 2
app/Services/TreasuryAccountService.php

@@ -7,9 +7,20 @@
 
 
 class TreasuryAccountService
 class TreasuryAccountService
 {
 {
-    public function getAll(): Collection
+    /**
+     * Lista as contas de tesouraria por escopo:
+     * - franqueador (rede/conta-mãe): sem unit_id => contas com unit_id null;
+     * - franchisee: unit_id da unidade ativa => contas daquela unidade.
+     */
+    public function getAll(?int $unitId = null): Collection
     {
     {
-        return TreasuryAccount::orderBy('created_at', 'desc')
+        return TreasuryAccount::query()
+            ->when(
+                $unitId !== null,
+                fn ($query) => $query->where('unit_id', $unitId),
+                fn ($query) => $query->whereNull('unit_id'),
+            )
+            ->orderBy('created_at', 'desc')
             ->get();
             ->get();
     }
     }
 
 
@@ -20,6 +31,9 @@ public function findById(int $id): ?TreasuryAccount
 
 
     public function create(array $data): TreasuryAccount
     public function create(array $data): TreasuryAccount
     {
     {
+        // Sem unit_id => conta da rede (franqueador). Com unit_id => conta da unidade (franchisee).
+        $data['unit_id'] = $data['unit_id'] ?? null;
+
         return TreasuryAccount::create($data);
         return TreasuryAccount::create($data);
     }
     }