diff --git a/backend/go.sum b/backend/go.sum index b8714fe86..0c62ad31c 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -71,6 +71,7 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= diff --git a/backend/ledger/integration_test.go b/backend/ledger/integration_test.go new file mode 100644 index 000000000..593bf93b1 --- /dev/null +++ b/backend/ledger/integration_test.go @@ -0,0 +1,69 @@ +package ledger + +import ( + "context" + "os" + "testing" +) + +// TestIntegration_RobustClient tests against real ledger database +// Run with: LEDGER_DB_URL=postgres://... go test -run Integration -v ./ledger +func TestIntegration_RobustClient(t *testing.T) { + dbURL := os.Getenv("LEDGER_DB_URL") + if dbURL == "" { + t.Skip("LEDGER_DB_URL not set, skipping integration test") + } + + client, err := NewRobustClient(dbURL) + if err != nil { + t.Fatalf("Failed to connect to ledger DB: %v", err) + } + defer client.db.Close() + + ctx := context.Background() + + t.Run("GetAccounts", func(t *testing.T) { + accounts, err := client.GetAccounts(ctx) + if err != nil { + t.Fatalf("GetAccounts failed: %v", err) + } + t.Logf("Found %d accounts", len(accounts)) + for _, a := range accounts[:min(5, len(accounts))] { + t.Logf(" %s: %s (%.2f)", a.Code, a.Name, a.Balance) + } + }) + + t.Run("GetBalanceSheet", func(t *testing.T) { + bs, err := client.GetBalanceSheet(ctx, "2026-01") + if err != nil { + t.Fatalf("GetBalanceSheet failed: %v", err) + } + t.Logf("Assets: %.2f, Liabilities: %.2f, Equity: %.2f", + bs.TotalAssets, bs.TotalLiabilities, bs.TotalEquity) + }) + + t.Run("GetIncomeStatement", func(t *testing.T) { + is, err := client.GetIncomeStatement(ctx, "2026-01") + if err != nil { + t.Fatalf("GetIncomeStatement failed: %v", err) + } + t.Logf("Revenue: %.2f, Expenses: %.2f, Net Income: %.2f", + is.TotalRevenue, is.TotalExpense, is.NetIncome) + }) + + t.Run("GetMomsReport", func(t *testing.T) { + moms, err := client.GetMomsReport(ctx, "2026-01") + if err != nil { + t.Fatalf("GetMomsReport failed: %v", err) + } + t.Logf("Moms Ut: %.2f, Moms Att Betala: %.2f", + moms.MomsUt, moms.MomsAttBetala) + }) +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} \ No newline at end of file diff --git a/backend/ledger/robust_client.go b/backend/ledger/robust_client.go index 9046f51e8..7019f4c1b 100644 --- a/backend/ledger/robust_client.go +++ b/backend/ledger/robust_client.go @@ -92,10 +92,10 @@ func (c *RobustClient) GetBalanceSheet(ctx context.Context, period string) (*Bal SUM(CASE WHEN jl.credit IS NOT NULL THEN jl.credit ELSE 0 END), 0) as balance FROM accounts a LEFT JOIN journal_lines jl ON a.id = jl.account_id - LEFT JOIN journal_entries je ON jl.journal_entry_id = je.id AND je.period = $1 AND je.status = 'posted' + LEFT JOIN journal_entries je ON jl.journal_entry_id = je.id AND je.entry_date >= $1::date AND je.entry_date < ($1::date + INTERVAL '1 month') GROUP BY a.id, a.code, a.name, a.account_type ORDER BY a.code - `, period) + `, period+"-01") if err != nil { return nil, fmt.Errorf("query balance sheet: %w", err) } @@ -146,11 +146,11 @@ func (c *RobustClient) GetIncomeStatement(ctx context.Context, period string) (* SUM(CASE WHEN jl.credit IS NOT NULL THEN jl.credit ELSE 0 END), 0) as balance FROM accounts a LEFT JOIN journal_lines jl ON a.id = jl.account_id - LEFT JOIN journal_entries je ON jl.journal_entry_id = je.id AND je.period = $1 AND je.status = 'posted' + LEFT JOIN journal_entries je ON jl.journal_entry_id = je.id AND je.entry_date >= $1::date AND je.entry_date < ($1::date + INTERVAL '1 month') WHERE a.account_type IN ('Revenue', 'Expense') GROUP BY a.id, a.code, a.name, a.account_type ORDER BY a.code - `, period) + `, period+"-01") if err != nil { return nil, fmt.Errorf("query income statement: %w", err) } @@ -200,9 +200,9 @@ func (c *RobustClient) GetMomsReport(ctx context.Context, period string) (*MomsR FROM journal_lines jl JOIN journal_entries je ON jl.journal_entry_id = je.id JOIN accounts a ON jl.account_id = a.id - WHERE je.period = $1 AND je.status = 'posted' + WHERE je.entry_date >= $1::date AND je.entry_date < ($1::date + INTERVAL '1 month') AND a.code LIKE '26%' - `, period).Scan(&report.MomsUt) + `, period+"-01").Scan(&report.MomsUt) if err != nil { return nil, fmt.Errorf("query moms ut: %w", err) }