-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
156 lines (133 loc) · 4.4 KB
/
main.py
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
146
147
148
149
150
151
152
153
154
155
156
import csv
import sys
# Debug = 1 to show debug traces
debug = 0
# Month names array
months = ["January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"]
n_months = 12
# Used files: N26 export csv file and output csv file
n26_csv = "n26-csv-transactions.csv"
_of_balance = "n26_year_balance_"
_of_exp_clas = "n26_year_expenses_classified_"
of_extension = ".csv"
# Column names:
date = "Fecha"
ben = "Beneficiario"
account = "Número de cuenta"
trans = "Tipo de transacción"
ref = "Referencia de pago"
category = "Categoría"
qty_eur = "Cantidad (EUR)"
qty_fc = "Cantidad (Divisa extranjera)"
currency = "Tipo de divisa extranjera"
exc_rate = "Tipo de cambio"
# Expense types
exp_typ = ["Ahorro & Inversión",
"Bares y restaurantes",
"Cajero",
"CASH26",
"Comida & Comestibles",
"Compras",
"Educación",
"Familia & Amigos",
"Gastos profesionales",
"Impuestos y multas",
"Ingresos",
"Invitación a N26",
"Multimedia & Electrónicos",
"Ocio & Entretenimiento",
"Otros",
"Salario",
"Salud y droguería",
"Seguros & Finanzas",
"Spaces",
"Suscripciones & Donaciones",
"Transporte & Coche",
"Viajes & Vacaciones",
"Vivienda & Servicios Públicos"]
exp_track = [[0 for i in range(n_months)] for j in range(len(exp_typ))]
# My N26 space names
n26_spaces = ["Hauptkonto", "Rettunschwimmer", "Kapitalistenschwein"]
def get_year(date):
# Get the year from a YYYY-MM-DD format date
return int(date.split("-")[0])
def get_month(date):
# Get the month from a YYYY-MM-DD format date
m = date.split("-")[1]
if int(m) < 0:
return -1
if int(m) > n_months:
return -1
return int(m)
def get_balance(year, income, expenses):
diff = [0] * n_months
with open(n26_csv, mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file)
line_count = 0
for row in csv_reader:
if debug:
if line_count == 0:
print(f'Column names are {", ".join(row)}')
if get_year(row[date]) == year:
if(float(row[qty_eur]) > 0):
if not (any(word in row[ben] for word in n26_spaces)): # Exclude transfers between N26 spaces.
income[get_month(row[date])-1] += float(row[qty_eur])
elif(float(row[qty_eur]) < 0):
if not (any(word in row[ben] for word in n26_spaces)): # Exclude transfers between N26 spaces.
month = get_month(row[date])
expenses[month-1] += float(row[qty_eur])
classify_expenses(row[category], row[qty_eur], month)
else:
print("ERROR: No data for the year " + str(year))
line_count += 1
if debug:
print(f'Processed {line_count} lines + 1 header line.')
for i in range(n_months):
diff[i] = income[i] + expenses[i]
return diff
def balance2file(file_name, income, expenses, diff):
diff = [0] * n_months
with open(file_name, mode='w') as csv_file:
csv_writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
csv_writer.writerow(["","Anual total"] + months)
csv_writer.writerow(["Income", sum(income)] + income)
csv_writer.writerow(["Expenses", sum(expenses)] + expenses)
for i in range(n_months):
diff[i] = income[i] + expenses[i]
csv_writer.writerow(["Difference", sum(diff)] + diff)
# Todo: Add difference excluding savings & investment
if debug:
print(income)
print(expenses)
print(diff)
def classify_expenses(cat, qty, month):
if debug:
print("Category: " + cat + ", index:" + str(exp_typ.index(cat)))
print("Month: " + months[month - 1] + ", " + str(month))
print("Quantity (EUR): " + str(qty))
exp_track[exp_typ.index(cat)][month - 1] += float(qty)
def expenses2file(file_name):
with open(file_name, mode='w') as csv_file:
csv_writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
csv_writer.writerow(["","Annual total"] + months)
for i in range(len(exp_typ)):
csv_writer.writerow([exp_typ[i], sum(exp_track[i])] + exp_track[i])
def main():
year = int(sys.argv[1])
if debug:
print("Year: " + str(year))
# Output file names
of_balance = _of_balance + str(year) + of_extension
of_exp_clas = _of_exp_clas + str(year) + of_extension
# Arrays for monthly income and expenses
expenses = [0] * n_months
income = [0] * n_months
balance = [0] * n_months
balance = get_balance(year, income, expenses)
balance2file(of_balance, income, expenses, balance)
expenses2file(of_exp_clas)
if __name__ == "__main__":
main()