further improvements on accounts screen

This commit is contained in:
Lucas Mathews
2024-05-26 23:23:51 +02:00
parent 739fb4d0c6
commit f1d3afb60c
5 changed files with 226 additions and 232 deletions

View File

@@ -1,14 +1,11 @@
# Lucas Mathews - Fontys Student ID: 5023572
# Banking System Dashboard Page
import tkinter as tk
from tkinter import messagebox, ttk
from tkinter import messagebox, ttk
import customtkinter
import json
from config import CONFIG
import os
from config import CONFIG
from connection import logout_client, get_client, update_client, get_accounts, format_balance
# Global variables
email_entry = None
phone_entry = None
address_entry = None
@@ -19,55 +16,56 @@ frame = None
#################
def logout():
response = logout_client() # Call the logout_client function
json_response = response.json() # Convert the response content to JSON
if json_response['success'] == True:
"""Logs out the client and closes the application."""
response = logout_client()
json_response = response.json()
if json_response['success']:
messagebox.showinfo("Logout", "You have been logged out.")
root.destroy()
else:
messagebox.showerror("Logout failed", json_response['message'])
def display_client_info():
global frame # Declare frame as global inside the function
"""Displays the client's information on the dashboard."""
global frame
if frame is not None:
for widget in frame.winfo_children(): # Destroy all widgets in the frame
for widget in frame.winfo_children():
widget.destroy()
else:
frame = customtkinter.CTkFrame(root)
frame.pack(anchor='w', side='left', padx=20, pady=20)
with open('application\\session_data.json', 'r') as f:
session_data = json.load(f)
client_id = session_data['client_id']
client_info = get_client(client_id)
if 'success' in client_info and client_info['success']:
client = client_info['data']
fields = [('Name', 'name'),
('Client ID', 'client_id'),
('Email', 'email'),
('Phone', 'phone_number'),
('Address', 'address'),
('Account Opened', 'opening_timestamp')]
for i, (display_name, key) in enumerate(fields):
value = client.get(key, 'N/A') # Use 'N/A' as the default value if the key is not found
label_key = customtkinter.CTkLabel(frame, text=f"{display_name}: ", font=("Helvetica", 14))
label_value = customtkinter.CTkLabel(frame, text=value, font=("Helvetica", 14))
label_key.grid(row=i, column=0, sticky='e')
label_value.grid(row=i, column=1, sticky='w')
else:
error_label = customtkinter.CTkLabel(root, text="Error: Could not retrieve client information", font=("Helvetica", 14))
error_label.pack(pady=20)
try:
with open('application\\session_data.json', 'r') as f:
session_data = json.load(f)
client_id = session_data['client_id']
client_info = get_client(client_id)
if client_info.get('success'):
client = client_info['data']
fields = [('Name', 'name'), ('Client ID', 'client_id'), ('Email', 'email'), ('Phone', 'phone_number'),
('Address', 'address'), ('Account Opened', 'opening_timestamp')]
for i, (display_name, key) in enumerate(fields):
value = client.get(key, 'N/A')
label_key = customtkinter.CTkLabel(frame, text=f"{display_name}: ", font=("Helvetica", 14))
label_value = customtkinter.CTkLabel(frame, text=value, font=("Helvetica", 14))
label_key.grid(row=i, column=0, sticky='e')
label_value.grid(row=i, column=1, sticky='w')
else:
error_label = customtkinter.CTkLabel(root, text="Error: Could not retrieve client information", font=("Helvetica", 14))
error_label.pack(pady=20)
except Exception as e:
messagebox.showerror("Error", f"Could not retrieve client information: {e}")
edit_button = customtkinter.CTkButton(frame, text="Edit Details", command=edit_details)
edit_button.grid(row=len(fields), column=0, columnspan=2)
def edit_details():
global edit_window, email_entry, phone_entry, address_entry # Declare the variables as global inside the function
"""Opens a new window for editing client details."""
global edit_window, email_entry, phone_entry, address_entry
edit_window = customtkinter.CTkToplevel(root)
edit_window.title("Edit Details")
edit_window.geometry("300x200")
edit_window.attributes('-topmost', True)
email_label = customtkinter.CTkLabel(edit_window, text="Email: ")
email_entry = customtkinter.CTkEntry(edit_window)
@@ -89,57 +87,68 @@ def edit_details():
edit_window.lift()
def save_details():
"""Saves the updated client details."""
global edit_window, email_entry, phone_entry, address_entry
new_email = email_entry.get() if email_entry.get() != '' else None
new_phone = phone_entry.get() if phone_entry.get() != '' else None
new_address = address_entry.get() if address_entry.get() != '' else None
with open('application\\session_data.json', 'r') as f:
session_data = json.load(f)
client_id = session_data['client_id']
if not messagebox.askyesno("Confirmation", "Are you sure you want to update the details?"):
return # If the user clicked 'No', exit the function
result = update_client(client_id, new_email, new_phone, new_address)
if result['success']:
display_client_info()
edit_window.destroy()
try:
with open('application\\session_data.json', 'r') as f:
session_data = json.load(f)
client_id = session_data['client_id']
if not messagebox.askyesno("Confirmation", "Are you sure you want to update the details?"):
return
result = update_client(client_id, new_email, new_phone, new_address)
if result['success']:
display_client_info()
else:
messagebox.showerror("Update Failed", result.get('message', 'Unknown error'))
edit_window.destroy()
except Exception as e:
messagebox.showerror("Error", f"Could not update details: {e}")
def populate_table():
with open('application\\session_data.json', 'r') as f: # Get the client_id from the session data
session_data = json.load(f)
client_id = session_data['client_id']
response = get_accounts(client_id) # Get the accounts for the client
accounts = response['data'] if 'data' in response else []
if not isinstance(accounts, list): # Check if accounts is a list
print(f"Error: Expected a list of accounts, but got {type(accounts)}")
return
for account in accounts: # Populate the table with the accounts
formatted_balance = format_balance(account['balance'])
table.insert('', 'end', values=(account['description'], account['account_id'], formatted_balance, account['account_type']))
"""Populates the accounts table with client accounts."""
try:
with open('application\\session_data.json', 'r') as f:
session_data = json.load(f)
client_id = session_data['client_id']
response = get_accounts(client_id)
accounts = response.get('data', [])
if not isinstance(accounts, list):
raise ValueError(f"Expected a list of accounts, but got {type(accounts)}")
for account in accounts:
formatted_balance = format_balance(account['balance'])
table.insert('', 'end', values=(account['description'], account['account_id'], formatted_balance, account['account_type']))
except Exception as e:
messagebox.showerror("Error", f"Could not populate accounts table: {e}")
def on_account_double_click(event):
"""Handles double-click event on an account in the table."""
try:
selected_account = table.item(table.selection()) # Get the selected account
session = json.load(open('application\\session_data.json', 'r')) # Get the session data
selected_account = table.item(table.selection())
session = json.load(open('application\\session_data.json', 'r'))
account_description = selected_account['values'][0]
os.system(f"python application\\account.py {selected_account['values'][1]} {session['client_id']} \"{account_description}\"")
command = f"python application\\account.py {selected_account['values'][1]} {session['client_id']} \"{account_description}\""
return_code = os.system(command)
if return_code != 0:
print(f"Error: The command failed with return code {return_code}")
except Exception as e:
print(f"Error: {e}")
##############
### Layout ###
##############
root = customtkinter.CTk()
root.title("Luxbank Dashboard")
root.iconbitmap("application/luxbank.ico")
root.geometry("800x350")
if CONFIG["preferences"]["dark_theme"] == "dark": # Check if dark mode is enabled
customtkinter.set_appearance_mode("dark") # Set the style for dark mode
else:
customtkinter.set_appearance_mode("light") # Set the style for light mode
# Set appearance mode based on configuration
customtkinter.set_appearance_mode(CONFIG["preferences"].get("dark_theme", "light"))
# Create a label for the title
welcome_label = customtkinter.CTkLabel(root, text="Welcome to the Luxbank Dashboard!", font=("Helvetica", 24))
@@ -171,11 +180,6 @@ table.pack(fill='both', expand=True)
populate_table()
# Create a scrollbar for the table
scrollbar = ttk.Scrollbar(table_frame, orient='vertical', command=table.yview)
scrollbar.pack(side='right', fill='y')
table.configure(yscrollcommand=scrollbar.set)
table.bind("<Double-1>", on_account_double_click)
root.mainloop()
root.mainloop()