Files
time-tracking/edit_time.py
2026-03-02 22:41:22 -06:00

90 lines
3.2 KiB
Python

import sqlite3
import os
def connect_db():
db_file = 'time_tracker.db'
if not os.path.exists(db_file):
print(f"Error: Database file '{db_file}' not found.")
return None
return sqlite3.connect(db_file)
def display_entry_details(entry):
print("\n--- Current Entry Details ---")
print(f"ID: {entry[0]} | Project: {entry[1]} | Date: {entry[4]}")
print(f"Description: {entry[2]}")
print(f"Hours: {entry[5]}")
print("-" * 30)
def edit_time_entry():
conn = connect_db()
if not conn: return
cursor = conn.cursor()
try:
# 1. Select Client
cursor.execute('SELECT client_id, client_name FROM clients ORDER BY client_name')
clients = cursor.fetchall()
print("\n--- Select Client to Edit Time ---")
for cid, name in clients:
print(f"{cid}: {name}")
client_id = input("\nEnter Client ID (or 0 to go back): ")
if client_id == '0' or not client_id: return
while True:
# 2. List Uninvoiced Entries for Client
cursor.execute('''SELECT entry_id, project, description, invoiced, date, hours
FROM time_tracking WHERE client_id = ? AND invoiced = 0
ORDER BY date DESC''', (client_id,))
entries = cursor.fetchall()
if not entries:
print("No uninvoiced entries found for this client.")
break
print(f"\n--- Time Entries for Client {client_id} ---")
for e in entries:
print(f"ID {e[0]}: [{e[4]}] {e[1]} - {e[5]} hrs")
print("0: Back to Main Menu")
entry_choice = input("\nSelect Entry ID to edit: ")
if entry_choice == '0': break
# Find the specific entry
selected_entry = next((e for e in entries if str(e[0]) == entry_choice), None)
if not selected_entry:
print("Invalid Entry ID.")
continue
# 3. Edit Fields one by one
display_entry_details(selected_entry)
# Project
new_project = input(f"Project [{selected_entry[1]}]: ") or selected_entry[1]
# Description
new_desc = input(f"Description [{selected_entry[2]}]: ") or selected_entry[2]
# Date
new_date = input(f"Date [{selected_entry[4]}]: ") or selected_entry[4]
# Hours
new_hours_raw = input(f"Hours [{selected_entry[5]}]: ")
new_hours = float(new_hours_raw) if new_hours_raw else selected_entry[5]
# 4. Update Database
cursor.execute('''UPDATE time_tracking
SET project = ?, description = ?, date = ?, hours = ?
WHERE entry_id = ?''',
(new_project, new_desc, new_date, new_hours, entry_choice))
conn.commit()
print("\nUpdate Successful! New Values:")
print(f"Project: {new_project}\nDescription: {new_desc}\nDate: {new_date}\nHours: {new_hours}")
print("="*30)
except Exception as e:
print(f"An error occurred: {e}")
finally:
conn.close()
if __name__ == "__main__":
edit_time_entry()