report.go130 lines, 4.0 KB
| 1 | package wallet |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "iter" |
| 7 | "strings" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/fiston-user/payments/internal/fx" |
| 11 | "github.com/fiston-user/payments/internal/wallet/walletdb" |
| 12 | "github.com/google/uuid" |
| 13 | ) |
| 14 | |
| 15 | // A StatementLine is one entry on a wallet's statement, and the balance after it. |
| 16 | type StatementLine struct { |
| 17 | PostedAt time.Time |
| 18 | TransactionID uuid.UUID |
| 19 | Description string |
| 20 | Amount int64 // money in is positive, money out negative |
| 21 | Balance int64 |
| 22 | Currency string |
| 23 | } |
| 24 | |
| 25 | // StatementHeader names the columns of Record. |
| 26 | var StatementHeader = []string{"date", "description", "amount", "balance", "currency", "transaction_id"} |
| 27 | |
| 28 | // Record is l as a CSV record: amounts as decimals, in the currency's own |
| 29 | // minor units, and the description as text that's safe in a spreadsheet. |
| 30 | func (l StatementLine) Record() ([]string, error) { |
| 31 | amount, err := fx.Format(l.Amount, l.Currency) |
| 32 | if err != nil { |
| 33 | return nil, err |
| 34 | } |
| 35 | balance, err := fx.Format(l.Balance, l.Currency) |
| 36 | if err != nil { |
| 37 | return nil, err |
| 38 | } |
| 39 | return []string{l.PostedAt.UTC().Format(time.RFC3339), cell(l.Description), amount, balance, |
| 40 | l.Currency, l.TransactionID.String()}, nil |
| 41 | } |
| 42 | |
| 43 | // cell keeps text from being run as a formula when a CSV file is opened in a |
| 44 | // spreadsheet: a cell that starts with =, +, -, @, a tab or a carriage |
| 45 | // return gets a ' in front, which the spreadsheet shows as text. |
| 46 | func cell(s string) string { |
| 47 | if s != "" && strings.ContainsRune("=+-@\t\r", rune(s[0])) { |
| 48 | return "'" + s |
| 49 | } |
| 50 | return s |
| 51 | } |
| 52 | |
| 53 | // statementPage is how many entries a statement reads at a time. |
| 54 | const statementPage = 500 |
| 55 | |
| 56 | // Statement returns acc's entries posted in [since, until), oldest first, |
| 57 | // each with the balance after it. They're read a page at a time, in one |
| 58 | // snapshot: memory stays flat, and the balances add up. |
| 59 | func (s *Store) Statement( |
| 60 | ctx context.Context, acc Account, since, until time.Time, |
| 61 | ) iter.Seq2[StatementLine, error] { |
| 62 | return func(yield func(StatementLine, error) bool) { |
| 63 | fail := func(err error) { yield(StatementLine{}, fmt.Errorf("statement: %w", err)) } |
| 64 | tx, err := s.reports.BeginTx(ctx, snapshot) |
| 65 | if err != nil { |
| 66 | fail(err) |
| 67 | return |
| 68 | } |
| 69 | defer tx.Rollback(ctx) |
| 70 | q := s.q.WithTx(tx) |
| 71 | balance, err := q.BalanceBefore(ctx, walletdb.BalanceBeforeParams{AccountID: acc.ID, Before: since}) |
| 72 | if err != nil { |
| 73 | fail(err) |
| 74 | return |
| 75 | } |
| 76 | page := walletdb.StatementPageParams{ |
| 77 | AccountID: acc.ID, AfterAt: since, Until: until, PageSize: statementPage, |
| 78 | } |
| 79 | for { |
| 80 | entries, err := q.StatementPage(ctx, page) |
| 81 | if err != nil { |
| 82 | fail(err) |
| 83 | return |
| 84 | } |
| 85 | for _, e := range entries { |
| 86 | amount := e.Amount |
| 87 | if e.Direction != acc.NormalBalance { |
| 88 | amount = -amount |
| 89 | } |
| 90 | balance += amount |
| 91 | line := StatementLine{e.CreatedAt, e.TransactionID, e.Description, |
| 92 | amount, balance, acc.Currency} |
| 93 | if !yield(line, nil) { |
| 94 | return // the caller stopped |
| 95 | } |
| 96 | page.AfterAt, page.AfterID = e.CreatedAt, e.ID // the next page starts after this entry |
| 97 | } |
| 98 | if len(entries) < statementPage { |
| 99 | return |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Statement returns the lines of a wallet's statement for the days from |
| 106 | // first to last, both included, in UTC. |
| 107 | func (s *Service) Statement(ctx context.Context, id uuid.UUID, first, last time.Time) ( |
| 108 | iter.Seq2[StatementLine, error], error, |
| 109 | ) { |
| 110 | w, err := s.Wallet(ctx, id) |
| 111 | if err != nil { |
| 112 | return nil, err |
| 113 | } |
| 114 | return s.store.Statement(ctx, w, first, last.AddDate(0, 0, 1)), nil |
| 115 | } |
| 116 | |
| 117 | // TrialBalance returns every ledger account's debits and credits before a |
| 118 | // moment, with a total per currency. |
| 119 | func (s *Store) TrialBalance(ctx context.Context, before time.Time) ([]walletdb.TrialBalanceRow, error) { |
| 120 | lines, err := walletdb.New(s.reports).TrialBalance(ctx, before) |
| 121 | if err != nil { |
| 122 | return nil, fmt.Errorf("trial balance: %w", err) |
| 123 | } |
| 124 | return lines, nil |
| 125 | } |
| 126 | |
| 127 | // TrialBalance is the trial balance before a moment: for us, never for customers. |
| 128 | func (s *Service) TrialBalance(ctx context.Context, before time.Time) ([]walletdb.TrialBalanceRow, error) { |
| 129 | return s.store.TrialBalance(ctx, before) |
| 130 | } |