Răsfoiți Sursa

feat(treasury): map manual entrada/saida and scope listing

Service translates account_id+type into from/to, defaults user/origin/date; index filters the statement by account_id.
ebagabee 1 lună în urmă
părinte
comite
c30e843144

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

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

+ 47 - 5
app/Services/TreasuryLaunchService.php

@@ -2,14 +2,26 @@
 
 namespace App\Services;
 
+use App\Models\TreasuryAccount;
 use App\Models\TreasuryLaunch;
 use Illuminate\Database\Eloquent\Collection;
+use Illuminate\Support\Facades\Auth;
 
 class TreasuryLaunchService
 {
-    public function getAll(): Collection
+    /**
+     * Extrato de um banco: lançamentos onde ele é origem ou destino.
+     * Sem account_id retorna o extrato consolidado (todos os lançamentos).
+     */
+    public function getAll(?int $accountId = null): Collection
     {
-        return TreasuryLaunch::orderBy('created_at', 'desc')
+        return TreasuryLaunch::query()
+            ->when($accountId !== null, fn ($query) => $query->where(function ($q) use ($accountId) {
+                $q->where('from_account_id', $accountId)
+                    ->orWhere('to_account_id', $accountId);
+            }))
+            ->orderBy('launch_date', 'desc')
+            ->orderBy('created_at', 'desc')
             ->get();
     }
 
@@ -20,7 +32,7 @@ public function findById(int $id): ?TreasuryLaunch
 
     public function create(array $data): TreasuryLaunch
     {
-        return TreasuryLaunch::create($data);
+        return TreasuryLaunch::create($this->normalize($data));
     }
 
     public function update(int $id, array $data): ?TreasuryLaunch
@@ -31,7 +43,7 @@ public function update(int $id, array $data): ?TreasuryLaunch
             return null;
         }
 
-        $model->update($data);
+        $model->update($this->normalize($data, isCreate: false));
         return $model->fresh();
     }
 
@@ -46,5 +58,35 @@ public function delete(int $id): bool
         return $model->delete();
     }
 
-    // Add custom business logic methods here
+    /**
+     * Traduz a movimentação manual (account_id + transaction_type) para os campos
+     * do lançamento: entrada credita o banco (to_account), saída debita (from_account).
+     */
+    private function normalize(array $data, bool $isCreate = true): array
+    {
+        $accountId = $data['account_id'] ?? null;
+        unset($data['account_id']);
+
+        if ($accountId !== null && isset($data['transaction_type'])) {
+            $account = TreasuryAccount::find($accountId);
+            $data['unit_id'] = $account?->unit_id;
+
+            if ($data['transaction_type'] === 'entrada') {
+                $data['to_account_id'] = $accountId;
+                $data['from_account_id'] = null;
+            } else {
+                $data['from_account_id'] = $accountId;
+                $data['to_account_id'] = null;
+            }
+        }
+
+        if ($isCreate) {
+            $data['user_id'] = Auth::id();
+            $data['origin'] = $data['origin'] ?? 'manual';
+            $data['launch_date'] = $data['launch_date'] ?? now()->toDateString();
+            $data['is_reconciled'] = false;
+        }
+
+        return $data;
+    }
 }