fix: associar automaticamente transações ao centro de custo Geral
- 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
This commit is contained in:
parent
366254312c
commit
c787077a39
@ -217,7 +217,7 @@ public function updateKeywords(Request $request, int $id): JsonResponse
|
|||||||
$costCenter = CostCenter::where('user_id', Auth::id())->findOrFail($id);
|
$costCenter = CostCenter::where('user_id', Auth::id())->findOrFail($id);
|
||||||
|
|
||||||
$validated = $request->validate([
|
$validated = $request->validate([
|
||||||
'keywords' => 'required|array',
|
'keywords' => 'nullable|array',
|
||||||
'keywords.*' => 'string|max:100',
|
'keywords.*' => 'string|max:100',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|||||||
@ -124,6 +124,17 @@ public function store(Request $request): JsonResponse
|
|||||||
|
|
||||||
$validated['user_id'] = $request->user()->id;
|
$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
|
// Se status é completed e não tem amount, usa planned_amount
|
||||||
if (($validated['status'] ?? 'pending') === 'completed') {
|
if (($validated['status'] ?? 'pending') === 'completed') {
|
||||||
$validated['amount'] = $validated['amount'] ?? $validated['planned_amount'];
|
$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}";
|
$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();
|
DB::beginTransaction();
|
||||||
try {
|
try {
|
||||||
// Criar transação de DÉBITO na conta de origem
|
// Criar transação de DÉBITO na conta de origem
|
||||||
@ -792,6 +809,7 @@ public function transfer(Request $request): JsonResponse
|
|||||||
'status' => 'completed',
|
'status' => 'completed',
|
||||||
'notes' => $validated['notes'] ?? "Transferência para {$toAccount->name}",
|
'notes' => $validated['notes'] ?? "Transferência para {$toAccount->name}",
|
||||||
'reference' => 'TRANSFER',
|
'reference' => 'TRANSFER',
|
||||||
|
'cost_center_id' => $costCenterId,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Criar transação de CRÉDITO na conta de destino
|
// Criar transação de CRÉDITO na conta de destino
|
||||||
@ -807,6 +825,7 @@ public function transfer(Request $request): JsonResponse
|
|||||||
'status' => 'completed',
|
'status' => 'completed',
|
||||||
'notes' => $validated['notes'] ?? "Transferência de {$fromAccount->name}",
|
'notes' => $validated['notes'] ?? "Transferência de {$fromAccount->name}",
|
||||||
'reference' => 'TRANSFER',
|
'reference' => 'TRANSFER',
|
||||||
|
'cost_center_id' => $costCenterId,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Vincular as duas transações
|
// Vincular as duas transações
|
||||||
|
|||||||
@ -253,6 +253,14 @@ public function importTransactions(
|
|||||||
$categoryId = $categoryId ?? $mapping->default_category_id;
|
$categoryId = $categoryId ?? $mapping->default_category_id;
|
||||||
$costCenterId = $costCenterId ?? $mapping->default_cost_center_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();
|
DB::beginTransaction();
|
||||||
|
|
||||||
foreach ($parsed['data'] as $rowIndex => $row) {
|
foreach ($parsed['data'] as $rowIndex => $row) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user