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
+9 -2
View File
@@ -71,11 +71,14 @@ func main() {
accountRepo := repository.NewAccountRepository(pool) accountRepo := repository.NewAccountRepository(pool)
cdiSvc := service.NewCDIYieldService(accountRepo, manualTxRepo) cdiSvc := service.NewCDIYieldService(accountRepo, manualTxRepo)
accountSvc := service.NewAccountService(accountRepo, cdiSvc) creditBillRepo := repository.NewCreditBillRepository(pool)
creditBillSvc := service.NewCreditBillService(creditBillRepo, accountRepo)
accountSvc := service.NewAccountService(accountRepo, cdiSvc, creditBillSvc)
accountHandler := handler.NewAccountHandler(accountSvc) accountHandler := handler.NewAccountHandler(accountSvc)
creditBillHandler := handler.NewCreditBillHandler(creditBillSvc)
dashboardRepo := repository.NewDashboardRepository(pool) dashboardRepo := repository.NewDashboardRepository(pool)
dashboardSvc := service.NewDashboardService(dashboardRepo, recurringSvc, accountRepo) dashboardSvc := service.NewDashboardService(dashboardRepo, recurringSvc, accountRepo, creditBillSvc)
dashboardHandler := handler.NewDashboardHandler(dashboardSvc) dashboardHandler := handler.NewDashboardHandler(dashboardSvc)
r.Get("/health", handler.Health) r.Get("/health", handler.Health)
@@ -113,6 +116,10 @@ func main() {
r.Post("/accounts", accountHandler.Create) r.Post("/accounts", accountHandler.Create)
r.Put("/accounts/{id}", accountHandler.Update) r.Put("/accounts/{id}", accountHandler.Update)
r.Delete("/accounts/{id}", accountHandler.Delete) r.Delete("/accounts/{id}", accountHandler.Delete)
r.Get("/accounts/{id}/bills", creditBillHandler.ListByAccount)
r.Get("/accounts/{id}/bills/current", creditBillHandler.GetCurrent)
r.Post("/accounts/{id}/bills/ensure", creditBillHandler.EnsureCurrent)
r.Post("/bills/{id}/pay", creditBillHandler.MarkPaid)
r.Get("/dashboard", dashboardHandler.Get) r.Get("/dashboard", dashboardHandler.Get)
+86
View File
@@ -0,0 +1,86 @@
package handler
import (
"encoding/json"
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
"financeiro-carvalho/internal/service"
)
type CreditBillHandler struct {
svc *service.CreditBillService
}
func NewCreditBillHandler(svc *service.CreditBillService) *CreditBillHandler {
return &CreditBillHandler{svc: svc}
}
func (h *CreditBillHandler) ListByAccount(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
respondError(w, http.StatusBadRequest, "invalid id")
return
}
bills, err := h.svc.ListByAccount(r.Context(), id)
if err != nil {
respondError(w, http.StatusInternalServerError, "failed to list bills")
return
}
respondJSON(w, http.StatusOK, bills)
}
func (h *CreditBillHandler) GetCurrent(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
respondError(w, http.StatusBadRequest, "invalid id")
return
}
bill, err := h.svc.GetCurrent(r.Context(), id)
if err != nil {
respondError(w, http.StatusInternalServerError, "failed to get current bill")
return
}
if bill == nil {
respondJSON(w, http.StatusOK, nil)
return
}
respondJSON(w, http.StatusOK, bill)
}
func (h *CreditBillHandler) EnsureCurrent(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
respondError(w, http.StatusBadRequest, "invalid id")
return
}
var body struct {
ClosingDay int `json:"closing_day"`
DueDay int `json:"due_day"`
}
_ = json.NewDecoder(r.Body).Decode(&body)
if err := h.svc.EnsureCurrentBill(r.Context(), id, body.ClosingDay, body.DueDay); err != nil {
respondError(w, http.StatusInternalServerError, "failed to ensure bill")
return
}
w.WriteHeader(http.StatusNoContent)
}
func (h *CreditBillHandler) MarkPaid(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
respondError(w, http.StatusBadRequest, "invalid id")
return
}
var body struct {
PaymentAccountID *int `json:"payment_account_id"`
}
_ = json.NewDecoder(r.Body).Decode(&body)
if err := h.svc.MarkPaid(r.Context(), id, body.PaymentAccountID); err != nil {
respondError(w, http.StatusInternalServerError, "failed to mark bill paid")
return
}
w.WriteHeader(http.StatusNoContent)
}
@@ -0,0 +1,19 @@
-- Credit card billing period configuration per account
ALTER TABLE accounts
ADD COLUMN IF NOT EXISTS closing_day INTEGER CHECK (closing_day BETWEEN 1 AND 28),
ADD COLUMN IF NOT EXISTS due_day INTEGER CHECK (due_day BETWEEN 1 AND 28);
-- Materialized bill metadata (calculated from transactions)
-- One row per (account, billing period start date)
CREATE TABLE IF NOT EXISTS credit_bills (
id SERIAL PRIMARY KEY,
account_id INTEGER NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
period_start DATE NOT NULL,
period_end DATE NOT NULL,
due_date DATE NOT NULL,
paid BOOLEAN NOT NULL DEFAULT FALSE,
paid_at TIMESTAMP WITH TIME ZONE,
payment_account_id INTEGER REFERENCES accounts(id) ON DELETE SET NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
UNIQUE(account_id, period_start)
);
+5
View File
@@ -8,6 +8,9 @@ type Account struct {
Balance float64 `json:"balance"` Balance float64 `json:"balance"`
YieldType string `json:"yield_type"` // "none" | "cdi" | "variable" YieldType string `json:"yield_type"` // "none" | "cdi" | "variable"
LastYieldDate *string `json:"last_yield_date,omitempty"` LastYieldDate *string `json:"last_yield_date,omitempty"`
ClosingDay *int `json:"closing_day,omitempty"`
DueDay *int `json:"due_day,omitempty"`
CurrentBill *CreditBill `json:"current_bill,omitempty"`
CreatedAt string `json:"created_at"` CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"` UpdatedAt string `json:"updated_at"`
} }
@@ -17,4 +20,6 @@ type AccountInput struct {
Type string `json:"type"` Type string `json:"type"`
InitialBalance float64 `json:"initial_balance"` InitialBalance float64 `json:"initial_balance"`
YieldType string `json:"yield_type"` // "none" | "cdi" | "variable" YieldType string `json:"yield_type"` // "none" | "cdi" | "variable"
ClosingDay *int `json:"closing_day,omitempty"`
DueDay *int `json:"due_day,omitempty"`
} }
+22
View File
@@ -0,0 +1,22 @@
package model
type CreditBill struct {
ID int `json:"id"`
AccountID int `json:"account_id"`
AccountName string `json:"account_name"`
PeriodStart string `json:"period_start"`
PeriodEnd string `json:"period_end"`
DueDate string `json:"due_date"`
Total float64 `json:"total"`
Paid bool `json:"paid"`
PaidAt *string `json:"paid_at,omitempty"`
PaymentAccountID *int `json:"payment_account_id,omitempty"`
}
type CreditBillInput struct {
AccountID int `json:"account_id"`
PeriodStart string `json:"period_start"`
PeriodEnd string `json:"period_end"`
DueDate string `json:"due_date"`
PaymentAccountID *int `json:"payment_account_id,omitempty"`
}
+1
View File
@@ -34,4 +34,5 @@ type DashboardData struct {
RecentTransactions []RecentTransaction `json:"recent_transactions"` RecentTransactions []RecentTransaction `json:"recent_transactions"`
PendingRecurring int `json:"pending_recurring"` PendingRecurring int `json:"pending_recurring"`
PendingIncomeRecurrings []PendingIncome `json:"pending_income_recurrings"` PendingIncomeRecurrings []PendingIncome `json:"pending_income_recurrings"`
CurrentBills []CreditBill `json:"current_bills"`
} }
+14 -12
View File
@@ -32,6 +32,7 @@ func (r *AccountRepository) List(ctx context.Context) ([]model.Account, error) {
SELECT SELECT
a.id, a.name, a.type, a.initial_balance, a.id, a.name, a.type, a.initial_balance,
a.yield_type, a.last_yield_date::text, a.yield_type, a.last_yield_date::text,
a.closing_day, a.due_day,
a.created_at::text, a.updated_at::text, a.created_at::text, a.updated_at::text,
a.initial_balance a.initial_balance
+ COALESCE(SUM(CASE WHEN t.type = 'income' THEN t.amount ELSE 0 END), 0) + 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 var out []model.Account
for rows.Next() { for rows.Next() {
var a model.Account 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 return nil, err
} }
out = append(out, a) out = append(out, a)
@@ -64,6 +65,7 @@ func (r *AccountRepository) GetByID(ctx context.Context, id int) (*model.Account
SELECT SELECT
a.id, a.name, a.type, a.initial_balance, a.id, a.name, a.type, a.initial_balance,
a.yield_type, a.last_yield_date::text, a.yield_type, a.last_yield_date::text,
a.closing_day, a.due_day,
a.created_at::text, a.updated_at::text, a.created_at::text, a.updated_at::text,
a.initial_balance a.initial_balance
+ COALESCE(SUM(CASE WHEN t.type = 'income' THEN t.amount ELSE 0 END), 0) + 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 LEFT JOIN transactions t ON t.account_id = a.id
WHERE a.id = $1 WHERE a.id = $1
GROUP BY a.id 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 { if err != nil {
return nil, err return nil, err
} }
@@ -87,11 +89,11 @@ func (r *AccountRepository) Create(ctx context.Context, in model.AccountInput) (
} }
var a model.Account var a model.Account
err := r.pool.QueryRow(ctx, ` err := r.pool.QueryRow(ctx, `
INSERT INTO accounts (name, type, initial_balance, yield_type) INSERT INTO accounts (name, type, initial_balance, yield_type, closing_day, due_day)
VALUES ($1, $2, $3, $4) VALUES ($1, $2, $3, $4, $5, $6)
RETURNING id, name, type, initial_balance, yield_type, last_yield_date::text, created_at::text, updated_at::text 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.Name, in.Type, in.InitialBalance, yt, in.ClosingDay, in.DueDay).
Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.YieldType, &a.LastYieldDate, &a.CreatedAt, &a.UpdatedAt) Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.YieldType, &a.LastYieldDate, &a.ClosingDay, &a.DueDay, &a.CreatedAt, &a.UpdatedAt)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -105,12 +107,12 @@ func (r *AccountRepository) Update(ctx context.Context, id int, in model.Account
yt = "none" yt = "none"
} }
row := r.pool.QueryRow(ctx, ` row := r.pool.QueryRow(ctx, `
UPDATE accounts SET name=$1, type=$2, initial_balance=$3, yield_type=$4, updated_at=NOW() UPDATE accounts SET name=$1, type=$2, initial_balance=$3, yield_type=$4, closing_day=$5, due_day=$6, updated_at=NOW()
WHERE id=$5 WHERE id=$7
RETURNING id, name, type, initial_balance, yield_type, last_yield_date::text, created_at::text, updated_at::text 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, id) `, in.Name, in.Type, in.InitialBalance, yt, in.ClosingDay, in.DueDay, id)
var a model.Account 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 return nil, err
} }
full, err := r.GetByID(ctx, a.ID) 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
}
+29 -6
View File
@@ -29,10 +29,11 @@ type AccountRepo interface {
type AccountService struct { type AccountService struct {
repo repository.AccountRepoWithYield repo repository.AccountRepoWithYield
cdiSvc *CDIYieldService cdiSvc *CDIYieldService
billSvc *CreditBillService
} }
func NewAccountService(repo repository.AccountRepoWithYield, cdiSvc *CDIYieldService) *AccountService { func NewAccountService(repo repository.AccountRepoWithYield, cdiSvc *CDIYieldService, billSvc *CreditBillService) *AccountService {
return &AccountService{repo: repo, cdiSvc: cdiSvc} return &AccountService{repo: repo, cdiSvc: cdiSvc, billSvc: billSvc}
} }
func (s *AccountService) validate(in model.AccountInput) error { func (s *AccountService) validate(in model.AccountInput) error {
@@ -45,22 +46,44 @@ func (s *AccountService) validate(in model.AccountInput) error {
return nil return nil
} }
// List returns accounts, applying CDI yield on-demand for cdi-type accounts. // List returns accounts, applying CDI yield on-demand and attaching current bill for credit accounts.
func (s *AccountService) List(ctx context.Context) ([]model.Account, error) { func (s *AccountService) List(ctx context.Context) ([]model.Account, error) {
accounts, err := s.repo.List(ctx) accounts, err := s.repo.List(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
yieldApplied := false
for i := range accounts { for i := range accounts {
if accounts[i].YieldType == "cdi" { if accounts[i].YieldType == "cdi" {
// Apply CDI yield — errors are non-fatal (BCB API may be unavailable)
_ = s.cdiSvc.ApplyYield(ctx, &accounts[i]) _ = s.cdiSvc.ApplyYield(ctx, &accounts[i])
yieldApplied = true
} }
} }
// Re-fetch after yield transactions may have been created to get updated balances if yieldApplied {
return s.repo.List(ctx) accounts, err = s.repo.List(ctx)
if err != nil {
return nil, err
}
}
for i := range accounts {
if accounts[i].Type == "credit" {
closing, due := 1, 10
if accounts[i].ClosingDay != nil {
closing = *accounts[i].ClosingDay
}
if accounts[i].DueDay != nil {
due = *accounts[i].DueDay
}
_ = s.billSvc.EnsureCurrentBill(ctx, accounts[i].ID, closing, due)
bill, _ := s.billSvc.GetCurrent(ctx, accounts[i].ID)
accounts[i].CurrentBill = bill
}
}
return accounts, nil
} }
func (s *AccountService) Create(ctx context.Context, in model.AccountInput) (*model.Account, error) { func (s *AccountService) Create(ctx context.Context, in model.AccountInput) (*model.Account, error) {
+15 -1
View File
@@ -41,10 +41,24 @@ func (m *mockAccountRepo) Delete(_ context.Context, _ int) error
func (m *mockAccountRepo) TotalPatrimony(_ context.Context) (float64, error) { return 0, nil } func (m *mockAccountRepo) TotalPatrimony(_ context.Context) (float64, error) { return 0, nil }
func (m *mockAccountRepo) UpdateLastYieldDate(_ context.Context, _ int, _ string) error { return nil } func (m *mockAccountRepo) UpdateLastYieldDate(_ context.Context, _ int, _ string) error { return nil }
type mockCreditBillRepo struct{}
func (m *mockCreditBillRepo) ListByAccount(_ context.Context, _ int) ([]model.CreditBill, error) {
return nil, nil
}
func (m *mockCreditBillRepo) GetCurrent(_ context.Context, _ int) (*model.CreditBill, error) {
return nil, nil
}
func (m *mockCreditBillRepo) Upsert(_ context.Context, _ model.CreditBillInput) (*model.CreditBill, error) {
return &model.CreditBill{}, nil
}
func (m *mockCreditBillRepo) MarkPaid(_ context.Context, _ int, _ *int) error { return nil }
func newAccountSvc() *service.AccountService { func newAccountSvc() *service.AccountService {
repo := &mockAccountRepo{} repo := &mockAccountRepo{}
cdiSvc := service.NewCDIYieldService(repo, &mockTxRepo{}) cdiSvc := service.NewCDIYieldService(repo, &mockTxRepo{})
return service.NewAccountService(repo, cdiSvc) billSvc := service.NewCreditBillService(&mockCreditBillRepo{}, repo)
return service.NewAccountService(repo, cdiSvc, billSvc)
} }
func TestCreateAccount_EmptyName(t *testing.T) { func TestCreateAccount_EmptyName(t *testing.T) {
+95
View File
@@ -0,0 +1,95 @@
package service
import (
"context"
"errors"
"time"
"financeiro-carvalho/internal/model"
"financeiro-carvalho/internal/repository"
)
var ErrBillNotCreditAccount = errors.New("account is not a credit card account")
type CreditBillService struct {
repo repository.CreditBillRepository
accountRepo repository.AccountRepoWithYield
}
func NewCreditBillService(repo repository.CreditBillRepository, accountRepo repository.AccountRepoWithYield) *CreditBillService {
return &CreditBillService{repo: repo, accountRepo: accountRepo}
}
func (s *CreditBillService) ListByAccount(ctx context.Context, accountID int) ([]model.CreditBill, error) {
bills, err := s.repo.ListByAccount(ctx, accountID)
if bills == nil {
return []model.CreditBill{}, err
}
return bills, err
}
func (s *CreditBillService) GetCurrent(ctx context.Context, accountID int) (*model.CreditBill, error) {
return s.repo.GetCurrent(ctx, accountID)
}
// EnsureCurrentBill creates (or refreshes) the current open bill for a credit account
// using the account's closing_day and due_day config (defaults: closing=1, due=10).
func (s *CreditBillService) EnsureCurrentBill(ctx context.Context, accountID int, closingDay, dueDay int) error {
if closingDay == 0 {
closingDay = 1
}
if dueDay == 0 {
dueDay = 10
}
now := time.Now()
// Determine the current billing period based on closing day
var periodStart, periodEnd time.Time
if now.Day() > closingDay {
// We're after the closing day: current period is from closingDay+1 this month to closingDay next month
y, m, _ := now.Date()
periodStart = time.Date(y, m, closingDay+1, 0, 0, 0, 0, time.UTC)
next := time.Date(y, m+1, closingDay, 0, 0, 0, 0, time.UTC)
periodEnd = next
} else {
// We're before the closing day: current period is from closingDay+1 last month to closingDay this month
y, m, _ := now.Date()
periodStart = time.Date(y, m-1, closingDay+1, 0, 0, 0, 0, time.UTC)
periodEnd = time.Date(y, m, closingDay, 0, 0, 0, 0, time.UTC)
}
// Due date: dueDay of the month after periodEnd
dueDate := time.Date(periodEnd.Year(), periodEnd.Month()+1, dueDay, 0, 0, 0, 0, time.UTC)
in := model.CreditBillInput{
AccountID: accountID,
PeriodStart: periodStart.Format("2006-01-02"),
PeriodEnd: periodEnd.Format("2006-01-02"),
DueDate: dueDate.Format("2006-01-02"),
}
_, err := s.repo.Upsert(ctx, in)
return err
}
func (s *CreditBillService) MarkPaid(ctx context.Context, id int, paymentAccountID *int) error {
return s.repo.MarkPaid(ctx, id, paymentAccountID)
}
// currentBillsForDashboard returns unpaid current bills across all credit accounts.
func (s *CreditBillService) currentBillsForDashboard(ctx context.Context) ([]model.CreditBill, error) {
accounts, err := s.accountRepo.List(ctx)
if err != nil {
return nil, err
}
var bills []model.CreditBill
for _, a := range accounts {
if a.Type != "credit" {
continue
}
bill, err := s.repo.GetCurrent(ctx, a.ID)
if err != nil || bill == nil {
continue
}
bills = append(bills, *bill)
}
return bills, nil
}
+9 -2
View File
@@ -21,10 +21,11 @@ type DashboardService struct {
repo DashboardRepo repo DashboardRepo
recurrSvc *RecurringService recurrSvc *RecurringService
patrimony PatrimonySource patrimony PatrimonySource
billSvc *CreditBillService
} }
func NewDashboardService(repo DashboardRepo, recurrSvc *RecurringService, patrimony PatrimonySource) *DashboardService { func NewDashboardService(repo DashboardRepo, recurrSvc *RecurringService, patrimony PatrimonySource, billSvc *CreditBillService) *DashboardService {
return &DashboardService{repo: repo, recurrSvc: recurrSvc, patrimony: patrimony} return &DashboardService{repo: repo, recurrSvc: recurrSvc, patrimony: patrimony, billSvc: billSvc}
} }
func (s *DashboardService) Get(ctx context.Context, month string) (*model.DashboardData, error) { func (s *DashboardService) Get(ctx context.Context, month string) (*model.DashboardData, error) {
@@ -95,6 +96,11 @@ func (s *DashboardService) Get(ctx context.Context, month string) (*model.Dashbo
return nil, err return nil, err
} }
currentBills, _ := s.billSvc.currentBillsForDashboard(ctx)
if currentBills == nil {
currentBills = []model.CreditBill{}
}
return &model.DashboardData{ return &model.DashboardData{
Month: month, Month: month,
TotalIncome: income, TotalIncome: income,
@@ -106,5 +112,6 @@ func (s *DashboardService) Get(ctx context.Context, month string) (*model.Dashbo
RecentTransactions: recent, RecentTransactions: recent,
PendingRecurring: pending, PendingRecurring: pending,
PendingIncomeRecurrings: pendingIncome, PendingIncomeRecurrings: pendingIncome,
CurrentBills: currentBills,
}, nil }, nil
} }
+3 -1
View File
@@ -31,8 +31,10 @@ type mockPatrimony struct{}
func (m *mockPatrimony) TotalPatrimony(_ context.Context) (float64, error) { return 0, nil } func (m *mockPatrimony) TotalPatrimony(_ context.Context) (float64, error) { return 0, nil }
func newDashboardSvc(income, expenses float64) *service.DashboardService { func newDashboardSvc(income, expenses float64) *service.DashboardService {
repo := &mockAccountRepo{}
recurrSvc := service.NewRecurringService(newMockRecurring(nil), &mockTxRepo{}) recurrSvc := service.NewRecurringService(newMockRecurring(nil), &mockTxRepo{})
return service.NewDashboardService(&mockDashboardRepo{income: income, expenses: expenses}, recurrSvc, &mockPatrimony{}) billSvc := service.NewCreditBillService(&mockCreditBillRepo{}, repo)
return service.NewDashboardService(&mockDashboardRepo{income: income, expenses: expenses}, recurrSvc, &mockPatrimony{}, billSvc)
} }
func TestDashboard_SavingsPct_40(t *testing.T) { func TestDashboard_SavingsPct_40(t *testing.T) {
+24 -1
View File
@@ -2,6 +2,19 @@ import { defineStore } from 'pinia'
import { ref } from 'vue' import { ref } from 'vue'
import { api } from '@/services/api' import { api } from '@/services/api'
export interface CreditBill {
id: number
account_id: number
account_name: string
period_start: string
period_end: string
due_date: string
total: number
paid: boolean
paid_at?: string
payment_account_id?: number
}
export interface Account { export interface Account {
id: number id: number
name: string name: string
@@ -10,6 +23,9 @@ export interface Account {
balance: number balance: number
yield_type: 'none' | 'cdi' | 'variable' yield_type: 'none' | 'cdi' | 'variable'
last_yield_date?: string last_yield_date?: string
closing_day?: number
due_day?: number
current_bill?: CreditBill
created_at: string created_at: string
updated_at: string updated_at: string
} }
@@ -19,6 +35,8 @@ export interface AccountInput {
type: string type: string
initial_balance: number initial_balance: number
yield_type: string yield_type: string
closing_day?: number | null
due_day?: number | null
} }
export const useAccountsStore = defineStore('accounts', () => { export const useAccountsStore = defineStore('accounts', () => {
@@ -56,5 +74,10 @@ export const useAccountsStore = defineStore('accounts', () => {
accounts.value = accounts.value.filter((a) => a.id !== id) accounts.value = accounts.value.filter((a) => a.id !== id)
} }
return { accounts, loading, error, fetchAll, create, update, remove } async function payBill(billId: number, paymentAccountId?: number) {
await api.post(`/bills/${billId}/pay`, { payment_account_id: paymentAccountId ?? null })
await fetchAll()
}
return { accounts, loading, error, fetchAll, create, update, remove, payBill }
}) })
+2
View File
@@ -1,6 +1,7 @@
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { ref } from 'vue' import { ref } from 'vue'
import { api } from '@/services/api' import { api } from '@/services/api'
import type { CreditBill } from './accounts'
export interface CategoryTotal { export interface CategoryTotal {
category_id: number | null category_id: number | null
@@ -44,6 +45,7 @@ export interface DashboardData {
recent_transactions: RecentTransaction[] recent_transactions: RecentTransaction[]
pending_recurring: number pending_recurring: number
pending_income_recurrings: PendingIncome[] pending_income_recurrings: PendingIncome[]
current_bills: CreditBill[]
} }
export const useDashboardStore = defineStore('dashboard', () => { export const useDashboardStore = defineStore('dashboard', () => {
+59 -2
View File
@@ -13,7 +13,7 @@ const typeLabels: Record<string, string> = {
credit: 'Cartão de Crédito', credit: 'Cartão de Crédito',
} }
const blank = (): AccountInput => ({ name: '', type: 'checking', initial_balance: 0, yield_type: 'none' }) const blank = (): AccountInput => ({ name: '', type: 'checking', initial_balance: 0, yield_type: 'none', closing_day: null, due_day: null })
const form = ref(blank()) const form = ref(blank())
const editId = ref<number | null>(null) const editId = ref<number | null>(null)
const initialRaw = ref('') const initialRaw = ref('')
@@ -27,11 +27,21 @@ function startEdit(id: number) {
const a = store.accounts.find((x) => x.id === id) const a = store.accounts.find((x) => x.id === id)
if (!a) return if (!a) return
editId.value = id editId.value = id
form.value = { name: a.name, type: a.type, initial_balance: a.initial_balance, yield_type: a.yield_type ?? 'none' } form.value = {
name: a.name, type: a.type, initial_balance: a.initial_balance,
yield_type: a.yield_type ?? 'none',
closing_day: a.closing_day ?? null,
due_day: a.due_day ?? null,
}
initialRaw.value = a.initial_balance.toLocaleString('pt-BR', { minimumFractionDigits: 2 }) initialRaw.value = a.initial_balance.toLocaleString('pt-BR', { minimumFractionDigits: 2 })
formError.value = null formError.value = null
} }
async function payBill(billId: number) {
if (!confirm('Marcar fatura como PAGA?')) return
await store.payBill(billId)
}
function cancelEdit() { function cancelEdit() {
editId.value = null editId.value = null
form.value = blank() form.value = blank()
@@ -90,6 +100,16 @@ function totalBalance() {
<option value="cdi">CDI automático</option> <option value="cdi">CDI automático</option>
<option value="variable">Renda variável</option> <option value="variable">Renda variável</option>
</select> </select>
<template v-if="form.type === 'credit'">
<div class="fc-label fc-acc-form__day-wrap">
Fechamento
<input type="number" v-model.number="form.closing_day" min="1" max="28" class="fc-input fc-acc-form__day" />
</div>
<div class="fc-label fc-acc-form__day-wrap">
Vencimento
<input type="number" v-model.number="form.due_day" min="1" max="28" class="fc-input fc-acc-form__day" />
</div>
</template>
<input <input
v-model="initialRaw" v-model="initialRaw"
placeholder="Saldo inicial (ex: 1.500,00)" placeholder="Saldo inicial (ex: 1.500,00)"
@@ -147,6 +167,32 @@ function totalBalance() {
<li v-if="store.accounts.length === 0" class="fc-acc-empty fc-mono"> nenhuma conta cadastrada </li> <li v-if="store.accounts.length === 0" class="fc-acc-empty fc-mono"> nenhuma conta cadastrada </li>
</ul> </ul>
</NeonPanel> </NeonPanel>
<!-- Current bills for credit accounts -->
<template v-for="a in store.accounts.filter(x => x.type === 'credit' && x.current_bill)" :key="'bill-' + a.id">
<NeonPanel :title="`FATURA · ${a.name}`" :variant="a.current_bill!.paid ? undefined : 'danger'">
<div class="fc-bill">
<div class="fc-bill__row">
<span class="fc-mono fc-bill__label">Período</span>
<span class="fc-mono fc-bill__val">{{ a.current_bill!.period_start }} {{ a.current_bill!.period_end }}</span>
</div>
<div class="fc-bill__row">
<span class="fc-mono fc-bill__label">Vencimento</span>
<span class="fc-mono fc-bill__val fc-blink" style="color:var(--fc-red)">{{ a.current_bill!.due_date }}</span>
</div>
<div class="fc-bill__row">
<span class="fc-pixel fc-bill__label" style="font-size:8px">TOTAL FATURA</span>
<span class="fc-mono fc-bill__total" style="color:var(--fc-red); font-size:20px; font-weight:700">
{{ fmt(a.current_bill!.total) }}
</span>
</div>
<div v-if="!a.current_bill!.paid" class="fc-bill__actions">
<button class="fc-btn fc-btn--primary" @click="payBill(a.current_bill!.id)">MARCAR PAGA</button>
</div>
<div v-else class="fc-bill__paid fc-pixel">FATURA PAGA </div>
</div>
</NeonPanel>
</template>
</div> </div>
</template> </template>
@@ -257,6 +303,17 @@ function totalBalance() {
.fc-acc-badge--cdi { background: rgba(34,197,94,.2); color: var(--fc-green); border: 1px solid var(--fc-green); } .fc-acc-badge--cdi { background: rgba(34,197,94,.2); color: var(--fc-green); border: 1px solid var(--fc-green); }
.fc-acc-badge--var { background: rgba(251,191,36,.2); color: var(--fc-gold); border: 1px solid var(--fc-gold); } .fc-acc-badge--var { background: rgba(251,191,36,.2); color: var(--fc-gold); border: 1px solid var(--fc-gold); }
.fc-acc-form__day-wrap { width: 90px; flex-shrink: 0; }
.fc-acc-form__day { width: 100%; }
/* Credit bill widget */
.fc-bill { display: flex; flex-direction: column; gap: var(--fc-space-3); }
.fc-bill__row { display: flex; justify-content: space-between; align-items: baseline; gap: var(--fc-space-2); }
.fc-bill__label { font-size: 10px; color: var(--fc-text-dim); }
.fc-bill__val { font-size: 12px; }
.fc-bill__actions { margin-top: var(--fc-space-2); }
.fc-bill__paid { font-size: 8px; color: var(--fc-green); margin-top: var(--fc-space-2); }
.fc-acc-item__right { .fc-acc-item__right {
display: flex; display: flex;
align-items: center; align-items: center;
+33
View File
@@ -3,6 +3,7 @@ import { ref, computed, onMounted } from 'vue'
import { useDashboardStore } from '@/stores/dashboard' import { useDashboardStore } from '@/stores/dashboard'
import { useGameStore } from '@/stores/game' import { useGameStore } from '@/stores/game'
import { useRecurringStore } from '@/stores/recurring' import { useRecurringStore } from '@/stores/recurring'
import { useAccountsStore } from '@/stores/accounts'
import NeonPanel from '@/components/NeonPanel.vue' import NeonPanel from '@/components/NeonPanel.vue'
import XPBar from '@/components/XPBar.vue' import XPBar from '@/components/XPBar.vue'
import CharacterSprite from '@/components/CharacterSprite.vue' import CharacterSprite from '@/components/CharacterSprite.vue'
@@ -11,6 +12,7 @@ import MonthSwitcher from '@/components/MonthSwitcher.vue'
const dash = useDashboardStore() const dash = useDashboardStore()
const game = useGameStore() const game = useGameStore()
const recurring = useRecurringStore() const recurring = useRecurringStore()
const accountsStore = useAccountsStore()
const currentMonth = ref(new Date().toISOString().slice(0, 7)) const currentMonth = ref(new Date().toISOString().slice(0, 7))
onMounted(() => dash.fetch(currentMonth.value)) onMounted(() => dash.fetch(currentMonth.value))
@@ -61,6 +63,13 @@ const pendingIncome = computed(() => dash.data?.pending_income_recurrings ?? [])
const showIncomeWidget = computed(() => isCurrentMonth.value && pendingIncome.value.length > 0) const showIncomeWidget = computed(() => isCurrentMonth.value && pendingIncome.value.length > 0)
const showIncomeConfirmButtons = computed(() => dayOfMonth <= 5) const showIncomeConfirmButtons = computed(() => dayOfMonth <= 5)
const currentBills = computed(() => dash.data?.current_bills?.filter(b => !b.paid) ?? [])
async function payBill(billId: number) {
await accountsStore.payBill(billId)
await dash.fetch(currentMonth.value)
}
async function confirmIncome(id: number) { async function confirmIncome(id: number) {
await recurring.confirmIncome(id, currentMonth.value) await recurring.confirmIncome(id, currentMonth.value)
await dash.fetch(currentMonth.value) await dash.fetch(currentMonth.value)
@@ -249,6 +258,21 @@ async function markLate(id: number) {
</div> </div>
</div> </div>
</NeonPanel> </NeonPanel>
<!-- Credit card bills -->
<NeonPanel v-if="currentBills.length > 0" title="FATURAS ABERTAS" :variant="'danger'">
<div class="bill-list">
<div v-for="bill in currentBills" :key="bill.id" class="bill-item">
<div class="bill-item__info">
<span class="fc-body bill-item__name">{{ bill.account_name }}</span>
<span class="fc-mono bill-item__due" style="color:var(--fc-red)">vence {{ bill.due_date }}</span>
</div>
<div class="bill-item__right">
<span class="fc-mono bill-item__total" style="color:var(--fc-red);font-weight:700">{{ fmt(bill.total) }}</span>
<button class="fc-btn fc-btn--sm fc-btn--ghost" @click="payBill(bill.id)">PAGAR</button>
</div>
</div>
</div>
</NeonPanel>
</div> </div>
</div> </div>
</template> </template>
@@ -338,6 +362,15 @@ async function markLate(id: number) {
.empty { font-size: 8px; color: var(--fc-text-dim); padding: 8px 0; } .empty { font-size: 8px; color: var(--fc-text-dim); padding: 8px 0; }
/* Bill widget */
.bill-list { display: flex; flex-direction: column; gap: 10px; }
.bill-item { display: flex; justify-content: space-between; align-items: center; gap: var(--fc-space-2); flex-wrap: wrap; }
.bill-item__info { display: flex; flex-direction: column; gap: 3px; }
.bill-item__name { font-size: 13px; font-weight: 500; }
.bill-item__due { font-size: 10px; }
.bill-item__right { display: flex; align-items: center; gap: var(--fc-space-2); }
.bill-item__total { font-size: 15px; }
/* Income widget */ /* Income widget */
.dash-income-panel { margin-bottom: 0; } .dash-income-panel { margin-bottom: 0; }
.dash-alert--salary { background: rgba(255,180,0,.08); border-color: var(--fc-gold); } .dash-alert--salary { background: rgba(255,180,0,.08); border-color: var(--fc-gold); }