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:
@@ -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>
|
||||
Reference in New Issue
Block a user