This repository has been archived by the owner on Jun 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathameria_csv_parser.go
145 lines (124 loc) · 3.31 KB
/
ameria_csv_parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"bytes"
"encoding/csv"
"fmt"
"io"
"os"
"strings"
"time"
)
const AmeriaBusinessDateFormat = "02/01/2006"
var (
csvHeaders = []string{
"Date",
"Transaction Type",
"Doc.No.",
"Account",
"Credit",
"Debit",
"Remitter/Beneficiary",
"Details",
}
)
type AmeriaBusinessTransaction struct {
Date time.Time
TransactionType string
DocNo string
Account string
Credit MoneyWith2DecimalPlaces
Debit MoneyWith2DecimalPlaces
RemitterBeneficiary string
Details string
}
type AmeriaCsvFileParser struct{}
func (p AmeriaCsvFileParser) ParseRawTransactionsFromFile(
filePath string,
) ([]Transaction, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()
// Read the file into a byte slice
fileData, err := io.ReadAll(file)
if err != nil {
panic(err)
}
// Convert UTF-16 to UTF-8
utf8Data, err := decodeUTF16ToUTF8(fileData)
if err != nil {
panic(err)
}
reader := csv.NewReader(bytes.NewReader(utf8Data))
reader.Comma = '\t' // Assuming the CSV is tab-delimited
reader.LazyQuotes = true // Allow the reader to handle bare quotes
// Read the header row
header, err := reader.Read()
if err != nil {
return nil, fmt.Errorf("failed to read header: %w", err)
}
// Strip BOM from the first header field if present
if len(header) > 0 && strings.HasPrefix(header[0], "\ufeff") {
header[0] = strings.TrimPrefix(header[0], "\ufeff")
}
// Validate header
for i, h := range csvHeaders {
if strings.TrimSpace(strings.Trim(header[i], `"`)) != h {
return nil, fmt.Errorf("unexpected header: got %s, want %s", header[i], h)
}
}
// Parse transactions
var csvTransactions []AmeriaBusinessTransaction
for {
record, err := reader.Read()
if err != nil {
break
}
// Strip quotes from each field
for i := range record {
record[i] = strings.Trim(record[i], `"`)
}
// Parse date
date, err := time.Parse(AmeriaBusinessDateFormat, record[0])
if err != nil {
return nil, fmt.Errorf("failed to parse date: %w", err)
}
// Parse credit and debit
var credit, debit MoneyWith2DecimalPlaces
if err := credit.UnmarshalText([]byte(record[4])); err != nil {
return nil, fmt.Errorf("failed to parse credit: %w", err)
}
if err := debit.UnmarshalText([]byte(record[5])); err != nil {
return nil, fmt.Errorf("failed to parse debit: %w", err)
}
transaction := AmeriaBusinessTransaction{
Date: date,
TransactionType: record[1],
DocNo: record[2],
Account: record[3],
Credit: credit,
Debit: debit,
RemitterBeneficiary: record[6],
Details: record[7],
}
csvTransactions = append(csvTransactions, transaction)
}
// Convert CSV rows to unified transactions and separate expenses from incomes.
transactions := make([]Transaction, len(csvTransactions))
for i, transaction := range csvTransactions {
isExpense := false
amount := transaction.Credit
if amount.int == 0 {
isExpense = true
amount = transaction.Debit
}
transactions[i] = Transaction{
IsExpense: isExpense,
Date: transaction.Date,
Details: transaction.Details,
Amount: amount,
}
}
return transactions, nil
}