feat(#21): contas bancárias e patrimônio consolidado

CRUD de contas (corrente/poupança/investimento/cartão) com saldo calculado
automaticamente. account_id nullable em transactions. Widget patrimônio no
dashboard. Tela /contas. Seletor de conta nas transações manuais.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
This commit is contained in:
2026-05-26 21:34:56 -03:00
co-authored by Claude Sonnet 4.6
parent fbcb1d76aa
commit 9a964423fd
21 changed files with 667 additions and 24 deletions
+1
View File
@@ -9,6 +9,7 @@
<RouterLink to="/categorias">Categorias</RouterLink>
<RouterLink to="/transacoes">Transações</RouterLink>
<RouterLink to="/importar">Importar</RouterLink>
<RouterLink to="/contas">Contas</RouterLink>
<RouterLink to="/configuracoes">Configurações</RouterLink>
</nav>
</header>
+5
View File
@@ -24,6 +24,11 @@ const router = createRouter({
name: 'transactions',
component: () => import('../views/TransactionsView.vue'),
},
{
path: '/contas',
name: 'accounts',
component: () => import('../views/AccountsView.vue'),
},
{
path: '/configuracoes',
name: 'settings',
+57
View File
@@ -0,0 +1,57 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { api } from '@/services/api'
export interface Account {
id: number
name: string
type: 'checking' | 'savings' | 'investment' | 'credit'
initial_balance: number
balance: number
created_at: string
updated_at: string
}
export interface AccountInput {
name: string
type: string
initial_balance: number
}
export const useAccountsStore = defineStore('accounts', () => {
const accounts = ref<Account[]>([])
const loading = ref(false)
const error = ref<string | null>(null)
async function fetchAll() {
loading.value = true
error.value = null
try {
accounts.value = await api.get<Account[]>('/accounts')
} catch (e: any) {
error.value = e.message
} finally {
loading.value = false
}
}
async function create(input: AccountInput) {
const item = await api.post<Account>('/accounts', input)
accounts.value.push(item)
return item
}
async function update(id: number, input: AccountInput) {
const item = await api.put<Account>(`/accounts/${id}`, input)
const idx = accounts.value.findIndex((a) => a.id === id)
if (idx !== -1) accounts.value[idx] = item
return item
}
async function remove(id: number) {
await api.delete(`/accounts/${id}`)
accounts.value = accounts.value.filter((a) => a.id !== id)
}
return { accounts, loading, error, fetchAll, create, update, remove }
})
+1
View File
@@ -30,6 +30,7 @@ export interface DashboardData {
total_income: number
total_expenses: number
savings_pct: number
total_patrimony: number
by_category: CategoryTotal[]
monthly_evolution: MonthEvolution[]
recent_transactions: RecentTransaction[]
+2
View File
@@ -10,6 +10,7 @@ export interface Transaction {
type: 'income' | 'expense'
source: 'manual' | 'import'
category_id: number | null
account_id: number | null
created_at: string
updated_at: string
}
@@ -20,6 +21,7 @@ export interface TransactionInput {
description: string
type: 'income' | 'expense'
category_id: number | null
account_id: number | null
}
export const useTransactionsStore = defineStore('transactions', () => {
+156
View File
@@ -0,0 +1,156 @@
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useAccountsStore, type AccountInput } from '@/stores/accounts'
const store = useAccountsStore()
onMounted(() => store.fetchAll())
const typeLabels: Record<string, string> = {
checking: 'Conta Corrente',
savings: 'Poupança',
investment: 'Investimento',
credit: 'Cartão de Crédito',
}
const blank = (): AccountInput => ({ name: '', type: 'checking', initial_balance: 0 })
const form = ref(blank())
const editId = ref<number | null>(null)
const initialRaw = ref('')
const formError = ref<string | null>(null)
function parseAmount(s: string) {
return parseFloat(s.replace(/\./g, '').replace(',', '.')) || 0
}
function startEdit(id: number) {
const a = store.accounts.find((x) => x.id === id)
if (!a) return
editId.value = id
form.value = { name: a.name, type: a.type, initial_balance: a.initial_balance }
initialRaw.value = a.initial_balance.toLocaleString('pt-BR', { minimumFractionDigits: 2 })
formError.value = null
}
function cancelEdit() {
editId.value = null
form.value = blank()
initialRaw.value = ''
formError.value = null
}
async function submit() {
formError.value = null
form.value.initial_balance = parseAmount(initialRaw.value)
try {
if (editId.value !== null) {
await store.update(editId.value, form.value)
cancelEdit()
} else {
await store.create(form.value)
form.value = blank()
initialRaw.value = ''
}
} catch (e: any) {
formError.value = e.message
}
}
async function remove(id: number, name: string) {
if (!confirm(`Excluir conta "${name}"?`)) return
await store.remove(id)
}
function fmt(v: number) {
return v.toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' })
}
</script>
<template>
<div class="page">
<h1>Contas Bancárias</h1>
<form class="form-card" @submit.prevent="submit">
<h2>{{ editId !== null ? 'Editar conta' : 'Nova conta' }}</h2>
<div class="form-row">
<input v-model="form.name" placeholder="Ex: Nubank Corrente" required class="input-name" />
<select v-model="form.type" class="input-sm">
<option value="checking">Conta Corrente</option>
<option value="savings">Poupança</option>
<option value="investment">Investimento</option>
<option value="credit">Cartão de Crédito</option>
</select>
<input v-model="initialRaw" placeholder="Saldo inicial (ex: 1.500,00)" class="input-sm input-amount" />
</div>
<p v-if="formError" class="form-error">{{ formError }}</p>
<div class="form-actions">
<button type="submit" class="btn btn-primary">{{ editId !== null ? 'Salvar' : 'Adicionar' }}</button>
<button v-if="editId !== null" type="button" class="btn btn-ghost" @click="cancelEdit">Cancelar</button>
</div>
</form>
<p v-if="store.loading">Carregando</p>
<div v-else>
<div class="total-bar" v-if="store.accounts.length > 0">
<span>Patrimônio total</span>
<strong>{{ fmt(store.accounts.reduce((s, a) => s + a.balance, 0)) }}</strong>
</div>
<ul class="account-list">
<li
v-for="a in store.accounts"
:key="a.id"
class="account-item"
:class="{ editing: editId === a.id }"
>
<div class="account-info">
<strong>{{ a.name }}</strong>
<span class="account-meta">{{ typeLabels[a.type] }} · Saldo inicial {{ fmt(a.initial_balance) }}</span>
</div>
<div class="account-right">
<span class="account-balance" :class="a.balance >= 0 ? 'positive' : 'negative'">
{{ fmt(a.balance) }}
</span>
<div class="item-actions">
<button class="btn btn-sm" @click="startEdit(a.id)">Editar</button>
<button class="btn btn-sm btn-danger" @click="remove(a.id, a.name)">Excluir</button>
</div>
</div>
</li>
<li v-if="store.accounts.length === 0" class="empty">Nenhuma conta cadastrada</li>
</ul>
</div>
</div>
</template>
<style scoped>
.page { max-width: 640px; margin: 0 auto; padding: 1.5rem 1rem; font-family: sans-serif; }
h1 { font-size: 1.5rem; margin-bottom: 1.25rem; }
h2 { font-size: 0.95rem; font-weight: 600; margin: 0 0 0.75rem; }
.form-card { background: #f9fafb; border: 1px solid #e5e7eb; border-radius: 8px; padding: 1rem; margin-bottom: 1.5rem; }
.form-row { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; }
.input-name { flex: 1; min-width: 160px; padding: 0.45rem 0.6rem; border: 1px solid #d1d5db; border-radius: 6px; font-size: 0.875rem; }
.input-sm { padding: 0.45rem 0.6rem; border: 1px solid #d1d5db; border-radius: 6px; font-size: 0.875rem; }
.input-amount { width: 140px; }
.form-error { color: #dc2626; font-size: 0.875rem; margin: 0.5rem 0 0; }
.form-actions { margin-top: 0.75rem; display: flex; gap: 0.5rem; }
.btn { padding: 0.4rem 1rem; border: 1px solid #d1d5db; border-radius: 6px; cursor: pointer; font-size: 0.875rem; background: #fff; }
.btn-primary { background: #4f46e5; color: #fff; border-color: #4f46e5; }
.btn-ghost { background: transparent; }
.btn-sm { padding: 0.25rem 0.6rem; }
.btn-danger { color: #dc2626; border-color: #fca5a5; }
.total-bar { display: flex; justify-content: space-between; align-items: center; background: #1e293b; color: #f8fafc; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; font-size: 0.9rem; }
.total-bar strong { font-size: 1.1rem; }
.account-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 0.5rem; }
.account-item { display: flex; align-items: center; justify-content: space-between; padding: 0.75rem 1rem; border: 1px solid #e5e7eb; border-radius: 8px; background: #fff; flex-wrap: wrap; gap: 0.5rem; }
.account-item.editing { border-color: #4f46e5; box-shadow: 0 0 0 2px #e0e7ff; }
.account-info { display: flex; flex-direction: column; gap: 0.2rem; }
.account-meta { font-size: 0.8rem; color: #6b7280; }
.account-right { display: flex; align-items: center; gap: 1rem; }
.account-balance { font-size: 1.1rem; font-weight: 700; }
.positive { color: #059669; }
.negative { color: #dc2626; }
.item-actions { display: flex; gap: 0.4rem; }
.empty { color: #9ca3af; text-align: center; padding: 2rem; }
</style>
+8 -1
View File
@@ -76,6 +76,13 @@ const maxEvolution = computed(() => {
<div class="savings-pct">{{ store.data.savings_pct.toFixed(1) }}%</div>
<div class="savings-target">Meta: 40%</div>
</div>
<div class="card patrimony-card" v-if="store.data.total_patrimony > 0 || store.data.total_patrimony < 0">
<div class="card-label">Patrimônio</div>
<div class="card-value" :class="store.data.total_patrimony >= 0 ? 'income' : 'expense'">
{{ fmt(store.data.total_patrimony) }}
</div>
<div class="savings-target">todas as contas</div>
</div>
</div>
<!-- Bottom section: two columns on wide, stacked on mobile -->
@@ -171,7 +178,7 @@ h2 { font-size: 0.95rem; font-weight: 600; margin: 0 0 0.75rem; color: #374151;
}
/* Summary */
.summary-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 0.75rem; margin-bottom: 1rem; }
.summary-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 0.75rem; margin-bottom: 1rem; }
@media (max-width: 600px) { .summary-grid { grid-template-columns: 1fr 1fr; } }
.card { background: #fff; border: 1px solid #e5e7eb; border-radius: 10px; padding: 1rem; }
+9 -1
View File
@@ -2,15 +2,18 @@
import { ref, computed, onMounted } from 'vue'
import { useTransactionsStore, type TransactionInput } from '@/stores/transactions'
import { useCategoriesStore } from '@/stores/categories'
import { useAccountsStore } from '@/stores/accounts'
const store = useTransactionsStore()
const catStore = useCategoriesStore()
const accStore = useAccountsStore()
const currentMonth = ref(new Date().toISOString().slice(0, 7))
onMounted(() => {
store.fetchAll(currentMonth.value)
catStore.fetchAll()
accStore.fetchAll()
})
function changeMonth(delta: number) {
@@ -26,6 +29,7 @@ const blank = (): TransactionInput => ({
description: '',
type: 'expense',
category_id: null,
account_id: null,
})
const form = ref(blank())
@@ -41,7 +45,7 @@ function startEdit(id: number) {
const t = store.transactions.find((x) => x.id === id)
if (!t || t.source !== 'manual') return
editId.value = id
form.value = { date: t.date, amount: t.amount, description: t.description, type: t.type as any, category_id: t.category_id }
form.value = { date: t.date, amount: t.amount, description: t.description, type: t.type as any, category_id: t.category_id, account_id: t.account_id }
amountRaw.value = t.amount.toLocaleString('pt-BR', { minimumFractionDigits: 2 })
formError.value = null
}
@@ -118,6 +122,10 @@ function catName(id: number | null) {
<option :value="null">Sem categoria</option>
<option v-for="c in catStore.categories" :key="c.id" :value="c.id">{{ c.name }}</option>
</select>
<select v-model="form.account_id" class="input-sm">
<option :value="null">Sem conta</option>
<option v-for="a in accStore.accounts" :key="a.id" :value="a.id">{{ a.name }}</option>
</select>
<input v-model="form.description" placeholder="Descrição" required class="input-desc" />
</div>
<p v-if="formError" class="form-error">{{ formError }}</p>