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