From c787077a393827d3c7f594ff041ee01c85d3813a Mon Sep 17 00:00:00 2001 From: marco Date: Fri, 19 Dec 2025 16:26:27 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20associar=20automaticamente=20transa?= =?UTF-8?q?=C3=A7=C3=B5es=20ao=20centro=20de=20custo=20Geral?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Corrigir validação de keywords permitindo array vazio - Atualizar todas transações existentes sem centro de custo para Geral - Criar centro de custo Geral para usuários sem ele - Associar automaticamente novas transações criadas ao Geral - Associar automaticamente transferências ao Geral - Associar automaticamente transações importadas ao Geral --- .../Controllers/Api/CostCenterController.php | 2 +- .../Controllers/Api/TransactionController.php | 19 +++++++++++++++++++ backend/app/Services/Import/ImportService.php | 8 ++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/backend/app/Http/Controllers/Api/CostCenterController.php b/backend/app/Http/Controllers/Api/CostCenterController.php index 48b0a4d..2cbc717 100755 --- a/backend/app/Http/Controllers/Api/CostCenterController.php +++ b/backend/app/Http/Controllers/Api/CostCenterController.php @@ -217,7 +217,7 @@ public function updateKeywords(Request $request, int $id): JsonResponse $costCenter = CostCenter::where('user_id', Auth::id())->findOrFail($id); $validated = $request->validate([ - 'keywords' => 'required|array', + 'keywords' => 'nullable|array', 'keywords.*' => 'string|max:100', ]); diff --git a/backend/app/Http/Controllers/Api/TransactionController.php b/backend/app/Http/Controllers/Api/TransactionController.php index 9d714b2..30a6549 100755 --- a/backend/app/Http/Controllers/Api/TransactionController.php +++ b/backend/app/Http/Controllers/Api/TransactionController.php @@ -124,6 +124,17 @@ public function store(Request $request): JsonResponse $validated['user_id'] = $request->user()->id; + // Se não foi especificado um centro de custo, usar o Geral (sistema) + if (!isset($validated['cost_center_id']) || $validated['cost_center_id'] === null) { + $generalCostCenter = \App\Models\CostCenter::where('user_id', $request->user()->id) + ->where('is_system', true) + ->first(); + + if ($generalCostCenter) { + $validated['cost_center_id'] = $generalCostCenter->id; + } + } + // Se status é completed e não tem amount, usa planned_amount if (($validated['status'] ?? 'pending') === 'completed') { $validated['amount'] = $validated['amount'] ?? $validated['planned_amount']; @@ -777,6 +788,12 @@ public function transfer(Request $request): JsonResponse $description = $validated['description'] ?? "Transferência: {$fromAccount->name} → {$toAccount->name}"; + // Obter centro de custo Geral do usuário + $generalCostCenter = \App\Models\CostCenter::where('user_id', $userId) + ->where('is_system', true) + ->first(); + $costCenterId = $generalCostCenter ? $generalCostCenter->id : null; + DB::beginTransaction(); try { // Criar transação de DÉBITO na conta de origem @@ -792,6 +809,7 @@ public function transfer(Request $request): JsonResponse 'status' => 'completed', 'notes' => $validated['notes'] ?? "Transferência para {$toAccount->name}", 'reference' => 'TRANSFER', + 'cost_center_id' => $costCenterId, ]); // Criar transação de CRÉDITO na conta de destino @@ -807,6 +825,7 @@ public function transfer(Request $request): JsonResponse 'status' => 'completed', 'notes' => $validated['notes'] ?? "Transferência de {$fromAccount->name}", 'reference' => 'TRANSFER', + 'cost_center_id' => $costCenterId, ]); // Vincular as duas transações diff --git a/backend/app/Services/Import/ImportService.php b/backend/app/Services/Import/ImportService.php index e5d8729..4b85f6d 100755 --- a/backend/app/Services/Import/ImportService.php +++ b/backend/app/Services/Import/ImportService.php @@ -253,6 +253,14 @@ public function importTransactions( $categoryId = $categoryId ?? $mapping->default_category_id; $costCenterId = $costCenterId ?? $mapping->default_cost_center_id; + // Se não há centro de custo definido, usar o Geral (sistema) + if (!$costCenterId) { + $generalCostCenter = \App\Models\CostCenter::where('user_id', $userId) + ->where('is_system', true) + ->first(); + $costCenterId = $generalCostCenter ? $generalCostCenter->id : null; + } + DB::beginTransaction(); foreach ($parsed['data'] as $rowIndex => $row) {