-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
316 lines (244 loc) · 8.05 KB
/
Copy pathmain.py
File metadata and controls
316 lines (244 loc) · 8.05 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
from pymongo import MongoClient
from dotenv import load_dotenv
from datetime import datetime
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
from rich.align import Align
from rich.text import Text
from rich.live import Live
import os
import time
load_dotenv()
console = Console()
client = MongoClient(os.getenv("MONGO_URI"))
db = client[os.getenv("DB_NAME")]
collection = db["transactions"]
def animate_welcome():
colors = ["red", "orange1", "yellow", "green", "cyan", "blue", "magenta"]
title_text = "💸 FINANCE TRACKER CLI"
subtitle_text = "Track your Income • Expense • Savings"
with Live(console=console, refresh_per_second=10, screen=True) as live:
for _ in range(2):
for color in colors:
title = Text(title_text, justify="center", style=f"bold {color}")
subtitle = Text(subtitle_text, justify="center", style="white")
panel = Panel(
Align.center(title + "\n\n" + subtitle),
title=f"[bold {color}]WELCOME[/bold {color}]",
border_style=color,
padding=(2, 6),
)
live.update(panel)
time.sleep(0.12)
time.sleep(0.5)
def add_income():
console.clear()
console.print("[bold cyan]➕ Add Income[/bold cyan]\n")
amount = float(input("Amount: "))
category = input("Category: ")
note = input("Note: ")
data = {
"type": "income",
"amount": amount,
"category": category,
"note": note,
"created_at": datetime.now(),
}
collection.insert_one(data)
console.print("\n[green]✅ Income added successfully[/green]")
input("\nPress Enter to return to menu...")
def add_expense():
console.clear()
console.print("[bold red]➖ Add Expense[/bold red]\n")
amount = float(input("Amount: "))
category = input("Category: ")
note = input("Note: ")
data = {
"type": "expense",
"amount": amount,
"category": category,
"note": note,
"created_at": datetime.now(),
}
collection.insert_one(data)
console.print("\n[green]✅ Expense added successfully[/green]")
input("\nPress Enter to return to menu...")
def view_transactions():
console.clear()
transactions = list(collection.find())
if not transactions:
console.print(
Panel(
"No transactions found",
title="Transactions",
style="red",
)
)
input("\nPress Enter to return to menu...")
return
table = Table(
title="📊 All Transactions",
show_header=True,
header_style="bold cyan",
)
table.add_column("Type", style="bold", width=10)
table.add_column("Amount (₹)", justify="right", width=12)
table.add_column("Category", style="cyan", width=14)
table.add_column("Note", style="white")
table.add_column("Date", style="dim", width=12)
for t in transactions:
tx_type = t.get("type", "").upper()
amount = float(t.get("amount", 0))
category = t.get("category", "-")
note = t.get("note", "-")
created_at = t.get("created_at")
date_str = (
created_at.strftime("%Y-%m-%d") if isinstance(created_at, datetime) else "-"
)
# color logic
if tx_type == "INCOME":
type_text = "[green]INCOME[/green]"
else:
type_text = "[red]EXPENSE[/red]"
table.add_row(
type_text,
f"{amount:.2f}",
category,
note,
date_str,
)
console.print(table)
input("\nPress Enter to return to menu...")
def monthly_summary():
console.clear()
now = datetime.now()
current_month = now.strftime("%Y-%m")
month_title = now.strftime("%B %Y")
total_income = 0.0
total_expense = 0.0
count = 0
for t in collection.find():
created_at = t.get("created_at")
if not isinstance(created_at, datetime):
continue
if created_at.strftime("%Y-%m") != current_month:
continue
amount = float(t.get("amount", 0))
if t.get("type") == "income":
total_income += amount
count += 1
elif t.get("type") == "expense":
total_expense += amount
count += 1
if count == 0:
console.print(
Panel(
f"No transactions found for {month_title}",
title="📅 Monthly Summary",
style="yellow",
)
)
input("\nPress Enter to return to menu...")
return
savings = total_income - total_expense
table = Table(
title=f"📅 Monthly Summary — {month_title}",
header_style="bold cyan",
)
table.add_column("Type", style="bold")
table.add_column("Amount (₹)", justify="right")
table.add_row("Total Income", f"[green]{total_income:.2f}[/green]")
table.add_row("Total Expense", f"[red]{total_expense:.2f}[/red]")
table.add_row(
"Savings",
(
f"[green]{savings:.2f}[/green]"
if savings >= 0
else f"[red]{savings:.2f}[/red]"
),
)
console.print(table)
input("\nPress Enter to return to menu...")
def check_balance():
console.clear()
total_income = 0.0
total_expense = 0.0
count = 0
transactions = collection.find()
for t in transactions:
if "type" not in t or "amount" not in t:
continue
amount = float(t["amount"])
if t["type"] == "income":
total_income += amount
count += 1
elif t["type"] == "expense":
total_expense += amount
count += 1
if count == 0:
console.print(
Panel("No transactions found", title="Balance Summary", style="red")
)
return
balance = total_income - total_expense
table = Table(
title="💰 Balance Summary", show_header=True, header_style="bold cyan"
)
table.add_column("Type", style="bold")
table.add_column("Amount (₹)", justify="right")
table.add_row("Total Income", f"{total_income:.2f}")
table.add_row("Total Expense", f"{total_expense:.2f}")
if balance > 0:
table.add_row("Balance", f"[green]{balance:.2f} (Saving)[/green]")
elif balance < 0:
table.add_row("Balance", f"[red]{balance:.2f} (Loss)[/red]")
else:
table.add_row("Balance", f"[yellow]0.00 (Break-even)[/yellow]")
console.print(table)
input("\nPress Enter to return to menu...")
def show_menu():
console.clear()
header = Text("📋 MAIN MENU", justify="center", style="bold cyan")
menu_table = Table(
show_header=False,
box=None,
padding=(0, 2),
)
menu_table.add_column("Option", style="bold yellow", width=6)
menu_table.add_column("Action", style="bold white")
menu_table.add_row("1️⃣", "Add Income")
menu_table.add_row("2️⃣", "Add Expense")
menu_table.add_row("3️⃣", "View Transactions")
menu_table.add_row("4️⃣", "Monthly Summary")
menu_table.add_row("5️⃣", "Check Balance")
menu_table.add_row("0️⃣", "Exit")
panel = Panel(
Align.center(menu_table),
title="💸 Finance Tracker",
border_style="cyan",
padding=(1, 4),
)
console.print(header)
console.print(panel)
def main():
print(">>> MAIN STARTED <<<")
animate_welcome()
while True:
show_menu()
choice = console.input("\n[bold cyan]👉 Choose an option:[/bold cyan] ")
if choice == "1":
add_income()
elif choice == "2":
add_expense()
elif choice == "3":
view_transactions()
elif choice == "4":
monthly_summary()
elif choice == "5":
check_balance()
elif choice == "0":
console.print("\n[bold green]👋 Goodbye! Thank you [/bold green]")
break
if __name__ == "__main__":
main()