feat: categoria is_tax exclui impostos do denominador do savings_pct
savings_pct = (receita - despesas) / (receita - impostos). Toggle IMPOSTO no CRUD de categorias; badge dourado na lista. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
This commit is contained in:
@@ -41,6 +41,9 @@ var m010 string
|
||||
//go:embed sql/011_credit_bills.sql
|
||||
var m011 string
|
||||
|
||||
//go:embed sql/012_category_is_tax.sql
|
||||
var m012 string
|
||||
|
||||
func Run(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
// Bootstrap: ensure schema_migrations table exists before checking versions.
|
||||
if _, err := pool.Exec(ctx, `
|
||||
@@ -52,7 +55,7 @@ func Run(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
return fmt.Errorf("bootstrap schema_migrations: %w", err)
|
||||
}
|
||||
|
||||
migrations := []string{m001, m002, m003, m004, m005, m006, m007, m008, m009, m010, m011}
|
||||
migrations := []string{m001, m002, m003, m004, m005, m006, m007, m008, m009, m010, m011, m012}
|
||||
for i, sql := range migrations {
|
||||
version := i + 1
|
||||
var applied bool
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE categories ADD COLUMN IF NOT EXISTS is_tax BOOLEAN NOT NULL DEFAULT FALSE;
|
||||
@@ -7,6 +7,7 @@ type Category struct {
|
||||
Name string `json:"name"`
|
||||
Color string `json:"color"`
|
||||
IsDefault bool `json:"is_default"`
|
||||
IsTax bool `json:"is_tax"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
@@ -14,4 +15,5 @@ type Category struct {
|
||||
type CategoryInput struct {
|
||||
Name string `json:"name"`
|
||||
Color string `json:"color"`
|
||||
IsTax bool `json:"is_tax"`
|
||||
}
|
||||
|
||||
@@ -15,8 +15,8 @@ var ErrNotFound = errors.New("not found")
|
||||
type CategoryRepository interface {
|
||||
List(ctx context.Context) ([]model.Category, error)
|
||||
GetByID(ctx context.Context, id int) (*model.Category, error)
|
||||
Create(ctx context.Context, name, color string) (*model.Category, error)
|
||||
Update(ctx context.Context, id int, name, color string) (*model.Category, error)
|
||||
Create(ctx context.Context, name, color string, isTax bool) (*model.Category, error)
|
||||
Update(ctx context.Context, id int, name, color string, isTax bool) (*model.Category, error)
|
||||
Delete(ctx context.Context, id int) error
|
||||
HasTransactions(ctx context.Context, id int) (bool, error)
|
||||
}
|
||||
@@ -29,7 +29,7 @@ func NewCategoryRepository(db *pgxpool.Pool) CategoryRepository {
|
||||
|
||||
func (r *categoryRepo) List(ctx context.Context) ([]model.Category, error) {
|
||||
rows, err := r.db.Query(ctx, `
|
||||
SELECT id, name, color, is_default, created_at, updated_at
|
||||
SELECT id, name, color, is_default, is_tax, created_at, updated_at
|
||||
FROM categories
|
||||
ORDER BY is_default DESC, name ASC`)
|
||||
if err != nil {
|
||||
@@ -40,7 +40,7 @@ func (r *categoryRepo) List(ctx context.Context) ([]model.Category, error) {
|
||||
var out []model.Category
|
||||
for rows.Next() {
|
||||
var c model.Category
|
||||
if err := rows.Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.CreatedAt, &c.UpdatedAt); err != nil {
|
||||
if err := rows.Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.IsTax, &c.CreatedAt, &c.UpdatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, c)
|
||||
@@ -51,35 +51,35 @@ func (r *categoryRepo) List(ctx context.Context) ([]model.Category, error) {
|
||||
func (r *categoryRepo) GetByID(ctx context.Context, id int) (*model.Category, error) {
|
||||
var c model.Category
|
||||
err := r.db.QueryRow(ctx, `
|
||||
SELECT id, name, color, is_default, created_at, updated_at
|
||||
SELECT id, name, color, is_default, is_tax, created_at, updated_at
|
||||
FROM categories WHERE id = $1`, id).
|
||||
Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.CreatedAt, &c.UpdatedAt)
|
||||
Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.IsTax, &c.CreatedAt, &c.UpdatedAt)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return &c, err
|
||||
}
|
||||
|
||||
func (r *categoryRepo) Create(ctx context.Context, name, color string) (*model.Category, error) {
|
||||
func (r *categoryRepo) Create(ctx context.Context, name, color string, isTax bool) (*model.Category, error) {
|
||||
var c model.Category
|
||||
err := r.db.QueryRow(ctx, `
|
||||
INSERT INTO categories (name, color)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id, name, color, is_default, created_at, updated_at`,
|
||||
name, color).
|
||||
Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.CreatedAt, &c.UpdatedAt)
|
||||
INSERT INTO categories (name, color, is_tax)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id, name, color, is_default, is_tax, created_at, updated_at`,
|
||||
name, color, isTax).
|
||||
Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.IsTax, &c.CreatedAt, &c.UpdatedAt)
|
||||
return &c, err
|
||||
}
|
||||
|
||||
func (r *categoryRepo) Update(ctx context.Context, id int, name, color string) (*model.Category, error) {
|
||||
func (r *categoryRepo) Update(ctx context.Context, id int, name, color string, isTax bool) (*model.Category, error) {
|
||||
var c model.Category
|
||||
err := r.db.QueryRow(ctx, `
|
||||
UPDATE categories
|
||||
SET name = $1, color = $2, updated_at = NOW()
|
||||
WHERE id = $3
|
||||
RETURNING id, name, color, is_default, created_at, updated_at`,
|
||||
name, color, id).
|
||||
Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.CreatedAt, &c.UpdatedAt)
|
||||
SET name = $1, color = $2, is_tax = $3, updated_at = NOW()
|
||||
WHERE id = $4
|
||||
RETURNING id, name, color, is_default, is_tax, created_at, updated_at`,
|
||||
name, color, isTax, id).
|
||||
Scan(&c.ID, &c.Name, &c.Color, &c.IsDefault, &c.IsTax, &c.CreatedAt, &c.UpdatedAt)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
@@ -90,6 +90,19 @@ func (r *DashboardRepository) MonthlyEvolution(ctx context.Context, month string
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (r *DashboardRepository) TotalTaxes(ctx context.Context, month string) (float64, error) {
|
||||
var total float64
|
||||
err := r.pool.QueryRow(ctx, `
|
||||
SELECT COALESCE(SUM(t.amount), 0)
|
||||
FROM transactions t
|
||||
JOIN categories c ON c.id = t.category_id
|
||||
WHERE to_char(t.date, 'YYYY-MM') = $1
|
||||
AND t.type = 'expense'
|
||||
AND c.is_tax = TRUE
|
||||
`, month).Scan(&total)
|
||||
return total, err
|
||||
}
|
||||
|
||||
func (r *DashboardRepository) RecentTransactions(ctx context.Context, month string) ([]model.RecentTransaction, error) {
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT
|
||||
|
||||
@@ -32,14 +32,14 @@ func (s *CategoryService) Create(ctx context.Context, in model.CategoryInput) (*
|
||||
if err := validateInput(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.repo.Create(ctx, strings.TrimSpace(in.Name), in.Color)
|
||||
return s.repo.Create(ctx, strings.TrimSpace(in.Name), in.Color, in.IsTax)
|
||||
}
|
||||
|
||||
func (s *CategoryService) Update(ctx context.Context, id int, in model.CategoryInput) (*model.Category, error) {
|
||||
if err := validateInput(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.repo.Update(ctx, id, strings.TrimSpace(in.Name), in.Color)
|
||||
return s.repo.Update(ctx, id, strings.TrimSpace(in.Name), in.Color, in.IsTax)
|
||||
}
|
||||
|
||||
func (s *CategoryService) Delete(ctx context.Context, id int) error {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
type DashboardRepo interface {
|
||||
MonthlySummary(ctx context.Context, month string) (income, expenses float64, err error)
|
||||
TotalTaxes(ctx context.Context, month string) (float64, error)
|
||||
ByCategory(ctx context.Context, month string) ([]model.CategoryTotal, error)
|
||||
MonthlyEvolution(ctx context.Context, month string) ([]model.MonthEvolution, error)
|
||||
RecentTransactions(ctx context.Context, month string) ([]model.RecentTransaction, error)
|
||||
@@ -34,6 +35,11 @@ func (s *DashboardService) Get(ctx context.Context, month string) (*model.Dashbo
|
||||
return nil, err
|
||||
}
|
||||
|
||||
taxes, err := s.repo.TotalTaxes(ctx, month)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
byCategory, err := s.repo.ByCategory(ctx, month)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -49,9 +55,11 @@ func (s *DashboardService) Get(ctx context.Context, month string) (*model.Dashbo
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// savings_pct uses net income (gross - taxes) as denominator (Option B)
|
||||
var savingsPct float64
|
||||
if income > 0 {
|
||||
savingsPct = (income - expenses) / income * 100
|
||||
netIncome := income - taxes
|
||||
if netIncome > 0 {
|
||||
savingsPct = (income - expenses) / netIncome * 100
|
||||
}
|
||||
|
||||
statuses, err := s.recurrSvc.MonthlyStatus(ctx, month)
|
||||
|
||||
@@ -16,6 +16,9 @@ type mockDashboardRepo struct {
|
||||
func (m *mockDashboardRepo) MonthlySummary(_ context.Context, _ string) (float64, float64, error) {
|
||||
return m.income, m.expenses, nil
|
||||
}
|
||||
func (m *mockDashboardRepo) TotalTaxes(_ context.Context, _ string) (float64, error) {
|
||||
return 0, nil
|
||||
}
|
||||
func (m *mockDashboardRepo) ByCategory(_ context.Context, _ string) ([]model.CategoryTotal, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ export interface Category {
|
||||
name: string
|
||||
color: string
|
||||
is_default: boolean
|
||||
is_tax: boolean
|
||||
created_at: string
|
||||
updated_at: string
|
||||
}
|
||||
@@ -28,14 +29,14 @@ export const useCategoriesStore = defineStore('categories', () => {
|
||||
}
|
||||
}
|
||||
|
||||
async function create(name: string, color: string) {
|
||||
const cat = await api.post<Category>('/categories', { name, color })
|
||||
async function create(name: string, color: string, isTax = false) {
|
||||
const cat = await api.post<Category>('/categories', { name, color, is_tax: isTax })
|
||||
categories.value.push(cat)
|
||||
return cat
|
||||
}
|
||||
|
||||
async function update(id: number, name: string, color: string) {
|
||||
const cat = await api.put<Category>(`/categories/${id}`, { name, color })
|
||||
async function update(id: number, name: string, color: string, isTax = false) {
|
||||
const cat = await api.put<Category>(`/categories/${id}`, { name, color, is_tax: isTax })
|
||||
const idx = categories.value.findIndex((c) => c.id === id)
|
||||
if (idx !== -1) categories.value[idx] = cat
|
||||
return cat
|
||||
|
||||
@@ -6,19 +6,19 @@ import NeonPanel from '@/components/NeonPanel.vue'
|
||||
const store = useCategoriesStore()
|
||||
onMounted(() => store.fetchAll())
|
||||
|
||||
const form = ref({ name: '', color: '#a855f7' })
|
||||
const form = ref({ name: '', color: '#a855f7', is_tax: false })
|
||||
const editId = ref<number | null>(null)
|
||||
const formError = ref<string | null>(null)
|
||||
|
||||
function startEdit(id: number, name: string, color: string) {
|
||||
function startEdit(id: number, name: string, color: string, isTax: boolean) {
|
||||
editId.value = id
|
||||
form.value = { name, color }
|
||||
form.value = { name, color, is_tax: isTax }
|
||||
formError.value = null
|
||||
}
|
||||
|
||||
function cancelEdit() {
|
||||
editId.value = null
|
||||
form.value = { name: '', color: '#a855f7' }
|
||||
form.value = { name: '', color: '#a855f7', is_tax: false }
|
||||
formError.value = null
|
||||
}
|
||||
|
||||
@@ -26,11 +26,11 @@ async function submit() {
|
||||
formError.value = null
|
||||
try {
|
||||
if (editId.value !== null) {
|
||||
await store.update(editId.value, form.value.name, form.value.color)
|
||||
await store.update(editId.value, form.value.name, form.value.color, form.value.is_tax)
|
||||
cancelEdit()
|
||||
} else {
|
||||
await store.create(form.value.name, form.value.color)
|
||||
form.value = { name: '', color: '#a855f7' }
|
||||
await store.create(form.value.name, form.value.color, form.value.is_tax)
|
||||
form.value = { name: '', color: '#a855f7', is_tax: false }
|
||||
}
|
||||
} catch (e: any) {
|
||||
formError.value = e.message
|
||||
@@ -61,6 +61,10 @@ async function remove(id: number, name: string) {
|
||||
<input type="color" v-model="form.color" class="fc-color-input" />
|
||||
<span class="fc-mono fc-cat-form__hex">{{ form.color }}</span>
|
||||
</div>
|
||||
<label class="fc-cat-form__tax fc-mono">
|
||||
<input type="checkbox" v-model="form.is_tax" />
|
||||
IMPOSTO
|
||||
</label>
|
||||
</div>
|
||||
<p v-if="formError" class="fc-cat-form__error fc-mono">{{ formError }}</p>
|
||||
<div class="fc-cat-form__actions">
|
||||
@@ -84,8 +88,9 @@ async function remove(id: number, name: string) {
|
||||
<span class="fc-cat-item__dot" :style="{ background: cat.color }" />
|
||||
<span class="fc-body fc-cat-item__name">{{ cat.name }}</span>
|
||||
<span v-if="cat.is_default" class="fc-pixel fc-cat-item__default">PADRÃO</span>
|
||||
<span v-if="cat.is_tax" class="fc-pixel fc-cat-item__tax">IMPOSTO</span>
|
||||
<div class="fc-cat-item__actions">
|
||||
<button class="fc-btn fc-btn--sm fc-btn--ghost" @click="startEdit(cat.id, cat.name, cat.color)">EDITAR</button>
|
||||
<button class="fc-btn fc-btn--sm fc-btn--ghost" @click="startEdit(cat.id, cat.name, cat.color, cat.is_tax)">EDITAR</button>
|
||||
<button
|
||||
v-if="!cat.is_default"
|
||||
class="fc-btn fc-btn--sm fc-btn--danger"
|
||||
@@ -211,5 +216,24 @@ async function remove(id: number, name: string) {
|
||||
border-radius: 2px;
|
||||
}
|
||||
|
||||
.fc-cat-item__tax {
|
||||
font-size: 6px;
|
||||
color: var(--fc-gold);
|
||||
background: rgba(255,180,0,.15);
|
||||
border: 1px solid rgba(255,180,0,.4);
|
||||
padding: 3px 6px;
|
||||
border-radius: 2px;
|
||||
}
|
||||
|
||||
.fc-cat-form__tax {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
font-size: 10px;
|
||||
color: var(--fc-text-dim);
|
||||
cursor: pointer;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.fc-cat-item__actions { display: flex; gap: var(--fc-space-1); }
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user