feat: salvar data original da transação e data de vencimento no cartão

Ao importar fatura de cartão com PaymentDate configurado, a data efetiva
da transação passa a ser o vencimento e a data de compra é preservada em
original_date. Migration 013 adiciona a coluna original_date em transactions.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
This commit is contained in:
2026-05-27 16:25:52 -03:00
co-authored by Claude Sonnet 4.6
parent 9ba3a3678b
commit 2b0aa352d5
7 changed files with 40 additions and 15 deletions
+4 -1
View File
@@ -44,6 +44,9 @@ var m011 string
//go:embed sql/012_category_is_tax.sql
var m012 string
//go:embed sql/013_transaction_original_date.sql
var m013 string
func Run(ctx context.Context, pool *pgxpool.Pool) error {
// Bootstrap: ensure schema_migrations table exists before checking versions.
if _, err := pool.Exec(ctx, `
@@ -55,7 +58,7 @@ func Run(ctx context.Context, pool *pgxpool.Pool) error {
return fmt.Errorf("bootstrap schema_migrations: %w", err)
}
migrations := []string{m001, m002, m003, m004, m005, m006, m007, m008, m009, m010, m011, m012}
migrations := []string{m001, m002, m003, m004, m005, m006, m007, m008, m009, m010, m011, m012, m013}
for i, sql := range migrations {
version := i + 1
var applied bool
@@ -0,0 +1 @@
ALTER TABLE transactions ADD COLUMN IF NOT EXISTS original_date DATE;
+9 -7
View File
@@ -17,13 +17,14 @@ type Transaction struct {
// ImportRow is a parsed-but-not-yet-saved transaction from a file.
type ImportRow struct {
Date string `json:"date"`
Amount float64 `json:"amount"`
Description string `json:"description"`
Type string `json:"type"`
CategoryID *int `json:"category_id,omitempty"`
ExternalID string `json:"external_id,omitempty"` // FITID from OFX
IsDuplicate bool `json:"is_duplicate"`
Date string `json:"date"` // effective date (payment date for credit cards)
OriginalDate string `json:"original_date"` // purchase date from the CSV (empty for non-credit imports)
Amount float64 `json:"amount"`
Description string `json:"description"`
Type string `json:"type"`
CategoryID *int `json:"category_id,omitempty"`
ExternalID string `json:"external_id,omitempty"` // FITID from OFX
IsDuplicate bool `json:"is_duplicate"`
}
// ImportResult is the response after confirming an import.
@@ -44,4 +45,5 @@ type CSVMapping struct {
FieldSeparator string `json:"field_separator"` // "," or ";"
SkipNegative bool `json:"skip_negative"` // skip rows with negative amount (e.g. credit card bill payments)
AllExpenses bool `json:"all_expenses"` // treat all rows as expense (credit card statements)
PaymentDate string `json:"payment_date"` // YYYY-MM-DD; when set, overrides date for all rows (credit card due date)
}
+13 -4
View File
@@ -96,11 +96,20 @@ func ParseCSV(r io.Reader, m model.CSVMapping) ([]model.ImportRow, []string) {
txType = "income"
}
purchaseDate := t.Format("2006-01-02")
effectiveDate := purchaseDate
originalDate := ""
if m.PaymentDate != "" {
effectiveDate = m.PaymentDate
originalDate = purchaseDate
}
rows = append(rows, model.ImportRow{
Date: t.Format("2006-01-02"),
Amount: math.Abs(amt),
Description: desc,
Type: txType,
Date: effectiveDate,
OriginalDate: originalDate,
Amount: math.Abs(amt),
Description: desc,
Type: txType,
})
}
return rows, errs
+7 -3
View File
@@ -62,10 +62,14 @@ func (r *transactionRepo) BulkInsert(ctx context.Context, rows []model.ImportRow
if row.ExternalID != "" {
extID = &row.ExternalID
}
var origDate *string
if row.OriginalDate != "" {
origDate = &row.OriginalDate
}
_, err := tx.Exec(ctx, `
INSERT INTO transactions (date, amount, description, type, source, external_id, category_id)
VALUES ($1, $2, $3, $4, 'import', $5, $6)`,
row.Date, row.Amount, row.Description, row.Type, extID, row.CategoryID)
INSERT INTO transactions (date, original_date, amount, description, type, source, external_id, category_id)
VALUES ($1, $2, $3, $4, $5, 'import', $6, $7)`,
row.Date, origDate, row.Amount, row.Description, row.Type, extID, row.CategoryID)
if err != nil {
return count, err
}