feat(#37): módulo de cartão de crédito com faturas mensais

- Migration 011: closing_day/due_day em accounts + tabela credit_bills
- CreditBillService: cria/atualiza fatura corrente on-demand no GET /accounts
- Widget FATURA ATUAL no dashboard com total e botão PAGAR
- AccountsView: campos closing_day/due_day para contas credit + painel fatura
- Dashboard: current_bills lista faturas não pagas de todos os cartões

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
This commit is contained in:
2026-05-27 15:12:53 -03:00
co-authored by Claude Sonnet 4.6
parent ee6cd9f37e
commit e3b3433703
17 changed files with 530 additions and 29 deletions
+14 -12
View File
@@ -32,6 +32,7 @@ func (r *AccountRepository) List(ctx context.Context) ([]model.Account, error) {
SELECT
a.id, a.name, a.type, a.initial_balance,
a.yield_type, a.last_yield_date::text,
a.closing_day, a.due_day,
a.created_at::text, a.updated_at::text,
a.initial_balance
+ COALESCE(SUM(CASE WHEN t.type = 'income' THEN t.amount ELSE 0 END), 0)
@@ -50,7 +51,7 @@ func (r *AccountRepository) List(ctx context.Context) ([]model.Account, error) {
var out []model.Account
for rows.Next() {
var a model.Account
if err := rows.Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.YieldType, &a.LastYieldDate, &a.CreatedAt, &a.UpdatedAt, &a.Balance); err != nil {
if err := rows.Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.YieldType, &a.LastYieldDate, &a.ClosingDay, &a.DueDay, &a.CreatedAt, &a.UpdatedAt, &a.Balance); err != nil {
return nil, err
}
out = append(out, a)
@@ -64,6 +65,7 @@ func (r *AccountRepository) GetByID(ctx context.Context, id int) (*model.Account
SELECT
a.id, a.name, a.type, a.initial_balance,
a.yield_type, a.last_yield_date::text,
a.closing_day, a.due_day,
a.created_at::text, a.updated_at::text,
a.initial_balance
+ COALESCE(SUM(CASE WHEN t.type = 'income' THEN t.amount ELSE 0 END), 0)
@@ -73,7 +75,7 @@ func (r *AccountRepository) GetByID(ctx context.Context, id int) (*model.Account
LEFT JOIN transactions t ON t.account_id = a.id
WHERE a.id = $1
GROUP BY a.id
`, id).Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.YieldType, &a.LastYieldDate, &a.CreatedAt, &a.UpdatedAt, &a.Balance)
`, id).Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.YieldType, &a.LastYieldDate, &a.ClosingDay, &a.DueDay, &a.CreatedAt, &a.UpdatedAt, &a.Balance)
if err != nil {
return nil, err
}
@@ -87,11 +89,11 @@ func (r *AccountRepository) Create(ctx context.Context, in model.AccountInput) (
}
var a model.Account
err := r.pool.QueryRow(ctx, `
INSERT INTO accounts (name, type, initial_balance, yield_type)
VALUES ($1, $2, $3, $4)
RETURNING id, name, type, initial_balance, yield_type, last_yield_date::text, created_at::text, updated_at::text
`, in.Name, in.Type, in.InitialBalance, yt).
Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.YieldType, &a.LastYieldDate, &a.CreatedAt, &a.UpdatedAt)
INSERT INTO accounts (name, type, initial_balance, yield_type, closing_day, due_day)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING id, name, type, initial_balance, yield_type, last_yield_date::text, closing_day, due_day, created_at::text, updated_at::text
`, in.Name, in.Type, in.InitialBalance, yt, in.ClosingDay, in.DueDay).
Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.YieldType, &a.LastYieldDate, &a.ClosingDay, &a.DueDay, &a.CreatedAt, &a.UpdatedAt)
if err != nil {
return nil, err
}
@@ -105,12 +107,12 @@ func (r *AccountRepository) Update(ctx context.Context, id int, in model.Account
yt = "none"
}
row := r.pool.QueryRow(ctx, `
UPDATE accounts SET name=$1, type=$2, initial_balance=$3, yield_type=$4, updated_at=NOW()
WHERE id=$5
RETURNING id, name, type, initial_balance, yield_type, last_yield_date::text, created_at::text, updated_at::text
`, in.Name, in.Type, in.InitialBalance, yt, id)
UPDATE accounts SET name=$1, type=$2, initial_balance=$3, yield_type=$4, closing_day=$5, due_day=$6, updated_at=NOW()
WHERE id=$7
RETURNING id, name, type, initial_balance, yield_type, last_yield_date::text, closing_day, due_day, created_at::text, updated_at::text
`, in.Name, in.Type, in.InitialBalance, yt, in.ClosingDay, in.DueDay, id)
var a model.Account
if err := row.Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.YieldType, &a.LastYieldDate, &a.CreatedAt, &a.UpdatedAt); err != nil {
if err := row.Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.YieldType, &a.LastYieldDate, &a.ClosingDay, &a.DueDay, &a.CreatedAt, &a.UpdatedAt); err != nil {
return nil, err
}
full, err := r.GetByID(ctx, a.ID)
+103
View File
@@ -0,0 +1,103 @@
package repository
import (
"context"
"errors"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"financeiro-carvalho/internal/model"
)
type CreditBillRepository interface {
ListByAccount(ctx context.Context, accountID int) ([]model.CreditBill, error)
GetCurrent(ctx context.Context, accountID int) (*model.CreditBill, error)
Upsert(ctx context.Context, in model.CreditBillInput) (*model.CreditBill, error)
MarkPaid(ctx context.Context, id int, paymentAccountID *int) error
}
type creditBillRepo struct{ db *pgxpool.Pool }
func NewCreditBillRepository(db *pgxpool.Pool) CreditBillRepository {
return &creditBillRepo{db: db}
}
func (r *creditBillRepo) ListByAccount(ctx context.Context, accountID int) ([]model.CreditBill, error) {
rows, err := r.db.Query(ctx, `
SELECT
cb.id, cb.account_id, a.name,
cb.period_start::text, cb.period_end::text, cb.due_date::text,
COALESCE(SUM(t.amount) FILTER (WHERE t.type = 'expense'), 0) AS total,
cb.paid, cb.paid_at::text, cb.payment_account_id
FROM credit_bills cb
JOIN accounts a ON a.id = cb.account_id
LEFT JOIN transactions t ON t.account_id = cb.account_id
AND t.date >= cb.period_start AND t.date <= cb.period_end
AND t.type = 'expense'
WHERE cb.account_id = $1
GROUP BY cb.id, a.name
ORDER BY cb.period_start DESC
`, accountID)
if err != nil {
return nil, err
}
defer rows.Close()
var out []model.CreditBill
for rows.Next() {
var b model.CreditBill
if err := rows.Scan(&b.ID, &b.AccountID, &b.AccountName, &b.PeriodStart, &b.PeriodEnd, &b.DueDate, &b.Total, &b.Paid, &b.PaidAt, &b.PaymentAccountID); err != nil {
return nil, err
}
out = append(out, b)
}
return out, rows.Err()
}
func (r *creditBillRepo) GetCurrent(ctx context.Context, accountID int) (*model.CreditBill, error) {
var b model.CreditBill
err := r.db.QueryRow(ctx, `
SELECT
cb.id, cb.account_id, a.name,
cb.period_start::text, cb.period_end::text, cb.due_date::text,
COALESCE(SUM(t.amount) FILTER (WHERE t.type = 'expense'), 0) AS total,
cb.paid, cb.paid_at::text, cb.payment_account_id
FROM credit_bills cb
JOIN accounts a ON a.id = cb.account_id
LEFT JOIN transactions t ON t.account_id = cb.account_id
AND t.date >= cb.period_start AND t.date <= cb.period_end
AND t.type = 'expense'
WHERE cb.account_id = $1 AND cb.paid = FALSE
GROUP BY cb.id, a.name
ORDER BY cb.period_start DESC
LIMIT 1
`, accountID).Scan(&b.ID, &b.AccountID, &b.AccountName, &b.PeriodStart, &b.PeriodEnd, &b.DueDate, &b.Total, &b.Paid, &b.PaidAt, &b.PaymentAccountID)
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil
}
return &b, err
}
func (r *creditBillRepo) Upsert(ctx context.Context, in model.CreditBillInput) (*model.CreditBill, error) {
var b model.CreditBill
err := r.db.QueryRow(ctx, `
INSERT INTO credit_bills (account_id, period_start, period_end, due_date, payment_account_id)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (account_id, period_start) DO UPDATE
SET period_end = EXCLUDED.period_end,
due_date = EXCLUDED.due_date,
payment_account_id = EXCLUDED.payment_account_id
RETURNING id, account_id, period_start::text, period_end::text, due_date::text, paid, paid_at::text, payment_account_id
`, in.AccountID, in.PeriodStart, in.PeriodEnd, in.DueDate, in.PaymentAccountID).
Scan(&b.ID, &b.AccountID, &b.PeriodStart, &b.PeriodEnd, &b.DueDate, &b.Paid, &b.PaidAt, &b.PaymentAccountID)
return &b, err
}
func (r *creditBillRepo) MarkPaid(ctx context.Context, id int, paymentAccountID *int) error {
_, err := r.db.Exec(ctx, `
UPDATE credit_bills
SET paid = TRUE, paid_at = NOW(), payment_account_id = $2
WHERE id = $1
`, id, paymentAccountID)
return err
}