Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions expense_tracker/core/transaction_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,43 @@ def get_months_with_expenses(self) -> list[tuple[int, int]]:
result.append((row["year"], row["month"]))
return result

def get_daily_spending_for_year(self, year: int) -> dict[str, float]:
"""
Returns spending keyed by ISO date string for the given year.
Only includes expenses (negative amounts).
"""
start = date(year, 1, 1).isoformat()
end = date(year + 1, 1, 1).isoformat()
rows = self.conn.execute(
"""
SELECT date, SUM(ABS(amount)) as total
FROM transactions
WHERE date >= ? AND date < ?
AND amount < 0
GROUP BY date
""",
(start, end),
)
result: dict[str, float] = {}
for row in rows.fetchall():
result[row["date"]] = row["total"]
return result

def get_years_with_expenses(self) -> list[int]:
"""
Returns sorted descending list of years that have expense data.
Only includes years with negative amounts (expenses).
"""
rows = self.conn.execute(
"""
SELECT DISTINCT CAST(strftime('%Y', date) AS INTEGER) as year
FROM transactions
WHERE amount < 0
ORDER BY year DESC
"""
)
return [row["year"] for row in rows.fetchall()]

def transaction_exists(self, transaction: Transaction) -> bool:
"""Checks if a transaction with the same date, amount, and description already exists in the database.
"""
Expand Down
18 changes: 12 additions & 6 deletions expense_tracker/gui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def on_close():
if dialog.winfo_exists():
dialog.destroy()
self._active_dialog = None
self.transactions_tab.refresh()
self._refresh_active_tab()

dialog.protocol("WM_DELETE_WINDOW", on_close)
dialog.transient(self.master)
Expand All @@ -63,17 +63,23 @@ def on_close():
self.master.wait_window(dialog)

self._active_dialog = None
self.transactions_tab.refresh()
self._refresh_active_tab()

def _on_tab_changed(self, event):
"""Refresh tab content when user switches tabs."""
def _refresh_active_tab(self):
"""Refresh whichever tab is currently visible."""
current_tab = self.notebook.select()
tab_index = self.notebook.index(current_tab)
if tab_index == 1: # Statistics tab
if tab_index == 0:
self.transactions_tab.refresh()
elif tab_index == 1:
self.statistics_tab.refresh()
elif tab_index == 2: # Heatmap tab
elif tab_index == 2:
self.heatmap_tab.refresh()

def _on_tab_changed(self, event):
"""Refresh tab content when user switches tabs."""
self._refresh_active_tab()

def show_transactions_for_date(self, target_date: date):
"""Switch to Transactions tab with date filter applied."""
self.notebook.select(0) # Switch to Transactions tab (index 0)
Expand Down
Loading