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

feat(chart-of-accounts): scope listing by unit and order by code

getAll scopes to unit_id (null = matriz) and orders by code; create defaults unit_id null and order = code; index forwards unit_id.
ebagabee 1 месяц назад
Родитель
Сommit
1996ff1565

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

@@ -6,6 +6,7 @@
 use App\Http\Requests\FinancialPlanAccountRequest;
 use App\Http\Resources\FinancialPlanAccountResource;
 use Illuminate\Http\JsonResponse;
+use Illuminate\Http\Request;
 
 class FinancialPlanAccountController extends Controller
 {
@@ -13,9 +14,10 @@ public function __construct(
         protected FinancialPlanAccountService $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: FinancialPlanAccountResource::collection($items));
     }
 

+ 15 - 2
app/Services/FinancialPlanAccountService.php

@@ -7,9 +7,19 @@
 
 class FinancialPlanAccountService
 {
-    public function getAll(): Collection
+    /**
+     * Plano de contas por escopo: franqueador (matriz) usa unit_id null;
+     * franchisee usa o unit_id da unidade. Ordenado por código (1.1, 1.1.1, ...).
+     */
+    public function getAll(?int $unitId = null): Collection
     {
-        return FinancialPlanAccount::orderBy('created_at', 'desc')
+        return FinancialPlanAccount::query()
+            ->when(
+                $unitId !== null,
+                fn ($query) => $query->where('unit_id', $unitId),
+                fn ($query) => $query->whereNull('unit_id'),
+            )
+            ->orderBy('code')
             ->get();
     }
 
@@ -20,6 +30,9 @@ public function findById(int $id): ?FinancialPlanAccount
 
     public function create(array $data): FinancialPlanAccount
     {
+        $data['unit_id'] = $data['unit_id'] ?? null;
+        $data['order'] = $data['order'] ?? $data['code'];
+
         return FinancialPlanAccount::create($data);
     }