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]>
94 lines
2.2 KiB
Go
94 lines
2.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"financeiro-carvalho/internal/model"
|
|
)
|
|
|
|
type DashboardRepo interface {
|
|
MonthlySummary(ctx context.Context, month string) (income, expenses float64, err 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)
|
|
}
|
|
|
|
type PatrimonySource interface {
|
|
TotalPatrimony(ctx context.Context) (float64, error)
|
|
}
|
|
|
|
type DashboardService struct {
|
|
repo DashboardRepo
|
|
recurrSvc *RecurringService
|
|
patrimony PatrimonySource
|
|
}
|
|
|
|
func NewDashboardService(repo DashboardRepo, recurrSvc *RecurringService, patrimony PatrimonySource) *DashboardService {
|
|
return &DashboardService{repo: repo, recurrSvc: recurrSvc, patrimony: patrimony}
|
|
}
|
|
|
|
func (s *DashboardService) Get(ctx context.Context, month string) (*model.DashboardData, error) {
|
|
income, expenses, err := s.repo.MonthlySummary(ctx, month)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
byCategory, err := s.repo.ByCategory(ctx, month)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
evolution, err := s.repo.MonthlyEvolution(ctx, month)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
recent, err := s.repo.RecentTransactions(ctx, month)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var savingsPct float64
|
|
if income > 0 {
|
|
savingsPct = (income - expenses) / income * 100
|
|
}
|
|
|
|
statuses, err := s.recurrSvc.MonthlyStatus(ctx, month)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
pending := 0
|
|
for _, s := range statuses {
|
|
if !s.Covered {
|
|
pending++
|
|
}
|
|
}
|
|
|
|
if byCategory == nil {
|
|
byCategory = []model.CategoryTotal{}
|
|
}
|
|
if evolution == nil {
|
|
evolution = []model.MonthEvolution{}
|
|
}
|
|
if recent == nil {
|
|
recent = []model.RecentTransaction{}
|
|
}
|
|
|
|
patrimony, err := s.patrimony.TotalPatrimony(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &model.DashboardData{
|
|
Month: month,
|
|
TotalIncome: income,
|
|
TotalExpenses: expenses,
|
|
SavingsPct: savingsPct,
|
|
TotalPatrimony: patrimony,
|
|
ByCategory: byCategory,
|
|
MonthlyEvolution: evolution,
|
|
RecentTransactions: recent,
|
|
PendingRecurring: pending,
|
|
}, nil
|
|
}
|