From 02492260268e055865cc1b29eebaae809da52d2f Mon Sep 17 00:00:00 2001 From: Felipe Coutinho Date: Thu, 5 Feb 2026 12:52:50 +0000 Subject: [PATCH] =?UTF-8?q?fix(orcamentos):=20alinhar=20c=C3=A1lculo=20de?= =?UTF-8?q?=20gastos=20com=20widget=20do=20dashboard?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A query de "Gasto até agora" nos orçamentos agora aplica os mesmos filtros do widget de despesas por categoria do dashboard: - INNER JOIN com pagadores (exclui lançamentos sem pagador) - Filtra apenas pagadores com role "admin" (exclui terceiros) - Exclui notas de faturas automáticas (AUTO_FATURA:*) Isso corrige a discrepância onde orçamentos mostravam valores diferentes do widget de despesas para a mesma categoria. Co-Authored-By: Claude Opus 4.5 --- app/(dashboard)/orcamentos/data.ts | 11 ++++++++++- components/orcamentos/budget-card.tsx | 4 ++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/app/(dashboard)/orcamentos/data.ts b/app/(dashboard)/orcamentos/data.ts index 8e336d1..7ae68c4 100644 --- a/app/(dashboard)/orcamentos/data.ts +++ b/app/(dashboard)/orcamentos/data.ts @@ -1,11 +1,14 @@ -import { and, asc, eq, inArray, sum } from "drizzle-orm"; +import { and, asc, eq, inArray, isNull, or, sql, sum } from "drizzle-orm"; import { categorias, lancamentos, type Orcamento, orcamentos, + pagadores, } from "@/db/schema"; +import { ACCOUNT_AUTO_INVOICE_NOTE_PREFIX } from "@/lib/accounts/constants"; import { db } from "@/lib/db"; +import { PAGADOR_ROLE_ADMIN } from "@/lib/pagadores/constants"; const toNumber = (value: string | number | null | undefined) => { if (typeof value === "number") return value; @@ -76,12 +79,18 @@ export async function fetchBudgetsForUser( totalAmount: sum(lancamentos.amount).as("totalAmount"), }) .from(lancamentos) + .innerJoin(pagadores, eq(lancamentos.pagadorId, pagadores.id)) .where( and( eq(lancamentos.userId, userId), eq(lancamentos.period, selectedPeriod), eq(lancamentos.transactionType, "Despesa"), + eq(pagadores.role, PAGADOR_ROLE_ADMIN), inArray(lancamentos.categoriaId, categoryIds), + or( + isNull(lancamentos.note), + sql`${lancamentos.note} NOT LIKE ${`${ACCOUNT_AUTO_INVOICE_NOTE_PREFIX}%`}`, + ), ), ) .groupBy(lancamentos.categoriaId); diff --git a/components/orcamentos/budget-card.tsx b/components/orcamentos/budget-card.tsx index 0f880e5..94e3c1e 100644 --- a/components/orcamentos/budget-card.tsx +++ b/components/orcamentos/budget-card.tsx @@ -76,13 +76,13 @@ export function BudgetCard({ -
+
{exceeded ? (
Excedeu em
) : ( -
+
Restam {" "} disponíveis.