initial commit
This commit is contained in:
114
track_time.py
Normal file
114
track_time.py
Normal file
@@ -0,0 +1,114 @@
|
||||
import sqlite3
|
||||
import os
|
||||
from datetime import date
|
||||
|
||||
def connect_db():
|
||||
"""
|
||||
Connects to the SQLite database file and returns the connection object.
|
||||
If the file does not exist, it prints an error message.
|
||||
"""
|
||||
db_file = 'time_tracker.db'
|
||||
if not os.path.exists(db_file):
|
||||
print(f"Error: Database file '{db_file}' not found. Please run the database creation script first.")
|
||||
return None
|
||||
|
||||
return sqlite3.connect(db_file)
|
||||
|
||||
def add_time_entry():
|
||||
"""
|
||||
Prompts the user for details and adds a new time entry to the database.
|
||||
"""
|
||||
conn = connect_db()
|
||||
if not conn:
|
||||
return
|
||||
|
||||
try:
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Display all active clients for selection
|
||||
cursor.execute('SELECT client_id, client_name FROM clients WHERE active = 1 ORDER BY client_name')
|
||||
active_clients = cursor.fetchall()
|
||||
|
||||
if not active_clients:
|
||||
print("No active clients found. Please add an active client first.")
|
||||
return
|
||||
|
||||
print("\n--- Select a Client ---")
|
||||
for client_id, client_name in active_clients:
|
||||
print(f"{client_id}: {client_name}")
|
||||
print("0: Exit") # Added exit option
|
||||
print("----------------------")
|
||||
|
||||
while True:
|
||||
try:
|
||||
choice = int(input("Enter the ID of the client (or 0 to exit): "))
|
||||
except ValueError:
|
||||
print("Invalid input. Please enter a number.")
|
||||
continue
|
||||
|
||||
if choice == 0:
|
||||
print("Exiting time entry.")
|
||||
return # Exit the function if the user chooses 0
|
||||
|
||||
# Check if the entered ID is in the list of active clients
|
||||
if any(c[0] == choice for c in active_clients):
|
||||
client_id = choice
|
||||
break
|
||||
else:
|
||||
print("Invalid client ID. Please enter a valid ID from the list.")
|
||||
|
||||
print(f"\n--- Add New Time Entry for Client ID {client_id} ---")
|
||||
project = input("Enter project name: ")
|
||||
description = input("Enter a brief description: ")
|
||||
|
||||
# Get the hours and validate it's a number
|
||||
while True:
|
||||
try:
|
||||
hours = float(input("Enter number of hours (e.g., 1.5): "))
|
||||
if hours <= 0:
|
||||
print("Hours must be a positive number.")
|
||||
else:
|
||||
break
|
||||
except ValueError:
|
||||
print("Invalid input. Please enter a number for hours.")
|
||||
|
||||
# Get the date, defaulting to today's date
|
||||
entry_date = input(f"Enter the date (YYYY-MM-DD, default is today: {date.today()}): ")
|
||||
if not entry_date:
|
||||
entry_date = str(date.today())
|
||||
|
||||
# The 'invoiced' field defaults to 0 (false)
|
||||
invoiced = 0
|
||||
|
||||
# Insert the new time entry into the database
|
||||
cursor.execute('''
|
||||
INSERT INTO time_tracking (project, description, invoiced, date, hours, client_id)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
''', (project, description, invoiced, entry_date, hours, client_id))
|
||||
|
||||
conn.commit()
|
||||
print("\nTime entry added successfully!")
|
||||
|
||||
# Fetch and display the newly created entry
|
||||
last_id = cursor.lastrowid
|
||||
cursor.execute('SELECT * FROM time_tracking WHERE entry_id = ?', (last_id,))
|
||||
new_entry = cursor.fetchone()
|
||||
|
||||
print("\n--- New Time Entry Details ---")
|
||||
print(f"ID: {new_entry[0]}")
|
||||
print(f"Project: {new_entry[1]}")
|
||||
print(f"Description: {new_entry[2]}")
|
||||
print(f"Invoiced: {'Yes' if new_entry[3] else 'No'}")
|
||||
print(f"Date: {new_entry[4]}")
|
||||
print(f"Hours: {new_entry[5]:.2f}")
|
||||
print(f"Client ID: {new_entry[6]}")
|
||||
print("------------------------------\n")
|
||||
|
||||
except sqlite3.Error as e:
|
||||
print(f"An error occurred: {e}")
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
add_time_entry()
|
||||
Reference in New Issue
Block a user