feat: mapeamento automatico de categoria na importacao de planilhas

This commit is contained in:
Yuri Argolo
2026-06-13 15:54:27 -03:00
committed by Felipe Coutinho
parent 2fd6e3c323
commit 558197e870
3 changed files with 19 additions and 6 deletions

View File

@@ -108,9 +108,18 @@ export function ImportPage({
setRows( setRows(
stmt.transactions.map((t) => { stmt.transactions.map((t) => {
const mappedCategoryId = let mappedCategoryId =
categoryMappings[normalizeDescriptionKey(t.description)] ?? null; categoryMappings[normalizeDescriptionKey(t.description)] ?? null;
if (t.categoryRaw) {
const matchedOption = categoryOptions.find(
(opt) => opt.label.toLowerCase() === t.categoryRaw?.toLowerCase()
);
if (matchedOption) {
mappedCategoryId = matchedOption.value;
}
}
return { return {
...t, ...t,
isDuplicate: t.externalId ? duplicates.has(t.externalId) : false, isDuplicate: t.externalId ? duplicates.has(t.externalId) : false,
@@ -129,7 +138,7 @@ export function ImportPage({
setIsChecking(false); setIsChecking(false);
} }
}, },
[isCategoryCompatible, payerId], [isCategoryCompatible, payerId, categoryOptions],
); );
// Pré-seleciona cartão ou conta com base no tipo detectado no OFX // Pré-seleciona cartão ou conta com base no tipo detectado no OFX

View File

@@ -4,6 +4,7 @@ export type ImportedTransaction = {
amount: number; // positivo = receita, negativo = despesa amount: number; // positivo = receita, negativo = despesa
description: string; // MEMO ou NAME description: string; // MEMO ou NAME
transactionType: "income" | "expense"; transactionType: "income" | "expense";
categoryRaw?: string | null;
}; };
export type ImportStatement = { export type ImportStatement = {

View File

@@ -99,6 +99,7 @@ export async function parseXls(buffer: ArrayBuffer): Promise<ImportStatement> {
const typeRaw = const typeRaw =
values[4] != null ? String(values[4]).toLowerCase().trim() : ""; values[4] != null ? String(values[4]).toLowerCase().trim() : "";
const transactionType = typeRaw === "receita" ? "income" : "expense"; const transactionType = typeRaw === "receita" ? "income" : "expense";
const categoryRaw = values[5] != null ? String(values[5]).trim() : null;
if (!date || !description || amount === null || amount <= 0) return; if (!date || !description || amount === null || amount <= 0) return;
@@ -108,6 +109,7 @@ export async function parseXls(buffer: ArrayBuffer): Promise<ImportStatement> {
amount, amount,
description, description,
transactionType, transactionType,
categoryRaw,
}); });
}); });
@@ -132,16 +134,17 @@ export async function generateXlsTemplate(): Promise<ArrayBuffer> {
const ws = workbook.addWorksheet("Lançamentos"); const ws = workbook.addWorksheet("Lançamentos");
ws.addRows([ ws.addRows([
["Data", "Descrição", "Valor", "Tipo"], ["Data", "Descrição", "Valor", "Tipo", "Categoria"],
["01/03/2026", "Ingressos São Januário", 160, "despesa"], ["01/03/2026", "Ingressos São Januário", 160, "despesa", "Lazer"],
["01/03/2026", "Salário", 3000.0, "receita"], ["01/03/2026", "Salário", 3000.0, "receita", "Salário"],
["01/03/2026", "Posto do Vasco da Gama", 89.9, "despesa"], ["01/03/2026", "Posto do Vasco da Gama", 89.9, "despesa", "Transporte"],
]); ]);
ws.getColumn(1).width = 14; ws.getColumn(1).width = 14;
ws.getColumn(2).width = 32; ws.getColumn(2).width = 32;
ws.getColumn(3).width = 12; ws.getColumn(3).width = 12;
ws.getColumn(4).width = 10; ws.getColumn(4).width = 10;
ws.getColumn(5).width = 24;
// Dropdown para coluna Tipo (D2:D100) // Dropdown para coluna Tipo (D2:D100)
for (let i = 2; i <= 100; i++) { for (let i = 2; i <= 100; i++) {